-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
executable file
·253 lines (204 loc) · 6.81 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
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
#!/usr/bin/env node
var irc = require("irc-upd");
var config = require(process.env.CONFIG_FILE || "./config");
var fs = require("fs");
var tr = require("transliteration");
var IrcAntiSpam = function (thisConfig) {
var self = this;
self.config = {
"server": "irc.freenode.net",
"botName": "IrcAntiSpam",
"debug": false,
"showErrors": true,
"autoRejoin": true,
"floodProtection": true,
"floodProtectionDelay": 500,
"retryCount": 10,
"voiceDelay": 30000,
"whiteListFile": false,
"blackListFile": false,
"messages": [],
"autoSendCommands": [],
"banNick": true,
"banUser": true,
"banHost": true,
... thisConfig
};
self.init();
};
IrcAntiSpam.prototype.init = function() {
var self = this;
if (self.config.messages.length) {
self.messagesRegExp = new RegExp(self.config.messages.join("|"), "i");
}
self.echoRegExp = new RegExp("^"+self.config.botName+", hi");
self.infoRegExp = new RegExp("^"+self.config.botName+", info");
self.blackList = [];
self.whiteList = [];
self.voiceTimers = {};
self.numSpamMessages = 0;
if (self.config.whiteListFile) {
console.log("INFO: reading whitelist from "+self.config.whiteListFile);
let data = fs.readFileSync(self.config.whiteListFile);
self.whiteList = JSON.parse(data);
console.log("INFO: ... "+self.whiteList.length+" entries found");
}
if (self.config.blackListFile) {
console.log("INFO: reading blacklist from "+self.config.blackListFile);
let data = fs.readFileSync(self.config.blackListFile);
self.blackList = JSON.parse(data);
console.log("INFO: ... "+self.blackList.length+" entries found");
}
self.client = new irc.Client(self.config.server, self.config.botName, {
channels: config.channels,
port: config.port,
debug: config.debug,
showErrors: config.showErrors,
autoRejoin: config.autoRejoin,
floodProtection: config.floodProtection,
floodProtectionDelay: config.floodProtectionDelay,
retryCount: config.retryCount,
stripColors: true
});
self.client.addListener("message", function (nick, channel, text, message) {
self.onMessage(nick, channel, text, message);
});
self.client.addListener("error", function(message) {
console.log("ERROR: ", message);
});
self.client.addListener("notice", function(from, to, text) {
console.log(text);
});
self.client.addListener("registered", function() {
self.config.autoSendCommands.forEach(function(element) {
self.client.send(...element);
});
});
self.client.addListener("join", function(channel, nick, message) {
self.onJoin(nick, channel, message);
});
self.client.addListener("part", function(channel, nick) {
self.onLeave(nick, channel);
});
self.client.addListener("quit", function(nick, reason, channels) {
channels.forEach(function(channel) {
self.onLeave(nick, channel);
});
});
self.client.addListener("kick", function(channel, nick) {
self.onLeave(nick, channel);
});
self.client.addListener("kill", function(nick, reason, channels) {
channels.forEach(function(channel) {
self.onLeave(nick, channel);
});
});
};
IrcAntiSpam.prototype.onJoin = function(nick, channel, message) {
var self = this,
key = nick + "#" + channel;
// process whiteList
if (self.whiteList.indexOf(nick) !== -1) {
console.log("INFO: immediately allowing known nick '" + nick + "' to speak");
self.client.send("mode", channel, "+v", nick);
return;
}
// process blackList
if (self.blackList.indexOf(nick) !== -1) {
console.log("INFO: immediately kicking known nick '" + nick + "' from channel " + channel);
self.handleSpammer(nick, channel, message);
return;
}
// otherwise
if (self.config.voiceDelay && nick !== self.config.botName) {
console.log("INFO: will allow "+nick+" to speak after "+self.config.voiceDelay+"ms ... ");
//self.client.say(channel, "Hi, "+nick+", you'll be allowed to speak soon.");
self.voiceTimers[key] = setTimeout(function() {
self.client.send("mode", channel, "+v", nick);
delete self.voiceTimers[key];
}, self.config.voiceDelay);
}
};
IrcAntiSpam.prototype.onLeave = function(nick, channel) {
var self = this,
key = nick + "#" + channel;
if (typeof(self.voiceTimers[key]) !== "undefined") {
console.log("INFO: "+nick+" left too early to be able to speak");
clearTimeout(self.voiceTimers[key]);
delete self.voiceTimers[key];
}
};
IrcAntiSpam.prototype.onMessage = function(nick, channel, text, message) {
var self = this,
text_tr = tr.transliterate(text, {
unknown: "."
});
if (self.echoRegExp.test(text)) {
self.handleEcho(nick, channel);
return;
}
if (self.infoRegExp.test(text)) {
self.handleInfo(nick, channel);
return;
}
if (self.whiteList.indexOf(nick) !== -1) {
//console.log("INFO: found nick "+nick+" on whitelist");
return;
}
if (self.blackList.indexOf(nick) !== -1) {
console.log("INFO: banned nick "+nick);
self.numSpamMessages++;
self.handleSpammer(nick, channel, message);
return;
}
if (self.messagesRegExp && self.messagesRegExp.test(text_tr)) {
console.log("INFO: spam message detected: ",text_tr);
self.handleSpammer(nick, channel, message);
return;
}
};
IrcAntiSpam.prototype.handleInfo = function(nick, channel) {
var self = this;
self.client.say(channel, self.blackList.length + " user(s) kicked. " + self.numSpamMessages + " spam message(s) blocked.");
};
IrcAntiSpam.prototype.handleEcho = function(nick, channel) {
var self = this;
self.client.say(channel, "Hi, " + nick);
};
IrcAntiSpam.prototype.handleSpammer = function(nick, channel, message) {
var self = this,
user = message.user.replace(/^~/, ""),
newSpammerFound = false;
self.numSpamMessages++;
if (self.blackList.indexOf(nick) === -1) {
self.blackList.push(nick);
newSpammerFound = true;
}
if (self.blackList.indexOf(user) === -1) {
self.blackList.push(user);
newSpammerFound = true;
}
console.log("INFO: kicking nick '" + nick + "' from channel "+channel);
self.client.send("kick", channel, nick, "you are a spammer");
// nick ban
if (self.config.banNick) {
console.log("INFO: banning nick '" + nick + "'");
self.client.send("mode", channel, "+b", nick +"!*@*");
}
// user ban
if (self.config.banUser) {
console.log("INFO: banning user '" + user + "'");
self.client.send("mode", channel, "+b", "*!" + user + "@*");
}
// host ban
if (self.config.banHost) {
console.log("INFO: banning host '" + message.host + "'");
self.client.send("mode", channel, "+b", "*!*@" + message.host);
}
// update blacklist
if (newSpammerFound && self.config.blackListFile) {
let data = JSON.stringify(self.blackList);
fs.writeFileSync(self.config.blackListFile, data);
}
};
new IrcAntiSpam(config);