added cmd_bots (lists all bots)
[NeonServV5.git] / src / bot_NeonSpam.c
1 /* bot_NeonSpam.c - NeonServ v5.2
2  * Copyright (C) 2011  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
18 #include "bot_NeonSpam.h"
19 #include "modcmd.h"
20 #include "IRCParser.h"
21 #include "IRCEvents.h"
22 #include "UserNode.h"
23 #include "ChanNode.h"
24 #include "ChanUser.h"
25 #include "ModeNode.h"
26 #include "BanNode.h"
27 #include "ClientSocket.h"
28 #include "mysqlConn.h"
29 #include "lang.h"
30 #include "HandleInfoHandler.h"
31 #include "WHOHandler.h"
32 #include "DBHelper.h"
33 #include "tools.h"
34 #include "timeq.h"
35 #include "version.h"
36 #include "EventLogger.h"
37 #include "bots.h"
38 #include "cmd_neonserv.h"
39 #include "cmd_neonspam.h"
40
41 #define BOTID 2
42 #define BOTALIAS "NeonSpam"
43
44 static const struct default_language_entry msgtab[] = {
45     {"SS_SET_PERCENT", "%u is an invalid percent value (valid: 0-100)"}, /* {ARGS: 120} */
46     {"SS_SET_SENSIBILITY", "%s is an invalid sensibility format. (valid: amount:time   e.g. 5:10)"}, /* {ARGS: "möp"} */
47     {"SS_SET_SENSIBILITY_AMOUNT", "%d is an invalid amount value. (valid: %d-%d)"}, /* {ARGS: 120, 1, 5} */
48     {"SS_SET_SENSIBILITY_TIME", "%d is an invalid time value. (valid: %d-%d)"}, /* {ARGS: 300, 15, 180} */
49     {"SS_SET_SPAMLIMIT", "%d is an invalid spamlimit value. (valid: %d-%d)"}, /* {ARGS: 120, 2, 8} */
50     {"SS_SET_OPTION_SpamReaction_0", "Kick"},
51     {"SS_SET_OPTION_SpamReaction_1", "KickBan"},
52     {"SS_SET_OPTION_SpamReaction_2", "Timed Ban"},
53     {"SS_SET_OPTION_FloodReaction_0", "Kick"},
54     {"SS_SET_OPTION_FloodReaction_1", "KickBan"},
55     {"SS_SET_OPTION_FloodReaction_2", "Timed Ban"},
56     {"SS_SET_OPTION_JoinReaction_0", "Kick"},
57     {"SS_SET_OPTION_JoinReaction_1", "KickBan"},
58     {"SS_SET_OPTION_JoinReaction_2", "Timed Ban"},
59     {"SS_SET_OPTION_CapsReaction_0", "Kick"},
60     {"SS_SET_OPTION_CapsReaction_1", "KickBan"},
61     {"SS_SET_OPTION_CapsReaction_2", "Timed Ban"},
62     {"SS_SET_OPTION_DigitReaction_0", "Kick"},
63     {"SS_SET_OPTION_DigitReaction_1", "KickBan"},
64     {"SS_SET_OPTION_DigitReaction_2", "Timed Ban"},
65     {NULL, NULL}
66 };
67
68 static unsigned int convertNeonSpamSettingsToFlags(char *str);
69 static int loadNeonSpamSettings(struct ChanNode *chan);
70 static void createSpamNode(struct ChanUser *chanuser);
71 static void freeJoinNode(struct NeonSpamJoinNode *joinnode);
72 static struct NeonSpamJoinNode *getNeonSpamJoinNode(struct ChanUser *chanuser);
73
74 #define SPAMSERV_CHECK_IGNORE 0
75 #define SPAMSERV_CHECK_WARN   1
76 #define SPAMSERV_CHECK_PUNISH 2
77 #define SPAMSERV_CHECK_DEAD   3 /* scanner has already killed the user */
78
79 #define SPAMSERV_MSG_SPAM       "Spamming"
80 #define SPAMSERV_MSG_FLOOD      "Flooding the channel/network"
81 #define SPAMSERV_MSG_ADV        "Advertising"
82 #define SPAMSERV_MSG_JOINFLOOD  "Join flooding the channel"
83 #define SPAMSERV_MSG_WARNING    "%s is against the channel rules"
84 #define SPAMSERV_MSG_BOTNET     "BotNet detected."
85 #define SPAMSERV_MSG_CAPS       "Using too many chars in UPPER CASE"
86 #define SPAMSERV_MSG_DIGIT      "Using too many numeric chars"
87
88 //EVENTS
89 #include "event_neonspam_join.c"
90 #include "event_neonspam_chanmsg.c"
91
92 static void neonspam_bot_ready(struct ClientSocket *client) {
93     MYSQL_RES *res;
94     MYSQL_ROW row;
95     
96     printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
97     res = mysql_use();
98     if ((row = mysql_fetch_row(res)) != NULL) {
99         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
100     }
101     
102     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);
103     res = mysql_use();
104     
105     while ((row = mysql_fetch_row(res)) != NULL) {
106         putsock(client, "JOIN %s %s", row[0], row[1]);
107     }
108 }
109
110 static void neonspam_trigger_callback(struct ChanNode *chan, char *trigger) {
111     MYSQL_RES *res;
112     MYSQL_ROW row;
113     loadChannelSettings(chan);
114     printf_mysql_query("SELECT `trigger` FROM `bot_channels` LEFT JOIN `bots` ON `botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, BOTID);
115     res = mysql_use();
116     row = mysql_fetch_row(res);
117     strcpy(trigger, (strlen(row[0]) ? row[0] : "~"));
118 }
119
120 static void start_bots() {
121     struct UserNode *user;
122     struct ClientSocket *client;
123     MYSQL_RES *res, *res2;
124     MYSQL_ROW row;
125     
126     printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
127     res = mysql_use();
128     
129     while ((row = mysql_fetch_row(res)) != NULL) {
130         user = addUser(row[0]);
131         strcpy(user->ident, row[1]);
132         strcpy(user->realname, row[2]);
133         user->flags |= USERFLAG_ISBOT;
134         client = create_socket(row[3], atoi(row[4]), row[5], user);
135         client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
136         client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
137         client->botid = BOTID;
138         client->clientid = atoi(row[7]);
139         connect_socket(client);
140     }
141     
142     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
143     res2 = mysql_use();
144     while ((row = mysql_fetch_row(res2)) != NULL) {
145         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
146             if(row[2] && strcmp(row[2], "")) {
147                 bind_set_parameters(BOTID, row[0], row[2]);
148             }
149             if(row[3]) {
150                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
151             }
152             if(row[4]) {
153                 bind_set_channel_access(BOTID, row[0], row[4]);
154             }
155         }
156     }
157     bind_unbound_required_functions(BOTID);
158 }
159
160 char* convertNeonSpamSettingsToString(unsigned int flags, char *buffer) {
161     int pos = 0;
162     unsigned int i;
163     int j = 0;
164     char *chars = SPAMSETTINGS_CHARS;
165     for(i = 1; i <= SPAMSETTINGS_FLAGS; i = i << 1) {
166         if(flags & i)
167             buffer[pos++] = chars[j];
168         j++;
169     }
170     buffer[pos] = '\0';
171     return buffer;
172 }
173
174 static unsigned int convertNeonSpamSettingsToFlags(char *str) {
175     unsigned int i = 1, flags = 0;
176     int j = 0;
177     char *chars = SPAMSETTINGS_CHARS;
178     while(*str) {
179         for(; i <= SPAMSETTINGS_FLAGS; i = i << 1) {
180             if(*str == chars[j]) {
181                 flags |= i;
182                 j++;
183                 i = i << 1;
184                 break;
185             }
186             j++;
187         }
188         str++;
189     }
190     return flags;
191 }
192
193 static int loadNeonSpamSettings(struct ChanNode *chan) {
194     if(chan->spam_settings) return 0;
195     struct NeonSpamSettings *settings = malloc(sizeof(*settings));
196     if(!settings) {
197         perror("malloc() failed");
198         return 0;
199     }
200     MYSQL_RES *res;
201     MYSQL_ROW row, defaults = NULL;
202     loadChannelSettings(chan);
203     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` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
204     res = mysql_use();
205     row = mysql_fetch_row(res);
206     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]) {
207         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` FROM `channels` WHERE `channel_name` = 'defaults'");
208         res = mysql_use();
209         defaults = mysql_fetch_row(res);
210     }
211     settings->flags = convertNeonSpamSettingsToFlags(row[0] ? row[0] : defaults[0]);
212     settings->spam_amount = atoi(row[1] ? row[1] : defaults[1]);
213     settings->exceptlevel[SPAMSETTINGS_SPAMEXCINDEX] = atoi(row[2] ? row[2] : defaults[2]);
214     settings->sensibility_amount[SPAMSETTINGS_FLOODSENINDEX] = atoi(row[3] ? row[3] : defaults[3]);
215     settings->sensibility_time[SPAMSETTINGS_FLOODSENINDEX] = atoi(row[4] ? row[4] : defaults[4]);
216     settings->exceptlevel[SPAMSETTINGS_FLOODEXCINDEX] = atoi(row[5] ? row[5] : defaults[5]);
217     settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX] = atoi(row[6] ? row[6] : defaults[6]);
218     settings->sensibility_time[SPAMSETTINGS_JOINSENINDEX] = atoi(row[7] ? row[7] : defaults[7]);
219     settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] = atoi(row[8] ? row[8] : defaults[8]);
220     settings->percent[SPAMSETTINGS_CAPSPERCENTINDEX] = atoi(row[9] ? row[9] : defaults[9]);
221     settings->exceptlevel[SPAMSETTINGS_CAPSEXCINDEX] = atoi(row[10] ? row[10] : defaults[10]);
222     settings->percent[SPAMSETTINGS_DIGITPERCENTINDEX] = atoi(row[11] ? row[11] : defaults[11]);
223     settings->exceptlevel[SPAMSETTINGS_DIGITEXCINDEX] = atoi(row[12] ? row[12] : defaults[12]);
224     settings->join_nodes = NULL;
225     settings->lastmsg_time = 0;
226     int i;
227     for(i = 0; i < BOTNETSCAN_USERS; i++) 
228         settings->botnicks[i] = NULL;
229     chan->spam_settings = settings;
230     return 1;
231 }
232
233 void freeNeonSpamSettings(struct NeonSpamSettings *settings) {
234     struct NeonSpamJoinNode *joinnode, *nextjoinnode;
235     for(joinnode = settings->join_nodes; joinnode; joinnode = nextjoinnode) {
236         nextjoinnode = joinnode->next;
237         freeJoinNode(joinnode);
238     }
239     free(settings);
240 }
241
242 static void freeJoinNode(struct NeonSpamJoinNode *joinnode) {
243     free(joinnode->ident);
244     free(joinnode->host);
245     free(joinnode);
246 }
247
248 static struct NeonSpamJoinNode *getNeonSpamJoinNode(struct ChanUser *chanuser) {
249     struct NeonSpamJoinNode *joinnode, *prevjoinnode = NULL, *nextjoinnode, *result = NULL;
250     for(joinnode = chanuser->chan->spam_settings->join_nodes; joinnode; joinnode = nextjoinnode) {
251         nextjoinnode = joinnode->next;
252         if(!stricmp(joinnode->ident, chanuser->user->ident) && !stricmp(joinnode->host, chanuser->user->host)) {
253             prevjoinnode = joinnode;
254             result = joinnode;
255         } else if(time(0) - joinnode->last_penalty_update > MAX_JOIN_TIME) {
256             freeJoinNode(joinnode);
257             if(prevjoinnode)
258                 prevjoinnode->next = nextjoinnode;
259             else
260                 chanuser->chan->spam_settings->join_nodes = nextjoinnode;
261         } else 
262             prevjoinnode = joinnode;
263     }
264     if(result)
265         return result;
266     joinnode = malloc(sizeof(*joinnode));
267     if(!joinnode) {
268         perror("malloc() failed");
269         return NULL;
270     }
271     joinnode->ident = strdup(chanuser->user->ident);
272     joinnode->host = strdup(chanuser->user->host);
273     joinnode->last_penalty_update = time(0);
274     joinnode->joinpenalty = 0;
275     joinnode->next = chanuser->chan->spam_settings->join_nodes;
276     chanuser->chan->spam_settings->join_nodes = joinnode;
277     return joinnode;
278 }
279
280 static void createSpamNode(struct ChanUser *chanuser) {
281     struct NeonSpamNode *spamnode = malloc(sizeof(*spamnode));
282     if(!spamnode) {
283         perror("malloc() failed");
284         return;
285     }
286     spamnode->lastmsg = 0;
287     spamnode->spamcount = 0;
288     spamnode->floodpenalty = 0;
289     spamnode->last_penalty_update = time(0);
290     chanuser->spamnode = spamnode;
291 }
292
293 void init_NeonSpam() {
294     
295     set_bot_alias(BOTID, BOTALIAS);
296     start_bots();
297     
298     //register events
299     bind_bot_ready(neonspam_bot_ready);
300     bind_join(neonspam_event_join);
301     bind_chanmsg(neonspam_event_chanmsg);
302     bind_privctcp(general_event_privctcp);
303     
304     set_trigger_callback(BOTID, neonspam_trigger_callback);
305     
306     register_default_language_table(msgtab);
307 }
308
309 void loop_NeonSpam() {
310     
311 }
312
313 void free_NeonSpam() {
314     
315 }
316
317 #undef BOTID
318 #undef BOTALIAS