-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
59 lines (55 loc) · 1.73 KB
/
commands.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
module.exports = function(msg, user, socket, db) {
var m = require('./message.js')
, c = require('./channel.js')
, e = require('./email.js')
, password = require('password');
var helpText = "<div> \
<div><strong>/help</strong></div> \
<div><strong>/adduser</strong> username email</div> \
</div>";
var match = msg.value.match(/^\/.*\s?$/);
if(match != null){
try {
switch (msg.value.match(/^(\/\w*)(.*|\n).*\s?$/)[1]) {
case "/join":
var channel = msg.value.match(/^\/\w*[ ](\w*)\s?$/)[1]
if(!c.hasUser(channel, user)) {
socket.emit('join', channel);
c.join(channel, socket, db);
} else {
emitMessage("Already in channel " + channel);
}
break;
case "/adduser":
var pattern = msg.value.match(/^\/\w*[ ](\w*)[ ]([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\s?$/i);
var name = pattern[1]
var email = pattern[2]
try {
db.collection('users', function(err, collection) {
collection.save({name: name, email: email}, function() {
emitMessage('Added new user.');
e.sendWelcome(name, email, password(3));
});
});
} catch(e) {
emitMessage('Could not save user!');
console.log('Could not save user!.' + e);
}
break;
case "/help":
default:
emitMessage(helpText);
}
} catch(e) {
console.log('Exception throw processing command.', msg, e);
emitMessage("Exception thrown while processing your command. <br/>" + helpText);
}
return true;
} else {
return false;
}
function emitMessage (value) {
console.log('channel: ' + msg.channel, 'name: help', 'message: ' + value);
socket.emit('new message', new m.Message(msg.channel, 'help', "<br/>" + value, new Date()));
}
};