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