fixed botwar detection for bot deops
[NeonServV5.git] / src / event_neonserv_mode.c
1 /* event_neonserv_mode.c - NeonServ v5.3
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
18 static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup);
19 static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc);
20 static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan);
21
22 static int botwar_detect_executed;
23
24 struct neonserv_event_mode_cache {
25     struct ClientSocket *client;
26     struct UserNode *user;
27     char *modes;
28     char **argv;
29     int argc;
30 };
31
32 static void neonserv_event_mode(struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
33     struct ClientSocket *client = getBotForChannel(chan);
34     if(!client) return; //we can't "see" this event
35     if(isNetworkService(user)) return;
36     loadChannelSettings(chan);
37     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
38     struct neonserv_event_mode_cache *cache = malloc(sizeof(*cache));
39     char **temp_argv = NULL;
40     if(argc) 
41         temp_argv = malloc(argc*sizeof(*temp_argv));
42     if (!cache || (argc && !temp_argv)) {
43         perror("malloc() failed");
44         return;
45     }
46     if(argc) {
47         int i;
48         for(i = 0; i < argc; i++) {
49             temp_argv[i] = strdup(argv[i]);
50         }
51     }
52     cache->client = client;
53     cache->user = user;
54     cache->modes = strdup(modes);
55     cache->argv = temp_argv;
56     cache->argc = argc;
57     get_userlist_with_invisible(chan, neonserv_event_mode_userlist_lookup, cache);
58 }
59
60 static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup) {
61     struct neonserv_event_mode_cache *cache = data;
62     neonserv_event_mode_async1(cache->client, cache->user, chan, cache->modes, cache->argv, cache->argc);
63     if(cache->argc) {
64         int i;
65         for(i = 0; i < cache->argc; i++) {
66             free(cache->argv[i]);
67         }
68         free(cache->argv);
69     }
70     free(cache->modes);
71     free(cache);
72 }
73
74 static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
75     struct ClientSocket *textclient = ((client->flags & SOCKET_FLAG_PREFERRED) ? client : get_prefered_bot(client->botid));
76     botwar_detect_executed = 0;
77     MYSQL_ROW row, defaults = NULL;
78     int i, arg, add = 1, skip = 0;
79     unsigned int modetype;
80     int db_canop, db_canhalfop, db_canvoice, db_canban, db_enfmodes, db_getop, db_gethalfop, db_getvoice;
81     struct ModeNode *modelock = createModeNode(NULL);
82     struct ModeBuffer *modeBuf;
83     struct UserNode *cuser;
84     struct ChanUser *chanuser;
85     int with_halfops = get_int_field("General.have_halfop");
86     modeBuf = initModeBuffer(client, chan);
87     printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes`, `channel_getop`, `channel_getvoice`, `channel_gethalfop`, `channel_canhalfop` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
88     row = mysql_fetch_row(mysql_use());
89     if(row[0] == NULL || row[1] == NULL || row[2] == NULL || row[3] == NULL || row[4] == NULL || row[5] == NULL || row[6] == NULL || (row[7] == NULL && with_halfops) || (row[8] == NULL && with_halfops)) {
90         printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes`, `channel_getop`, `channel_getvoice`, `channel_gethalfop`, `channel_canhalfop` FROM `channels` WHERE `channel_name` = 'defaults'");
91         defaults = mysql_fetch_row(mysql_use());
92     }
93     db_canop = atoi((row[0] ? row[0] : defaults[0]));
94     db_canvoice = atoi((row[1] ? row[1] : defaults[1]));
95     db_canban = atoi((row[2] ? row[2] : defaults[2]));
96     db_enfmodes = atoi((row[3] ? row[3] : defaults[3]));
97     db_getop = atoi((row[5] ? row[5] : defaults[5]));
98     db_getvoice = atoi((row[6] ? row[6] : defaults[6]));
99     db_gethalfop = (with_halfops ? atoi((row[7] ? row[7] : defaults[7])) : 0);
100     db_canhalfop = (with_halfops ? atoi((row[8] ? row[8] : defaults[8])) : 0);
101     if(row[4])
102         parseModeString(modelock, row[4]);
103     else if(defaults[4])
104         parseModeString(modelock, defaults[4]);
105     int uaccess = getChannelAccess(user, chan);
106     int caccess;
107     char *carg;
108     int sent_modes_locked = 0;
109     char deop_user = 0;
110     char tmp[MAXLEN];
111     arg = 0;
112     for(i = 0; i < strlen(modes); i++) {
113         switch(modes[i]) {
114             case '+':
115                 add = 1;
116                 break;
117             case '-':
118                 add = 0;
119                 break;
120             case 'o':
121             case 'h':
122             case 'v':
123                 if(arg == argc) {
124                     break;
125                 }
126                 carg = argv[arg++];
127                 cuser = searchUserByNick(carg);
128                 if(!cuser) {
129                     break; //internal Bot error - this should never happen
130                 }
131                 caccess = getChannelAccess(cuser, chan);
132                 if(modes[i] == 'o' && !add && cuser == client->user) {
133                     //someone deopped the bot???
134                     if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
135                                                 requestOp(client->user, chan);
136                 }
137                 if((modes[i] == 'o' || (modes[i] == 'h' && !with_halfops)) && !(add && isBot(cuser))) {
138                     if(uaccess < db_canop) {
139                         reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
140                         db_canop = -1;
141                         if(uaccess < db_getop)
142                             deop_user = 1;
143                     }
144                     if(db_canop == -1 && caccess < db_getop) {
145                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
146                             modeBufferSet(modeBuf, !add, modes[i], carg);
147                         break;
148                     }
149                 } else if(modes[i] == 'h' && with_halfops && !(add && isBot(cuser))) {
150                     if(uaccess < db_canhalfop) {
151                         reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
152                         db_canhalfop = -1;
153                         if(uaccess < db_gethalfop)
154                             deop_user = 1;
155                     }
156                     if(db_canhalfop == -1 && caccess < db_gethalfop) {
157                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
158                             modeBufferSet(modeBuf, !add, modes[i], carg);
159                         break;
160                     }
161                 } else if(modes[i] == 'v' && !(add && isBot(cuser))) {
162                     if(uaccess < db_canvoice) {
163                         reply(textclient, user, "NS_MODE_ENFVOICE", chan->name);
164                         db_canvoice = -1;
165                         if(uaccess < db_getop)
166                             deop_user = 1;
167                     }
168                     if(db_canvoice == -1 && caccess < db_getvoice) {
169                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
170                             modeBufferSet(modeBuf, !add, modes[i], carg);
171                         break;
172                     }
173                 }
174                 if(!add) {
175                     //check protection
176                     if(isBot(cuser)) {
177                         //don't send this - just try to reop
178                         //reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
179                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
180                             modeBufferSet(modeBuf, !add, modes[i], carg);
181                         break;
182                     }
183                     if(isUserProtected(chan, cuser, user)) {
184                         reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
185                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
186                             modeBufferSet(modeBuf, !add, modes[i], carg);
187                         if(uaccess < db_getop)
188                             deop_user = 1;
189                         break;
190                     }
191                 }
192                 break;
193             case 'b':
194                 if(arg == argc) {
195                     break;
196                 }
197                 carg = argv[arg++];
198                 if(uaccess < db_canban) {
199                     reply(textclient, user, "NS_MODE_CANBAN", chan->name);
200                     db_canban = -1;
201                 }
202                 if(db_canban == -1) {
203                     if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
204                         modeBufferSet(modeBuf, !add, modes[i], carg);
205                     if(uaccess < db_getop)
206                         deop_user = 1;
207                     break;
208                 }
209                 char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
210                 char usermask[NICKLEN+USERLEN+HOSTLEN+3];
211                 int match_count = 0;
212                 carg = make_banmask(carg, hostmask_buffer);
213                 if(add) {
214                     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
215                         cuser = chanuser->user;
216                         sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
217                         if(!match(carg, usermask)) {
218                             if(isUserProtected(chan, cuser, user)) {
219                                 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
220                                 skip = 1;
221                                 break;
222                             }
223                             match_count++;
224                         }
225                     }
226                     if(match_count > 4 && (match_count * 3) > chan->usercount) {
227                         reply(textclient, user, "NS_LAME_MASK_WARNING", carg, match_count);
228                     }
229                 }
230                 if(skip) {
231                     if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
232                         modeBufferSet(modeBuf, !add, modes[i], carg);
233                     if(uaccess < db_getop)
234                         deop_user = 1;
235                 }
236                 break;
237             default:
238                 modetype = getModeType(modelock, modes[i]);
239                 if(modetype == 0) {
240                     break; //unknown mode
241                 }
242                 
243                 if(add && (modetype & CHANNEL_MODE_TYPE) != CHANNEL_MODE_TYPE_D) {
244                     if(arg == argc) {
245                         break;
246                     }
247                     carg = argv[arg++];
248                     if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
249                         char *modelock_val = getModeValue(modelock, modes[i]);
250                         if(stricmp(carg, modelock_val) && uaccess < db_enfmodes && !isGodMode(user)) {
251                             if(!sent_modes_locked) {
252                                 getFullModeString(modelock, tmp);
253                                 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
254                                 sent_modes_locked = 1;
255                                 if(uaccess < db_getop)
256                                     deop_user = 1;
257                             }
258                             if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
259                                 modeBufferSet(modeBuf, add, modes[i], modelock_val);
260                             break;
261                         }
262                     }
263                     if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
264                         int *modelock_val = getModeValue(modelock, modes[i]);
265                         if(atoi(carg) != *modelock_val && uaccess < db_enfmodes && !isGodMode(user)) {
266                             if(!sent_modes_locked) {
267                                 getFullModeString(modelock, tmp);
268                                 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
269                                 sent_modes_locked = 1;
270                                 if(uaccess < db_getop)
271                                     deop_user = 1;
272                             }
273                             sprintf(tmp, "%d", *modelock_val);
274                             if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
275                                 modeBufferSet(modeBuf, add, modes[i], tmp);
276                             break;
277                         }
278                     }
279                 } else if(!add && (modetype & CHANNEL_MODE_TYPE) == CHANNEL_MODE_TYPE_B) {
280                     if(arg == argc) {
281                         break;
282                     }
283                     carg = argv[arg++];
284                 } else
285                     carg = NULL;
286                 if(isModeAffected(modelock, modes[i]) && add == !isModeSet(modelock, modes[i]) && uaccess < db_enfmodes && !isGodMode(user)) {
287                     if(!sent_modes_locked) {
288                         getFullModeString(modelock, tmp);
289                         reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
290                         sent_modes_locked = 1;
291                         if(uaccess < db_getop)
292                             deop_user = 1;
293                     }
294                     if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
295                         modeBufferSet(modeBuf, !add, modes[i], carg);
296                     break;
297                 }
298                 break;
299         }
300     }
301     if(deop_user && !neonserv_cmd_mode_botwar_detect(client, user, chan)) {
302         modeBufferDeop(modeBuf, user->nick);
303     }
304     freeModeBuffer(modeBuf);
305     freeModeNode(modelock);
306 }
307
308 static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan) {
309     struct ChanUser *chanuser = getChanUser(user, chan);
310     if(!chanuser) return 0; //flying super cow?
311     if(time(0) - chanuser->changeTime > BOTWAR_DETECTION_TIME) {
312         chanuser->changeTime = time(0);
313         chanuser->chageEvents = 1;
314     } else {
315         if(!botwar_detect_executed)
316             chanuser->chageEvents++;
317         botwar_detect_executed = 1;
318         if(chanuser->chageEvents >= BOTWAR_DETECTION_EVENTS || chanuser->chageEvents < 0) {
319             //BOTWAR!
320             chanuser->changeTime = time(0);
321             if(chanuser->chageEvents > 0) {
322                 char *alertchan = get_string_field("General.alertchan");
323                 putsock(client, "NOTICE @%s :%s %s", chan->name, get_language_string(user, "NS_BOTWAR_DETECTED"), (alertchan ? get_language_string(user, "NS_BOTWAR_REPORTED") : ""));
324                 if(alertchan) {
325                     struct ChanNode *alertchan_chan = getChanByName(alertchan);
326                     struct ClientSocket *alertclient;
327                     if(alertchan_chan && (alertclient = getBotForChannel(chan)) != NULL) {
328                         char msgBuf[MAXLEN];
329                         putsock(alertclient, "PRIVMSG %s :%s", alertchan_chan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
330                     }
331                 }
332             }
333             chanuser->chageEvents = -2;
334             return 1;
335         }
336     }
337     return 0;
338 }
339