-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.io.js
325 lines (315 loc) · 12.9 KB
/
socket.io.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
/*
BEGIN MESSAGING CODE
*/
var uuid = require("uuid");
var curVersion = "1.4";
var changes = "Your pe-tit-ion was succesful. That word isn't censored as much anymore.";
var exports = module.exports = {
init: function(server, settings) {
var io = require('socket.io')(server);
var cookie = require('cookie'),
Message = require('./models/Message'),
Setting = require('./models/Setting')
Session = require("./models/Session"),
User = require("./models/User"),
badwords = require("./badwords"),
emo = require('emojize');
emo.base('https://github.com/ded/emojize/blob/master/sprite/')
var redisAdapter = require('socket.io-redis');
var redis = require('redis'),
Autolinker = require("autolinker");
var pub = redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST);
pub.auth(process.env.REDIS_PASSWORD);
var sub = redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST, {
detect_buffers: true
});
sub.auth(process.env.REDIS_PASSWORD);
var redisClient = redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST);
redisClient.auth(process.env.REDIS_PASSWORD);
/*io.set('transports', [
'websocket'
]);
*/
io.adapter(redisAdapter({
pubClient: pub,
subClient: sub
}));
var permissions = new Object();
io.on('connection', function(socket) {
var settings = new Object();
Setting.find(function(err, settingsRes) {
if (err) return res.send("Error in app. Section: 2");
for (i in settingsRes) {
var setting = settingsRes[i];
var key = setting.key;
settings[key] = setting;
}
socket.emit("current version", { version: curVersion, notes: changes });
var cookies = cookie.parse(socket.handshake.headers['cookie']);
if (!cookies || !cookies.sid) {
socket.disconnect();
return console.log('unauthorized 1');
}
Session.findOne({
sid: cookies.sid
}).populate('_user').exec(function(err, session) {
if (!session || err || !session._user) {
console.log(session, err, session._user)
socket.disconnect();
return console.log('unauthorized 2');
}
if (!session._user.online) {
session._user.online = true;
/* this is strange code. my original idea was that you could only be logged
in to one place at a time because otherwise there are "status":
online issues but i gave up. this is a bug. if someone knows a fix to disconnect
a client by it's id let me know. it has to work over redis is the main issue.
the hacky fix i use is in the client js. if the client is marked as offline
it sends a ping to fix it. shit fix but it works.
*/
if (session._user.socket) {
//socket.broadcast.to(session._user.socket).disconnect('logged in somewhere else');
}
}
session._user.socket = socket.id;
session._user.save(function(err, user) {
var userStripped = {
name: user.name,
profile: user.profile.substr(user.profile.lastIndexOf(".") + 1),
username: user.username,
chatting: user.chatting,
owner: user.owner,
online: user.online,
_id: user._id
}
if (err) {
console.log(err);
return socket.emit('notify', {
message: "Unable to update your status to online.",
code: 3,
type: 'error',
redirect: "/messages"
});
}
if (session._user.chatting)
io.sockets.emit('update user', userStripped);
else
io.sockets.emit('add user', userStripped);
redisClient.set("user:" + session._user._id, (session._user.chatting) ? 1 : 0); // 1 = chatting; 0 = visitor
});
socket.on('disconnect', function() {
if (session && session._user) {
session._user.online = false;
session._user.save(function(err, user) {
if (err) {
console.log(err);
}
var userStripped = {
name: user.name,
profile: user.profile.substr(user.profile.lastIndexOf(".") + 1),
username: user.username,
chatting: user.chatting,
owner: user.owner,
online: user.online,
_id: user._id
}
redisClient.get("user:" + session._user._id, function(err, reply) {
if (reply == 1) io.sockets.emit('update user', userStripped);
else io.sockets.emit('remove user', userStripped);
});
});
}
});
socket.on("find", function(toFind) {
if (toFind["id"])
var query = {
_id: toFind["id"]
}
else
var query = {
username: { $regex : new RegExp('^' + toFind["username"] + '$', "i") }
}
console.log("query", query);
if (session && session._user && session._user.admin) {
User.findOne(query, function(err, user) {
if (err || !user) {
console.log("unable", err, user);
return socket.emit("notify", {
title: "Unable To Find User",
message: "Unable to get the user. Remember, usernames are case sensitive.",
code: 9,
type: 'error'
});
}
var minutes = Math.floor(Math.floor((new Date() - user.last_seen) / 1000) / 60);
if (minutes > 2880) // 2 days
var fakeFan = " #fakefan";
else
fakeFan = "";
socket.emit('notify', {
title: "Found User",
message: toFind.username + " was last seen " + minutes + " minutes ago." + fakeFan,
type: (fakeFan.length > 0) ? 'error' : 'success'
});
});
}
});
socket.on("toggle chatting", function(toFind) {
if (toFind["id"])
var query = {
_id: toFind["id"]
}
else
var query = {
username: { $regex : new RegExp('^' + toFind["username"] + '$', "i") }
}
if (session && session._user && session._user.admin) {
User.findOne(query, function(err, user) {
if (err || !user) {
console.log("unable", err, user);
return socket.emit("notify", {
message: "Unable to get the user. Remember, usernames are case sensitive.",
code: 9,
type: 'error'
});
}
if (user.owner)
return socket.emit("notify", {
message: "You can't edit the group owner.",
code: 11,
type: 'error'
});
else if (user._id == session._user._id)
return socket.emit("notify", {
message: "You can't edit yourself.",
code: 12,
type: 'error'
});
redisClient.set("user:" + user._id, (user.chatting) ? 0 : 1); // 1 = chatting; 0 = visitor
user.chatting = !user.chatting;
user.save(function(err, user) {
if (err) {
console.log(err);
return socket.emit("notify", {
message: "Unable to get the user.",
code: 10,
redirect: "/messages",
type: 'error'
});
}
var userStripped = {
name: user.name,
profile: user.profile.substr(user.profile.lastIndexOf(".") + 1),
username: user.username,
chatting: user.chatting,
owner: user.owner,
online: user.online,
_id: user._id
}
io.sockets.emit('update user', userStripped);
});
});
}
});
socket.on('send message', function(data) {
redisClient.get("user:" + session._user._id, function(err, reply) {
if (reply != 1 && !session._user.admin && !session._user.owner) // if not chatting and not an owner or admin
return socket.emit('notify', {
message: settings.notchatting.value,
code: 50,
type: 'warning'
});
if (!session._user.owner && !session._user.admin)
for (var word in badwords.bannedWords) {
if (Object.prototype.hasOwnProperty.call(badwords.bannedWords, word)) {
if (data["text"].toLowerCase().indexOf(word.toLowerCase()) > -1) {
return socket.emit('notify', {
message: badwords.bannedWords[word],
code: 51,
type: 'error'
});
}
}
}
var message = new Message({
_user: session._user,
text: data["text"]
});
if (session._user.admin || session._user.owner)
message["image"] = data["image"];
message.save(function(err, message) {
if (err) {
return socket.emit("notify", {
message: "Couldn't send the message.",
code: 52,
redirect: "/messages",
type: 'error'
});
}
Message.populate(message, {
path: "_user"
}, function(err, message) {
if (err) {
return socket.emit('notify', {
message: "Could not get your information.",
code: 53,
redirect: "/messages",
type: 'error'
});
}
var userStripped = {
name: message._user.name,
profile: message._user.profile.substr(message._user.profile.lastIndexOf(".") + 1),
username: message._user.username,
chatting: message._user.chatting,
owner: message._user.owner,
online: message._user.online,
_id: message._user._id
}
var messageStripped = {
text: message.text,
_user: userStripped,
_id: message._id,
created_at: message.created_at
}
messageStripped.text = messageStripped.text.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>').replace(new RegExp('\r?\n', 'g'), '<br />');
if (message._user.owner || message._user.admin)
messageStripped.text = Autolinker.link(messageStripped.text, {
truncate: 25
});
for (ii = 0; ii < badwords.list.length; ii++) {
var word = badwords.list[ii];
if (messageStripped.text.toLowerCase().indexOf(word.toLowerCase()) > -1) {
socket.emit('notify', {
message: "Woah there! Let's try to think of a nicer word than " + word + ".",
code: 54,
type: 'warning'
});
var stars = "";
for (iii = 0; iii < word.length; iii++) {
stars = stars + "*";
}
var regex = new RegExp("(" + word + ")", "gi");
messageStripped.text = messageStripped.text.replace(regex, stars);
}
}
message.text = emo.emojize(message.text);
if (message.image)
messageStripped.image = message.image;
io.sockets.emit('new message', messageStripped);
});
});
});
});
});
socket.on('disconnect', function() {
// disconnected called again
});
});
});
}
};