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