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