-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandProcessor.js
56 lines (47 loc) · 1.78 KB
/
commandProcessor.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
var connections = [];
module.exports = function(userInfo) {
var nick = userInfo.nick;
var user;
var registered = userInfo.registered;
var connection = userInfo.connection;
return {
user : function(user) {
user = user;
},
process: function(message) {
var command = message.match(/\w+/g);
console.log(command);
if (command && command.length > 1) {
if (command[0] === 'NICK') {
nick = command[1];
connection.write('Nick changed to ' + nick + '\n');
} else if (command[0] === 'USER') {
registered = true;
user = command[1];
if (!nick) {
nick = user;
}
connections.push(connection);
connection.write('User registered: ' + user + '\n');
} else if (command[0] === 'MESSAGE') {
if (!registered) {
connection.write('User not registered');
return;
}
var delimited = message.match(/"(.*)"/g);
console.log(delimited);
for (var i = 0; i < connections.length; i++) {
console.log(nick + ': ' + delimited[0]);
connections[i].write(nick + ': ' + delimited[0] + '\n');
}
}
} else if (!nick) {
connection.write('Use NICK to set user name\n');
} else if (!registered) {
connection.write('Use USER to register user\n');
} else {
connection.write('Command not recognised\n');
}
}
};
};