1 /* bots.c - NeonServ v5.6
2 * Copyright (C) 2011-2012 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"
28 #include "IRCEvents.h"
31 struct cmd_bot_alias {
34 struct cmd_bot_alias *next;
37 static struct cmd_bot_alias *bot_aliases = NULL;
39 static void start_zero_bots() {
40 struct ClientSocket *client;
41 MYSQL_RES *res, *res2;
44 printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind`, `secret` FROM `bots` WHERE `botclass` = '0' AND `active` = '1'");
47 while ((row = mysql_fetch_row(res)) != NULL) {
48 client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
49 client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
50 client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
51 client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
52 client->flags |= (strcmp(row[11], "0") ? SOCKET_FLAG_SECRET_BOT : 0);
54 client->clientid = atoi(row[7]);
55 connect_socket(client);
57 printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
59 while ((row = mysql_fetch_row(res2)) != NULL) {
60 if(bind_botwise_cmd_to_command(0, client->clientid, row[0], row[1])) {
61 if(row[2] && strcmp(row[2], "")) {
62 bind_botwise_set_parameters(0, client->clientid, row[0], row[2]);
65 bind_botwise_set_global_access(0, client->clientid, row[0], atoi(row[3]));
68 bind_botwise_set_channel_access(0, client->clientid, row[0], row[4]);
70 if(strcmp(row[5], "0"))
71 bind_botwise_set_bind_flags(0, client->clientid, row[0], atoi(row[5]));
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 if(!(row = mysql_fetch_row(res))) {
93 strcpy(trigger, row[0]);
95 strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "+"));
98 static void zero_bots_bot_ready(struct ClientSocket *client) {
99 if(client->botid != 0)
104 printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
106 if ((row = mysql_fetch_row(res)) != NULL) {
107 if(row[1] && row[2]) {
108 putsock(client, "OPER %s %s", row[1], row[2]);
110 putsock(client, "MODE %s +%s", client->user->nick, row[0]);
113 printf_mysql_query("SELECT `channel_name`, `channel_key` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `botid` = '%d' AND `suspended` = '0'", client->clientid);
116 while ((row = mysql_fetch_row(res)) != NULL) {
117 putsock(client, "JOIN %s %s", row[0], row[1]);
121 static TIMEQ_CALLBACK(load_timed_bans) {
124 //load all timed bans for the next 7 days
125 printf_mysql_query("SELECT `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_timeout` > 0 AND `ban_timeout` < (UNIX_TIMESTAMP() + (86400 * 7))");
128 while ((row = mysql_fetch_row(res)) != NULL) {
129 if(atol(row[1]) - time(0) > 0) {
130 sprintf(nameBuf, "ban_%s", row[0]);
131 timeq_add_name(nameBuf, atol(row[1]) - time(0), 0, channel_ban_timeout, strdup(row[0]));
134 printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[0]);
137 timeq_add(86400*7, 0, load_timed_bans, NULL);
141 set_bot_alias(0, "0");
143 set_trigger_callback(0, 0, zero_bots_trigger_callback);
144 bind_bot_ready(zero_bots_bot_ready, 0);
146 timeq_add(10, 0, load_timed_bans, NULL);
150 struct ClientSocket *getChannelBot(struct ChanNode *chan, int botid) {
151 struct ClientSocket *bot, *use_bot = NULL, *second_bot = NULL, *third_bot = NULL;
152 struct ChanUser *chanuser;
153 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
154 if(botid && bot->botid != botid) continue;
155 if(!chan || (chanuser = getChanUser(bot->user, chan)) != NULL) {
156 if(chan && (chanuser->flags & CHANUSERFLAG_OPPED)) {
158 if(bot->flags & SOCKET_FLAG_PREFERRED) break;
159 } else if(bot->flags & SOCKET_FLAG_PREFERRED)
165 if(!use_bot) use_bot = second_bot;
166 if(!use_bot) use_bot = third_bot;
170 void requestOp(struct UserNode *user, struct ChanNode *chan) {
171 struct ClientSocket *bot, *userbot = NULL;
172 struct ChanUser *chanuser = getChanUser(user, chan);
174 if(!chanuser) return;
175 if((chanuser->flags & CHANUSERFLAG_OPPED)) return;
176 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
177 if(!opped && (chanuser = getChanUser(bot->user, chan)) != NULL && (chanuser->flags & CHANUSERFLAG_OPPED)) {
179 putsock(bot, "MODE %s +o %s", chan->name, user->nick);
181 if(bot->user == user) {
186 if(userbot && (isUserModeSet(user, 'o') || isUserModeSet(user, 'O') || isUserModeSet(user, 'k') || isUserModeSet(user, 'X'))) {
187 putsock(userbot, "MODE %s +o %s", chan->name, user->nick);
188 putsock(userbot, "OPMODE %s +o %s", chan->name, user->nick);
193 void requestInvite(struct UserNode *user, struct ChanNode *chan) {
194 struct ClientSocket *bot;
195 struct ChanUser *chanuser = getChanUser(user, chan);
198 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
199 if(!invited && (chanuser = getChanUser(bot->user, chan)) != NULL && (chanuser->flags & CHANUSERFLAG_OPPED)) {
201 putsock(bot, "INVITE %s %s", user->nick, chan->name);
206 TIMEQ_CALLBACK(channel_ban_timeout) {
207 char *str_banid = data;
210 printf_mysql_query("SELECT `ban_mask`, `channel_name` FROM `bans` LEFT JOIN `channels` ON `ban_channel` = `channel_id` WHERE `ban_id` = '%s'", str_banid);
212 struct ChanNode *chan;
213 if((row = mysql_fetch_row(res)) != NULL && (chan = getChanByName(row[1])) != NULL) {
214 struct ClientSocket *use_bot = getChannelBot(chan, 0);
216 putsock(use_bot, "MODE %s -b %s", chan->name, row[0]);
218 printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", str_banid);
223 static int general_ctcp(char *buffer, char *command, char *text);
225 void general_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
226 char ctcpBuf[MAXLEN];
227 if(general_ctcp(ctcpBuf, command, text)) {
228 struct ClientSocket *bot;
229 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
230 if(bot->user == target) break;
233 putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
237 static int general_ctcp(char *buffer, char *command, char *text) {
238 if(!stricmp(command, "VERSION")) {
239 sprintf(buffer, "VERSION NeonServ v%s.%d by pk910 (%s)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
242 if(!stricmp(command, "FINGER")) {
243 sprintf(buffer, "FINGER NeonServ v%s.%d (%s) build %s lines C code using " COMPILER " (see +netinfo)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"), codelines);
246 if(!stricmp(command, "PING")) {
247 sprintf(buffer, "PING %s", (text ? text : "0"));
250 if(!stricmp(command, "TIME")) {
255 timeinfo = localtime(&rawtime);
256 strftime(timeBuf, 80, "%c", timeinfo);
257 sprintf(buffer, "TIME %s", timeBuf);
263 void set_bot_alias(int botid, char *alias) {
264 struct cmd_bot_alias *botalias, *existing_alias = NULL;
265 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
266 if(!stricmp(botalias->alias, alias))
268 if(botalias->botid == botid)
269 existing_alias = botalias;
272 free(existing_alias->alias);
273 existing_alias->alias = strdup(alias);
276 botalias = malloc(sizeof(*botalias));
278 perror("malloc() failed");
281 botalias->botid = botid;
282 botalias->alias = strdup(alias);
283 botalias->next = bot_aliases;
284 bot_aliases = botalias;
287 const char *resolve_botid(int botid) {
288 struct cmd_bot_alias *botalias;
289 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
290 if(botalias->botid == botid)
291 return botalias->alias;
296 int resolve_botalias(const char *alias) {
297 struct cmd_bot_alias *botalias;
298 for(botalias = bot_aliases; botalias; botalias = botalias->next) {
299 if(!stricmp(botalias->alias, alias))
300 return botalias->botid;