-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.js
More file actions
328 lines (290 loc) · 9.45 KB
/
bot.js
File metadata and controls
328 lines (290 loc) · 9.45 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
323
324
325
326
327
328
/*jslint node: true nomen: true stupid: true */
'use strict';
var events = require('events');
var fs = require('fs');
var path = require('path');
var util = require('util');
var irc = require('irc');
var Log = require('log');
var getenv = require('getenv');
var botUtils = require('./bot_utils.js');
/* Shortcut to event.EventEmitter */
var EventEmitter = events.EventEmitter;
var CMD_CHAR = '@',
CMD_HELP = 'help';
var EVENTS = {
//irc
REGISTERED: 'registered',
NOTICE: 'notice',
JOIN: 'join',
PART: 'part',
KICK: 'kick',
QUIT: 'quit',
MESSAGE: 'message',
PRIVATE_MESSAGE: 'pm',
//customs!
PUBLIC_MESSAGE: 'public_message',
TALKED_TO_ME: 'talked_to_me',
};
/**
* Represents a IRCBot.
* @constructor
* @param <String> server
* @param <String> nickname
* @param <Object> options
*/
function IRCBot(server, nickname, options) {
// If the function has been called without 'new'
if (!(this instanceof IRCBot)) {
return new IRCBot(server, nickname, options);
}
this.commands = [];
this.commandsNames = [];
this.DEBUG = options.debug || false;
this.events = EVENTS;
/* private variable */
this._client = new irc.Client(server, nickname, options);
this.config = this._client.opt;
this.pluginsSettings = options.pluginsSettings || {};
this.logger = new Log(options.logLevel || 'info');
this.setupClientListeners();
EventEmitter.call(this);
if (options.loadPlugins || false) {
this.loadPlugins(__dirname + '/plugins/');
}
}
// extend the IRCBot class using our EventEmitter class
util.inherits(IRCBot, EventEmitter);
IRCBot.prototype.loadPlugins = function (rootDir) {
this.logger.info('Loading Plugins...');
var bot = this,
pluginsFile = fs.readdirSync(rootDir).sort();
pluginsFile.forEach(function load(fileName) {
var ext = path.extname(fileName),
baseName = path.basename(fileName, ext),
plugin = null;
try {
plugin = require(path.join(rootDir, baseName));
plugin(bot);
bot.logger.info('Loaded successfully: ' + baseName);
} catch (error) {
bot.logger.error('Error loading the plugin: ' + fileName);
bot.logger.error(error.stack);
}
});
};
IRCBot.prototype.setupClientListeners = function () {
/*
Here we use this.<callback>.bind(this)
because by default EventEmitter pass itself
as *this* (inside the callback) and we want to pass
an IRCBot instance as *this*.
See: http://nodejs.org/api/events.html#events_events
*/
var self = this, events = [
EVENTS.REGISTERED,
EVENTS.NOTICE,
EVENTS.JOIN,
EVENTS.PART,
EVENTS.KICK,
EVENTS.QUIT,
'error'
//EVENTS.PRIVATE_MESSAGE,
];
// Get some events and re-emit them with other context
events.forEach(function iter(evt) {
self._client.on(evt, function wrapper() {
var args = Array.prototype.slice.call(arguments);
args.unshift(evt);
self.emit.apply(self, args);
});
});
// special handling for private, change the context
self._client.on(EVENTS.PRIVATE_MESSAGE, self.handleMessageAdapter.bind(self));
// special handling for message, change the context
self._client.on(EVENTS.MESSAGE, self.handleMessageAdapter.bind(self));
};
IRCBot.prototype.handleMessageAdapter = function handleMessageAdapter() {
var ircClient = this._client,
my_nick = ircClient.opt.nick,
blacklistedNicks = null,
nick = null, to = null,
text = null, message = null,
callable = null, cmd_line = null,
cmd = null, c_args = null;
// Dispatch based on the number of arguments
if (arguments.length === 3) {
// Private Message
//nick, text, message
nick = arguments[0];
to = nick;
text = arguments[1];
message = arguments[2];
callable = this.handlePrivateMessage.bind(this);
} else {
// General Message
//nick, chan, text, message
nick = arguments[0];
to = arguments[1];
text = arguments[2];
message = arguments[3];
callable = this.handleGeneralMessage.bind(this);
}
// Ignore my messages
if (nick === my_nick) {
this.logger.debug('I do not reply to myself!');
return;
}
// Ignore blacklisted nicks
blacklistedNicks = getenv.array('BLACKLISTED_NICKS', 'string', []);
if (blacklistedNicks.indexOf(nick) > -1) {
this.logger.debug('I do not reply to blacklisted nicks!');
return;
}
// Special handling for commands
if (text[0] === CMD_CHAR) {
cmd_line = text.split(' ');
cmd = cmd_line[0].slice(1);
c_args = cmd_line.slice(1);
this.handleCommand(nick, to, cmd, c_args);
return;
}
// dispatch some custom logic!
callable(nick, to, text, message);
return;
}
/**
* Special logic for private messages.
* @param <String> nick
* @param <String> _
* @param <String> text
* @param <Object> IRC library message
*/
IRCBot.prototype.handlePrivateMessage = function handlePrivateMessage(nick, _, text, message) {
this.emit(EVENTS.PRIVATE_MESSAGE, nick, text, message);
};
/**
* Special logic for general messages.
* @param <String> nick
* @param <String> chan
* @param <String> text
* @param <Object> IRC library message
*/
IRCBot.prototype.handleGeneralMessage = function handleGeneralMessage(nick, chan, text, message) {
var ircClient = this._client,
my_nick = ircClient.opt.nick;
if (text.match(my_nick)) {
if (botUtils.isChannel(chan)) {
this.emit(EVENTS.TALKED_TO_ME, nick, chan, text, message);
} else {
this.logger.info('Is this condition even possible?');
}
} else {
this.emit(EVENTS.PUBLIC_MESSAGE, nick, chan, text, message);
}
};
/**
* Handles the command call the plugins if needed
* @param <String> nick
* @param <String> to
* @param <String> cmd
* @param <Array> cmdArgs
*/
IRCBot.prototype.handleCommand = function handleCommand(nick, to, cmd, cmdArgs) {
var self = this,
inPrivate = !botUtils.isChannel(to),
commandsLength = this.commands.length,
cmdObj = null,
helpText = [];
if (cmd === CMD_HELP) {
// @help validCmd
if (this.commandsNames.indexOf(cmdArgs[0]) > -1) {
// Its not possible to have more than one action for a command!
helpText = this.commands.filter(function filter(obj) { return (obj.cmd === cmdArgs[0]); });
helpText = helpText[0];
if (helpText.help === '') {
self.say(to, '@' + helpText.cmd + ' has not help, Im not a mind reader!');
} else {
self.say(to, 'Help for @' + helpText.cmd + ': ' + helpText.help);
}
} else {
// @help
// @help invalidCmd
this.commands.forEach(function helpIter(obj) {
helpText.push(obj.cmd);
});
self.say(to, 'Available commands: ' + helpText.join(', '));
}
return;
}
if (this.commandsNames.indexOf(cmd) > -1) {
for(var index=0; index < commandsLength; index++) {
cmdObj = this.commands[index];
if (cmdObj.cmd === cmd) {
if (cmdObj.onlyPrivate) {
if (inPrivate) {
cmdObj.callback.call(this, nick, to, cmdArgs);
} else {
this.say(to, 'This command has to be used in private!');
}
} else {
cmdObj.callback.call(this, nick, to, cmdArgs);
}
// get out of the loop!
break;
}
}
} else {
this.say(to, nick + ' I havent seen that command and Im not a wizard yet!');
}
return;
};
/**
* Register a command with the bot
* @param <String> cmd
* @param <Function> callback
* @param <String> help
* @param <Boolean> onlyPrivate
*/
IRCBot.prototype.registerCommand = function registerCommand(cmd, callback, help, onlyPrivate) {
var self = this;
if (help === undefined) {
help = '';
}
if (cmd instanceof Array) {
// multiples commands for an action
cmd.forEach(function (oneCmd) {
if (self.commandsNames.indexOf(oneCmd) < 0) {
self.commands.push({'cmd': oneCmd, 'callback': callback, 'help': help, 'onlyPrivate': onlyPrivate});
self.commandsNames.push(oneCmd);
} else {
this.logger.info('The command (%s) already exists!', oneCmd);
}
});
} else {
// single command for an action
if (this.commandsNames.indexOf(cmd) < 0) {
this.commands.push({'cmd': cmd, 'callback': callback, 'help': help, 'onlyPrivate': onlyPrivate});
this.commandsNames.push(cmd);
} else {
this.logger.info('The command (%s) already exists!', cmd);
}
}
};
/**
* Proxy the method directly to irc.Client
*/
var clientMethods = [
'join', 'part', 'say',
'ctcp', 'action',
'notice', 'whois',
'list', 'connect',
'disconnect'
];
clientMethods.forEach(function (name) {
IRCBot.prototype[name] = function () {
var args = Array.prototype.slice.call(arguments);
this._client[name].apply(this._client, args);
};
});
module.exports = {IRCBot: IRCBot};