1 /* bots.c - NeonServ v5.2
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"
27 #include "bot_NeonServ.h"
28 #include "bot_NeonSpam.h"
29 #include "bot_DummyServ.h"
30 #include "bot_NeonHelp.h"
32 struct cmd_bot_alias {
35 struct cmd_bot_alias *next;
38 static struct cmd_bot_alias *bot_aliases = NULL;
49 printf_mysql_query("SELECT `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_timeout` > 0");
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]));
58 printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[0]);
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)) {
85 if(bot->flags & SOCKET_FLAG_PREFERRED) break;
86 } else if(bot->flags & SOCKET_FLAG_PREFERRED)
92 if(!use_bot) use_bot = second_bot;
93 if(!use_bot) use_bot = third_bot;
97 TIMEQ_CALLBACK(channel_ban_timeout) {
98 char *str_banid = data;
101 printf_mysql_query("SELECT `ban_mask`, `channel_name` FROM `bans` LEFT JOIN `channels` ON `ban_channel` = `channel_id` WHERE `ban_id` = '%s'", str_banid);
103 struct ChanNode *chan;
104 if((row = mysql_fetch_row(res)) != NULL && (chan = getChanByName(row[1])) != NULL) {
105 struct ClientSocket *use_bot = getChannelBot(chan, 0);
107 putsock(use_bot, "MODE %s -b %s", chan->name, row[0]);
109 printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", str_banid);
114 static int general_ctcp(char *buffer, char *command, char *text);
116 void general_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
117 char ctcpBuf[MAXLEN];
118 if(general_ctcp(ctcpBuf, command, text)) {
119 struct ClientSocket *bot;
120 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
121 if(bot->user == target) break;
124 putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
128 static int general_ctcp(char *buffer, char *command, char *text) {
129 if(!stricmp(command, "VERSION")) {
130 sprintf(buffer, "VERSION NeonServ v%s.%d by pk910 (%s)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
133 if(!stricmp(command, "FINGER")) {
134 sprintf(buffer, "FINGER NeonServ v%s.%d (%s) build %s lines C code using " COMPILER " (see +netinfo)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"), codelines);
137 if(!stricmp(command, "PING")) {
138 sprintf(buffer, "PING %s", (text ? text : "0"));
141 if(!stricmp(command, "TIME")) {
146 timeinfo = localtime(&rawtime);
147 strftime(timeBuf, 80, "%c", timeinfo);
148 sprintf(buffer, "TIME %s", timeBuf);
154 void set_bot_alias(int botid, char *alias) {
155 struct cmd_bot_alias *botalias, *existing_alias = NULL;
156 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
157 if(!stricmp(botalias->alias, alias))
159 if(botalias->botid == botid)
160 existing_alias = botalias;
163 free(existing_alias->alias);
164 existing_alias->alias = strdup(alias);
167 botalias = malloc(sizeof(*botalias));
169 perror("malloc() failed");
172 botalias->botid = botid;
173 botalias->alias = strdup(alias);
174 botalias->next = bot_aliases;
175 bot_aliases = botalias;
178 const char *resolve_botid(int botid) {
179 struct cmd_bot_alias *botalias;
180 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
181 if(botalias->botid == botid)
182 return botalias->alias;
187 int resolve_botalias(const char *alias) {
188 struct cmd_bot_alias *botalias;
189 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
190 if(!stricmp(botalias->alias, alias))
191 return botalias->botid;