aece825925436b39f296e18c6ecf9f539c9601b8
[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 #include "bot_NeonHelp.h"
31
32 struct cmd_bot_alias {
33     int botid;
34     char *alias;
35     struct cmd_bot_alias *next;
36 };
37
38 static struct cmd_bot_alias *bot_aliases = NULL;
39
40 void init_bots() {
41     init_NeonServ();
42     init_NeonSpam();
43     init_DummyServ();
44     init_NeonHelp();
45     
46     MYSQL_RES *res;
47     MYSQL_ROW row;
48     //load all timed bans
49     printf_mysql_query("SELECT `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_timeout` > 0");
50     res = mysql_use();
51     char nameBuf[20];
52     while ((row = mysql_fetch_row(res)) != NULL) {
53         if(atol(row[1]) - time(0) > 0) {
54             sprintf(nameBuf, "ban_%s", row[0]);
55             timeq_add_name(nameBuf, atol(row[1]) - time(0), channel_ban_timeout, strdup(row[0]));
56         } else {
57             //timed out
58             printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[0]);
59         }
60     }
61 }
62
63 void loop_bots() {
64     loop_NeonServ();
65     loop_NeonSpam();
66     loop_DummyServ();
67     loop_NeonHelp();
68 }
69
70 void free_bots() {
71     free_NeonServ();
72     free_NeonSpam();
73     free_DummyServ();
74     free_NeonHelp();
75 }
76
77 struct ClientSocket *getChannelBot(struct ChanNode *chan, int botid) {
78     struct ClientSocket *bot, *use_bot = NULL, *second_bot = NULL, *third_bot = NULL;
79     struct ChanUser *chanuser;
80     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
81         if(botid && bot->botid != botid) continue;
82         if((chanuser = getChanUser(bot->user, chan)) != NULL) {
83             if((chanuser->flags & CHANUSERFLAG_OPPED)) {
84                 use_bot = bot;
85                 if(bot->flags & SOCKET_FLAG_PREFERRED) break;
86             } else if(bot->flags & SOCKET_FLAG_PREFERRED)
87                 second_bot = bot;
88             else
89                 third_bot = bot;
90         }
91     }
92     if(!use_bot) use_bot = second_bot;
93     if(!use_bot) use_bot = third_bot;
94     return use_bot;
95 }
96
97 void requestOp(struct UserNode *user, struct ChanNode *chan) {
98     struct ClientSocket *bot;
99     struct ChanUser *chanuser = getChanUser(user, chan);
100     char opped = 0;
101     if(!chanuser) return;
102     if((chanuser->flags & CHANUSERFLAG_OPPED)) return;
103     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
104         if((chanuser = getChanUser(bot->user, chan)) != NULL && (chanuser->flags & CHANUSERFLAG_OPPED)) {
105             opped = 1;
106             putsock(bot, "MODE %s +o %s", chan->name, user->nick);
107             break;
108         }
109     }
110     if(!opped) {
111         //self op?
112     }
113 }
114
115 TIMEQ_CALLBACK(channel_ban_timeout) {
116     char *str_banid = data;
117     MYSQL_RES *res;
118     MYSQL_ROW row;
119     printf_mysql_query("SELECT `ban_mask`, `channel_name` FROM `bans` LEFT JOIN `channels` ON `ban_channel` = `channel_id` WHERE `ban_id` = '%s'", str_banid);
120     res = mysql_use();
121     struct ChanNode *chan;
122     if((row = mysql_fetch_row(res)) != NULL && (chan = getChanByName(row[1])) != NULL) {
123         struct ClientSocket *use_bot = getChannelBot(chan, 0);
124         if(use_bot) {
125             putsock(use_bot, "MODE %s -b %s", chan->name, row[0]);
126         }
127         printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", str_banid);
128     }
129     free(str_banid);
130 }
131
132 static int general_ctcp(char *buffer, char *command, char *text);
133
134 void general_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
135     char ctcpBuf[MAXLEN];
136     if(general_ctcp(ctcpBuf, command, text)) {
137         struct ClientSocket *bot;
138         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
139             if(bot->user == target) break;
140         }
141         if(bot)
142             putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
143     }
144 }
145
146 static int general_ctcp(char *buffer, char *command, char *text) {
147     if(!stricmp(command, "VERSION")) {
148         sprintf(buffer, "VERSION NeonServ v%s.%d by pk910 (%s)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
149         return 1;
150     }
151     if(!stricmp(command, "FINGER")) {
152         sprintf(buffer, "FINGER NeonServ v%s.%d (%s) build %s lines C code using " COMPILER " (see +netinfo)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"), codelines);
153         return 1;
154     }
155     if(!stricmp(command, "PING")) {
156         sprintf(buffer, "PING %s", (text ? text : "0"));
157         return 1;
158     }
159     if(!stricmp(command, "TIME")) {
160         time_t rawtime;
161         struct tm *timeinfo;
162         char timeBuf[80];
163         time(&rawtime);
164         timeinfo = localtime(&rawtime);
165         strftime(timeBuf, 80, "%c", timeinfo);
166         sprintf(buffer, "TIME %s", timeBuf);
167         return 1;
168     }
169     return 0;
170 }
171
172 void set_bot_alias(int botid, char *alias) {
173     struct cmd_bot_alias *botalias, *existing_alias = NULL;
174     for(botalias = bot_aliases; botalias; botalias = botalias->next) {
175         if(!stricmp(botalias->alias, alias))
176             return;
177         if(botalias->botid == botid)
178             existing_alias = botalias;
179     }
180     if(existing_alias) {
181         free(existing_alias->alias);
182         existing_alias->alias = strdup(alias);
183         return;
184     }
185     botalias = malloc(sizeof(*botalias));
186     if (!botalias) {
187         perror("malloc() failed");
188         return;
189     }
190     botalias->botid = botid;
191     botalias->alias = strdup(alias);
192     botalias->next = bot_aliases;
193     bot_aliases = botalias;
194 }
195
196 const char *resolve_botid(int botid) {
197     struct cmd_bot_alias *botalias;
198     for(botalias = bot_aliases; botalias; botalias = botalias->next) {
199         if(botalias->botid == botid)
200             return botalias->alias;
201     }
202     return NULL;
203 }
204
205 int resolve_botalias(const char *alias) {
206     struct cmd_bot_alias *botalias;
207     for(botalias = bot_aliases; botalias; botalias = botalias->next) {
208         if(!stricmp(botalias->alias, alias))
209             return botalias->botid;
210     }
211     return 0;
212 }