@@ -47,60 +47,40 @@ var ircData = new ircDataProvider('localhost', 27017,'laticus',ready);
47
47
48
48
var genericUserUpdate = function ( nick , mongoUpdate ) {
49
49
if ( dbReady ) {
50
- ircData . getCollection ( function ( error , collection ) {
50
+ ircData . update ( function ( error , modified , result ) {
51
51
if ( error ) console . log ( error ) ;
52
- else {
53
- collection . update ( { "user" : nick } , mongoUpdate , { "upsert" : true } , function ( error , modified , result ) {
54
- if ( error ) console . log ( error ) ;
55
- } ) ;
56
- }
57
- } , 'users' ) ;
52
+ } , 'users' , { "user" : nick } , mongoUpdate , { "upsert" : true } ) ;
58
53
}
59
54
} ;
60
55
61
56
var genericLinkUpdate = function ( linkURL , nick , mongoUpdate ) {
62
57
if ( dbReady ) {
63
- ircData . getCollection ( function ( error , collection ) {
58
+ ircData . update ( function ( error , modified , result ) {
64
59
if ( error ) console . log ( error ) ;
65
- else {
66
- collection . update ( { "url" : linkURL , "user" : nick } , mongoUpdate , { "upsert" : true } , function ( error , modified , result ) {
67
- if ( error ) console . log ( error ) ;
68
- } ) ;
69
- }
70
- } , 'links' ) ;
60
+ } , 'links' , { "url" : linkURL , "user" : nick } , mongoUpdate , { "upsert" : true } ) ;
71
61
}
72
62
} ;
73
63
74
- var genericActivityUpdate = function ( mongoUpdate ) {
64
+ var genericActivityInsert = function ( mongoInsert ) {
75
65
if ( dbReady ) {
76
- ircData . getCollection ( function ( error , collection ) {
66
+ ircData . insert ( function ( error , result ) {
77
67
if ( error ) console . log ( error ) ;
78
- else {
79
- collection . insert ( mongoUpdate , function ( error , modified , result ) {
80
- if ( error ) console . log ( error ) ;
81
- } ) ;
82
- }
83
- } , 'activity' ) ;
68
+ } , 'activity' , mongoInsert ) ;
84
69
}
85
70
} ;
86
71
87
72
var genericTubeUpdate = function ( tubeID , nick , mongoUpdate ) {
88
73
if ( dbReady ) {
89
- ircData . getCollection ( function ( error , collection ) {
74
+ var criteria = { } ;
75
+ if ( nick == "BetterBot" ) {
76
+ criteria = { "id" : tubeID } ; //don't insert unique links if they were echoed by BetterBot, only by unique non-bot users
77
+ }
78
+ else {
79
+ criteria = { "id" : tubeID , "user" : nick } ;
80
+ }
81
+ ircData . update ( function ( error , modified , result ) {
90
82
if ( error ) console . log ( error ) ;
91
- else {
92
- var criteria = { } ;
93
- if ( nick == "BetterBot" ) {
94
- criteria = { "id" : tubeID } ;
95
- }
96
- else {
97
- criteria = { "id" : tubeID , "user" : nick } ;
98
- }
99
- collection . update ( criteria , mongoUpdate , { "upsert" : true } , function ( error , modified , result ) {
100
- if ( error ) console . log ( error ) ;
101
- } ) ;
102
- }
103
- } , 'tubes' ) ;
83
+ } , 'tubes' , criteria , mongoUpdate , { "upsert" : true } ) ;
104
84
}
105
85
} ;
106
86
@@ -125,7 +105,7 @@ var youtubeLink = function(url) {
125
105
}
126
106
127
107
/* IRC CLIENT CONNECTION AND LISTENERS */
128
- var client = new irc . Client ( 'morgan .freenode.net' , 'Laticus-Prime' , { channels : [ '#laticus' ] , retryCount : 5000 , retryDelay : 30000 } ) ;
108
+ var client = new irc . Client ( 'irc .freenode.net' , 'Laticus-Prime' , { channels : [ '#laticus' ] , retryCount : 5000 , retryDelay : 30000 } ) ;
129
109
130
110
client . addListener ( 'message#laticus' , function ( from , message ) {
131
111
var usersUpdate = { "$inc" : { "posts" : 1 , "links" : 0 } , "$setOnInsert" : { "lastLogin" : "unknown" , "online" : true } } ;
@@ -190,49 +170,49 @@ client.addListener('names#laticus', function(nicks) {
190
170
client . addListener ( 'join#laticus' , function ( nick ) {
191
171
var timeStamp = new Date ( ) . toISOString ( ) ;
192
172
var usersUpdate = { "$set" : { "online" : true , "lastLogin" : timeStamp } , "$setOnInsert" : { "posts" : 0 , "links" : 0 } } ;
193
- var activityUpdate = { "user" : nick , "event" : "joined" , "time" : timeStamp } ;
173
+ var activityInsert = { "user" : nick , "event" : "joined" , "time" : timeStamp } ;
194
174
genericUserUpdate ( nick , usersUpdate ) ;
195
- genericActivityUpdate ( activityUpdate ) ;
175
+ genericActivityInsert ( activityInsert ) ;
196
176
} ) ;
197
177
198
178
client . addListener ( 'part#laticus' , function ( nick , reason ) {
199
179
var timeStamp = new Date ( ) . toISOString ( ) ;
200
180
var usersUpdate = { "$set" : { "online" : false } , "$setOnInsert" : { "posts" : 0 , "links" : 0 , "lastLogin" : "unknown" } } ;
201
- var activityUpdate = { "user" : nick , "event" : "parted" , "reason" : reason , "time" : timeStamp } ;
181
+ var activityInsert = { "user" : nick , "event" : "parted" , "reason" : reason , "time" : timeStamp } ;
202
182
genericUserUpdate ( nick , usersUpdate ) ;
203
- genericActivityUpdate ( activityUpdate ) ;
183
+ genericActivityInsert ( activityInsert ) ;
204
184
} ) ;
205
185
206
186
client . addListener ( 'kick#laticus' , function ( nick , by , reason ) {
207
187
var timeStamp = new Date ( ) . toISOString ( ) ;
208
188
var usersUpdate = { "$set" : { "online" : false } , "$setOnInsert" : { "posts" : 0 , "links" : 0 , "lastLogin" : "unknown" } } ;
209
- var activityUpdate = { "user" : nick , "event" : "kicked" , "by" : by , "reason" : reason , "time" : timeStamp } ;
189
+ var activityInsert = { "user" : nick , "event" : "kicked" , "by" : by , "reason" : reason , "time" : timeStamp } ;
210
190
genericUserUpdate ( nick , usersUpdate ) ;
211
- genericActivityUpdate ( activityUpdate ) ;
191
+ genericActivityInsert ( activityInsert ) ;
212
192
} ) ;
213
193
214
194
client . addListener ( 'quit' , function ( nick , reason ) {
215
195
var timeStamp = new Date ( ) . toISOString ( ) ;
216
196
var usersUpdate = { "$set" : { "online" : false } , "$setOnInsert" : { "posts" : 0 , "links" : 0 , "lastLogin" : "unknown" } } ;
217
- var activityUpdate = { "user" : nick , "event" : "quit" , "reason" : reason , "time" : timeStamp } ;
197
+ var activityInsert = { "user" : nick , "event" : "quit" , "reason" : reason , "time" : timeStamp } ;
218
198
genericUserUpdate ( nick , usersUpdate ) ;
219
- genericActivityUpdate ( activityUpdate ) ;
199
+ genericActivityInsert ( activityInsert ) ;
220
200
} ) ;
221
201
222
202
client . addListener ( 'kill' , function ( nick , reason ) {
223
203
var timeStamp = new Date ( ) . toISOString ( ) ;
224
204
var usersUpdate = { "$set" : { "online" : false } , "$setOnInsert" : { "posts" : 0 , "links" : 0 , "lastLogin" : "unknown" } } ;
225
- var activityUpdate = { "user" : nick , "event" : "killed" , "reason" : reason , "time" : timeStamp } ;
205
+ var activityInsert = { "user" : nick , "event" : "killed" , "reason" : reason , "time" : timeStamp } ;
226
206
genericUserUpdate ( nick , usersUpdate ) ;
227
- genericActivityUpdate ( activityUpdate ) ;
207
+ genericActivityInsert ( activityInsert ) ;
228
208
} ) ;
229
209
230
210
client . addListener ( 'nick' , function ( oldnick , newnick ) {
231
211
var timeStamp = new Date ( ) . toISOString ( ) ;
232
212
var usersUpdate = { "$set" : { "online" : false } , "$setOnInsert" : { "posts" : 0 , "links" : 0 , "lastLogin" : "unknown" } } ;
233
- var activityUpdate = { "user" : oldnick , "event" : "nick" , "oldnick" : oldnick , "newnick" : newnick , "time" : timeStamp } ;
213
+ var activityInsert = { "user" : oldnick , "event" : "nick" , "oldnick" : oldnick , "newnick" : newnick , "time" : timeStamp } ;
234
214
genericUserUpdate ( oldnick , usersUpdate ) ;
235
- genericActivityUpdate ( activityUpdate ) ;
215
+ genericActivityInsert ( activityInsert ) ;
236
216
237
217
usersUpdate = { "$set" : { "online" : true , "lastLogin" : timeStamp } , "$setOnInsert" : { "posts" : 0 , "links" : 0 } } ;
238
218
genericUserUpdate ( newnick , usersUpdate ) ;
@@ -291,8 +271,18 @@ app.post('/auth/', cors(corsOptions), function(req, res, next){
291
271
if ( dbReady ) {
292
272
var user = req . body . user , pass = req . body . pass ;
293
273
console . log ( user + ":" + pass ) ;
294
- crypto . randomBytes ( 48 , function ( ex , buf ) {
295
- res . json ( { token : buf . toString ( 'base64' ) } ) ;
274
+ var expireAt = new Date ( Date . now ( ) + ( 60 * 1000 * 2 ) ) ; //15)); //session tokens expire after 15 minutes
275
+
276
+ crypto . randomBytes ( 32 , function ( ex , buf ) {
277
+ var tokenHex = buf . toString ( 'hex' ) ;
278
+ ircData . update ( function ( error , modified , result ) {
279
+ if ( error ) console . log ( error ) ;
280
+ else {
281
+ res . json ( { token : tokenHex } ) ;
282
+ console . log ( modified ) ;
283
+ console . log ( result ) ;
284
+ }
285
+ } , 'sessions' , { "token" : tokenHex } , { "$set" : { "token" : tokenHex , "expireAt" : expireAt } } , { "upsert" : true } ) ;
296
286
} ) ;
297
287
}
298
288
} ) ;
@@ -303,6 +293,30 @@ app.delete('/rem/:collection/:doc_id/:sessionToken/', cors(corsOptions), functio
303
293
var collection = req . params . collection ;
304
294
var id = req . params . doc_id ;
305
295
var token = req . params . sessionToken ;
296
+
297
+ ircData . find ( function ( error , result ) {
298
+ if ( error ) console . log ( error ) ;
299
+ else {
300
+ if ( result . length > 0 && result [ 0 ] . token == token ) {
301
+ res . json ( { "deleted" : false } ) ;
302
+
303
+ //Refactor below to separate function
304
+ var expireAt = new Date ( Date . now ( ) + ( 60 * 1000 * 2 ) ) ; //15)); //session tokens expire after 15 minutes
305
+ ircData . update ( function ( error , modified , result ) {
306
+ if ( error ) console . log ( error ) ;
307
+ else {
308
+ console . log ( modified ) ;
309
+ console . log ( result ) ;
310
+ }
311
+ } , 'sessions' , { "token" : token } , { "$set" : { "expireAt" : expireAt } } , { "upsert" : false } ) ;
312
+
313
+ }
314
+ else {
315
+ console . log ( "Found token: " + result ) ;
316
+ res . json ( { "deleted" : false } ) ;
317
+ }
318
+ } ;
319
+ } , 'sessions' , { "token" : token } ) ;
306
320
/*
307
321
ircData.delete(function(error, nRemoved) {
308
322
if(error) console.log(error);
0 commit comments