*** VERSION 5.2.0 ***
[NeonServV5.git] / src / cmd_neonserv_move.c
1 /* cmd_neonserv_move.c - NeonServ v5.2
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_neonserv.h"
19
20 /*
21 * argv[0] - channel
22 * argv[1] - new channel
23 */
24 CMD_BIND(neonserv_cmd_move) {
25     MYSQL_RES *res;
26     MYSQL_ROW row;
27     char *channel = argv[0];
28     char *new_channel = argv[1];
29     if(!is_valid_chan(new_channel)) {
30         reply(getTextBot(), user, "NS_INVALID_CHANNEL_NAME", new_channel);
31         return;
32     }
33     if(!stricmp(channel, new_channel)) {
34         reply(getTextBot(), user, "NS_MOVE_SELF");
35         return;
36     }
37     printf_mysql_query("SELECT `channel_id` FROM `bot_channels` LEFT JOIN `channels` ON `channel_id` = `chanid` WHERE `channel_name` = '%s'", escape_string(new_channel));
38     res = mysql_use();
39     if ((row = mysql_fetch_row(res)) != NULL) {
40         reply(getTextBot(), user, "NS_REGISTER_ALREADY", new_channel, client->user->nick);
41         return;
42     }
43     int chanid;
44     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(channel));
45     res = mysql_use();
46     if ((row = mysql_fetch_row(res)) != NULL) {
47         chanid = atoi(row[0]);
48     } else {
49         reply(getTextBot(), user, "NS_UNREGISTER_NOT_REGISTERED", argv[0], client->user->nick);
50         return;
51     }
52     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);
53     res = mysql_use();
54     if ((row = mysql_fetch_row(res)) == NULL) {
55         reply(getTextBot(), user, "NS_UNREGISTER_NOT_REGISTERED", argv[0], client->user->nick);
56         return;
57     }
58     if(!strcmp(row[2], "1")) {
59         reply(getTextBot(), user, "NS_MOVE_SUSPENDED");
60         return;
61     }
62     int botid = atoi(row[0]);
63     struct ClientSocket *bot;
64     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
65         if(bot->clientid == botid)
66             break;
67     }
68     if(bot) {
69         putsock(bot, "PART %s :Channel moved to %s.", channel, new_channel);
70         putsock(bot, "JOIN %s", new_channel);
71     }
72     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(new_channel));
73     res = mysql_use();
74     if ((row = mysql_fetch_row(res)) != NULL) {
75         chanid = atoi(row[0]);
76         printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_cid` = '%d'", chanid);
77         printf_mysql_query("DELETE FROM `bans` WHERE `ban_channel` = '%d'", chanid);
78         printf_mysql_query("DELETE FROM `events` WHERE `cid` = '%d'", chanid);
79         printf_mysql_query("DELETE FROM `noinvite` WHERE `cid` = '%d'", chanid);
80         printf_mysql_query("DELETE FROM `owner_history` WHERE `owner_history_cid` = '%d'", chanid);
81         printf_mysql_query("DELETE FROM `channels` WHERE `channel_id` = '%d'", chanid);
82     }
83     printf_mysql_query("UPDATE `channels` SET `channel_name` = '%s' WHERE `channel_name` = '%s'", escape_string(new_channel), escape_string(channel));
84     struct ChanNode *channode = getChanByName(channel);
85     if(channode && channode->flags & CHANFLAG_REQUESTED_CHANINFO) {
86         channode->flags &= ~CHANFLAG_CHAN_REGISTERED;
87         channode->channel_id = 0;
88     }
89     reply(getTextBot(), user, "NS_MOVE_DONE", channel, new_channel);
90     logEvent(event);
91 }