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