moved cmd_move to global.mod and fixed crash in bot_SpamServ.c
[NeonServV5.git] / src / modules / global.mod / cmd_global_move.c
1 /* cmd_global_move.c - NeonServ v5.4
2  * Copyright (C) 2011-2012  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_global.h"
19
20 /*
21 * argv[0] - channel
22 * argv[1] - new channel
23 */
24 CMD_BIND(global_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     struct ChanNode *channode = getChanByName(channel);
65     if(channode) {
66         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
67             if(isUserOnChan(bot->user, channode)) {
68                 putsock(bot, "PART %s", channel);
69                 putsock(bot, "JOIN %s", new_channel);
70             }
71         }
72     } else {
73         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
74             if(bot->clientid == botid)
75                 putsock(bot, "JOIN %s", new_channel);
76         }
77     }
78     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(new_channel));
79     res = mysql_use();
80     if ((row = mysql_fetch_row(res)) != NULL) {
81         chanid = atoi(row[0]);
82         printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_cid` = '%d'", chanid);
83         printf_mysql_query("DELETE FROM `bans` WHERE `ban_channel` = '%d'", chanid);
84         printf_mysql_query("DELETE FROM `events` WHERE `cid` = '%d'", chanid);
85         printf_mysql_query("DELETE FROM `noinvite` WHERE `cid` = '%d'", chanid);
86         printf_mysql_query("DELETE FROM `owner_history` WHERE `owner_history_cid` = '%d'", chanid);
87         printf_mysql_query("DELETE FROM `channels` WHERE `channel_id` = '%d'", chanid);
88     }
89     printf_mysql_query("UPDATE `channels` SET `channel_name` = '%s' WHERE `channel_name` = '%s'", escape_string(new_channel), escape_string(channel));
90     
91     if(channode && channode->flags & CHANFLAG_REQUESTED_CHANINFO) {
92         channode->flags &= ~CHANFLAG_CHAN_REGISTERED;
93         channode->channel_id = 0;
94     }
95     reply(getTextBot(), user, "NS_MOVE_DONE", channel, new_channel);
96     logEvent(event);
97 }