added NeonBackup bot
[NeonServV5.git] / src / modules / NeonSpam.mod / bot_NeonSpam.c
1 /* bot_NeonSpam.c - NeonServ v5.4
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "../module.h"
18 #include "../botid.h"
19
20 #include "bot_NeonSpam.h"
21 #include "../../modcmd.h"
22 #include "../../IRCParser.h"
23 #include "../../IRCEvents.h"
24 #include "../../UserNode.h"
25 #include "../../ChanNode.h"
26 #include "../../ChanUser.h"
27 #include "../../ModeNode.h"
28 #include "../../BanNode.h"
29 #include "../../ClientSocket.h"
30 #include "../../mysqlConn.h"
31 #include "../../lang.h"
32 #include "../../HandleInfoHandler.h"
33 #include "../../WHOHandler.h"
34 #include "../../DBHelper.h"
35 #include "../../tools.h"
36 #include "../../timeq.h"
37 #include "../../version.h"
38 #include "../../EventLogger.h"
39 #include "../../bots.h"
40 #include "cmd_neonspam.h"
41
42 #define BOTID NEONSPAM_BOTID
43 #define BOTALIAS "NeonSpam"
44
45 static const struct default_language_entry msgtab[] = {
46     {"SS_SET_PERCENT", "%u is an invalid percent value (valid: 0-100)"}, /* {ARGS: 120} */
47     {"SS_SET_SENSIBILITY", "%s is an invalid sensibility format. (valid: amount:time   e.g. 5:10)"}, /* {ARGS: "möp"} */
48     {"SS_SET_SENSIBILITY_AMOUNT", "%d is an invalid amount value. (valid: %d-%d)"}, /* {ARGS: 120, 1, 5} */
49     {"SS_SET_SENSIBILITY_TIME", "%d is an invalid time value. (valid: %d-%d)"}, /* {ARGS: 300, 15, 180} */
50     {"SS_SET_SPAMLIMIT", "%d is an invalid spamlimit value. (valid: %d-%d)"}, /* {ARGS: 120, 2, 8} */
51     {"SS_SET_OPTION_SpamReaction_0", "Kick"},
52     {"SS_SET_OPTION_SpamReaction_1", "KickBan"},
53     {"SS_SET_OPTION_SpamReaction_2", "Timed Ban"},
54     {"SS_SET_OPTION_FloodReaction_0", "Kick"},
55     {"SS_SET_OPTION_FloodReaction_1", "KickBan"},
56     {"SS_SET_OPTION_FloodReaction_2", "Timed Ban"},
57     {"SS_SET_OPTION_JoinReaction_0", "Kick"},
58     {"SS_SET_OPTION_JoinReaction_1", "KickBan"},
59     {"SS_SET_OPTION_JoinReaction_2", "Timed Ban"},
60     {"SS_SET_OPTION_CapsReaction_0", "Kick"},
61     {"SS_SET_OPTION_CapsReaction_1", "KickBan"},
62     {"SS_SET_OPTION_CapsReaction_2", "Timed Ban"},
63     {"SS_SET_OPTION_DigitReaction_0", "Kick"},
64     {"SS_SET_OPTION_DigitReaction_1", "KickBan"},
65     {"SS_SET_OPTION_DigitReaction_2", "Timed Ban"},
66     {"SS_SET_OPTION_BadwordReaction_0", "Kick"},
67     {"SS_SET_OPTION_BadwordReaction_1", "KickBan"},
68     {"SS_SET_OPTION_BadwordReaction_2", "Timed Ban"},
69     {"SS_BADWORD_ADDED", "Badword '%s' added. (ID: %d)"}, /* {ARGS: "NeonServ is bad", 1} */
70     {"SS_BADWORD_ID_UNKNOWN", "Badword ID %d was not found in %s."}, /* {ARGS: 1, "#channel"} */
71     {"SS_BADWORD_DELETED", "Badword '%s' deleted. (ID: %d)"}, /* {ARGS: "NeonServ is bad", 1} */
72     {"SS_BADWORDS_REACTION_0", "Kick"},
73     {"SS_BADWORDS_REACTION_1", "KickBan"},
74     {"SS_BADWORDS_REACTION_2", "Timed Ban"},
75     {"SS_BADWORDS_ID", "Id"},
76     {"SS_BADWORDS_PATTERN", "Pattern"},
77     {"SS_BADWORDS_SCAN", "Scan"},
78     {"SS_BADWORDS_REACTION", "Reaction"},
79     {NULL, NULL}
80 };
81
82 static unsigned int convertNeonSpamSettingsToFlags(char *str);
83 static void createSpamNode(struct ChanUser *chanuser);
84 static void freeJoinNode(struct NeonSpamJoinNode *joinnode);
85 static struct NeonSpamJoinNode *getNeonSpamJoinNode(struct ChanUser *chanuser);
86
87 #define SPAMSERV_CHECK_IGNORE 0
88 #define SPAMSERV_CHECK_WARN   1
89 #define SPAMSERV_CHECK_PUNISH 2
90 #define SPAMSERV_CHECK_DEAD   3 /* scanner has already killed the user */
91
92 #define SPAMSERV_MSG_SPAM       "Spamming"
93 #define SPAMSERV_MSG_FLOOD      "Flooding the channel/network"
94 #define SPAMSERV_MSG_ADV        "Advertising"
95 #define SPAMSERV_MSG_JOINFLOOD  "Join flooding the channel"
96 #define SPAMSERV_MSG_WARNING    "%s is against the channel rules"
97 #define SPAMSERV_MSG_BOTNET     "BotNet detected."
98 #define SPAMSERV_MSG_CAPS       "Using too many chars in UPPER CASE"
99 #define SPAMSERV_MSG_DIGIT      "Using too many numeric chars"
100 #define SPAMSERV_MSG_BADWORD    "Your message contained a forbidden word."
101
102 //EVENTS
103 #include "event_neonspam_join.c"
104 #include "event_neonspam_chanmsg.c"
105
106 static void neonspam_event_kick(struct UserNode *user, struct ChanUser *target, char *reason) {
107     struct ChanNode *chan = target->chan;
108     if(isBot(target->user)) {
109         struct ClientSocket *client = getChannelBot(chan, 0);
110         struct ClientSocket *bot = client;
111         for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
112             if(client->user == target->user) {
113                 break;
114             }
115         }
116         if(!client) return;
117         if(bot && bot != client && (isModeSet(chan->modes, 'i') || isModeSet(chan->modes, 'a') || isModeSet(chan->modes, 'l'))) {
118             struct ChanUser *chanuser = getChanUser(bot->user, chan);
119             if(chanuser && chanuser->flags & CHANUSERFLAG_OPPED)
120                 putsock(bot, "INVITE %s %s", target->user->nick, chan->name);
121         }
122         char *key = "";
123         if(isModeSet(chan->modes, 'k')) {
124             key = getModeValue(chan->modes, 'k');
125         }
126         putsock(client, "JOIN %s %s", chan->name, key);
127         return;
128     }
129 }
130
131 static void neonspam_bot_ready(struct ClientSocket *client) {
132     MYSQL_RES *res;
133     MYSQL_ROW row;
134     
135     printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
136     res = mysql_use();
137     if ((row = mysql_fetch_row(res)) != NULL) {
138         if(row[1] && row[2]) {
139             putsock(client, "OPER %s %s", row[1], row[2]);
140         }
141         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
142     }
143     
144     printf_mysql_query("SELECT `channel_name`, `channel_key` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `botid` = '%d' AND `suspended` = '0'", client->clientid);
145     res = mysql_use();
146     
147     while ((row = mysql_fetch_row(res)) != NULL) {
148         putsock(client, "JOIN %s %s", row[0], row[1]);
149     }
150 }
151
152 static void neonspam_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
153     MYSQL_RES *res;
154     MYSQL_ROW row;
155     loadChannelSettings(chan);
156     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
157         strcpy(trigger, "~");
158         return;
159     }
160     printf_mysql_query("SELECT `trigger`, `defaulttrigger` FROM `bot_channels` LEFT JOIN `bots` ON `botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, BOTID);
161     res = mysql_use();
162     if(!(row = mysql_fetch_row(res))) {
163         strcpy(trigger, "~");
164         return;
165     }
166     if(row[0] && *row[0])
167         strcpy(trigger, row[0]);
168     else
169         strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "~"));
170 }
171
172 static void start_bots(int type) {
173     struct ClientSocket *client;
174     MYSQL_RES *res, *res2;
175     MYSQL_ROW row;
176     
177     if(type == MODSTATE_STARTSTOP) {
178         printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
179         res = mysql_use();
180         
181         while ((row = mysql_fetch_row(res)) != NULL) {
182             client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
183             client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
184             client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
185             client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
186             client->flags |= SOCKET_FLAG_REQUEST_INVITE;
187             client->botid = BOTID;
188             client->clientid = atoi(row[7]);
189             connect_socket(client);
190         }
191     }
192     
193     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
194     res2 = mysql_use();
195     while ((row = mysql_fetch_row(res2)) != NULL) {
196         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
197             if(row[2] && strcmp(row[2], "")) {
198                 bind_set_parameters(BOTID, row[0], row[2]);
199             }
200             if(row[3]) {
201                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
202             }
203             if(row[4]) {
204                 bind_set_channel_access(BOTID, row[0], row[4]);
205             }
206             if(strcmp(row[5], "0"))
207                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
208         }
209     }
210     bind_unbound_required_functions(BOTID);
211 }
212
213 char* convertNeonSpamSettingsToString(unsigned int flags, char *buffer) {
214     int pos = 0;
215     unsigned int i;
216     int j = 0;
217     char *chars = SPAMSETTINGS_CHARS;
218     for(i = 1; i <= SPAMSETTINGS_FLAGS; i = i << 1) {
219         if(flags & i)
220             buffer[pos++] = chars[j];
221         j++;
222     }
223     buffer[pos] = '\0';
224     return buffer;
225 }
226
227 static unsigned int convertNeonSpamSettingsToFlags(char *str) {
228     unsigned int i = 1, flags = 0;
229     int j = 0;
230     char *chars = SPAMSETTINGS_CHARS;
231     while(*str) {
232         for(; i <= SPAMSETTINGS_FLAGS; i = i << 1) {
233             if(*str == chars[j]) {
234                 flags |= i;
235                 j++;
236                 i = i << 1;
237                 break;
238             }
239             j++;
240         }
241         str++;
242     }
243     return flags;
244 }
245
246 int loadNeonSpamSettings(struct ChanNode *chan) {
247     if(chan->spam_settings) return 0;
248     struct NeonSpamSettings *settings = malloc(sizeof(*settings));
249     if(!settings) {
250         perror("malloc() failed");
251         return 0;
252     }
253     MYSQL_RES *res;
254     MYSQL_ROW row, defaults = NULL;
255     loadChannelSettings(chan);
256     printf_mysql_query("SELECT `channel_scanner`, `channel_spam_limit`, `channel_spam_except`, `channel_flood_limit`, `channel_flood_time`, `channel_flood_except`, `channel_join_limit`, `channel_join_time`, `channel_join_except`, `channel_caps_percent`, `channel_caps_except`, `channel_digit_percent`, `channel_digit_except`, `channel_badword_except` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
257     res = mysql_use();
258     if(!(row = mysql_fetch_row(res)))
259         return 0;
260     if(!row[0] || !row[1] || !row[2] || !row[3] || !row[4] || !row[5] || !row[6] || !row[7] || !row[8] || !row[9] || !row[10] || !row[11] || !row[12]) {
261         printf_mysql_query("SELECT `channel_scanner`, `channel_spam_limit`, `channel_spam_except`, `channel_flood_limit`, `channel_flood_time`, `channel_flood_except`, `channel_join_limit`, `channel_join_time`, `channel_join_except`, `channel_caps_percent`, `channel_caps_except`, `channel_digit_percent`, `channel_digit_except`, `channel_badword_except` FROM `channels` WHERE `channel_name` = 'defaults'");
262         res = mysql_use();
263         defaults = mysql_fetch_row(res);
264     }
265     settings->flags = convertNeonSpamSettingsToFlags(row[0] ? row[0] : defaults[0]);
266     settings->spam_amount = atoi(row[1] ? row[1] : defaults[1]);
267     settings->exceptlevel[SPAMSETTINGS_SPAMEXCINDEX] = atoi(row[2] ? row[2] : defaults[2]);
268     settings->sensibility_amount[SPAMSETTINGS_FLOODSENINDEX] = atoi(row[3] ? row[3] : defaults[3]);
269     settings->sensibility_time[SPAMSETTINGS_FLOODSENINDEX] = atoi(row[4] ? row[4] : defaults[4]);
270     settings->exceptlevel[SPAMSETTINGS_FLOODEXCINDEX] = atoi(row[5] ? row[5] : defaults[5]);
271     settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX] = atoi(row[6] ? row[6] : defaults[6]);
272     settings->sensibility_time[SPAMSETTINGS_JOINSENINDEX] = atoi(row[7] ? row[7] : defaults[7]);
273     settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] = atoi(row[8] ? row[8] : defaults[8]);
274     settings->percent[SPAMSETTINGS_CAPSPERCENTINDEX] = atoi(row[9] ? row[9] : defaults[9]);
275     settings->exceptlevel[SPAMSETTINGS_CAPSEXCINDEX] = atoi(row[10] ? row[10] : defaults[10]);
276     settings->percent[SPAMSETTINGS_DIGITPERCENTINDEX] = atoi(row[11] ? row[11] : defaults[11]);
277     settings->exceptlevel[SPAMSETTINGS_DIGITEXCINDEX] = atoi(row[12] ? row[12] : defaults[12]);
278     settings->exceptlevel[SPAMSETTINGS_BADWORDEXCINDEX] = atoi(row[13] ? row[13] : defaults[13]);
279     settings->join_nodes = NULL;
280     settings->lastmsg_time = 0;
281     int i;
282     for(i = 0; i < BOTNETSCAN_USERS; i++) 
283         settings->botnicks[i] = NULL;
284     settings->badwords = NULL;
285     printf_mysql_query("SELECT `badword_match`, `badword_reaction`, `badword_reaction_time`, `badword_id`, `badword_scan_ops`, `badword_scan_voice`, `badword_exceptlevel`, `badword_use_default`, `badword_use_default_reaction` FROM `spamserv_badwords` WHERE `badword_cid` = '%d'", chan->channel_id);
286     res = mysql_use();
287     while((row = mysql_fetch_row(res))) {
288         struct NeonSpamBadword *badword = malloc(sizeof(*badword));
289         badword->id = atoi(row[3]);
290         badword->badword = strdup(row[0]);
291         badword->use_defaults = atoi(row[7]);
292         badword->exceptlevel = atoi(row[6]);
293         badword->scan_ops = atoi(row[4]);
294         badword->scan_voice = atoi(row[5]);
295         badword->use_default_reaction = atoi(row[8]);
296         badword->reaction = atoi(row[1]);
297         badword->reaction_time = atoi(row[2]);
298         badword->next = settings->badwords;
299         settings->badwords = badword;
300     }
301     chan->spam_settings = settings;
302     return 1;
303 }
304
305 void freeNeonSpamSettings(struct NeonSpamSettings *settings) {
306     struct NeonSpamJoinNode *joinnode, *nextjoinnode;
307     for(joinnode = settings->join_nodes; joinnode; joinnode = nextjoinnode) {
308         nextjoinnode = joinnode->next;
309         freeJoinNode(joinnode);
310     }
311     struct NeonSpamBadword *badword, *nextbadword;
312     for(badword = settings->badwords; badword; badword = nextbadword) {
313         nextbadword = badword->next;
314         free(badword->badword);
315         free(badword);
316     }
317     free(settings);
318 }
319
320 static void freeJoinNode(struct NeonSpamJoinNode *joinnode) {
321     free(joinnode->ident);
322     free(joinnode->host);
323     free(joinnode);
324 }
325
326 static struct NeonSpamJoinNode *getNeonSpamJoinNode(struct ChanUser *chanuser) {
327     struct NeonSpamJoinNode *joinnode, *prevjoinnode = NULL, *nextjoinnode, *result = NULL;
328     for(joinnode = chanuser->chan->spam_settings->join_nodes; joinnode; joinnode = nextjoinnode) {
329         nextjoinnode = joinnode->next;
330         if(!stricmp(joinnode->ident, chanuser->user->ident) && !stricmp(joinnode->host, chanuser->user->host)) {
331             prevjoinnode = joinnode;
332             result = joinnode;
333         } else if(time(0) - joinnode->last_penalty_update > MAX_JOIN_TIME) {
334             freeJoinNode(joinnode);
335             if(prevjoinnode)
336                 prevjoinnode->next = nextjoinnode;
337             else
338                 chanuser->chan->spam_settings->join_nodes = nextjoinnode;
339         } else 
340             prevjoinnode = joinnode;
341     }
342     if(result)
343         return result;
344     joinnode = malloc(sizeof(*joinnode));
345     if(!joinnode) {
346         perror("malloc() failed");
347         return NULL;
348     }
349     joinnode->ident = strdup(chanuser->user->ident);
350     joinnode->host = strdup(chanuser->user->host);
351     joinnode->last_penalty_update = time(0);
352     joinnode->joinpenalty = 0;
353     joinnode->next = chanuser->chan->spam_settings->join_nodes;
354     chanuser->chan->spam_settings->join_nodes = joinnode;
355     return joinnode;
356 }
357
358 static void createSpamNode(struct ChanUser *chanuser) {
359     struct NeonSpamNode *spamnode = malloc(sizeof(*spamnode));
360     if(!spamnode) {
361         perror("malloc() failed");
362         return;
363     }
364     spamnode->lastmsg = 0;
365     spamnode->spamcount = 0;
366     spamnode->floodpenalty = 0;
367     spamnode->last_penalty_update = time(0);
368     chanuser->spamnode = spamnode;
369 }
370
371 static int neonspam_event_freechan(struct ChanNode *chan) {
372     if(chan->spam_settings)
373         freeNeonSpamSettings(chan->spam_settings);
374     return 1;
375 }
376
377 static void neonspam_event_invite(struct ClientSocket *client, struct UserNode *user, char *channel) {
378         if(client->botid != BOTID)
379                 return;
380     MYSQL_RES *res;
381     MYSQL_ROW row;
382     printf_mysql_query("SELECT `botid`, `bot_channels`.`id`, `suspended` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `channel_name` = '%s' AND `botclass` = '%d'", escape_string(channel), client->botid);
383     res = mysql_use();
384     if ((row = mysql_fetch_row(res)) == NULL) {
385         reply(client, user, "NS_INVITE_FAIL", channel, client->user->nick);
386         return;
387     }
388     if(!strcmp(row[2], "1")) {
389         reply(client, user, "MODCMD_CHAN_SUSPENDED");
390         return;
391     }
392     int botid = atoi(row[0]);
393     struct ClientSocket *bot;
394     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
395         if(bot->clientid == botid)
396             break;
397     }
398     if(bot) {
399         struct ChanNode *chan = getChanByName(channel);
400         if(chan && isUserOnChan(bot->user, chan)) {
401             reply(client, user, "NS_INVITE_ON_CHAN", bot->user->nick, chan->name);
402         } else
403             putsock(bot, "JOIN %s", channel);
404     }
405 }
406
407 void init_NeonSpam(int type) {
408     
409     set_bot_alias(BOTID, BOTALIAS);
410     start_bots(type);
411     
412     if(type == MODSTATE_REBIND) return;
413     
414     //register events
415     bind_bot_ready(neonspam_bot_ready, module_id);
416     bind_join(neonspam_event_join, module_id);
417     bind_chanmsg(neonspam_event_chanmsg, module_id);
418     bind_privctcp(general_event_privctcp, module_id);
419     bind_kick(neonspam_event_kick, module_id);
420     bind_freechan(neonspam_event_freechan, module_id);
421     bind_invite(neonspam_event_invite, module_id);
422         
423     set_trigger_callback(BOTID, module_id, neonspam_trigger_callback);
424     
425     register_default_language_table(msgtab);
426 }
427
428 void loop_NeonSpam() {
429     
430 }
431
432 void free_NeonSpam(int type) {
433     unbind_allcmd(BOTID);
434     if(type == MODSTATE_STARTSTOP) {
435         //disconnect all our bots
436         struct ClientSocket *client;
437         for(client = getBots(0, NULL); client; client = getBots(0, client)) {
438             if(client->botid == BOTID) {
439                 unbind_botwise_allcmd(0, client->clientid);
440                 close_socket(client);
441                 break;
442             }
443         }
444     }
445 }
446
447 #undef BOTID
448 #undef BOTALIAS