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