X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=src%2Fmodcmd.c;h=8ec2517ef9c640bcbeb3f909a306980c02800d94;hp=7ba0b939d6cae97eba0645fade114475474accfd;hb=HEAD;hpb=706e48b1e666054030c491d864f740071e390038 diff --git a/src/modcmd.c b/src/modcmd.c index 7ba0b93..8ec2517 100644 --- a/src/modcmd.c +++ b/src/modcmd.c @@ -1,4 +1,4 @@ -/* modcmd.c - NeonServ v5.3 +/* modcmd.c - NeonServ v5.6 * Copyright (C) 2011-2012 Philipp Kreil (pk910) * * This program is free software: you can redistribute it and/or modify @@ -27,6 +27,8 @@ #include "mysqlConn.h" #include "DBHelper.h" #include "EventLogger.h" +#include "tools.h" +#include "log.h" struct trigger_callback { int botid; @@ -57,7 +59,6 @@ static struct cmd_binding **cmd_binds; static struct cmd_function *cmd_functions = NULL; static struct trigger_callback *trigger_callbacks = NULL; static struct cmd_bot_alias *bot_aliases = NULL; -static struct ClientSocket *tmp_text_client; static int total_triggered = 0; int statistics_commands = 0; @@ -113,7 +114,7 @@ static char* get_channel_trigger(int botid, int clientid, struct ChanNode *chan) triggerStr[0] = '\0'; trigger = malloc(sizeof(*trigger)); if (!trigger) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return 0; } trigger->botid = botid; @@ -124,13 +125,12 @@ static char* get_channel_trigger(int botid, int clientid, struct ChanNode *chan) return trigger->trigger; } -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); +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); static USERAUTH_CALLBACK(command_checked_auth) { struct command_check_user_cache *cache = data; - tmp_text_client = cache->textclient; if(user) - handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc); + handle_command_async(cache->client, cache->textclient, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc); free(cache->message); if(cache->args_buffer) free(cache->args_buffer); @@ -143,7 +143,7 @@ static CMD_BIND(modcmd_linker) { //just empty } -static struct cmd_binding *modcmd_linker_command(struct ClientSocket *client, struct UserNode *user, struct cmd_binding *cbind, int bind_index, char **args_ptr) { +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) { //links subcommands char command[MAXLEN]; struct cmd_binding *parent_bind = cbind; @@ -162,7 +162,7 @@ static struct cmd_binding *modcmd_linker_command(struct ClientSocket *client, st } *args_ptr = args; if(cbind && cbind->func->func == modcmd_linker) { - return modcmd_linker_command(client, user, cbind, bind_index, args_ptr); + return modcmd_linker_command(client, textclient, user, cbind, bind_index, args_ptr); } return cbind; } else { @@ -176,11 +176,41 @@ static struct cmd_binding *modcmd_linker_command(struct ClientSocket *client, st subcompos += sprintf(subcommands + subcompos, (subcompos ? ", %s" : "%s"), cbind->cmd + commandlen); } } - reply(tmp_text_client, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "none")); + reply(textclient, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "none")); return NULL; } } +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) { + //links subcommands + char command[MAXLEN]; + struct cmd_binding *parent_bind = cbind; + char *args = *args_ptr; + if(args) { + char *subcmd = args; + args = strchr(args, ' '); + if(args) { + *args = '\0'; + args++; + } + sprintf(command, "%s %s", parent_bind->cmd, subcmd); + if(args) + args[-1] = ' '; + for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) { + if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0) + break; + } + if(cbind) { + *args_ptr = args; + if(cbind->func->func == modcmd_linker) + parent_bind = modcmd_linker_command(client, textclient, user, cbind, bind_index, args_ptr); + else + parent_bind = cbind; + } + } + return parent_bind; +} + static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) { struct ChanNode *sent_chan = chan; if(message[0] == '#') { @@ -195,7 +225,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s } message = strdup(message); int bind_index = get_binds_index(message[0]); - char *args = strstr(message, " "); + char *args = strchr(message, ' '); char *args_buffer = NULL; //we need this to save a possible pointer to a allocation we need to free if(args) { *args = '\0'; @@ -207,13 +237,13 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, message) == 0) { found_cmd = 1; //get a text bot - tmp_text_client = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0)); + struct ClientSocket *textclient = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0)); if(cbind->func->func == modcmd_linker) { - cbind = modcmd_linker_command(client, user, cbind, bind_index, &args); + cbind = modcmd_linker_command(client, textclient, user, cbind, bind_index, &args); if(cbind == NULL) break; - } - if(statistics_enabled) - statistics_commands++; + } else if(cbind->flags & CMDFLAG_SUB_LINKER) + cbind = modcmd_sub_linker_command(client, textclient, user, cbind, bind_index, &args); + statistics_commands++; total_triggered++; cbind->triggered++; if((BIND_FLAGS(cbind) & CMDFLAG_FUNCMD)) { @@ -343,11 +373,11 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s } } if(argc < cbind->func->paramcount) { - reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT"); + reply(textclient, user, "MODCMD_LESS_PARAM_COUNT"); break; } if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_CHAN) && !chan) { - reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED"); + reply(textclient, user, "MODCMD_CHAN_REQUIRED"); break; } if(((BIND_FLAGS(cbind) & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) { @@ -355,7 +385,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s struct command_check_user_cache *data = malloc(sizeof(*data)); char **temp_argv = malloc(argc*sizeof(*temp_argv)); if (!data || !temp_argv) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); break; } memcpy(temp_argv, argv, argc*sizeof(*temp_argv)); @@ -368,11 +398,11 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s data->message = message; data->args_buffer = args_buffer; data->cbind = cbind; - data->textclient = tmp_text_client; - get_userauth(user, command_checked_auth, data); + data->textclient = textclient; + get_userauth(user, 0, command_checked_auth, data); return; } else - handle_command_async(client, user, chan, sent_chan, cbind, argv, argc); + handle_command_async(client, textclient, user, chan, sent_chan, cbind, argv, argc); break; } } @@ -383,14 +413,14 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s free(args_buffer); } -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) { +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) { MYSQL_RES *res; MYSQL_ROW row; int uaccess; char requested_uaccess = 0; int eventflags = (BIND_FLAGS(cbind) & (CMDFLAG_LOG | CMDFLAG_OPLOG)); if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) { - reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED"); + reply(textclient, user, "MODCMD_AUTH_REQUIRED"); return; } if(chan && sent_chan != chan && (BIND_FLAGS(cbind) & CMDFLAG_NO_CROSSCHAN) && !isUserOnChan(user, chan)) { @@ -413,7 +443,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u if(isGodMode(user)) { eventflags |= CMDFLAG_OPLOG; } else { - reply(tmp_text_client, user, "MODCMD_CROSSCHAN", chan->name); + reply(textclient, user, "MODCMD_CROSSCHAN", chan->name); return; } } @@ -426,7 +456,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u if ((row = mysql_fetch_row(res)) != NULL) { int saccess = getChannelAccess(user, sent_chan); if(row[0] && saccess < atoi(row[0]) && !isGodMode(user)) { //NOTE: HARDCODED DEFAULT: pubcmd = 0 - reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name); + reply(textclient, user, "MODCMD_PUBCMD", sent_chan->name); return; } } @@ -441,9 +471,9 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u } if(user_global_access < global_access) { if(!user_global_access) - reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd); + reply(textclient, user, "MODCMD_PRIVILEGED", cbind->cmd); else - reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED"); + reply(textclient, user, "MODCMD_ACCESS_DENIED"); return; } } @@ -499,7 +529,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u eventflags |= CMDFLAG_OPLOG; } else if(uaccess < minaccess) { //ACCESS DENIED - reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED"); + reply(textclient, user, "MODCMD_ACCESS_DENIED"); return; } if(!row[1] && !defaults) { @@ -511,7 +541,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u eventflags |= CMDFLAG_OPLOG; } else { //PUBCMD - reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name); + reply(textclient, user, "MODCMD_PUBCMD", chan->name); return; } } @@ -525,7 +555,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u if(isGodMode(user)) { eventflags |= CMDFLAG_OPLOG; } else { - reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED"); + reply(textclient, user, "MODCMD_ACCESS_DENIED"); return; } } @@ -535,25 +565,25 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u chan->flags |= CHANFLAG_REQUESTED_CHANINFO; } if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) { - reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED"); + reply(textclient, user, "MODCMD_CHAN_REQUIRED"); return; } 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); res = mysql_use(); if ((row = mysql_fetch_row(res)) == NULL) { - reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED"); + reply(textclient, user, "MODCMD_CHAN_REQUIRED"); return; } else if(!strcmp(row[1], "1")) { - reply(tmp_text_client, user, "MODCMD_CHAN_SUSPENDED"); + reply(textclient, user, "MODCMD_CHAN_SUSPENDED"); return; } } if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) { - reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd); + reply(textclient, user, "MODCMD_PRIVILEGED", cbind->cmd); return; } struct Event *event = createEvent(client, user, chan, cbind, argv, argc, eventflags); - cbind->func->func(client, user, chan, argv, argc, event); + cbind->func->func(client, textclient, user, chan, argv, argc, event); } static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) { @@ -600,12 +630,12 @@ static void got_privmsg(struct UserNode *user, struct UserNode *target, char *me 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) { struct cmd_function *cmdfunc; for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) { - if((cmdfunc->botid == botid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, name) == 0) + if((cmdfunc->botid == botid || cmdfunc->botid == 0) && !stricmp(cmdfunc->name, name)) return 0; } cmdfunc = malloc(sizeof(*cmdfunc)); if (!cmdfunc) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return 0; } cmdfunc->botid = botid; @@ -630,7 +660,7 @@ int set_trigger_callback(int botid, int module_id, trigger_callback_t *func) { if(!cb) { cb = malloc(sizeof(*cb)); if (!cb) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return 0; } cb->botid = botid; @@ -679,12 +709,12 @@ int bind_botwise_cmd_to_function(int botid, int clientid, char *cmd, struct cmd_ int bind_index = get_binds_index(cmd[0]); struct cmd_binding *cbind; for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) { - if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && strcmp(cbind->cmd, cmd) == 0) + if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && !stricmp(cbind->cmd, cmd)) return 0; } cbind = malloc(sizeof(*cbind)); if (!cbind) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return 0; } cbind->botid = botid; @@ -718,19 +748,19 @@ int bind_botwise_cmd_to_command(int botid, int clientid, char *cmd, char *func) func = c+1; } for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) { - if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, func) == 0) + if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && !stricmp(cmdfunc->name, func)) break; } if(!cmdfunc) return 0; int bind_index = get_binds_index(cmd[0]); struct cmd_binding *cbind; for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) { - if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && strcmp(cbind->cmd, cmd) == 0) + if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && !stricmp(cbind->cmd, cmd)) return 0; } cbind = malloc(sizeof(*cbind)); if (!cbind) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return 0; } cbind->botid = botid; @@ -751,7 +781,7 @@ int unbind_botwise_cmd(int botid, int clientid, char *cmd) { int bind_index = get_binds_index(cmd[0]); struct cmd_binding *cbind, *last = NULL; for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) { - if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) { + if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) { if(last) last->next = cbind->next; else @@ -821,16 +851,12 @@ struct cmd_function *find_cmd_function(int botid, char *name) { return cmdfunc; } -struct ClientSocket *getTextBot() { - return tmp_text_client; -} - void init_modcmd() { cmd_binds = calloc(27, sizeof(*cmd_binds)); - bind_chanmsg(got_chanmsg); - bind_privmsg(got_privmsg); + bind_chanmsg(got_chanmsg, 0); + bind_privmsg(got_privmsg, 0); register_default_language_table(msgtab); - register_command(0, "linker", modcmd_linker, 0, 0, 0, 0); //fake command for subcommands + register_command(0, "linker", 0, modcmd_linker, 0, 0, 0, 0); //fake command for subcommands } void free_modcmd() { @@ -877,7 +903,7 @@ void bind_botwise_set_parameters(int botid, int clientid, char *cmd, char *param int bind_index = get_binds_index(cmd[0]); struct cmd_binding *cbind; for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) { - if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) { + if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) { if(cbind->paramcount) { int i; for(i = 0; i < cbind->paramcount; i++) @@ -902,7 +928,7 @@ void bind_botwise_set_global_access(int botid, int clientid, char *cmd, int gacc int bind_index = get_binds_index(cmd[0]); struct cmd_binding *cbind; for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) { - if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) { + if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) { if(gaccess > -1) { cbind->global_access = gaccess; cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS; @@ -918,7 +944,7 @@ void bind_botwise_set_channel_access(int botid, int clientid, char *cmd, char *c int bind_index = get_binds_index(cmd[0]); struct cmd_binding *cbind; for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) { - if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) { + if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) { if(cbind->channel_access) free(cbind->channel_access); if(chanaccess) { @@ -937,7 +963,7 @@ void bind_botwise_set_bind_flags(int botid, int clientid, char *cmd, unsigned in int bind_index = get_binds_index(cmd[0]); struct cmd_binding *cbind; for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) { - if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) { + if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) { cbind->flags |= flags; return; } @@ -948,7 +974,7 @@ struct cmd_binding *find_botwise_cmd_binding(int botid, int clientid, char *cmd) int bind_index = get_binds_index(cmd[0]); struct cmd_binding *cbind; for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) { - if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) { + if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) { return cbind; } } @@ -988,7 +1014,7 @@ void register_command_alias(int botid, char *alias) { } botalias = malloc(sizeof(*botalias)); if (!botalias) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return; } botalias->botid = botid; @@ -1052,7 +1078,7 @@ void unregister_module_commands(int module_id) { } else prevfunct = cmdfunct; } - static struct trigger_callback *cb, *prevcb = NULL, *nextcb; + struct trigger_callback *cb, *prevcb = NULL, *nextcb; for(cb = trigger_callbacks; cb; cb = nextcb) { nextcb = cb->next; if(cb->module_id == module_id) {