From a04f2a3bad9a1d96cc69e4bf29cd9d9d6581d4be Mon Sep 17 00:00:00 2001 From: pk910 Date: Thu, 22 Dec 2011 01:01:01 +0100 Subject: [PATCH] added cmd_addbot and cmd_delbot for dynamic bot management --- Makefile.am | 2 ++ src/bot_NeonServ.c | 4 ++++ src/cmd_global.h | 2 ++ src/cmd_global_addbot.c | 43 +++++++++++++++++++++++++++++++++++++++++ src/cmd_global_delbot.c | 43 +++++++++++++++++++++++++++++++++++++++++ src/commands.c | 2 ++ 6 files changed, 96 insertions(+) create mode 100644 src/cmd_global_addbot.c create mode 100644 src/cmd_global_delbot.c diff --git a/Makefile.am b/Makefile.am index 094eb2c..40af57e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -121,6 +121,8 @@ neonserv_SOURCES = src/version.c \ src/cmd_global_die.c \ src/cmd_neonserv_nicklist.c \ src/cmd_global_setbot.c \ + src/cmd_global_addbot.c \ + src/cmd_global_delbot.c \ src/cmd_funcmds.c \ src/ConfigParser.c diff --git a/src/bot_NeonServ.c b/src/bot_NeonServ.c index a55e9ad..0421fdd 100644 --- a/src/bot_NeonServ.c +++ b/src/bot_NeonServ.c @@ -356,6 +356,10 @@ static const struct default_language_entry msgtab[] = { {"NS_SETBOT_PRIORITY_INVALID", "`%s` is an invalid priority value."}, /* {ARGS: "-1"} */ {"NS_SETBOT_TRIGGER_INVALID", "`%s` is an invalid bot trigger."}, /* {ARGS: "tooLongTrigger"} */ {"NS_SETBOT_TRIGGER_NOTE", "Please note: This Setting will only affect new channels."}, + {"NS_ADDBOT_EXISTING", "A bot with nick %s does already exist."}, /* {ARGS: "NeonServ"} */ + {"NS_ADDBOT_DONE", "Added %s with BotID $b%d$b."}, /* {ARGS: "NeonServ", 2} */ + {"NS_DELBOT_NOT_FOUND", "Bot with BotID / nick $b%s$b not found."}, /* {ARGS: "NeonServ"} */ + {"NS_DELBOT_DONE", "Bot deleted."}, {NULL, NULL} }; diff --git a/src/cmd_global.h b/src/cmd_global.h index aa5a67d..98dc94e 100644 --- a/src/cmd_global.h +++ b/src/cmd_global.h @@ -37,10 +37,12 @@ #include "EventLogger.h" #include "bots.h" +CMD_BIND(global_cmd_addbot); CMD_BIND(global_cmd_bind); CMD_BIND(global_cmd_bots); CMD_BIND(global_cmd_command); CMD_BIND(global_cmd_commands); +CMD_BIND(global_cmd_delbot); CMD_BIND(global_cmd_die); CMD_BIND(global_cmd_emote); CMD_BIND(global_cmd_god); diff --git a/src/cmd_global_addbot.c b/src/cmd_global_addbot.c new file mode 100644 index 0000000..3c12865 --- /dev/null +++ b/src/cmd_global_addbot.c @@ -0,0 +1,43 @@ +/* cmd_global_addbot.c - NeonServ v5.2 + * Copyright (C) 2011 Philipp Kreil (pk910) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "cmd_global.h" + +/* +* argv[0] nick +* argv[1] class +*/ + +CMD_BIND(global_cmd_addbot) { + MYSQL_RES *res; + int botid; + if((botid = resolve_botalias(argv[1])) != -1) { + reply(getTextBot(), user, "NS_SETBOT_INVALID_CLASS", argv[1]); + return; + } + printf_mysql_query("SELECT `id` FROM `bots` WHERE `nick` = '%s'", escape_string(argv[0])); + res = mysql_use(); + if(mysql_fetch_row(res)) { + reply(getTextBot(), user, "NS_ADDBOT_EXISTING", argv[0]); + return; + } + printf_mysql_query("INSERT INTO `bots` (`nick`, `botclass`) VALUES ('%s', '%d')", escape_string(argv[0]), botid); + botid = (int) mysql_insert_id(mysql_conn); + reply(getTextBot(), user, "NS_ADDBOT_DONE", argv[0], botid); + logEvent(event); +} + diff --git a/src/cmd_global_delbot.c b/src/cmd_global_delbot.c new file mode 100644 index 0000000..1a37dd4 --- /dev/null +++ b/src/cmd_global_delbot.c @@ -0,0 +1,43 @@ +/* cmd_global_delbot.c - NeonServ v5.2 + * Copyright (C) 2011 Philipp Kreil (pk910) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "cmd_global.h" + +/* +* argv[0] nick/botid +*/ + +CMD_BIND(global_cmd_delbot) { + MYSQL_RES *res; + MYSQL_ROW row; + printf_mysql_query("SELECT `id` FROM `bots` WHERE `nick` = '%s' OR `id` = '%s'", escape_string(argv[0]), escape_string(argv[0])); + res = mysql_use(); + if((row = mysql_fetch_row(res)) == NULL) { + reply(getTextBot(), user, "NS_DELBOT_NOT_FOUND", argv[0]); + return; + } + int botid = atoi(row[0]); + printf_mysql_query("DELETE FROM `bots` WHERE `id` = '%s'", row[0]); + for(client = getBots(0, NULL); client; client = getBots(0, client)) { + if(client->clientid == botid) { + close_socket(client); + break; + } + } + reply(getTextBot(), user, "NS_DELBOT_DONE"); + logEvent(event); +} diff --git a/src/commands.c b/src/commands.c index 6d59818..7963b07 100644 --- a/src/commands.c +++ b/src/commands.c @@ -52,6 +52,8 @@ void register_commands() { OPER_COMMAND("restart", global_cmd_restart, 0, 1000, CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG); OPER_COMMAND("die", global_cmd_die, 0, 1000, CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG); OPER_COMMAND("setbot", global_cmd_setbot, 1, 1000, CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG | CMDFLAG_REQUIRED); + OPER_COMMAND("addbot", global_cmd_addbot, 2, 1000, CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG | CMDFLAG_REQUIRED); + OPER_COMMAND("delbot", global_cmd_delbot, 1, 1000, CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG | CMDFLAG_REQUIRED); #undef OPER_COMMAND //NeonServ Commands -- 2.20.1