fixed modcmd parameter handling
[NeonServV5.git] / src / modcmd.c
1 /* modcmd.c - NeonServ v5.0
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 = 1;
184                             b[strlen(b)-1] = '\0';
185                             argi = atoi(b);
186                             b[strlen(b)-1] = '-';
187                         } else {
188                             allargs = 0;
189                             argi = atoi(b);
190                         }
191                         if(argi > 0) {
192                             if(argi <= argc) {
193                                 uargs[uargc++] = argv[argi-1];
194                                 if(allargs) {
195                                     for(argi++; argi <= argc; argi++)
196                                         uargs[uargc++] = argv[argi-1];
197                                 }
198                             }
199                         } else if(!strcmp(b, "c")) {
200                             uargs[uargc++] = (chan ? chan->name : NULL);
201                         } else if(!strcmp(b, "n")) {
202                             uargs[uargc++] = user->nick;
203                         }
204                     } else {
205                         uargs[uargc++] = b;
206                     }
207                 }
208                 argv = uargs;
209                 argc = uargc;
210             }
211             if(argc < cbind->func->paramcount) {
212                 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
213                 break;
214             }
215             if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
216                 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
217                 break;
218             }
219             if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
220                 //check auth...
221                 struct command_check_user_cache *data = malloc(sizeof(*data));
222                 char **temp_argv = malloc(argc*sizeof(*temp_argv));
223                 if (!data || !temp_argv) {
224                     perror("malloc() failed");
225                     break;
226                 }
227                 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
228                 data->argv = temp_argv;
229                 data->argc = argc;
230                 data->client = client;
231                 data->user = user;
232                 data->chan = chan;
233                 data->sent_chan = sent_chan;
234                 data->message = message;
235                 data->cbind = cbind;
236                 data->textclient = tmp_text_client;
237                 get_userauth(user, command_checked_auth, data);
238                 return;
239             } else
240                 handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
241             break;
242         }
243     }
244     free(message);
245 }
246
247 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) {
248     MYSQL_RES *res;
249     MYSQL_ROW row;
250     int uaccess;
251     int eventflags = (cbind->func->flags & (CMDFLAG_LOG | CMDFLAG_OPLOG));
252     if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
253         reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
254         return;
255     }
256     if(sent_chan && sent_chan != chan) {
257         //check pubcmd of this channel
258         printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
259         res = mysql_use();
260         if ((row = mysql_fetch_row(res)) != NULL) {
261             uaccess = getChannelAccess(user, sent_chan, 1);
262             if(row[0] && uaccess < atoi(row[0])) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
263                 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
264                 return;
265             }
266         }
267     }
268     int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
269     if(global_access > 0) {
270         int user_global_access = 0;
271         printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
272         res = mysql_use();
273         if ((row = mysql_fetch_row(res)) != NULL) {
274             user_global_access = atoi(row[0]);
275         }
276         if(user_global_access < global_access) {
277             if(!user_global_access)
278                 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
279             else
280                 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
281             return;
282         }
283     }
284     if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
285         MYSQL_ROW defaults = NULL;
286         char access_list[256];
287         int access_pos = 0;
288         int access_count = 0;
289         int minaccess = 0;
290         char *str_a, *str_b = cbind->func->channel_access, *str_c;
291         if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
292             str_b = cbind->channel_access;
293         access_list[0] = '\0';
294         if(str_b) {
295             str_c = strdup(str_b);
296             str_b = str_c;
297             while((str_a = str_b)) {
298                 str_b = strstr(str_a, ",");
299                 if(str_b) {
300                     *str_b = '\0';
301                     str_b++;
302                 }
303                 if(*str_a == '#') {
304                     str_a++;
305                     access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
306                     access_count++;
307                 } else {
308                     if(atoi(str_a) > minaccess)
309                         minaccess = atoi(str_a);
310                 }
311             }
312             free(str_c);
313         }
314         if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
315             printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
316             res = mysql_use();
317             if ((row = mysql_fetch_row(res)) != NULL) {
318                 chan->flags |= CHANFLAG_CHAN_REGISTERED;
319                 chan->channel_id = atoi(row[0]);
320                 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
321                     uaccess = getChannelAccess(user, chan, 0);
322                     if(uaccess < minaccess && isGodMode(user)) {
323                         eventflags |= CMDFLAG_OPLOG;
324                     } else if(uaccess < minaccess) {
325                         //ACCESS DENIED
326                         reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
327                         return;
328                     }
329                     if(!row[1] && !defaults) {
330                         printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
331                         defaults = mysql_fetch_row(mysql_use());
332                     }
333                     if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
334                         if(isGodMode(user)) {
335                             eventflags |= CMDFLAG_OPLOG;
336                         } else {
337                             //PUBCMD
338                             reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
339                             return;
340                         }
341                     }
342                     int i;
343                     for(i = 0; i < access_count; i++) {
344                         if(!row[2+i] && !defaults) {
345                             printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
346                             defaults = mysql_fetch_row(mysql_use());
347                         }
348                         if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
349                             if(isGodMode(user)) {
350                                 eventflags |= CMDFLAG_OPLOG;
351                             } else {
352                                 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
353                                 return;
354                             }
355                         }
356                     }
357                 }
358             }
359             chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
360         }
361         if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
362             reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
363             return;
364         }
365         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);
366         res = mysql_use();
367         if ((row = mysql_fetch_row(res)) == NULL) {
368             reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
369             return;
370         } else if(!strcmp(row[1], "1")) {
371             reply(tmp_text_client, user, "MODCMD_CHAN_SUSPENDED");
372             return;
373         }
374     }
375     if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
376         reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
377         return;
378     }
379     struct Event *event = createEvent(client, user, chan, cbind->func->name, argv, argc, eventflags);
380     cbind->func->func(client, user, chan, argv, argc, event);
381 }
382
383 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
384     fd_set fds;
385     char *trigger;
386     struct ClientSocket *client;
387     FD_ZERO(&fds);
388     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
389         if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
390             FD_SET(client->botid, &fds);
391             trigger = get_channel_trigger(client->botid, chan);
392             if(stricmplen(message, trigger, strlen(trigger)) == 0) {
393                 handle_command(client, user, chan, message + strlen(trigger));
394             }
395         }
396     }
397     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
398         if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
399             FD_SET(client->botid, &fds);
400             trigger = get_channel_trigger(client->botid, chan);
401             if(stricmplen(message, trigger, strlen(trigger)) == 0) {
402                 handle_command(client, user, chan, message + strlen(trigger));
403             }
404         }
405     }
406 }
407
408 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
409     struct ClientSocket *client;
410     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
411         if(client->user == target) {
412             handle_command(client, user, NULL, message);
413         }
414     }
415 }
416
417 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, char *channel_access, int global_access, unsigned int flags) {
418     struct cmd_function *cmdfunc;
419     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
420         if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
421             return 0;
422     }
423     cmdfunc = malloc(sizeof(*cmdfunc));
424     if (!cmdfunc) {
425         perror("malloc() failed");
426         return 0;
427     }
428     cmdfunc->botid = botid;
429     cmdfunc->name = strdup(name);
430     cmdfunc->func = func;
431     cmdfunc->flags = flags;
432     cmdfunc->paramcount = paramcount;
433     cmdfunc->channel_access = channel_access;
434     cmdfunc->global_access = global_access;
435     cmdfunc->next = cmd_functions;
436     cmd_functions = cmdfunc;
437     return 1;
438 }
439
440 int set_trigger_callback(int botid, trigger_callback_t *func) {
441     static struct trigger_callback *cb = NULL;
442     for(cb = trigger_callbacks; cb; cb = cb->next) {
443         if(cb->botid == botid)
444             break;
445     }
446     if(!cb) {
447         cb = malloc(sizeof(*cb));
448         if (!cb) {
449             perror("malloc() failed");
450             return 0;
451         }
452         cb->botid = botid;
453         cb->next = trigger_callbacks;
454         trigger_callbacks = cb;
455     }
456     cb->func = func;
457     return 1;
458 }
459
460 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
461     struct trigger_cache *trigger;
462     for(trigger = chan->trigger; trigger; trigger = trigger->next) {
463         if(trigger->botid == botid) {
464             free(trigger->trigger);
465             trigger->trigger = strdup(new_trigger);
466             return 1;
467         }
468     }
469     return 0;
470 }
471
472 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
473     int bind_index = get_binds_index(cmd[0]);
474     struct cmd_binding *cbind;
475     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
476         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
477             return 0;
478     }
479     cbind = malloc(sizeof(*cbind));
480     if (!cbind) {
481         perror("malloc() failed");
482         return 0;
483     }
484     cbind->botid = botid;
485     cbind->cmd = strdup(cmd);
486     cbind->func = func;
487     cbind->paramcount = 0;
488     cbind->global_access = 0;
489     cbind->channel_access = NULL;
490     cbind->flags = 0;
491     cbind->next = cmd_binds[bind_index];
492     cmd_binds[bind_index] = cbind;
493     return 1;
494 }
495
496 int bind_cmd_to_command(int botid, char *cmd, char *func) {
497     struct cmd_function *cmdfunc;
498     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
499         if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
500             break;
501     }
502     if(!cmdfunc) return 0;
503     int bind_index = get_binds_index(cmd[0]);
504     struct cmd_binding *cbind;
505     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
506         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
507             return 0;
508     }
509     cbind = malloc(sizeof(*cbind));
510     if (!cbind) {
511         perror("malloc() failed");
512         return 0;
513     }
514     cbind->botid = botid;
515     cbind->cmd = strdup(cmd);
516     cbind->func = cmdfunc;
517     cbind->next = cmd_binds[bind_index];
518     cbind->paramcount = 0;
519     cbind->global_access = 0;
520     cbind->channel_access = NULL;
521     cbind->flags = 0;
522     cmd_binds[bind_index] = cbind;
523     return 1;
524 }
525
526 int unbind_cmd(int botid, char *cmd) {
527     int bind_index = get_binds_index(cmd[0]);
528     struct cmd_binding *cbind, *last = NULL;
529     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
530         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
531             if(last)
532                 last->next = cbind->next;
533             else
534                 cmd_binds[bind_index] = cbind->next;
535             free(cbind->cmd);
536             if(cbind->paramcount) {
537                 int i;
538                 for(i = 0; i < cbind->paramcount; i++)
539                     free(cbind->parameters[i]);
540             }
541             free(cbind);
542             return 1;
543         } else
544             last = cbind;
545     }
546     return 0;
547 }
548
549 struct cmd_function *find_cmd_function(int botid, char *name) {
550     struct cmd_function *cmdfunc;
551     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
552         if(cmdfunc->botid == botid && stricmp(cmdfunc->name, name) == 0)
553             break;
554     }
555     return cmdfunc;
556 }
557
558 struct ClientSocket *getTextBot() {
559     return tmp_text_client;
560 }
561
562 void init_modcmd() {
563     cmd_binds = calloc(27, sizeof(*cmd_binds));
564     bind_chanmsg(got_chanmsg);
565     bind_privmsg(got_privmsg);
566     register_default_language_table(msgtab);
567 }
568
569 void free_modcmd() {
570     int i;
571     for(i = 0; i < 27; i++) {
572         struct cmd_binding *cbind, *next;
573         for(cbind = cmd_binds[i]; cbind; cbind = next) {
574             next = cbind->next;
575             free(cbind->cmd);
576             if(cbind->paramcount) {
577                 int j;
578                 for(j = 0; j < cbind->paramcount; j++)
579                     free(cbind->parameters[j]);
580             }
581             if(cbind->channel_access)
582                 free(cbind->channel_access);
583             free(cbind);
584         }
585     }
586     free(cmd_binds);
587     struct cmd_function *cmdfunct, *next;
588     for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
589         next = cmdfunct->next;
590         free(cmdfunct->name);
591         free(cmdfunct);
592     }
593     struct trigger_callback *cb, *next_cb;
594     for(cb = trigger_callbacks; cb; cb = next_cb) {
595         next_cb = cb->next;
596         free(next_cb);
597     }
598     cmd_functions = NULL;
599     trigger_callbacks = NULL;
600 }
601
602 void bind_set_parameters(int botid, char *cmd, char *parameters) {
603     int bind_index = get_binds_index(cmd[0]);
604     struct cmd_binding *cbind;
605     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
606         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
607             if(cbind->paramcount) {
608                 int i;
609                 for(i = 0; i < cbind->paramcount; i++)
610                     free(cbind->parameters[i]);
611                 cbind->paramcount = 0;
612             }
613             char *a, *b = parameters;
614             do {
615                 a = strstr(b, " ");
616                 if(a) *a = '\0';
617                 cbind->parameters[cbind->paramcount++] = strdup(b);
618                 if(a) b = a+1;
619             } while(a);
620             return;
621         }
622     }
623 }
624
625 void bind_set_global_access(int botid, char *cmd, int gaccess) {
626     int bind_index = get_binds_index(cmd[0]);
627     struct cmd_binding *cbind;
628     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
629         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
630             if(gaccess > -1) {
631                 cbind->global_access = gaccess;
632                 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
633             } else {
634                 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
635             }
636             return;
637         }
638     }
639 }
640
641 void bind_set_channel_access(int botid, char *cmd, char *chanaccess) {
642     int bind_index = get_binds_index(cmd[0]);
643     struct cmd_binding *cbind;
644     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
645         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
646             if(cbind->channel_access)
647                 free(cbind->channel_access);
648             if(chanaccess) {
649                 cbind->channel_access = strdup(chanaccess);
650                 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
651             } else {
652                 cbind->channel_access = NULL;
653                 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
654             }
655             return;
656         }
657     }
658 }
659
660 struct cmd_binding *find_cmd_binding(int botid, char *cmd) {
661     int bind_index = get_binds_index(cmd[0]);
662     struct cmd_binding *cbind;
663     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
664         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
665             return cbind;
666         }
667     }
668     return NULL;
669 }
670