-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
132 lines (113 loc) · 4.86 KB
/
events.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
module.exports = function (app) {
var events = require('events');
var formidable = require("formidable");
var helpers = require(__dirname + '/helpers');
var Datastore = require('nedb'), db = new Datastore({ filename: __dirname + '/.db', autoload: true });
var active = new Datastore({ filename: __dirname + '/.active', autoload: true });
var dateFormat = require('dateformat');
var now = new Date();
// UserManagement Events======================================================================
app.on('registration', function (req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields) {
var password = fields.password;
var username = fields.username;
var passwordData = helpers.saltHashPassword(password, 0);
var data = {
username: username,
salt: passwordData.salt,
pw: passwordData.passwordHash
}
//make sure no empty entires will be made
if (username == '' || password == '') {
res.render('register', { message: ' Invalid Username or Password!' });
return;
}
//make sure that username is unique
db.ensureIndex({ fieldName: 'username', unique: true }, function (err) {
});
db.findOne({ username: fields.username }, function (err, doc) {
if (doc) {
res.render('register', { message: ' User with this Username allready exists!' });
return;
} else {
db.insert(data, function (err, newDoc) {
});
res.redirect('/');
}
});
});
});
app.on('authentication', function (req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields) {
db.findOne({ username: fields.username }, function (err, doc) {
if (doc) {
var passwordData = helpers.saltHashPassword(fields.password, doc.salt);
if (passwordData.passwordHash == doc.pw) {
req.session.regenerate(function () {
req.session.user = fields.username;
res.redirect('/chatroom');
});
}
else {
res.render('login', { message: ' Username or Password invalid!' });
}
}
else {
res.render('login', { message: ' Username or Password invalid!' });
}
});
});
});
app.on('logout', function (req, res) {
req.session.destroy(function (err) {
})
res.redirect('/');
});
// chat events ======================================================================
app.on('addUserToList', function (socket, io) {
active.ensureIndex({
fieldName: 'onlineUser', unique: true,
}, function (err) { });
active.findOne({ onlineUser: socket.username }, function (err, doc) {
if (doc) {
// io.clients[doc.sessionId].disconnected();
if (io.sockets.connected[doc.sessionId]) {
var oldsocket = io.sockets.connected[doc.sessionId];
oldsocket.emit('message', "DISCONNECTED!!!");
oldsocket.disconnect();
}
// console.log('NEU##########################' + io.sockets.connected[socket.id])
active.update({ onlineUser: socket.username }, { $set: { sessionId: socket.id } }, function (err, numReplaced) {
});
// res.render('register', { message: ' User allready!' });
return;
} else {
active.insert({ onlineUser: socket.username, sessionId: socket.id }, function (err, newDoc) {
});
}
});
});
app.on('privateMessage', function (socket, io, rcv, msg) {
// socket.emit('message', rcv + ': ' + msg);
// console.log(socket);
active.findOne({ onlineUser: rcv }, function (err, doc) {
if (doc) {
var rcvsocket = io.sockets.connected[doc.sessionId]
rcvsocket.emit('message', 'PRIVATE ' + socket.username + " at " + dateFormat(now) + ': ' + msg);
return;
}
else {
socket.emit('message', 'User with name ' + rcv + ' was not found or is not online');
}
});
});
app.on('listAllUsers', function (socket) {
active.find({ }, function (err, docs) {
for(i in docs){
socket.emit('message',docs[i].onlineUser);
}
});
});
}