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