瀏覽代碼

* After editing the binary with -C, also kill/remove any bots which were removed from the list

svn: 2010
Bryan Drewery 21 年之前
父節點
當前提交
57ac308bc9
共有 5 個文件被更改,包括 82 次插入24 次删除
  1. 4 2
      doc/UPDATES
  2. 6 1
      src/binary.c
  3. 66 17
      src/conf.c
  4. 4 2
      src/conf.h
  5. 2 2
      src/main.c

+ 4 - 2
doc/UPDATES

@@ -75,8 +75,10 @@ Lines prefixxed with '-' were disabled before release and are not finishsed, or
 * +O now implies +o
 * +O now implies +o
 * Combined kick reasons for +k and .kickban
 * Combined kick reasons for +k and .kickban
 * Fixed cmd_conf not correctly writing settings back to the binary
 * Fixed cmd_conf not correctly writing settings back to the binary
-* After editing binary with -C, new bots/hosts are automatically added if first bot is linked.
-  Plus, bots are automatically killed/started now.
+* Some automatic things done after editing the binary with -C are:
+  -New bots are spawned and added (if linked)
+  -Changed (NEW) ip/host for bots are added to the botnet (if linked)
+  -Removed bots are killed and removed from the userlist (if linked)
 * Added options 'enable' and 'disable' to cmd_conf for bots
 * Added options 'enable' and 'disable' to cmd_conf for bots
 
 
 
 

+ 6 - 1
src/binary.c

@@ -504,15 +504,20 @@ void conf_to_bin(conf_t *in, bool move, int die)
 
 
 void reload_bin_data() {
 void reload_bin_data() {
   if (bin_checksum(binname, GET_CONF)) {
   if (bin_checksum(binname, GET_CONF)) {
+    conf_bot *oldlist = NULL;
+    
+    oldlist = conf_bots_dup(conf.bots);
     free_conf();
     free_conf();
     bin_to_conf();
     bin_to_conf();
     fill_conf_bot();
     fill_conf_bot();
     if (!conf.bot->localhub)
     if (!conf.bot->localhub)
-      free_conf_bots();
+      free_conf_bots(conf.bots);
     else {
     else {
+      kill_removed_bots(oldlist, conf.bots);
       conf_add_userlist_bots();
       conf_add_userlist_bots();
       spawnbots();
       spawnbots();
     }
     }
+    free_conf_bots(oldlist);
   }
   }
 }
 }
 
 

+ 66 - 17
src/conf.c

@@ -14,6 +14,7 @@
 #include "crypt.h"
 #include "crypt.h"
 #include "main.h"
 #include "main.h"
 #include "settings.h"
 #include "settings.h"
+#include "src/mod/irc.mod/irc.h"
 #include "misc.h"
 #include "misc.h"
 #include "users.h"
 #include "users.h"
 #include "misc_file.h"
 #include "misc_file.h"
@@ -128,16 +129,16 @@ spawnbots()
 }
 }
 
 
 int
 int
-killbot(char *botnick, int signal)
+killbot(char *botnick, conf_bot *bot, int signal)
 {
 {
-  conf_bot *bot = NULL;
-
-  for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
-    if (!egg_strcasecmp(botnick, bot->nick)) {
-      if (bot->pid)
-        return kill(bot->pid, signal);
-    }
+  if (!bot) {
+    for (bot = conf.bots; bot && bot->nick; bot = bot->next)
+      if (!egg_strcasecmp(botnick, bot->nick))
+        break;
   }
   }
+  if (bot)
+    if (bot->pid)
+      return kill(bot->pid, signal);
   return -1;
   return -1;
 }
 }
 
 
@@ -454,7 +455,7 @@ conf_delbot(char *botn)
   for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
   for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
     if (!strcmp(bot->nick, botn)) {     /* found it! */
     if (!strcmp(bot->nick, botn)) {     /* found it! */
       bot->pid = checkpid(bot->nick, bot);
       bot->pid = checkpid(bot->nick, bot);
-      killbot(bot->nick, SIGKILL);
+      killbot(NULL, bot, SIGKILL);
       free_bot(bot);
       free_bot(bot);
       return 0;
       return 0;
     }
     }
@@ -465,7 +466,7 @@ conf_delbot(char *botn)
 void
 void
 free_conf()
 free_conf()
 {
 {
-  free_conf_bots();
+  free_conf_bots(conf.bots);
   free_bot(conf.bot);
   free_bot(conf.bot);
   conf.bot = NULL;
   conf.bot = NULL;
   free(conf.localhub);
   free(conf.localhub);
@@ -477,15 +478,17 @@ free_conf()
 }
 }
 
 
 void
 void
-free_conf_bots(void)
+free_conf_bots(conf_bot *list)
 {
 {
-  conf_bot *bot = NULL, *bot_n = NULL;
+  if (list) {
+    conf_bot *bot = NULL, *bot_n = NULL;
 
 
-  for (bot = conf.bots; bot; bot = bot_n) {
-    bot_n = bot->next;
-    free_bot(bot);
+    for (bot = list; bot; bot = bot_n) {
+      bot_n = bot->next;
+      free_bot(bot);
+    }
+    list = NULL;
   }
   }
-  conf.bots = NULL;
 }
 }
 
 
 int
 int
@@ -537,7 +540,7 @@ readconf(const char *fname, int bits)
   if (!(f = fopen(fname, "r")))
   if (!(f = fopen(fname, "r")))
     fatal("Cannot read config", 0);
     fatal("Cannot read config", 0);
 
 
-  free_conf_bots();
+  free_conf_bots(conf.bots);
   inbuf = (char *) my_calloc(1, 201);
   inbuf = (char *) my_calloc(1, 201);
   while (fgets(inbuf, 201, f) != NULL) {
   while (fgets(inbuf, 201, f) != NULL) {
     char *line = NULL, *temp_ptr = NULL;
     char *line = NULL, *temp_ptr = NULL;
@@ -802,6 +805,51 @@ conf_bot_dup(conf_bot *dest, conf_bot *src)
   }
   }
 }
 }
 
 
+conf_bot *conf_bots_dup(conf_bot *src)
+{
+  conf_bot *ret = NULL;
+  if (src) {
+    conf_bot *bot = NULL, *newbot = NULL;
+
+    for (bot = src; bot && bot->nick; bot = bot->next) {
+      newbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
+      conf_bot_dup(newbot, bot);
+      list_append((struct list_type **) &(ret), (struct list_type *) newbot);
+    }
+  }
+  return ret;
+}
+
+void kill_removed_bots(conf_bot *oldlist, conf_bot *newlist)
+{
+  if (oldlist && newlist) {
+    conf_bot *botold = NULL, *botnew = NULL;
+    bool found = 0;
+    struct userrec *u = NULL;
+
+    for (botold = oldlist; botold && botold->nick; botold = botold->next) {
+      found = 0;
+      for (botnew = newlist; botnew && botnew->nick; botnew = botnew->next) {
+        if (!egg_strcasecmp(botold->nick, botnew->nick)) {
+          found = 1;
+          break;
+        }
+      }
+      if (!found) {
+        killbot(NULL, botold, SIGKILL);
+        if ((u = get_user_by_handle(userlist, botold->nick))) {
+          putlog(LOG_MISC, "*", "Removing '%s' as it has been removed from the binary config.", botold->nick);
+          if (!conf.bot->hub)
+            check_this_user(botold->nick, 1, NULL);
+          if (deluser(botold->nick)) {
+            if (conf.bot->hub)
+              write_userfile(-1);
+          }
+        }
+      }
+    }
+  }
+}
 
 
 void
 void
 fill_conf_bot()
 fill_conf_bot()
@@ -909,6 +957,7 @@ void conf_add_userlist_bots()
       u = get_user_by_handle(userlist, bot->nick);
       u = get_user_by_handle(userlist, bot->nick);
       if (!u) {
       if (!u) {
         userlist = adduser(userlist, bot->nick, "none", "-", USER_OP, 1);
         userlist = adduser(userlist, bot->nick, "none", "-", USER_OP, 1);
+        putlog(LOG_MISC, "*", "Adding '%s' as it has been added to the binary config.", bot->nick);
         u = get_user_by_handle(userlist, bot->nick);
         u = get_user_by_handle(userlist, bot->nick);
 
 
         egg_snprintf(tmp, sizeof(tmp), "%li [internal]", now);
         egg_snprintf(tmp, sizeof(tmp), "%li [internal]", now);

+ 4 - 2
src/conf.h

@@ -55,14 +55,14 @@ enum {
 
 
 void spawnbot(const char *);
 void spawnbot(const char *);
 void spawnbots();
 void spawnbots();
-int killbot(char *, int);
+int killbot(char *, conf_bot *, int);
 void confedit() __attribute__((noreturn));
 void confedit() __attribute__((noreturn));
 void conf_addbot(char *, char *, char *, char *);
 void conf_addbot(char *, char *, char *, char *);
 int conf_delbot(char *);
 int conf_delbot(char *);
 pid_t checkpid(const char *, conf_bot *);
 pid_t checkpid(const char *, conf_bot *);
 void init_conf();
 void init_conf();
 void free_conf();
 void free_conf();
-void free_conf_bots();
+void free_conf_bots(conf_bot *);
 int readconf(const char *, int);
 int readconf(const char *, int);
 int parseconf(bool);
 int parseconf(bool);
 int writeconf(char *, FILE *, int);
 int writeconf(char *, FILE *, int);
@@ -70,6 +70,8 @@ void fill_conf_bot();
 void bin_to_conf(void);
 void bin_to_conf(void);
 void conf_checkpids();
 void conf_checkpids();
 void conf_add_userlist_bots();
 void conf_add_userlist_bots();
+conf_bot *conf_bots_dup(conf_bot *);
+void kill_removed_bots(conf_bot *, conf_bot *);
 
 
 #ifdef CYGWIN_HACKS
 #ifdef CYGWIN_HACKS
 extern char		cfile[DIRMAX];
 extern char		cfile[DIRMAX];

+ 2 - 2
src/main.c

@@ -581,7 +581,7 @@ static void startup_checks(int hack) {
     if (do_killbot[0]) {
     if (do_killbot[0]) {
       const char *what = (kill_sig == SIGKILL ? "kill" : "restart");
       const char *what = (kill_sig == SIGKILL ? "kill" : "restart");
 
 
-      if (killbot(do_killbot, kill_sig) == 0)
+      if (killbot(do_killbot, NULL, kill_sig) == 0)
         printf("'%s' successfully %sed.\n", do_killbot, what);
         printf("'%s' successfully %sed.\n", do_killbot, what);
       else {
       else {
         printf("Error %sing '%s'\n", what, do_killbot);
         printf("Error %sing '%s'\n", what, do_killbot);
@@ -619,7 +619,7 @@ static void startup_checks(int hack) {
     werr(ERR_BOTDISABLED);
     werr(ERR_BOTDISABLED);
 
 
   if (!conf.bot->hub && !conf.bot->localhub)
   if (!conf.bot->hub && !conf.bot->localhub)
-    free_conf_bots();			/* not a localhub, so no need to store all bot info */
+    free_conf_bots(conf.bots);			/* not a localhub, so no need to store all bot info */
 }
 }
 
 
 static char *fake_md5 = "596a96cc7bf9108cd896f33c44aedc8a";
 static char *fake_md5 = "596a96cc7bf9108cd896f33c44aedc8a";