1 /* modcmd.c - NeonServ v5.3
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 {
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."}, /* {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 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 struct cmd_binding *modcmd_linker_command(struct ClientSocket *client, 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, 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(tmp_text_client, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "
\1dnone
\1d"));
182 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
183 struct ChanNode *sent_chan = chan;
184 if(message[0] == '#') {
185 char *chanName = message;
186 message = strstr(message, " ");
190 struct ChanNode *chan2 = getChanByName(chanName);
194 message = strdup(message);
195 int bind_index = get_binds_index(message[0]);
196 char *args = strstr(message, " ");
197 char *args_buffer = NULL; //we need this to save a possible pointer to a allocation we need to free
202 struct cmd_binding *cbind;
204 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
205 if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, message) == 0) {
208 tmp_text_client = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0));
209 if(cbind->func->func == modcmd_linker) {
210 cbind = modcmd_linker_command(client, user, cbind, bind_index, &args);
211 if(cbind == NULL) break;
213 if(statistics_enabled)
214 statistics_commands++;
217 if((BIND_FLAGS(cbind) & CMDFLAG_FUNCMD)) {
222 //parse the arguments...
223 char *arga[MAXNUMPARAMS];
230 //skip leading spaces
234 if (argc >= MAXNUMPARAMS)
236 while ((escape || *args != ' ') && *args) {
237 if((BIND_FLAGS(cbind) & CMDFLAG_ESCAPE_ARGS) && *args == '\\') {
242 if(!escape && offset) {
243 args[0 - offset] = args[0];
249 args[0-offset] = '\0';
255 if(cbind->paramcount) {
256 //userdefined parameters...
257 args_buffer = malloc(MAXLEN * 2 * sizeof(*args_buffer));
259 char *uargs[MAXNUMPARAMS];
264 for(i = 0; i < cbind->paramcount; i++) {
265 b = cbind->parameters[i];
269 while(*c && *c != ' ') {
273 if(!*c || *c == ' ') c = NULL;
278 if(b[strlen(b)-1] == '-') {
279 allargs = strlen(b)-1;
284 } else if(b[strlen(b)-1] == '+') {
285 allargs = strlen(b)-1;
296 uargs[uargc++] = args_buffer + args_pos;
298 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
299 } else if(allargs == 1) {
300 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
301 for(argi++; argi <= argc; argi++) {
302 uargs[uargc++] = args_buffer + args_pos;
303 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
305 } else if(allargs == 2) {
306 for(;argi <= argc; argi++) {
307 args_pos += sprintf(args_buffer + args_pos, (allargs ? "%s" : " %s"), argv[argi-1]);
312 } else if((BIND_FLAGS(cbind) & CMDFLAG_EMPTY_ARGS)) {
313 uargs[uargc++] = args_buffer + args_pos;
314 args_buffer[args_pos++] = '\0';
316 uargs[uargc++] = args_buffer + args_pos;
317 args_pos += sprintf(args_buffer + args_pos, "%s", c) + 1;
319 } else if(!strcmp(b, "c")) {
320 uargs[uargc++] = args_buffer + args_pos;
321 args_pos += sprintf(args_buffer + args_pos, "%s", (chan ? chan->name : "")) + 1;
322 } else if(!strcmp(b, "n")) {
323 uargs[uargc++] = args_buffer + args_pos;
324 args_pos += sprintf(args_buffer + args_pos, "%s", user->nick) + 1;
326 if(c) c[-1] = '|'; //reset \0 to |
328 uargs[uargc++] = args_buffer + args_pos;
329 args_pos += sprintf(args_buffer + args_pos, "%s", b) + 1;
335 if(argc != 0 && argv[0][0] == '#' && !(BIND_FLAGS(cbind) & CMDFLAG_CHAN_PARAM)) {
336 struct ChanNode *chan2 = getChanByName(argv[0]);
343 if(argc < cbind->func->paramcount) {
344 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
347 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_CHAN) && !chan) {
348 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
351 if(((BIND_FLAGS(cbind) & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) {
353 struct command_check_user_cache *data = malloc(sizeof(*data));
354 char **temp_argv = malloc(argc*sizeof(*temp_argv));
355 if (!data || !temp_argv) {
356 perror("malloc() failed");
359 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
360 data->argv = temp_argv;
362 data->client = client;
365 data->sent_chan = sent_chan;
366 data->message = message;
367 data->args_buffer = args_buffer;
369 data->textclient = tmp_text_client;
370 get_userauth(user, command_checked_auth, data);
373 handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
377 if(!found_cmd && !sent_chan && !(client->flags & SOCKET_FLAG_SILENT))
378 reply(get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0)), user, "MODCMD_UNKNOWN", message);
384 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) {
388 char requested_uaccess = 0;
389 int eventflags = (BIND_FLAGS(cbind) & (CMDFLAG_LOG | CMDFLAG_OPLOG));
390 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
391 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
394 if(chan && sent_chan != chan && (BIND_FLAGS(cbind) & CMDFLAG_NO_CROSSCHAN) && !isUserOnChan(user, chan)) {
395 char user_in_chan = 0;
396 if((user->flags & USERFLAG_ISAUTHED)) {
397 //maybe there's another user authed to user->auth on the channel...
398 struct ChanUser *cchanuser;
399 for(cchanuser = getChannelUsers(chan, NULL); cchanuser; cchanuser = getChannelUsers(chan, cchanuser)) {
400 if((cchanuser->user->flags & USERFLAG_ISAUTHED) && !stricmp(user->auth, cchanuser->user->auth)) {
407 //check if we are allowed to execute commands in this channel
408 requested_uaccess = 1;
409 uaccess = getChannelAccess(user, chan);
411 if(isGodMode(user)) {
412 eventflags |= CMDFLAG_OPLOG;
414 reply(tmp_text_client, user, "MODCMD_CROSSCHAN", chan->name);
420 if(sent_chan && sent_chan != chan) {
421 //check pubcmd of this channel
422 printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
424 if ((row = mysql_fetch_row(res)) != NULL) {
425 int saccess = getChannelAccess(user, sent_chan);
426 if(row[0] && saccess < atoi(row[0]) && !isGodMode(user)) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
427 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
432 int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
433 if(global_access > 0) {
434 int user_global_access = 0;
435 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
437 if ((row = mysql_fetch_row(res)) != NULL) {
438 user_global_access = atoi(row[0]);
440 if(user_global_access < global_access) {
441 if(!user_global_access)
442 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
444 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
448 if((BIND_FLAGS(cbind) & CMDFLAG_REGISTERED_CHAN)) {
449 MYSQL_ROW defaults = NULL;
450 char access_list[256];
452 int access_count = 0;
454 char *str_a, *str_b = cbind->func->channel_access, *str_c;
455 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
456 str_b = cbind->channel_access;
457 access_list[0] = '\0';
459 struct ChanUser *chanuser = getChanUser(user, chan);
460 str_c = strdup(str_b);
462 while((str_a = str_b)) {
463 str_b = strstr(str_a, ",");
468 if(*str_a == '@' || *str_a == '+') {
469 //privs can override this access requirement
471 if(*str_a == '@') priv = CHANUSERFLAG_OPPED;
472 else if(*str_a == '%') priv = CHANUSERFLAG_HALFOPPED;
473 else if(*str_a == '+') priv = CHANUSERFLAG_VOICED;
474 if(chanuser && (chanuser->flags & priv)) continue;
479 access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
482 if(atoi(str_a) > minaccess)
483 minaccess = atoi(str_a);
488 if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
489 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
491 if ((row = mysql_fetch_row(res)) != NULL) {
492 chan->flags |= CHANFLAG_CHAN_REGISTERED;
493 chan->channel_id = atoi(row[0]);
494 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
495 if(!requested_uaccess) uaccess = getChannelAccess(user, chan);
496 if(uaccess < minaccess && isGodMode(user)) {
497 eventflags |= CMDFLAG_OPLOG;
498 } else if(uaccess < minaccess) {
500 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
503 if(!row[1] && !defaults) {
504 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
505 defaults = mysql_fetch_row(mysql_use());
507 if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
508 if(isGodMode(user)) {
509 eventflags |= CMDFLAG_OPLOG;
512 reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
517 for(i = 0; i < access_count; i++) {
518 if(!row[2+i] && !defaults) {
519 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
520 defaults = mysql_fetch_row(mysql_use());
522 if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
523 if(isGodMode(user)) {
524 eventflags |= CMDFLAG_OPLOG;
526 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
533 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
535 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
536 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
539 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);
541 if ((row = mysql_fetch_row(res)) == NULL) {
542 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
544 } else if(!strcmp(row[1], "1")) {
545 reply(tmp_text_client, user, "MODCMD_CHAN_SUSPENDED");
549 if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
550 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
553 struct Event *event = createEvent(client, user, chan, cbind, argv, argc, eventflags);
554 cbind->func->func(client, user, chan, argv, argc, event);
557 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
560 struct ClientSocket *client;
564 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
565 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)))) {
566 FD_SET(client->clientid, &fds);
567 FD_SET(client->botid, &fds2);
568 trigger = get_channel_trigger(client->botid, client->clientid, chan);
569 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
570 handle_command(client, user, chan, message + strlen(trigger));
571 goto got_chanmsg_loop1; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
576 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
577 if(isUserOnChan(client->user, chan) && ((client->botid == 0 && !FD_ISSET(client->clientid, &fds)) || (client->botid && !FD_ISSET(client->botid, &fds2)))) {
578 FD_SET(client->clientid, &fds);
579 FD_SET(client->botid, &fds2);
580 trigger = get_channel_trigger(client->botid, client->clientid, chan);
581 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
582 handle_command(client, user, chan, message + strlen(trigger));
583 goto got_chanmsg_loop2; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
589 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
590 struct ClientSocket *client;
591 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
592 if(client->user == target) {
593 handle_command(client, user, NULL, message);
598 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, char *channel_access, int global_access, unsigned int flags) {
599 struct cmd_function *cmdfunc;
600 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
601 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, name) == 0)
604 cmdfunc = malloc(sizeof(*cmdfunc));
606 perror("malloc() failed");
609 cmdfunc->botid = botid;
610 cmdfunc->name = strdup(name);
611 cmdfunc->func = func;
612 cmdfunc->flags = flags;
613 cmdfunc->paramcount = paramcount;
614 cmdfunc->channel_access = channel_access;
615 cmdfunc->global_access = global_access;
616 cmdfunc->next = cmd_functions;
617 cmd_functions = cmdfunc;
621 int set_trigger_callback(int botid, trigger_callback_t *func) {
622 static struct trigger_callback *cb = NULL;
623 for(cb = trigger_callbacks; cb; cb = cb->next) {
624 if(cb->botid == botid)
628 cb = malloc(sizeof(*cb));
630 perror("malloc() failed");
634 cb->next = trigger_callbacks;
635 trigger_callbacks = cb;
641 int flush_trigger_cache(int botid, int clientid) {
642 struct ChanNode *chan;
643 struct trigger_cache *trigger, *last;
644 for(chan = getAllChans(NULL); chan; chan = getAllChans(chan)) {
646 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
647 if(trigger->botid == botid && (botid || trigger->clientid == clientid)) {
649 last->next = trigger->next;
651 chan->trigger = trigger->next;
652 free(trigger->trigger);
662 int changeBotwiseChannelTrigger(int botid, int clientid, struct ChanNode *chan, char *new_trigger) {
663 struct trigger_cache *trigger;
664 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
665 if(trigger->botid == botid && (botid || trigger->clientid == clientid)) {
666 free(trigger->trigger);
667 trigger->trigger = strdup(new_trigger);
674 int bind_botwise_cmd_to_function(int botid, int clientid, char *cmd, struct cmd_function *func) {
675 int bind_index = get_binds_index(cmd[0]);
676 struct cmd_binding *cbind;
677 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
678 if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && strcmp(cbind->cmd, cmd) == 0)
681 cbind = malloc(sizeof(*cbind));
683 perror("malloc() failed");
686 cbind->botid = botid;
687 cbind->clientid = clientid;
688 cbind->cmd = strdup(cmd);
690 cbind->paramcount = 0;
691 cbind->global_access = 0;
692 cbind->channel_access = NULL;
694 cbind->triggered = 0;
695 cbind->next = cmd_binds[bind_index];
696 cmd_binds[bind_index] = cbind;
700 int bind_botwise_cmd_to_command(int botid, int clientid, char *cmd, char *func) {
701 struct cmd_function *cmdfunc;
704 if((c = strstr(func, "."))) {
706 struct cmd_bot_alias *botalias;
707 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
708 if(!stricmp(botalias->alias, func)) {
709 fbotid = botalias->botid;
716 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
717 if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, func) == 0)
720 if(!cmdfunc) return 0;
721 int bind_index = get_binds_index(cmd[0]);
722 struct cmd_binding *cbind;
723 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
724 if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && strcmp(cbind->cmd, cmd) == 0)
727 cbind = malloc(sizeof(*cbind));
729 perror("malloc() failed");
732 cbind->botid = botid;
733 cbind->clientid = clientid;
734 cbind->cmd = strdup(cmd);
735 cbind->func = cmdfunc;
736 cbind->next = cmd_binds[bind_index];
737 cbind->paramcount = 0;
738 cbind->global_access = 0;
739 cbind->channel_access = NULL;
741 cbind->triggered = 0;
742 cmd_binds[bind_index] = cbind;
746 int unbind_botwise_cmd(int botid, int clientid, char *cmd) {
747 int bind_index = get_binds_index(cmd[0]);
748 struct cmd_binding *cbind, *last = NULL;
749 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
750 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
752 last->next = cbind->next;
754 cmd_binds[bind_index] = cbind->next;
756 if(cbind->paramcount) {
758 for(i = 0; i < cbind->paramcount; i++)
759 free(cbind->parameters[i]);
761 if(cbind->channel_access)
762 free(cbind->channel_access);
771 int unbind_botwise_allcmd(int clientid) {
773 for(i = 0; i < 27; i++) {
774 struct cmd_binding *cbind, *next, *last = NULL;
775 for(cbind = cmd_binds[i]; cbind; cbind = next) {
777 if(clientid == cbind->clientid) {
779 last->next = cbind->next;
781 cmd_binds[i] = cbind->next;
783 if(cbind->paramcount) {
785 for(j = 0; j < cbind->paramcount; j++)
786 free(cbind->parameters[j]);
788 if(cbind->channel_access)
789 free(cbind->channel_access);
798 struct cmd_function *find_cmd_function(int botid, char *name) {
799 struct cmd_function *cmdfunc;
801 if((c = strstr(name, "."))) {
803 struct cmd_bot_alias *botalias;
804 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
805 if(!stricmp(botalias->alias, name)) {
806 botid = botalias->botid;
813 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
814 if((cmdfunc->botid == botid || cmdfunc->botid == 0) && stricmp(cmdfunc->name, name) == 0)
820 struct ClientSocket *getTextBot() {
821 return tmp_text_client;
825 cmd_binds = calloc(27, sizeof(*cmd_binds));
826 bind_chanmsg(got_chanmsg);
827 bind_privmsg(got_privmsg);
828 register_default_language_table(msgtab);
829 register_command(0, "linker", modcmd_linker, 0, 0, 0, 0); //fake command for subcommands
834 for(i = 0; i < 27; i++) {
835 struct cmd_binding *cbind, *next;
836 for(cbind = cmd_binds[i]; cbind; cbind = next) {
839 if(cbind->paramcount) {
841 for(j = 0; j < cbind->paramcount; j++)
842 free(cbind->parameters[j]);
844 if(cbind->channel_access)
845 free(cbind->channel_access);
850 struct cmd_function *cmdfunct, *next;
851 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
852 next = cmdfunct->next;
853 free(cmdfunct->name);
856 struct trigger_callback *cb, *next_cb;
857 for(cb = trigger_callbacks; cb; cb = next_cb) {
861 struct cmd_bot_alias *botalias, *next_botalias;
862 for(botalias = bot_aliases; botalias; botalias = next_botalias) {
863 next_botalias = botalias->next;
864 free(botalias->alias);
867 cmd_functions = NULL;
868 trigger_callbacks = NULL;
872 void bind_botwise_set_parameters(int botid, int clientid, char *cmd, char *parameters) {
873 int bind_index = get_binds_index(cmd[0]);
874 struct cmd_binding *cbind;
875 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
876 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
877 if(cbind->paramcount) {
879 for(i = 0; i < cbind->paramcount; i++)
880 free(cbind->parameters[i]);
881 cbind->paramcount = 0;
884 char *a, *b = parameters;
888 cbind->parameters[cbind->paramcount++] = strdup(b);
897 void bind_botwise_set_global_access(int botid, int clientid, char *cmd, int gaccess) {
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) && strcmp(cbind->cmd, cmd) == 0) {
903 cbind->global_access = gaccess;
904 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
906 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
913 void bind_botwise_set_channel_access(int botid, int clientid, char *cmd, char *chanaccess) {
914 int bind_index = get_binds_index(cmd[0]);
915 struct cmd_binding *cbind;
916 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
917 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
918 if(cbind->channel_access)
919 free(cbind->channel_access);
921 cbind->channel_access = strdup(chanaccess);
922 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
924 cbind->channel_access = NULL;
925 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
932 void bind_botwise_set_bind_flags(int botid, int clientid, char *cmd, unsigned int flags) {
933 int bind_index = get_binds_index(cmd[0]);
934 struct cmd_binding *cbind;
935 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
936 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
937 cbind->flags |= flags;
943 struct cmd_binding *find_botwise_cmd_binding(int botid, int clientid, char *cmd) {
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) && strcmp(cbind->cmd, cmd) == 0) {
954 void bind_botwise_unbound_required_functions(int botid, int clientid) {
955 struct cmd_function *cmdfunc;
957 struct cmd_binding *cbind;
958 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
959 if((cmdfunc->flags & CMDFLAG_REQUIRED)) {
961 for(i = 0; i < 27; i++) {
962 for(cbind = cmd_binds[i]; cbind; cbind = cbind->next) {
963 if(cbind->botid == botid && (botid || clientid == cbind->clientid) && cbind->func == cmdfunc) {
971 if(!found && bind_botwise_cmd_to_function(botid, clientid, cmdfunc->name, cmdfunc)) {
972 cbind = find_botwise_cmd_binding(botid, clientid, cmdfunc->name);
973 cbind->flags |= CMDFLAG_TEMPONARY_BIND;
979 void register_command_alias(int botid, char *alias) {
980 struct cmd_bot_alias *botalias;
981 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
982 if(!stricmp(botalias->alias, alias))
985 botalias = malloc(sizeof(*botalias));
987 perror("malloc() failed");
990 botalias->botid = botid;
991 botalias->alias = strdup(alias);
992 botalias->next = bot_aliases;
993 bot_aliases = botalias;
996 struct cmd_binding *getAllBinds(struct cmd_binding *last) {
1001 bind_index = get_binds_index(last->cmd[0]) + 1;
1007 if(cmd_binds[bind_index])
1008 return cmd_binds[bind_index];
1010 } while(bind_index < 27);