execute mysql_check only if a query fails.
[NeonServV5.git] / modcmd.c
1
2 #include "modcmd.h"
3 #include "IRCEvents.h"
4 #include "IRCParser.h"
5 #include "ClientSocket.h"
6 #include "UserNode.h"
7 #include "ChanNode.h"
8 #include "ChanUser.h"
9 #include "WHOHandler.h"
10 #include "lang.h"
11 #include "mysqlConn.h"
12 #include "DBHelper.h"
13
14 struct trigger_callback {
15     int botid;
16     trigger_callback_t *func;
17     
18     struct trigger_callback *next;
19 };
20
21 struct command_check_user_cache {
22     struct ClientSocket *client, *textclient;
23     struct UserNode *user;
24     struct ChanNode *chan, *sent_chan;
25     char **argv;
26     int argc;
27     char *message;
28     struct cmd_binding *cbind;
29 };
30
31 static struct cmd_binding **cmd_binds;
32 static struct cmd_function *cmd_functions = NULL;
33 static struct trigger_callback *trigger_callbacks = NULL;
34 static struct ClientSocket *tmp_text_client;
35
36 static const struct default_language_entry msgtab[] = {
37     {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
38     {"MODCMD_CHAN_REQUIRED",    "You must provide the name of a channel that exists."},
39     {"MODCMD_AUTH_REQUIRED",    "You need to be authenticated with AuthServ to use this command."},
40     {"MODCMD_PRIVILEGED",       "\002%s\002 is a privileged command."},
41     {"MODCMD_PUBCMD",           "Public commands in \002%s\002 are restricted."},
42     {"MODCMD_ACCESS_DENIED",    "Access denied."},
43     {NULL, NULL}
44 };
45
46 static int get_binds_index(char first_char) {
47     if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
48         return tolower(first_char) - 'a';
49     }
50     return 26;
51 }
52
53 struct ClientSocket* get_prefered_bot(int botid) {
54     struct ClientSocket *client;
55     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
56         if(client->botid == botid && (client->flags & SOCKET_FLAG_PREFERRED))
57             return client;
58     }
59     return NULL;
60 }
61
62 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
63     struct trigger_cache *trigger;
64     for(trigger = chan->trigger; trigger; trigger = trigger->next) {
65         if(trigger->botid == botid)
66             return trigger->trigger;
67     }
68     struct trigger_callback *cb;
69     for(cb = trigger_callbacks; cb; cb = cb->next) {
70         if(cb->botid == botid)
71             break;
72     }
73     char triggerStr[TRIGGERLEN];
74     if(cb)
75         cb->func(chan, triggerStr);
76     else
77         strcpy(triggerStr, "+");
78     trigger = malloc(sizeof(*trigger));
79     if (!trigger) {
80         perror("malloc() failed");
81         return 0;
82     }
83     trigger->botid = botid;
84     trigger->trigger = strdup(triggerStr);
85     trigger->next = chan->trigger;
86     chan->trigger = trigger;
87     return trigger->trigger;
88 }
89
90 static void handle_command_async(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, struct ChanNode *sent_chan, struct cmd_binding *cbind, char **argv, int argc);
91
92 static USERAUTH_CALLBACK(command_checked_auth) {
93     struct command_check_user_cache *cache = data;
94     tmp_text_client = cache->textclient;
95     handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
96     free(cache->message);
97     free(cache);
98 }
99
100 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
101     struct ChanNode *sent_chan = chan;
102     if(message[0] == '#') {
103         char *chanName = message;
104         message = strstr(message, " ");
105         if(!message) return;
106         *message = '\0';
107         message++;
108         struct ChanNode *chan2 = getChanByName(chanName);
109         if(chan2)
110             chan = chan2;
111     }
112     message = strdup(message);
113     int bind_index = get_binds_index(message[0]);
114     char *args = strstr(message, " ");
115     if(args) {
116         *args = '\0';
117         args++;
118     }
119     struct cmd_binding *cbind;
120     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
121         if(cbind->botid == client->botid && stricmp(cbind->cmd, message) == 0) {
122             //get a text bot
123             tmp_text_client = get_prefered_bot(client->botid);
124             //parse the arguments...
125             char *arga[MAXNUMPARAMS];
126             char **argv;
127             int argc = 0;
128             if(args) {
129                 while(*args) {
130                     //skip leading spaces
131                     while (*args == ' ')
132                         *args++ = 0;
133                     arga[argc++] = args;
134                     if (argc >= MAXNUMPARAMS)
135                         break;
136                     while (*args != ' ' && *args)
137                         args++;
138                 }
139             }
140             argv = arga;
141             if(argc != 0 && argv[0][0] == '#') {
142                 struct ChanNode *chan2 = getChanByName(argv[0]);
143                 if(chan2) {
144                     argv += 1;
145                     argc -= 1;
146                     chan = chan2;
147                 }
148             }
149             if(cbind->parameters) {
150                 //userdefined parameters...
151                 char *uarga[MAXNUMPARAMS];
152                 char params[strlen(cbind->parameters)+1];
153                 strcpy(params, cbind->parameters);
154                 int uargpos = 0, argi, allargs = 0;
155                 char *ppos = params;
156                 char *prev_ppos = params;
157                 while((ppos = strstr(ppos, " "))) {
158                     *ppos = '\0';
159                     if(prev_ppos[0] == '%') {
160                         prev_ppos++;
161                         if(prev_ppos[strlen(prev_ppos)-1] == '-') {
162                             allargs = 1;
163                             prev_ppos[strlen(prev_ppos)-1] = '\0';
164                         } else
165                             allargs = 0;
166                         argi = atoi(prev_ppos);
167                         if(argi > 0) {
168                             if(argi <= argc) continue;
169                             uarga[uargpos++] = argv[argi-1];
170                             if(allargs) {
171                                 for(;argi < argc; argi++)
172                                     uarga[uargpos++] = argv[argi-1];
173                             }
174                         } else if(!strcmp(prev_ppos, "c"))
175                             uarga[uargpos++] = (chan ? chan->name : NULL);
176                         else if(!strcmp(prev_ppos, "n")) 
177                             uarga[uargpos++] = user->nick;
178                     } else {
179                         uarga[uargpos++] = prev_ppos;
180                     }
181                     ppos++;
182                     prev_ppos = ppos;
183                 }
184                 argv = uarga;
185                 argc = uargpos;
186             }
187             if(argc < cbind->func->paramcount) {
188                 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
189                 break;
190             }
191             if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
192                 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
193                 break;
194             }
195             if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
196                 //check auth...
197                 struct command_check_user_cache *data = malloc(sizeof(*data));
198                 char **temp_argv = malloc(argc*sizeof(*temp_argv));
199                 if (!data || !temp_argv) {
200                     perror("malloc() failed");
201                     break;
202                 }
203                 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
204                 data->argv = temp_argv;
205                 data->argc = argc;
206                 data->client = client;
207                 data->user = user;
208                 data->chan = chan;
209                 data->sent_chan = sent_chan;
210                 data->message = message;
211                 data->cbind = cbind;
212                 data->textclient = tmp_text_client;
213                 get_userauth(user, command_checked_auth, data);
214                 return;
215             } else
216                 handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
217             break;
218         }
219     }
220     free(message);
221 }
222
223 static void handle_command_async(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, struct ChanNode *sent_chan, struct cmd_binding *cbind, char **argv, int argc) {
224     MYSQL_RES *res;
225     MYSQL_ROW row;
226     int uaccess;
227     if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
228         reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
229         return;
230     }
231     if(sent_chan && sent_chan != chan) {
232         //check pubcmd of this channel
233         printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
234         res = mysql_use();
235         if ((row = mysql_fetch_row(res)) != NULL) {
236             uaccess = getChannelAccess(user, sent_chan, 1);
237             if(row[0] && uaccess < atoi(row[0])) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
238                 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
239                 return;
240             }
241         }
242     }
243     int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
244     if(global_access > 0) {
245         int user_global_access = 0;
246         printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
247         res = mysql_use();
248         if ((row = mysql_fetch_row(res)) != NULL) {
249             user_global_access = atoi(row[0]);
250         }
251         if(user_global_access < global_access) {
252             if(!user_global_access)
253                 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
254             else
255                 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
256             return;
257         }
258     }
259     if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
260         MYSQL_ROW defaults = NULL;
261         char access_list[256];
262         int access_pos = 0;
263         int access_count = 0;
264         int minaccess = 0;
265         char *str_a, *str_b = cbind->func->channel_access, *str_c;
266         if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
267             str_b = cbind->channel_access;
268         access_list[0] = '\0';
269         if(str_b) {
270             str_c = strdup(str_b);
271             str_b = str_c;
272             while((str_a = str_b)) {
273                 str_b = strstr(str_a, ",");
274                 if(str_b) {
275                     *str_b = '\0';
276                     str_b++;
277                 }
278                 if(*str_a == '#') {
279                     str_a++;
280                     access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
281                     access_count++;
282                 } else {
283                     if(atoi(str_a) > minaccess)
284                         minaccess = atoi(str_a);
285                 }
286             }
287             free(str_c);
288         }
289         if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
290             printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
291             res = mysql_use();
292             if ((row = mysql_fetch_row(res)) != NULL) {
293                 chan->flags |= CHANFLAG_CHAN_REGISTERED;
294                 chan->channel_id = atoi(row[0]);
295                 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
296                     uaccess = getChannelAccess(user, chan, 1);
297                     if(uaccess < minaccess) {
298                         //ACCESS DENIED
299                         reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
300                         return;
301                     }
302                     if(!row[1] && !defaults) {
303                         printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
304                         defaults = mysql_fetch_row(mysql_use());
305                     }
306                     if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
307                         //PUBCMD
308                         reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
309                         return;
310                     }
311                     int i;
312                     for(i = 0; i < access_count; i++) {
313                         if(!row[2+i] && !defaults) {
314                             printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
315                             defaults = mysql_fetch_row(mysql_use());
316                         }
317                         if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
318                             reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
319                             return;
320                         }
321                     }
322                 }
323             }
324             chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
325         }
326         if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
327             reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
328             return;
329         }
330         printf_mysql_query("SELECT `botid` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, client->botid);
331         res = mysql_use();
332         if ((row = mysql_fetch_row(res)) == NULL) {
333             reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
334             return;
335         }
336     }
337     if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
338         reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
339         return;
340     }
341     cbind->func->func(client, user, chan, argv, argc);
342 }
343
344 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
345     fd_set fds;
346     char *trigger;
347     struct ClientSocket *client;
348     FD_ZERO(&fds);
349     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
350         if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
351             FD_SET(client->botid, &fds);
352             trigger = get_channel_trigger(client->botid, chan);
353             if(stricmplen(message, trigger, strlen(trigger)) == 0) {
354                 handle_command(client, user, chan, message + strlen(trigger));
355             }
356         }
357     }
358     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
359         if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
360             FD_SET(client->botid, &fds);
361             trigger = get_channel_trigger(client->botid, chan);
362             if(stricmplen(message, trigger, strlen(trigger)) == 0) {
363                 handle_command(client, user, chan, message + strlen(trigger));
364             }
365         }
366     }
367 }
368
369 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
370     struct ClientSocket *client;
371     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
372         if(client->user == target) {
373             handle_command(client, user, NULL, message);
374         }
375     }
376 }
377
378 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags, char *channel_access, int global_access) {
379     struct cmd_function *cmdfunc;
380     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
381         if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
382             return 0;
383     }
384     cmdfunc = malloc(sizeof(*cmdfunc));
385     if (!cmdfunc) {
386         perror("malloc() failed");
387         return 0;
388     }
389     cmdfunc->botid = botid;
390     cmdfunc->name = strdup(name);
391     cmdfunc->func = func;
392     cmdfunc->flags = flags;
393     cmdfunc->paramcount = paramcount;
394     cmdfunc->channel_access = channel_access;
395     cmdfunc->global_access = global_access;
396     cmdfunc->next = cmd_functions;
397     cmd_functions = cmdfunc;
398     return 1;
399 }
400
401 int set_trigger_callback(int botid, trigger_callback_t *func) {
402     static struct trigger_callback *cb = NULL;
403     for(cb = trigger_callbacks; cb; cb = cb->next) {
404         if(cb->botid == botid)
405             break;
406     }
407     if(!cb) {
408         cb = malloc(sizeof(*cb));
409         if (!cb) {
410             perror("malloc() failed");
411             return 0;
412         }
413         cb->botid = botid;
414         cb->next = trigger_callbacks;
415         trigger_callbacks = cb;
416     }
417     cb->func = func;
418     return 1;
419 }
420
421 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
422     struct trigger_cache *trigger;
423     for(trigger = chan->trigger; trigger; trigger = trigger->next) {
424         if(trigger->botid == botid) {
425             free(trigger->trigger);
426             trigger->trigger = strdup(new_trigger);
427             return 1;
428         }
429     }
430     return 0;
431 }
432
433 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
434     int bind_index = get_binds_index(cmd[0]);
435     struct cmd_binding *cbind;
436     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
437         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
438             return 0;
439     }
440     cbind = malloc(sizeof(*cbind));
441     if (!cbind) {
442         perror("malloc() failed");
443         return 0;
444     }
445     cbind->botid = botid;
446     cbind->cmd = strdup(cmd);
447     cbind->func = func;
448     cbind->parameters = NULL;
449     cbind->global_access = 0;
450     cbind->channel_access = NULL;
451     cbind->flags = 0;
452     cbind->next = cmd_binds[bind_index];
453     cmd_binds[bind_index] = cbind;
454     return 1;
455 }
456
457 int bind_cmd_to_command(int botid, char *cmd, char *func) {
458     struct cmd_function *cmdfunc;
459     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
460         if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
461             break;
462     }
463     if(!cmdfunc) return 0;
464     int bind_index = get_binds_index(cmd[0]);
465     struct cmd_binding *cbind;
466     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
467         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
468             return 0;
469     }
470     cbind = malloc(sizeof(*cbind));
471     if (!cbind) {
472         perror("malloc() failed");
473         return 0;
474     }
475     cbind->botid = botid;
476     cbind->cmd = strdup(cmd);
477     cbind->func = cmdfunc;
478     cbind->next = cmd_binds[bind_index];
479     cbind->parameters = NULL;
480     cbind->global_access = 0;
481     cbind->channel_access = NULL;
482     cbind->flags = 0;
483     cmd_binds[bind_index] = cbind;
484     return 1;
485 }
486
487 int unbind_cmd(int botid, char *cmd) {
488     int bind_index = get_binds_index(cmd[0]);
489     struct cmd_binding *cbind, *last = NULL;
490     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
491         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
492             if(last)
493                 last->next = cbind->next;
494             else
495                 cmd_binds[bind_index] = cbind->next;
496             free(cbind->cmd);
497             if(cbind->parameters)
498                 free(cbind->parameters);
499             free(cbind);
500             return 1;
501         } else
502             last = cbind;
503     }
504     return 0;
505 }
506
507 struct ClientSocket *getTextBot() {
508     return tmp_text_client;
509 }
510
511 void init_modcmd() {
512     cmd_binds = calloc(27, sizeof(*cmd_binds));
513     bind_chanmsg(got_chanmsg);
514     bind_privmsg(got_privmsg);
515     register_default_language_table(msgtab);
516 }
517
518 void free_modcmd() {
519     int i;
520     for(i = 0; i < 27; i++) {
521         struct cmd_binding *cbind, *next;
522         for(cbind = cmd_binds[i]; cbind; cbind = next) {
523             next = cbind->next;
524             free(cbind->cmd);
525             if(cbind->parameters)
526                 free(cbind->parameters);
527             if(cbind->channel_access)
528                 free(cbind->channel_access);
529             free(cbind);
530         }
531     }
532     free(cmd_binds);
533     struct cmd_function *cmdfunct, *next;
534     for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
535         next = cmdfunct->next;
536         free(cmdfunct->name);
537         free(cmdfunct);
538     }
539     struct trigger_callback *cb, *next_cb;
540     for(cb = trigger_callbacks; cb; cb = next_cb) {
541         next_cb = cb->next;
542         free(next_cb);
543     }
544     cmd_functions = NULL;
545     trigger_callbacks = NULL;
546 }
547
548 void bind_set_parameters(int botid, char *cmd, char *parameters) {
549     int bind_index = get_binds_index(cmd[0]);
550     struct cmd_binding *cbind;
551     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
552         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
553             if(cbind->parameters)
554                 free(cbind->parameters);
555             cbind->parameters = strdup(parameters);
556             return;
557         }
558     }
559 }
560
561 void bind_set_global_access(int botid, char *cmd, int gaccess) {
562     int bind_index = get_binds_index(cmd[0]);
563     struct cmd_binding *cbind;
564     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
565         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
566             if(gaccess > -1) {
567                 cbind->global_access = gaccess;
568                 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
569             } else {
570                 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
571             }
572             return;
573         }
574     }
575 }
576
577 void bind_set_channel_access(int botid, char *cmd, char *chanaccess) {
578     int bind_index = get_binds_index(cmd[0]);
579     struct cmd_binding *cbind;
580     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
581         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
582             if(cbind->channel_access)
583                 free(cbind->channel_access);
584             if(chanaccess) {
585                 cbind->channel_access = strdup(chanaccess);
586                 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
587             } else {
588                 cbind->channel_access = NULL;
589                 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
590             }
591             return;
592         }
593     }
594 }