c9b06164a0caf974d992324dd92fff1e69ff82e5
[NeonServV5.git] / src / bots.c
1 /* bots.c - NeonServ v5.3
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
18 #include "bots.h"
19 #include "timeq.h"
20 #include "mysqlConn.h"
21 #include "ClientSocket.h"
22 #include "UserNode.h"
23 #include "ChanNode.h"
24 #include "ChanUser.h"
25 #include "version.h"
26 #include "modcmd.h"
27 #include "DBHelper.h"
28
29 #include "bot_NeonServ.h"
30 #include "bot_NeonSpam.h"
31 #include "bot_DummyServ.h"
32 #include "bot_NeonHelp.h"
33
34 struct cmd_bot_alias {
35     int botid;
36     char *alias;
37     struct cmd_bot_alias *next;
38 };
39
40 static struct cmd_bot_alias *bot_aliases = NULL;
41
42 static void start_zero_bots() {
43     struct ClientSocket *client;
44     MYSQL_RES *res, *res2;
45     MYSQL_ROW row;
46     
47     printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '0' AND `active` = '1'");
48     res = mysql_use();
49     
50     while ((row = mysql_fetch_row(res)) != NULL) {
51         client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
52         client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
53         client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
54         client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
55         client->botid = 0;
56         client->clientid = atoi(row[7]);
57         connect_socket(client);
58         
59         printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
60         res2 = mysql_use();
61         while ((row = mysql_fetch_row(res2)) != NULL) {
62             if(bind_botwise_cmd_to_command(0, client->clientid, row[0], row[1])) {
63                 if(row[2] && strcmp(row[2], "")) {
64                     bind_botwise_set_parameters(0, client->clientid, row[0], row[2]);
65                 }
66                 if(row[3]) {
67                     bind_botwise_set_global_access(0, client->clientid, row[0], atoi(row[3]));
68                 }
69                 if(row[4]) {
70                     bind_botwise_set_channel_access(0, client->clientid, row[0], row[4]);
71                 }
72                 if(strcmp(row[5], "0"))
73                     bind_botwise_set_bind_flags(0, client->clientid, row[0], atoi(row[5]));
74             }
75         }
76         bind_botwise_unbound_required_functions(0, client->clientid);
77     }
78 }
79
80 static void zero_bots_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
81     MYSQL_RES *res;
82     MYSQL_ROW row;
83     loadChannelSettings(chan);
84     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
85         strcpy(trigger, "+");
86         return;
87     }
88     printf_mysql_query("SELECT `trigger`, `defaulttrigger` FROM `bot_channels` LEFT JOIN `bots` ON `botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '0' AND `botid` = '%d'", chan->channel_id, clientid);
89     res = mysql_use();
90     if(!(row = mysql_fetch_row(res))) {
91         strcpy(trigger, "");
92         return;
93     }
94     if(row[0] && *row[0])
95         strcpy(trigger, row[0]);
96     else
97         strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "+"));
98 }
99
100 void init_bots() {
101     init_NeonServ();
102     init_NeonSpam();
103     init_DummyServ();
104     init_NeonHelp();
105     
106     set_bot_alias(0, "0");
107     start_zero_bots();
108     set_trigger_callback(0, zero_bots_trigger_callback);
109     
110     MYSQL_RES *res;
111     MYSQL_ROW row;
112     //load all timed bans
113     printf_mysql_query("SELECT `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_timeout` > 0");
114     res = mysql_use();
115     char nameBuf[20];
116     while ((row = mysql_fetch_row(res)) != NULL) {
117         if(atol(row[1]) - time(0) > 0) {
118             sprintf(nameBuf, "ban_%s", row[0]);
119             timeq_add_name(nameBuf, atol(row[1]) - time(0), channel_ban_timeout, strdup(row[0]));
120         } else {
121             //timed out
122             printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[0]);
123         }
124     }
125 }
126
127 void loop_bots() {
128     loop_NeonServ();
129     loop_NeonSpam();
130     loop_DummyServ();
131     loop_NeonHelp();
132 }
133
134 void free_bots() {
135     free_NeonServ();
136     free_NeonSpam();
137     free_DummyServ();
138     free_NeonHelp();
139 }
140
141 struct ClientSocket *getChannelBot(struct ChanNode *chan, int botid) {
142     struct ClientSocket *bot, *use_bot = NULL, *second_bot = NULL, *third_bot = NULL;
143     struct ChanUser *chanuser;
144     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
145         if(botid && bot->botid != botid) continue;
146         if((chanuser = getChanUser(bot->user, chan)) != NULL) {
147             if((chanuser->flags & CHANUSERFLAG_OPPED)) {
148                 use_bot = bot;
149                 if(bot->flags & SOCKET_FLAG_PREFERRED) break;
150             } else if(bot->flags & SOCKET_FLAG_PREFERRED)
151                 second_bot = bot;
152             else
153                 third_bot = bot;
154         }
155     }
156     if(!use_bot) use_bot = second_bot;
157     if(!use_bot) use_bot = third_bot;
158     return use_bot;
159 }
160
161 void requestOp(struct UserNode *user, struct ChanNode *chan) {
162     struct ClientSocket *bot;
163     struct ChanUser *chanuser = getChanUser(user, chan);
164     char opped = 0;
165     if(!chanuser) return;
166     if((chanuser->flags & CHANUSERFLAG_OPPED)) return;
167     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
168         if((chanuser = getChanUser(bot->user, chan)) != NULL && (chanuser->flags & CHANUSERFLAG_OPPED)) {
169             opped = 1;
170             putsock(bot, "MODE %s +o %s", chan->name, user->nick);
171             break;
172         }
173     }
174     if(!opped) {
175         //self op?
176     }
177 }
178
179 TIMEQ_CALLBACK(channel_ban_timeout) {
180     char *str_banid = data;
181     MYSQL_RES *res;
182     MYSQL_ROW row;
183     printf_mysql_query("SELECT `ban_mask`, `channel_name` FROM `bans` LEFT JOIN `channels` ON `ban_channel` = `channel_id` WHERE `ban_id` = '%s'", str_banid);
184     res = mysql_use();
185     struct ChanNode *chan;
186     if((row = mysql_fetch_row(res)) != NULL && (chan = getChanByName(row[1])) != NULL) {
187         struct ClientSocket *use_bot = getChannelBot(chan, 0);
188         if(use_bot) {
189             putsock(use_bot, "MODE %s -b %s", chan->name, row[0]);
190         }
191         printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", str_banid);
192     }
193     free(str_banid);
194 }
195
196 static int general_ctcp(char *buffer, char *command, char *text);
197
198 void general_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
199     char ctcpBuf[MAXLEN];
200     if(general_ctcp(ctcpBuf, command, text)) {
201         struct ClientSocket *bot;
202         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
203             if(bot->user == target) break;
204         }
205         if(bot)
206             putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
207     }
208 }
209
210 static int general_ctcp(char *buffer, char *command, char *text) {
211     if(!stricmp(command, "VERSION")) {
212         sprintf(buffer, "VERSION NeonServ v%s.%d by pk910 (%s)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
213         return 1;
214     }
215     if(!stricmp(command, "FINGER")) {
216         sprintf(buffer, "FINGER NeonServ v%s.%d (%s) build %s lines C code using " COMPILER " (see +netinfo)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"), codelines);
217         return 1;
218     }
219     if(!stricmp(command, "PING")) {
220         sprintf(buffer, "PING %s", (text ? text : "0"));
221         return 1;
222     }
223     if(!stricmp(command, "TIME")) {
224         time_t rawtime;
225         struct tm *timeinfo;
226         char timeBuf[80];
227         time(&rawtime);
228         timeinfo = localtime(&rawtime);
229         strftime(timeBuf, 80, "%c", timeinfo);
230         sprintf(buffer, "TIME %s", timeBuf);
231         return 1;
232     }
233     return 0;
234 }
235
236 void set_bot_alias(int botid, char *alias) {
237     struct cmd_bot_alias *botalias, *existing_alias = NULL;
238     for(botalias = bot_aliases; botalias; botalias = botalias->next) {
239         if(!stricmp(botalias->alias, alias))
240             return;
241         if(botalias->botid == botid)
242             existing_alias = botalias;
243     }
244     if(existing_alias) {
245         free(existing_alias->alias);
246         existing_alias->alias = strdup(alias);
247         return;
248     }
249     botalias = malloc(sizeof(*botalias));
250     if (!botalias) {
251         perror("malloc() failed");
252         return;
253     }
254     botalias->botid = botid;
255     botalias->alias = strdup(alias);
256     botalias->next = bot_aliases;
257     bot_aliases = botalias;
258 }
259
260 const char *resolve_botid(int botid) {
261     struct cmd_bot_alias *botalias;
262     for(botalias = bot_aliases; botalias; botalias = botalias->next) {
263         if(botalias->botid == botid)
264             return botalias->alias;
265     }
266     return NULL;
267 }
268
269 int resolve_botalias(const char *alias) {
270     struct cmd_bot_alias *botalias;
271     for(botalias = bot_aliases; botalias; botalias = botalias->next) {
272         if(!stricmp(botalias->alias, alias))
273             return botalias->botid;
274     }
275     return -1;
276 }