-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
220 lines (212 loc) · 7.49 KB
/
server.js
File metadata and controls
220 lines (212 loc) · 7.49 KB
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
// Imports
var sanitize = require('validator').sanitize,
__ = require('underscore')._;
module.exports = function(server) {
var io = require('socket.io').listen(server);
// Data object contract
/**
* data = {
* message: "chat string..",
* type: "chat-me" // chat-me or chat-other
* };
*
* -- or --
*
* data = {
* message: "alert string..",
* type: "alert-normal" // alert-normal or alert-red or alert-green
* };
*/
// JS helper functions
String.prototype.startsWith = function(needle) {
return(this.indexOf(needle) == 0);
};
// Shared state
var users = [],
validCommands,
rooms = [
'python_room',
'javascript_room',
'design_room',
'wash_room'
];
// Respond to client
function respond(client, message, type) {
client.socket.emit('serverMessage', {
message : message,
type : type
});
}
// Respond to room except client himself
function respondToRoom(client, message, type) {
client.socket.broadcast.to(client.room).emit('serverMessage', {
message : message,
type : type
});
}
// Notify room including himself
function notifyRoom(client, message, type) {
io.sockets.in(client.room).emit('serverMessage', {
message : message,
type : type
});
}
// Validate username
function checkUsername(client, username) {
var msg,
hasSpace = (username.indexOf(' ') !== -1);
if(hasSpace) {
msg = '`'+ username +'` : Username can-not have spaces';
respond(client, msg, 'alert-red');
respond(client, 'Choose a username ?', 'alert-green');
return false;
} else if(username.startsWith('/')) {
msg = '`'+ username +'` : Username can-not start with `/`';
respond(client, msg, 'alert-red');
respond(client, 'Choose a username ?', 'alert-green');
return false;
} else if(__.indexOf(users, username) !== -1) {
msg = '`'+ username +'` : Username is already taken';
respond(client, msg, 'alert-red');
respond(client, 'Choose a username ?', 'alert-green');
return false;
}
return true;
}
// Valid Commands
function cmdRooms(client, cmd) {
if(cmd === '/rooms') {
var roomsStr = rooms.join('<br />');
roomsStr = 'Rooms :<br />' + roomsStr;
respond(client, roomsStr, 'alert-normal');
return true;
}
return false;
}
function cmdJoin(client, cmd) {
var split = cmd.split(' ');
if(split[0] === '/join') {
split.splice(0, 1);
roomName = split.join(' ');
if(__.indexOf(rooms, roomName) === -1) {
msg = 'Room `'+ roomName +'` does-not exists !';
respond(client, msg, 'alert-red');
respond(client, 'Checkout chat rooms with `/rooms`', 'alert-normal');
} else {
client.room = roomName;
client.socket.join(roomName);
respond(client, ' ===== Joined `'+ client.room +'` ===== ', 'alert-green');
respondToRoom(client, 'User @'+ client.username +' has joined in', 'alert-normal');
}
return true;
}
return false;
}
function cmdLeave(client, cmd) {
if(cmd === '/leave') {
if(client.room) {
// If still connected
if(client.socket.status) {
respond(client, ' ===== Exited `'+ client.room +'` room ===== ', 'alert-red');
client.socket.leave(client.room);
}
notifyRoom(client, 'User @'+ client.username +' has left the room', 'alert-normal');
client.room = undefined; // Clear room once everyone notified
} else {
// Havn't yet joined any-room
if(client.socket.status) {
respond(client, "You havn't joined any room yet !", 'alert-red');
}
}
return true;
}
return false;
}
function cmdQuit(client, cmd) {
if(cmd === '/quit') {
var index = users.indexOf(client.username);
users.splice(index, 1);
if(client.room) {
cmdLeave(client, '/leave');
}
// If still connected
if(client.socket.status) {
respond(client, ' ===== Bye @'+ client.username +' - Quitting chat ===== ', 'alert-red');
client.socket.disconnect();
}
return true;
}
return false;
}
validCommands = {
'/rooms' : cmdRooms,
'/join' : cmdJoin,
'/leave' : cmdLeave,
'/quit' : cmdQuit
};
// Check for command
function command(client, msg) {
var i, len, valid = false;
if(msg.startsWith('/')) {
var cmds = __.keys(validCommands);
for(i=0, len=cmds.length; i<len; i++) {
var cmd = cmds[i];
if(msg.startsWith(cmd)) {
valid = validCommands[cmd](client, msg);
}
}
if(!valid) {
respond(client, '`'+ msg +'` : Invalid command !', 'alert-red');
}
return true;
}
return false;
}
// Connection
io.sockets.on('connection', function(socket) {
var client = {
username : undefined,
room : undefined,
socket : socket
};
// Add connection status
client.socket.status = true;
// Welcome message
respond(client, 'Welcome to cloud-irc !', 'alert-normal');
respond(client, 'Choose a username ?', 'alert-green');
// Receive from client
client.socket.on('clientMessage', function(data) {
var msg = sanitize(data.message).escape();
// Get him a username
if(!client.username && checkUsername(client, msg)) {
client.username = msg;
users.push(client.username);
respond(client, 'Username `'+ client.username +'` granted !', 'alert-green');
respond(client, 'Checkout chat rooms with `/rooms`', 'alert-normal');
}
else if(client.username){
// Check for command
if(!command(client, msg)) {
// Get him a room
if(!client.room) {
respond(client, 'You must `/join` a room to chat.', 'alert-red');
respond(client, 'Checkout chat rooms with `/rooms`', 'alert-normal');
}
// Have username & inside a room
else {
// Acknowledge user
respond(client, data.message, 'chat-me');
// Broadcast to the room
data.message = '@' + client.username + ' : ' + data.message;
respondToRoom(client, data.message, 'chat-other');
}
}
}
});
// Disconnect
client.socket.on('disconnect', function() {
client.socket.status = false;
cmdQuit(client, '/quit'); // Fire /quit
});
});
};