-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (147 loc) · 8.01 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
var net = require('net');
var os = require( 'os' );
var parse = require('irc-message').parse;
var clientStore = require('./clients');
var chanels = require('./channels');
var replies = require('./replies').codes;
var networkInterfaces = os.networkInterfaces( );
var hostkeys = Object.keys(networkInterfaces);
var hostaddress = networkInterfaces[hostkeys[1]][0]["address"] || '127.0.0.1';
var server = net.createServer(function(socket) {
socket.write('Connected to simple-node-irc-server\r\n');
socket.on("data", function(d){
console.log(d.toString());
handleMessage(d, socket);
})
});
var pass = "pwd";
server.listen(6667);
function handleMessage(data, socket){
var lines = data.toString().split("\r\n");
lines.pop(); // remove empty
for (var i = 0; i < lines.length; i++) {
var message = parse(lines[i]);
switch (message.command) {
case "PASS":
if (clientStore.clients[socket.remoteAddress + ":" + socket.remotePort] == undefined){
if (message.params[0] == pass){
clientStore.buildClient(socket);
}
} else {
var nick = clientStore.getClientName(socket).split("!")[0];
socket.write(respondMessage(hostaddress, [replies.ERR_ALREADYREGISTRED, nick], "Already registered"));
}
break;
case "NICK":
if (message.params.length == 0){
socket.write(respondMessage(hostaddress, [replies.ERR_ERRONEUSNICKNAME], "No nickname given"));
} else {
if (clientStore.names().indexOf(message.params[0]) > -1) {
var nick = clientStore.getClientName(socket).split("!")[0];
socket.write(respondMessage(hostaddress, [replies.ERR_NICKCOLLISION, nick], "Nick collision"));
} else {
clientStore.buildClient(socket, "nick", message.params[0]);
}
}
break;
case "USER":
var nick = clientStore.getClientName(socket).split("!")[0];
if (clientStore.clients[socket.remoteAddress + ":" + socket.remotePort]["user"] != undefined){
socket.write(respondMessage(hostaddress,[replies.ERR_ALREADYREGISTRED, nick], "Already registered"));
break;
}
clientStore.buildClient(socket, "user", message.params[0] + "@" + message.params[2]);
socket.write(respondMessage(null, ["PING"], Math.floor(Math.random()*10000).toString()));
socket.write(respondMessage(hostaddress, [replies.RPL_MOTDSTART, nick], ""));
socket.write(respondMessage(hostaddress, [replies.RPL_MOTD, nick], ""));
socket.write(respondMessage(hostaddress, [replies.RPL_ENDOFMOTD, nick], ""));
socket.write(respondMessage(nick, ["MODE", nick], "+xi"));
break;
case "JOIN":
var name = clientStore.getClientName(socket);
var nick = name.split("!")[0];
if (message.params[0].indexOf("#") != 0 && message.params[0].indexOf("&") != 0){
socket.write(respondMessage(hostaddress, [replies.ERR_BADCHANMASK, nick, message.params[0]], "Cannot join channel"));
break;
}
if (clientStore.joinChannel(socket, message.params[0])){
chanels.joinOrCreate(nick, message.params[0]);
broadcast(respondMessage(name, ["JOIN", message.params[0]]));
socket.write(respondMessage(hostaddress, [replies.RPL_TOPIC, nick, message.params[0]], chanels.list[message.params[0]].topic));
socket.write(respondMessage(hostaddress, [replies.RPL_NAMREPLY, nick, "=", message.params[0]], clientStore.names().join(" ")));
socket.write(respondMessage(hostaddress, [replies.RPL_ENDOFNAMES, nick, message.params[0]], "End of NAMES list"));
}
break;
case "LIST":
var nick = clientStore.getClientName(socket).split("!")[0];
var chans = chanels.channelNames();
for (var i = 0; i < chans.length; i++) {
socket.write(respondMessage(hostaddress, [replies.RPL_LIST, nick, chans[i], chanels.list[chans[i]].users.length], chanels.list[chans[i]].topic));
}
socket.write(respondMessage(hostaddress, [replies.RPL_LISTEND], "End of LIST"));
break;
case "NAMES":
var nick = clientStore.getClientName(socket).split("!")[0];
var reply = respondMessage(hostaddress, [replies.RPL_NAMREPLY, nick, "=", message.params[0]], " :")
if (message.params[0] != undefined && chanels.list[message.params[0]] != undefined){
socket.write(respondMessage(hostaddress, [replies.RPL_NAMREPLY, nick, "=", message.params[0]], chanels.list[message.params[0]].users.join(" ")))
} else {
socket.write(respondMessage(hostaddress, [replies.RPL_NAMREPLY, nick, "=", message.params[0]], clientStore.names().join(" ")))
}
socket.write(respondMessage(hostaddress, [replies.RPL_ENDOFNAMES, nick, message.params[0]], "End of NAMES list"));
break;
case "PING":
socket.write(respondMessage(hostaddress, ["PONG", hostaddress], message.params[0]));
break;
case "PART":
var nick = clientStore.getClientName(socket).split("!")[0];
var leavingError = chanels.leave(nick, message.params[0]);
clientStore.partChannel(socket, message.params[0]);
if (leavingError){
socket.write(respondMessage(hostaddress, [leavingError, nick]));
}
break;
case "PRIVMSG":
var from = clientStore.getClientName(socket);
var nick = from.split("!")[0];
if (message.params[0].indexOf("#") == 0 || message.params[0].indexOf("&") == 0){
if (chanels.list[message.params[0]] == undefined){
socket.write(hostaddress, [replies.ERR_NOSUCHNICK, nick], "No such channel")
}
for (var i = 0; i < chanels.list[message.params[0]].users.length; i++) {
if (chanels.list[message.params[0]].users[i] != nick)
clientStore.getSocketByNick(chanels.list[message.params[0]].users[i]).write(respondMessage(from, ["PRIVMSG", message.params[0]], message.params[1]));
}
} else {
var recipient = clientStore.getSocketByNick(message.params[0]);
if (recipient == null) {
socket.write(hostaddress, [replies.ERR_NOSUCHNICK, nick], "No such nick")
}
recipient.write(respondMessage(from, ["PRIVMSG", message.params[0]], message.params[1]));
}
break;
}
}
}
function respondMessage(prefix, args, trailing){
if (prefix){
args.unshift(":"+prefix);
}
if (trailing){
args.push(":"+trailing)
}
args.push("\r\n");
return args.join(" ");
}
function broadcast(message){
var keys = Object.keys(clientStore.clients)
for (var i = 0; i < keys.length; i++) {
clientStore.clients[keys[i]].socket.write(message);
}
}
process.on('SIGTERM', function () {
console.log("Bye");
server.close(function () {
process.exit(0);
});
});