This repository was archived by the owner on Sep 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathserver.IrcServer.js
More file actions
executable file
·322 lines (292 loc) · 9.55 KB
/
server.IrcServer.js
File metadata and controls
executable file
·322 lines (292 loc) · 9.55 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* jshint asi: true */
/* jshint esnext: true */
version = "0.1a";
var fs = require('fs');
var ws = require('ws');
var net = require('net');
var ChatClientBase = require('./server.ChatClientBase.js');
var util = require("util");
function ToastyClient(ircClient, channelName) {
ChatClientBase.call(this);
this.channelName = channelName;
this.ircClient = ircClient;
var data = {cmd:'join', channel: channelName, nick: ircClient.nick, pass: ''};
if (ircClient.password)
data.pass = ircClient.password;
this.ircClient.chatServerBase.onMessage(this, data);
}
util.inherits(ToastyClient, ChatClientBase);
ToastyClient.prototype.getId = function() {
return this.ircClient;
}
ToastyClient.prototype.getIpAddress = function() {
return this.ircClient.getRemoteAddress();
}
ToastyClient.prototype.close = function() {
this.ircClient.chatServerBase.onClose(this);
// this.ircClient.close();
}
ToastyClient.prototype._generateIdentifier = function(nick) {
return nick+'!~'+nick+'@'+this.ircClient.config.serverHostname;
}
ToastyClient.prototype._getSystemUser = function() {
return '&system';
}
ToastyClient.prototype.send = function(causingClient, data) {
var self = this;
data.time = Date.now(); // Add timestamp to command
switch (data.cmd) {
case 'onlineAdd':
this.ircClient.write(':'+data.nick+' JOIN :#'+this.channelName);
break;
case 'onlineRemove':
this.ircClient.write(':'+data.nick+' PART :#'+this.channelName);
break;
case 'onlineSet':
var nicks = [];
data.nicks.forEach(function(nick) {
nicks.push(nick);
});
this.ircClient.answer('353 '+this.ircClient.nick+' = #'+this.channelName+' :'+nicks.join(" "));
this.ircClient.answer('366 '+this.ircClient.nick+' #'+this.channelName+' :End of NAMES list');
break;
case 'shout':
data.text.split('\n').forEach(function(line) {
this.ircClient.write(':'+this._generateIdentifier(this._getSystemUser())+' PRIVMSG #'+this.channelName+' :'+line.replace('\r', ''));
}, this);
break;
case 'chat':
if (causingClient !== this) { // show only messages from different (specialized) clients
data.text.split('\n').forEach(function(line) {
this.ircClient.write(':'+this._generateIdentifier(data.nick)+' PRIVMSG #'+this.channelName+' :'+line.replace('\r', ''));
}, this);
}
break;
case 'action':
if (causingClient !== this) {
data.text.split('\n').forEach(function(line) {
this.ircClient.write(':'+this._generateIdentifier(data.nick)+' PRIVMSG #'+this.channelName+ ' :' + String.fromCharCode(1) + 'ACTION '+line.replace('\r', '').substr(4) + String.fromCharCode(1));
}, this);
}
break;
case 'warn':
data.text.split('\n').forEach(function(line) {
this.ircClient.write(':'+this._generateIdentifier(this._getSystemUser())+' PRIVMSG #'+this.channelName+' :'+line.replace('\r', ''));
}, this);
break;
case 'info':
data.text.split('\n').forEach(function(line) {
this.ircClient.write(':'+this._generateIdentifier(this._getSystemUser())+' PRIVMSG #'+this.channelName+' :'+line.replace('\r', ''));
}, this);
break;
}
// @TODO: check for errors and disconnect/reconnect
}
function IrcServer() {
}
IrcServer.STATE = {
initial: 0,
ready: 1
};
module.exports = function() {
return new IrcServer();
};
IrcServer.prototype.initialize = function(config) {
this.config = config;
}
IrcServer.prototype.prepareRequest = function(line) {
var args = line.split(" ");
for (var i = 0; i < args.length; ++i) {
if (args[i][0] == ':') {
args[i] = args[i].substr(1);
var rest = args.splice(i, args.length-i).join(" ");
args.push(rest);
break;
}
}
return args;
}
IrcServer.prototype.run = function(chatServerBase) {
var self = this;
net.createServer(function(c) {
var newIrcClient = new IrcClient(chatServerBase, c, self.config);
c.on('data', function(data) {
try {
data = String(data); // always string data
lines = data.split('\n');
while (lines.length > 0) {
var line = lines.shift().replace('\r', '');
if (line.length === 0) continue;
var args = self.prepareRequest(line);
self.handleCommand(newIrcClient, args);
}
} catch(e) {
console.error(e.stack); // @TODO: i normally don't yolo this hard
}
});
c.on('end', function() {
newIrcClient.close();
});
c.on('error', function() {
newIrcClient.close();
});
}).listen(this.config.port, this.config.ip);
console.log("Started irc server on " + this.config.ip + ":" + this.config.port);
}
IrcServer.prototype.handleCommand = function(client, args) {
var command = args[0];
switch(command) {
case 'CAP':
switch (args[1]) {
case 'LS':
client.answer('CAP * LS :multi-prefix');
break;
case 'REQ':
client.requests.push(args.slice(2));
if (client.requests.length > 20)
server.killClient(client, KILLREASON.kill);
break;
case 'END':
if (client.state != IrcServer.STATE.ready)
break;
client.requests.forEach(function(req) {
switch (req[0]) {
case ':multi-prefix':
client.answer('CAP '+client.nick+' ACK '+req[0]);
}
});
break;
default:
console.log('Unknown cap: '+args[1]);
}
return;
case 'PASS':
client.password = args[1];
return;
case 'NICK':
if (client.state != IrcServer.STATE.initial) return;
client.nick = args[1];
client.state = IrcServer.STATE.ready;
return;
case 'USER':
client.user = args[1];
client.sendWelcome();
return;
case 'JOIN':
if (client.state != IrcServer.STATE.ready) return;
var channelString = args[1] || "";
if (channelString.length === 0) return;
var channelList = channelString.split(",");
var keys = args[2];
var keyList = (keys && keys.length > 0) ? keys.split(",") : [];
channelList.forEach(function(channelName, i) {
client.addChannel(channelName.substr(1));
});
return;
case 'PART':
if (client.state != IrcServer.STATE.ready) return;
var channelString = args[1] || "";
if (channelString.length === 0) return;
var channelList = channelString.split(",");
channelList.forEach(function(channelName) {
client.removeChannel(channelName.substr(1));
});
return;
case 'PING':
client.answer('PONG '+this.config.servHostname+' :'+args[1]);
return;
case 'INVITE':
if (client.state != IrcServer.STATE.ready) return;
var channelName = args[1].substr(1);
var message = args[2];
var toastyClient = client.channels[channelName];
if (toastyClient === void 0) return;
client.chatServerBase.onMessage(toastyClient, {cmd: 'invite', nick: message});
return;
case 'PRIVMSG':
if (client.state != IrcServer.STATE.ready) return;
var channelName = args[1].substr(1);
var message = args[2];
if (/^\x01ACTION /.test(message)) // "me" action
message = message.replace(/^\x01ACTION (.*)\x01$/, "/me $1");
if (/x03/.test(message)) // contains coloring
message = message.replace(/\x03[01]?[0-9](,[01]?[0-9])?([^\x03]+)/g, "$2").replace(/\x03/g, "");
var toastyClient = client.channels[channelName];
if (toastyClient === void 0) return;
client.chatServerBase.onMessage(toastyClient, {cmd: 'chat', channel: channelName, text: message});
return;
case 'QUIT':
client.close();
return;
}
};
function IrcClient(chatServerBase, socket, config) {
this.chatServerBase = chatServerBase;
this.socket = socket;
this.config = config;
this.host = this.getRemoteAddress();
this.requests = [];
this.socketClosed = false;
this.state = IrcServer.STATE.initial;
this.channels = {};
this.channelCount = 0;
this.nick = null;
this.guid = null;
}
IrcClient.prototype.close = function() {
this.socketClosed = true;
if (this.socket)
this.socket.destroy();
for (var i in this.channels)
this.channels[i].close(); // cleanup channels
this.channels = {};
this.channelCount = 0;
}
IrcClient.prototype.write = function(data) {
if (this.socketClosed) return;
return this.socket.write(data+'\r\n');
}
IrcClient.prototype.getRemoteAddress = function() {
return this.socket.remoteAddress;
}
IrcClient.prototype.getServerHostname = function() {
return this.config.serverHostname;
}
IrcClient.prototype.getServerIdentifier = function(bExtended) {
return this.nick+(bExtended?'!~'+this.user:'')+'@'+this.getServerHostname();
}
IrcClient.prototype.getIdentifier = function() {
return this.nick+'!~'+this.user+'@'+this.getRemoteAddress();
}
IrcClient.prototype.setNickName = function(nick) {
this.nick = nick;
}
IrcClient.prototype.sendWelcome = function() {
this.answer('001 '+this.nick+' :Welcome to the fancy IRC server '+this.nick);
this.answer('002 '+this.nick+' :Your host is '+this.config.serverHostname+' running version '+version);
this.answer('003 '+this.nick+' :This server was created '+this.timeCreated);
this.answer('004 '+this.nick+' '+this.config.serverHostname+' '+version);
}
IrcClient.prototype.addChannel = function(channelName) {
// @TODO: check for valid channel name
this.answerFrom('JOIN :#'+channelName, this.getIdentifier()); // notify irc client of success
if (this.channels[channelName] !== void 0) return; // already joined
this.channels[channelName] = new ToastyClient(this, channelName);
this.channelCount += 1;
}
IrcClient.prototype.removeChannel = function(channelName) {
this.answerFrom('PART :#'+channelName, this.getIdentifier());
if (this.channels[channelName] === void 0) return; // not joined yet
this.channels[channelName].close();
delete this.channels[channelName];
this.channelCount -= 1;
}
IrcClient.prototype.answerSimple = function(str) {
return this.write(str);
}
IrcClient.prototype.answerFrom = function(str, from) {
return this.write(':'+from+' '+str);
}
IrcClient.prototype.answer = function(str) {
this.answerFrom(str, this.getServerHostname());
}