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