-/* bots.c - NeonServ v5.1
+/* bots.c - NeonServ v5.2
* Copyright (C) 2011 Philipp Kreil (pk910)
*
* This program is free software: you can redistribute it and/or modify
*/
#include "bots.h"
+#include "timeq.h"
+#include "mysqlConn.h"
+#include "ClientSocket.h"
+#include "UserNode.h"
+#include "ChanNode.h"
+#include "ChanUser.h"
+#include "version.h"
+#include "modcmd.h"
+#include "DBHelper.h"
#include "bot_NeonServ.h"
+#include "bot_NeonSpam.h"
+#include "bot_DummyServ.h"
+#include "bot_NeonHelp.h"
+
+struct cmd_bot_alias {
+ int botid;
+ char *alias;
+ struct cmd_bot_alias *next;
+};
+
+static struct cmd_bot_alias *bot_aliases = NULL;
+
+static void start_zero_bots() {
+ struct ClientSocket *client;
+ MYSQL_RES *res, *res2;
+ MYSQL_ROW row;
+
+ printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '0' AND `active` = '1'");
+ res = mysql_use();
+
+ while ((row = mysql_fetch_row(res)) != NULL) {
+ client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
+ client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
+ client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
+ client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
+ client->botid = 0;
+ client->clientid = atoi(row[7]);
+ connect_socket(client);
+
+ printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '0' AND `botid` = '%d'", client->clientid);
+ res2 = mysql_use();
+ while ((row = mysql_fetch_row(res2)) != NULL) {
+ if(bind_botwise_cmd_to_command(0, client->clientid, row[0], row[1])) {
+ if(row[2] && strcmp(row[2], "")) {
+ bind_botwise_set_parameters(0, client->clientid, row[0], row[2]);
+ }
+ if(row[3]) {
+ bind_botwise_set_global_access(0, client->clientid, row[0], atoi(row[3]));
+ }
+ if(row[4]) {
+ bind_botwise_set_channel_access(0, client->clientid, row[0], row[4]);
+ }
+ }
+ }
+ bind_botwise_unbound_required_functions(0, client->clientid);
+ }
+}
+
+static void zero_bots_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ loadChannelSettings(chan);
+ if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
+ strcpy(trigger, "+");
+ return;
+ }
+ printf_mysql_query("SELECT `trigger` FROM `bot_channels` LEFT JOIN `bots` ON `botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '0' AND `botid` = '%d'", chan->channel_id, clientid);
+ res = mysql_use();
+ row = mysql_fetch_row(res);
+ strcpy(trigger, (strlen(row[0]) ? row[0] : "+"));
+}
void init_bots() {
init_NeonServ();
+ init_NeonSpam();
+ init_DummyServ();
+ init_NeonHelp();
+
+ set_bot_alias(0, "0");
+ start_zero_bots();
+ set_trigger_callback(0, zero_bots_trigger_callback);
+
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ //load all timed bans
+ printf_mysql_query("SELECT `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_timeout` > 0");
+ res = mysql_use();
+ char nameBuf[20];
+ while ((row = mysql_fetch_row(res)) != NULL) {
+ if(atol(row[1]) - time(0) > 0) {
+ sprintf(nameBuf, "ban_%s", row[0]);
+ timeq_add_name(nameBuf, atol(row[1]) - time(0), channel_ban_timeout, strdup(row[0]));
+ } else {
+ //timed out
+ printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[0]);
+ }
+ }
}
void loop_bots() {
loop_NeonServ();
+ loop_NeonSpam();
+ loop_DummyServ();
+ loop_NeonHelp();
}
void free_bots() {
free_NeonServ();
+ free_NeonSpam();
+ free_DummyServ();
+ free_NeonHelp();
+}
+
+struct ClientSocket *getChannelBot(struct ChanNode *chan, int botid) {
+ struct ClientSocket *bot, *use_bot = NULL, *second_bot = NULL, *third_bot = NULL;
+ struct ChanUser *chanuser;
+ for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+ if(botid && bot->botid != botid) continue;
+ if((chanuser = getChanUser(bot->user, chan)) != NULL) {
+ if((chanuser->flags & CHANUSERFLAG_OPPED)) {
+ use_bot = bot;
+ if(bot->flags & SOCKET_FLAG_PREFERRED) break;
+ } else if(bot->flags & SOCKET_FLAG_PREFERRED)
+ second_bot = bot;
+ else
+ third_bot = bot;
+ }
+ }
+ if(!use_bot) use_bot = second_bot;
+ if(!use_bot) use_bot = third_bot;
+ return use_bot;
+}
+
+void requestOp(struct UserNode *user, struct ChanNode *chan) {
+ struct ClientSocket *bot;
+ struct ChanUser *chanuser = getChanUser(user, chan);
+ char opped = 0;
+ if(!chanuser) return;
+ if((chanuser->flags & CHANUSERFLAG_OPPED)) return;
+ for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+ if((chanuser = getChanUser(bot->user, chan)) != NULL && (chanuser->flags & CHANUSERFLAG_OPPED)) {
+ opped = 1;
+ putsock(bot, "MODE %s +o %s", chan->name, user->nick);
+ break;
+ }
+ }
+ if(!opped) {
+ //self op?
+ }
+}
+
+TIMEQ_CALLBACK(channel_ban_timeout) {
+ char *str_banid = data;
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ printf_mysql_query("SELECT `ban_mask`, `channel_name` FROM `bans` LEFT JOIN `channels` ON `ban_channel` = `channel_id` WHERE `ban_id` = '%s'", str_banid);
+ res = mysql_use();
+ struct ChanNode *chan;
+ if((row = mysql_fetch_row(res)) != NULL && (chan = getChanByName(row[1])) != NULL) {
+ struct ClientSocket *use_bot = getChannelBot(chan, 0);
+ if(use_bot) {
+ putsock(use_bot, "MODE %s -b %s", chan->name, row[0]);
+ }
+ printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", str_banid);
+ }
+ free(str_banid);
+}
+
+static int general_ctcp(char *buffer, char *command, char *text);
+
+void general_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
+ char ctcpBuf[MAXLEN];
+ if(general_ctcp(ctcpBuf, command, text)) {
+ struct ClientSocket *bot;
+ for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+ if(bot->user == target) break;
+ }
+ if(bot)
+ putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
+ }
+}
+
+static int general_ctcp(char *buffer, char *command, char *text) {
+ if(!stricmp(command, "VERSION")) {
+ sprintf(buffer, "VERSION NeonServ v%s.%d by pk910 (%s)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"));
+ return 1;
+ }
+ if(!stricmp(command, "FINGER")) {
+ sprintf(buffer, "FINGER NeonServ v%s.%d (%s) build %s lines C code using " COMPILER " (see +netinfo)", NEONSERV_VERSION, patchlevel, (strcmp(revision, "") ? revision : "-"), codelines);
+ return 1;
+ }
+ if(!stricmp(command, "PING")) {
+ sprintf(buffer, "PING %s", (text ? text : "0"));
+ return 1;
+ }
+ if(!stricmp(command, "TIME")) {
+ time_t rawtime;
+ struct tm *timeinfo;
+ char timeBuf[80];
+ time(&rawtime);
+ timeinfo = localtime(&rawtime);
+ strftime(timeBuf, 80, "%c", timeinfo);
+ sprintf(buffer, "TIME %s", timeBuf);
+ return 1;
+ }
+ return 0;
+}
+
+void set_bot_alias(int botid, char *alias) {
+ struct cmd_bot_alias *botalias, *existing_alias = NULL;
+ for(botalias = bot_aliases; botalias; botalias = botalias->next) {
+ if(!stricmp(botalias->alias, alias))
+ return;
+ if(botalias->botid == botid)
+ existing_alias = botalias;
+ }
+ if(existing_alias) {
+ free(existing_alias->alias);
+ existing_alias->alias = strdup(alias);
+ return;
+ }
+ botalias = malloc(sizeof(*botalias));
+ if (!botalias) {
+ perror("malloc() failed");
+ return;
+ }
+ botalias->botid = botid;
+ botalias->alias = strdup(alias);
+ botalias->next = bot_aliases;
+ bot_aliases = botalias;
+}
+
+const char *resolve_botid(int botid) {
+ struct cmd_bot_alias *botalias;
+ for(botalias = bot_aliases; botalias; botalias = botalias->next) {
+ if(botalias->botid == botid)
+ return botalias->alias;
+ }
+ return NULL;
+}
+
+int resolve_botalias(const char *alias) {
+ struct cmd_bot_alias *botalias;
+ for(botalias = bot_aliases; botalias; botalias = botalias->next) {
+ if(!stricmp(botalias->alias, alias))
+ return botalias->botid;
+ }
+ return -1;
}