1 /* modcmd.c - NeonServ v5.2
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
19 #include "IRCEvents.h"
20 #include "IRCParser.h"
21 #include "ClientSocket.h"
25 #include "WHOHandler.h"
27 #include "mysqlConn.h"
29 #include "EventLogger.h"
31 struct trigger_callback {
33 trigger_callback_t *func;
35 struct trigger_callback *next;
38 struct cmd_bot_alias {
42 struct cmd_bot_alias *next;
45 struct command_check_user_cache {
46 struct ClientSocket *client, *textclient;
47 struct UserNode *user;
48 struct ChanNode *chan, *sent_chan;
51 char *message, *args_buffer;
52 struct cmd_binding *cbind;
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;
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 {"MODCMD_SUBCOMMANDS", "Subcommands of %s: %s"}, /* {ARGS: "bot", "ADD, DEL, EDIT"} */
72 {"MODCMD_CROSSCHAN", "You must be in %s (or on its userlist) to use this command."},
76 static int get_binds_index(char first_char) {
77 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
78 return tolower(first_char) - 'a';
83 struct ClientSocket* get_prefered_bot(int botid) {
84 struct ClientSocket *client, *lowbot = NULL;
85 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
86 if(client->botid == botid) {
87 if((client->flags & SOCKET_FLAG_PREFERRED))
96 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
97 struct trigger_cache *trigger;
98 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
99 if(trigger->botid == botid)
100 return trigger->trigger;
102 struct trigger_callback *cb;
103 for(cb = trigger_callbacks; cb; cb = cb->next) {
104 if(cb->botid == botid)
107 char triggerStr[TRIGGERLEN];
109 cb->func(chan, triggerStr);
111 strcpy(triggerStr, "+");
112 trigger = malloc(sizeof(*trigger));
114 perror("malloc() failed");
117 trigger->botid = botid;
118 trigger->trigger = (triggerStr[0] ? strdup(triggerStr) : NULL);
119 trigger->next = chan->trigger;
120 chan->trigger = trigger;
121 return trigger->trigger;
124 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);
126 static USERAUTH_CALLBACK(command_checked_auth) {
127 struct command_check_user_cache *cache = data;
128 tmp_text_client = cache->textclient;
129 handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
130 free(cache->message);
131 if(cache->args_buffer)
132 free(cache->args_buffer);
137 static CMD_BIND(modcmd_linker) {
138 //fake command for subcommands
142 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
143 struct ChanNode *sent_chan = chan;
144 if(message[0] == '#') {
145 char *chanName = message;
146 message = strstr(message, " ");
150 struct ChanNode *chan2 = getChanByName(chanName);
154 message = strdup(message);
155 int bind_index = get_binds_index(message[0]);
156 char *args = strstr(message, " ");
157 char *args_buffer = NULL; //we need this to save a possible pointer to a allocation we need to free
162 struct cmd_binding *cbind;
163 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
164 if(cbind->botid == client->botid && stricmp(cbind->cmd, message) == 0) {
165 if(cbind->func->func == modcmd_linker) {
167 char command[MAXLEN];
168 struct cmd_binding *parent_bind = cbind;
171 args = strstr(args, " ");
176 sprintf(command, "%s %s", parent_bind->cmd, subcmd);
177 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
178 if(cbind->botid == client->botid && stricmp(cbind->cmd, command) == 0)
184 //list all sub commands
185 int commandlen = sprintf(command, "%s ", parent_bind->cmd);
186 char subcommands[MAXLEN];
188 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
189 if(cbind->botid == client->botid && stricmplen(cbind->cmd, command, commandlen) == 0) {
190 subcompos += sprintf(subcommands + subcompos, (subcompos ? ", %s" : "%s"), cbind->cmd + commandlen);
193 reply(tmp_text_client, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, subcommands);
197 if(statistics_enabled)
198 statistics_commands++;
201 if((cbind->func->flags & CMDFLAG_FUNCMD)) {
207 tmp_text_client = get_prefered_bot(client->botid);
208 //parse the arguments...
209 char *arga[MAXNUMPARAMS];
216 //skip leading spaces
220 if (argc >= MAXNUMPARAMS)
222 while ((escape || *args != ' ') && *args) {
223 if((cbind->func->flags & CMDFLAG_ESCAPE_ARGS) && *args == '\\') {
228 if(!escape && offset) {
229 args[0 - offset] = args[0];
235 args[0-offset] = '\0';
241 if(argc != 0 && argv[0][0] == '#' && !(cbind->func->flags & CMDFLAG_CHAN_PARAM)) {
242 struct ChanNode *chan2 = getChanByName(argv[0]);
249 if(cbind->paramcount) {
250 //userdefined parameters...
251 args_buffer = malloc(MAXLEN * 2 * sizeof(*args_buffer));
253 char *uargs[MAXNUMPARAMS];
258 for(i = 0; i < cbind->paramcount; i++) {
259 b = cbind->parameters[i];
262 if(b[strlen(b)-1] == '-') {
263 allargs = strlen(b)-1;
268 } else if(b[strlen(b)-1] == '+') {
269 allargs = strlen(b)-1;
280 uargs[uargc++] = args_buffer + args_pos;
282 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
283 } else if(allargs == 1) {
284 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
285 for(argi++; argi <= argc; argi++) {
286 uargs[uargc++] = args_buffer + args_pos;
287 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
289 } else if(allargs == 2) {
290 for(;argi <= argc; argi++) {
291 args_pos += sprintf(args_buffer + args_pos, (allargs ? "%s" : " %s"), argv[argi-1]);
296 } else if((cbind->func->flags & CMDFLAG_EMPTY_ARGS)) {
297 uargs[uargc++] = args_buffer + args_pos;
298 args_buffer[args_pos++] = '\0';
300 } else if(!strcmp(b, "c")) {
301 uargs[uargc++] = args_buffer + args_pos;
302 args_pos += sprintf(args_buffer + args_pos, "%s", (chan ? chan->name : "")) + 1;
303 } else if(!strcmp(b, "n")) {
304 uargs[uargc++] = args_buffer + args_pos;
305 args_pos += sprintf(args_buffer + args_pos, "%s", user->nick) + 1;
308 uargs[uargc++] = args_buffer + args_pos;
309 args_pos += sprintf(args_buffer + args_pos, "%s", b) + 1;
315 if(argc < cbind->func->paramcount) {
316 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
319 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
320 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
323 if(((cbind->func->flags & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) {
325 struct command_check_user_cache *data = malloc(sizeof(*data));
326 char **temp_argv = malloc(argc*sizeof(*temp_argv));
327 if (!data || !temp_argv) {
328 perror("malloc() failed");
331 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
332 data->argv = temp_argv;
334 data->client = client;
337 data->sent_chan = sent_chan;
338 data->message = message;
339 data->args_buffer = args_buffer;
341 data->textclient = tmp_text_client;
342 get_userauth(user, command_checked_auth, data);
345 handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
354 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) {
358 char requested_uaccess = 0;
359 int eventflags = (cbind->func->flags & (CMDFLAG_LOG | CMDFLAG_OPLOG));
360 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
361 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
364 if(chan && sent_chan != chan && !isUserOnChan(user, chan)) {
365 char user_in_chan = 0;
366 if((user->flags & USERFLAG_ISAUTHED)) {
367 //maybe there's another user authed to user->auth on the channel...
368 struct ChanUser *cchanuser;
369 for(cchanuser = getChannelUsers(chan, NULL); cchanuser; cchanuser = getChannelUsers(chan, cchanuser)) {
370 if((cchanuser->user->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cchanuser->user->auth)) {
377 //check if we are allowed to execute commands in this channel
378 requested_uaccess = 1;
379 uaccess = getChannelAccess(user, chan);
381 reply(tmp_text_client, user, "MODCMD_CROSSCHAN", chan->name);
386 if(sent_chan && sent_chan != chan) {
387 //check pubcmd of this channel
388 printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
390 if ((row = mysql_fetch_row(res)) != NULL) {
391 int saccess = getChannelAccess(user, sent_chan);
392 if(row[0] && saccess < atoi(row[0]) && !isGodMode(user)) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
393 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
398 int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
399 if(global_access > 0) {
400 int user_global_access = 0;
401 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
403 if ((row = mysql_fetch_row(res)) != NULL) {
404 user_global_access = atoi(row[0]);
406 if(user_global_access < global_access) {
407 if(!user_global_access)
408 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
410 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
414 if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
415 MYSQL_ROW defaults = NULL;
416 char access_list[256];
418 int access_count = 0;
420 char *str_a, *str_b = cbind->func->channel_access, *str_c;
421 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
422 str_b = cbind->channel_access;
423 access_list[0] = '\0';
425 str_c = strdup(str_b);
427 while((str_a = str_b)) {
428 str_b = strstr(str_a, ",");
435 access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
438 if(atoi(str_a) > minaccess)
439 minaccess = atoi(str_a);
444 if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
445 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
447 if ((row = mysql_fetch_row(res)) != NULL) {
448 chan->flags |= CHANFLAG_CHAN_REGISTERED;
449 chan->channel_id = atoi(row[0]);
450 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
451 if(!requested_uaccess) uaccess = getChannelAccess(user, chan);
452 if(uaccess < minaccess && isGodMode(user)) {
453 eventflags |= CMDFLAG_OPLOG;
454 } else if(uaccess < minaccess) {
456 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
459 if(!row[1] && !defaults) {
460 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
461 defaults = mysql_fetch_row(mysql_use());
463 if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
464 if(isGodMode(user)) {
465 eventflags |= CMDFLAG_OPLOG;
468 reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
473 for(i = 0; i < access_count; i++) {
474 if(!row[2+i] && !defaults) {
475 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
476 defaults = mysql_fetch_row(mysql_use());
478 if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
479 if(isGodMode(user)) {
480 eventflags |= CMDFLAG_OPLOG;
482 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
489 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
491 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
492 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
495 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);
497 if ((row = mysql_fetch_row(res)) == NULL) {
498 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
500 } else if(!strcmp(row[1], "1")) {
501 reply(tmp_text_client, user, "MODCMD_CHAN_SUSPENDED");
505 if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
506 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
509 struct Event *event = createEvent(client, user, chan, cbind, argv, argc, eventflags);
510 cbind->func->func(client, user, chan, argv, argc, event);
513 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
516 struct ClientSocket *client;
518 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
519 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
520 FD_SET(client->botid, &fds);
521 trigger = get_channel_trigger(client->botid, chan);
522 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
523 handle_command(client, user, chan, message + strlen(trigger));
527 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
528 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
529 FD_SET(client->botid, &fds);
530 trigger = get_channel_trigger(client->botid, chan);
531 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
532 handle_command(client, user, chan, message + strlen(trigger));
538 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
539 struct ClientSocket *client;
540 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
541 if(client->user == target) {
542 handle_command(client, user, NULL, message);
547 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, char *channel_access, int global_access, unsigned int flags) {
548 struct cmd_function *cmdfunc;
549 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
550 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, name) == 0)
553 cmdfunc = malloc(sizeof(*cmdfunc));
555 perror("malloc() failed");
558 cmdfunc->botid = botid;
559 cmdfunc->name = strdup(name);
560 cmdfunc->func = func;
561 cmdfunc->flags = flags;
562 cmdfunc->paramcount = paramcount;
563 cmdfunc->channel_access = channel_access;
564 cmdfunc->global_access = global_access;
565 cmdfunc->next = cmd_functions;
566 cmd_functions = cmdfunc;
570 int set_trigger_callback(int botid, trigger_callback_t *func) {
571 static struct trigger_callback *cb = NULL;
572 for(cb = trigger_callbacks; cb; cb = cb->next) {
573 if(cb->botid == botid)
577 cb = malloc(sizeof(*cb));
579 perror("malloc() failed");
583 cb->next = trigger_callbacks;
584 trigger_callbacks = cb;
590 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
591 struct trigger_cache *trigger;
592 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
593 if(trigger->botid == botid) {
594 free(trigger->trigger);
595 trigger->trigger = strdup(new_trigger);
602 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
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)
609 cbind = malloc(sizeof(*cbind));
611 perror("malloc() failed");
614 cbind->botid = botid;
615 cbind->cmd = strdup(cmd);
617 cbind->paramcount = 0;
618 cbind->global_access = 0;
619 cbind->channel_access = NULL;
621 cbind->triggered = 0;
622 cbind->next = cmd_binds[bind_index];
623 cmd_binds[bind_index] = cbind;
627 int bind_cmd_to_command(int botid, char *cmd, char *func) {
628 struct cmd_function *cmdfunc;
631 if((c = strstr(func, "."))) {
633 struct cmd_bot_alias *botalias;
634 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
635 if(!stricmp(botalias->alias, func)) {
636 fbotid = botalias->botid;
643 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
644 if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, func) == 0)
647 if(!cmdfunc) return 0;
648 int bind_index = get_binds_index(cmd[0]);
649 struct cmd_binding *cbind;
650 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
651 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
654 cbind = malloc(sizeof(*cbind));
656 perror("malloc() failed");
659 cbind->botid = botid;
660 cbind->cmd = strdup(cmd);
661 cbind->func = cmdfunc;
662 cbind->next = cmd_binds[bind_index];
663 cbind->paramcount = 0;
664 cbind->global_access = 0;
665 cbind->channel_access = NULL;
667 cbind->triggered = 0;
668 cmd_binds[bind_index] = cbind;
672 int unbind_cmd(int botid, char *cmd) {
673 int bind_index = get_binds_index(cmd[0]);
674 struct cmd_binding *cbind, *last = NULL;
675 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
676 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
678 last->next = cbind->next;
680 cmd_binds[bind_index] = cbind->next;
682 if(cbind->paramcount) {
684 for(i = 0; i < cbind->paramcount; i++)
685 free(cbind->parameters[i]);
695 struct cmd_function *find_cmd_function(int botid, char *name) {
696 struct cmd_function *cmdfunc;
698 if((c = strstr(name, "."))) {
700 struct cmd_bot_alias *botalias;
701 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
702 if(!stricmp(botalias->alias, name)) {
703 botid = botalias->botid;
710 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
711 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && stricmp(cmdfunc->name, name) == 0)
717 struct ClientSocket *getTextBot() {
718 return tmp_text_client;
722 cmd_binds = calloc(27, sizeof(*cmd_binds));
723 bind_chanmsg(got_chanmsg);
724 bind_privmsg(got_privmsg);
725 register_default_language_table(msgtab);
726 register_command(0, "linker", modcmd_linker, 0, 0, 0, 0); //fake command for subcommands
731 for(i = 0; i < 27; i++) {
732 struct cmd_binding *cbind, *next;
733 for(cbind = cmd_binds[i]; cbind; cbind = next) {
736 if(cbind->paramcount) {
738 for(j = 0; j < cbind->paramcount; j++)
739 free(cbind->parameters[j]);
741 if(cbind->channel_access)
742 free(cbind->channel_access);
747 struct cmd_function *cmdfunct, *next;
748 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
749 next = cmdfunct->next;
750 free(cmdfunct->name);
753 struct trigger_callback *cb, *next_cb;
754 for(cb = trigger_callbacks; cb; cb = next_cb) {
758 struct cmd_bot_alias *botalias, *next_botalias;
759 for(botalias = bot_aliases; botalias; botalias = next_botalias) {
760 next_botalias = botalias->next;
761 free(botalias->alias);
764 cmd_functions = NULL;
765 trigger_callbacks = NULL;
769 void bind_set_parameters(int botid, char *cmd, char *parameters) {
770 int bind_index = get_binds_index(cmd[0]);
771 struct cmd_binding *cbind;
772 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
773 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
774 if(cbind->paramcount) {
776 for(i = 0; i < cbind->paramcount; i++)
777 free(cbind->parameters[i]);
778 cbind->paramcount = 0;
780 char *a, *b = parameters;
784 cbind->parameters[cbind->paramcount++] = strdup(b);
792 void bind_set_global_access(int botid, char *cmd, int gaccess) {
793 int bind_index = get_binds_index(cmd[0]);
794 struct cmd_binding *cbind;
795 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
796 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
798 cbind->global_access = gaccess;
799 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
801 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
808 void bind_set_channel_access(int botid, char *cmd, char *chanaccess) {
809 int bind_index = get_binds_index(cmd[0]);
810 struct cmd_binding *cbind;
811 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
812 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
813 if(cbind->channel_access)
814 free(cbind->channel_access);
816 cbind->channel_access = strdup(chanaccess);
817 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
819 cbind->channel_access = NULL;
820 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
827 struct cmd_binding *find_cmd_binding(int botid, char *cmd) {
828 int bind_index = get_binds_index(cmd[0]);
829 struct cmd_binding *cbind;
830 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
831 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
838 void bind_unbound_required_functions(int botid) {
839 struct cmd_function *cmdfunc;
841 struct cmd_binding *cbind;
842 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
843 if((cmdfunc->flags & CMDFLAG_REQUIRED)) {
845 for(i = 0; i < 27; i++) {
846 for(cbind = cmd_binds[i]; cbind; cbind = cbind->next) {
847 if(cbind->botid == botid && cbind->func == cmdfunc) {
855 if(!found && bind_cmd_to_function(botid, cmdfunc->name, cmdfunc)) {
856 cbind = find_cmd_binding(botid, cmdfunc->name);
857 cbind->flags |= CMDFLAG_TEMPONARY_BIND;
863 void register_command_alias(int botid, char *alias) {
864 struct cmd_bot_alias *botalias;
865 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
866 if(!stricmp(botalias->alias, alias))
869 botalias = malloc(sizeof(*botalias));
871 perror("malloc() failed");
874 botalias->botid = botid;
875 botalias->alias = strdup(alias);
876 botalias->next = bot_aliases;
877 bot_aliases = botalias;
880 struct cmd_binding *getAllBinds(struct cmd_binding *last) {
885 bind_index = get_binds_index(last->cmd[0]) + 1;
891 if(cmd_binds[bind_index])
892 return cmd_binds[bind_index];
894 } while(bind_index < 27);