*** VERSION 5.2.0 ***
[NeonServV5.git] / src / bots.c
index 43096d4079f548342b9878dc57f34f4e18d72d34..21d43a06d5f208e22e7ff7eb96378037ad3ba671 100644 (file)
@@ -1,4 +1,4 @@
-/* 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 "bot_NeonServ.h"
+#include "bot_NeonSpam.h"
 
 void init_bots() {
     init_NeonServ();
+    init_NeonSpam();
+    
+    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();
 }
 
 void free_bots() {
     free_NeonServ();
+    free_NeonSpam();
+}
+
+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;
+}
+
+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;
 }