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