From 5a9d04e79a4d27880eb5a588cbc50e00a4cd26aa Mon Sep 17 00:00:00 2001 From: pk910 Date: Sun, 25 Dec 2011 21:49:18 +0100 Subject: [PATCH] added possibility to edit command flags (cmd_modcmd) --- src/bot_DummyServ.c | 4 ++- src/bot_NeonHelp.c | 4 ++- src/bot_NeonServ.c | 5 ++- src/bot_NeonSpam.c | 4 ++- src/bots.c | 4 ++- src/cmd_global_modcmd.c | 78 +++++++++++++++++++++++++++++++++++++++++ src/modcmd.c | 31 ++++++++++------ src/modcmd.h | 5 +++ 8 files changed, 120 insertions(+), 15 deletions(-) diff --git a/src/bot_DummyServ.c b/src/bot_DummyServ.c index f6fc7ac..dc95739 100644 --- a/src/bot_DummyServ.c +++ b/src/bot_DummyServ.c @@ -82,7 +82,7 @@ static void start_bots() { connect_socket(client); } - printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID); + printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID); res2 = mysql_use(); while ((row = mysql_fetch_row(res2)) != NULL) { if(bind_cmd_to_command(BOTID, row[0], row[1])) { @@ -95,6 +95,8 @@ static void start_bots() { if(row[4]) { bind_set_channel_access(BOTID, row[0], row[4]); } + if(strcmp(row[5], "0")) + bind_set_bind_flags(BOTID, row[0], atoi(row[5])); } } bind_unbound_required_functions(BOTID); diff --git a/src/bot_NeonHelp.c b/src/bot_NeonHelp.c index 3473d3c..ee8525f 100644 --- a/src/bot_NeonHelp.c +++ b/src/bot_NeonHelp.c @@ -84,7 +84,7 @@ static void start_bots() { connect_socket(client); } - printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID); + printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID); res2 = mysql_use(); while ((row = mysql_fetch_row(res2)) != NULL) { if(bind_cmd_to_command(BOTID, row[0], row[1])) { @@ -97,6 +97,8 @@ static void start_bots() { if(row[4]) { bind_set_channel_access(BOTID, row[0], row[4]); } + if(strcmp(row[5], "0")) + bind_set_bind_flags(BOTID, row[0], atoi(row[5])); } } bind_unbound_required_functions(BOTID); diff --git a/src/bot_NeonServ.c b/src/bot_NeonServ.c index 9ec41e4..32de3d6 100644 --- a/src/bot_NeonServ.c +++ b/src/bot_NeonServ.c @@ -364,6 +364,7 @@ static const struct default_language_entry msgtab[] = { {"NS_MODCMD_SETTING", "$b%s$b is an unknown modcmd setting."}, /* {ARGS: "strangeSetting"} */ {"NS_MODCMD_HEADER", "$bSettings for command %s:$b"}, /* {ARGS: "access"} */ {"NS_MODCMD_OUTRANKED", "$b%s$b outranks you. (required access: %d)"}, /* {ARGS: "die", 1000} */ + {"NS_MODCMD_STATIC_FLAG", "This Flag is added statically. It can't be modified manually."}, {NULL, NULL} }; @@ -449,7 +450,7 @@ static void start_bots() { connect_socket(client); } - printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID); + printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID); res2 = mysql_use(); while ((row = mysql_fetch_row(res2)) != NULL) { if(bind_cmd_to_command(BOTID, row[0], row[1])) { @@ -462,6 +463,8 @@ static void start_bots() { if(row[4]) { bind_set_channel_access(BOTID, row[0], row[4]); } + if(strcmp(row[5], "0")) + bind_set_bind_flags(BOTID, row[0], atoi(row[5])); } } bind_unbound_required_functions(BOTID); diff --git a/src/bot_NeonSpam.c b/src/bot_NeonSpam.c index 0ba3754..6111641 100644 --- a/src/bot_NeonSpam.c +++ b/src/bot_NeonSpam.c @@ -167,7 +167,7 @@ static void start_bots() { connect_socket(client); } - printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID); + printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID); res2 = mysql_use(); while ((row = mysql_fetch_row(res2)) != NULL) { if(bind_cmd_to_command(BOTID, row[0], row[1])) { @@ -180,6 +180,8 @@ static void start_bots() { if(row[4]) { bind_set_channel_access(BOTID, row[0], row[4]); } + if(strcmp(row[5], "0")) + bind_set_bind_flags(BOTID, row[0], atoi(row[5])); } } bind_unbound_required_functions(BOTID); diff --git a/src/bots.c b/src/bots.c index 9cc84d2..d239dff 100644 --- a/src/bots.c +++ b/src/bots.c @@ -56,7 +56,7 @@ static void start_zero_bots() { client->clientid = atoi(row[7]); connect_socket(client); - printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid); + printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid); res2 = mysql_use(); while ((row = mysql_fetch_row(res2)) != NULL) { if(bind_botwise_cmd_to_command(0, client->clientid, row[0], row[1])) { @@ -69,6 +69,8 @@ static void start_zero_bots() { if(row[4]) { bind_botwise_set_channel_access(0, client->clientid, row[0], row[4]); } + if(strcmp(row[5], "0")) + bind_botwise_set_bind_flags(0, client->clientid, row[0], atoi(row[5])); } } bind_botwise_unbound_required_functions(0, client->clientid); diff --git a/src/cmd_global_modcmd.c b/src/cmd_global_modcmd.c index 7a3b80b..a5f36ad 100644 --- a/src/cmd_global_modcmd.c +++ b/src/cmd_global_modcmd.c @@ -24,6 +24,7 @@ */ static int global_cmd_modcmd_params(struct UserNode *user, struct cmd_binding *cbind, char *value); +static int global_cmd_modcmd_flags(struct UserNode *user, struct cmd_binding *cbind, char *value); static int global_cmd_modcmd_caccess(struct UserNode *user, struct cmd_binding *cbind, char *value); static int global_cmd_modcmd_oaccess(struct UserNode *user, struct cmd_binding *cbind, char *value); @@ -56,6 +57,7 @@ CMD_BIND(global_cmd_modcmd) { if(!stricmp(argv[1], "caccess")) log_event = global_cmd_modcmd_caccess(user, cbind, value); else if(!stricmp(argv[1], "oaccess")) log_event = global_cmd_modcmd_oaccess(user, cbind, value); else if(!stricmp(argv[1], "parameters")) log_event = global_cmd_modcmd_params(user, cbind, value); + else if(!stricmp(argv[1], "flags")) log_event = global_cmd_modcmd_flags(user, cbind, value); else { reply(getTextBot(), user, "NS_MODCMD_SETTING", argv[1]); } @@ -67,6 +69,7 @@ CMD_BIND(global_cmd_modcmd) { global_cmd_modcmd_params(user, cbind, NULL); global_cmd_modcmd_caccess(user, cbind, NULL); global_cmd_modcmd_oaccess(user, cbind, NULL); + global_cmd_modcmd_flags(user, cbind, NULL); } } @@ -95,6 +98,81 @@ static int global_cmd_modcmd_params(struct UserNode *user, struct cmd_binding *c return ret; } +static const struct { + const char *name; + unsigned int flag; +} global_cmd_modcmd_show_flags[] = { + {"REQUIRE_CHAN", CMDFLAG_REQUIRE_CHAN}, //The command requires a valid channel (at least one bot in it) + {"REQUIRE_AUTH", CMDFLAG_REQUIRE_AUTH}, //The command requires the user to be authenticated + {"REQUIRE_GOD", CMDFLAG_REQUIRE_GOD}, //The command requires the user to have security override enabled + {"REQUIRE_REGISTERED", CMDFLAG_REGISTERED_CHAN}, //The command requires the channel to be registered (database entry) + {"CHECK_AUTH", CMDFLAG_CHECK_AUTH}, //WHO the user if no auth is known, yet + {"CHANNEL_ARGS", CMDFLAG_CHAN_PARAM}, //don't interpret channel arguments as channel - just pass them as a string + {"LOG", CMDFLAG_LOG}, + {"OPLOG", CMDFLAG_OPLOG}, + {"FUNCMD", CMDFLAG_FUNCMD}, + {"ESCAPED_ARGS", CMDFLAG_ESCAPE_ARGS}, //allows arguments to be escaped ("a\ b" = "a b" as one argument) + {NULL, 0} +}; + +static int global_cmd_modcmd_flags(struct UserNode *user, struct cmd_binding *cbind, char *value) { + char flags[MAXLEN]; + int flagpos = 0; + int ret = 0; + int i = 0; + while(global_cmd_modcmd_show_flags[i].name) { + if(cbind->func->flags & global_cmd_modcmd_show_flags[i].flag) { + flagpos += sprintf(flags + flagpos, (flagpos ? " %s" : "\00314%s"), global_cmd_modcmd_show_flags[i].name); + } + i++; + } + if(flagpos) + flags[flagpos++] = '\003'; + if(value) { + int add = 1; + unsigned int current_flag = 0; + if(value[0] == '+') { + value++; + } else if(value[0] == '-') { + add = 0; + value++; + } + if(*value) { + i = 0; + while(global_cmd_modcmd_show_flags[i].name) { + if(!stricmp(global_cmd_modcmd_show_flags[i].name, value)) { + current_flag = global_cmd_modcmd_show_flags[i].flag; + break; + } + i++; + } + } + if(cbind->func->flags & current_flag) { + reply(getTextBot(), user, "NS_MODCMD_STATIC_FLAG"); + return 0; + } + if(add) + cbind->flags |= current_flag; + else + cbind->flags &= ~current_flag; + if(cbind->botid == 0) + printf_mysql_query("UPDATE `bot_binds` SET `flags` = '%u' WHERE `botclass` = '0' AND `botid` = '%d' AND `command` = '%s'", cbind->flags, cbind->clientid, escape_string(cbind->cmd)); + else + printf_mysql_query("UPDATE `bot_binds` SET `flags` = '%u' WHERE `botclass` = '%d' AND `command` = '%s'", cbind->flags, cbind->botid, escape_string(cbind->cmd)); + ret = 1; + } + i = 0; + while(global_cmd_modcmd_show_flags[i].name) { + if(cbind->flags & global_cmd_modcmd_show_flags[i].flag) { + flagpos += sprintf(flags + flagpos, (flagpos ? " %s" : "%s"), global_cmd_modcmd_show_flags[i].name); + } + i++; + } + flags[flagpos] = '\0'; + reply(getTextBot(), user, "\002FLAGS \002 %s", flags); + return ret; +} + static int global_cmd_modcmd_caccess(struct UserNode *user, struct cmd_binding *cbind, char *value) { char caccess[MAXLEN]; int ret = 0; diff --git a/src/modcmd.c b/src/modcmd.c index 9af1321..03b24d8 100644 --- a/src/modcmd.c +++ b/src/modcmd.c @@ -204,7 +204,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s statistics_commands++; total_triggered++; cbind->triggered++; - if((cbind->func->flags & CMDFLAG_FUNCMD)) { + if((BIND_FLAGS(cbind) & CMDFLAG_FUNCMD)) { if(!sent_chan) break; chan = sent_chan; @@ -224,7 +224,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s if (argc >= MAXNUMPARAMS) break; while ((escape || *args != ' ') && *args) { - if((cbind->func->flags & CMDFLAG_ESCAPE_ARGS) && *args == '\\') { + if((BIND_FLAGS(cbind) & CMDFLAG_ESCAPE_ARGS) && *args == '\\') { escape = 1; offset++; } else if(escape) @@ -242,7 +242,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s } } argv = arga; - if(argc != 0 && argv[0][0] == '#' && !(cbind->func->flags & CMDFLAG_CHAN_PARAM)) { + if(argc != 0 && argv[0][0] == '#' && !(BIND_FLAGS(cbind) & CMDFLAG_CHAN_PARAM)) { struct ChanNode *chan2 = getChanByName(argv[0]); if(chan2) { argv += 1; @@ -307,7 +307,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s } args_pos++; } - } else if((cbind->func->flags & CMDFLAG_EMPTY_ARGS)) { + } else if((BIND_FLAGS(cbind) & CMDFLAG_EMPTY_ARGS)) { uargs[uargc++] = args_buffer + args_pos; args_buffer[args_pos++] = '\0'; } else if(c) { @@ -334,11 +334,11 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT"); break; } - if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) { + if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_CHAN) && !chan) { reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED"); break; } - if(((cbind->func->flags & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) { + if(((BIND_FLAGS(cbind) & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) { //check auth... struct command_check_user_cache *data = malloc(sizeof(*data)); char **temp_argv = malloc(argc*sizeof(*temp_argv)); @@ -376,8 +376,8 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u MYSQL_ROW row; int uaccess; char requested_uaccess = 0; - int eventflags = (cbind->func->flags & (CMDFLAG_LOG | CMDFLAG_OPLOG)); - if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) { + 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"); return; } @@ -431,7 +431,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u return; } } - if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) { + if((BIND_FLAGS(cbind) & CMDFLAG_REGISTERED_CHAN)) { MYSQL_ROW defaults = NULL; char access_list[256]; int access_pos = 0; @@ -522,7 +522,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u return; } } - if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) { + if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) { reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd); return; } @@ -905,6 +905,17 @@ void bind_botwise_set_channel_access(int botid, int clientid, char *cmd, char *c } } +void bind_botwise_set_bind_flags(int botid, int clientid, char *cmd, unsigned int flags) { + 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) { + cbind->flags |= flags; + return; + } + } +} + 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; diff --git a/src/modcmd.h b/src/modcmd.h index 2be6f3d..9bb799c 100644 --- a/src/modcmd.h +++ b/src/modcmd.h @@ -72,6 +72,8 @@ struct cmd_binding { struct cmd_binding *next; }; +#define BIND_FLAGS(BIND) (BIND->flags | BIND->func->flags) + struct trigger_cache { int botid; int clientid; @@ -116,6 +118,9 @@ void bind_botwise_set_global_access(int botid, int clientid, char *cmd, int gacc #define bind_set_channel_access(BOTID,CMD,CACCESS) bind_botwise_set_channel_access(BOTID, 0, CMD, CACCESS) void bind_botwise_set_channel_access(int botid, int clientid, char *cmd, char *chanaccess); +#define bind_set_bind_flags(BOTID,CMD,FLAGS) bind_botwise_set_bind_flags(BOTID, 0, CMD, FLAGS) +void bind_botwise_set_bind_flags(int botid, int clientid, char *cmd, unsigned int flags); + #define find_cmd_binding(BOTID,CMD) find_botwise_cmd_binding(BOTID, 0, CMD) struct cmd_binding *find_botwise_cmd_binding(int botid, int clientid, char *cmd); -- 2.20.1