Bläddra i källkod

Merge branch 'ducch-435-suicide-removes-bots'

* ducch-435-suicide-removes-bots:
  * Cleanups
  * Update docs
  * Suicide (from the localhub) will now remove all bots in conf.bots and then the localhub itself.
  * Suicide will take place through the localhub.
Bryan Drewery 15 år sedan
förälder
incheckning
2dcb0b395b
3 ändrade filer med 51 tillägg och 10 borttagningar
  1. 1 0
      doc/UPDATES
  2. 22 0
      src/botcmd.c
  3. 28 10
      src/shell.c

+ 1 - 0
doc/UPDATES

@@ -20,6 +20,7 @@
   * libcrypto (openssl) is now loaded at startup and is required.
   * Added TCL support. This is *only* a .tcl command currently, no scripts are loadable yet.
   * Fix blowfish not working correctly on 64bit
+  * Suicide will now remove all bots related to the binary being removed (fixes #435)
 
 1.3.1 - http://wraith.botpack.net/milestone/1.3.1
   * Fix crash related to slowpart

+ 22 - 0
src/botcmd.c

@@ -47,6 +47,7 @@
 #include "chan.h"
 #include "core_binds.h"
 #include "egg_timer.h"
+#include "shell.h"
 
 static char TBUF[1024] = "";		/* Static buffer for goofy bot stuff */
 
@@ -1369,11 +1370,32 @@ static void bot_rd(char* botnick, char* code, char* msg)
   }
 }
 
+static void bot_suicide(char* botnick, char* code, char* msg)
+{
+  conf_bot *bot = NULL;
+  bool valid_source = 0;
+
+  for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
+    if (!strcmp(botnick, bot->nick)) {
+      valid_source = 1;
+      break;
+    }
+  }
+
+  if (!valid_source) {
+    putlog(LOG_WARN, "*", STR("AN INVALID BOT (%s) JUST SENT ME A SUICIDE REQUEST!"), botnick);
+    return;
+  }
+
+  suicide(msg);
+}
+
 static cmd_t my_bot[] = 
 {
   {"rd",	"",	(Function) bot_rd,	NULL, 0},
   {"r-sr",	"",	(Function) bot_rsimr,	NULL, HUB},
   {"r-s",	"",	(Function) bot_rsim,	NULL, 0},
+  {"suicide",	"",	(Function) bot_suicide,	NULL, 0},
   {NULL, 	NULL, 	NULL, 			NULL, 0}
 };
 

+ 28 - 10
src/shell.c

@@ -69,6 +69,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <dirent.h>
+#include "botmsg.h"
 
 bool clear_tmpdir = 0;
 
@@ -501,9 +502,27 @@ void suicide(const char *msg)
 {
   char tmp[512] = "";
 
-  putlog(LOG_WARN, "*", STR("Comitting suicide: %s"), msg);
-  simple_snprintf(tmp, sizeof(tmp), STR("Suicide: %s"), msg);
-  set_user(&USERENTRY_COMMENT, conf.bot->u, tmp);
+  if (!conf.bot->localhub) {
+    //im not a localhub, ask the localhub to suicide
+    simple_snprintf(tmp, sizeof(tmp), STR("suicide %s"), msg);
+    putbot(conf.localhub, tmp);
+    return;
+  } else {
+    //im the localhub, loop thru bots and kill 'em
+    putlog(LOG_WARN, "*", STR("Comitting suicide: %s"), msg);
+    crontab_del();
+
+    conf_bot *bot = NULL;
+    for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
+      if (!strcmp(conf.bot->nick, bot->nick))
+        continue; //skip myself or i wont be able to remove the rest
+      bot->pid = checkpid(bot->nick, bot);
+      conf_killbot(conf.bots, NULL, bot, SIGKILL);
+      unlink(bot->pid_file);
+      deluser(bot->nick);
+    }
+  }
+
   if (!conf.bot->hub) {
     nuke_server(STR("kill the infidels!"));
     sleep(1);
@@ -520,17 +539,16 @@ void suicide(const char *msg)
     simple_snprintf(tmp, sizeof(tmp), STR("%s/.u.1"), conf.datadir);
     unlink(tmp);
   }
-  unlink(binname);
 
-  if (conf.bot->localhub) {
-    conf_checkpids(conf.bots);
-    conf_killbot(conf.bots, NULL, NULL, SIGKILL);
-  }
-  unlink(conf.bot->pid_file);
+  unlink(binname);
   //Not recursively clearing these dirs as they may be ~USER/ ..
   unlink(conf.datadir); //Probably will fail, shrug
   unlink(tempdir); //Probably will fail too, oh well
-  crontab_del();
+
+  //now deal with myself after the rest of the conf.bots are gone
+  deluser(conf.bot->nick);
+  unlink(conf.bot->pid_file);
+  //and die in agony!
   fatal(msg, 0);
 }