-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
277 lines (258 loc) · 9.67 KB
/
index.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
/* server/index.js
* Main Server File
* Open-source! Free for all
*/
//Node.js Standard Modules
var path = require("path");
var http = require('http');
var fs = require("fs");
//NPM Modules
var express = require("express");
var socketIO = require('socket.io');
var moment = require('moment');
var bodyParser = require('body-parser');
var compression = require('compression');
var markdownIt = require('markdown-it');
//montreus-chat Modules
var rooms = require("./room"); //JSON with Rooms
var db = require("./database");
var errorPage = require("./error-page");
//Module Setup
var app = express();
var server = http.Server(app);
var io = socketIO(server);
var markdown = markdownIt({
html: false,
xhtmlOut: true,
breaks: true,
langPrefix: 'language-',
linkify: true,
typographer: true,
quotes: '“”‘’',
highlight: function() {return '';}
});
var urlencodedParser = bodyParser.urlencoded({ extended: false });
//Init Room List
var publicRooms = [];
for(i = 0; i < rooms.length; i++){
var roomAtIndex = rooms[i];
if(roomAtIndex.public == true){
publicRooms.push(roomAtIndex);
}
}
//Connection Handlers
app.get('/', function(req, res){
res.set('Content-Type', 'text/html');
res.status(200).render("list.ejs", {rooms: publicRooms});
});
app.set("views", path.resolve("views"));
//Uses EJS
var roomRouter = express.Router();
roomRouter.get('/room/:id/', function(req, res,next){
var id = req.params.id;
var roomName;
var roomId;
var roomPassword;
var roomIndex = rooms.indexOf(socket.handshake.query.room);
if(roomIndex != -1){
var room = rooms[roomIndex];
roomName = room.name;
roomId = room.roomId;
roomPassword = room.password;
}
if(roomName == null){
res.status(404).render("error.ejs", {title: 'Montreus Chat', error: "Uh oh! This room sadly doesn't exist."});
}else{
res.set('Content-Type', 'text/html');
if(roomPassword != null){
res.status(200).render("room.ejs", {title: roomName, id: id, isPasswordProtected: true});
}else{
res.status(200).render("room.ejs", {title: roomName, id: id, isPasswordProtected: false});
}
}
});
roomRouter.use(compression({ threshold: 512 }));
app.use(roomRouter);
//Public Folder
var pagesRouter = express.Router();
pagesRouter.use(express.static(__dirname + '/public'));
pagesRouter.use(compression({ threshold: 512 }));
app.use('/', pagesRouter);
//404 Router
app.use(errorPage);
//Sockets
io.on('connection', function(socket){
if(socketConnections().length <= 1024){
socket.username = socket.handshake.query.username;
socket.join(socket.handshake.query.room);
var roomName;
var roomId;
var roomPassword;
var isPublic;
var enteredPassword = socket.handshake.query.password;
//Look for room with the name
var roomIndex = rooms.indexOf(socket.handshake.query.room);
if(roomIndex != -1){
var room = rooms[roomIndex];
roomName = room.name;
roomId = room.roomId;
roomPassword = room.password;
isPublic = room.public;
}
if(!isPublic) {
if(roomPassword + '' == enteredPassword){
db.find(socket.handshake.query.room).then(function(messages){
socket.emit('old messages', messages);
}, function(error){
socket.emit('error event', 'Uh oh! An error ocurred: ' + error.message);
});
}else{
socket.emit('error event', 'Incorrect password, please refresh the page and try again.');
}
}else{
db.find(socket.handshake.query.room).then(function(messages){
socket.emit('old messages', messages)
}, function(error){
socket.emit('error event', 'Uh oh! An error ocurred: ' + error.message);
});
}
socket.on('postName', function(username){
socket.username = username;
});
var socketsConnected = socketConnections(socket.handshake.query.room);
io.in(socket.handshake.query.room).emit('connections', socketsConnected.length + 1);
socket.on('chat message', function(msg){
if(!verifyEmptyness(msg.message)){
var result = processMessage(msg);
if(result.sendToAll === true){
io.in(socket.handshake.query.room).emit('chat message', result);
db.add(result, socket.handshake.query.room);
}else{
socket.emit('chat message', result);
}
}else{
var time = moment(time).format("LT, D/M");
socket.emit('chat message', createResponse('','You may not send empty messages',time, '', true,false, false));
}
});
socket.on('users', function(){
var socketsConnected = socketConnections(socket.handshake.query.room);
io.in(socket.handshake.query.room).emit('connections', socketsConnected.length + 1);
});
socket.on('disconnect', function(){
var socketsConnected = socketConnections(socket.handshake.query.room);
io.in(socket.handshake.query.room).emit('connections', socketsConnected.length + 1);
});
}else{
socket.emit('chat message', createResponse('PM','Sorry, we cannot allow more than 1024 connections in the server',time, ': ', true,false, false));
socket.emit('chat message', createResponse('PM','Disconnecting! Try again later.',time, ': ', true,false, false));
socket.emit('connections', 'You are not connected.');
socket.disconnect();
}
});
var processMessage = function(message){
var time = moment(new Date()).format("LT, D/M");
var response;
if(message.message.length <= 8192){
if(message.message.slice(0,1) !== "/"){
response = createResponse(message.username, message.message,time, ': ', true,true, true);
}else{
var command = firstWord(message.message);
switch(command.toLowerCase()){
case "/help":
response = createResponse('',"Montreus Chat - v2.4.1<br>Available commands:<br>/help - Display help commands<br>/bot-say <message> - Give something for the bot to say!<br>/broadcast <message> - Broadcast a message<br>/version - See the current Montreus Chat version</p>", time, '', false,false, false);
break;
case "/bot-say":
var msg = otherWords(message.message);
if(msg.length <= 0){
response = createResponse('PM','Uh oh! You forgot the message: /bot-say <message>', time, ': ', true, false, true);
}else{
response = createResponse('Chat bot',msg, time, ': ', true, true, true);
}
break;
case "/broadcast":
var msg = otherWords(message.message);
if(msg.length <= 0){
response = createResponse('PM','Uh oh! You forgot the message: /broadcast <message>', time, ': ', true, false, true);
}else{
response = createResponse('BROADCAST',msg, time, ': ', true, true, true);
}
break;
case "/me":
response = createResponse('', 'Montreus Chat - v2.4.1<br>Username: ' + message.username, time, '', false, false, true);
break;
case "/version":
response = createResponse('', 'Montreus Chat - v2.4.1', time, '', false, false, false);
break;
default:
response = createResponse('', 'Invalid command', time, '', false, false, false);
}
}
}else{
response = createResponse('PM', 'Uh oh! Sorry, you cannot send messages longer than 8192 characters.', time, '', false, false, false);
}
return response;
}
var createResponse = function(username, message, time, usernameMessageSperator, processMarkdown, sendToAll, notify){
var response = {
username : html.escape(username),
message : message,
processMarkdown : processMarkdown,
time : time,
usernameMessageSperator : usernameMessageSperator,
sendToAll : sendToAll,
notify: notify
};
if(processMarkdown === true){
msg = markdown.renderInline(message);
response.message = msg;
}
return(response);
};
var firstWord = function(string){
if(string.indexOf(" ") == -1){
return string;
}
return string.substr(0, string.indexOf(" "));
}
var otherWords = function(string){
if(string.indexOf(" ") == -1){
return '';
}
return string.substr(string.indexOf(" ") + 1,string.length);
}
server.listen(3030, function(){
console.log('listening on *:3030');
});
var verifyEmptyness = function(str) {
return (str.length === 0 || !str.trim());
};
function socketConnections(roomId, namespace) {
var res = [];
var ns = io.of(namespace ||"/");
if (ns) {
for (var id in ns.connected) {
if(roomId) {
var index = ns.connected[id].rooms.indexOf(roomId);
if(index !== -1) {
res.push(ns.connected[id]);
}
} else {
res.push(ns.connected[id]);
}
}
}
return res;
}
var html = {
escape: function(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
}