--- /dev/null
+/* cmd_global_setbot.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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "cmd_global.h"
+
+/*
+* argv[0] botid
+* argv[1] setting
+* argv[2] value
+*/
+
+static int global_cmd_setbot_active(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_nick(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_ident(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_realname(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_server(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_port(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_bind(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_ssl(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_serverpass(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_class(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_queue(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_prefered(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_maxchan(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_priority(struct UserNode *user, MYSQL_ROW bot, char *value);
+static int global_cmd_setbot_trigger(struct UserNode *user, MYSQL_ROW bot, char *value);
+
+CMD_BIND(global_cmd_setbot) {
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ int botid = atoi(argv[0]);
+ printf_mysql_query("SELECT `active`, `nick`, `server`, `port`, `pass`, `botclass`, `textbot`, `queue`, `defaulttrigger`, `max_channels`, `register_priority`, `bind`, `ident`, `realname`, `ssl`, `id` FROM `bots` WHERE `id` = '%d'", botid);
+ res = mysql_use();
+ if(!(row = mysql_fetch_row(res))) {
+ reply(getTextBot(), user, "NS_SETBOT_UNKNOWN", botid);
+ return;
+ }
+ if(argc > 1) {
+ char *value;
+ if(argc > 2) {
+ value = merge_argv(argv, 2, argc);
+ } else
+ value = NULL;
+ int log_event = 0;
+ if(!stricmp(argv[1], "active")) log_event = global_cmd_setbot_active(user, row, value);
+ else if(!stricmp(argv[1], "nick")) log_event = global_cmd_setbot_nick(user, row, value);
+ else if(!stricmp(argv[1], "ident")) log_event = global_cmd_setbot_ident(user, row, value);
+ else if(!stricmp(argv[1], "realname")) log_event = global_cmd_setbot_realname(user, row, value);
+ else if(!stricmp(argv[1], "server")) log_event = global_cmd_setbot_server(user, row, value);
+ else if(!stricmp(argv[1], "port")) log_event = global_cmd_setbot_port(user, row, value);
+ else if(!stricmp(argv[1], "bind")) log_event = global_cmd_setbot_bind(user, row, value);
+ else if(!stricmp(argv[1], "ssl")) log_event = global_cmd_setbot_ssl(user, row, value);
+ else if(!stricmp(argv[1], "serverpass")) log_event = global_cmd_setbot_serverpass(user, row, value);
+ else if(!stricmp(argv[1], "botclass")) log_event = global_cmd_setbot_class(user, row, value);
+ else if(!stricmp(argv[1], "queue")) log_event = global_cmd_setbot_queue(user, row, value);
+ else if(!stricmp(argv[1], "prefered")) log_event = global_cmd_setbot_prefered(user, row, value);
+ else if(!stricmp(argv[1], "maxchan")) log_event = global_cmd_setbot_maxchan(user, row, value);
+ else if(!stricmp(argv[1], "priority")) log_event = global_cmd_setbot_priority(user, row, value);
+ else if(!stricmp(argv[1], "trigger")) log_event = global_cmd_setbot_nick(user, row, value);
+ else {
+ reply(getTextBot(), user, "NS_SETBOT_SETTING", argv[1]);
+ }
+ if(log_event) {
+ logEvent(event);
+ }
+ } else {
+ reply(getTextBot(), user, "NS_SETBOT_HEADER", botid);
+ global_cmd_setbot_active(user, row, NULL);
+ global_cmd_setbot_nick(user, row, NULL);
+ global_cmd_setbot_ident(user, row, NULL);
+ global_cmd_setbot_realname(user, row, NULL);
+ global_cmd_setbot_server(user, row, NULL);
+ global_cmd_setbot_port(user, row, NULL);
+ global_cmd_setbot_bind(user, row, NULL);
+ global_cmd_setbot_ssl(user, row, NULL);
+ global_cmd_setbot_serverpass(user, row, NULL);
+ global_cmd_setbot_class(user, row, NULL);
+ global_cmd_setbot_queue(user, row, NULL);
+ global_cmd_setbot_prefered(user, row, NULL);
+ global_cmd_setbot_maxchan(user, row, NULL);
+ global_cmd_setbot_priority(user, row, NULL);
+ global_cmd_setbot_trigger(user, row, NULL);
+ }
+}
+
+static int global_cmd_setbot_active(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ int val = ((bot[0] && !strcmp(bot[0], "1")) ? 1 : 0);
+ int ret = 0;
+ if(value) {
+ if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
+ val = 0;
+ } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
+ val = 1;
+ } else {
+ reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
+ return 0;
+ }
+ if(val != ((bot[0] && !strcmp(bot[0], "1")) ? 1 : 0)) {
+ if(val) {
+ //add the bot
+ struct ClientSocket *client;
+ client = create_socket(bot[2], atoi(bot[3]), bot[11], bot[4], bot[1], bot[12], bot[13]);
+ client->flags |= (strcmp(bot[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
+ client->flags |= (strcmp(bot[7], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
+ client->flags |= (strcmp(bot[14], "0") ? SOCKET_FLAG_SSL : 0);
+ client->botid = atoi(bot[5]);
+ client->clientid = atoi(bot[15]);
+ connect_socket(client);
+ } else {
+ //remove the bot
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ close_socket(client);
+ break;
+ }
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `active` = '%d' WHERE `id` = '%s'", val, bot[15]);
+ ret = 1;
+ }
+ }
+ reply(getTextBot(), user, "\002ACTIVE \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
+ return ret;
+}
+
+static int global_cmd_setbot_nick(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ char *val = bot[1];
+ int ret = 0;
+ if(value) {
+ if(!is_valid_nick(value)) {
+ reply(getTextBot(), user, "NS_SETBOT_NICK_INVALID", value);
+ return 0;
+ }
+ //rename the bot
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ if(client->nick)
+ free(client->nick);
+ client->nick = strdup(value);
+ if(client->flags & SOCKET_FLAG_READY)
+ putsock(client, "NICK %s", value);
+ break;
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `nick` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
+ val = value;
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002NICK \002 %s", val);
+ return ret;
+}
+
+static int global_cmd_setbot_ident(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ char *val = bot[12];
+ int ret = 0;
+ if(value) {
+ if(strlen(value) > 12)
+ value[12] = '\0';
+ //rename the bot
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ if(client->ident)
+ free(client->ident);
+ client->ident = strdup(value);
+ break;
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `ident` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
+ val = value;
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002IDENT \002 %s", val);
+ return ret;
+}
+
+static int global_cmd_setbot_realname(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ char *val = bot[13];
+ int ret = 0;
+ if(value) {
+ if(strlen(value) > 255)
+ value[255] = '\0';
+ //rename the bot
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ if(client->ident)
+ free(client->ident);
+ client->ident = strdup(value);
+ break;
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `realname` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
+ val = value;
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002REALNAME \002 %s", val);
+ return ret;
+}
+
+static int global_cmd_setbot_server(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ char *val = bot[2];
+ int ret = 0;
+ if(value) {
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ if(client->host)
+ free(client->host);
+ client->host = strdup(value);
+ if(client->flags & SOCKET_FLAG_READY)
+ reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
+ break;
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `server` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
+ val = value;
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002SERVER \002 %s", val);
+ return ret;
+}
+
+static int global_cmd_setbot_port(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ int val = atoi(bot[3]);
+ int ret = 0;
+ if(value) {
+ val = atoi(value);
+ if(val <= 0 || val > 65534) {
+ reply(getTextBot(), user, "NS_SETBOT_PORT_INVALID", value);
+ return 0;
+ }
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ client->port = val;
+ if(client->flags & SOCKET_FLAG_READY)
+ reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
+ break;
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `port` = '%d' WHERE `id` = '%s'", val, bot[15]);
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002PORT \002 %d", val);
+ return ret;
+}
+
+static int global_cmd_setbot_bind(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ char *val = bot[11];
+ int ret = 0;
+ if(value) {
+ if(!strcmp(value, "*"))
+ value = NULL;
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ if(client->bind)
+ free(client->bind);
+ client->bind = (value ? strdup(value) : NULL);
+ if(client->flags & SOCKET_FLAG_READY)
+ reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
+ break;
+ }
+ }
+ if(value)
+ printf_mysql_query("UPDATE `bots` SET `bind` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
+ else
+ printf_mysql_query("UPDATE `bots` SET `bind` = NULL WHERE `id` = '%s'", bot[15]);
+ val = value;
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002BIND \002 %s", val);
+ return ret;
+}
+
+static int global_cmd_setbot_ssl(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ int val = (strcmp(bot[14], "0") ? 1 : 0);
+ int ret = 0;
+ if(value) {
+ if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
+ val = 0;
+ } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
+ val = 1;
+ } else {
+ reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
+ return 0;
+ }
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ if(val)
+ client->flags |= SOCKET_FLAG_SSL;
+ else
+ client->flags &= ~SOCKET_FLAG_SSL;
+ if(client->flags & SOCKET_FLAG_READY)
+ reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
+ break;
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `ssl` = '%d' WHERE `id` = '%s'", val, bot[15]);
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002SSL \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
+ return ret;
+}
+
+static int global_cmd_setbot_serverpass(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ char *val = bot[4];
+ int ret = 0;
+ if(value) {
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ if(client->pass)
+ free(client->pass);
+ client->pass = strdup(value);
+ if(client->flags & SOCKET_FLAG_READY)
+ reply(getTextBot(), user, "NS_SETBOT_NEED_RESTART");
+ break;
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `pass` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
+ val = value;
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002SERVERPASS \002 %s", val);
+ return ret;
+}
+
+static int global_cmd_setbot_class(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ int val = atoi(bot[5]);
+ int ret = 0;
+ if(value) {
+ if((val = resolve_botalias(value)) != -1) {
+ reply(getTextBot(), user, "NS_SETBOT_INVALID_CLASS", value);
+ return 0;
+ }
+ if(val != atoi(bot[5])) {
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ unbind_botwise_allcmd(client->clientid);
+ client->botid = val;
+ if(client->botid == 0) {
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
+ res = mysql_use();
+ while ((row = mysql_fetch_row(res)) != NULL) {
+ if(bind_botwise_cmd_to_command(client->botid, client->clientid, row[0], row[1])) {
+ if(row[2] && strcmp(row[2], "")) {
+ bind_botwise_set_parameters(client->botid, client->clientid, row[0], row[2]);
+ }
+ if(row[3]) {
+ bind_botwise_set_global_access(client->botid, client->clientid, row[0], atoi(row[3]));
+ }
+ if(row[4]) {
+ bind_botwise_set_channel_access(client->botid, client->clientid, row[0], row[4]);
+ }
+ }
+ }
+ bind_botwise_unbound_required_functions(client->botid, client->clientid);
+ }
+ break;
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `botclass` = '%d' WHERE `id` = '%s'", val, bot[15]);
+ ret = 1;
+ }
+ }
+ reply(getTextBot(), user, "\002BOTCLASS \002 %s", resolve_botid(val));
+ return ret;
+}
+
+static int global_cmd_setbot_queue(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ int val = (strcmp(bot[7], "0") ? 1 : 0);
+ int ret = 0;
+ if(value) {
+ if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
+ val = 0;
+ } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
+ val = 1;
+ } else {
+ reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
+ return 0;
+ }
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ if(val)
+ client->flags |= SOCKET_FLAG_USE_QUEUE;
+ else
+ client->flags &= ~SOCKET_FLAG_USE_QUEUE;
+ break;
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `queue` = '%d' WHERE `id` = '%s'", val, bot[15]);
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002QUEUE \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
+ return ret;
+}
+
+static int global_cmd_setbot_prefered(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ int val = (strcmp(bot[6], "0") ? 1 : 0);
+ int ret = 0;
+ if(value) {
+ if(!strcmp(value, "0") || !stricmp(value, "off") || !stricmp(value, get_language_string(user, "NS_SET_OFF"))) {
+ val = 0;
+ } else if(!strcmp(value, "1") || !stricmp(value, "on") || !stricmp(value, get_language_string(user, "NS_SET_ON"))) {
+ val = 1;
+ } else {
+ reply(getTextBot(), user, "NS_SET_INVALID_BOOLEAN", value);
+ return 0;
+ }
+ struct ClientSocket *client;
+ for(client = getBots(0, NULL); client; client = getBots(0, client)) {
+ if(client->clientid == atoi(bot[15])) {
+ if(val)
+ client->flags |= SOCKET_FLAG_PREFERRED;
+ else
+ client->flags &= ~SOCKET_FLAG_PREFERRED;
+ break;
+ }
+ }
+ printf_mysql_query("UPDATE `bots` SET `queue` = '%d' WHERE `id` = '%s'", val, bot[15]);
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002PREFERED \002 %s", get_language_string(user, (val ? "NS_SET_ON" : "NS_SET_OFF")));
+ return ret;
+}
+
+static int global_cmd_setbot_maxchan(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ int val = atoi(bot[9]);
+ int ret = 0;
+ if(value) {
+ val = atoi(value);
+ if(val < 0 || val > 99999) {
+ reply(getTextBot(), user, "NS_SETBOT_MAXCHAN_INVALID", value);
+ return 0;
+ }
+ printf_mysql_query("UPDATE `bots` SET `maxchan` = '%d' WHERE `id` = '%s'", val, bot[15]);
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002MAXCHAN \002 %d", val);
+ return ret;
+}
+
+static int global_cmd_setbot_priority(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ int val = atoi(bot[10]);
+ int ret = 0;
+ if(value) {
+ val = atoi(value);
+ if(val < 0 || val > 99) {
+ reply(getTextBot(), user, "NS_SETBOT_PRIORITY_INVALID", value);
+ return 0;
+ }
+ printf_mysql_query("UPDATE `bots` SET `register_priority` = '%d' WHERE `id` = '%s'", val, bot[15]);
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002PRIORITY \002 %d", val);
+ return ret;
+}
+
+static int global_cmd_setbot_trigger(struct UserNode *user, MYSQL_ROW bot, char *value) {
+ char *val = bot[8];
+ int ret = 0;
+ if(value) {
+ if(!*value || strlen(value) > 10) {
+ reply(getTextBot(), user, "NS_SETBOT_TRIGGER_INVALID", value);
+ return 0;
+ }
+ printf_mysql_query("UPDATE `bots` SET `defaulttrigger` = '%s' WHERE `id` = '%s'", escape_string(value), bot[15]);
+ reply(getTextBot(), user, "NS_SETBOT_TRIGGER_NOTE");
+ val = value;
+ ret = 1;
+ }
+ reply(getTextBot(), user, "\002TRIGGER \002 %s", val);
+ return ret;
+}