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