5c87873d430d6d073884b00e065fb0a7c36f274a
[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);
71 }
72
73 static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
74     struct ClientSocket *textclient = ((client->flags & SOCKET_FLAG_PREFERRED) ? client : get_prefered_bot(client->botid));
75     botwar_detect_executed = 0;
76     MYSQL_ROW row, defaults = NULL;
77     int i, arg, add = 1, skip = 0;
78     unsigned int modetype;
79     int db_canop, db_canvoice, db_canban, db_enfmodes, db_getop, db_getvoice;
80     struct ModeNode *modelock = createModeNode(NULL);
81     struct ModeBuffer *modeBuf;
82     struct UserNode *cuser;
83     struct ChanUser *chanuser;
84     modeBuf = initModeBuffer(client, chan);
85     printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes`, `channel_getop`, `channel_getvoice` 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) {
88         printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes`, `channel_getop`, `channel_getvoice` 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     if(row[4])
98         parseModeString(modelock, row[4]);
99     else if(defaults[4])
100         parseModeString(modelock, defaults[4]);
101     int uaccess = getChannelAccess(user, chan);
102     int caccess;
103     char *carg;
104     int sent_modes_locked = 0;
105     char deop_user = 0;
106     char tmp[MAXLEN];
107     arg = 0;
108     for(i = 0; i < strlen(modes); i++) {
109         switch(modes[i]) {
110             case '+':
111                 add = 1;
112                 break;
113             case '-':
114                 add = 0;
115                 break;
116             case 'o':
117             case 'v':
118                 if(arg == argc) {
119                     break;
120                 }
121                 carg = argv[arg++];
122                 cuser = searchUserByNick(carg);
123                 if(!cuser) {
124                     break; //internal Bot error - this should never happen
125                 }
126                 caccess = getChannelAccess(cuser, chan);
127                 if(modes[i] == 'o' && !add && cuser == client->user) {
128                     //someone deopped the bot???
129                     requestOp(client->user, chan);
130                 }
131                 if(modes[i] == 'o' && !(add && isBot(cuser))) {
132                     if(uaccess < db_canop) {
133                         reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
134                         db_canop = -1;
135                         if(uaccess < db_getop)
136                             deop_user = 1;
137                     }
138                     if(db_canop == -1 && caccess < db_getop) {
139                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
140                             modeBufferSet(modeBuf, !add, modes[i], carg);
141                         break;
142                     }
143                 } else if(modes[i] == 'v') {
144                     if(uaccess < db_canvoice) {
145                         reply(textclient, user, "NS_MODE_ENFVOICE", chan->name);
146                         db_canvoice = -1;
147                         if(uaccess < db_getop)
148                             deop_user = 1;
149                     }
150                     if(db_canvoice == -1 && caccess < db_getvoice) {
151                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
152                             modeBufferSet(modeBuf, !add, modes[i], carg);
153                         break;
154                     }
155                 }
156                 if(!add) {
157                     //check protection
158                     if(isBot(cuser)) {
159                         //don't send this - just try to reop
160                         //reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
161                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
162                             modeBufferSet(modeBuf, !add, modes[i], carg);
163                         break;
164                     }
165                     if(isUserProtected(chan, cuser, user)) {
166                         reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
167                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
168                             modeBufferSet(modeBuf, !add, modes[i], carg);
169                         if(uaccess < db_getop)
170                             deop_user = 1;
171                         break;
172                     }
173                 }
174                 break;
175             case 'b':
176                 if(arg == argc) {
177                     break;
178                 }
179                 carg = argv[arg++];
180                 if(uaccess < db_canban) {
181                     reply(textclient, user, "NS_MODE_CANBAN", chan->name);
182                     db_canban = -1;
183                 }
184                 if(db_canban == -1) {
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                 char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
192                 char usermask[NICKLEN+USERLEN+HOSTLEN+3];
193                 int match_count = 0;
194                 carg = make_banmask(carg, hostmask_buffer);
195                 if(add) {
196                     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
197                         cuser = chanuser->user;
198                         sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
199                         if(!match(carg, usermask)) {
200                             if(isUserProtected(chan, cuser, user)) {
201                                 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
202                                 skip = 1;
203                                 break;
204                             }
205                             match_count++;
206                         }
207                     }
208                     if(match_count > 4 && (match_count * 3) > chan->usercount) {
209                         reply(textclient, user, "NS_LAME_MASK_WARNING", carg, match_count);
210                     }
211                 }
212                 if(skip) {
213                     if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
214                         modeBufferSet(modeBuf, !add, modes[i], carg);
215                     if(uaccess < db_getop)
216                         deop_user = 1;
217                 }
218                 break;
219             default:
220                 modetype = getModeType(modelock, modes[i]);
221                 if(modetype == 0) {
222                     break; //unknown mode
223                 }
224                 
225                 if(add && (modetype & CHANNEL_MODE_TYPE) != CHANNEL_MODE_TYPE_D) {
226                     if(arg == argc) {
227                         break;
228                     }
229                     carg = argv[arg++];
230                     if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
231                         char *modelock_val = getModeValue(modelock, modes[i]);
232                         if(stricmp(carg, modelock_val) && uaccess < db_enfmodes && !isGodMode(user)) {
233                             if(!sent_modes_locked) {
234                                 getFullModeString(modelock, tmp);
235                                 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
236                                 sent_modes_locked = 1;
237                                 if(uaccess < db_getop)
238                                     deop_user = 1;
239                             }
240                             if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
241                                 modeBufferSet(modeBuf, add, modes[i], modelock_val);
242                             break;
243                         }
244                     }
245                     if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
246                         int *modelock_val = getModeValue(modelock, modes[i]);
247                         if(atoi(carg) != *modelock_val && uaccess < db_enfmodes && !isGodMode(user)) {
248                             if(!sent_modes_locked) {
249                                 getFullModeString(modelock, tmp);
250                                 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
251                                 sent_modes_locked = 1;
252                                 if(uaccess < db_getop)
253                                     deop_user = 1;
254                             }
255                             sprintf(tmp, "%d", *modelock_val);
256                             if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
257                                 modeBufferSet(modeBuf, add, modes[i], tmp);
258                             break;
259                         }
260                     }
261                 } else if(!add && (modetype & CHANNEL_MODE_TYPE) == CHANNEL_MODE_TYPE_B) {
262                     if(arg == argc) {
263                         break;
264                     }
265                     carg = argv[arg++];
266                 } else
267                     carg = NULL;
268                 if(isModeAffected(modelock, modes[i]) && add == !isModeSet(modelock, modes[i]) && uaccess < db_enfmodes && !isGodMode(user)) {
269                     if(!sent_modes_locked) {
270                         getFullModeString(modelock, tmp);
271                         reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
272                         sent_modes_locked = 1;
273                         if(uaccess < db_getop)
274                             deop_user = 1;
275                     }
276                     if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
277                         modeBufferSet(modeBuf, !add, modes[i], carg);
278                     break;
279                 }
280                 break;
281         }
282     }
283     if(deop_user && !neonserv_cmd_mode_botwar_detect(client, user, chan)) {
284         modeBufferDeop(modeBuf, user->nick);
285     }
286     freeModeBuffer(modeBuf);
287     freeModeNode(modelock);
288 }
289
290 static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan) {
291     struct ChanUser *chanuser = getChanUser(user, chan);
292     if(!chanuser) return 0; //flying super cow?
293     if(time(0) - chanuser->changeTime > BOTWAR_DETECTION_TIME) {
294         chanuser->changeTime = time(0);
295         chanuser->chageEvents = 1;
296     } else {
297         if(!botwar_detect_executed)
298             chanuser->chageEvents++;
299         botwar_detect_executed = 1;
300         if(chanuser->chageEvents >= BOTWAR_DETECTION_EVENTS || chanuser->chageEvents < 0) {
301             //BOTWAR!
302             chanuser->changeTime = time(0);
303             if(chanuser->chageEvents > 0) {
304                 putsock(client, "NOTICE @%s :%s %s", chan->name, get_language_string(user, "NS_BOTWAR_DETECTED"), (BOTWAR_ALERT_CHAN ? get_language_string(user, "NS_BOTWAR_REPORTED") : ""));
305                 if(BOTWAR_ALERT_CHAN) {
306                     struct ChanNode *alertchan = getChanByName(BOTWAR_ALERT_CHAN);
307                     struct ClientSocket *alertclient;
308                     if(alertchan && (alertclient = getBotForChannel(chan)) != NULL) {
309                         char msgBuf[MAXLEN];
310                         putsock(alertclient, "PRIVMSG %s :%s", alertchan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
311                     }
312                 }
313             }
314             chanuser->chageEvents = -2;
315             return 1;
316         }
317     }
318     return 0;
319 }
320