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