tried to reorder the program structure and build process
[NeonServV5.git] / src / cmd_neonserv_move.c
diff --git a/src/cmd_neonserv_move.c b/src/cmd_neonserv_move.c
new file mode 100644 (file)
index 0000000..804954f
--- /dev/null
@@ -0,0 +1,70 @@
+
+#include "cmd_neonserv.h"
+
+/*
+* argv[0] - channel
+* argv[1] - new channel
+*/
+CMD_BIND(neonserv_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;
+    for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+        if(bot->clientid == botid)
+            break;
+    }
+    if(bot) {
+        putsock(bot, "PART %s :Channel moved to %s.", channel, new_channel);
+        putsock(bot, "JOIN %s", new_channel);
+    }
+    printf_mysql_query("DELETE FROM `channels` WHERE `channel_name` = '%s'", escape_string(new_channel));
+    printf_mysql_query("UPDATE `channels` SET `channel_name` = '%s' WHERE `channel_id` = '%s'", escape_string(new_channel), row[1]);
+    struct ChanNode *channode = getChanByName(channel);
+    if(channode && channode->flags & CHANFLAG_REQUESTED_CHANINFO) {
+        channode->flags &= ~CHANFLAG_CHAN_REGISTERED;
+        channode->channel_id = 0;
+    }
+    channode = getChanByName(new_channel);
+    if(channode && channode->flags & CHANFLAG_REQUESTED_CHANINFO) {
+        channode->flags |= CHANFLAG_CHAN_REGISTERED;
+        channode->channel_id = atoi(row[1]);
+    }
+    reply(getTextBot(), user, "NS_MOVE_DONE", channel, new_channel);
+    logEvent(event);
+}