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