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