modified code to use IOHandler functions instead of own ones
[NeonServV5.git] / src / modules / NeonSpam.mod / bot_NeonSpam.c
1 /* bot_NeonSpam.c - NeonServ v5.5
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_bot_ready(struct ClientSocket *client) {
107     if(client->botid != BOTID)
108         return;
109     MYSQL_RES *res;
110     MYSQL_ROW row;
111     
112     printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
113     res = mysql_use();
114     if ((row = mysql_fetch_row(res)) != NULL) {
115         if(row[1] && row[2]) {
116             putsock(client, "OPER %s %s", row[1], row[2]);
117         }
118         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
119     }
120     
121     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);
122     res = mysql_use();
123     
124     while ((row = mysql_fetch_row(res)) != NULL) {
125         putsock(client, "JOIN %s %s", row[0], row[1]);
126     }
127 }
128
129 static void neonspam_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
130     MYSQL_RES *res;
131     MYSQL_ROW row;
132     loadChannelSettings(chan);
133     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
134         strcpy(trigger, "~");
135         return;
136     }
137     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);
138     res = mysql_use();
139     if(!(row = mysql_fetch_row(res))) {
140         strcpy(trigger, "~");
141         return;
142     }
143     if(row[0] && *row[0])
144         strcpy(trigger, row[0]);
145     else
146         strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "~"));
147 }
148
149 static void start_bots(int type) {
150     struct ClientSocket *client;
151     MYSQL_RES *res, *res2;
152     MYSQL_ROW row;
153     
154     if(type == MODSTATE_STARTSTOP) {
155         printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
156         res = mysql_use();
157         
158         while ((row = mysql_fetch_row(res)) != NULL) {
159             client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
160             client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
161             client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
162             client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
163             client->flags |= SOCKET_FLAG_REQUEST_INVITE | SOCKET_FLAG_REQUEST_OP;
164             client->botid = BOTID;
165             client->clientid = atoi(row[7]);
166             connect_socket(client);
167         }
168     }
169     
170     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
171     res2 = mysql_use();
172     while ((row = mysql_fetch_row(res2)) != NULL) {
173         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
174             if(row[2] && strcmp(row[2], "")) {
175                 bind_set_parameters(BOTID, row[0], row[2]);
176             }
177             if(row[3]) {
178                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
179             }
180             if(row[4]) {
181                 bind_set_channel_access(BOTID, row[0], row[4]);
182             }
183             if(strcmp(row[5], "0"))
184                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
185         }
186     }
187     bind_unbound_required_functions(BOTID);
188 }
189
190 char* convertNeonSpamSettingsToString(unsigned int flags, char *buffer) {
191     int pos = 0;
192     unsigned int i;
193     int j = 0;
194     char *chars = SPAMSETTINGS_CHARS;
195     for(i = 1; i <= SPAMSETTINGS_FLAGS; i = i << 1) {
196         if(flags & i)
197             buffer[pos++] = chars[j];
198         j++;
199     }
200     buffer[pos] = '\0';
201     return buffer;
202 }
203
204 static unsigned int convertNeonSpamSettingsToFlags(char *str) {
205     unsigned int i = 1, flags = 0;
206     int j = 0;
207     char *chars = SPAMSETTINGS_CHARS;
208     while(*str) {
209         for(; i <= SPAMSETTINGS_FLAGS; i = i << 1) {
210             if(*str == chars[j]) {
211                 flags |= i;
212                 j++;
213                 i = i << 1;
214                 break;
215             }
216             j++;
217         }
218         str++;
219     }
220     return flags;
221 }
222
223 int loadNeonSpamSettings(struct ChanNode *chan) {
224     if(chan->spam_settings) return 0;
225     struct NeonSpamSettings *settings = malloc(sizeof(*settings));
226     if(!settings) {
227         perror("malloc() failed");
228         return 0;
229     }
230     MYSQL_RES *res;
231     MYSQL_ROW row, defaults = NULL;
232     loadChannelSettings(chan);
233     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);
234     res = mysql_use();
235     if(!(row = mysql_fetch_row(res)))
236         return 0;
237     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]) {
238         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'");
239         res = mysql_use();
240         defaults = mysql_fetch_row(res);
241     }
242     settings->flags = convertNeonSpamSettingsToFlags(row[0] ? row[0] : defaults[0]);
243     settings->spam_amount = atoi(row[1] ? row[1] : defaults[1]);
244     settings->exceptlevel[SPAMSETTINGS_SPAMEXCINDEX] = atoi(row[2] ? row[2] : defaults[2]);
245     settings->sensibility_amount[SPAMSETTINGS_FLOODSENINDEX] = atoi(row[3] ? row[3] : defaults[3]);
246     settings->sensibility_time[SPAMSETTINGS_FLOODSENINDEX] = atoi(row[4] ? row[4] : defaults[4]);
247     settings->exceptlevel[SPAMSETTINGS_FLOODEXCINDEX] = atoi(row[5] ? row[5] : defaults[5]);
248     settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX] = atoi(row[6] ? row[6] : defaults[6]);
249     settings->sensibility_time[SPAMSETTINGS_JOINSENINDEX] = atoi(row[7] ? row[7] : defaults[7]);
250     settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] = atoi(row[8] ? row[8] : defaults[8]);
251     settings->percent[SPAMSETTINGS_CAPSPERCENTINDEX] = atoi(row[9] ? row[9] : defaults[9]);
252     settings->exceptlevel[SPAMSETTINGS_CAPSEXCINDEX] = atoi(row[10] ? row[10] : defaults[10]);
253     settings->percent[SPAMSETTINGS_DIGITPERCENTINDEX] = atoi(row[11] ? row[11] : defaults[11]);
254     settings->exceptlevel[SPAMSETTINGS_DIGITEXCINDEX] = atoi(row[12] ? row[12] : defaults[12]);
255     settings->exceptlevel[SPAMSETTINGS_BADWORDEXCINDEX] = atoi(row[13] ? row[13] : defaults[13]);
256     settings->join_nodes = NULL;
257     settings->lastmsg_time = 0;
258     int i;
259     for(i = 0; i < BOTNETSCAN_USERS; i++) 
260         settings->botnicks[i] = NULL;
261     settings->badwords = NULL;
262     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);
263     res = mysql_use();
264     while((row = mysql_fetch_row(res))) {
265         struct NeonSpamBadword *badword = malloc(sizeof(*badword));
266         badword->id = atoi(row[3]);
267         badword->badword = strdup(row[0]);
268         badword->use_defaults = atoi(row[7]);
269         badword->exceptlevel = atoi(row[6]);
270         badword->scan_ops = atoi(row[4]);
271         badword->scan_voice = atoi(row[5]);
272         badword->use_default_reaction = atoi(row[8]);
273         badword->reaction = atoi(row[1]);
274         badword->reaction_time = atoi(row[2]);
275         badword->next = settings->badwords;
276         settings->badwords = badword;
277     }
278     chan->spam_settings = settings;
279     return 1;
280 }
281
282 void freeNeonSpamSettings(struct NeonSpamSettings *settings) {
283     struct NeonSpamJoinNode *joinnode, *nextjoinnode;
284     for(joinnode = settings->join_nodes; joinnode; joinnode = nextjoinnode) {
285         nextjoinnode = joinnode->next;
286         freeJoinNode(joinnode);
287     }
288     struct NeonSpamBadword *badword, *nextbadword;
289     for(badword = settings->badwords; badword; badword = nextbadword) {
290         nextbadword = badword->next;
291         free(badword->badword);
292         free(badword);
293     }
294     free(settings);
295 }
296
297 static void freeJoinNode(struct NeonSpamJoinNode *joinnode) {
298     free(joinnode->ident);
299     free(joinnode->host);
300     free(joinnode);
301 }
302
303 static struct NeonSpamJoinNode *getNeonSpamJoinNode(struct ChanUser *chanuser) {
304     struct NeonSpamJoinNode *joinnode, *prevjoinnode = NULL, *nextjoinnode, *result = NULL;
305     for(joinnode = chanuser->chan->spam_settings->join_nodes; joinnode; joinnode = nextjoinnode) {
306         nextjoinnode = joinnode->next;
307         if(!stricmp(joinnode->ident, chanuser->user->ident) && !stricmp(joinnode->host, chanuser->user->host)) {
308             prevjoinnode = joinnode;
309             result = joinnode;
310         } else if(time(0) - joinnode->last_penalty_update > MAX_JOIN_TIME) {
311             freeJoinNode(joinnode);
312             if(prevjoinnode)
313                 prevjoinnode->next = nextjoinnode;
314             else
315                 chanuser->chan->spam_settings->join_nodes = nextjoinnode;
316         } else 
317             prevjoinnode = joinnode;
318     }
319     if(result)
320         return result;
321     joinnode = malloc(sizeof(*joinnode));
322     if(!joinnode) {
323         perror("malloc() failed");
324         return NULL;
325     }
326     joinnode->ident = strdup(chanuser->user->ident);
327     joinnode->host = strdup(chanuser->user->host);
328     joinnode->last_penalty_update = time(0);
329     joinnode->joinpenalty = 0;
330     joinnode->next = chanuser->chan->spam_settings->join_nodes;
331     chanuser->chan->spam_settings->join_nodes = joinnode;
332     return joinnode;
333 }
334
335 static void createSpamNode(struct ChanUser *chanuser) {
336     struct NeonSpamNode *spamnode = malloc(sizeof(*spamnode));
337     if(!spamnode) {
338         perror("malloc() failed");
339         return;
340     }
341     spamnode->lastmsg = 0;
342     spamnode->spamcount = 0;
343     spamnode->floodpenalty = 0;
344     spamnode->last_penalty_update = time(0);
345     chanuser->spamnode = spamnode;
346 }
347
348 static int neonspam_event_freechan(struct ChanNode *chan) {
349     if(chan->spam_settings)
350         freeNeonSpamSettings(chan->spam_settings);
351     return 1;
352 }
353
354 static void neonspam_event_invite(struct ClientSocket *client, struct UserNode *user, char *channel) {
355         if(client->botid != BOTID)
356                 return;
357     MYSQL_RES *res;
358     MYSQL_ROW row;
359     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);
360     res = mysql_use();
361     if ((row = mysql_fetch_row(res)) == NULL) {
362         reply(client, user, "NS_INVITE_FAIL", channel, client->user->nick);
363         return;
364     }
365     if(!strcmp(row[2], "1")) {
366         reply(client, user, "MODCMD_CHAN_SUSPENDED");
367         return;
368     }
369     int botid = atoi(row[0]);
370     struct ClientSocket *bot;
371     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
372         if(bot->clientid == botid)
373             break;
374     }
375     if(bot) {
376         struct ChanNode *chan = getChanByName(channel);
377         if(chan && isUserOnChan(bot->user, chan)) {
378             reply(client, user, "NS_INVITE_ON_CHAN", bot->user->nick, chan->name);
379         } else
380             putsock(bot, "JOIN %s", channel);
381     }
382 }
383
384 void init_NeonSpam(int type) {
385     
386     set_bot_alias(BOTID, BOTALIAS);
387     start_bots(type);
388     
389     if(type == MODSTATE_REBIND) return;
390     
391     //register events
392     bind_bot_ready(neonspam_bot_ready, module_id);
393     bind_join(neonspam_event_join, module_id);
394     bind_chanmsg(neonspam_event_chanmsg, module_id);
395     bind_privctcp(general_event_privctcp, module_id);
396     bind_freechan(neonspam_event_freechan, module_id);
397     bind_invite(neonspam_event_invite, module_id);
398         
399     set_trigger_callback(BOTID, module_id, neonspam_trigger_callback);
400     
401     register_default_language_table(msgtab);
402 }
403
404 void free_NeonSpam(int type) {
405     unbind_allcmd(BOTID);
406     if(type == MODSTATE_STARTSTOP) {
407         //disconnect all our bots
408         struct ClientSocket *client;
409         for(client = getBots(0, NULL); client; client = getBots(0, client)) {
410             if(client->botid == BOTID) {
411                 unbind_botwise_allcmd(0, client->clientid);
412                 close_socket(client);
413                 break;
414             }
415         }
416     }
417 }
418
419 #undef BOTID
420 #undef BOTALIAS