added NeonBackup bot
[NeonServV5.git] / src / modules / NeonFun.mod / bot_NeonFun.c
1 /* bot_NeonFun.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_NeonFun.h"
21 #include "../../modcmd.h"
22 #include "../../IRCParser.h"
23 #include "../../IRCEvents.h"
24 #include "../../UserNode.h"
25 #include "../../ChanNode.h"
26 #include "../../ChanUser.h"
27 #include "../../ModeNode.h"
28 #include "../../BanNode.h"
29 #include "../../ClientSocket.h"
30 #include "../../mysqlConn.h"
31 #include "../../lang.h"
32 #include "../../HandleInfoHandler.h"
33 #include "../../WHOHandler.h"
34 #include "../../DBHelper.h"
35 #include "../../tools.h"
36 #include "../../timeq.h"
37 #include "../../version.h"
38 #include "../../EventLogger.h"
39 #include "../../bots.h"
40 #include "game_uno.h"
41 #include "game_4wins.h"
42
43 #define BOTID NEONFUN_BOTID
44 #define BOTALIAS "NeonFun"
45
46 static void neonfun_bot_ready(struct ClientSocket *client) {
47     MYSQL_RES *res;
48     MYSQL_ROW row;
49     
50     printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
51     res = mysql_use();
52     if ((row = mysql_fetch_row(res)) != NULL) {
53         if(row[1] && row[2]) {
54             putsock(client, "OPER %s %s", row[1], row[2]);
55         }
56         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
57     }
58     
59     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);
60     res = mysql_use();
61     
62     while ((row = mysql_fetch_row(res)) != NULL) {
63         putsock(client, "JOIN %s %s", row[0], row[1]);
64     }
65 }
66
67 static void neonfun_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
68     MYSQL_RES *res;
69     MYSQL_ROW row;
70     loadChannelSettings(chan);
71     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
72         strcpy(trigger, "+");
73         return;
74     }
75     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);
76     res = mysql_use();
77     if(!(row = mysql_fetch_row(res))) {
78         strcpy(trigger, "+");
79         return;
80     }
81     if(row[0] && *row[0])
82         strcpy(trigger, row[0]);
83     else
84         strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "~"));
85 }
86
87 static void start_bots(int type) {
88     struct ClientSocket *client;
89     MYSQL_RES *res, *res2;
90     MYSQL_ROW row;
91     
92     if(type == MODSTATE_STARTSTOP) {
93         printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
94         res = mysql_use();
95         
96         while ((row = mysql_fetch_row(res)) != NULL) {
97             client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
98             client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
99             client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
100             client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
101             client->flags |= SOCKET_FLAG_SILENT;
102             client->flags |= SOCKET_FLAG_REQUEST_INVITE;
103             client->botid = BOTID;
104             client->clientid = atoi(row[7]);
105             connect_socket(client);
106         }
107     }
108     
109     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
110     res2 = mysql_use();
111     while ((row = mysql_fetch_row(res2)) != NULL) {
112         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
113             if(row[2] && strcmp(row[2], "")) {
114                 bind_set_parameters(BOTID, row[0], row[2]);
115             }
116             if(row[3]) {
117                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
118             }
119             if(row[4]) {
120                 bind_set_channel_access(BOTID, row[0], row[4]);
121             }
122             if(strcmp(row[5], "0"))
123                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
124         }
125     }
126     bind_unbound_required_functions(BOTID);
127 }
128
129 static void neonfun_parted(struct ChanUser *chanuser, char *reason) {
130     uno_event_part(chanuser);
131     fourwins_event_part(chanuser);
132 }
133
134 static void neonfun_quitted(struct UserNode *user, char *reason) {
135     uno_event_quit(user);
136     fourwins_event_quit(user);
137 }
138
139 static int neonfun_freechan(struct ChanNode *chan) {
140     uno_event_freechan(chan);
141     fourwins_event_freechan(chan);
142     return 0;
143 }
144
145 static void neonfun_event_invite(struct ClientSocket *client, struct UserNode *user, char *channel) {
146         if(client->botid != BOTID)
147                 return;
148     MYSQL_RES *res;
149     MYSQL_ROW row;
150     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);
151     res = mysql_use();
152     if ((row = mysql_fetch_row(res)) == NULL) {
153         reply(client, user, "NS_INVITE_FAIL", channel, client->user->nick);
154         return;
155     }
156     if(!strcmp(row[2], "1")) {
157         reply(client, user, "MODCMD_CHAN_SUSPENDED");
158         return;
159     }
160     int botid = atoi(row[0]);
161     struct ClientSocket *bot;
162     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
163         if(bot->clientid == botid)
164             break;
165     }
166     if(bot) {
167         struct ChanNode *chan = getChanByName(channel);
168         if(chan && isUserOnChan(bot->user, chan)) {
169             reply(client, user, "NS_INVITE_ON_CHAN", bot->user->nick, chan->name);
170         } else
171             putsock(bot, "JOIN %s", channel);
172     }
173 }
174
175 void init_NeonFun(int type) {
176     set_bot_alias(BOTID, BOTALIAS);
177     start_bots(type);
178     
179     if(type == MODSTATE_REBIND) return;
180     
181     //register events
182     bind_bot_ready(neonfun_bot_ready, module_id);
183     bind_part(neonfun_parted, module_id);
184     bind_quit(neonfun_quitted, module_id);
185     bind_freechan(neonfun_freechan, module_id);
186     bind_invite(neonfun_event_invite, module_id);
187         
188     set_trigger_callback(BOTID, module_id, neonfun_trigger_callback);
189 }
190
191 void loop_NeonFun() {
192     
193 }
194
195 void free_NeonFun(int type) {
196     unbind_allcmd(BOTID);
197     if(type == MODSTATE_STARTSTOP) {
198         //disconnect all our bots
199         struct ClientSocket *client;
200         for(client = getBots(0, NULL); client; client = getBots(0, client)) {
201             if(client->botid == BOTID) {
202                 unbind_botwise_allcmd(0, client->clientid);
203                 close_socket(client);
204                 break;
205             }
206         }
207     }
208 }
209
210 char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
211     char *uname = "";
212     int cid = 0;
213     MYSQL_RES *res;
214     MYSQL_ROW row;
215     if(user) {
216         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
217     }
218     if(chan) {
219         loadChannelSettings(chan);
220         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
221             cid = chan->channel_id;
222     }
223     printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
224     res = mysql_use();
225     if ((row = mysql_fetch_row(res)) != NULL) {
226         return row[0];
227     } else
228         return NULL;
229 }
230
231 void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) {
232     char *uname = "";
233     int cid = 0;
234     MYSQL_RES *res;
235     MYSQL_ROW row;
236     if(user) {
237         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
238     }
239     if(chan) {
240         loadChannelSettings(chan);
241         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
242             cid = chan->channel_id;
243     }
244     printf_mysql_query("SELECT `id`, `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
245     res = mysql_use();
246     if ((row = mysql_fetch_row(res)) != NULL) {
247         if(strcmp(row[1], value))
248             printf_mysql_query("UPDATE `fundata` SET `value` = '%s' WHERE `id` = '%s'", escape_string(value), row[0]);
249     } else
250         printf_mysql_query("INSERT INTO `fundata` (`user`, `cid`, `name`, `value`) VALUES ('%s', '%d', '%s', '%s')", escape_string(uname), cid, escape_string(setting), escape_string(value));
251 }
252
253 void uno_reply(struct uno_game *game, struct UserNode *user, const char *text, ...) {
254     struct ClientSocket *client = game->textbot;
255     const char *reply_format = get_language_string(user, text);
256     if(reply_format == NULL)
257         reply_format = text;
258     char formatBuf[MAXLEN];
259     sprintf(formatBuf, "PRIVMSG %s :[UNO] %s", game->channel->name, reply_format);
260     va_list arg_list;
261     char sendBuf[MAXLEN];
262     int pos;
263     if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
264     sendBuf[0] = '\0';
265     va_start(arg_list, text);
266     pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
267     va_end(arg_list);
268     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
269     sendBuf[pos] = '\n';
270     sendBuf[pos+1] = '\0';
271     write_socket(client, sendBuf, pos+1);
272 }
273
274 void fourwins_reply(struct fourwins_game *game, const char *text, ...) {
275     struct ClientSocket *client = game->textbot;
276     struct fourwins_guest *guest;
277     int guest_count = 0;
278     for(guest = game->guests; guest; guest = guest->next) {
279         guest_count++;
280     }
281     struct ChanUser *chanusers[guest_count + 2];
282     chanusers[0] = game->player[0];
283     chanusers[1] = game->player[1];
284     guest_count = 0;
285     for(guest = game->guests; guest; guest = guest->next) {
286         chanusers[2 + (guest_count++)] = guest->chanuser;
287     }
288     int i;
289     guest_count += 2;
290     for(i = 0; i < guest_count; i++) {
291         const char *reply_format = get_language_string(chanusers[i]->user, text);
292         if(reply_format == NULL)
293             reply_format = text;
294         char formatBuf[MAXLEN];
295         sprintf(formatBuf, "NOTICE %s :[4WINS] %s", chanusers[i]->user->nick, reply_format);
296         va_list arg_list;
297         char sendBuf[MAXLEN];
298         int pos;
299         if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
300         sendBuf[0] = '\0';
301         va_start(arg_list, text);
302         pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
303         va_end(arg_list);
304         if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
305         sendBuf[pos] = '\n';
306         sendBuf[pos+1] = '\0';
307         write_socket(client, sendBuf, pos+1);
308     }
309 }
310
311 #undef BOTID
312 #undef BOTALIAS