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);
199 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
200 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0)
205 if(cbind->func->func == modcmd_linker)
206 parent_bind = modcmd_linker_command(client, textclient, user, cbind, bind_index, args_ptr);
214 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
215 struct ChanNode *sent_chan = chan;
216 if(message[0] == '#') {
217 char *chanName = message;
218 message = strstr(message, " ");
222 struct ChanNode *chan2 = getChanByName(chanName);
226 message = strdup(message);
227 int bind_index = get_binds_index(message[0]);
228 char *args = strchr(message, ' ');
229 char *args_buffer = NULL; //we need this to save a possible pointer to a allocation we need to free
234 struct cmd_binding *cbind;
236 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
237 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, message) == 0) {
240 struct ClientSocket *textclient = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0));
241 if(cbind->func->func == modcmd_linker) {
242 cbind = modcmd_linker_command(client, textclient, user, cbind, bind_index, &args);
243 if(cbind == NULL) break;
244 } else if(cbind->flags & CMDFLAG_SUB_LINKER)
245 cbind = modcmd_sub_linker_command(client, textclient, user, cbind, bind_index, &args);
246 statistics_commands++;
249 if((BIND_FLAGS(cbind) & CMDFLAG_FUNCMD)) {
254 //parse the arguments...
255 char *arga[MAXNUMPARAMS];
262 //skip leading spaces
266 if (argc >= MAXNUMPARAMS)
268 while ((escape || *args != ' ') && *args) {
269 if((BIND_FLAGS(cbind) & CMDFLAG_ESCAPE_ARGS) && *args == '\\') {
274 if(!escape && offset) {
275 args[0 - offset] = args[0];
281 args[0-offset] = '\0';
287 if(cbind->paramcount) {
288 //userdefined parameters...
289 args_buffer = malloc(MAXLEN * 2 * sizeof(*args_buffer));
291 char *uargs[MAXNUMPARAMS];
296 for(i = 0; i < cbind->paramcount; i++) {
297 b = cbind->parameters[i];
301 while(*c && *c != ' ') {
305 if(!*c || *c == ' ') c = NULL;
310 if(b[strlen(b)-1] == '-') {
311 allargs = strlen(b)-1;
316 } else if(b[strlen(b)-1] == '+') {
317 allargs = strlen(b)-1;
328 uargs[uargc++] = args_buffer + args_pos;
330 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
331 } else if(allargs == 1) {
332 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
333 for(argi++; argi <= argc; argi++) {
334 uargs[uargc++] = args_buffer + args_pos;
335 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
337 } else if(allargs == 2) {
338 for(;argi <= argc; argi++) {
339 args_pos += sprintf(args_buffer + args_pos, (allargs ? "%s" : " %s"), argv[argi-1]);
344 } else if((BIND_FLAGS(cbind) & CMDFLAG_EMPTY_ARGS)) {
345 uargs[uargc++] = args_buffer + args_pos;
346 args_buffer[args_pos++] = '\0';
348 uargs[uargc++] = args_buffer + args_pos;
349 args_pos += sprintf(args_buffer + args_pos, "%s", c) + 1;
351 } else if(!strcmp(b, "c")) {
352 uargs[uargc++] = args_buffer + args_pos;
353 args_pos += sprintf(args_buffer + args_pos, "%s", (chan ? chan->name : "")) + 1;
354 } else if(!strcmp(b, "n")) {
355 uargs[uargc++] = args_buffer + args_pos;
356 args_pos += sprintf(args_buffer + args_pos, "%s", user->nick) + 1;
358 if(c) c[-1] = '|'; //reset \0 to |
360 uargs[uargc++] = args_buffer + args_pos;
361 args_pos += sprintf(args_buffer + args_pos, "%s", b) + 1;
367 if(argc != 0 && argv[0][0] == '#' && !(BIND_FLAGS(cbind) & CMDFLAG_CHAN_PARAM)) {
368 struct ChanNode *chan2 = getChanByName(argv[0]);
375 if(argc < cbind->func->paramcount) {
376 reply(textclient, user, "MODCMD_LESS_PARAM_COUNT");
379 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_CHAN) && !chan) {
380 reply(textclient, user, "MODCMD_CHAN_REQUIRED");
383 if(((BIND_FLAGS(cbind) & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) {
385 struct command_check_user_cache *data = malloc(sizeof(*data));
386 char **temp_argv = malloc(argc*sizeof(*temp_argv));
387 if (!data || !temp_argv) {
388 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
391 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
392 data->argv = temp_argv;
394 data->client = client;
397 data->sent_chan = sent_chan;
398 data->message = message;
399 data->args_buffer = args_buffer;
401 data->textclient = textclient;
402 get_userauth(user, 0, command_checked_auth, data);
405 handle_command_async(client, textclient, user, chan, sent_chan, cbind, argv, argc);
409 if(!found_cmd && !sent_chan && !(client->flags & SOCKET_FLAG_SILENT))
410 reply(get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0)), user, "MODCMD_UNKNOWN", message);
416 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) {
420 char requested_uaccess = 0;
421 int eventflags = (BIND_FLAGS(cbind) & (CMDFLAG_LOG | CMDFLAG_OPLOG));
422 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
423 reply(textclient, user, "MODCMD_AUTH_REQUIRED");
426 if(chan && sent_chan != chan && (BIND_FLAGS(cbind) & CMDFLAG_NO_CROSSCHAN) && !isUserOnChan(user, chan)) {
427 char user_in_chan = 0;
428 if((user->flags & USERFLAG_ISAUTHED)) {
429 //maybe there's another user authed to user->auth on the channel...
430 struct ChanUser *cchanuser;
431 for(cchanuser = getChannelUsers(chan, NULL); cchanuser; cchanuser = getChannelUsers(chan, cchanuser)) {
432 if((cchanuser->user->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cchanuser->user->auth)) {
439 //check if we are allowed to execute commands in this channel
440 requested_uaccess = 1;
441 uaccess = getChannelAccess(user, chan);
443 if(isGodMode(user)) {
444 eventflags |= CMDFLAG_OPLOG;
446 reply(textclient, user, "MODCMD_CROSSCHAN", chan->name);
452 if(sent_chan && sent_chan != chan) {
453 //check pubcmd of this channel
454 printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
456 if ((row = mysql_fetch_row(res)) != NULL) {
457 int saccess = getChannelAccess(user, sent_chan);
458 if(row[0] && saccess < atoi(row[0]) && !isGodMode(user)) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
459 reply(textclient, user, "MODCMD_PUBCMD", sent_chan->name);
464 int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
465 if(global_access > 0) {
466 int user_global_access = 0;
467 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
469 if ((row = mysql_fetch_row(res)) != NULL) {
470 user_global_access = atoi(row[0]);
472 if(user_global_access < global_access) {
473 if(!user_global_access)
474 reply(textclient, user, "MODCMD_PRIVILEGED", cbind->cmd);
476 reply(textclient, user, "MODCMD_ACCESS_DENIED");
480 if((BIND_FLAGS(cbind) & CMDFLAG_REGISTERED_CHAN)) {
481 MYSQL_ROW defaults = NULL;
482 char access_list[256];
484 int access_count = 0;
486 char *str_a, *str_b = cbind->func->channel_access, *str_c;
487 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
488 str_b = cbind->channel_access;
489 access_list[0] = '\0';
491 struct ChanUser *chanuser = getChanUser(user, chan);
492 str_c = strdup(str_b);
494 while((str_a = str_b)) {
495 str_b = strstr(str_a, ",");
500 if(*str_a == '@' || *str_a == '+') {
501 //privs can override this access requirement
503 if(*str_a == '@') priv = CHANUSERFLAG_OPPED;
504 else if(*str_a == '%') priv = CHANUSERFLAG_HALFOPPED;
505 else if(*str_a == '+') priv = CHANUSERFLAG_VOICED;
506 if(chanuser && (chanuser->flags & priv)) continue;
511 access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
514 if(atoi(str_a) > minaccess)
515 minaccess = atoi(str_a);
520 if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
521 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
523 if ((row = mysql_fetch_row(res)) != NULL) {
524 chan->flags |= CHANFLAG_CHAN_REGISTERED;
525 chan->channel_id = atoi(row[0]);
526 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
527 if(!requested_uaccess) uaccess = getChannelAccess(user, chan);
528 if(uaccess < minaccess && isGodMode(user)) {
529 eventflags |= CMDFLAG_OPLOG;
530 } else if(uaccess < minaccess) {
532 reply(textclient, user, "MODCMD_ACCESS_DENIED");
535 if(!row[1] && !defaults) {
536 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
537 defaults = mysql_fetch_row(mysql_use());
539 if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
540 if(isGodMode(user)) {
541 eventflags |= CMDFLAG_OPLOG;
544 reply(textclient, user, "MODCMD_PUBCMD", chan->name);
549 for(i = 0; i < access_count; i++) {
550 if(!row[2+i] && !defaults) {
551 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
552 defaults = mysql_fetch_row(mysql_use());
554 if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
555 if(isGodMode(user)) {
556 eventflags |= CMDFLAG_OPLOG;
558 reply(textclient, user, "MODCMD_ACCESS_DENIED");
565 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
567 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
568 reply(textclient, user, "MODCMD_CHAN_REQUIRED");
571 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);
573 if ((row = mysql_fetch_row(res)) == NULL) {
574 reply(textclient, user, "MODCMD_CHAN_REQUIRED");
576 } else if(!strcmp(row[1], "1")) {
577 reply(textclient, user, "MODCMD_CHAN_SUSPENDED");
581 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
582 reply(textclient, user, "MODCMD_PRIVILEGED", cbind->cmd);
585 struct Event *event = createEvent(client, user, chan, cbind, argv, argc, eventflags);
586 cbind->func->func(client, textclient, user, chan, argv, argc, event);
589 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
592 struct ClientSocket *client;
596 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
597 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)))) {
598 FD_SET(client->clientid, &fds);
599 FD_SET(client->botid, &fds2);
600 trigger = get_channel_trigger(client->botid, client->clientid, chan);
601 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
602 handle_command(client, user, chan, message + strlen(trigger));
603 goto got_chanmsg_loop1; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
608 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
609 if(isUserOnChan(client->user, chan) && ((client->botid == 0 && !FD_ISSET(client->clientid, &fds)) || (client->botid && !FD_ISSET(client->botid, &fds2)))) {
610 FD_SET(client->clientid, &fds);
611 FD_SET(client->botid, &fds2);
612 trigger = get_channel_trigger(client->botid, client->clientid, chan);
613 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
614 handle_command(client, user, chan, message + strlen(trigger));
615 goto got_chanmsg_loop2; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
621 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
622 struct ClientSocket *client;
623 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
624 if(client->user == target) {
625 handle_command(client, user, NULL, message);
630 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) {
631 struct cmd_function *cmdfunc;
632 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
633 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && !stricmp(cmdfunc->name, name))
636 cmdfunc = malloc(sizeof(*cmdfunc));
638 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
641 cmdfunc->botid = botid;
642 cmdfunc->name = strdup(name);
643 cmdfunc->module_id = module_id;
644 cmdfunc->func = func;
645 cmdfunc->flags = flags;
646 cmdfunc->paramcount = paramcount;
647 cmdfunc->channel_access = channel_access;
648 cmdfunc->global_access = global_access;
649 cmdfunc->next = cmd_functions;
650 cmd_functions = cmdfunc;
654 int set_trigger_callback(int botid, int module_id, trigger_callback_t *func) {
655 static struct trigger_callback *cb = NULL;
656 for(cb = trigger_callbacks; cb; cb = cb->next) {
657 if(cb->botid == botid)
661 cb = malloc(sizeof(*cb));
663 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
667 cb->next = trigger_callbacks;
668 trigger_callbacks = cb;
670 cb->module_id = module_id;
675 int flush_trigger_cache(int botid, int clientid) {
676 struct ChanNode *chan;
677 struct trigger_cache *trigger, *last;
678 for(chan = getAllChans(NULL); chan; chan = getAllChans(chan)) {
680 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
681 if(trigger->botid == botid && (botid || trigger->clientid == clientid)) {
683 last->next = trigger->next;
685 chan->trigger = trigger->next;
686 free(trigger->trigger);
696 int changeBotwiseChannelTrigger(int botid, int clientid, struct ChanNode *chan, char *new_trigger) {
697 struct trigger_cache *trigger;
698 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
699 if(trigger->botid == botid && (botid || trigger->clientid == clientid)) {
700 free(trigger->trigger);
701 trigger->trigger = strdup(new_trigger);
708 int bind_botwise_cmd_to_function(int botid, int clientid, char *cmd, struct cmd_function *func) {
709 int bind_index = get_binds_index(cmd[0]);
710 struct cmd_binding *cbind;
711 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
712 if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && !stricmp(cbind->cmd, cmd))
715 cbind = malloc(sizeof(*cbind));
717 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
720 cbind->botid = botid;
721 cbind->clientid = clientid;
722 cbind->cmd = strdup(cmd);
724 cbind->paramcount = 0;
725 cbind->global_access = 0;
726 cbind->channel_access = NULL;
728 cbind->triggered = 0;
729 cbind->next = cmd_binds[bind_index];
730 cmd_binds[bind_index] = cbind;
734 int bind_botwise_cmd_to_command(int botid, int clientid, char *cmd, char *func) {
735 struct cmd_function *cmdfunc;
738 if((c = strstr(func, "."))) {
740 struct cmd_bot_alias *botalias;
741 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
742 if(!stricmp(botalias->alias, func)) {
743 fbotid = botalias->botid;
750 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
751 if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && !stricmp(cmdfunc->name, func))
754 if(!cmdfunc) return 0;
755 int bind_index = get_binds_index(cmd[0]);
756 struct cmd_binding *cbind;
757 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
758 if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && !stricmp(cbind->cmd, cmd))
761 cbind = malloc(sizeof(*cbind));
763 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
766 cbind->botid = botid;
767 cbind->clientid = clientid;
768 cbind->cmd = strdup(cmd);
769 cbind->func = cmdfunc;
770 cbind->next = cmd_binds[bind_index];
771 cbind->paramcount = 0;
772 cbind->global_access = 0;
773 cbind->channel_access = NULL;
775 cbind->triggered = 0;
776 cmd_binds[bind_index] = cbind;
780 int unbind_botwise_cmd(int botid, int clientid, char *cmd) {
781 int bind_index = get_binds_index(cmd[0]);
782 struct cmd_binding *cbind, *last = NULL;
783 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
784 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
786 last->next = cbind->next;
788 cmd_binds[bind_index] = cbind->next;
790 if(cbind->paramcount) {
792 for(i = 0; i < cbind->paramcount; i++)
793 free(cbind->parameters[i]);
795 if(cbind->channel_access)
796 free(cbind->channel_access);
805 int unbind_botwise_allcmd(int botid, int clientid) {
807 for(i = 0; i < 27; i++) {
808 struct cmd_binding *cbind, *next, *last = NULL;
809 for(cbind = cmd_binds[i]; cbind; cbind = next) {
811 if((botid == 0 && clientid == cbind->clientid) || (botid && botid == cbind->botid)) {
813 last->next = cbind->next;
815 cmd_binds[i] = cbind->next;
817 if(cbind->paramcount) {
819 for(j = 0; j < cbind->paramcount; j++)
820 free(cbind->parameters[j]);
822 if(cbind->channel_access)
823 free(cbind->channel_access);
832 struct cmd_function *find_cmd_function(int botid, char *name) {
833 struct cmd_function *cmdfunc;
835 if((c = strstr(name, "."))) {
837 struct cmd_bot_alias *botalias;
838 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
839 if(!stricmp(botalias->alias, name)) {
840 botid = botalias->botid;
847 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
848 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && stricmp(cmdfunc->name, name) == 0)
855 cmd_binds = calloc(27, sizeof(*cmd_binds));
856 bind_chanmsg(got_chanmsg, 0);
857 bind_privmsg(got_privmsg, 0);
858 register_default_language_table(msgtab);
859 register_command(0, "linker", 0, modcmd_linker, 0, 0, 0, 0); //fake command for subcommands
864 for(i = 0; i < 27; i++) {
865 struct cmd_binding *cbind, *next;
866 for(cbind = cmd_binds[i]; cbind; cbind = next) {
869 if(cbind->paramcount) {
871 for(j = 0; j < cbind->paramcount; j++)
872 free(cbind->parameters[j]);
874 if(cbind->channel_access)
875 free(cbind->channel_access);
880 struct cmd_function *cmdfunct, *next;
881 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
882 next = cmdfunct->next;
883 free(cmdfunct->name);
886 struct trigger_callback *cb, *next_cb;
887 for(cb = trigger_callbacks; cb; cb = next_cb) {
891 struct cmd_bot_alias *botalias, *next_botalias;
892 for(botalias = bot_aliases; botalias; botalias = next_botalias) {
893 next_botalias = botalias->next;
894 free(botalias->alias);
897 cmd_functions = NULL;
898 trigger_callbacks = NULL;
902 void bind_botwise_set_parameters(int botid, int clientid, char *cmd, char *parameters) {
903 int bind_index = get_binds_index(cmd[0]);
904 struct cmd_binding *cbind;
905 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
906 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
907 if(cbind->paramcount) {
909 for(i = 0; i < cbind->paramcount; i++)
910 free(cbind->parameters[i]);
911 cbind->paramcount = 0;
914 char *a, *b = parameters;
918 cbind->parameters[cbind->paramcount++] = strdup(b);
927 void bind_botwise_set_global_access(int botid, int clientid, char *cmd, int gaccess) {
928 int bind_index = get_binds_index(cmd[0]);
929 struct cmd_binding *cbind;
930 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
931 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
933 cbind->global_access = gaccess;
934 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
936 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
943 void bind_botwise_set_channel_access(int botid, int clientid, char *cmd, char *chanaccess) {
944 int bind_index = get_binds_index(cmd[0]);
945 struct cmd_binding *cbind;
946 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
947 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
948 if(cbind->channel_access)
949 free(cbind->channel_access);
951 cbind->channel_access = strdup(chanaccess);
952 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
954 cbind->channel_access = NULL;
955 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
962 void bind_botwise_set_bind_flags(int botid, int clientid, char *cmd, unsigned int flags) {
963 int bind_index = get_binds_index(cmd[0]);
964 struct cmd_binding *cbind;
965 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
966 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
967 cbind->flags |= flags;
973 struct cmd_binding *find_botwise_cmd_binding(int botid, int clientid, char *cmd) {
974 int bind_index = get_binds_index(cmd[0]);
975 struct cmd_binding *cbind;
976 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
977 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
984 void bind_botwise_unbound_required_functions(int botid, int clientid) {
985 struct cmd_function *cmdfunc;
987 struct cmd_binding *cbind;
988 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
989 if((cmdfunc->flags & CMDFLAG_REQUIRED)) {
991 for(i = 0; i < 27; i++) {
992 for(cbind = cmd_binds[i]; cbind; cbind = cbind->next) {
993 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && cbind->func == cmdfunc) {
1001 if(!found && bind_botwise_cmd_to_function(botid, clientid, cmdfunc->name, cmdfunc)) {
1002 cbind = find_botwise_cmd_binding(botid, clientid, cmdfunc->name);
1003 cbind->flags |= CMDFLAG_TEMPONARY_BIND;
1009 void register_command_alias(int botid, char *alias) {
1010 struct cmd_bot_alias *botalias;
1011 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
1012 if(!stricmp(botalias->alias, alias))
1015 botalias = malloc(sizeof(*botalias));
1017 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
1020 botalias->botid = botid;
1021 botalias->alias = strdup(alias);
1022 botalias->next = bot_aliases;
1023 bot_aliases = botalias;
1026 struct cmd_binding *getAllBinds(struct cmd_binding *last) {
1031 bind_index = get_binds_index(last->cmd[0]) + 1;
1037 if(cmd_binds[bind_index])
1038 return cmd_binds[bind_index];
1040 } while(bind_index < 27);
1044 void unregister_module_commands(int module_id) {
1045 struct cmd_function *cmdfunct, *nextfunct, *prevfunct = NULL;
1047 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = nextfunct) {
1048 nextfunct = cmdfunct->next;
1049 if(cmdfunct->module_id == module_id) {
1050 for(i = 0; i < 27; i++) {
1051 struct cmd_binding *cbind, *next, *last = NULL;
1052 for(cbind = cmd_binds[i]; cbind; cbind = next) {
1054 if(cmdfunct == cbind->func) {
1056 last->next = cbind->next;
1058 cmd_binds[i] = cbind->next;
1060 if(cbind->paramcount) {
1062 for(j = 0; j < cbind->paramcount; j++)
1063 free(cbind->parameters[j]);
1065 if(cbind->channel_access)
1066 free(cbind->channel_access);
1073 prevfunct->next = nextfunct;
1075 cmd_functions = nextfunct;
1076 free(cmdfunct->name);
1079 prevfunct = cmdfunct;
1081 struct trigger_callback *cb, *prevcb = NULL, *nextcb;
1082 for(cb = trigger_callbacks; cb; cb = nextcb) {
1084 if(cb->module_id == module_id) {
1086 prevcb->next = nextcb;
1088 trigger_callbacks = nextcb;