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