added NeonFun Bot with UNO 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
42 #define BOTID NEONFUN_BOTID
43 #define BOTALIAS "NeonFun"
44
45 static void neonfun_bot_ready(struct ClientSocket *client) {
46     MYSQL_RES *res;
47     MYSQL_ROW row;
48     
49     printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
50     res = mysql_use();
51     if ((row = mysql_fetch_row(res)) != NULL) {
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 neonfun_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_SILENT;
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 static void neonfun_parted(struct ChanUser *chanuser, char *reason) {
125     uno_event_part(chanuser);
126 }
127
128 static void neonfun_quitted(struct UserNode *user, char *reason) {
129     uno_event_quit(user);
130 }
131
132 static int neonfun_freechan(struct ChanNode *chan) {
133     uno_event_freechan(chan);
134     return 0;
135 }
136
137 void init_NeonFun(int type) {
138     set_bot_alias(BOTID, BOTALIAS);
139     start_bots(type);
140     
141     if(type == MODSTATE_REBIND) return;
142     
143     //register events
144     bind_bot_ready(neonfun_bot_ready, module_id);
145     bind_part(neonfun_parted, module_id);
146     bind_quit(neonfun_quitted, module_id);
147     bind_freechan(neonfun_freechan, module_id);
148     
149     set_trigger_callback(BOTID, module_id, neonfun_trigger_callback);
150 }
151
152 void loop_NeonFun() {
153     
154 }
155
156 void free_NeonFun(int type) {
157     unbind_allcmd(BOTID);
158     if(type == MODSTATE_STARTSTOP) {
159         //disconnect all our bots
160         struct ClientSocket *client;
161         for(client = getBots(0, NULL); client; client = getBots(0, client)) {
162             if(client->botid == BOTID) {
163                 unbind_botwise_allcmd(0, client->clientid);
164                 close_socket(client);
165                 break;
166             }
167         }
168     }
169 }
170
171 char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
172     char *uname = "";
173     int cid = 0;
174     MYSQL_RES *res;
175     MYSQL_ROW row;
176     if(user) {
177         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
178     }
179     if(chan) {
180         loadChannelSettings(chan);
181         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
182             cid = chan->channel_id;
183     }
184     printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
185     res = mysql_use();
186     if ((row = mysql_fetch_row(res)) != NULL) {
187         return row[0];
188     } else
189         return NULL;
190 }
191
192 void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) {
193     char *uname = "";
194     int cid = 0;
195     MYSQL_RES *res;
196     MYSQL_ROW row;
197     if(user) {
198         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
199     }
200     if(chan) {
201         loadChannelSettings(chan);
202         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
203             cid = chan->channel_id;
204     }
205     printf_mysql_query("SELECT `id`, `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
206     res = mysql_use();
207     if ((row = mysql_fetch_row(res)) != NULL) {
208         if(strcmp(row[1], value))
209             printf_mysql_query("UPDATE `fundata` SET `value` = '%s' WHERE `id` = '%s'", escape_string(value), row[0]);
210     } else
211         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));
212 }
213
214 void uno_reply(struct uno_game *game, struct UserNode *user, const char *text, ...) {
215     struct ClientSocket *client = game->textbot;
216     const char *reply_format = get_language_string(user, text);
217     if(reply_format == NULL)
218         reply_format = text;
219     char formatBuf[MAXLEN];
220     sprintf(formatBuf, "PRIVMSG %s :[UNO] %s", game->channel->name, reply_format);
221     va_list arg_list;
222     char sendBuf[MAXLEN];
223     int pos;
224     if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
225     sendBuf[0] = '\0';
226     va_start(arg_list, text);
227     pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
228     va_end(arg_list);
229     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
230     sendBuf[pos] = '\n';
231     sendBuf[pos+1] = '\0';
232     write_socket(client, sendBuf, pos+1);
233 }
234
235 #undef BOTID
236 #undef BOTALIAS