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