Переглянути джерело

* Finished localhub spawning bots.
* Will not load bots with / in front of name
Also, will auto kill() any /bots with SIGKILL


svn: 712

Bryan Drewery 22 роки тому
батько
коміт
a5b3286fe3
5 змінених файлів з 70 додано та 13 видалено
  1. 55 10
      src/conf.c
  2. 5 2
      src/conf.h
  3. 6 0
      src/main.c
  4. 2 0
      src/shell.c
  5. 2 1
      src/shell.h

+ 55 - 10
src/conf.c

@@ -24,7 +24,7 @@
 #include <signal.h>
 
 extern char             origbotname[], tempdir[],
-                        userfile[], natip[];
+                        userfile[], natip[], *binname;
 extern int              localhub;
 extern uid_t		myuid;
 extern conf_t           conf;
@@ -33,6 +33,31 @@ conf_t		conf;		/* global conf struct */
 
 static conf_t conffile;
 
+#ifdef LEAF
+void spawnbots() {
+  conf_bot *bot = NULL;
+
+  for (bot = conffile.bots; bot && bot->nick; bot = bot->next) {
+    if (bot->nick[0] == '/') {
+      /* kill it if running */
+      if (bot->pid) kill(bot->pid, SIGKILL);
+      else continue;
+    } else if (!strcmp(bot->nick, conf.bot->nick) || bot->pid) {
+      continue;
+    } else {
+      char *run = NULL;
+      size_t size = 0;
+  
+      size = strlen(conf.bot->nick) + strlen(binname) + 10;
+      run = calloc(1, size);
+      egg_snprintf(run, size, "%s -B %s", binname, bot->nick);
+      system(run);
+      free(run);
+    }
+  }
+}
+#endif /* LEAF */
+
 #ifdef S_CONFEDIT
 static uid_t save_euid, save_egid;
 static int swap_uids()
@@ -182,26 +207,43 @@ void init_conf() {
  * Return the PID of a bot if it is running, otherwise return 0
  */
 
-int checkpid(char *nick, conf_bot *bot) {
+pid_t checkpid(char *nick, conf_bot *bot) {
   FILE *f = NULL;
-  int xx;
-  char buf[DIRMAX] = "", s[11] = "";
+  char buf[DIRMAX] = "", s[11] = "", *tmpnick = NULL, *tmp_ptr = NULL;
+
+
+  tmpnick = tmp_ptr = strdup(nick);
+
+  if (tmpnick[0] == '/')
+    tmpnick++;
+
+  egg_snprintf(buf, sizeof buf, "%s.pid.%s", tempdir, tmpnick);
+  free(tmp_ptr);
 
-  egg_snprintf(buf, sizeof buf, "%s.pid.%s", tempdir, nick);
   if (bot && !(bot->pid_file))
     bot->pid_file = strdup(buf);
   else if (bot && strcmp(bot->pid_file, buf))
     str_redup(&bot->pid_file, buf);
 
   if ((f = fopen(buf, "r"))) {
+    pid_t xx = 0;
+
     fgets(s, 10, f);
+    s[10] = 0;
     fclose(f);
     xx = atoi(s);
-    kill(xx, SIGCHLD);
-
-    if (errno != ESRCH) /* PID is !running */
-      return xx;
+    if (bot) {
+      int x = 0;
+      x = kill(xx, SIGCHLD);
+      if (x == -1 && errno == ESRCH) 
+        return 0;
+      else if (x == 0)
+        return xx;
+    } else {
+      return xx ? xx : 0;
+    }
   }
+
   return 0;
 }
 
@@ -347,7 +389,7 @@ int readconf(char *cfile)
     i++;
 
     sdprintf("CONF LINE: %s", line);
-    if (!strchr("*#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ", line[0])) {
+    if (!strchr("*/#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ", line[0])) {
       sdprintf(STR("line %d, char %c "), i, line[0]);
       werr(ERR_CONFBADENC);
     } else {                    /* line is good to parse */
@@ -572,6 +614,9 @@ void fillconf(conf_t *inconf) {
   for (bot = conffile.bots; bot && bot->nick; bot = bot->next)
     if (!strcmp(bot->nick, mynick)) break;
 
+  if (bot->nick && bot->nick[0] == '/')
+    werr(ERR_BOTDISABLED);
+
   if (!bot->nick || (bot->nick && strcmp(bot->nick, mynick)))
     werr(ERR_BADBOT);
 

+ 5 - 2
src/conf.h

@@ -10,7 +10,7 @@ typedef struct conf_bot_b {
   char *host6;
   char *ip;
   char *ip6;
-  int pid;              /* contains the PID for the bot (read for the pidfile) */
+  pid_t pid;              /* contains the PID for the bot (read for the pidfile) */
   char *pid_file;       /* path and filename of the .pid file */
 #ifdef LEAF
   int localhub;         /* bot is localhub */
@@ -46,10 +46,13 @@ enum {
 
 #ifndef MAKING_MODS
 
+#ifdef LEAF
+void spawnbots();
+#endif /* LEAF */
 #ifdef S_CONFEDIT
 void confedit(char *);
 #endif /* S_CONFEDIT */
-int checkpid(char *, conf_bot *);
+pid_t checkpid(char *, conf_bot *);
 void showconf();
 void init_conf();
 void free_conf();

+ 6 - 0
src/main.c

@@ -674,6 +674,12 @@ int main(int argc, char **argv)
   }
 #endif /* LEAF */
   fillconf(&conf);
+#ifdef LEAF
+  if (localhub) {
+    spawnbots();
+    if (updating) exit(0); /* just let cron restart us bleh */
+  }
+#endif /* LEAF */
   free_conf();
 
   if ((localhub && !updating) || !localhub) {

+ 2 - 0
src/shell.c

@@ -649,6 +649,8 @@ char *werr_tostr(int errnum)
     return STR("Config file is incomplete");
   case ERR_BADBOT:
     return STR("No such botnick");
+  case ERR_BOTDISABLED:
+    return STR("Bot is disabled, remove '/' in config");
   default:
     return STR("Unforseen error");
   }

+ 2 - 1
src/shell.h

@@ -17,7 +17,8 @@
 #define ERR_BADCONF     14
 #define ERR_BADBOT	15
 #define ERR_BADPASS	16
-#define ERR_MAX         17
+#define ERR_BOTDISABLED	17
+#define ERR_MAX         18
 
 #define EMAIL_OWNERS    0x1
 #define EMAIL_TEAM      0x2