This repository has been archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.js
150 lines (138 loc) · 4.54 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
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
/* 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;
/*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 IrcBot = function(){
var that = this;
this._log_factory = log4js;
this._log_factory.getLogger("INIT").info("starting")
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){
that._log_factory.getLogger("IRC").error(JSON.stringify(message));
});
this.commands = {};
this.filters = {};
this.blacklists = {};
this.channelwarn = {};
this.nick = nick;
this.loadModules();
this.config = config;
};
IrcBot.prototype.loadModules = function(){
this.loaded_modules = config.modules;
var that = this;
config.modules.forEach(function(modname){
that._log_factory.getLogger("INIT").info("loading module %s", modname);
var hooks = require('./modules/irc_' + modname + '.js')(config, that._log_factory, that);
that.installHooks(hooks);
});
};
IrcBot.prototype.installHooks = function(hooks){
var that = this;
Object.keys(hooks.commands).forEach(function(command){
if(!that.commands[command])
that.commands[command]=[];
that.commands[command].push(hooks.commands[command])
})
};
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, nowarn){
var channelwarnTimeout = 3*60*1000;
var warnstart = 3;
var replyquery = 4;
var logger = this._log_factory.getLogger("IRC");
if(this.isChannel(sender, to)){
message = sender + ": " + message;
if(!nowarn){
var old = this.channelwarn[to];
this.channelwarn[to] = old?old+1:1;//fix if value is undefined
logger.debug("channelwarn incr %s:%s", to, this.channelwarn[to]);
var that = this;
setTimeout(function(){
that.channelwarn[to]--;
logger.debug("channelwarn cooldown: %s -> %s", to, that.channelwarn[to]);
}, channelwarnTimeout);
if(this.channelwarn[to]>=warnstart){
logger.info("channelwarn > 5 %s", to);
message = message + " tip: you can send most commands via query.";
}
if(this.channelwarn[to]>=replyquery){
to = sender;
}
}
}
this.irc_client.say(this.sendToWho(sender, to), message);
};
IrcBot.prototype.messageDispatcher = function(message, sender, to){
var args = message.split(/\s+/);
var command = args[0];
var funs;
if(funs = this.commands[command]){
if(this.isBlacklisted(message, sender, to))
return;
var that = this;
funs.forEach(function(fun){
fun.apply(that, [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();