-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathircbot.js
More file actions
258 lines (226 loc) · 10.3 KB
/
ircbot.js
File metadata and controls
258 lines (226 loc) · 10.3 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
var irc = require('irc');
var util = require('util');
var search = require('./search');
var log = require('./log');
var web = require('./web/app');
/*
** Constructor. Create state in the object
*/
IrcBot = function(config, webconfig) {
this.config = config;
this.webconfig = webconfig;
this.client = new irc.Client(this.config.server, this.config.nick, this.config);
this.logger = new Log();
this.names = { };
}
/*
** Opens a connection to the IRC server
*/
IrcBot.prototype.open = function() {
var ircBot = this;
ircBot.logger.send(util.format("Connecting to IRC. ircserver=%s, ircnick=%s, ircconfig=%j",
this.config.server, this.config.nick, this.config));
// Autoreconnect if we time out or get another error
try {
ircBot.client.connect(ircBot.config.retryCount, function() {
ircBot.logger.send(util.format("Connected. ircserver=%s, ircnick=%s", ircBot.config.server, ircBot.config.nick));
ircBot.addListeners();
});
} catch (ex) {
console.log("Caught exception attempting to connect: ", ex);
console.log("Reconnecting in two seconds.");
setTimeout(function() { ircBot.open() }, 2000);
}
}
/*
** Add listeners to parse IRC messages
*/
IrcBot.prototype.addListeners = function() {
var ircBot = this;
this.client.addListener("registered", function(message) {
var strout = 'server='+this.opt.server+' action=registered nick='+this.opt.nick;
ircBot.logger.send(strout);
});
this.client.addListener("names", function(channel, nicks) {
// Update names cache which we'll look up against when we get messages and notices
ircBot.names[channel] = nicks;
var strout = 'server='+this.opt.server+' action=names names="';
for (nick in nicks) {
strout += nicks[nick]+nick+' ';
}
strout = strout+'"';
ircBot.logger.send(strout);
});
this.client.addListener("topic", function(channel, topic, nick, message) {
var objout = { server: this.opt.server, action: 'topic', nick: nick, prettynick: ircBot.prettynick(channel, nick), topic: topic };
ircBot.logger.send(objout);
});
this.client.addListener("join", function(channel, nick, message) {
var objout = { server: this.opt.server, action: 'join', nick: nick, channel: channel };
ircBot.logger.send(objout);
});
this.client.addListener("part", function(channel, nick, reason, message) {
var objout = { server: this.opt.server, action: 'part', nick: nick, channel: channel, reason: reason };
ircBot.logger.send(objout);
});
this.client.addListener("quit", function(nick, reason, channels, message) {
var objout = { server: this.opt.server, action: 'quit', nick: nick, channels: channels, reason: reason };
ircBot.logger.send(objout);
});
this.client.addListener("kick", function(channel, nick, by, reason, message) {
var objout = { server: this.opt.server, action: 'kick', nick: nick, channel: channel, kicked_by: by,
reason: reason };
ircBot.logger.send(objout);
});
this.client.addListener("kill", function(nick, reason, channels, message) {
var objout = { server: this.opt.server, action: 'kill', nick: nick, channels: channels, message: message };
ircBot.logger.send(objout);
});
this.client.addListener("message", function(nick, to, text, message) {
var action = 'message';
// Check if we're CTCP
if (text.charAt(0) == "\u0001") {
var seppos = text.search(" ") > 0 ? text.search(" ") : text.length-1;
var command = text.substr(1, seppos-1);
var argstr = text.substr(seppos+1);
switch (command) {
case 'VERSION':
action = 'ctcp_version';
ircBot.client.notice(nick, "\u0001VERSION "+ircBot.config.version+"\u0001");
case 'PING':
action = 'ctcp_ping';
ircBot.client.notice(nick, "\u0001PING "+Math.round(new Date().getTime()/1000,0)+"\u0001");
}
}
if (text.charAt(0) == "!") {
var seppos = text.search(" ") > 0 ? text.search(" ") : text.length;
var command = text.substr(1, seppos-1);
var argstr = text.substr(seppos+1);
var channel = '';
// Validate input
if (!argstr.search("|")) {
if (to.charAt(0) != "#") {
for (var i=0; i < Object.keys(ircBot.names).length; i++) {
console.log(ircBot.names[Object.keys(ircBot.names)[i]]);
console.log(nick);
if (nick in ircBot.names[Object.keys(ircBot.names)[i]]) {
channel = Object.keys(ircBot.names)[i];
break;
}
}
// Stop gap if we don't find the nick, return results from the first configured channel
if (channel.length == 0) {
channel = ircBot.config.channels[0];
}
to = nick;
} else {
channel = to;
}
ircBot.dispatchCommand(nick, command, argstr, to, channel);
}
}
var objout = { server: this.opt.server, action: action, nick: nick, prettynick: ircBot.prettynick(to, nick),
to: to, text: text };
ircBot.logger.send(objout);
});
this.client.addListener("notice", function(nick, to, text, message) {
var objout = { server: this.opt.server, action: 'notice', nick: nick, prettynick: ircBot.prettynick(to, nick),
to: to, text: text };
ircBot.logger.send(objout);
});
this.client.addListener("nick", function(oldnick, newnick, channels, message) {
var objout = { server: this.opt.server, action: 'nick', oldnick: oldnick, newnick: newnick, channels: channels };
ircBot.logger.send(objout);
});
this.client.addListener("invite", function(channel, nick, message) {
var objout = { server: this.opt.server, action: 'invite', nick: nick, channel: channel };
ircBot.logger.send(objout);
});
this.client.addListener("+mode", function(channel, by, mode, argument, message) {
var objout = { server: this.opt.server, action: '+mode', channel: channel, by: by,
mode: mode, argument: argument };
ircBot.logger.send(objout);
// Update state of the names cache when modes change
this.client.send("NAMES", channel);
});
this.client.addListener("-mode", function(channel, by, mode, argument, message) {
var objout = { server: this.opt.server, action: '-mode', channel: channel, by: by,
mode: mode, argument: argument };
ircBot.logger.send(objout);
// Update state of the names cache when modes change
ircBot.client.send("NAMES", channel);
});
// Ignore client errors
this.client.addListener("error", function (message) { });
// Every minute, send ourselves a PING
setInterval(function() {
ircBot.client.say(ircBot.config.nick, "\u0001PING "+Math.round(new Date().getTime()/1000,0)+"\u0001");
}, 60000);
}
/*
** When we receive a message, if it contains a preceding !, we send the parsed
** output to this function to determine which actions to take
*/
IrcBot.prototype.dispatchCommand = function(nick, command, argstr, to, channel) {
var ircBot = this;
var dispatch = { }
dispatch.live = function() {
ircBot.client.say(to, nick+': Live view of '+channel+' can be found at http://'+ircBot.webconfig.host+':'+ircBot.webconfig.port
+'/live/'+encodeURIComponent(channel));
}
dispatch.stats = function() {
ircBot.client.say(to, nick+': Stats can be found at http://'+ircBot.webconfig.host+':'+ircBot.webconfig.port
+'/stats/'+encodeURIComponent(channel));
}
dispatch.search = function() {
// 2.28 CS - Replacing IRC functionality with link to web interface
// search.logsearch(argstr, function(err, log) {
// if (typeof log === 'string') {
// var logarr = log.split("\n");
// for (var i=0; i<logarr.length; i++) {
// ircBot.client.say(nick, logarr[i]);
// }
// }
// });
ircBot.client.say(to, nick+': Results can be found at http://'+ircBot.webconfig.host+':'+ircBot.webconfig.port
+'/search?q='+encodeURIComponent(argstr));
}
dispatch.lasturls = function() {
// 2.28 CS - Replacing IRC functionality with link to web interface
// search.lasturls(to, function(err, log) {
// if (typeof log === 'string') {
// var logarr = log.split("\n");
// for (var i=0; i<logarr.length; i++) {
// ircBot.client.say(nick, logarr[i]);
// }
// }
// }, argstr);
ircBot.client.say(to, nick+': Last 10 URLs can be found at http://'+ircBot.webconfig.host+':'+ircBot.webconfig.port
+'/urls/10/'+encodeURIComponent(channel));
}
dispatch.map = function () {
ircBot.client.say(to, nick+': Who talks to Whom can be found at http://'+ircBot.webconfig.host+':'+ircBot.webconfig.port
+'/map/'+encodeURIComponent(channel));
}
if (typeof dispatch[command] === 'function') {
dispatch[command]();
}
}
/*
** Disconnects the IRC Client
*/
IrcBot.prototype.close = function(message) {
this.client.disconnect(message);
}
/*
** Return status of the nick from the names cache along with the nick
** Will return nick, +nick, @nick, etc.
*/
IrcBot.prototype.prettynick = function (channel, nick) {
var prefix = "";
if (typeof this.names[channel] !== 'undefined') {
prefix = this.names[channel][nick];
}
return prefix+nick;
}
exports.IrcBot = IrcBot;