show how many times a command is triggered in cmd_commands
[NeonServV5.git] / src / modcmd.c
1 /* modcmd.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 "modcmd.h"
19 #include "IRCEvents.h"
20 #include "IRCParser.h"
21 #include "ClientSocket.h"
22 #include "UserNode.h"
23 #include "ChanNode.h"
24 #include "ChanUser.h"
25 #include "WHOHandler.h"
26 #include "lang.h"
27 #include "mysqlConn.h"
28 #include "DBHelper.h"
29 #include "EventLogger.h"
30
31 struct trigger_callback {
32     int botid;
33     trigger_callback_t *func;
34     
35     struct trigger_callback *next;
36 };
37
38 struct cmd_bot_alias {
39     int botid;
40     char *alias;
41     
42     struct cmd_bot_alias *next;
43 };
44
45 struct command_check_user_cache {
46     struct ClientSocket *client, *textclient;
47     struct UserNode *user;
48     struct ChanNode *chan, *sent_chan;
49     char **argv;
50     int argc;
51     char *message, *args_buffer;
52     struct cmd_binding *cbind;
53 };
54
55 static struct cmd_binding **cmd_binds;
56 static struct cmd_function *cmd_functions = NULL;
57 static struct trigger_callback *trigger_callbacks = NULL;
58 static struct cmd_bot_alias *bot_aliases = NULL;
59 static struct ClientSocket *tmp_text_client;
60 static int total_triggered = 0;
61 int statistics_commands = 0;
62
63 static const struct default_language_entry msgtab[] = {
64     {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
65     {"MODCMD_CHAN_REQUIRED",    "You must provide the name of a channel that exists and the bot is on."},
66     {"MODCMD_AUTH_REQUIRED",    "You need to be authenticated with AuthServ to use this command."},
67     {"MODCMD_CHAN_SUSPENDED",   "This channel is currently suspended."},
68     {"MODCMD_PRIVILEGED",       "$b%s$b is a privileged command."}, /* {ARGS: "god"} */
69     {"MODCMD_PUBCMD",           "Public commands in $b%s$b are restricted."}, /* {ARGS: "#TestChan"} */
70     {"MODCMD_ACCESS_DENIED",    "Access denied."},
71     {NULL, NULL}
72 };
73
74 static int get_binds_index(char first_char) {
75     if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
76         return tolower(first_char) - 'a';
77     }
78     return 26;
79 }
80
81 struct ClientSocket* get_prefered_bot(int botid) {
82     struct ClientSocket *client, *lowbot = NULL;
83     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
84         if(client->botid == botid) {
85             if((client->flags & SOCKET_FLAG_PREFERRED))
86                 return client;
87             else
88                 lowbot = client;
89         }
90     }
91     return lowbot;
92 }
93
94 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
95     struct trigger_cache *trigger;
96     for(trigger = chan->trigger; trigger; trigger = trigger->next) {
97         if(trigger->botid == botid)
98             return trigger->trigger;
99     }
100     struct trigger_callback *cb;
101     for(cb = trigger_callbacks; cb; cb = cb->next) {
102         if(cb->botid == botid)
103             break;
104     }
105     char triggerStr[TRIGGERLEN];
106     if(cb)
107         cb->func(chan, triggerStr);
108     else
109         strcpy(triggerStr, "+");
110     trigger = malloc(sizeof(*trigger));
111     if (!trigger) {
112         perror("malloc() failed");
113         return 0;
114     }
115     trigger->botid = botid;
116     trigger->trigger = (triggerStr[0] ? strdup(triggerStr) : NULL);
117     trigger->next = chan->trigger;
118     chan->trigger = trigger;
119     return trigger->trigger;
120 }
121
122 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);
123
124 static USERAUTH_CALLBACK(command_checked_auth) {
125     struct command_check_user_cache *cache = data;
126     tmp_text_client = cache->textclient;
127     handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
128     free(cache->message);
129     if(cache->args_buffer)
130         free(cache->args_buffer);
131     free(cache->argv);
132     free(cache);
133 }
134
135 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
136     struct ChanNode *sent_chan = chan;
137     if(message[0] == '#') {
138         char *chanName = message;
139         message = strstr(message, " ");
140         if(!message) return;
141         *message = '\0';
142         message++;
143         struct ChanNode *chan2 = getChanByName(chanName);
144         if(chan2)
145             chan = chan2;
146     }
147     message = strdup(message);
148     int bind_index = get_binds_index(message[0]);
149     char *args = strstr(message, " ");
150     char *args_buffer = NULL; //we need this to save a possible pointer to a allocation we need to free
151     if(args) {
152         *args = '\0';
153         args++;
154     }
155     struct cmd_binding *cbind;
156     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
157         if(cbind->botid == client->botid && stricmp(cbind->cmd, message) == 0) {
158             if(statistics_enabled)
159                 statistics_commands++;
160             total_triggered++;
161             cbind->func->triggered++;
162             if((cbind->func->flags & CMDFLAG_FUNCMD)) {
163                 if(!sent_chan)
164                     break;
165                 chan = sent_chan;
166             }
167             //get a text bot
168             tmp_text_client = get_prefered_bot(client->botid);
169             //parse the arguments...
170             char *arga[MAXNUMPARAMS];
171             char **argv;
172             int argc = 0;
173             if(args) {
174                 while(*args) {
175                     //skip leading spaces
176                     while (*args == ' ')
177                         *args++ = 0;
178                     arga[argc++] = args;
179                     if (argc >= MAXNUMPARAMS)
180                         break;
181                     while (*args != ' ' && *args)
182                         args++;
183                 }
184             }
185             argv = arga;
186             if(argc != 0 && argv[0][0] == '#' && !(cbind->func->flags & CMDFLAG_CHAN_PARAM)) {
187                 struct ChanNode *chan2 = getChanByName(argv[0]);
188                 if(chan2) {
189                     argv += 1;
190                     argc -= 1;
191                     chan = chan2;
192                 }
193             }
194             if(cbind->paramcount) {
195                 //userdefined parameters...
196                 args_buffer = malloc(MAXLEN * 2 * sizeof(*args_buffer));
197                 int args_pos = 0;
198                 char *uargs[MAXNUMPARAMS];
199                 int uargc = 0;
200                 char *b;
201                 int i;
202                 int allargs, argi;
203                 for(i = 0; i < cbind->paramcount; i++) {
204                     b = cbind->parameters[i];
205                     if(b[0] == '%') {
206                         b++;
207                         if(b[strlen(b)-1] == '-') {
208                             allargs = strlen(b)-1;
209                             b[allargs] = '\0';
210                             argi = atoi(b);
211                             b[allargs] = '-';
212                             allargs = 1;
213                         } else if(b[strlen(b)-1] == '+') {
214                             allargs = strlen(b)-1;
215                             b[allargs] = '\0';
216                             argi = atoi(b);
217                             b[allargs] = '+';
218                             allargs = 2;
219                         } else {
220                             allargs = 0;
221                             argi = atoi(b);
222                         }
223                         if(argi > 0) {
224                             if(argi <= argc) {
225                                 uargs[uargc++] = args_buffer + args_pos;
226                                 if(allargs == 0) {
227                                     args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
228                                 } else if(allargs == 1) {
229                                     args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
230                                     for(argi++; argi <= argc; argi++) {
231                                         uargs[uargc++] = args_buffer + args_pos;
232                                         args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
233                                     }
234                                 } else if(allargs == 2) {
235                                     for(;argi <= argc; argi++) {
236                                         args_pos += sprintf(args_buffer + args_pos, (allargs ? "%s" : " %s"), argv[argi-1]);
237                                         allargs = 0;
238                                     }
239                                     args_pos++;
240                                 }
241                             } else if((cbind->func->flags & CMDFLAG_EMPTY_ARGS)) {
242                                 uargs[uargc++] = args_buffer + args_pos;
243                                 args_buffer[args_pos++] = '\0';
244                             }
245                         } else if(!strcmp(b, "c")) {
246                             uargs[uargc++] = args_buffer + args_pos;
247                             args_pos += sprintf(args_buffer + args_pos, "%s", (chan ? chan->name : "")) + 1;
248                         } else if(!strcmp(b, "n")) {
249                             uargs[uargc++] = args_buffer + args_pos;
250                             args_pos += sprintf(args_buffer + args_pos, "%s", user->nick) + 1;
251                         }
252                     } else {
253                         uargs[uargc++] = args_buffer + args_pos;
254                         args_pos += sprintf(args_buffer + args_pos, "%s", b) + 1;
255                     }
256                 }
257                 argv = uargs;
258                 argc = uargc;
259             }
260             if(argc < cbind->func->paramcount) {
261                 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
262                 break;
263             }
264             if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
265                 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
266                 break;
267             }
268             if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
269                 //check auth...
270                 struct command_check_user_cache *data = malloc(sizeof(*data));
271                 char **temp_argv = malloc(argc*sizeof(*temp_argv));
272                 if (!data || !temp_argv) {
273                     perror("malloc() failed");
274                     break;
275                 }
276                 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
277                 data->argv = temp_argv;
278                 data->argc = argc;
279                 data->client = client;
280                 data->user = user;
281                 data->chan = chan;
282                 data->sent_chan = sent_chan;
283                 data->message = message;
284                 data->args_buffer = args_buffer;
285                 data->cbind = cbind;
286                 data->textclient = tmp_text_client;
287                 get_userauth(user, command_checked_auth, data);
288                 return;
289             } else
290                 handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
291             break;
292         }
293     }
294     free(message);
295     if(args_buffer)
296         free(args_buffer);
297 }
298
299 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) {
300     MYSQL_RES *res;
301     MYSQL_ROW row;
302     int uaccess;
303     int eventflags = (cbind->func->flags & (CMDFLAG_LOG | CMDFLAG_OPLOG));
304     if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
305         reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
306         return;
307     }
308     if(sent_chan && sent_chan != chan) {
309         //check pubcmd of this channel
310         printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
311         res = mysql_use();
312         if ((row = mysql_fetch_row(res)) != NULL) {
313             uaccess = getChannelAccess(user, sent_chan);
314             if(row[0] && uaccess < atoi(row[0]) && !isGodMode(user)) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
315                 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
316                 return;
317             }
318         }
319     }
320     int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
321     if(global_access > 0) {
322         int user_global_access = 0;
323         printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
324         res = mysql_use();
325         if ((row = mysql_fetch_row(res)) != NULL) {
326             user_global_access = atoi(row[0]);
327         }
328         if(user_global_access < global_access) {
329             if(!user_global_access)
330                 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
331             else
332                 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
333             return;
334         }
335     }
336     if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
337         MYSQL_ROW defaults = NULL;
338         char access_list[256];
339         int access_pos = 0;
340         int access_count = 0;
341         int minaccess = 0;
342         char *str_a, *str_b = cbind->func->channel_access, *str_c;
343         if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
344             str_b = cbind->channel_access;
345         access_list[0] = '\0';
346         if(str_b) {
347             str_c = strdup(str_b);
348             str_b = str_c;
349             while((str_a = str_b)) {
350                 str_b = strstr(str_a, ",");
351                 if(str_b) {
352                     *str_b = '\0';
353                     str_b++;
354                 }
355                 if(*str_a == '#') {
356                     str_a++;
357                     access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
358                     access_count++;
359                 } else {
360                     if(atoi(str_a) > minaccess)
361                         minaccess = atoi(str_a);
362                 }
363             }
364             free(str_c);
365         }
366         if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
367             printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
368             res = mysql_use();
369             if ((row = mysql_fetch_row(res)) != NULL) {
370                 chan->flags |= CHANFLAG_CHAN_REGISTERED;
371                 chan->channel_id = atoi(row[0]);
372                 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
373                     uaccess = getChannelAccess(user, chan);
374                     if(uaccess < minaccess && isGodMode(user)) {
375                         eventflags |= CMDFLAG_OPLOG;
376                     } else if(uaccess < minaccess) {
377                         //ACCESS DENIED
378                         reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
379                         return;
380                     }
381                     if(!row[1] && !defaults) {
382                         printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
383                         defaults = mysql_fetch_row(mysql_use());
384                     }
385                     if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
386                         if(isGodMode(user)) {
387                             eventflags |= CMDFLAG_OPLOG;
388                         } else {
389                             //PUBCMD
390                             reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
391                             return;
392                         }
393                     }
394                     int i;
395                     for(i = 0; i < access_count; i++) {
396                         if(!row[2+i] && !defaults) {
397                             printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
398                             defaults = mysql_fetch_row(mysql_use());
399                         }
400                         if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
401                             if(isGodMode(user)) {
402                                 eventflags |= CMDFLAG_OPLOG;
403                             } else {
404                                 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
405                                 return;
406                             }
407                         }
408                     }
409                 }
410             }
411             chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
412         }
413         if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
414             reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
415             return;
416         }
417         printf_mysql_query("SELECT `botid`, `suspended` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, client->botid);
418         res = mysql_use();
419         if ((row = mysql_fetch_row(res)) == NULL) {
420             reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
421             return;
422         } else if(!strcmp(row[1], "1")) {
423             reply(tmp_text_client, user, "MODCMD_CHAN_SUSPENDED");
424             return;
425         }
426     }
427     if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
428         reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
429         return;
430     }
431     struct Event *event = createEvent(client, user, chan, cbind->func->name, argv, argc, eventflags);
432     cbind->func->func(client, user, chan, argv, argc, event);
433 }
434
435 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
436     fd_set fds;
437     char *trigger;
438     struct ClientSocket *client;
439     FD_ZERO(&fds);
440     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
441         if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
442             FD_SET(client->botid, &fds);
443             trigger = get_channel_trigger(client->botid, chan);
444             if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
445                 handle_command(client, user, chan, message + strlen(trigger));
446             }
447         }
448     }
449     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
450         if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
451             FD_SET(client->botid, &fds);
452             trigger = get_channel_trigger(client->botid, chan);
453             if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
454                 handle_command(client, user, chan, message + strlen(trigger));
455             }
456         }
457     }
458 }
459
460 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
461     struct ClientSocket *client;
462     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
463         if(client->user == target) {
464             handle_command(client, user, NULL, message);
465         }
466     }
467 }
468
469 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, char *channel_access, int global_access, unsigned int flags) {
470     struct cmd_function *cmdfunc;
471     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
472         if((cmdfunc->botid == botid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, name) == 0)
473             return 0;
474     }
475     cmdfunc = malloc(sizeof(*cmdfunc));
476     if (!cmdfunc) {
477         perror("malloc() failed");
478         return 0;
479     }
480     cmdfunc->botid = botid;
481     cmdfunc->name = strdup(name);
482     cmdfunc->func = func;
483     cmdfunc->flags = flags;
484     cmdfunc->paramcount = paramcount;
485     cmdfunc->channel_access = channel_access;
486     cmdfunc->global_access = global_access;
487     cmdfunc->triggered = 0;
488     cmdfunc->next = cmd_functions;
489     cmd_functions = cmdfunc;
490     return 1;
491 }
492
493 int set_trigger_callback(int botid, trigger_callback_t *func) {
494     static struct trigger_callback *cb = NULL;
495     for(cb = trigger_callbacks; cb; cb = cb->next) {
496         if(cb->botid == botid)
497             break;
498     }
499     if(!cb) {
500         cb = malloc(sizeof(*cb));
501         if (!cb) {
502             perror("malloc() failed");
503             return 0;
504         }
505         cb->botid = botid;
506         cb->next = trigger_callbacks;
507         trigger_callbacks = cb;
508     }
509     cb->func = func;
510     return 1;
511 }
512
513 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
514     struct trigger_cache *trigger;
515     for(trigger = chan->trigger; trigger; trigger = trigger->next) {
516         if(trigger->botid == botid) {
517             free(trigger->trigger);
518             trigger->trigger = strdup(new_trigger);
519             return 1;
520         }
521     }
522     return 0;
523 }
524
525 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
526     int bind_index = get_binds_index(cmd[0]);
527     struct cmd_binding *cbind;
528     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
529         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
530             return 0;
531     }
532     cbind = malloc(sizeof(*cbind));
533     if (!cbind) {
534         perror("malloc() failed");
535         return 0;
536     }
537     cbind->botid = botid;
538     cbind->cmd = strdup(cmd);
539     cbind->func = func;
540     cbind->paramcount = 0;
541     cbind->global_access = 0;
542     cbind->channel_access = NULL;
543     cbind->flags = 0;
544     cbind->next = cmd_binds[bind_index];
545     cmd_binds[bind_index] = cbind;
546     return 1;
547 }
548
549 int bind_cmd_to_command(int botid, char *cmd, char *func) {
550     struct cmd_function *cmdfunc;
551     int fbotid = botid;
552     char *c;
553     if((c = strstr(func, "."))) {
554         *c = '\0';
555         struct cmd_bot_alias *botalias;
556         for(botalias = bot_aliases; botalias; botalias = botalias->next) {
557             if(!stricmp(botalias->alias, func)) {
558                 fbotid = botalias->botid;
559                 break;
560             }
561         }
562         *c = '.';
563         func = c+1;
564     }
565     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
566         if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, func) == 0)
567             break;
568     }
569     if(!cmdfunc) return 0;
570     int bind_index = get_binds_index(cmd[0]);
571     struct cmd_binding *cbind;
572     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
573         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
574             return 0;
575     }
576     cbind = malloc(sizeof(*cbind));
577     if (!cbind) {
578         perror("malloc() failed");
579         return 0;
580     }
581     cbind->botid = botid;
582     cbind->cmd = strdup(cmd);
583     cbind->func = cmdfunc;
584     cbind->next = cmd_binds[bind_index];
585     cbind->paramcount = 0;
586     cbind->global_access = 0;
587     cbind->channel_access = NULL;
588     cbind->flags = 0;
589     cmd_binds[bind_index] = cbind;
590     return 1;
591 }
592
593 int unbind_cmd(int botid, char *cmd) {
594     int bind_index = get_binds_index(cmd[0]);
595     struct cmd_binding *cbind, *last = NULL;
596     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
597         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
598             if(last)
599                 last->next = cbind->next;
600             else
601                 cmd_binds[bind_index] = cbind->next;
602             free(cbind->cmd);
603             if(cbind->paramcount) {
604                 int i;
605                 for(i = 0; i < cbind->paramcount; i++)
606                     free(cbind->parameters[i]);
607             }
608             free(cbind);
609             return 1;
610         } else
611             last = cbind;
612     }
613     return 0;
614 }
615
616 struct cmd_function *find_cmd_function(int botid, char *name) {
617     struct cmd_function *cmdfunc;
618     char *c;
619     if((c = strstr(name, "."))) {
620         *c = '\0';
621         struct cmd_bot_alias *botalias;
622         for(botalias = bot_aliases; botalias; botalias = botalias->next) {
623             if(!stricmp(botalias->alias, name)) {
624                 botid = botalias->botid;
625                 break;
626             }
627         }
628         *c = '.';
629         name = c+1;
630     }
631     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
632         if((cmdfunc->botid == botid || cmdfunc->botid == 0) && stricmp(cmdfunc->name, name) == 0)
633             break;
634     }
635     return cmdfunc;
636 }
637
638 struct ClientSocket *getTextBot() {
639     return tmp_text_client;
640 }
641
642 void init_modcmd() {
643     cmd_binds = calloc(27, sizeof(*cmd_binds));
644     bind_chanmsg(got_chanmsg);
645     bind_privmsg(got_privmsg);
646     register_default_language_table(msgtab);
647 }
648
649 void free_modcmd() {
650     int i;
651     for(i = 0; i < 27; i++) {
652         struct cmd_binding *cbind, *next;
653         for(cbind = cmd_binds[i]; cbind; cbind = next) {
654             next = cbind->next;
655             free(cbind->cmd);
656             if(cbind->paramcount) {
657                 int j;
658                 for(j = 0; j < cbind->paramcount; j++)
659                     free(cbind->parameters[j]);
660             }
661             if(cbind->channel_access)
662                 free(cbind->channel_access);
663             free(cbind);
664         }
665     }
666     free(cmd_binds);
667     struct cmd_function *cmdfunct, *next;
668     for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
669         next = cmdfunct->next;
670         free(cmdfunct->name);
671         free(cmdfunct);
672     }
673     struct trigger_callback *cb, *next_cb;
674     for(cb = trigger_callbacks; cb; cb = next_cb) {
675         next_cb = cb->next;
676         free(next_cb);
677     }
678     struct cmd_bot_alias *botalias, *next_botalias;
679     for(botalias = bot_aliases; botalias; botalias = next_botalias) {
680         next_botalias = botalias->next;
681         free(botalias->alias);
682         free(botalias);
683     }
684     cmd_functions = NULL;
685     trigger_callbacks = NULL;
686     bot_aliases = NULL;
687 }
688
689 void bind_set_parameters(int botid, char *cmd, char *parameters) {
690     int bind_index = get_binds_index(cmd[0]);
691     struct cmd_binding *cbind;
692     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
693         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
694             if(cbind->paramcount) {
695                 int i;
696                 for(i = 0; i < cbind->paramcount; i++)
697                     free(cbind->parameters[i]);
698                 cbind->paramcount = 0;
699             }
700             char *a, *b = parameters;
701             do {
702                 a = strstr(b, " ");
703                 if(a) *a = '\0';
704                 cbind->parameters[cbind->paramcount++] = strdup(b);
705                 if(a) b = a+1;
706             } while(a);
707             return;
708         }
709     }
710 }
711
712 void bind_set_global_access(int botid, char *cmd, int gaccess) {
713     int bind_index = get_binds_index(cmd[0]);
714     struct cmd_binding *cbind;
715     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
716         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
717             if(gaccess > -1) {
718                 cbind->global_access = gaccess;
719                 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
720             } else {
721                 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
722             }
723             return;
724         }
725     }
726 }
727
728 void bind_set_channel_access(int botid, char *cmd, char *chanaccess) {
729     int bind_index = get_binds_index(cmd[0]);
730     struct cmd_binding *cbind;
731     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
732         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
733             if(cbind->channel_access)
734                 free(cbind->channel_access);
735             if(chanaccess) {
736                 cbind->channel_access = strdup(chanaccess);
737                 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
738             } else {
739                 cbind->channel_access = NULL;
740                 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
741             }
742             return;
743         }
744     }
745 }
746
747 struct cmd_binding *find_cmd_binding(int botid, char *cmd) {
748     int bind_index = get_binds_index(cmd[0]);
749     struct cmd_binding *cbind;
750     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
751         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
752             return cbind;
753         }
754     }
755     return NULL;
756 }
757
758 void bind_unbound_required_functions(int botid) {
759     struct cmd_function *cmdfunc;
760     int i, found;
761     struct cmd_binding *cbind;
762     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
763         if((cmdfunc->flags & CMDFLAG_REQUIRED)) {
764             found = 0;
765             for(i = 0; i < 27; i++) {
766                 for(cbind = cmd_binds[i]; cbind; cbind = cbind->next) {
767                     if(cbind->botid == botid && cbind->func == cmdfunc) {
768                         found = 1;
769                         break;
770                     }
771                 }
772                 if(found)
773                     break;
774             }
775             if(!found && bind_cmd_to_function(botid, cmdfunc->name, cmdfunc)) {
776                 cbind = find_cmd_binding(botid, cmdfunc->name);
777                 cbind->flags |= CMDFLAG_TEMPONARY_BIND;
778             }
779         }
780     }
781 }
782
783 void register_command_alias(int botid, char *alias) {
784     struct cmd_bot_alias *botalias;
785     for(botalias = bot_aliases; botalias; botalias = botalias->next) {
786         if(!stricmp(botalias->alias, alias))
787             return;
788     }
789     botalias = malloc(sizeof(*botalias));
790     if (!botalias) {
791         perror("malloc() failed");
792         return;
793     }
794     botalias->botid = botid;
795     botalias->alias = strdup(alias);
796     botalias->next = bot_aliases;
797     bot_aliases = botalias;
798 }
799
800 struct cmd_binding *getAllBinds(struct cmd_binding *last) {
801     int bind_index;
802     if(last) {
803         if(last->next)
804             return last->next;
805         bind_index = get_binds_index(last->cmd[0]) + 1;
806         if(bind_index > 26)
807             return NULL;
808     } else
809         bind_index = 0;
810     do {
811         if(cmd_binds[bind_index])
812             return cmd_binds[bind_index];
813         bind_index++;
814     } while(bind_index < 27);
815     return NULL;
816 }