1d95e182a05799ffdb7198e90dcefceb3b50ab31
[NeonServV5.git] / src / bots.c
1 /* bots.c - NeonServ v5.1
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
27 #include "bot_NeonServ.h"
28 #include "bot_NeonSpam.h"
29
30 void init_bots() {
31     init_NeonServ();
32     init_NeonSpam();
33     
34     MYSQL_RES *res;
35     MYSQL_ROW row;
36     //load all timed bans
37     printf_mysql_query("SELECT `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_timeout` > 0");
38     res = mysql_use();
39     char nameBuf[20];
40     while ((row = mysql_fetch_row(res)) != NULL) {
41         if(atol(row[1]) - time(0) > 0) {
42             sprintf(nameBuf, "ban_%s", row[0]);
43             timeq_add_name(nameBuf, atol(row[1]) - time(0), channel_ban_timeout, strdup(row[0]));
44         } else {
45             //timed out
46             printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[0]);
47         }
48     }
49 }
50
51 void loop_bots() {
52     loop_NeonServ();
53     loop_NeonSpam();
54 }
55
56 void free_bots() {
57     free_NeonServ();
58     free_NeonSpam();
59 }
60
61 struct ClientSocket *getChannelBot(struct ChanNode *chan, int botid) {
62     struct ClientSocket *bot, *use_bot = NULL, *second_bot = NULL, *third_bot = NULL;
63     struct ChanUser *chanuser;
64     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
65         if(botid && bot->botid != botid) continue;
66         if((chanuser = getChanUser(bot->user, chan)) != NULL) {
67             if((chanuser->flags & CHANUSERFLAG_OPPED)) {
68                 use_bot = bot;
69                 if(bot->flags & SOCKET_FLAG_PREFERRED) break;
70             } else if(bot->flags & SOCKET_FLAG_PREFERRED)
71                 second_bot = bot;
72             else
73                 third_bot = bot;
74         }
75     }
76     if(!use_bot) use_bot = second_bot;
77     if(!use_bot) use_bot = third_bot;
78     return use_bot;
79 }
80
81 TIMEQ_CALLBACK(channel_ban_timeout) {
82     char *str_banid = data;
83     MYSQL_RES *res;
84     MYSQL_ROW row;
85     printf_mysql_query("SELECT `ban_mask`, `channel_name` FROM `bans` LEFT JOIN `channels` ON `ban_channel` = `channel_id` WHERE `ban_id` = '%s'", str_banid);
86     res = mysql_use();
87     struct ChanNode *chan;
88     if((row = mysql_fetch_row(res)) != NULL && (chan = getChanByName(row[1])) != NULL) {
89         struct ClientSocket *use_bot = getChannelBot(chan, 0);
90         if(use_bot) {
91             putsock(use_bot, "MODE %s -b %s", chan->name, row[0]);
92         }
93         printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", str_banid);
94     }
95     free(str_banid);
96 }
97
98 static int general_ctcp(char *buffer, char *command, char *text);
99
100 void general_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
101     char ctcpBuf[MAXLEN];
102     if(general_ctcp(ctcpBuf, command, text)) {
103         struct ClientSocket *bot;
104         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
105             if(bot->user == target) break;
106         }
107         if(bot)
108             putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
109     }
110 }
111
112 static int general_ctcp(char *buffer, char *command, char *text) {
113     if(!stricmp(command, "VERSION")) {
114         sprintf(buffer, "VERSION NeonServ v%s.%d by pk910 (%s)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
115         return 1;
116     }
117     if(!stricmp(command, "FINGER")) {
118         sprintf(buffer, "FINGER NeonServ v%s.%d (%s) build %s lines C code using " COMPILER " (see +netinfo)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"), codelines);
119         return 1;
120     }
121     if(!stricmp(command, "PING")) {
122         sprintf(buffer, "PING %s", (text ? text : "0"));
123         return 1;
124     }
125     if(!stricmp(command, "TIME")) {
126         time_t rawtime;
127         struct tm *timeinfo;
128         char timeBuf[80];
129         time(&rawtime);
130         timeinfo = localtime(&rawtime);
131         strftime(timeBuf, 80, "%c", timeinfo);
132         sprintf(buffer, "TIME %s", timeBuf);
133         return 1;
134     }
135     return 0;
136 }