1 /* bots.c - NeonServ v5.3
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
20 #include "mysqlConn.h"
21 #include "ClientSocket.h"
29 #include "bot_NeonServ.h"
30 #include "bot_NeonSpam.h"
31 #include "bot_DummyServ.h"
32 #include "bot_NeonHelp.h"
34 struct cmd_bot_alias {
37 struct cmd_bot_alias *next;
40 static struct cmd_bot_alias *bot_aliases = NULL;
42 static void start_zero_bots() {
43 struct ClientSocket *client;
44 MYSQL_RES *res, *res2;
47 printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '0' AND `active` = '1'");
50 while ((row = mysql_fetch_row(res)) != NULL) {
51 client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
52 client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
53 client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
54 client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
56 client->clientid = atoi(row[7]);
57 connect_socket(client);
59 printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
61 while ((row = mysql_fetch_row(res2)) != NULL) {
62 if(bind_botwise_cmd_to_command(0, client->clientid, row[0], row[1])) {
63 if(row[2] && strcmp(row[2], "")) {
64 bind_botwise_set_parameters(0, client->clientid, row[0], row[2]);
67 bind_botwise_set_global_access(0, client->clientid, row[0], atoi(row[3]));
70 bind_botwise_set_channel_access(0, client->clientid, row[0], row[4]);
74 bind_botwise_unbound_required_functions(0, client->clientid);
78 static void zero_bots_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
81 loadChannelSettings(chan);
82 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
86 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);
88 row = mysql_fetch_row(res);
90 strcpy(trigger, row[0]);
92 strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "+"));
101 set_bot_alias(0, "0");
103 set_trigger_callback(0, zero_bots_trigger_callback);
107 //load all timed bans
108 printf_mysql_query("SELECT `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_timeout` > 0");
111 while ((row = mysql_fetch_row(res)) != NULL) {
112 if(atol(row[1]) - time(0) > 0) {
113 sprintf(nameBuf, "ban_%s", row[0]);
114 timeq_add_name(nameBuf, atol(row[1]) - time(0), channel_ban_timeout, strdup(row[0]));
117 printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[0]);
136 struct ClientSocket *getChannelBot(struct ChanNode *chan, int botid) {
137 struct ClientSocket *bot, *use_bot = NULL, *second_bot = NULL, *third_bot = NULL;
138 struct ChanUser *chanuser;
139 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
140 if(botid && bot->botid != botid) continue;
141 if((chanuser = getChanUser(bot->user, chan)) != NULL) {
142 if((chanuser->flags & CHANUSERFLAG_OPPED)) {
144 if(bot->flags & SOCKET_FLAG_PREFERRED) break;
145 } else if(bot->flags & SOCKET_FLAG_PREFERRED)
151 if(!use_bot) use_bot = second_bot;
152 if(!use_bot) use_bot = third_bot;
156 void requestOp(struct UserNode *user, struct ChanNode *chan) {
157 struct ClientSocket *bot;
158 struct ChanUser *chanuser = getChanUser(user, chan);
160 if(!chanuser) return;
161 if((chanuser->flags & CHANUSERFLAG_OPPED)) return;
162 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
163 if((chanuser = getChanUser(bot->user, chan)) != NULL && (chanuser->flags & CHANUSERFLAG_OPPED)) {
165 putsock(bot, "MODE %s +o %s", chan->name, user->nick);
174 TIMEQ_CALLBACK(channel_ban_timeout) {
175 char *str_banid = data;
178 printf_mysql_query("SELECT `ban_mask`, `channel_name` FROM `bans` LEFT JOIN `channels` ON `ban_channel` = `channel_id` WHERE `ban_id` = '%s'", str_banid);
180 struct ChanNode *chan;
181 if((row = mysql_fetch_row(res)) != NULL && (chan = getChanByName(row[1])) != NULL) {
182 struct ClientSocket *use_bot = getChannelBot(chan, 0);
184 putsock(use_bot, "MODE %s -b %s", chan->name, row[0]);
186 printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", str_banid);
191 static int general_ctcp(char *buffer, char *command, char *text);
193 void general_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
194 char ctcpBuf[MAXLEN];
195 if(general_ctcp(ctcpBuf, command, text)) {
196 struct ClientSocket *bot;
197 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
198 if(bot->user == target) break;
201 putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
205 static int general_ctcp(char *buffer, char *command, char *text) {
206 if(!stricmp(command, "VERSION")) {
207 sprintf(buffer, "VERSION NeonServ v%s.%d by pk910 (%s)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
210 if(!stricmp(command, "FINGER")) {
211 sprintf(buffer, "FINGER NeonServ v%s.%d (%s) build %s lines C code using " COMPILER " (see +netinfo)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"), codelines);
214 if(!stricmp(command, "PING")) {
215 sprintf(buffer, "PING %s", (text ? text : "0"));
218 if(!stricmp(command, "TIME")) {
223 timeinfo = localtime(&rawtime);
224 strftime(timeBuf, 80, "%c", timeinfo);
225 sprintf(buffer, "TIME %s", timeBuf);
231 void set_bot_alias(int botid, char *alias) {
232 struct cmd_bot_alias *botalias, *existing_alias = NULL;
233 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
234 if(!stricmp(botalias->alias, alias))
236 if(botalias->botid == botid)
237 existing_alias = botalias;
240 free(existing_alias->alias);
241 existing_alias->alias = strdup(alias);
244 botalias = malloc(sizeof(*botalias));
246 perror("malloc() failed");
249 botalias->botid = botid;
250 botalias->alias = strdup(alias);
251 botalias->next = bot_aliases;
252 bot_aliases = botalias;
255 const char *resolve_botid(int botid) {
256 struct cmd_bot_alias *botalias;
257 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
258 if(botalias->botid == botid)
259 return botalias->alias;
264 int resolve_botalias(const char *alias) {
265 struct cmd_bot_alias *botalias;
266 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
267 if(!stricmp(botalias->alias, alias))
268 return botalias->botid;