moved cmd_move to global.mod and fixed crash in bot_SpamServ.c
[NeonServV5.git] / src / modules / global.mod / cmd_global_move.c
diff --git a/src/modules/global.mod/cmd_global_move.c b/src/modules/global.mod/cmd_global_move.c
new file mode 100644 (file)
index 0000000..3a1d50f
--- /dev/null
@@ -0,0 +1,97 @@
+/* cmd_global_move.c - NeonServ v5.4
+ * Copyright (C) 2011-2012  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] - channel
+* argv[1] - new channel
+*/
+CMD_BIND(global_cmd_move) {
+    MYSQL_RES *res;
+    MYSQL_ROW row;
+    char *channel = argv[0];
+    char *new_channel = argv[1];
+    if(!is_valid_chan(new_channel)) {
+        reply(getTextBot(), user, "NS_INVALID_CHANNEL_NAME", new_channel);
+        return;
+    }
+    if(!stricmp(channel, new_channel)) {
+        reply(getTextBot(), user, "NS_MOVE_SELF");
+        return;
+    }
+    printf_mysql_query("SELECT `channel_id` FROM `bot_channels` LEFT JOIN `channels` ON `channel_id` = `chanid` WHERE `channel_name` = '%s'", escape_string(new_channel));
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) != NULL) {
+        reply(getTextBot(), user, "NS_REGISTER_ALREADY", new_channel, client->user->nick);
+        return;
+    }
+    int chanid;
+    printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(channel));
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) != NULL) {
+        chanid = atoi(row[0]);
+    } else {
+        reply(getTextBot(), user, "NS_UNREGISTER_NOT_REGISTERED", argv[0], client->user->nick);
+        return;
+    }
+    printf_mysql_query("SELECT `botid`, `bot_channels`.`id`, `suspended` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chanid, client->botid);
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) == NULL) {
+        reply(getTextBot(), user, "NS_UNREGISTER_NOT_REGISTERED", argv[0], client->user->nick);
+        return;
+    }
+    if(!strcmp(row[2], "1")) {
+        reply(getTextBot(), user, "NS_MOVE_SUSPENDED");
+        return;
+    }
+    int botid = atoi(row[0]);
+    struct ClientSocket *bot;
+    struct ChanNode *channode = getChanByName(channel);
+    if(channode) {
+        for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+            if(isUserOnChan(bot->user, channode)) {
+                putsock(bot, "PART %s", channel);
+                putsock(bot, "JOIN %s", new_channel);
+            }
+        }
+    } else {
+        for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+            if(bot->clientid == botid)
+                putsock(bot, "JOIN %s", new_channel);
+        }
+    }
+    printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(new_channel));
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) != NULL) {
+        chanid = atoi(row[0]);
+        printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_cid` = '%d'", chanid);
+        printf_mysql_query("DELETE FROM `bans` WHERE `ban_channel` = '%d'", chanid);
+        printf_mysql_query("DELETE FROM `events` WHERE `cid` = '%d'", chanid);
+        printf_mysql_query("DELETE FROM `noinvite` WHERE `cid` = '%d'", chanid);
+        printf_mysql_query("DELETE FROM `owner_history` WHERE `owner_history_cid` = '%d'", chanid);
+        printf_mysql_query("DELETE FROM `channels` WHERE `channel_id` = '%d'", chanid);
+    }
+    printf_mysql_query("UPDATE `channels` SET `channel_name` = '%s' WHERE `channel_name` = '%s'", escape_string(new_channel), escape_string(channel));
+    
+    if(channode && channode->flags & CHANFLAG_REQUESTED_CHANINFO) {
+        channode->flags &= ~CHANFLAG_CHAN_REGISTERED;
+        channode->channel_id = 0;
+    }
+    reply(getTextBot(), user, "NS_MOVE_DONE", channel, new_channel);
+    logEvent(event);
+}