6dc3908ede04861bf530d996146376d4c22e8647
[NeonServV5.git] / cmd_neonserv_move.c
1
2 /*
3 * argv[0] - channel
4 * argv[1] - new channel
5 */
6 static CMD_BIND(neonserv_cmd_move) {
7     MYSQL_RES *res;
8     MYSQL_ROW row;
9     char *channel = argv[0];
10     char *new_channel = argv[1];
11     if(!is_valid_chan(new_channel)) {
12         reply(getTextBot(), user, "NS_INVALID_CHANNEL_NAME", new_channel);
13         return;
14     }
15     if(!stricmp(channel, new_channel)) {
16         reply(getTextBot(), user, "NS_MOVE_SELF");
17         return;
18     }
19     printf_mysql_query("SELECT `channel_id` FROM `bot_channels` LEFT JOIN `channels` ON `channel_id` = `chanid` WHERE `channel_name` = '%s'", escape_string(new_channel));
20     res = mysql_use();
21     if ((row = mysql_fetch_row(res)) != NULL) {
22         reply(getTextBot(), user, "NS_REGISTER_ALREADY", new_channel, client->user->nick);
23         return;
24     }
25     int chanid;
26     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(channel));
27     res = mysql_use();
28     if ((row = mysql_fetch_row(res)) != NULL) {
29         chanid = atoi(row[0]);
30     } else {
31         reply(getTextBot(), user, "NS_UNREGISTER_NOT_REGISTERED", argv[0], client->user->nick);
32         return;
33     }
34     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);
35     res = mysql_use();
36     if ((row = mysql_fetch_row(res)) == NULL) {
37         reply(getTextBot(), user, "NS_UNREGISTER_NOT_REGISTERED", argv[0], client->user->nick);
38         return;
39     }
40     if(!strcmp(row[2], "1")) {
41         reply(getTextBot(), user, "NS_MOVE_SUSPENDED");
42         return;
43     }
44     int botid = atoi(row[0]);
45     struct ClientSocket *bot;
46     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
47         if(bot->clientid == botid)
48             break;
49     }
50     if(bot) {
51         putsock(bot, "PART %s :Channel moved to %s.", channel, new_channel);
52         putsock(bot, "JOIN %s", new_channel);
53     }
54     printf_mysql_query("DELETE FROM `channels` WHERE `channel_name` = '%s'", escape_string(new_channel));
55     printf_mysql_query("UPDATE `channels` SET `channel_name` = '%s' WHERE `channel_id` = '%s'", escape_string(new_channel), row[1]);
56     struct ChanNode *channode = getChanByName(channel);
57     if(channode && channode->flags & CHANFLAG_REQUESTED_CHANINFO) {
58         channode->flags &= ~CHANFLAG_CHAN_REGISTERED;
59         channode->channel_id = 0;
60     }
61     channode = getChanByName(new_channel);
62     if(channode && channode->flags & CHANFLAG_REQUESTED_CHANINFO) {
63         channode->flags |= CHANFLAG_CHAN_REGISTERED;
64         channode->channel_id = atoi(row[1]);
65     }
66     reply(getTextBot(), user, "NS_MOVE_DONE", channel, new_channel);
67     logEvent(event);
68 }