modified code to use IOHandler functions instead of own ones
[NeonServV5.git] / src / modules / NeonBackup.mod / bot_NeonBackup.c
1 /* bot_NeonBackup.c - NeonServ v5.5
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 #include "../module.h"
18 #include "../botid.h"
19
20 #include "bot_NeonBackup.h"
21 #include "../../modcmd.h"
22 #include "cmd_neonbackup.h"
23 #include "../../lang.h"
24 #include "../../mysqlConn.h"
25 #include "../../ClientSocket.h"
26 #include "../../UserNode.h"
27 #include "../../ChanNode.h"
28 #include "../../ChanUser.h"
29 #include "../../IRCEvents.h"
30 #include "../../IRCParser.h"
31 #include "../../bots.h"
32 #include "../../DBHelper.h"
33 #include "../../WHOHandler.h"
34
35 #define BOTID NEONBACKUP_BOTID
36 #define BOTALIAS "NeonBackup"
37
38 static const struct default_language_entry msgtab[] = {
39     {NULL, NULL}
40 };
41
42 static void neonbackup_bot_ready(struct ClientSocket *client) {
43     if(client->botid != BOTID)
44         return;
45     MYSQL_RES *res;
46     MYSQL_ROW row;
47     
48     printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
49     res = mysql_use();
50     if ((row = mysql_fetch_row(res)) != NULL) {
51         if(row[1] && row[2]) {
52             putsock(client, "OPER %s %s", row[1], row[2]);
53         }
54         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
55     }
56     
57     printf_mysql_query("SELECT `channel_name`, `channel_key` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `botid` = '%d' AND `suspended` = '0'", client->clientid);
58     res = mysql_use();
59     
60     while ((row = mysql_fetch_row(res)) != NULL) {
61         putsock(client, "JOIN %s %s", row[0], row[1]);
62     }
63 }
64
65 static void neonbackup_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
66     MYSQL_RES *res;
67     MYSQL_ROW row;
68     loadChannelSettings(chan);
69     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
70         strcpy(trigger, "!");
71         return;
72     }
73     printf_mysql_query("SELECT `trigger`, `defaulttrigger` FROM `bot_channels` LEFT JOIN `bots` ON `botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, BOTID);
74     res = mysql_use();
75     if(!(row = mysql_fetch_row(res))) {
76         strcpy(trigger, "+");
77         return;
78     }
79     if(row[0] && *row[0])
80         strcpy(trigger, row[0]);
81     else
82         strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "+"));
83 }
84
85 static void start_bots(int type) {
86     struct ClientSocket *client;
87     MYSQL_RES *res, *res2;
88     MYSQL_ROW row;
89     
90     if(type == MODSTATE_STARTSTOP) {
91         printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
92         res = mysql_use();
93         
94         while ((row = mysql_fetch_row(res)) != NULL) {
95             client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
96             client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
97             client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
98             client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
99             client->flags |= SOCKET_FLAG_REQUEST_INVITE | SOCKET_FLAG_REQUEST_OP;
100             client->botid = BOTID;
101             client->clientid = atoi(row[7]);
102             connect_socket(client);
103         }
104     }
105     
106     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
107     res2 = mysql_use();
108     while ((row = mysql_fetch_row(res2)) != NULL) {
109         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
110             if(row[2] && strcmp(row[2], "")) {
111                 bind_set_parameters(BOTID, row[0], row[2]);
112             }
113             if(row[3]) {
114                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
115             }
116             if(row[4]) {
117                 bind_set_channel_access(BOTID, row[0], row[4]);
118             }
119             if(strcmp(row[5], "0"))
120                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
121         }
122     }
123     bind_unbound_required_functions(BOTID);
124 }
125
126 void neonbackup_recover_chan(struct ChanNode *chan) {
127     struct ClientSocket *bot = getChannelBot(chan, BOTID); // prefer backup bot ;)
128     struct ChanUser *chanuser = (bot ? getChanUser(bot->user, chan) : NULL);
129     if(!chanuser || !(chanuser->flags & CHANUSERFLAG_OPPED)) {
130         //search an opped bot
131         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
132             if((chanuser = getChanUser(bot->user, chan)) && (chanuser->flags & CHANUSERFLAG_OPPED))
133                 break;
134         }
135     }
136     if(!bot) //no opped bots present... channel can't be recovered
137         return;
138     struct ClientSocket *target;
139     struct ModeBuffer *modeBuf;
140     modeBuf = initModeBuffer(bot, chan);
141     for(target = getBots(SOCKET_FLAG_READY, NULL); target; target = getBots(SOCKET_FLAG_READY, target)) {
142         if((target->flags & SOCKET_FLAG_REQUEST_OP) && (chanuser = getChanUser(target->user, chan)) && !(chanuser->flags & CHANUSERFLAG_OPPED)) {
143             modeBufferOp(modeBuf, target->user->nick);
144         }
145     }
146     freeModeBuffer(modeBuf);
147 }
148
149 static void neonbackup_event_join(struct ChanUser *chanuser) {
150     struct ClientSocket *client = getChannelBot(chanuser->chan, BOTID);
151     if(!client) return; //we can't "see" this event
152     if(chanuser->user == client->user) {
153         requestOp(client->user, chanuser->chan);
154         neonbackup_recover_chan(chanuser->chan);
155     }
156 }
157
158 static void neonbackup_event_invite(struct ClientSocket *client, struct UserNode *user, char *channel) {
159         if(client->botid != BOTID)
160                 return;
161     MYSQL_RES *res;
162     MYSQL_ROW row;
163     printf_mysql_query("SELECT `botid`, `bot_channels`.`id`, `suspended` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `channel_name` = '%s' AND `botclass` = '%d'", escape_string(channel), client->botid);
164     res = mysql_use();
165     if ((row = mysql_fetch_row(res)) == NULL) {
166         reply(client, user, "NS_INVITE_FAIL", channel, client->user->nick);
167         return;
168     }
169     if(!strcmp(row[2], "1")) {
170         reply(client, user, "MODCMD_CHAN_SUSPENDED");
171         return;
172     }
173     int botid = atoi(row[0]);
174     struct ClientSocket *bot;
175     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
176         if(bot->clientid == botid)
177             break;
178     }
179     if(bot) {
180         struct ChanNode *chan = getChanByName(channel);
181         if(chan && isUserOnChan(bot->user, chan)) {
182             reply(client, user, "NS_INVITE_ON_CHAN", bot->user->nick, chan->name);
183         } else
184             putsock(bot, "JOIN %s", channel);
185     }
186 }
187
188 void init_NeonBackup(int type) {
189     set_bot_alias(BOTID, BOTALIAS);
190     start_bots(type);
191     
192     if(type == MODSTATE_REBIND) return;
193     
194     //register events
195     bind_bot_ready(neonbackup_bot_ready, module_id);
196     bind_join(neonbackup_event_join, module_id);
197         bind_invite(neonbackup_event_invite, module_id);
198     
199     set_trigger_callback(BOTID, module_id, neonbackup_trigger_callback);
200     
201     register_default_language_table(msgtab);
202 }
203
204 void free_NeonBackup(int type) {
205     unbind_allcmd(BOTID);
206     if(type == MODSTATE_STARTSTOP) {
207         //disconnect all our bots
208         struct ClientSocket *client;
209         for(client = getBots(0, NULL); client; client = getBots(0, client)) {
210             if(client->botid == BOTID) {
211                 unbind_botwise_allcmd(0, client->clientid);
212                 close_socket(client);
213                 break;
214             }
215         }
216     }
217 }
218
219 #undef BOTID
220 #undef BOTALIAS