-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathirc.js
325 lines (278 loc) · 7.98 KB
/
irc.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
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
323
324
325
/**
* node irc bot
* Based on the work from Michael Owens and Karl Tiedt.
*
* https://github.com/ktiedt/NodeJS-IRC-Bot
*
*
* @author TJ Hunter
* @copyright TJ Hunter 2012
*/
var sys = require('util')
, net = require('net')
, Channel = require('./channel').Channel;
irc = exports.irc = function(options) {
this.init(options);
};
sys.inherits(irc, process.EventEmitter);
irc.prototype.init = function(config) {
var that = this;
this.config = config;
this.host = config.host || '127.0.0.1';
this.port = config.port || 6667;
this.botNick = config.botNick || 'NodeBot';
this.username = config.username || 'NodeBot';
this.realname = config.realname || 'Node Bot';
this.triggerPrefix = config.triggerPrefix || '.';
this.joinChannels = config.channels || [];
this.pluginList = config.plugins || [];
this.plugins = [];
this.pluginTriggers = {};
this.encoding = 'utf8';
this.timeout = 60 * 60 * 1000;
this.channels = {};
this.buffer = '';
for (var i=0; i<this.joinChannels.length; i++) {
this.joinChannels[i] = this.joinChannels[i].toLowerCase();
}
this.pluginList.forEach(function(plugin){
var Plugin = require('./plugins/' + plugin);
that.plugins.push(new Plugin.Plugin(that));
});
};
irc.prototype.addTrigger = function(plugin, trigger, fn) {
if (this.pluginTriggers[trigger]) {
this.pluginTriggers[trigger].push({plugin: plugin, callback: fn});
} else {
this.pluginTriggers[trigger] = [{plugin: plugin, callback: fn}];
}
};
irc.prototype.connect = function() {
var conn = this.conn = net.createConnection(this.port, this.host);
conn.setEncoding(this.encoding);
conn.setTimeout(this.timeout);
this.on('connect', this.onConnect);
this.on('data', this.onReceive);
this.on('eof', this.onEOF);
this.on('timeout', this.onTimeout);
this.on('close', this.onClose);
};
irc.prototype.disconnect = function(reason) {
if (this.conn.readyState !== 'closed') {
this.conn.end();
console.log('Disconnected: ' + reason);
}
}
irc.prototype.onConnect = function() {
console.log('Connected');
this.raw('NICK', this.botNick);
this.raw('USER', this.username, '0', '*', ':' + this.realname);
};
irc.prototype.onReceive = function(chunk) {
this.buffer += chunk;
while (this.buffer) {
var offset = this.buffer.indexOf("\r\n");
if (offset < 0) {
return;
}
var msg = this.buffer.slice(0, offset);
this.buffer = this.buffer.slice(offset + 2);
msg = this.parse(msg);
this.onMessage(msg);
}
};
irc.prototype.onMessage = function(msg) {
var target = msg.arguments[0]
, nick = this.parseNick(msg.prefix)
, command = msg.command;
switch (true) {
case (command === '353'): // NAMES
this.parseNames(msg);
break;
case (command === '366'): // End of /NAMES
case (command === '372'): // MOTD
// console.log('<-- [' + command + '] ' + msg.arguments[1]);
break;
case (command === '376'): // End of MOTD, Ready to go
var that = this;
that.joinChannels.forEach(function(name){ // Join all the channels in the config
that.channels[name] = new Channel(that, name);
});
this.plugins.forEach(function(plugin){
if (typeof plugin.onReady === 'function') {
plugin.onReady();
}
});
break;
case (command === 'JOIN'):
var channel = msg.arguments[0].toLowerCase();
console.log('<-- [' + command + '] ' + nick + ' joined ' + channel);
this.channels[channel].userJoin(nick);
this.plugins.forEach(function(plugin){
if (typeof plugin.onJoin === 'function') {
plugin.onJoin(channel, nick);
}
});
break;
case (command === 'KICK'):
var channel = msg.arguments[0].toLowerCase()
, user = msg.arguments[1].toLowerCase();
console.log('<-- [' + command + '] ' + nick + ' kicked ' + user + ' from ' + channel);
this.channels[channel].userKick(user);
this.plugins.forEach(function(plugin){
if (typeof plugin.onKick === 'function') {
plugin.onKick(channel, nick, user);
}
});
break;
case (command === 'MODE'):
break;
case (command === 'NICK'):
var newNick = msg.arguments[0].toLowerCase();
console.log('<-- [' + command + '] ' + nick + ' is now known as ' + newNick);
for (channel in this.channels) {
this.channels[channel].userNickChange(nick, newNick);
}
this.plugins.forEach(function(plugin){
if (typeof plugin.onNick === 'function') {
plugin.onNick(nick, newNick);
}
});
break;
case (command === 'PART'):
var channel = msg.arguments[0].toLowerCase();
console.log('<-- [' + command + '] ' + nick + ' left ' + channel);
this.channels[channel].userPart(nick);
this.plugins.forEach(function(plugin){
if (typeof plugin.onPart === 'function') {
plugin.onPart(channel, nick);
}
});
break;
case (command === 'PING'):
// console.log('<-- [' + command + '] ' + msg.arguments[0]);
this.raw('PONG', msg.arguments);
break;
case (command === 'PRIVMSG'):
var channel = msg.arguments[0].toLowerCase()
, message = msg.arguments[1];
console.log('<-- [' + command + '] <' + nick + '> ' + message);
if (message.substring(0,1) == this.triggerPrefix) { // Check for a trigger event
var trigger = message.split(' ')[0].substring(1, message.length);
if (this.pluginTriggers[trigger]) {
this.pluginTriggers[trigger].forEach(function(cb){
cb.callback.call(cb.plugin, msg);
});
}
}
this.plugins.forEach(function(plugin){
if (typeof plugin.onMessage === 'function') {
plugin.onMessage(channel, nick, message);
}
});
break;
case (command === 'QUIT'):
console.log('<-- [' + command + '] ' + nick);
for (channel in this.channels) {
this.channels[channel].userQuit(nick);
}
this.plugins.forEach(function(plugin){
if (typeof plugin.onQuit === 'function') {
plugin.onQuit(nick);
}
});
break;
default:
console.log('<-- [' + command + '] (not implemented) <' + msg.prefix + '> ' + msg.arguments);
//console.log(msg);
break;
}
};
irc.prototype.onEOF = function() {
this.disconnect('EOF');
};
irc.prototype.onTimeout = function() {
this.disconnect('Timeout');
};
irc.prototype.onClose = function() {
this.disconnect('Close');
};
irc.prototype.raw = function(cmd) {
if (this.conn.readyState !== 'open') {
return this.disconnect("irc.raw: cannot send; ready state: " + this.conn.readyState);
}
var msg = Array.prototype.slice.call(arguments, 1).join(' ') + "\r\n";
if (cmd !== 'PONG') {
console.log('--> [' + cmd + '] ' + msg.trim());
}
this.conn.write(cmd + ' ' + msg, this.encoding);
};
irc.prototype.send = function(target, msg) {
var message = Array.prototype.slice.call(arguments, 1).join(' ') + "\r\n";
if (arguments.length > 1) {
this.raw('PRIVMSG', target, ':' + message);
}
};
irc.prototype.on = function(ev, f) {
var that = this;
return this.conn.on(ev, (function() {
return function() {
f.apply(that, arguments);
};
})());
};
irc.prototype.parseNames = function(msg) {
var irc = this
, chan = msg.arguments[2].toLowerCase()
, nickList = msg.arguments[3].replace(/\+|@/g, '').split(' ');
nickList.forEach(function(nick){
irc.channels[chan].userJoin(nick.toLowerCase());
});
};
irc.prototype.parse = function(text) {
if (typeof text !== "string") {
return false;
}
var tmp = text.split(" ");
if (tmp.length < 2) {
return false;
}
var prefix = null
, command = null
, lastarg = null
, args = [];
for (var i = 0, j = tmp.length; i < j; i++) {
if (i == 0 && tmp[i].indexOf(":") == 0) {
prefix = tmp[0].substr(1);
} else if (tmp[i] == "") {
continue;
} else if (!command && tmp[i].indexOf(":") != 0) {
command = tmp[i].toUpperCase();
} else if (tmp[i].indexOf(":") == 0) {
tmp[i ] = tmp[ i].substr(1);
tmp.splice(0, i);
args.push(tmp.join(" "));
lastarg = args.length - 1;
break;
} else {
args.push(tmp[i]);
}
}
return {
prefix: prefix,
command: command,
arguments: args,
lastarg: lastarg,
orig: text
};
};
irc.prototype.parseNick = function(mask) {
if (!mask) {
return;
}
var match = mask.match(/([^!]+)![^@]+@.+/);
if (!match) {
return;
}
return match[1].toLowerCase();
};