forked from mohlendo/node_chat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
337 lines (286 loc) · 9.01 KB
/
server.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
HOST = null; // localhost
PORT = 8001;
GLOBAL.DEBUG = true;
var fu = require("./fu");
var sys = require("sys");
var http = require("http");
var redis = require("./redis");
var MESSAGE_BACKLOG = 200;
var CALLBACK_TIMEOUT = 30 * 1000;
var SESSION_TIMEOUT = 2 * CALLBACK_TIMEOUT;
var CHAT_DB_NUMBER = 7;
var DEFAULT_CHANNEL = "default";
var rclient = new redis.Redis(function(r) {
r.select(CHAT_DB_NUMBER);
});
var channels = {};
function createChannel(name) {
var channel = new function () {
var members = {};
var nMembers = 0;
this.name = name;
this.join = function (session, text) {
if (!members[session.id]) {
sys.puts("channel.join(" + session.nick + ", " + this.name + ")");
members[session.id] = { timestamp: new Date(), session: session };
nMembers++;
this.appendMessage(session.nick, "join", text);
}
};
this.leave = function (session, text) {
if (members[session.id]) {
sys.puts("channel.leave(" + session.nick + ", " + this.name + ")");
this.appendMessage(session.nick, "part", text)
delete members[session.id];
nMembers--;
}
};
this.getNumberOfMembers = function() {
return nMembers;
};
this.getMembers = function() {
var nicks = [];
for (sessionId in members) {
if (!members.hasOwnProperty(sessionId)) continue;
nicks.push(members[sessionId].session.nick);
}
return nicks;
};
this.appendMessage = function (nick, type, text) {
rclient.llen(name).addCallback(function (value) {
var m = { index: value
, nick: nick
, type: type // "msg", "join", "part"
, text: text
, timestamp: (new Date()).getTime()
};
rclient.rpush(name, JSON.stringify(m));
for (var sessionId in members) {
if (!members.hasOwnProperty(sessionId)) continue;
members[sessionId].session.deliver([m]);
}
});
};
this.query = function (since, callback) {
rclient.llen(name).addCallback( function(value) {
if(since < value-1) {
rclient.lrange(name, since, -1).addCallback( function(values) {
var matching = [];
if (values) {
for(var i = 0; i < values.length; i++) {
var message = JSON.parse(values[i]);
matching.push(message);
}
}
callback(matching);
});
} else {
callback([]);
}
});
};
};
channels[name] = channel;
return channel;
}
createChannel(DEFAULT_CHANNEL);
var sessions = {};
function createSession (nick) {
if (nick.length > 50) return null;
if (/[^\w_\-^!]/.exec(nick)) return null;
for (var i in sessions) {
var session = sessions[i];
if (session && session.nick === nick) return null;
}
var session = new function() {
this.nick = nick;
this.id = Math.floor(Math.random()*99999999999).toString();
this.channel = channels[DEFAULT_CHANNEL];
this.timestamp = new Date();
// function (messages)
this.callback = null;
// private messages are transient and will be delivered in-band to this session
this.systemMessages = [];
this.poke = function () {
this.timestamp = new Date();
};
this.destroy = function () {
this.channel.leave(this, this.nick + " parted");
delete sessions[this.id];
};
this.switchTo = function (channelName) {
if (this.channel.name !== channelName) {
this.channel.leave(this, "left " + this.channel.name);
this.channel = channels[channelName] || createChannel(channelName);
this.channel.join(this, "enters " + this.channel.name);
}
};
this.query = function (since, callback) {
if (this.systemMessages.length > 0) {
callback(this.systemMessages);
this.systemMessages.clear();
} else {
var s = this;
this.channel.query(since, function(messages) {
if (messages.length > 0) {
callback(messages);
} else {
s.callback = { timestamp: new Date(), callback: callback };
}
});
}
};
this.deliver = function (messages) {
if (this.callback) {
this.callback.callback(messages || []);
this.callback = null;
}
};
this.sendSystemMessage = function (text) {
var message = {
nick: "system",
type: "msg",
text: text,
timestamp: (new Date()).getTime()
};
if (this.callback) {
this.deliver([message]);
} else {
this.systemMessages.push(message);
}
};
this.validate = function() {
var now = new Date();
if (now - this.timestamp > SESSION_TIMEOUT) {
this.destroy();
} else if (this.callback && (now - this.callback.timestamp > CALLBACK_TIMEOUT)) {
this.deliver();
}
};
};
sessions[session.id] = session;
session.channel.join(session, session.nick + " joined");
return session;
}
// interval to kill off old sessions
setInterval(function () {
var now = new Date();
for (var id in sessions) {
if (!sessions.hasOwnProperty(id)) continue;
sessions[id].validate();
}
}, 1000);
fu.listen(PORT, HOST);
fu.get("/", fu.staticHandler("index.html"));
fu.get("/style.css", fu.staticHandler("style.css"));
fu.get("/client.js", fu.staticHandler("client.js"));
fu.get("/jquery-1.2.6.min.js", fu.staticHandler("jquery-1.2.6.min.js"));
fu.get("/who", function (req, res) {
var nicks = [];
for (var id in sessions) {
if (!sessions.hasOwnProperty(id)) continue;
var session = sessions[id];
nicks.push(session.nick);
}
res.simpleJSON(200, { nicks: nicks });
});
fu.get("/join", function (req, res) {
var nick = req.uri.params["nick"];
if (nick == null || nick.length == 0) {
res.simpleJSON(400, {error: "Bad nick."});
return;
}
var session = createSession(nick);
if (session == null) {
res.simpleJSON(400, {error: "Nick in use"});
return;
}
//sys.puts("connection: " + nick + "@" + res.connection.remoteAddress);
res.simpleJSON(200, { id: session.id, nick: session.nick, channel: session.channel.name, nicks: session.channel.getMembers() });
});
fu.get("/part", function (req, res) {
var id = req.uri.params.id;
var session;
if (id && sessions[id]) {
session = sessions[id];
session.destroy();
}
res.simpleJSON(200, { });
});
fu.get("/recv", function (req, res) {
if (!req.uri.params.since) {
res.simpleJSON(400, { error: "Must supply since parameter" });
return;
}
var id = req.uri.params.id;
var session;
if (id && sessions[id]) {
session = sessions[id];
session.poke();
var since = parseInt(req.uri.params.since, 10);
session.query(since, function(messages) {
session.poke();
res.simpleJSON(200, { messages: messages });
});
}
});
var commands = {
"join": function(session, args) { session.switchTo(args[0]); },
"leave": function(session) { session.switchTo(DEFAULT_CHANNEL); },
"whoami": function(session) { session.sendSystemMessage("You are " + session.nick); },
"where": function(session) { session.sendSystemMessage("You are in channel '" + session.channel.name + "'."); },
"channels": function(session) {
var names = [];
for (var name in channels) {
if (!channels.hasOwnProperty(name)) continue;
var label = "'" + name + "' (" + channels[name].getNumberOfMembers() + ")" + (name == session.channel.name ? "*" : "");
names.push(label);
}
session.sendSystemMessage("Available channels are " + names.join(", "));
},
"who": function(session) {
var allNicks = session.channel.getMembers();
// remove own name
var nicks = [];
for (var i in allNicks) {
var nick = allNicks[i];
if (nick !== session.nick)
nicks.push(nick);
}
var text = (nicks.length > 0) ? (nicks.join(", ") + (nicks.length == 1 ? " is" : " are") + " here with you.") :
"You are all alone. Try /channels to find channels with someone to talk to.";
session.sendSystemMessage(text);
},
"help": function(session) {
var cmdNames = [];
for (cmd in commands) {
if (!commands.hasOwnProperty(cmd)) continue;
cmdNames.push("/"+cmd);
}
session.sendSystemMessage("Available commands: " + cmdNames.join(", "));
}
};
fu.get("/send", function (req, res) {
var id = req.uri.params.id;
var text = req.uri.params.text;
var session = sessions[id];
if (!session || !text) {
res.simpleJSON(400, { error: "No such session id" });
return;
}
session.poke();
var match = text.match(/^\/(\S+)\s*(.+)?$/);
var response = {};
if (match) {
sys.puts(match.length + " " + match)
var command = commands[match[1]];
if (command) {
command(session, match[2] ? match[2].split(/\s/) : []);
response["channel"] = session.channel.name;
response["nicks"] = session.channel.getMembers();
}
} else {
session.channel.appendMessage(session.nick, "msg", text);
}
res.simpleJSON(200, response);
});