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 command_check_user_cache {
39 struct ClientSocket *client, *textclient;
40 struct UserNode *user;
41 struct ChanNode *chan, *sent_chan;
44 char *message, *args_buffer;
45 struct cmd_binding *cbind;
48 static struct cmd_binding **cmd_binds;
49 static struct cmd_function *cmd_functions = NULL;
50 static struct trigger_callback *trigger_callbacks = NULL;
51 static struct ClientSocket *tmp_text_client;
53 static const struct default_language_entry msgtab[] = {
54 {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
55 {"MODCMD_CHAN_REQUIRED", "You must provide the name of a channel that exists and the bot is on."},
56 {"MODCMD_AUTH_REQUIRED", "You need to be authenticated with AuthServ to use this command."},
57 {"MODCMD_CHAN_SUSPENDED", "This channel is currently suspended."},
58 {"MODCMD_PRIVILEGED", "$b%s$b is a privileged command."}, /* {ARGS: "god"} */
59 {"MODCMD_PUBCMD", "Public commands in $b%s$b are restricted."}, /* {ARGS: "#TestChan"} */
60 {"MODCMD_ACCESS_DENIED", "Access denied."},
64 static int get_binds_index(char first_char) {
65 if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
66 return tolower(first_char) - 'a';
71 struct ClientSocket* get_prefered_bot(int botid) {
72 struct ClientSocket *client, *lowbot = NULL;
73 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
74 if(client->botid == botid) {
75 if((client->flags & SOCKET_FLAG_PREFERRED))
84 static char* get_channel_trigger(int botid, struct ChanNode *chan) {
85 struct trigger_cache *trigger;
86 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
87 if(trigger->botid == botid)
88 return trigger->trigger;
90 struct trigger_callback *cb;
91 for(cb = trigger_callbacks; cb; cb = cb->next) {
92 if(cb->botid == botid)
95 char triggerStr[TRIGGERLEN];
97 cb->func(chan, triggerStr);
99 strcpy(triggerStr, "+");
100 trigger = malloc(sizeof(*trigger));
102 perror("malloc() failed");
105 trigger->botid = botid;
106 trigger->trigger = (triggerStr[0] ? strdup(triggerStr) : NULL);
107 trigger->next = chan->trigger;
108 chan->trigger = trigger;
109 return trigger->trigger;
112 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);
114 static USERAUTH_CALLBACK(command_checked_auth) {
115 struct command_check_user_cache *cache = data;
116 tmp_text_client = cache->textclient;
117 handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
118 free(cache->message);
119 if(cache->args_buffer)
120 free(cache->args_buffer);
125 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
126 struct ChanNode *sent_chan = chan;
127 if(message[0] == '#') {
128 char *chanName = message;
129 message = strstr(message, " ");
133 struct ChanNode *chan2 = getChanByName(chanName);
137 message = strdup(message);
138 int bind_index = get_binds_index(message[0]);
139 char *args = strstr(message, " ");
140 char *args_buffer = NULL; //we need this to save a possible pointer to a allocation we need to free
145 struct cmd_binding *cbind;
146 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
147 if(cbind->botid == client->botid && stricmp(cbind->cmd, message) == 0) {
149 tmp_text_client = get_prefered_bot(client->botid);
150 //parse the arguments...
151 char *arga[MAXNUMPARAMS];
156 //skip leading spaces
160 if (argc >= MAXNUMPARAMS)
162 while (*args != ' ' && *args)
167 if(argc != 0 && argv[0][0] == '#' && !(cbind->func->flags & CMDFLAG_CHAN_PARAM)) {
168 struct ChanNode *chan2 = getChanByName(argv[0]);
175 if(cbind->paramcount) {
176 //userdefined parameters...
177 args_buffer = malloc(MAXLEN * 2 * sizeof(*args_buffer));
179 char *uargs[MAXNUMPARAMS];
184 for(i = 0; i < cbind->paramcount; i++) {
185 b = cbind->parameters[i];
188 if(b[strlen(b)-1] == '-') {
189 allargs = strlen(b)-1;
194 } else if(b[strlen(b)-1] == '+') {
195 allargs = strlen(b)-1;
206 uargs[uargc++] = args_buffer + args_pos;
208 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
209 } else if(allargs == 1) {
210 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
211 for(argi++; argi <= argc; argi++) {
212 uargs[uargc++] = args_buffer + args_pos;
213 args_pos += sprintf(args_buffer + args_pos, "%s", argv[argi-1]) + 1;
215 } else if(allargs == 2) {
216 for(;argi <= argc; argi++) {
217 args_pos += sprintf(args_buffer + args_pos, (allargs ? "%s" : " %s"), argv[argi-1]);
222 } else if((cbind->func->flags & CMDFLAG_EMPTY_ARGS)) {
223 uargs[uargc++] = args_buffer + args_pos;
224 args_buffer[args_pos++] = '\0';
226 } else if(!strcmp(b, "c")) {
227 uargs[uargc++] = args_buffer + args_pos;
228 args_pos += sprintf(args_buffer + args_pos, "%s", (chan ? chan->name : "")) + 1;
229 } else if(!strcmp(b, "n")) {
230 uargs[uargc++] = args_buffer + args_pos;
231 args_pos += sprintf(args_buffer + args_pos, "%s", user->nick) + 1;
234 uargs[uargc++] = args_buffer + args_pos;
235 args_pos += sprintf(args_buffer + args_pos, "%s", b) + 1;
241 if(argc < cbind->func->paramcount) {
242 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
245 if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
246 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
249 if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
251 struct command_check_user_cache *data = malloc(sizeof(*data));
252 char **temp_argv = malloc(argc*sizeof(*temp_argv));
253 if (!data || !temp_argv) {
254 perror("malloc() failed");
257 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
258 data->argv = temp_argv;
260 data->client = client;
263 data->sent_chan = sent_chan;
264 data->message = message;
265 data->args_buffer = args_buffer;
267 data->textclient = tmp_text_client;
268 get_userauth(user, command_checked_auth, data);
271 handle_command_async(client, user, chan, sent_chan, cbind, argv, argc);
280 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) {
284 int eventflags = (cbind->func->flags & (CMDFLAG_LOG | CMDFLAG_OPLOG));
285 if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
286 reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
289 if(sent_chan && sent_chan != chan) {
290 //check pubcmd of this channel
291 printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
293 if ((row = mysql_fetch_row(res)) != NULL) {
294 uaccess = getChannelAccess(user, sent_chan, 1);
295 if(row[0] && uaccess < atoi(row[0])) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
296 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
301 int global_access = ((cbind->flags & CMDFLAG_OVERRIDE_GLOBAL_ACCESS) ? cbind->global_access : cbind->func->global_access);
302 if(global_access > 0) {
303 int user_global_access = 0;
304 printf_mysql_query("SELECT `user_access` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
306 if ((row = mysql_fetch_row(res)) != NULL) {
307 user_global_access = atoi(row[0]);
309 if(user_global_access < global_access) {
310 if(!user_global_access)
311 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
313 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
317 if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
318 MYSQL_ROW defaults = NULL;
319 char access_list[256];
321 int access_count = 0;
323 char *str_a, *str_b = cbind->func->channel_access, *str_c;
324 if(cbind->flags & CMDFLAG_OVERRIDE_CHANNEL_ACCESS)
325 str_b = cbind->channel_access;
326 access_list[0] = '\0';
328 str_c = strdup(str_b);
330 while((str_a = str_b)) {
331 str_b = strstr(str_a, ",");
338 access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);
341 if(atoi(str_a) > minaccess)
342 minaccess = atoi(str_a);
347 if(!(chan->flags & CHANFLAG_REQUESTED_CHANINFO) || (sent_chan && sent_chan == chan) || access_count || minaccess) {
348 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = '%s'", access_list, escape_string(chan->name));
350 if ((row = mysql_fetch_row(res)) != NULL) {
351 chan->flags |= CHANFLAG_CHAN_REGISTERED;
352 chan->channel_id = atoi(row[0]);
353 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
354 uaccess = getChannelAccess(user, chan, 0);
355 if(uaccess < minaccess && isGodMode(user)) {
356 eventflags |= CMDFLAG_OPLOG;
357 } else if(uaccess < minaccess) {
359 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
362 if(!row[1] && !defaults) {
363 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
364 defaults = mysql_fetch_row(mysql_use());
366 if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
367 if(isGodMode(user)) {
368 eventflags |= CMDFLAG_OPLOG;
371 reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
376 for(i = 0; i < access_count; i++) {
377 if(!row[2+i] && !defaults) {
378 printf_mysql_query("SELECT `channel_id`, `channel_pubcmd` %s FROM `channels` WHERE `channel_name` = 'defaults'", access_list);
379 defaults = mysql_fetch_row(mysql_use());
381 if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
382 if(isGodMode(user)) {
383 eventflags |= CMDFLAG_OPLOG;
385 reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
392 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
394 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
395 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
398 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);
400 if ((row = mysql_fetch_row(res)) == NULL) {
401 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
403 } else if(!strcmp(row[1], "1")) {
404 reply(tmp_text_client, user, "MODCMD_CHAN_SUSPENDED");
408 if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
409 reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
412 struct Event *event = createEvent(client, user, chan, cbind->func->name, argv, argc, eventflags);
413 cbind->func->func(client, user, chan, argv, argc, event);
416 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
419 struct ClientSocket *client;
421 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
422 if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
423 FD_SET(client->botid, &fds);
424 trigger = get_channel_trigger(client->botid, chan);
425 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
426 handle_command(client, user, chan, message + strlen(trigger));
430 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
431 if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
432 FD_SET(client->botid, &fds);
433 trigger = get_channel_trigger(client->botid, chan);
434 if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
435 handle_command(client, user, chan, message + strlen(trigger));
441 static void got_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
442 struct ClientSocket *client;
443 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
444 if(client->user == target) {
445 handle_command(client, user, NULL, message);
450 int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, char *channel_access, int global_access, unsigned int flags) {
451 struct cmd_function *cmdfunc;
452 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
453 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, name) == 0)
456 cmdfunc = malloc(sizeof(*cmdfunc));
458 perror("malloc() failed");
461 cmdfunc->botid = botid;
462 cmdfunc->name = strdup(name);
463 cmdfunc->func = func;
464 cmdfunc->flags = flags;
465 cmdfunc->paramcount = paramcount;
466 cmdfunc->channel_access = channel_access;
467 cmdfunc->global_access = global_access;
468 cmdfunc->next = cmd_functions;
469 cmd_functions = cmdfunc;
473 int set_trigger_callback(int botid, trigger_callback_t *func) {
474 static struct trigger_callback *cb = NULL;
475 for(cb = trigger_callbacks; cb; cb = cb->next) {
476 if(cb->botid == botid)
480 cb = malloc(sizeof(*cb));
482 perror("malloc() failed");
486 cb->next = trigger_callbacks;
487 trigger_callbacks = cb;
493 int changeChannelTrigger(int botid, struct ChanNode *chan, char *new_trigger) {
494 struct trigger_cache *trigger;
495 for(trigger = chan->trigger; trigger; trigger = trigger->next) {
496 if(trigger->botid == botid) {
497 free(trigger->trigger);
498 trigger->trigger = strdup(new_trigger);
505 int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
506 int bind_index = get_binds_index(cmd[0]);
507 struct cmd_binding *cbind;
508 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
509 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
512 cbind = malloc(sizeof(*cbind));
514 perror("malloc() failed");
517 cbind->botid = botid;
518 cbind->cmd = strdup(cmd);
520 cbind->paramcount = 0;
521 cbind->global_access = 0;
522 cbind->channel_access = NULL;
524 cbind->next = cmd_binds[bind_index];
525 cmd_binds[bind_index] = cbind;
529 int bind_cmd_to_command(int botid, char *cmd, char *func) {
530 struct cmd_function *cmdfunc;
531 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
532 if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
535 if(!cmdfunc) return 0;
536 int bind_index = get_binds_index(cmd[0]);
537 struct cmd_binding *cbind;
538 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
539 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0)
542 cbind = malloc(sizeof(*cbind));
544 perror("malloc() failed");
547 cbind->botid = botid;
548 cbind->cmd = strdup(cmd);
549 cbind->func = cmdfunc;
550 cbind->next = cmd_binds[bind_index];
551 cbind->paramcount = 0;
552 cbind->global_access = 0;
553 cbind->channel_access = NULL;
555 cmd_binds[bind_index] = cbind;
559 int unbind_cmd(int botid, char *cmd) {
560 int bind_index = get_binds_index(cmd[0]);
561 struct cmd_binding *cbind, *last = NULL;
562 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
563 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
565 last->next = cbind->next;
567 cmd_binds[bind_index] = cbind->next;
569 if(cbind->paramcount) {
571 for(i = 0; i < cbind->paramcount; i++)
572 free(cbind->parameters[i]);
582 struct cmd_function *find_cmd_function(int botid, char *name) {
583 struct cmd_function *cmdfunc;
584 for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
585 if(cmdfunc->botid == botid && stricmp(cmdfunc->name, name) == 0)
591 struct ClientSocket *getTextBot() {
592 return tmp_text_client;
596 cmd_binds = calloc(27, sizeof(*cmd_binds));
597 bind_chanmsg(got_chanmsg);
598 bind_privmsg(got_privmsg);
599 register_default_language_table(msgtab);
604 for(i = 0; i < 27; i++) {
605 struct cmd_binding *cbind, *next;
606 for(cbind = cmd_binds[i]; cbind; cbind = next) {
609 if(cbind->paramcount) {
611 for(j = 0; j < cbind->paramcount; j++)
612 free(cbind->parameters[j]);
614 if(cbind->channel_access)
615 free(cbind->channel_access);
620 struct cmd_function *cmdfunct, *next;
621 for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
622 next = cmdfunct->next;
623 free(cmdfunct->name);
626 struct trigger_callback *cb, *next_cb;
627 for(cb = trigger_callbacks; cb; cb = next_cb) {
631 cmd_functions = NULL;
632 trigger_callbacks = NULL;
635 void bind_set_parameters(int botid, char *cmd, char *parameters) {
636 int bind_index = get_binds_index(cmd[0]);
637 struct cmd_binding *cbind;
638 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
639 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
640 if(cbind->paramcount) {
642 for(i = 0; i < cbind->paramcount; i++)
643 free(cbind->parameters[i]);
644 cbind->paramcount = 0;
646 char *a, *b = parameters;
650 cbind->parameters[cbind->paramcount++] = strdup(b);
658 void bind_set_global_access(int botid, char *cmd, int gaccess) {
659 int bind_index = get_binds_index(cmd[0]);
660 struct cmd_binding *cbind;
661 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
662 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
664 cbind->global_access = gaccess;
665 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
667 cbind->flags &= ~CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
674 void bind_set_channel_access(int botid, char *cmd, char *chanaccess) {
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(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
679 if(cbind->channel_access)
680 free(cbind->channel_access);
682 cbind->channel_access = strdup(chanaccess);
683 cbind->flags |= CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
685 cbind->channel_access = NULL;
686 cbind->flags &= ~CMDFLAG_OVERRIDE_CHANNEL_ACCESS;
693 struct cmd_binding *find_cmd_binding(int botid, char *cmd) {
694 int bind_index = get_binds_index(cmd[0]);
695 struct cmd_binding *cbind;
696 for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
697 if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {