-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
297 lines (235 loc) · 7.74 KB
/
app.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
var events = {};
function on(event, func) {events[event] = func;}
function noop() {};
var connections = {};
var welcome = function(connection) {
connection._user = {
id: utils.generateId(),
name: utils.randomName()
// country gets filled in on arrive
}
connections[connection._user.id] = connection;
send("hello", connection, {
user: connection._user,
server: serverId.slice(0,6),
live: live
});
connection.on('data', function(message) {
if (message.length > 1000) return; // 1KB limit
try {
var data = JSON.parse(message);
(events[data._event] || noop)(connection, data, message);
} catch (e) {
log.error("Error parsing message - " + message);
log.error(e);
}
});
connection.on('close', function() {
userLeft(connection._user.id, (connection._timed_out ? "timed out" : "departing"));
});
};
// send a single message to be serialized
var send = function(event, connection, object) {
object._event = event;
if (connection)
connection.write(JSON.stringify(object));
}
// broadcast a single message to be serialized.
// currently: used only by config changes and chat commands.
// so: recipients do not need to know the sender 'id'.
var broadcast = function(event, from, object) {
object._event = event;
var serialized = JSON.stringify(object);
for (id in connections) {
if (id != from)
connections[id].write(serialized);
}
}
// even thinner layer, just shuttle the original message to others
// currently: used to send sender-specific events.
// so: the 'id' field needs to have been set by the sending client.
var rebroadcast = function(connection, data, original) {
var from = connection._user.id;
if (from != data.id) return;
for (id in connections) {
if (id != from)
connections[id].write(original);
}
}
var userLeft = function(id, cause) {
delete connections[id];
broadcast("leave", id, {id: id});
manager.removeUser(id, cause);
}
var setUserHeartbeat = function(id) {
if (connections[id]) {
clearTimeout(connections[id]._heartbeat);
connections[id]._heartbeat = setTimeout(function() {
if (connections[id]) {
log.info("timing out user: " + id);
connections[id]._timed_out = true;
connections[id].close();
}
}, live.death_interval);
}
}
// events
// quickly shuttle mouse events through the system
on('motion', rebroadcast);
on('click', rebroadcast);
on('scroll', rebroadcast);
on('arrive', function(connection, data, original) {
rebroadcast(connection, data, original);
// country is set here, trusted henceforth
connection._user.country = data.country;
manager.addUser(connection, data, false); // new user
setUserHeartbeat(connection._user.id);
});
on('heartbeat', function(connection, data) {
send('heartbeat', connections[data.id], data);
manager.addUser(connection, data, true); // update user
setUserHeartbeat(connection._user.id);
});
on('here', function(connection, data) {
to = connections[data.to];
if (to) {
data.country = to._user.country;
data.id = to._user.id;
send('here', to, data);
}
});
on('rename', function(connection, data) {
if (!data.name) return;
var name = data.name.slice(0,20).trim();
// still send down the name message even if it got rejected
if (!utils.rejectText(data.name))
connection._user.name = name;
send('rename', connection, {name: connection._user.name});
});
on('chat', function(connection, data) {
if (live.chat != "true") return;
var user = connection._user;
var time = Date.now();
manager.isBanned(user.id, function(answer) {
data.message = data.message.toString(); // just in case
if (answer || (data.message == connection._user.lastMessage) || utils.rejectText(data.message))
onBannedChat(user.id, user.name, user.country, data.message);
else
manager.newChat(user.id, time, user.name, user.country, data.message);
connection._user.lastMessage = data.message;
});
});
// client explicitly requested the last X chat messages
on('recent', function(connection, data) {
if (live.chat != "true") return;
manager.recentChats(function(chats) {
if (chats == null) return;
send('recent', connection, {chats: chats});
});
});
var express = require('express'),
http = require('http'),
sockjs = require('sockjs');
var util = require('util');
var utils = require("./lib/utils"),
env = (process.env.NODE_ENV || "development"),
admin = (process.env.IIC_ADMIN == "true");
var config = utils.config(env),
live = (config.live || {}),
port = parseInt(process.env.PORT || config.port || 80);
// full server ID is 12 chars long, only first 6 shared with client
var serverId = (admin ? "admin" : utils.generateId(12)),
log = utils.logger(serverId, config),
manager = require("./lib/manager")(serverId, config.manager, log);
// start everything
var app = express(),
server = http.createServer(app);
var sockets = sockjs.createServer({log: log});
sockets.installHandlers(server, {prefix: '/christmas'});
// used for receiving Slack posts
app.use(express.urlencoded());
app.get('/', function(req, res) {res.send(admin ? "Admin!" : "Up!");});
// this can be used as a separate admin app
if (admin)
require('./lib/admin')(app, config, manager);
// wipe the users clean on process start, the live ones will heartbeat in
else
manager.clearUsers();
// if we have Slack hooks configured, wire them up in the manager
// and enable /christmas listening
if (config.slack.hooks && (config.slack.hooks.length > 0)) {
// allows chat to post to Slack
manager.slack = config.slack;
// allows Slack to post to chat
require('./lib/slack')(app, config, manager);
}
// turn on CORS
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
// start up express server
app.enable('trust proxy');
app.engine('.html', require('ejs').__express);
app.set('view engine', 'html');
server.listen(port, function(){
log.warn("Express " + app.settings.env + " server listening on port " + port);
});
/****************************
handling pub/sub events
*****************************/
// target is 'client' or 'server'
manager.onConfig = function(target, key, value) {
log.warn("live " + target + " change: " + key + " [" + live[key] + " -> " + value + "]");
live[key] = value;
log.warn("live config: " + JSON.stringify(live));
if (target == "client") {
broadcast("config", null, {
key: key,
value: value
});
} else if (target == "server") {
// nothing targeting server right now that requires additional events
}
};
manager.onChat = function(id, time, name, country, message) {
broadcast("chat", null, {
id: id,
time: time,
name: name,
country: country,
message: message
});
};
// this should only happen for someone on this server
var onBannedChat = function(id, name, country, message) {
log.warn("[banned] [" + id + "] [" + country + "] " + name + ": " + message);
if (connections[id]) {
send("chat", connections[id], {
id: id,
name: name,
country: country,
message: message
});
}
};
manager.onCommand = function(command, args) {
log.warn("live command: " + command + " (" + args.join(", ") + ")");
broadcast("command", null, {
command: command,
arguments: args
});
};
// get current starting configuration and wait for users
log.info("Loading config from manager, and beginning.");
manager.loadConfig(function(initLive, err) {
if (err) {
log.error("Couldn't load live config! Crashing myself")
throw "Oh nooooooo";
}
for (var key in initLive)
live[key] = initLive[key];
log.info("Starting up with live config: " + JSON.stringify(live));
sockets.on('connection', welcome);
});