-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
289 lines (253 loc) · 8.08 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
var fs = require('fs');
var gravatar = require('gravatar');
var _ = require('underscore');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var pubsub = require('pubsub');
var request = require('request');
var cheerio = require('cheerio');
var async = require('async');
var url = require('url');
var app = express();
app.use(favicon(__dirname + '/public/img/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public/html'))); // for index.html
var linkRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var msgFilters = [];
msgFilters.push(function(msgType, item, cb){
if(item.msg){
var msg = replaceUrlWithHtmlLinks(item.msg);
item.msg = msg;
}
cb(null, msgType, item);
});
msgFilters.push(function(msgType, item, cb){
if(item.msg && hasLink(item.msg)){
var rawLink = extractRawLink(item.msg);
if(rawLink.match(/\.(gif|jpg|jpeg|tiff|png)$/i)){
item.imageURL = rawLink;
console.log("Got image link: " + item.imageURL);
}
}
cb(null, msgType, item);
});
msgFilters.push(function(msgType, item, cb){
var msg = item.msg;
if (msg && msg.substring(0, 1) == '/') {
var bits = msg.split(" ");
var cmd = bits[0].substring(1, bits[0].length);
switch (cmd) {
case "me":
console.log("/me command received from " + item.user.nick + ": " + cmd);
var ann = msg.substring(msg.indexOf(' '), msg.length);
var annItem = {announce: ann, user: item.user, when: item.when};
cb(null, msgType, annItem);
break;
case "coffee":
case "foos":
}
} else {
cb(null, msgType, item);
}
});
function addMsg(msg, user){
var item = {msg: msg, user: user, when: currentTime()};
var initialCB = function(cb){cb(null,'msg',item);};
async.waterfall(_.flatten([initialCB,msgFilters]), function(err, msgType, item){
mq.publish(msgType, item);
});
}
var mq = pubsub();
mq.subscribe(function (msgType, item) {
io.sockets.emit(msgType, item);
});
mq.subscribe(function (msgType, item) {
var historyItem = {type: msgType, payload: item};
historyItem.payload.history = true;
history.push(historyItem);
if (history.length > 50) {
history = history.slice(history.length - 50, history.length);
}
fs.writeFile('.history', JSON.stringify(history), function (err) {
if (err)throw err;
});
});
mq.subscribe(function (msgType, item) {
if (msgType !== 'msg') {
return;
}
if (hasLink(item.msg) === false) {
return;
}
var rawLink = extractRawLink(item.msg);
request({proxy: 'http://www-proxy.us.oracle.com:80', uri: rawLink}, function (err, rsp, body) {
var pageTitle = rawLink;
var favIcon = null;
if (!err && rsp.statusCode == 200) {
if(rsp.headers['content-type'].match(/text\/html/) === null){
return;
}
var $ = cheerio.load(body);
pageTitle = $('head title').text();
$('head link').each(function (i, elem) {
var rel = $(this).attr('rel');
if (rel.match(/icon/i) !== null) {
favIcon = $(this).attr('href');
if(favIcon.match(/^http/i) === null){
favIcon = url.resolve(rawLink, favIcon);
}
if(rel.toLowerCase() === 'icon'){
// if we get an exact match break free
return false;
}
if(rel.toLowerCase() === 'shortcut icon'){
return false;
}
}
});
} else {
console.log("Failed to retrieve title and favicon for " + rawLink + " - " + err + " - " + rsp.statusCode);
return;
}
var newlink = {link: rawLink, title: pageTitle, favicon: favIcon, user: item.user, when: currentTime()};
links.unshift(newlink);
io.sockets.emit("updatelinks", links);
fs.writeFile('.linklist', JSON.stringify(links), function (err) {
if (err)throw err;
});
});
});
app.get('/msg/:msg', function (rq, rs) {
console.log('Incoming msg from REST endpoint: ' + rq.params.msg);
addMsg(rq.params.msg, 'system');
rs.send('Thanks');
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.send(err.message);
});
var server = http.Server(app);
var io = require('socket.io').listen(server);
var users = [];
var history = [];
var links = [];
fs.readFile('.linklist',
function (err, data) {
if (err) {
console.log("Failed to load .history file:" + err);
} else {
links = JSON.parse(data);
console.log("Loaded link list, size: " + links.length);
}
}
);
fs.readFile('.history',
function (err, data) {
if (err) {
console.log("Failed to load .history file:" + err);
} else {
history = JSON.parse(data);
console.log("Loaded history, size: " + history.length);
}
}
);
function currentTime() {
var d = new Date();
var hours = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
var minutes = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
var time = hours + ":" + minutes;
return time;
}
function replaceUrlWithHtmlLinks(text) {
return text.replace(linkRegex, "<a href='$1' target='_blank'>$1</a>");
}
function hasLink(text) {
if(text){
return text.match(linkRegex) !== null;
}
return false;
}
function extractRawLink(text) {
return text.match(linkRegex)[0];
}
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function retrieveGravatar(email) {
return gravatar.url("" + email, {s: '50', r: 'pg', d: 'retro'});
}
io.sockets.on('connection', function (socket) {
// first send history
for (i = 0; i < history.length; i++) {
var item = history[i];
socket.emit(item.type, item.payload);
}
socket.emit("updatelinks", links.slice(0,10));
socket.on('adduser', function (username, email) {
if (username === undefined || username === "") {
username = "lazyuser-" + s4();
}
if (email === null) {
email = username; // just to make different default icons get produced
}
var user = {nick: username, gravatar: retrieveGravatar(email)};
socket.user = user;
users.push(user);
io.sockets.emit('updateusers', users);
});
socket.on('disconnect', function () {
nickToRemove = typeof socket.user !== 'undefined' ? socket.user.nick : "";
users = _.reject(users, function (user) {
return user.nick == nickToRemove;
});
io.sockets.emit('updateusers', users);
});
socket.on('coffeecommand', function (data) {
data.user = socket.user;
data.when = currentTime();
if (data.type == 'vote') {
data.voteid = guid();
}
mq.publish('coffeecommand', data);
});
socket.on('msg', function (data) {
addMsg(data.msg,socket.user);
});
socket.on('vote', function (data) {
var item = {
votename: data.votename,
voteid: data.voteid,
vote: data.vote,
user: socket.user,
when: currentTime()
};
mq.publish('voted', item);
});
});
var port = process.env.PORT || 7777;
server.listen(port);
console.log("Server started on port " + port);
module.exports = app;