forked from b4ckspace/node-ircbot-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
117 lines (102 loc) · 3.31 KB
/
bot.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
/* IRC SETTNGS */
/* settings will be set/overwritten in the following order:
* 1. config.js
* 2. environment variables
* 3. config.local.js
*/
var config;
try{
config = require('./config.local.js');
}catch(e){
config = require('./config.js');
}
var nick = config.nick;
var realname = config.realname;
var username = config.username;
var irc_server = config.irc_server;
var irc_port = config.irc_port;
var ircpass = config.ircpass;
var secure = config.secure;
var ignoreSsl = config.ignoreSsl;
var channels = config.channels;
var disable_mpd = config.disable_mpd;
/*REQUIRES*/
var irc = require('irc');
var util = require('util');
var log4js = require('log4js');
/*LOG SETUP*/
log4js.configure({
appenders: [
{ type: 'console' },
{ type: 'file', filename: 'logs/logs.log'}
]
});
var logger = log4js.getLogger("CORE");
logger.info("STARTUP");
var IrcBot = function(){
var that = this;
this.irc_client = new irc.Client( irc_server, nick, {
'channels' : channels,
'userName' : username,
'realName' : realname,
'port' : irc_port,
'secure' : secure,
'selfSigned' : ignoreSsl,
'certExpired' : ignoreSsl,
'password' : ircpass,
});
this.irc_client .addListener('message', function (from, to, message) {
if((from!=nick) && (!that.contentFilter(message, from, to)) ){
that.messageDispatcher(message, from, to);
}
});
this.irc_client .addListener('error', function(message){
logger.error(JSON.stringify(message));
});
this.commands = {};
this.filters = {};
this.blacklists = {};
//this.irc_client = ircclient;
require('./modules/irc_core.js')(config, log4js, this);
require('./modules/irc_plenking.js')(config, log4js, this);
require('./modules/irc_karma.js')(config, log4js, this);
require('./modules/irc_bckspc.js')(config, log4js, this);
require('./modules/irc_webrelais.js')(config, log4js, this);
if(!disable_mpd)
require('./modules/irc_mpd.js')(config, log4js, this);
require('./modules/irc_github.js')(config, log4js, this);
};
IrcBot.prototype.sendToWho = function(sender, to){
return this.isChannel(sender, to) ? to : sender;
};
IrcBot.prototype.isChannel = function(sender, to){
return to && to[0]=='#';
};
IrcBot.prototype.reply = function(sender, to, message){
if(this.isChannel(sender, to))
message = sender + " " + message;
this.irc_client.say(this.sendToWho(sender, to), message);
};
IrcBot.prototype.messageDispatcher = function(message, sender, to){
var args = message.split(' ');
var command = args[0];
var fun;
if(fun = this.commands[command]){
if(this.isBlacklisted(message, sender, to))
return;
fun.apply(this, [sender, to].concat(args.slice(1)) );
}
};
IrcBot.prototype.contentFilter = function(message, sender, to){
for(var name in this.filters){
if( this.filters[name].apply(bot, [message, sender, to]))
return true;
}
};
IrcBot.prototype.isBlacklisted = function(message, sender, to){
for(var name in this.blacklists){
if( this.blacklists[name].apply(bot, [message, sender, to]))
return true;
}
};
var bot = new IrcBot();