fixed bot user handling (bot should only have a user when connected)
[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 ClientSocket *client;
122     MYSQL_RES *res, *res2;
123     MYSQL_ROW row;
124     
125     printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
126     res = mysql_use();
127     
128     while ((row = mysql_fetch_row(res)) != NULL) {
129         client = create_socket(row[3], atoi(row[4]), row[5], row[0], row[1], row[2]);
130         client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
131         client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
132         client->botid = BOTID;
133         client->clientid = atoi(row[7]);
134         connect_socket(client);
135     }
136     
137     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
138     res2 = mysql_use();
139     while ((row = mysql_fetch_row(res2)) != NULL) {
140         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
141             if(row[2] && strcmp(row[2], "")) {
142                 bind_set_parameters(BOTID, row[0], row[2]);
143             }
144             if(row[3]) {
145                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
146             }
147             if(row[4]) {
148                 bind_set_channel_access(BOTID, row[0], row[4]);
149             }
150         }
151     }
152     bind_unbound_required_functions(BOTID);
153 }
154
155 char* convertNeonSpamSettingsToString(unsigned int flags, char *buffer) {
156     int pos = 0;
157     unsigned int i;
158     int j = 0;
159     char *chars = SPAMSETTINGS_CHARS;
160     for(i = 1; i <= SPAMSETTINGS_FLAGS; i = i << 1) {
161         if(flags & i)
162             buffer[pos++] = chars[j];
163         j++;
164     }
165     buffer[pos] = '\0';
166     return buffer;
167 }
168
169 static unsigned int convertNeonSpamSettingsToFlags(char *str) {
170     unsigned int i = 1, flags = 0;
171     int j = 0;
172     char *chars = SPAMSETTINGS_CHARS;
173     while(*str) {
174         for(; i <= SPAMSETTINGS_FLAGS; i = i << 1) {
175             if(*str == chars[j]) {
176                 flags |= i;
177                 j++;
178                 i = i << 1;
179                 break;
180             }
181             j++;
182         }
183         str++;
184     }
185     return flags;
186 }
187
188 static int loadNeonSpamSettings(struct ChanNode *chan) {
189     if(chan->spam_settings) return 0;
190     struct NeonSpamSettings *settings = malloc(sizeof(*settings));
191     if(!settings) {
192         perror("malloc() failed");
193         return 0;
194     }
195     MYSQL_RES *res;
196     MYSQL_ROW row, defaults = NULL;
197     loadChannelSettings(chan);
198     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);
199     res = mysql_use();
200     row = mysql_fetch_row(res);
201     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]) {
202         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'");
203         res = mysql_use();
204         defaults = mysql_fetch_row(res);
205     }
206     settings->flags = convertNeonSpamSettingsToFlags(row[0] ? row[0] : defaults[0]);
207     settings->spam_amount = atoi(row[1] ? row[1] : defaults[1]);
208     settings->exceptlevel[SPAMSETTINGS_SPAMEXCINDEX] = atoi(row[2] ? row[2] : defaults[2]);
209     settings->sensibility_amount[SPAMSETTINGS_FLOODSENINDEX] = atoi(row[3] ? row[3] : defaults[3]);
210     settings->sensibility_time[SPAMSETTINGS_FLOODSENINDEX] = atoi(row[4] ? row[4] : defaults[4]);
211     settings->exceptlevel[SPAMSETTINGS_FLOODEXCINDEX] = atoi(row[5] ? row[5] : defaults[5]);
212     settings->sensibility_amount[SPAMSETTINGS_JOINSENINDEX] = atoi(row[6] ? row[6] : defaults[6]);
213     settings->sensibility_time[SPAMSETTINGS_JOINSENINDEX] = atoi(row[7] ? row[7] : defaults[7]);
214     settings->exceptlevel[SPAMSETTINGS_JOINEXCINDEX] = atoi(row[8] ? row[8] : defaults[8]);
215     settings->percent[SPAMSETTINGS_CAPSPERCENTINDEX] = atoi(row[9] ? row[9] : defaults[9]);
216     settings->exceptlevel[SPAMSETTINGS_CAPSEXCINDEX] = atoi(row[10] ? row[10] : defaults[10]);
217     settings->percent[SPAMSETTINGS_DIGITPERCENTINDEX] = atoi(row[11] ? row[11] : defaults[11]);
218     settings->exceptlevel[SPAMSETTINGS_DIGITEXCINDEX] = atoi(row[12] ? row[12] : defaults[12]);
219     settings->join_nodes = NULL;
220     settings->lastmsg_time = 0;
221     int i;
222     for(i = 0; i < BOTNETSCAN_USERS; i++) 
223         settings->botnicks[i] = NULL;
224     chan->spam_settings = settings;
225     return 1;
226 }
227
228 void freeNeonSpamSettings(struct NeonSpamSettings *settings) {
229     struct NeonSpamJoinNode *joinnode, *nextjoinnode;
230     for(joinnode = settings->join_nodes; joinnode; joinnode = nextjoinnode) {
231         nextjoinnode = joinnode->next;
232         freeJoinNode(joinnode);
233     }
234     free(settings);
235 }
236
237 static void freeJoinNode(struct NeonSpamJoinNode *joinnode) {
238     free(joinnode->ident);
239     free(joinnode->host);
240     free(joinnode);
241 }
242
243 static struct NeonSpamJoinNode *getNeonSpamJoinNode(struct ChanUser *chanuser) {
244     struct NeonSpamJoinNode *joinnode, *prevjoinnode = NULL, *nextjoinnode, *result = NULL;
245     for(joinnode = chanuser->chan->spam_settings->join_nodes; joinnode; joinnode = nextjoinnode) {
246         nextjoinnode = joinnode->next;
247         if(!stricmp(joinnode->ident, chanuser->user->ident) && !stricmp(joinnode->host, chanuser->user->host)) {
248             prevjoinnode = joinnode;
249             result = joinnode;
250         } else if(time(0) - joinnode->last_penalty_update > MAX_JOIN_TIME) {
251             freeJoinNode(joinnode);
252             if(prevjoinnode)
253                 prevjoinnode->next = nextjoinnode;
254             else
255                 chanuser->chan->spam_settings->join_nodes = nextjoinnode;
256         } else 
257             prevjoinnode = joinnode;
258     }
259     if(result)
260         return result;
261     joinnode = malloc(sizeof(*joinnode));
262     if(!joinnode) {
263         perror("malloc() failed");
264         return NULL;
265     }
266     joinnode->ident = strdup(chanuser->user->ident);
267     joinnode->host = strdup(chanuser->user->host);
268     joinnode->last_penalty_update = time(0);
269     joinnode->joinpenalty = 0;
270     joinnode->next = chanuser->chan->spam_settings->join_nodes;
271     chanuser->chan->spam_settings->join_nodes = joinnode;
272     return joinnode;
273 }
274
275 static void createSpamNode(struct ChanUser *chanuser) {
276     struct NeonSpamNode *spamnode = malloc(sizeof(*spamnode));
277     if(!spamnode) {
278         perror("malloc() failed");
279         return;
280     }
281     spamnode->lastmsg = 0;
282     spamnode->spamcount = 0;
283     spamnode->floodpenalty = 0;
284     spamnode->last_penalty_update = time(0);
285     chanuser->spamnode = spamnode;
286 }
287
288 void init_NeonSpam() {
289     
290     set_bot_alias(BOTID, BOTALIAS);
291     start_bots();
292     
293     //register events
294     bind_bot_ready(neonspam_bot_ready);
295     bind_join(neonspam_event_join);
296     bind_chanmsg(neonspam_event_chanmsg);
297     bind_privctcp(general_event_privctcp);
298     
299     set_trigger_callback(BOTID, neonspam_trigger_callback);
300     
301     register_default_language_table(msgtab);
302 }
303
304 void loop_NeonSpam() {
305     
306 }
307
308 void free_NeonSpam() {
309     
310 }
311
312 #undef BOTID
313 #undef BOTALIAS