-
Notifications
You must be signed in to change notification settings - Fork 0
/
paintserver.js
175 lines (159 loc) · 4.88 KB
/
paintserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const io = require("socket.io");
const { Packet } = require("./server_packet");
const Names = require("./names").names;
const fs = require("fs");
const { Room } = require("./room");
const { ServerName } = require("./ServerName");
/* Server class */
this.Server = function (server) {
const socket = io(server);
const rooms = {};
fs.readdir("static/roomcache", function (err) {
if (err) {
fs.mkdirSync("static/roomcache", "0777");
}
});
this.getRooms = function () {
return rooms;
};
this.getPublicRooms = function () {
const pubrooms = {};
for (const key in rooms) {
if (key.indexOf("hidden") !== 0) {
pubrooms[key] = rooms[key];
}
}
return pubrooms;
};
this.uploadImage = function (req, roomid, buf, callback) {
const room = rooms[roomid];
if (!room) {
callback(404);
return;
}
const clientid = req.headers["x-client-id"];
const client = room.clientById(clientid);
if (client) {
callback(403);
return;
}
room.addImage(req, buf, function (id) {
callback(200, id);
});
};
socket.on("connection", function (client) {
const SRVclient = {
info: {
name: '<span style="color: red">' + ServerName + "</span>",
},
listener: client.listener,
};
client.Disconnect = function (reason, room) {
if (reason) {
new Packet().Reject(reason, room).Send(client);
}
if (client.data && client.data.room) {
client.data.room.removeClient(client);
}
new Packet()
.memberLeft(client, client.data.room, reason)
.broadcastToRoom(client.data.room);
client.connected = false;
client.on("message", function () {}); //no clue if this works.
};
//Inline Client class!
client.data = {
room: null,
};
client.info = {
name: Names[Math.floor(Math.random() * Names.length)],
id: client.id,
};
client.on("disconnect", function () {
if (client.data && client.data.room) {
client.data.room.Chat(SRVclient, client.info.name + " has left.");
client.data.room.removeClient(client);
}
new Packet()
.memberLeft(client, client.data.room, "disconnected")
.broadcastToRoom(client.data.room);
});
client.on("message", function (data) {
if (!client.connected) {
return;
}
//Client has initiated the handshake procedure
if ("connect" in data) {
let room;
if (client.data.room) {
client.Disconnect("You are already in a room.");
return;
}
if (!(data.connect.room in rooms)) {
//Create a new room if needed exists
room = new Room(data.connect.room, rooms);
rooms[data.connect.room] = room;
room.addMember(client, true);
room.Chat(
SRVclient,
"Welcome to your new room! If you would like to change your name, simply type /name <name>"
);
} else {
//Otherwise connect to an existing room
room = rooms[data.connect.room];
room.addMember(client, false);
}
room.Chat(SRVclient, client.info.name + " has joined");
}
if ("command" in data) {
//TODO: Validate commands.
const cmd = data.command;
const room = client.data.room;
try {
room.DoCommand(cmd);
new Packet().Set("command", cmd).broadcastToRoom(room);
} catch (e) {
console.log(e);
}
}
if ("chat" in data) {
//TODO: Check that msg is a string.
let msg = data.chat;
const room = client.data.room;
if (!room) return;
const htmlEntities = function (str) {
return String(str)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
};
if (msg.indexOf("/name") === 0) {
const name = msg.substring(msg.indexOf(" ") + 1);
if (name.length > 20) {
new Packet().Chat(SRVclient, "Name too long").Send(client);
} else if (client.data.room.clientByName(name) !== null) {
new Packet().Chat(SRVclient, "Name already taken").Send(client);
} else {
const old = client.info.name;
client.info.name = htmlEntities(name);
new Packet()
.nameChange(client, old, false)
.broadcastToRoom(room, client);
new Packet().nameChange(client, old, true).Send(client);
room.Chat(
SRVclient,
`* <em>${old}</em> has changed name to <em>${client.info.name}</em>`
);
}
} else {
if (msg.length > 600) {
msg = msg.substr(0, 597) + "...";
}
msg = htmlEntities(msg);
room.Chat(client, msg);
}
}
});
});
};