*** VERSION 5.3.0 ***
[NeonServV5.git] / src / bots.c
1 /* bots.c - NeonServ v5.3
2  * Copyright (C) 2011  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` 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             }
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` 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     row = mysql_fetch_row(res);
89     strcpy(trigger, (strlen(row[0]) ? row[0] : "+"));
90 }
91
92 void init_bots() {
93     init_NeonServ();
94     init_NeonSpam();
95     init_DummyServ();
96     init_NeonHelp();
97     
98     set_bot_alias(0, "0");
99     start_zero_bots();
100     set_trigger_callback(0, zero_bots_trigger_callback);
101     
102     MYSQL_RES *res;
103     MYSQL_ROW row;
104     //load all timed bans
105     printf_mysql_query("SELECT `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_timeout` > 0");
106     res = mysql_use();
107     char nameBuf[20];
108     while ((row = mysql_fetch_row(res)) != NULL) {
109         if(atol(row[1]) - time(0) > 0) {
110             sprintf(nameBuf, "ban_%s", row[0]);
111             timeq_add_name(nameBuf, atol(row[1]) - time(0), channel_ban_timeout, strdup(row[0]));
112         } else {
113             //timed out
114             printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[0]);
115         }
116     }
117 }
118
119 void loop_bots() {
120     loop_NeonServ();
121     loop_NeonSpam();
122     loop_DummyServ();
123     loop_NeonHelp();
124 }
125
126 void free_bots() {
127     free_NeonServ();
128     free_NeonSpam();
129     free_DummyServ();
130     free_NeonHelp();
131 }
132
133 struct ClientSocket *getChannelBot(struct ChanNode *chan, int botid) {
134     struct ClientSocket *bot, *use_bot = NULL, *second_bot = NULL, *third_bot = NULL;
135     struct ChanUser *chanuser;
136     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
137         if(botid && bot->botid != botid) continue;
138         if((chanuser = getChanUser(bot->user, chan)) != NULL) {
139             if((chanuser->flags & CHANUSERFLAG_OPPED)) {
140                 use_bot = bot;
141                 if(bot->flags & SOCKET_FLAG_PREFERRED) break;
142             } else if(bot->flags & SOCKET_FLAG_PREFERRED)
143                 second_bot = bot;
144             else
145                 third_bot = bot;
146         }
147     }
148     if(!use_bot) use_bot = second_bot;
149     if(!use_bot) use_bot = third_bot;
150     return use_bot;
151 }
152
153 void requestOp(struct UserNode *user, struct ChanNode *chan) {
154     struct ClientSocket *bot;
155     struct ChanUser *chanuser = getChanUser(user, chan);
156     char opped = 0;
157     if(!chanuser) return;
158     if((chanuser->flags & CHANUSERFLAG_OPPED)) return;
159     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
160         if((chanuser = getChanUser(bot->user, chan)) != NULL && (chanuser->flags & CHANUSERFLAG_OPPED)) {
161             opped = 1;
162             putsock(bot, "MODE %s +o %s", chan->name, user->nick);
163             break;
164         }
165     }
166     if(!opped) {
167         //self op?
168     }
169 }
170
171 TIMEQ_CALLBACK(channel_ban_timeout) {
172     char *str_banid = data;
173     MYSQL_RES *res;
174     MYSQL_ROW row;
175     printf_mysql_query("SELECT `ban_mask`, `channel_name` FROM `bans` LEFT JOIN `channels` ON `ban_channel` = `channel_id` WHERE `ban_id` = '%s'", str_banid);
176     res = mysql_use();
177     struct ChanNode *chan;
178     if((row = mysql_fetch_row(res)) != NULL && (chan = getChanByName(row[1])) != NULL) {
179         struct ClientSocket *use_bot = getChannelBot(chan, 0);
180         if(use_bot) {
181             putsock(use_bot, "MODE %s -b %s", chan->name, row[0]);
182         }
183         printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", str_banid);
184     }
185     free(str_banid);
186 }
187
188 static int general_ctcp(char *buffer, char *command, char *text);
189
190 void general_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
191     char ctcpBuf[MAXLEN];
192     if(general_ctcp(ctcpBuf, command, text)) {
193         struct ClientSocket *bot;
194         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
195             if(bot->user == target) break;
196         }
197         if(bot)
198             putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
199     }
200 }
201
202 static int general_ctcp(char *buffer, char *command, char *text) {
203     if(!stricmp(command, "VERSION")) {
204         sprintf(buffer, "VERSION NeonServ v%s.%d by pk910 (%s)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
205         return 1;
206     }
207     if(!stricmp(command, "FINGER")) {
208         sprintf(buffer, "FINGER NeonServ v%s.%d (%s) build %s lines C code using " COMPILER " (see +netinfo)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"), codelines);
209         return 1;
210     }
211     if(!stricmp(command, "PING")) {
212         sprintf(buffer, "PING %s", (text ? text : "0"));
213         return 1;
214     }
215     if(!stricmp(command, "TIME")) {
216         time_t rawtime;
217         struct tm *timeinfo;
218         char timeBuf[80];
219         time(&rawtime);
220         timeinfo = localtime(&rawtime);
221         strftime(timeBuf, 80, "%c", timeinfo);
222         sprintf(buffer, "TIME %s", timeBuf);
223         return 1;
224     }
225     return 0;
226 }
227
228 void set_bot_alias(int botid, char *alias) {
229     struct cmd_bot_alias *botalias, *existing_alias = NULL;
230     for(botalias = bot_aliases; botalias; botalias = botalias->next) {
231         if(!stricmp(botalias->alias, alias))
232             return;
233         if(botalias->botid == botid)
234             existing_alias = botalias;
235     }
236     if(existing_alias) {
237         free(existing_alias->alias);
238         existing_alias->alias = strdup(alias);
239         return;
240     }
241     botalias = malloc(sizeof(*botalias));
242     if (!botalias) {
243         perror("malloc() failed");
244         return;
245     }
246     botalias->botid = botid;
247     botalias->alias = strdup(alias);
248     botalias->next = bot_aliases;
249     bot_aliases = botalias;
250 }
251
252 const char *resolve_botid(int botid) {
253     struct cmd_bot_alias *botalias;
254     for(botalias = bot_aliases; botalias; botalias = botalias->next) {
255         if(botalias->botid == botid)
256             return botalias->alias;
257     }
258     return NULL;
259 }
260
261 int resolve_botalias(const char *alias) {
262     struct cmd_bot_alias *botalias;
263     for(botalias = bot_aliases; botalias; botalias = botalias->next) {
264         if(!stricmp(botalias->alias, alias))
265             return botalias->botid;
266     }
267     return -1;
268 }