-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
395 lines (318 loc) · 11.1 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
const express = require('express')
const app = express()
const router = express.Router();
const path = require('path');
const moment = require('moment-timezone');
var session = require('express-session');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
app.set('view engine', 'ejs');
const fs = require('fs')
const https = require('https')
const sqlite3 = require('sqlite3')
var http = require('http').Server(app);
var io = require('socket.io')(http);
var crypto = require("crypto");
var sha256 = crypto.createHash("sha256");
var showdown = require('showdown');
const multer = require('multer');
const mysql = require('mysql');
var storage = multer.diskStorage({
destination: function(request, file, cb) {
cb(null, './views/pfps')
},
filename: function(request, file, cb) {
cb(null, request.body.username + '.jpg')
}
})
var upload = multer({ storage: storage })
var converter = new showdown.Converter({ simplifiedAutoLink: 'true' });
const generateRandomAnimalName = require('random-animal-name-generator')
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env['email'],
pass: process.env['password']
}
});
var mailOptions = {
from: process.env['email'],
to: '[email protected]',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
var db2 = new sqlite3.Database('/home/ravi/lightning-chat/chat.db');
app.use(session({
secret: process.env['secret'],
proxy: true,
resave: true,
saveUninitialized: true,
expires: new Date(Date.now() + (30 * 86400 * 1000))
}));
// SQLITE Table Creation
// db2.run(
// `
// -- chatroom definition
// CREATE TABLE chatroom(name varchar(100), author varchar(100), id int primary_key auto_increment, private bool, link varchar(200), filter bool, reset bool);
// -- credentials definition
// CREATE TABLE credentials(id int auto_increment, username varchar(100), password varchar(100), type varchar(50), email varchar(100), verificationcode varchar(100), verified bool, name VARCHAR, author VARCHAR, private BOOL, link VARCHAR, "filter" BOOL, reset BOOL);
// -- joinedRooms definition
// CREATE TABLE joinedRooms(name varchar(100), person varchar(100));
// -- messages definition
// CREATE TABLE messages(text text, user varchar(100), id auto_increment primary_key, chatroom varchar(100), timenumber varchar(1000));
// `
// )
db2.all("SELECT count(*) as num from credentials;", [undefined], async (err, result) => {
console.log("There are currently " + result[0].num + " accounts!")
})
var bodyParser = require('body-parser')
setInterval(function() {
db2.all("SELECT * FROM chatroom where reset = true", async (err, result) => {
result.forEach(item => {
db2.run('delete from messages where chatroom = ?;', [item.name])
})
})
db2.run('delete from messages where chatroom is null;')
db2.run('insert into messages(user, text) values(?,?)', ['ADMIN', 'Welcome to Lightning Chat!'])
db2.run('insert into messages(user, text) values(?,?)', ['ADMIN', 'Messages are deleted every 30 minutes for privacy!'])
}, 1800000);
setInterval(function() {
db2.all("SELECT * FROM messages where chatroom is null order by id asc", async (err, result) => {
if (result[0] == undefined) {
db2.run('insert into messages(user, text) values(?,?)', ['ADMIN', 'Welcome to Lightning Chat!'])
}
})
}, 15000);
app.use(express.static('views'))
app.use(express.static('views/pfps'))
app.use(express.static('views/static'))
app.use(express.static('views/bootstrap-5.1.3-dist/css'))
app.use(express.static('views/bootstrap-5.1.3-dist/js'))
app.use(express.static('.well-known'))
app.use(express.json({
limit: '50mb'
}));
app.use(express.urlencoded({
limit: '50mb',
extended: true
}));
app.use(cookieParser());
app.set('trust proxy', true);
io.on('connection', (socket) => {
socket.on('chat message', async function(msg, author, date) {
var Filter = require('bad-words');
var filter1 = new Filter();
var messages;
if (msg.replace(/\s/g, '') != '') {
try {
messages = {
user: author,
text: filter1.clean(msg),
date: date
}
db2.run('insert into messages(user, text, timenumber) values(?,?,?)', [author, filter1.clean(msg), new Date().getTime().toString()])
}
catch (err) {
messages = {
user: author,
text: msg,
date: date
}
db2.run('insert into messages(user, text, timenumber) values(?,?,?)', [author, msg, new Date().getTime().toString()])
} //db2.run('insert into messages(user, text, timenumber) values(?,?,?)', [author, filter1.clean(msg), new Date().getTime().toString()])
io.emit('message', messages);
}
})
socket.on('chatroom message', async function(msg, author, chatroom, date) {
var text = msg.trim().split(' ');
var profanityFilter = require('simple-profanity-filter');
var Filter = require('bad-words');
var filter1 = new Filter();
var author = author;
if (msg.replace(/\s/g, '') != '') {
var messages = {
user: author,
//text: filter1.clean(msg),
text: filter1.clean(msg),
chatroom: chatroom,
date: date
}
var hasCursed = false;
var filter = true;
db2.all("SELECT * FROM chatroom where name = ?", [chatroom], async (err, result) => {
if (result[0].filter == 0) {
filter = false;
messages = {
user: author,
text: msg,
chatroom: chatroom
}
}
if (msg != ' ' && hasCursed == false) {
if (result[0].filter == 0) {
db2.run('insert into messages(user, text, chatroom, timenumber) values(?,?,?,?)', [author, msg, chatroom, new Date().getTime().toString()])
} else {
//db2.run('insert into messages(user, text, chatroom) values(?,?,?)', [author, filter1.clean(msg), chatroom])
db2.run('insert into messages(user, text, chatroom,timenumber) values(?,?,?,?)', [author, filter1.clean(msg), chatroom, new Date().getTime().toString()])
}
io.emit('chatroommessage', messages);
}
})
}
})
socket.on('typing', (data) => {
if (data.typing == true)
io.emit('display', data)
else
io.emit('display', data)
})
})
app.get('/', function(req, res) {
if (req.session.loggedin == undefined) {
res.redirect('/login')
req.session.originalURL = '/'
req.session.save()
return;
}
if (req.session.name == undefined) {
req.session.name = 'Anonymous'
}
let obj = {}
let html = []
db2.all("SELECT * FROM chatroom where author = ?", [req.session.name], async (err, author1) => {
db2.all("SELECT * FROM messages where chatroom is null order by id asc", async (err, result) => {
db2.all("SELECT * FROM chatroom where private = false", async (err, publicrooms) => {
db2.all("SELECT * FROM joinedRooms where person = ?", [req.session.name], async (err, results) => {
res.render('index', {
item: result,
name1: req.session.name,
loggedin: req.session.loggedin,
name: req.session.name,
other: results,
publicrooms: publicrooms,
author: author1,
host: req.get('host'),
});
})
})
});
})
})
// Get Methods
app.get('/chatrooms', function(req, res) {
require('./GET_METHODS/chatrooms.js')(req, res, db2);
})
app.get('/.well-known/brave-rewards-verification.txt', function(req, res) {
res.sendFile('brave-rewards-verification.txt', {
root: './well-known'
});
})
app.get('/logout', function(req, res) {
require('./GET_METHODS/logout.js')(req, res, db2);
})
app.get('/login', function(req, res) {
if (req.session.loggedin != undefined) {
res.redirect('/')
return;
}
else {
res.render('login', {
host: req.get('host'),
});
}
})
app.get('/signup', function(req, res) {
res.sendFile('signup.html', {
root: './views'
});
})
app.get('/help', function(req, res) {
res.sendFile('help.html', {
root: './views'
});
})
app.get('/account', async function(request, response) {
require('./GET_METHODS/account.js')(request, response, db2);
})
app.get('/forgotpassword', async function(request, response) {
require('./GET_METHODS/forgotpassword.js')(request, response, db2);
})
app.post('/forgotpasswordemail', async function(request, response) {
require('./EDIT_METHODS/forgotpassword.js')(request, response, db2, nodemailer, transporter);
})
app.post('/editpassword', async function(request, response) {
require('./EDIT_METHODS/editpassword.js')(request, response, db2);
})
app.get('/changepassword', async function(request, response) {
require('./GET_METHODS/changepassword.js')(request, response, db2);
})
app.get('/room', async function(req, res) {
require('./GET_METHODS/room.js')(req, res, db2);
})
app.get('/roomdetails', async function(req, res) {
require('./GET_METHODS/roomdetails.js')(req, res, db2);
})
// Create Methods
app.get('/newchatroom', function(req, res) {
res.sendFile('newchatroom.html', {
root: './views'
});
})
app.post('/newaccount', async function(request, response) {
require('./CREATE_METHODS/newaccount.js')(request, response, db2, nodemailer, transporter);
});
// Edit Methods
app.post('/editaccount', upload.single('image'), async function(request, response) {
require('./EDIT_METHODS/editaccount.js')(request, response, db2);
});
// Delete Methods
app.get('/deleteroom', async function(request, response) {
require('./DELETE_METHODS/deleteroom.js')(request, response, db2);
});
app.get('/verifyemail', async function(request, response) {
require('./GET_METHODS/verifyemail.js')(request, response, db2);
});
app.post('/newroom', async function(request, response) {
require('./CREATE_METHODS/newroom.js')(request, response, db2);
});
// Login Authentication
app.post('/auth', async function(request, response) {
var username = request.body.username;
var password = request.body.password;
var finalResult;
if (username && password) {
db2.all('SELECT * FROM credentials', async (err, results) => {
results.forEach(result => {
if (result.username == username && result.password == password) {
finalResult = result;
}
});
if (finalResult == undefined) {
response.send('Incorrect Username and/or Password!');
response.end();
return
}
else if (finalResult.verified == null) {
response.send('Please verify your email! The verification email may take a couple minutes to arrive.')
response.end();
return
}
else {
if (request.body.remember) {
request.session.cookie.maxAge = 30 * 24 * 60 * 60 * 1000; // Cookie expires after 30 days
}
request.session.loggedin = true;
request.session.username = username;
request.session.name = username
response.redirect(request.session.originalURL || '/');
response.end();
return
}
})
} else {
response.send('Please enter Username and Password!');
response.end();
}
});
http.listen(7290, () => console.log('Server is live on port 7290!'))