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