-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
315 lines (280 loc) · 6.79 KB
/
index.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
var port = 9399;
var http = require('http');
var net = require('net');
var server = net.createServer(handleNewSocket).listen(port);
// Batman modules
var User = require('./modules/user');
var Room = require('./modules/room');
/**
* Handle an incoming socket connection
* @param socket
*/
function handleNewSocket(socket) {
var user = User.createUser(socket);
user.notify('Welcome to the XYZ chat server');
/**
* Incoming socket
*/
user.socket.on('data', function(data) {
handleIncomingData(user, data);
});
/**
* Socket abruptly ends
*/
user.socket.on('end', function() {
userDisconnected(user);
});
// Login the user
getLogin(user);
}
/**
* Ask the user for their username
* @param user
*/
function getLogin(user) {
user.notify('Login Name?');
}
/**
* Welcome a new user with a message
* @param user
*/
function welcomeUser(user) {
user.notify('Welcome ' + user.getName() + '!');
}
/**
* Parse incoming data from the client and check for commands, or distribute the message
* @param user Speaking user
* @param data Text from user
*/
function handleIncomingData(user, data) {
var text = cleanBuffer(data);
if(!user.getName()) {
if(attemptLogin(user, text)) {
welcomeUser(user);
} else {
getLogin(user);
}
return;
}
if(text.charAt(0) !== '/') {
// Not a command
distributeMessage(user, text);
} else {
// is a command
handleCommand(user, text);
}
}
/**
* Handle a command from the user
* @param user User issuing command
* @param text Command and params
*/
function handleCommand(user, text) {
var command = text.split(' ')[0];
var param = text.split(' ')[1];
switch(command) {
case '/users':
listUsers(user);
break;
case '/w':
sendPrivateMessage(user, param, text);
break;
case '/r':
replyToUser(user, text);
break;
case '/members':
listRoomMembers(user);
break;
case '/rooms':
listRooms(user);
break;
case '/join':
joinRoom(user, param);
break;
case '/leave':
leaveRoom(user);
break;
case '/create':
createRoom(user, param);
break;
case '/quit':
userExit(user);
break;
default:
unknownCommand(user, command);
break;
}
}
/**
* Attempt a login by checking for free username
* @param user
* @param text
* @returns boolean If login was successful
*/
function attemptLogin(user, text) {
if(!/^[a-zA-Z0-9_]+$/.test(text)) {
user.notify('Sorry, name contains illegal characters.');
return false;
}
if(User.getUserByName(text)) {
user.notify('Sorry, name taken.');
return false;
}
user.setName(text);
return true;
}
/**
* Distribute a message from one user to other users
* @param user Speaking user
* @param text Text message from user
*/
function distributeMessage(user, text) {
var msg = user.getName() + ': ' + text;
var room = Room.getRoomByName(user.getRoom());
if(room && text.trim().length) {
room.distributeMessage(user, msg);
} else if(text.trim().length) {
user.notify('No one can hear you.');
}
}
/**
* Close communication with user
* @param user User to be booted
*/
function userExit(user) {
if(user.getRoom()) {
leaveRoom(user);
}
user.end('BYE');
console.log('Closed socket for user:', user.getName());
}
/**
* Handle user that disconnected unexpectedly
* @param user User who disconnected
*/
function userDisconnected(user) {
if(user.getRoom()) {
leaveRoom(user);
}
User.removeUser(user);
}
/**
* List rooms to a user
* @param user Requesting user
*/
function listRooms(user) {
user.notify(Room.getRoomList());
}
/**
* Join a room as a user
* @param user User joining room
* @param name Room name
*/
function joinRoom(user, name) {
var room;
if(user.getRoom()) {
leaveRoom(user);
}
room = Room.getRoomByName(name);
if(room) {
room.addMember(user);
listRoomMembers(user);
} else {
user.notify('Room \'' + room + '\' does not exist.');
}
}
/**
* Create a new room
* @param user User creating room
* @param name Room name to be created
*/
function createRoom(user, name) {
if(Room.createRoom(name)) {
user.notify('Room \'' + name + '\' created!');
} else {
user.notify('Room already exists.');
}
}
/**
* Remove a user from a room
* @param user User to be removed
*/
function leaveRoom(user) {
var room = Room.getRoomByName(user.getRoom());
if(room) {
room.removeUser(user);
user.leaveRoom();
} else {
user.notify('You are not in a room.');
}
}
/**
* List all active users
* @param user Requesting users
*/
function listUsers(user) {
user.notify(User.getUserList(user));
}
/**
* Send a private message to another user
* @param user User sending message
* @param name User name we are whispering
* @param text Entire message
*/
function sendPrivateMessage(user, name, text) {
var target;
var message = text.split(' ');
message.splice(0, 2);
message = message.join(' ');
target = User.getUserByName(name);
if(target) {
target.sendPrivateMessage(user, message);
} else {
user.notify('Sorry, private message target does not exist.');
}
}
/**
* Reply to the most recent user who private messaged you
* @param user User sending message
* @param text Entire message
*/
function replyToUser(user, text) {
var target;
var message = text.split(' ');
message.splice(0, 1);
message = message.join(' ');
target = User.getUserByName(user.getRecentPM());
if(target) {
target.sendPrivateMessage(user, message);
} else {
user.notify('You have no one to reply to.');
}
}
/**
* List room members
* @param user User to be notified
*/
function listRoomMembers(user) {
var room = Room.getRoomByName(user.getRoom());
if(room) {
user.notify(room.getMemberList(user));
} else {
user.notify('You are not in a room.');
}
}
/**
* Inform the user at this socket that they attempted an unknown command
* @param user User that attempted the command
* @param command The attempted command
*/
function unknownCommand(user, command) {
user.notify('Unknown command: \'' + command + '\'.');
}
/**
* Clean a buffer of newline and CR and convert it to a string
* @param buffer Buffer to clean
* @returns {string} Cleaned buffer as a string
*/
function cleanBuffer(buffer) {
return buffer.toString().replace(/(\r\n|\n|\r|\t)/gm, "");
}