added NeonBackup bot
[NeonServV5.git] / src / modules / NeonBackup.mod / bot_NeonBackup.c
1 /* bot_NeonBackup.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 #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     MYSQL_RES *res;
44     MYSQL_ROW row;
45     
46     printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
47     res = mysql_use();
48     if ((row = mysql_fetch_row(res)) != NULL) {
49         if(row[1] && row[2]) {
50             putsock(client, "OPER %s %s", row[1], row[2]);
51         }
52         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
53     }
54     
55     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);
56     res = mysql_use();
57     
58     while ((row = mysql_fetch_row(res)) != NULL) {
59         putsock(client, "JOIN %s %s", row[0], row[1]);
60     }
61 }
62
63 static void neonbackup_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
64     MYSQL_RES *res;
65     MYSQL_ROW row;
66     loadChannelSettings(chan);
67     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
68         strcpy(trigger, "!");
69         return;
70     }
71     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);
72     res = mysql_use();
73     if(!(row = mysql_fetch_row(res))) {
74         strcpy(trigger, "+");
75         return;
76     }
77     if(row[0] && *row[0])
78         strcpy(trigger, row[0]);
79     else
80         strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "+"));
81 }
82
83 static void start_bots(int type) {
84     struct ClientSocket *client;
85     MYSQL_RES *res, *res2;
86     MYSQL_ROW row;
87     
88     if(type == MODSTATE_STARTSTOP) {
89         printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
90         res = mysql_use();
91         
92         while ((row = mysql_fetch_row(res)) != NULL) {
93             client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
94             client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
95             client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
96             client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
97             client->flags |= SOCKET_FLAG_REQUEST_INVITE;
98             client->botid = BOTID;
99             client->clientid = atoi(row[7]);
100             connect_socket(client);
101         }
102     }
103     
104     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
105     res2 = mysql_use();
106     while ((row = mysql_fetch_row(res2)) != NULL) {
107         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
108             if(row[2] && strcmp(row[2], "")) {
109                 bind_set_parameters(BOTID, row[0], row[2]);
110             }
111             if(row[3]) {
112                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
113             }
114             if(row[4]) {
115                 bind_set_channel_access(BOTID, row[0], row[4]);
116             }
117             if(strcmp(row[5], "0"))
118                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
119         }
120     }
121     bind_unbound_required_functions(BOTID);
122 }
123
124 void neonbackup_recover_chan(struct ChanNode *chan) {
125     
126 }
127
128 static void neonbackup_event_join(struct ChanUser *chanuser) {
129     struct ClientSocket *client = getChannelBot(chanuser->chan, BOTID);
130     if(!client) return; //we can't "see" this event
131     if(chanuser->user == client->user) {
132         requestOp(client->user, chanuser->chan);
133         neonbackup_recover_chan(chanuser->chan);
134     }
135 }
136
137 static void neonbackup_event_invite(struct ClientSocket *client, struct UserNode *user, char *channel) {
138         if(client->botid != BOTID)
139                 return;
140     MYSQL_RES *res;
141     MYSQL_ROW row;
142     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);
143     res = mysql_use();
144     if ((row = mysql_fetch_row(res)) == NULL) {
145         reply(client, user, "NS_INVITE_FAIL", channel, client->user->nick);
146         return;
147     }
148     if(!strcmp(row[2], "1")) {
149         reply(client, user, "MODCMD_CHAN_SUSPENDED");
150         return;
151     }
152     int botid = atoi(row[0]);
153     struct ClientSocket *bot;
154     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
155         if(bot->clientid == botid)
156             break;
157     }
158     if(bot) {
159         struct ChanNode *chan = getChanByName(channel);
160         if(chan && isUserOnChan(bot->user, chan)) {
161             reply(client, user, "NS_INVITE_ON_CHAN", bot->user->nick, chan->name);
162         } else
163             putsock(bot, "JOIN %s", channel);
164     }
165 }
166
167 void init_NeonBackup(int type) {
168     set_bot_alias(BOTID, BOTALIAS);
169     start_bots(type);
170     
171     if(type == MODSTATE_REBIND) return;
172     
173     //register events
174     bind_bot_ready(neonbackup_bot_ready, module_id);
175     bind_join(neonbackup_event_join, module_id);
176         bind_invite(neonbackup_event_invite, module_id);
177     
178     set_trigger_callback(BOTID, module_id, neonbackup_trigger_callback);
179     
180     register_default_language_table(msgtab);
181 }
182
183 void loop_NeonBackup() {
184     
185 }
186
187 void free_NeonBackup(int type) {
188     unbind_allcmd(BOTID);
189     if(type == MODSTATE_STARTSTOP) {
190         //disconnect all our bots
191         struct ClientSocket *client;
192         for(client = getBots(0, NULL); client; client = getBots(0, client)) {
193             if(client->botid == BOTID) {
194                 unbind_botwise_allcmd(0, client->clientid);
195                 close_socket(client);
196                 break;
197             }
198         }
199     }
200 }
201
202 #undef BOTID
203 #undef BOTALIAS