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."},
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 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;
130 tmp_text_client = cache->textclient;
131 handle_command_async(cache->client, 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 void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
145 struct ChanNode *sent_chan = chan;
146 if(message[0] == '#') {
147 char *chanName = message;
148 message = strstr(message, " ");
152 struct ChanNode *chan2 = getChanByName(chanName);
156 message = strdup(message);
157 int bind_index = get_binds_index(message[0]);
158 char *args = strstr(message, " ");
159 char *args_buffer = NULL; //we need this to save a possible pointer to a allocation we need to free
164 struct cmd_binding *cbind;
166 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
167 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, message) == 0) {
170 tmp_text_client = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0));
171 if(cbind->func->func == modcmd_linker) {
173 char command[MAXLEN];
174 struct cmd_binding *parent_bind = cbind;
177 args = strstr(args, " ");
182 sprintf(command, "%s %s", parent_bind->cmd, subcmd);
183 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
184 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0)
190 //list all sub commands
191 int commandlen = sprintf(command, "%s ", parent_bind->cmd);
192 char subcommands[MAXLEN];
194 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
195 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmplen(cbind->cmd, command, commandlen) == 0) {
196 subcompos += sprintf(subcommands + subcompos, (subcompos ? ", %s" : "%s"), cbind->cmd + commandlen);
199 reply(tmp_text_client, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "
\1dnone
\1d"));
203 if(statistics_enabled)
204 statistics_commands++;
207 if((cbind->func->flags & CMDFLAG_FUNCMD)) {
212 //parse the arguments...
213 char *arga[MAXNUMPARAMS];
220 //skip leading spaces
224 if (argc >= MAXNUMPARAMS)
226 while ((escape || *args != ' ') && *args) {
227 if((cbind->func->flags & CMDFLAG_ESCAPE_ARGS) && *args == '\\') {
232 if(!escape && offset) {
233 args[0 - offset] = args[0];
239 args[0-offset] = '\0';
245 if(argc != 0 && argv[0][0] == '#' && !(cbind->func->flags & CMDFLAG_CHAN_PARAM)) {
246 struct ChanNode *chan2 = getChanByName(argv[0]);
253 if(cbind->paramcount) {
254 //userdefined parameters...
255 args_buffer = malloc(MAXLEN * 2 * sizeof(*args_buffer));
257 char *uargs[MAXNUMPARAMS];
262 for(i = 0; i < cbind->paramcount; i++) {
263 b = cbind->parameters[i];
267 while(*c && *c != ' ') {
271 if(!*c || *c == ' ') c = NULL;
276 if(b[strlen(b)-1] == '-') {
277 allargs = strlen(b)-1;
282 } else if(b[strlen(b)-1] == '+') {
283 allargs = strlen(b)-1;
294 uargs[uargc++] = args_buffer + args_pos;
296 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
297 } else if(allargs == 1) {
298 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
299 for(argi++; argi <= argc; argi++) {
300 uargs[uargc++] = args_buffer + args_pos;
301 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
303 } else if(allargs == 2) {
304 for(;argi <= argc; argi++) {
305 args_pos += sprintf(args_buffer + args_pos, (allargs ? "%s" : " %s"), argv[argi-1]);
310 } else if((cbind->func->flags & CMDFLAG_EMPTY_ARGS)) {
311 uargs[uargc++] = args_buffer + args_pos;
312 args_buffer[args_pos++] = '\0';
314 uargs[uargc++] = args_buffer + args_pos;
315 args_pos += sprintf(args_buffer + args_pos, "%s", c) + 1;
317 } else if(!strcmp(b, "c")) {
318 uargs[uargc++] = args_buffer + args_pos;
319 args_pos += sprintf(args_buffer + args_pos, "%s", (chan ? chan->name : "")) + 1;
320 } else if(!strcmp(b, "n")) {
321 uargs[uargc++] = args_buffer + args_pos;
322 args_pos += sprintf(args_buffer + args_pos, "%s", user->nick) + 1;
324 if(c) c[-1] = '|'; //reset \0 to |
326 uargs[uargc++] = args_buffer + args_pos;
327 args_pos += sprintf(args_buffer + args_pos, "%s", b) + 1;
333 if(argc < cbind->func->paramcount) {
334 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
337 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
338 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
341 if(((cbind->func->flags & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) {
343 struct command_check_user_cache *data = malloc(sizeof(*data));
344 char **temp_argv = malloc(argc*sizeof(*temp_argv));
345 if (!data || !temp_argv) {
346 perror("malloc() failed");
349 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
350 data->argv = temp_argv;
352 data->client = client;
355 data->sent_chan = sent_chan;
356 data->message = message;
357 data->args_buffer = args_buffer;
359 data->textclient = tmp_text_client;
360 get_userauth(user, command_checked_auth, data);
363 handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
367 if(!found_cmd && !sent_chan)
368 reply(get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0)), user, "MODCMD_UNKNOWN", message);
374 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) {
378 char requested_uaccess = 0;
379 int eventflags = (cbind->func->flags & (CMDFLAG_LOG | CMDFLAG_OPLOG));
380 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
381 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
384 if(chan && sent_chan != chan && !isUserOnChan(user, chan)) {
385 char user_in_chan = 0;
386 if((user->flags & USERFLAG_ISAUTHED)) {
387 //maybe there's another user authed to user->auth on the channel...
388 struct ChanUser *cchanuser;
389 for(cchanuser = getChannelUsers(chan, NULL); cchanuser; cchanuser = getChannelUsers(chan, cchanuser)) {
390 if((cchanuser->user->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cchanuser->user->auth)) {
397 //check if we are allowed to execute commands in this channel
398 requested_uaccess = 1;
399 uaccess = getChannelAccess(user, chan);
401 reply(tmp_text_client, user, "MODCMD_CROSSCHAN", chan->name);
406 if(sent_chan && sent_chan != chan) {
407 //check pubcmd of this channel
408 printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
410 if ((row = mysql_fetch_row(res)) != NULL) {
411 int saccess = getChannelAccess(user, sent_chan);
412 if(row[0] && saccess < atoi(row[0]) && !isGodMode(user)) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
413 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
418 int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
419 if(global_access > 0) {
420 int user_global_access = 0;
421 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
423 if ((row = mysql_fetch_row(res)) != NULL) {
424 user_global_access = atoi(row[0]);
426 if(user_global_access < global_access) {
427 if(!user_global_access)
428 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
430 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
434 if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
435 MYSQL_ROW defaults = NULL;
436 char access_list[256];
438 int access_count = 0;
440 char *str_a, *str_b = cbind->func->channel_access, *str_c;
441 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
442 str_b = cbind->channel_access;
443 access_list[0] = '\0';
445 str_c = strdup(str_b);
447 while((str_a = str_b)) {
448 str_b = strstr(str_a, ",");
455 access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
458 if(atoi(str_a) > minaccess)
459 minaccess = atoi(str_a);
464 if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
465 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
467 if ((row = mysql_fetch_row(res)) != NULL) {
468 chan->flags |= CHANFLAG_CHAN_REGISTERED;
469 chan->channel_id = atoi(row[0]);
470 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
471 if(!requested_uaccess) uaccess = getChannelAccess(user, chan);
472 if(uaccess < minaccess && isGodMode(user)) {
473 eventflags |= CMDFLAG_OPLOG;
474 } else if(uaccess < minaccess) {
476 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
479 if(!row[1] && !defaults) {
480 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
481 defaults = mysql_fetch_row(mysql_use());
483 if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
484 if(isGodMode(user)) {
485 eventflags |= CMDFLAG_OPLOG;
488 reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
493 for(i = 0; i < access_count; i++) {
494 if(!row[2+i] && !defaults) {
495 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
496 defaults = mysql_fetch_row(mysql_use());
498 if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
499 if(isGodMode(user)) {
500 eventflags |= CMDFLAG_OPLOG;
502 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
509 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
511 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
512 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
515 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);
517 if ((row = mysql_fetch_row(res)) == NULL) {
518 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
520 } else if(!strcmp(row[1], "1")) {
521 reply(tmp_text_client, user, "MODCMD_CHAN_SUSPENDED");
525 if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
526 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
529 struct Event *event = createEvent(client, user, chan, cbind, argv, argc, eventflags);
530 cbind->func->func(client, user, chan, argv, argc, event);
533 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
536 struct ClientSocket *client;
540 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
541 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)))) {
542 FD_SET(client->clientid, &fds);
543 FD_SET(client->botid, &fds2);
544 trigger = get_channel_trigger(client->botid, client->clientid, chan);
545 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
546 handle_command(client, user, chan, message + strlen(trigger));
547 goto got_chanmsg_loop1; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
552 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
553 if(isUserOnChan(client->user, chan) && ((client->botid == 0 && !FD_ISSET(client->clientid, &fds)) || (client->botid && !FD_ISSET(client->botid, &fds2)))) {
554 FD_SET(client->clientid, &fds);
555 FD_SET(client->botid, &fds2);
556 trigger = get_channel_trigger(client->botid, client->clientid, chan);
557 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
558 handle_command(client, user, chan, message + strlen(trigger));
559 goto got_chanmsg_loop2; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
565 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
566 struct ClientSocket *client;
567 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
568 if(client->user == target) {
569 handle_command(client, user, NULL, message);
574 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, char *channel_access, int global_access, unsigned int flags) {
575 struct cmd_function *cmdfunc;
576 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
577 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, name) == 0)
580 cmdfunc = malloc(sizeof(*cmdfunc));
582 perror("malloc() failed");
585 cmdfunc->botid = botid;
586 cmdfunc->name = strdup(name);
587 cmdfunc->func = func;
588 cmdfunc->flags = flags;
589 cmdfunc->paramcount = paramcount;
590 cmdfunc->channel_access = channel_access;
591 cmdfunc->global_access = global_access;
592 cmdfunc->next = cmd_functions;
593 cmd_functions = cmdfunc;
597 int set_trigger_callback(int botid, trigger_callback_t *func) {
598 static struct trigger_callback *cb = NULL;
599 for(cb = trigger_callbacks; cb; cb = cb->next) {
600 if(cb->botid == botid)
604 cb = malloc(sizeof(*cb));
606 perror("malloc() failed");
610 cb->next = trigger_callbacks;
611 trigger_callbacks = cb;
617 int changeBotwiseChannelTrigger(int botid, int clientid, struct ChanNode *chan, char *new_trigger) {
618 struct trigger_cache *trigger;
619 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
620 if(trigger->botid == botid && (botid || trigger->clientid == clientid)) {
621 free(trigger->trigger);
622 trigger->trigger = strdup(new_trigger);
629 int bind_botwise_cmd_to_function(int botid, int clientid, char *cmd, struct cmd_function *func) {
630 int bind_index = get_binds_index(cmd[0]);
631 struct cmd_binding *cbind;
632 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
633 if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && strcmp(cbind->cmd, cmd) == 0)
636 cbind = malloc(sizeof(*cbind));
638 perror("malloc() failed");
641 cbind->botid = botid;
642 cbind->clientid = clientid;
643 cbind->cmd = strdup(cmd);
645 cbind->paramcount = 0;
646 cbind->global_access = 0;
647 cbind->channel_access = NULL;
649 cbind->triggered = 0;
650 cbind->next = cmd_binds[bind_index];
651 cmd_binds[bind_index] = cbind;
655 int bind_botwise_cmd_to_command(int botid, int clientid, char *cmd, char *func) {
656 struct cmd_function *cmdfunc;
659 if((c = strstr(func, "."))) {
661 struct cmd_bot_alias *botalias;
662 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
663 if(!stricmp(botalias->alias, func)) {
664 fbotid = botalias->botid;
671 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
672 if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, func) == 0)
675 if(!cmdfunc) return 0;
676 int bind_index = get_binds_index(cmd[0]);
677 struct cmd_binding *cbind;
678 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
679 if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && strcmp(cbind->cmd, cmd) == 0)
682 cbind = malloc(sizeof(*cbind));
684 perror("malloc() failed");
687 cbind->botid = botid;
688 cbind->clientid = clientid;
689 cbind->cmd = strdup(cmd);
690 cbind->func = cmdfunc;
691 cbind->next = cmd_binds[bind_index];
692 cbind->paramcount = 0;
693 cbind->global_access = 0;
694 cbind->channel_access = NULL;
696 cbind->triggered = 0;
697 cmd_binds[bind_index] = cbind;
701 int unbind_botwise_cmd(int botid, int clientid, char *cmd) {
702 int bind_index = get_binds_index(cmd[0]);
703 struct cmd_binding *cbind, *last = NULL;
704 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
705 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
707 last->next = cbind->next;
709 cmd_binds[bind_index] = cbind->next;
711 if(cbind->paramcount) {
713 for(i = 0; i < cbind->paramcount; i++)
714 free(cbind->parameters[i]);
716 if(cbind->channel_access)
717 free(cbind->channel_access);
726 int unbind_botwise_allcmd(int clientid) {
728 for(i = 0; i < 27; i++) {
729 struct cmd_binding *cbind, *next, *last = NULL;
730 for(cbind = cmd_binds[i]; cbind; cbind = next) {
732 if(clientid == cbind->clientid) {
734 last->next = cbind->next;
736 cmd_binds[i] = cbind->next;
738 if(cbind->paramcount) {
740 for(j = 0; j < cbind->paramcount; j++)
741 free(cbind->parameters[j]);
743 if(cbind->channel_access)
744 free(cbind->channel_access);
753 struct cmd_function *find_cmd_function(int botid, char *name) {
754 struct cmd_function *cmdfunc;
756 if((c = strstr(name, "."))) {
758 struct cmd_bot_alias *botalias;
759 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
760 if(!stricmp(botalias->alias, name)) {
761 botid = botalias->botid;
768 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
769 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && stricmp(cmdfunc->name, name) == 0)
775 struct ClientSocket *getTextBot() {
776 return tmp_text_client;
780 cmd_binds = calloc(27, sizeof(*cmd_binds));
781 bind_chanmsg(got_chanmsg);
782 bind_privmsg(got_privmsg);
783 register_default_language_table(msgtab);
784 register_command(0, "linker", modcmd_linker, 0, 0, 0, 0); //fake command for subcommands
789 for(i = 0; i < 27; i++) {
790 struct cmd_binding *cbind, *next;
791 for(cbind = cmd_binds[i]; cbind; cbind = next) {
794 if(cbind->paramcount) {
796 for(j = 0; j < cbind->paramcount; j++)
797 free(cbind->parameters[j]);
799 if(cbind->channel_access)
800 free(cbind->channel_access);
805 struct cmd_function *cmdfunct, *next;
806 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
807 next = cmdfunct->next;
808 free(cmdfunct->name);
811 struct trigger_callback *cb, *next_cb;
812 for(cb = trigger_callbacks; cb; cb = next_cb) {
816 struct cmd_bot_alias *botalias, *next_botalias;
817 for(botalias = bot_aliases; botalias; botalias = next_botalias) {
818 next_botalias = botalias->next;
819 free(botalias->alias);
822 cmd_functions = NULL;
823 trigger_callbacks = NULL;
827 void bind_botwise_set_parameters(int botid, int clientid, char *cmd, char *parameters) {
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 && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
832 if(cbind->paramcount) {
834 for(i = 0; i < cbind->paramcount; i++)
835 free(cbind->parameters[i]);
836 cbind->paramcount = 0;
838 char *a, *b = parameters;
842 cbind->parameters[cbind->paramcount++] = strdup(b);
850 void bind_botwise_set_global_access(int botid, int clientid, char *cmd, int gaccess) {
851 int bind_index = get_binds_index(cmd[0]);
852 struct cmd_binding *cbind;
853 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
854 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
856 cbind->global_access = gaccess;
857 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
859 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
866 void bind_botwise_set_channel_access(int botid, int clientid, char *cmd, char *chanaccess) {
867 int bind_index = get_binds_index(cmd[0]);
868 struct cmd_binding *cbind;
869 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
870 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
871 if(cbind->channel_access)
872 free(cbind->channel_access);
874 cbind->channel_access = strdup(chanaccess);
875 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
877 cbind->channel_access = NULL;
878 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
885 struct cmd_binding *find_botwise_cmd_binding(int botid, int clientid, char *cmd) {
886 int bind_index = get_binds_index(cmd[0]);
887 struct cmd_binding *cbind;
888 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
889 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
896 void bind_botwise_unbound_required_functions(int botid, int clientid) {
897 struct cmd_function *cmdfunc;
899 struct cmd_binding *cbind;
900 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
901 if((cmdfunc->flags & CMDFLAG_REQUIRED)) {
903 for(i = 0; i < 27; i++) {
904 for(cbind = cmd_binds[i]; cbind; cbind = cbind->next) {
905 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && cbind->func == cmdfunc) {
913 if(!found && bind_botwise_cmd_to_function(botid, clientid, cmdfunc->name, cmdfunc)) {
914 cbind = find_botwise_cmd_binding(botid, clientid, cmdfunc->name);
915 cbind->flags |= CMDFLAG_TEMPONARY_BIND;
921 void register_command_alias(int botid, char *alias) {
922 struct cmd_bot_alias *botalias;
923 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
924 if(!stricmp(botalias->alias, alias))
927 botalias = malloc(sizeof(*botalias));
929 perror("malloc() failed");
932 botalias->botid = botid;
933 botalias->alias = strdup(alias);
934 botalias->next = bot_aliases;
935 bot_aliases = botalias;
938 struct cmd_binding *getAllBinds(struct cmd_binding *last) {
943 bind_index = get_binds_index(last->cmd[0]) + 1;
949 if(cmd_binds[bind_index])
950 return cmd_binds[bind_index];
952 } while(bind_index < 27);