1 /* modcmd.c - NeonServ v5.6
2 * Copyright (C) 2011-2012 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 {
34 trigger_callback_t *func;
36 struct trigger_callback *next;
39 struct cmd_bot_alias {
43 struct cmd_bot_alias *next;
46 struct command_check_user_cache {
47 struct ClientSocket *client, *textclient;
48 struct UserNode *user;
49 struct ChanNode *chan, *sent_chan;
52 char *message, *args_buffer;
53 struct cmd_binding *cbind;
56 static struct cmd_binding **cmd_binds;
57 static struct cmd_function *cmd_functions = NULL;
58 static struct trigger_callback *trigger_callbacks = NULL;
59 static struct cmd_bot_alias *bot_aliases = NULL;
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."}, /* {ARGS: "#TestChan"} */
73 {"MODCMD_UNKNOWN", "$b%s$b is an unknown command."}, /* {ARGS: "bla"} */
77 static int get_binds_index(char first_char) {
78 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
79 return tolower(first_char) - 'a';
84 struct ClientSocket* get_botwise_prefered_bot(int botid, int clientid) {
85 struct ClientSocket *client, *lowbot = NULL;
86 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
87 if(client->botid == botid && (botid || clientid == client->clientid)) {
88 if((client->flags & SOCKET_FLAG_PREFERRED))
97 static char* get_channel_trigger(int botid, int clientid, struct ChanNode *chan) {
98 struct trigger_cache *trigger;
99 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
100 if(trigger->botid == botid && (botid || trigger->clientid == clientid))
101 return trigger->trigger;
103 struct trigger_callback *cb;
104 for(cb = trigger_callbacks; cb; cb = cb->next) {
105 if(cb->botid == botid)
108 char triggerStr[TRIGGERLEN];
110 cb->func(clientid, chan, triggerStr);
112 triggerStr[0] = '\0';
113 trigger = malloc(sizeof(*trigger));
115 perror("malloc() failed");
118 trigger->botid = botid;
119 trigger->clientid = clientid;
120 trigger->trigger = (triggerStr[0] ? strdup(triggerStr) : NULL);
121 trigger->next = chan->trigger;
122 chan->trigger = trigger;
123 return trigger->trigger;
126 static void handle_command_async(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct ChanNode *sent_chan, struct cmd_binding *cbind, char **argv, int argc);
128 static USERAUTH_CALLBACK(command_checked_auth) {
129 struct command_check_user_cache *cache = data;
131 handle_command_async(cache->client, cache->textclient, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
132 free(cache->message);
133 if(cache->args_buffer)
134 free(cache->args_buffer);
139 static CMD_BIND(modcmd_linker) {
140 //fake command for subcommands
144 static struct cmd_binding *modcmd_linker_command(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, int bind_index, char **args_ptr) {
146 char command[MAXLEN];
147 struct cmd_binding *parent_bind = cbind;
148 char *args = *args_ptr;
151 args = strstr(args, " ");
156 sprintf(command, "%s %s", parent_bind->cmd, subcmd);
157 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
158 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0)
162 if(cbind && cbind->func->func == modcmd_linker) {
163 return modcmd_linker_command(client, textclient, user, cbind, bind_index, args_ptr);
167 //list all sub commands
168 int commandlen = sprintf(command, "%s ", parent_bind->cmd);
169 char subcommands[MAXLEN];
171 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
172 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmplen(cbind->cmd, command, commandlen) == 0) {
173 if(strstr(cbind->cmd + commandlen, " ")) continue; //ignore if there is another space in the command (sub-sub-command :D)
174 subcompos += sprintf(subcommands + subcompos, (subcompos ? ", %s" : "%s"), cbind->cmd + commandlen);
177 reply(textclient, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "
\1dnone
\1d"));
182 static struct cmd_binding *modcmd_sub_linker_command(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct cmd_binding *cbind, int bind_index, char **args_ptr) {
184 char command[MAXLEN];
185 struct cmd_binding *parent_bind = cbind;
186 char *args = *args_ptr;
189 args = strchr(args, ' ');
194 sprintf(command, "%s %s", parent_bind->cmd, subcmd);
195 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
196 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0)
200 if(cbind && cbind->func->func == modcmd_linker) {
201 return modcmd_linker_command(client, textclient, user, cbind, bind_index, args_ptr);
203 return (cbind ? cbind : parent_bind);
208 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
209 struct ChanNode *sent_chan = chan;
210 if(message[0] == '#') {
211 char *chanName = message;
212 message = strstr(message, " ");
216 struct ChanNode *chan2 = getChanByName(chanName);
220 message = strdup(message);
221 int bind_index = get_binds_index(message[0]);
222 char *args = strchr(message, ' ');
223 char *args_buffer = NULL; //we need this to save a possible pointer to a allocation we need to free
228 struct cmd_binding *cbind;
230 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
231 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, message) == 0) {
234 struct ClientSocket *textclient = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0));
235 if(cbind->func->func == modcmd_linker) {
236 cbind = modcmd_linker_command(client, textclient, user, cbind, bind_index, &args);
237 if(cbind == NULL) break;
238 } else if(cbind->flags & CMDFLAG_SUB_LINKER)
239 cbind = modcmd_sub_linker_command(client, textclient, user, cbind, bind_index, &args);
240 if(statistics_enabled)
241 statistics_commands++;
244 if((BIND_FLAGS(cbind) & CMDFLAG_FUNCMD)) {
249 //parse the arguments...
250 char *arga[MAXNUMPARAMS];
257 //skip leading spaces
261 if (argc >= MAXNUMPARAMS)
263 while ((escape || *args != ' ') && *args) {
264 if((BIND_FLAGS(cbind) & CMDFLAG_ESCAPE_ARGS) && *args == '\\') {
269 if(!escape && offset) {
270 args[0 - offset] = args[0];
276 args[0-offset] = '\0';
282 if(cbind->paramcount) {
283 //userdefined parameters...
284 args_buffer = malloc(MAXLEN * 2 * sizeof(*args_buffer));
286 char *uargs[MAXNUMPARAMS];
291 for(i = 0; i < cbind->paramcount; i++) {
292 b = cbind->parameters[i];
296 while(*c && *c != ' ') {
300 if(!*c || *c == ' ') c = NULL;
305 if(b[strlen(b)-1] == '-') {
306 allargs = strlen(b)-1;
311 } else if(b[strlen(b)-1] == '+') {
312 allargs = strlen(b)-1;
323 uargs[uargc++] = args_buffer + args_pos;
325 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
326 } else if(allargs == 1) {
327 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
328 for(argi++; argi <= argc; argi++) {
329 uargs[uargc++] = args_buffer + args_pos;
330 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
332 } else if(allargs == 2) {
333 for(;argi <= argc; argi++) {
334 args_pos += sprintf(args_buffer + args_pos, (allargs ? "%s" : " %s"), argv[argi-1]);
339 } else if((BIND_FLAGS(cbind) & CMDFLAG_EMPTY_ARGS)) {
340 uargs[uargc++] = args_buffer + args_pos;
341 args_buffer[args_pos++] = '\0';
343 uargs[uargc++] = args_buffer + args_pos;
344 args_pos += sprintf(args_buffer + args_pos, "%s", c) + 1;
346 } else if(!strcmp(b, "c")) {
347 uargs[uargc++] = args_buffer + args_pos;
348 args_pos += sprintf(args_buffer + args_pos, "%s", (chan ? chan->name : "")) + 1;
349 } else if(!strcmp(b, "n")) {
350 uargs[uargc++] = args_buffer + args_pos;
351 args_pos += sprintf(args_buffer + args_pos, "%s", user->nick) + 1;
353 if(c) c[-1] = '|'; //reset \0 to |
355 uargs[uargc++] = args_buffer + args_pos;
356 args_pos += sprintf(args_buffer + args_pos, "%s", b) + 1;
362 if(argc != 0 && argv[0][0] == '#' && !(BIND_FLAGS(cbind) & CMDFLAG_CHAN_PARAM)) {
363 struct ChanNode *chan2 = getChanByName(argv[0]);
370 if(argc < cbind->func->paramcount) {
371 reply(textclient, user, "MODCMD_LESS_PARAM_COUNT");
374 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_CHAN) && !chan) {
375 reply(textclient, user, "MODCMD_CHAN_REQUIRED");
378 if(((BIND_FLAGS(cbind) & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) {
380 struct command_check_user_cache *data = malloc(sizeof(*data));
381 char **temp_argv = malloc(argc*sizeof(*temp_argv));
382 if (!data || !temp_argv) {
383 perror("malloc() failed");
386 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
387 data->argv = temp_argv;
389 data->client = client;
392 data->sent_chan = sent_chan;
393 data->message = message;
394 data->args_buffer = args_buffer;
396 data->textclient = textclient;
397 get_userauth(user, 0, command_checked_auth, data);
400 handle_command_async(client, textclient, user, chan, sent_chan, cbind, argv, argc);
404 if(!found_cmd && !sent_chan && !(client->flags & SOCKET_FLAG_SILENT))
405 reply(get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0)), user, "MODCMD_UNKNOWN", message);
411 static void handle_command_async(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct ChanNode *sent_chan, struct cmd_binding *cbind, char **argv, int argc) {
415 char requested_uaccess = 0;
416 int eventflags = (BIND_FLAGS(cbind) & (CMDFLAG_LOG | CMDFLAG_OPLOG));
417 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
418 reply(textclient, user, "MODCMD_AUTH_REQUIRED");
421 if(chan && sent_chan != chan && (BIND_FLAGS(cbind) & CMDFLAG_NO_CROSSCHAN) && !isUserOnChan(user, chan)) {
422 char user_in_chan = 0;
423 if((user->flags & USERFLAG_ISAUTHED)) {
424 //maybe there's another user authed to user->auth on the channel...
425 struct ChanUser *cchanuser;
426 for(cchanuser = getChannelUsers(chan, NULL); cchanuser; cchanuser = getChannelUsers(chan, cchanuser)) {
427 if((cchanuser->user->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cchanuser->user->auth)) {
434 //check if we are allowed to execute commands in this channel
435 requested_uaccess = 1;
436 uaccess = getChannelAccess(user, chan);
438 if(isGodMode(user)) {
439 eventflags |= CMDFLAG_OPLOG;
441 reply(textclient, user, "MODCMD_CROSSCHAN", chan->name);
447 if(sent_chan && sent_chan != chan) {
448 //check pubcmd of this channel
449 printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
451 if ((row = mysql_fetch_row(res)) != NULL) {
452 int saccess = getChannelAccess(user, sent_chan);
453 if(row[0] && saccess < atoi(row[0]) && !isGodMode(user)) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
454 reply(textclient, user, "MODCMD_PUBCMD", sent_chan->name);
459 int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
460 if(global_access > 0) {
461 int user_global_access = 0;
462 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
464 if ((row = mysql_fetch_row(res)) != NULL) {
465 user_global_access = atoi(row[0]);
467 if(user_global_access < global_access) {
468 if(!user_global_access)
469 reply(textclient, user, "MODCMD_PRIVILEGED", cbind->cmd);
471 reply(textclient, user, "MODCMD_ACCESS_DENIED");
475 if((BIND_FLAGS(cbind) & CMDFLAG_REGISTERED_CHAN)) {
476 MYSQL_ROW defaults = NULL;
477 char access_list[256];
479 int access_count = 0;
481 char *str_a, *str_b = cbind->func->channel_access, *str_c;
482 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
483 str_b = cbind->channel_access;
484 access_list[0] = '\0';
486 struct ChanUser *chanuser = getChanUser(user, chan);
487 str_c = strdup(str_b);
489 while((str_a = str_b)) {
490 str_b = strstr(str_a, ",");
495 if(*str_a == '@' || *str_a == '+') {
496 //privs can override this access requirement
498 if(*str_a == '@') priv = CHANUSERFLAG_OPPED;
499 else if(*str_a == '%') priv = CHANUSERFLAG_HALFOPPED;
500 else if(*str_a == '+') priv = CHANUSERFLAG_VOICED;
501 if(chanuser && (chanuser->flags & priv)) continue;
506 access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
509 if(atoi(str_a) > minaccess)
510 minaccess = atoi(str_a);
515 if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
516 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
518 if ((row = mysql_fetch_row(res)) != NULL) {
519 chan->flags |= CHANFLAG_CHAN_REGISTERED;
520 chan->channel_id = atoi(row[0]);
521 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
522 if(!requested_uaccess) uaccess = getChannelAccess(user, chan);
523 if(uaccess < minaccess && isGodMode(user)) {
524 eventflags |= CMDFLAG_OPLOG;
525 } else if(uaccess < minaccess) {
527 reply(textclient, user, "MODCMD_ACCESS_DENIED");
530 if(!row[1] && !defaults) {
531 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
532 defaults = mysql_fetch_row(mysql_use());
534 if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
535 if(isGodMode(user)) {
536 eventflags |= CMDFLAG_OPLOG;
539 reply(textclient, user, "MODCMD_PUBCMD", chan->name);
544 for(i = 0; i < access_count; i++) {
545 if(!row[2+i] && !defaults) {
546 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
547 defaults = mysql_fetch_row(mysql_use());
549 if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
550 if(isGodMode(user)) {
551 eventflags |= CMDFLAG_OPLOG;
553 reply(textclient, user, "MODCMD_ACCESS_DENIED");
560 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
562 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
563 reply(textclient, user, "MODCMD_CHAN_REQUIRED");
566 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);
568 if ((row = mysql_fetch_row(res)) == NULL) {
569 reply(textclient, user, "MODCMD_CHAN_REQUIRED");
571 } else if(!strcmp(row[1], "1")) {
572 reply(textclient, user, "MODCMD_CHAN_SUSPENDED");
576 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
577 reply(textclient, user, "MODCMD_PRIVILEGED", cbind->cmd);
580 struct Event *event = createEvent(client, user, chan, cbind, argv, argc, eventflags);
581 cbind->func->func(client, textclient, user, chan, argv, argc, event);
584 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
587 struct ClientSocket *client;
591 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
592 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && ((client->botid == 0 && !FD_ISSET(client->clientid, &fds)) || (client->botid && !FD_ISSET(client->botid, &fds2)))) {
593 FD_SET(client->clientid, &fds);
594 FD_SET(client->botid, &fds2);
595 trigger = get_channel_trigger(client->botid, client->clientid, chan);
596 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
597 handle_command(client, user, chan, message + strlen(trigger));
598 goto got_chanmsg_loop1; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
603 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
604 if(isUserOnChan(client->user, chan) && ((client->botid == 0 && !FD_ISSET(client->clientid, &fds)) || (client->botid && !FD_ISSET(client->botid, &fds2)))) {
605 FD_SET(client->clientid, &fds);
606 FD_SET(client->botid, &fds2);
607 trigger = get_channel_trigger(client->botid, client->clientid, chan);
608 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
609 handle_command(client, user, chan, message + strlen(trigger));
610 goto got_chanmsg_loop2; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
616 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
617 struct ClientSocket *client;
618 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
619 if(client->user == target) {
620 handle_command(client, user, NULL, message);
625 int register_command(int botid, char *name, int module_id, cmd_bind_t *func, int paramcount, char *channel_access, int global_access, unsigned int flags) {
626 struct cmd_function *cmdfunc;
627 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
628 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && !stricmp(cmdfunc->name, name))
631 cmdfunc = malloc(sizeof(*cmdfunc));
633 perror("malloc() failed");
636 cmdfunc->botid = botid;
637 cmdfunc->name = strdup(name);
638 cmdfunc->module_id = module_id;
639 cmdfunc->func = func;
640 cmdfunc->flags = flags;
641 cmdfunc->paramcount = paramcount;
642 cmdfunc->channel_access = channel_access;
643 cmdfunc->global_access = global_access;
644 cmdfunc->next = cmd_functions;
645 cmd_functions = cmdfunc;
649 int set_trigger_callback(int botid, int module_id, trigger_callback_t *func) {
650 static struct trigger_callback *cb = NULL;
651 for(cb = trigger_callbacks; cb; cb = cb->next) {
652 if(cb->botid == botid)
656 cb = malloc(sizeof(*cb));
658 perror("malloc() failed");
662 cb->next = trigger_callbacks;
663 trigger_callbacks = cb;
665 cb->module_id = module_id;
670 int flush_trigger_cache(int botid, int clientid) {
671 struct ChanNode *chan;
672 struct trigger_cache *trigger, *last;
673 for(chan = getAllChans(NULL); chan; chan = getAllChans(chan)) {
675 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
676 if(trigger->botid == botid && (botid || trigger->clientid == clientid)) {
678 last->next = trigger->next;
680 chan->trigger = trigger->next;
681 free(trigger->trigger);
691 int changeBotwiseChannelTrigger(int botid, int clientid, struct ChanNode *chan, char *new_trigger) {
692 struct trigger_cache *trigger;
693 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
694 if(trigger->botid == botid && (botid || trigger->clientid == clientid)) {
695 free(trigger->trigger);
696 trigger->trigger = strdup(new_trigger);
703 int bind_botwise_cmd_to_function(int botid, int clientid, char *cmd, struct cmd_function *func) {
704 int bind_index = get_binds_index(cmd[0]);
705 struct cmd_binding *cbind;
706 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
707 if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && !stricmp(cbind->cmd, cmd))
710 cbind = malloc(sizeof(*cbind));
712 perror("malloc() failed");
715 cbind->botid = botid;
716 cbind->clientid = clientid;
717 cbind->cmd = strdup(cmd);
719 cbind->paramcount = 0;
720 cbind->global_access = 0;
721 cbind->channel_access = NULL;
723 cbind->triggered = 0;
724 cbind->next = cmd_binds[bind_index];
725 cmd_binds[bind_index] = cbind;
729 int bind_botwise_cmd_to_command(int botid, int clientid, char *cmd, char *func) {
730 struct cmd_function *cmdfunc;
733 if((c = strstr(func, "."))) {
735 struct cmd_bot_alias *botalias;
736 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
737 if(!stricmp(botalias->alias, func)) {
738 fbotid = botalias->botid;
745 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
746 if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && !stricmp(cmdfunc->name, func))
749 if(!cmdfunc) return 0;
750 int bind_index = get_binds_index(cmd[0]);
751 struct cmd_binding *cbind;
752 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
753 if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && !stricmp(cbind->cmd, cmd))
756 cbind = malloc(sizeof(*cbind));
758 perror("malloc() failed");
761 cbind->botid = botid;
762 cbind->clientid = clientid;
763 cbind->cmd = strdup(cmd);
764 cbind->func = cmdfunc;
765 cbind->next = cmd_binds[bind_index];
766 cbind->paramcount = 0;
767 cbind->global_access = 0;
768 cbind->channel_access = NULL;
770 cbind->triggered = 0;
771 cmd_binds[bind_index] = cbind;
775 int unbind_botwise_cmd(int botid, int clientid, char *cmd) {
776 int bind_index = get_binds_index(cmd[0]);
777 struct cmd_binding *cbind, *last = NULL;
778 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
779 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
781 last->next = cbind->next;
783 cmd_binds[bind_index] = cbind->next;
785 if(cbind->paramcount) {
787 for(i = 0; i < cbind->paramcount; i++)
788 free(cbind->parameters[i]);
790 if(cbind->channel_access)
791 free(cbind->channel_access);
800 int unbind_botwise_allcmd(int botid, int clientid) {
802 for(i = 0; i < 27; i++) {
803 struct cmd_binding *cbind, *next, *last = NULL;
804 for(cbind = cmd_binds[i]; cbind; cbind = next) {
806 if((botid == 0 && clientid == cbind->clientid) || (botid && botid == cbind->botid)) {
808 last->next = cbind->next;
810 cmd_binds[i] = cbind->next;
812 if(cbind->paramcount) {
814 for(j = 0; j < cbind->paramcount; j++)
815 free(cbind->parameters[j]);
817 if(cbind->channel_access)
818 free(cbind->channel_access);
827 struct cmd_function *find_cmd_function(int botid, char *name) {
828 struct cmd_function *cmdfunc;
830 if((c = strstr(name, "."))) {
832 struct cmd_bot_alias *botalias;
833 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
834 if(!stricmp(botalias->alias, name)) {
835 botid = botalias->botid;
842 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
843 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && stricmp(cmdfunc->name, name) == 0)
850 cmd_binds = calloc(27, sizeof(*cmd_binds));
851 bind_chanmsg(got_chanmsg, 0);
852 bind_privmsg(got_privmsg, 0);
853 register_default_language_table(msgtab);
854 register_command(0, "linker", 0, modcmd_linker, 0, 0, 0, 0); //fake command for subcommands
859 for(i = 0; i < 27; i++) {
860 struct cmd_binding *cbind, *next;
861 for(cbind = cmd_binds[i]; cbind; cbind = next) {
864 if(cbind->paramcount) {
866 for(j = 0; j < cbind->paramcount; j++)
867 free(cbind->parameters[j]);
869 if(cbind->channel_access)
870 free(cbind->channel_access);
875 struct cmd_function *cmdfunct, *next;
876 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
877 next = cmdfunct->next;
878 free(cmdfunct->name);
881 struct trigger_callback *cb, *next_cb;
882 for(cb = trigger_callbacks; cb; cb = next_cb) {
886 struct cmd_bot_alias *botalias, *next_botalias;
887 for(botalias = bot_aliases; botalias; botalias = next_botalias) {
888 next_botalias = botalias->next;
889 free(botalias->alias);
892 cmd_functions = NULL;
893 trigger_callbacks = NULL;
897 void bind_botwise_set_parameters(int botid, int clientid, char *cmd, char *parameters) {
898 int bind_index = get_binds_index(cmd[0]);
899 struct cmd_binding *cbind;
900 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
901 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
902 if(cbind->paramcount) {
904 for(i = 0; i < cbind->paramcount; i++)
905 free(cbind->parameters[i]);
906 cbind->paramcount = 0;
909 char *a, *b = parameters;
913 cbind->parameters[cbind->paramcount++] = strdup(b);
922 void bind_botwise_set_global_access(int botid, int clientid, char *cmd, int gaccess) {
923 int bind_index = get_binds_index(cmd[0]);
924 struct cmd_binding *cbind;
925 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
926 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
928 cbind->global_access = gaccess;
929 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
931 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
938 void bind_botwise_set_channel_access(int botid, int clientid, char *cmd, char *chanaccess) {
939 int bind_index = get_binds_index(cmd[0]);
940 struct cmd_binding *cbind;
941 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
942 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
943 if(cbind->channel_access)
944 free(cbind->channel_access);
946 cbind->channel_access = strdup(chanaccess);
947 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
949 cbind->channel_access = NULL;
950 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
957 void bind_botwise_set_bind_flags(int botid, int clientid, char *cmd, unsigned int flags) {
958 int bind_index = get_binds_index(cmd[0]);
959 struct cmd_binding *cbind;
960 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
961 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
962 cbind->flags |= flags;
968 struct cmd_binding *find_botwise_cmd_binding(int botid, int clientid, char *cmd) {
969 int bind_index = get_binds_index(cmd[0]);
970 struct cmd_binding *cbind;
971 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
972 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
979 void bind_botwise_unbound_required_functions(int botid, int clientid) {
980 struct cmd_function *cmdfunc;
982 struct cmd_binding *cbind;
983 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
984 if((cmdfunc->flags & CMDFLAG_REQUIRED)) {
986 for(i = 0; i < 27; i++) {
987 for(cbind = cmd_binds[i]; cbind; cbind = cbind->next) {
988 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && cbind->func == cmdfunc) {
996 if(!found && bind_botwise_cmd_to_function(botid, clientid, cmdfunc->name, cmdfunc)) {
997 cbind = find_botwise_cmd_binding(botid, clientid, cmdfunc->name);
998 cbind->flags |= CMDFLAG_TEMPONARY_BIND;
1004 void register_command_alias(int botid, char *alias) {
1005 struct cmd_bot_alias *botalias;
1006 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
1007 if(!stricmp(botalias->alias, alias))
1010 botalias = malloc(sizeof(*botalias));
1012 perror("malloc() failed");
1015 botalias->botid = botid;
1016 botalias->alias = strdup(alias);
1017 botalias->next = bot_aliases;
1018 bot_aliases = botalias;
1021 struct cmd_binding *getAllBinds(struct cmd_binding *last) {
1026 bind_index = get_binds_index(last->cmd[0]) + 1;
1032 if(cmd_binds[bind_index])
1033 return cmd_binds[bind_index];
1035 } while(bind_index < 27);
1039 void unregister_module_commands(int module_id) {
1040 struct cmd_function *cmdfunct, *nextfunct, *prevfunct = NULL;
1042 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = nextfunct) {
1043 nextfunct = cmdfunct->next;
1044 if(cmdfunct->module_id == module_id) {
1045 for(i = 0; i < 27; i++) {
1046 struct cmd_binding *cbind, *next, *last = NULL;
1047 for(cbind = cmd_binds[i]; cbind; cbind = next) {
1049 if(cmdfunct == cbind->func) {
1051 last->next = cbind->next;
1053 cmd_binds[i] = cbind->next;
1055 if(cbind->paramcount) {
1057 for(j = 0; j < cbind->paramcount; j++)
1058 free(cbind->parameters[j]);
1060 if(cbind->channel_access)
1061 free(cbind->channel_access);
1068 prevfunct->next = nextfunct;
1070 cmd_functions = nextfunct;
1071 free(cmdfunct->name);
1074 prevfunct = cmdfunct;
1076 struct trigger_callback *cb, *prevcb = NULL, *nextcb;
1077 for(cb = trigger_callbacks; cb; cb = nextcb) {
1079 if(cb->module_id == module_id) {
1081 prevcb->next = nextcb;
1083 trigger_callbacks = nextcb;