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"
33 struct trigger_callback {
36 trigger_callback_t *func;
38 struct trigger_callback *next;
41 struct cmd_bot_alias {
45 struct cmd_bot_alias *next;
48 struct command_check_user_cache {
49 struct ClientSocket *client, *textclient;
50 struct UserNode *user;
51 struct ChanNode *chan, *sent_chan;
54 char *message, *args_buffer;
55 struct cmd_binding *cbind;
58 static struct cmd_binding **cmd_binds;
59 static struct cmd_function *cmd_functions = NULL;
60 static struct trigger_callback *trigger_callbacks = NULL;
61 static struct cmd_bot_alias *bot_aliases = NULL;
62 static int total_triggered = 0;
63 int statistics_commands = 0;
65 static const struct default_language_entry msgtab[] = {
66 {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
67 {"MODCMD_CHAN_REQUIRED", "You must provide the name of a channel that exists and the bot is on."},
68 {"MODCMD_AUTH_REQUIRED", "You need to be authenticated with AuthServ to use this command."},
69 {"MODCMD_CHAN_SUSPENDED", "This channel is currently suspended."},
70 {"MODCMD_PRIVILEGED", "$b%s$b is a privileged command."}, /* {ARGS: "god"} */
71 {"MODCMD_PUBCMD", "Public commands in $b%s$b are restricted."}, /* {ARGS: "#TestChan"} */
72 {"MODCMD_ACCESS_DENIED", "Access denied."},
73 {"MODCMD_SUBCOMMANDS", "Subcommands of %s: %s"}, /* {ARGS: "bot", "ADD, DEL, EDIT"} */
74 {"MODCMD_CROSSCHAN", "You must be in %s (or on its userlist) to use this command."}, /* {ARGS: "#TestChan"} */
75 {"MODCMD_UNKNOWN", "$b%s$b is an unknown command."}, /* {ARGS: "bla"} */
79 static int get_binds_index(char first_char) {
80 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
81 return tolower(first_char) - 'a';
86 struct ClientSocket* get_botwise_prefered_bot(int botid, int clientid) {
87 struct ClientSocket *client, *lowbot = NULL;
88 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
89 if(client->botid == botid && (botid || clientid == client->clientid)) {
90 if((client->flags & SOCKET_FLAG_PREFERRED))
99 static char* get_channel_trigger(int botid, int clientid, struct ChanNode *chan) {
100 struct trigger_cache *trigger;
101 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
102 if(trigger->botid == botid && (botid || trigger->clientid == clientid))
103 return trigger->trigger;
105 struct trigger_callback *cb;
106 for(cb = trigger_callbacks; cb; cb = cb->next) {
107 if(cb->botid == botid)
110 char triggerStr[TRIGGERLEN];
112 cb->func(clientid, chan, triggerStr);
114 triggerStr[0] = '\0';
115 trigger = malloc(sizeof(*trigger));
117 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
120 trigger->botid = botid;
121 trigger->clientid = clientid;
122 trigger->trigger = (triggerStr[0] ? strdup(triggerStr) : NULL);
123 trigger->next = chan->trigger;
124 chan->trigger = trigger;
125 return trigger->trigger;
128 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);
130 static USERAUTH_CALLBACK(command_checked_auth) {
131 struct command_check_user_cache *cache = data;
133 handle_command_async(cache->client, cache->textclient, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
134 free(cache->message);
135 if(cache->args_buffer)
136 free(cache->args_buffer);
141 static CMD_BIND(modcmd_linker) {
142 //fake command for subcommands
146 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) {
148 char command[MAXLEN];
149 struct cmd_binding *parent_bind = cbind;
150 char *args = *args_ptr;
153 args = strstr(args, " ");
158 sprintf(command, "%s %s", parent_bind->cmd, subcmd);
159 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
160 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0)
164 if(cbind && cbind->func->func == modcmd_linker) {
165 return modcmd_linker_command(client, textclient, user, cbind, bind_index, args_ptr);
169 //list all sub commands
170 int commandlen = sprintf(command, "%s ", parent_bind->cmd);
171 char subcommands[MAXLEN];
173 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
174 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmplen(cbind->cmd, command, commandlen) == 0) {
175 if(strstr(cbind->cmd + commandlen, " ")) continue; //ignore if there is another space in the command (sub-sub-command :D)
176 subcompos += sprintf(subcommands + subcompos, (subcompos ? ", %s" : "%s"), cbind->cmd + commandlen);
179 reply(textclient, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "
\1dnone
\1d"));
184 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) {
186 char command[MAXLEN];
187 struct cmd_binding *parent_bind = cbind;
188 char *args = *args_ptr;
191 args = strchr(args, ' ');
196 sprintf(command, "%s %s", parent_bind->cmd, subcmd);
197 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
198 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0)
202 if(cbind && cbind->func->func == modcmd_linker) {
203 return modcmd_linker_command(client, textclient, user, cbind, bind_index, args_ptr);
205 return (cbind ? cbind : parent_bind);
210 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
211 struct ChanNode *sent_chan = chan;
212 if(message[0] == '#') {
213 char *chanName = message;
214 message = strstr(message, " ");
218 struct ChanNode *chan2 = getChanByName(chanName);
222 message = strdup(message);
223 int bind_index = get_binds_index(message[0]);
224 char *args = strchr(message, ' ');
225 char *args_buffer = NULL; //we need this to save a possible pointer to a allocation we need to free
230 struct cmd_binding *cbind;
232 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
233 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, message) == 0) {
236 struct ClientSocket *textclient = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0));
237 if(cbind->func->func == modcmd_linker) {
238 cbind = modcmd_linker_command(client, textclient, user, cbind, bind_index, &args);
239 if(cbind == NULL) break;
240 } else if(cbind->flags & CMDFLAG_SUB_LINKER)
241 cbind = modcmd_sub_linker_command(client, textclient, user, cbind, bind_index, &args);
242 statistics_commands++;
245 if((BIND_FLAGS(cbind) & CMDFLAG_FUNCMD)) {
250 //parse the arguments...
251 char *arga[MAXNUMPARAMS];
258 //skip leading spaces
262 if (argc >= MAXNUMPARAMS)
264 while ((escape || *args != ' ') && *args) {
265 if((BIND_FLAGS(cbind) & CMDFLAG_ESCAPE_ARGS) && *args == '\\') {
270 if(!escape && offset) {
271 args[0 - offset] = args[0];
277 args[0-offset] = '\0';
283 if(cbind->paramcount) {
284 //userdefined parameters...
285 args_buffer = malloc(MAXLEN * 2 * sizeof(*args_buffer));
287 char *uargs[MAXNUMPARAMS];
292 for(i = 0; i < cbind->paramcount; i++) {
293 b = cbind->parameters[i];
297 while(*c && *c != ' ') {
301 if(!*c || *c == ' ') c = NULL;
306 if(b[strlen(b)-1] == '-') {
307 allargs = strlen(b)-1;
312 } else if(b[strlen(b)-1] == '+') {
313 allargs = strlen(b)-1;
324 uargs[uargc++] = args_buffer + args_pos;
326 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
327 } else if(allargs == 1) {
328 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
329 for(argi++; argi <= argc; argi++) {
330 uargs[uargc++] = args_buffer + args_pos;
331 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
333 } else if(allargs == 2) {
334 for(;argi <= argc; argi++) {
335 args_pos += sprintf(args_buffer + args_pos, (allargs ? "%s" : " %s"), argv[argi-1]);
340 } else if((BIND_FLAGS(cbind) & CMDFLAG_EMPTY_ARGS)) {
341 uargs[uargc++] = args_buffer + args_pos;
342 args_buffer[args_pos++] = '\0';
344 uargs[uargc++] = args_buffer + args_pos;
345 args_pos += sprintf(args_buffer + args_pos, "%s", c) + 1;
347 } else if(!strcmp(b, "c")) {
348 uargs[uargc++] = args_buffer + args_pos;
349 args_pos += sprintf(args_buffer + args_pos, "%s", (chan ? chan->name : "")) + 1;
350 } else if(!strcmp(b, "n")) {
351 uargs[uargc++] = args_buffer + args_pos;
352 args_pos += sprintf(args_buffer + args_pos, "%s", user->nick) + 1;
354 if(c) c[-1] = '|'; //reset \0 to |
356 uargs[uargc++] = args_buffer + args_pos;
357 args_pos += sprintf(args_buffer + args_pos, "%s", b) + 1;
363 if(argc != 0 && argv[0][0] == '#' && !(BIND_FLAGS(cbind) & CMDFLAG_CHAN_PARAM)) {
364 struct ChanNode *chan2 = getChanByName(argv[0]);
371 if(argc < cbind->func->paramcount) {
372 reply(textclient, user, "MODCMD_LESS_PARAM_COUNT");
375 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_CHAN) && !chan) {
376 reply(textclient, user, "MODCMD_CHAN_REQUIRED");
379 if(((BIND_FLAGS(cbind) & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) {
381 struct command_check_user_cache *data = malloc(sizeof(*data));
382 char **temp_argv = malloc(argc*sizeof(*temp_argv));
383 if (!data || !temp_argv) {
384 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
387 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
388 data->argv = temp_argv;
390 data->client = client;
393 data->sent_chan = sent_chan;
394 data->message = message;
395 data->args_buffer = args_buffer;
397 data->textclient = textclient;
398 get_userauth(user, 0, command_checked_auth, data);
401 handle_command_async(client, textclient, user, chan, sent_chan, cbind, argv, argc);
405 if(!found_cmd && !sent_chan && !(client->flags & SOCKET_FLAG_SILENT))
406 reply(get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0)), user, "MODCMD_UNKNOWN", message);
412 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) {
416 char requested_uaccess = 0;
417 int eventflags = (BIND_FLAGS(cbind) & (CMDFLAG_LOG | CMDFLAG_OPLOG));
418 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
419 reply(textclient, user, "MODCMD_AUTH_REQUIRED");
422 if(chan && sent_chan != chan && (BIND_FLAGS(cbind) & CMDFLAG_NO_CROSSCHAN) && !isUserOnChan(user, chan)) {
423 char user_in_chan = 0;
424 if((user->flags & USERFLAG_ISAUTHED)) {
425 //maybe there's another user authed to user->auth on the channel...
426 struct ChanUser *cchanuser;
427 for(cchanuser = getChannelUsers(chan, NULL); cchanuser; cchanuser = getChannelUsers(chan, cchanuser)) {
428 if((cchanuser->user->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cchanuser->user->auth)) {
435 //check if we are allowed to execute commands in this channel
436 requested_uaccess = 1;
437 uaccess = getChannelAccess(user, chan);
439 if(isGodMode(user)) {
440 eventflags |= CMDFLAG_OPLOG;
442 reply(textclient, user, "MODCMD_CROSSCHAN", chan->name);
448 if(sent_chan && sent_chan != chan) {
449 //check pubcmd of this channel
450 printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
452 if ((row = mysql_fetch_row(res)) != NULL) {
453 int saccess = getChannelAccess(user, sent_chan);
454 if(row[0] && saccess < atoi(row[0]) && !isGodMode(user)) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
455 reply(textclient, user, "MODCMD_PUBCMD", sent_chan->name);
460 int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
461 if(global_access > 0) {
462 int user_global_access = 0;
463 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
465 if ((row = mysql_fetch_row(res)) != NULL) {
466 user_global_access = atoi(row[0]);
468 if(user_global_access < global_access) {
469 if(!user_global_access)
470 reply(textclient, user, "MODCMD_PRIVILEGED", cbind->cmd);
472 reply(textclient, user, "MODCMD_ACCESS_DENIED");
476 if((BIND_FLAGS(cbind) & CMDFLAG_REGISTERED_CHAN)) {
477 MYSQL_ROW defaults = NULL;
478 char access_list[256];
480 int access_count = 0;
482 char *str_a, *str_b = cbind->func->channel_access, *str_c;
483 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
484 str_b = cbind->channel_access;
485 access_list[0] = '\0';
487 struct ChanUser *chanuser = getChanUser(user, chan);
488 str_c = strdup(str_b);
490 while((str_a = str_b)) {
491 str_b = strstr(str_a, ",");
496 if(*str_a == '@' || *str_a == '+') {
497 //privs can override this access requirement
499 if(*str_a == '@') priv = CHANUSERFLAG_OPPED;
500 else if(*str_a == '%') priv = CHANUSERFLAG_HALFOPPED;
501 else if(*str_a == '+') priv = CHANUSERFLAG_VOICED;
502 if(chanuser && (chanuser->flags & priv)) continue;
507 access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
510 if(atoi(str_a) > minaccess)
511 minaccess = atoi(str_a);
516 if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
517 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
519 if ((row = mysql_fetch_row(res)) != NULL) {
520 chan->flags |= CHANFLAG_CHAN_REGISTERED;
521 chan->channel_id = atoi(row[0]);
522 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
523 if(!requested_uaccess) uaccess = getChannelAccess(user, chan);
524 if(uaccess < minaccess && isGodMode(user)) {
525 eventflags |= CMDFLAG_OPLOG;
526 } else if(uaccess < minaccess) {
528 reply(textclient, user, "MODCMD_ACCESS_DENIED");
531 if(!row[1] && !defaults) {
532 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
533 defaults = mysql_fetch_row(mysql_use());
535 if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
536 if(isGodMode(user)) {
537 eventflags |= CMDFLAG_OPLOG;
540 reply(textclient, user, "MODCMD_PUBCMD", chan->name);
545 for(i = 0; i < access_count; i++) {
546 if(!row[2+i] && !defaults) {
547 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
548 defaults = mysql_fetch_row(mysql_use());
550 if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
551 if(isGodMode(user)) {
552 eventflags |= CMDFLAG_OPLOG;
554 reply(textclient, user, "MODCMD_ACCESS_DENIED");
561 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
563 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
564 reply(textclient, user, "MODCMD_CHAN_REQUIRED");
567 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);
569 if ((row = mysql_fetch_row(res)) == NULL) {
570 reply(textclient, user, "MODCMD_CHAN_REQUIRED");
572 } else if(!strcmp(row[1], "1")) {
573 reply(textclient, user, "MODCMD_CHAN_SUSPENDED");
577 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
578 reply(textclient, user, "MODCMD_PRIVILEGED", cbind->cmd);
581 struct Event *event = createEvent(client, user, chan, cbind, argv, argc, eventflags);
582 cbind->func->func(client, textclient, user, chan, argv, argc, event);
585 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
588 struct ClientSocket *client;
592 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
593 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)))) {
594 FD_SET(client->clientid, &fds);
595 FD_SET(client->botid, &fds2);
596 trigger = get_channel_trigger(client->botid, client->clientid, chan);
597 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
598 handle_command(client, user, chan, message + strlen(trigger));
599 goto got_chanmsg_loop1; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
604 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
605 if(isUserOnChan(client->user, chan) && ((client->botid == 0 && !FD_ISSET(client->clientid, &fds)) || (client->botid && !FD_ISSET(client->botid, &fds2)))) {
606 FD_SET(client->clientid, &fds);
607 FD_SET(client->botid, &fds2);
608 trigger = get_channel_trigger(client->botid, client->clientid, chan);
609 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
610 handle_command(client, user, chan, message + strlen(trigger));
611 goto got_chanmsg_loop2; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
617 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
618 struct ClientSocket *client;
619 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
620 if(client->user == target) {
621 handle_command(client, user, NULL, message);
626 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) {
627 struct cmd_function *cmdfunc;
628 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
629 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && !stricmp(cmdfunc->name, name))
632 cmdfunc = malloc(sizeof(*cmdfunc));
634 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
637 cmdfunc->botid = botid;
638 cmdfunc->name = strdup(name);
639 cmdfunc->module_id = module_id;
640 cmdfunc->func = func;
641 cmdfunc->flags = flags;
642 cmdfunc->paramcount = paramcount;
643 cmdfunc->channel_access = channel_access;
644 cmdfunc->global_access = global_access;
645 cmdfunc->next = cmd_functions;
646 cmd_functions = cmdfunc;
650 int set_trigger_callback(int botid, int module_id, trigger_callback_t *func) {
651 static struct trigger_callback *cb = NULL;
652 for(cb = trigger_callbacks; cb; cb = cb->next) {
653 if(cb->botid == botid)
657 cb = malloc(sizeof(*cb));
659 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
663 cb->next = trigger_callbacks;
664 trigger_callbacks = cb;
666 cb->module_id = module_id;
671 int flush_trigger_cache(int botid, int clientid) {
672 struct ChanNode *chan;
673 struct trigger_cache *trigger, *last;
674 for(chan = getAllChans(NULL); chan; chan = getAllChans(chan)) {
676 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
677 if(trigger->botid == botid && (botid || trigger->clientid == clientid)) {
679 last->next = trigger->next;
681 chan->trigger = trigger->next;
682 free(trigger->trigger);
692 int changeBotwiseChannelTrigger(int botid, int clientid, struct ChanNode *chan, char *new_trigger) {
693 struct trigger_cache *trigger;
694 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
695 if(trigger->botid == botid && (botid || trigger->clientid == clientid)) {
696 free(trigger->trigger);
697 trigger->trigger = strdup(new_trigger);
704 int bind_botwise_cmd_to_function(int botid, int clientid, char *cmd, struct cmd_function *func) {
705 int bind_index = get_binds_index(cmd[0]);
706 struct cmd_binding *cbind;
707 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
708 if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && !stricmp(cbind->cmd, cmd))
711 cbind = malloc(sizeof(*cbind));
713 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
716 cbind->botid = botid;
717 cbind->clientid = clientid;
718 cbind->cmd = strdup(cmd);
720 cbind->paramcount = 0;
721 cbind->global_access = 0;
722 cbind->channel_access = NULL;
724 cbind->triggered = 0;
725 cbind->next = cmd_binds[bind_index];
726 cmd_binds[bind_index] = cbind;
730 int bind_botwise_cmd_to_command(int botid, int clientid, char *cmd, char *func) {
731 struct cmd_function *cmdfunc;
734 if((c = strstr(func, "."))) {
736 struct cmd_bot_alias *botalias;
737 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
738 if(!stricmp(botalias->alias, func)) {
739 fbotid = botalias->botid;
746 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
747 if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && !stricmp(cmdfunc->name, func))
750 if(!cmdfunc) return 0;
751 int bind_index = get_binds_index(cmd[0]);
752 struct cmd_binding *cbind;
753 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
754 if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && !stricmp(cbind->cmd, cmd))
757 cbind = malloc(sizeof(*cbind));
759 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
762 cbind->botid = botid;
763 cbind->clientid = clientid;
764 cbind->cmd = strdup(cmd);
765 cbind->func = cmdfunc;
766 cbind->next = cmd_binds[bind_index];
767 cbind->paramcount = 0;
768 cbind->global_access = 0;
769 cbind->channel_access = NULL;
771 cbind->triggered = 0;
772 cmd_binds[bind_index] = cbind;
776 int unbind_botwise_cmd(int botid, int clientid, char *cmd) {
777 int bind_index = get_binds_index(cmd[0]);
778 struct cmd_binding *cbind, *last = NULL;
779 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
780 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
782 last->next = cbind->next;
784 cmd_binds[bind_index] = cbind->next;
786 if(cbind->paramcount) {
788 for(i = 0; i < cbind->paramcount; i++)
789 free(cbind->parameters[i]);
791 if(cbind->channel_access)
792 free(cbind->channel_access);
801 int unbind_botwise_allcmd(int botid, int clientid) {
803 for(i = 0; i < 27; i++) {
804 struct cmd_binding *cbind, *next, *last = NULL;
805 for(cbind = cmd_binds[i]; cbind; cbind = next) {
807 if((botid == 0 && clientid == cbind->clientid) || (botid && botid == cbind->botid)) {
809 last->next = cbind->next;
811 cmd_binds[i] = cbind->next;
813 if(cbind->paramcount) {
815 for(j = 0; j < cbind->paramcount; j++)
816 free(cbind->parameters[j]);
818 if(cbind->channel_access)
819 free(cbind->channel_access);
828 struct cmd_function *find_cmd_function(int botid, char *name) {
829 struct cmd_function *cmdfunc;
831 if((c = strstr(name, "."))) {
833 struct cmd_bot_alias *botalias;
834 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
835 if(!stricmp(botalias->alias, name)) {
836 botid = botalias->botid;
843 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
844 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && stricmp(cmdfunc->name, name) == 0)
851 cmd_binds = calloc(27, sizeof(*cmd_binds));
852 bind_chanmsg(got_chanmsg, 0);
853 bind_privmsg(got_privmsg, 0);
854 register_default_language_table(msgtab);
855 register_command(0, "linker", 0, modcmd_linker, 0, 0, 0, 0); //fake command for subcommands
860 for(i = 0; i < 27; i++) {
861 struct cmd_binding *cbind, *next;
862 for(cbind = cmd_binds[i]; cbind; cbind = next) {
865 if(cbind->paramcount) {
867 for(j = 0; j < cbind->paramcount; j++)
868 free(cbind->parameters[j]);
870 if(cbind->channel_access)
871 free(cbind->channel_access);
876 struct cmd_function *cmdfunct, *next;
877 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
878 next = cmdfunct->next;
879 free(cmdfunct->name);
882 struct trigger_callback *cb, *next_cb;
883 for(cb = trigger_callbacks; cb; cb = next_cb) {
887 struct cmd_bot_alias *botalias, *next_botalias;
888 for(botalias = bot_aliases; botalias; botalias = next_botalias) {
889 next_botalias = botalias->next;
890 free(botalias->alias);
893 cmd_functions = NULL;
894 trigger_callbacks = NULL;
898 void bind_botwise_set_parameters(int botid, int clientid, char *cmd, char *parameters) {
899 int bind_index = get_binds_index(cmd[0]);
900 struct cmd_binding *cbind;
901 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
902 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
903 if(cbind->paramcount) {
905 for(i = 0; i < cbind->paramcount; i++)
906 free(cbind->parameters[i]);
907 cbind->paramcount = 0;
910 char *a, *b = parameters;
914 cbind->parameters[cbind->paramcount++] = strdup(b);
923 void bind_botwise_set_global_access(int botid, int clientid, char *cmd, int gaccess) {
924 int bind_index = get_binds_index(cmd[0]);
925 struct cmd_binding *cbind;
926 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
927 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
929 cbind->global_access = gaccess;
930 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
932 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
939 void bind_botwise_set_channel_access(int botid, int clientid, char *cmd, char *chanaccess) {
940 int bind_index = get_binds_index(cmd[0]);
941 struct cmd_binding *cbind;
942 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
943 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
944 if(cbind->channel_access)
945 free(cbind->channel_access);
947 cbind->channel_access = strdup(chanaccess);
948 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
950 cbind->channel_access = NULL;
951 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
958 void bind_botwise_set_bind_flags(int botid, int clientid, char *cmd, unsigned int flags) {
959 int bind_index = get_binds_index(cmd[0]);
960 struct cmd_binding *cbind;
961 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
962 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
963 cbind->flags |= flags;
969 struct cmd_binding *find_botwise_cmd_binding(int botid, int clientid, char *cmd) {
970 int bind_index = get_binds_index(cmd[0]);
971 struct cmd_binding *cbind;
972 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
973 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
980 void bind_botwise_unbound_required_functions(int botid, int clientid) {
981 struct cmd_function *cmdfunc;
983 struct cmd_binding *cbind;
984 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
985 if((cmdfunc->flags & CMDFLAG_REQUIRED)) {
987 for(i = 0; i < 27; i++) {
988 for(cbind = cmd_binds[i]; cbind; cbind = cbind->next) {
989 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && cbind->func == cmdfunc) {
997 if(!found && bind_botwise_cmd_to_function(botid, clientid, cmdfunc->name, cmdfunc)) {
998 cbind = find_botwise_cmd_binding(botid, clientid, cmdfunc->name);
999 cbind->flags |= CMDFLAG_TEMPONARY_BIND;
1005 void register_command_alias(int botid, char *alias) {
1006 struct cmd_bot_alias *botalias;
1007 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
1008 if(!stricmp(botalias->alias, alias))
1011 botalias = malloc(sizeof(*botalias));
1013 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
1016 botalias->botid = botid;
1017 botalias->alias = strdup(alias);
1018 botalias->next = bot_aliases;
1019 bot_aliases = botalias;
1022 struct cmd_binding *getAllBinds(struct cmd_binding *last) {
1027 bind_index = get_binds_index(last->cmd[0]) + 1;
1033 if(cmd_binds[bind_index])
1034 return cmd_binds[bind_index];
1036 } while(bind_index < 27);
1040 void unregister_module_commands(int module_id) {
1041 struct cmd_function *cmdfunct, *nextfunct, *prevfunct = NULL;
1043 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = nextfunct) {
1044 nextfunct = cmdfunct->next;
1045 if(cmdfunct->module_id == module_id) {
1046 for(i = 0; i < 27; i++) {
1047 struct cmd_binding *cbind, *next, *last = NULL;
1048 for(cbind = cmd_binds[i]; cbind; cbind = next) {
1050 if(cmdfunct == cbind->func) {
1052 last->next = cbind->next;
1054 cmd_binds[i] = cbind->next;
1056 if(cbind->paramcount) {
1058 for(j = 0; j < cbind->paramcount; j++)
1059 free(cbind->parameters[j]);
1061 if(cbind->channel_access)
1062 free(cbind->channel_access);
1069 prevfunct->next = nextfunct;
1071 cmd_functions = nextfunct;
1072 free(cmdfunct->name);
1075 prevfunct = cmdfunct;
1077 struct trigger_callback *cb, *prevcb = NULL, *nextcb;
1078 for(cb = trigger_callbacks; cb; cb = nextcb) {
1080 if(cb->module_id == module_id) {
1082 prevcb->next = nextcb;
1084 trigger_callbacks = nextcb;