Explorar o código

* Fxied up some misc bugs left from new conf system

svn: 1998
Bryan Drewery %!s(int64=21) %!d(string=hai) anos
pai
achega
693c4d652f
Modificáronse 6 ficheiros con 60 adicións e 48 borrados
  1. 46 42
      src/conf.c
  2. 2 1
      src/conf.h
  3. 7 3
      src/main.c
  4. 1 1
      src/main.h
  5. 2 0
      src/shell.c
  6. 2 1
      src/shell.h

+ 46 - 42
src/conf.c

@@ -36,7 +36,7 @@ conf_t conf;                    /* global conf struct */
 static void
 static void
 tellconf()
 tellconf()
 {
 {
-  conf_bot *bot;
+  conf_bot *bot = NULL;
   int i = 0;
   int i = 0;
 
 
   sdprintf("tempdir: %s\n", tempdir);
   sdprintf("tempdir: %s\n", tempdir);
@@ -54,8 +54,9 @@ tellconf()
   sdprintf("bots:\n");
   sdprintf("bots:\n");
   for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
   for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
     i++;
     i++;
-    sdprintf("%d: %s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d HUB: %d PID: %d\n", i,
+    sdprintf("%d: %s%s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d HUB: %d PID: %d\n", i,
              bot->nick,
              bot->nick,
+             bot->disabled ? "/" : "",
              bot->net.ip ? bot->net.ip : "",
              bot->net.ip ? bot->net.ip : "",
              bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "", 
              bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "", 
              bot->net.v6,
              bot->net.v6,
@@ -64,8 +65,9 @@ tellconf()
   }
   }
   if (conf.bot && ((bot = conf.bot))) {
   if (conf.bot && ((bot = conf.bot))) {
     sdprintf("me:\n");
     sdprintf("me:\n");
-    sdprintf("%s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d HUB: %d PID: %d\n",
+    sdprintf("%s%s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d HUB: %d PID: %d\n",
              bot->nick,
              bot->nick,
+             bot->disabled ? "/" : "",
              bot->net.ip ? bot->net.ip : "",
              bot->net.ip ? bot->net.ip : "",
              bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "", 
              bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "", 
              bot->net.v6,
              bot->net.v6,
@@ -99,7 +101,7 @@ spawnbots()
   for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
   for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
     sdprintf("checking bot: %s", bot->nick);
     sdprintf("checking bot: %s", bot->nick);
 
 
-    if (bot->nick[0] == '/') {
+    if (bot->disabled) {
       /* kill it if running */
       /* kill it if running */
       if (bot->pid)
       if (bot->pid)
         kill(bot->pid, SIGKILL);
         kill(bot->pid, SIGKILL);
@@ -131,9 +133,7 @@ killbot(char *botnick, int signal)
   conf_bot *bot = NULL;
   conf_bot *bot = NULL;
 
 
   for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
   for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
-    if (bot->nick[0] == '/')
-      continue;
-    else if (!egg_strcasecmp(botnick, bot->nick)) {
+    if (!egg_strcasecmp(botnick, bot->nick)) {
       if (bot->pid)
       if (bot->pid)
         return kill(bot->pid, signal);
         return kill(bot->pid, signal);
     }
     }
@@ -321,16 +321,13 @@ void conf_checkpids()
  */
  */
 
 
 pid_t
 pid_t
-checkpid(char *nick, conf_bot *bot)
+checkpid(const char *nick, conf_bot *bot)
 {
 {
   FILE *f = NULL;
   FILE *f = NULL;
   char buf[DIRMAX] = "", s[11] = "", *tmpnick = NULL, *tmp_ptr = NULL;
   char buf[DIRMAX] = "", s[11] = "", *tmpnick = NULL, *tmp_ptr = NULL;
 
 
   tmpnick = tmp_ptr = strdup(nick);
   tmpnick = tmp_ptr = strdup(nick);
 
 
-  if (tmpnick[0] == '/')
-    tmpnick++;
-
   simple_snprintf(buf, sizeof buf, "%s.pid.%s", tempdir, tmpnick);
   simple_snprintf(buf, sizeof buf, "%s.pid.%s", tempdir, tmpnick);
   free(tmp_ptr);
   free(tmp_ptr);
 
 
@@ -369,6 +366,10 @@ conf_addbot(char *nick, char *ip, char *host, char *ip6)
 
 
   bot->next = NULL;
   bot->next = NULL;
   bot->pid_file = NULL;
   bot->pid_file = NULL;
+  if (nick[0] == '/') {
+    bot->disabled = 1;
+    nick++;
+  }
   bot->nick = strdup(nick);
   bot->nick = strdup(nick);
   bot->net.ip = NULL;
   bot->net.ip = NULL;
   bot->net.host = NULL;
   bot->net.host = NULL;
@@ -410,16 +411,18 @@ conf_addbot(char *nick, char *ip, char *host, char *ip6)
     }
     }
   }
   }
 
 
-  if (!bot->hub && !conf.bots) {
+  /* not a hub 
+   AND
+   * no bots added yet (first bot) yet, not disabled.
+   OR
+   * bots already listed but we dont have a localhub yet, so we're it!
+   */
+  if (!bot->hub && ((!conf.bots && !bot->disabled) || (conf.bots && !conf.localhub))) {
     bot->localhub = 1;          /* first bot */
     bot->localhub = 1;          /* first bot */
-    conf.localhub = strdup(nick ? nick : origbotname);
-    /* perhaps they did -B localhub-bot ? */
-    if (origbotname[0] && !strcmp(origbotname, bot->nick))
-      localhub = 1;
+    conf.localhub = strdup(bot->nick);
   }
   }
 
 
   list_append((struct list_type **) &(conf.bots), (struct list_type *) bot);
   list_append((struct list_type **) &(conf.bots), (struct list_type *) bot);
-//  list_append((struct list_type **) &(cache->cchan), (struct list_type *) cchan);
 }
 }
 
 
 void
 void
@@ -778,52 +781,53 @@ writeconf(char *filename, FILE * stream, int bits)
 }
 }
 
 
 static void
 static void
-conf_bot_dup(conf_bot * dest, conf_bot * src)
+conf_bot_dup(conf_bot *dest, conf_bot *src)
 {
 {
-  dest->nick = src->nick ? strdup(src->nick) : NULL;
-  dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
-  dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
-  dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
-  dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
-  dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
-  dest->net.v6 = src->net.v6;
-  dest->u = src->u ? src->u : NULL;
-  dest->pid = src->pid;
-  dest->hub = src->hub;
-  dest->localhub = src->localhub;
-  dest->next = NULL;
+  if (dest && src) {
+    dest->nick = src->nick ? strdup(src->nick) : NULL;
+    dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
+    dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
+    dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
+    dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
+    dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
+    dest->net.v6 = src->net.v6;
+    dest->u = src->u ? src->u : NULL;
+    dest->pid = src->pid;
+    dest->hub = src->hub;
+    dest->localhub = src->localhub;
+    dest->next = NULL;
+  }
 }
 }
 
 
 
 
 void
 void
 fill_conf_bot()
 fill_conf_bot()
 {
 {
-  conf_bot *me = NULL;
-  char *mynick = NULL;
-
   if (!conf.bots || !conf.bots->nick)
   if (!conf.bots || !conf.bots->nick)
     return;
     return;
 
 
-  if (localhub && conf.bots && conf.bots->nick) {
+  char *mynick = NULL;
+  conf_bot *me = NULL;
+
+  if (!used_B && conf.bots && conf.bots->nick) {
     mynick = strdup(conf.bots->nick);
     mynick = strdup(conf.bots->nick);
     strlcpy(origbotname, conf.bots->nick, NICKLEN + 1);
     strlcpy(origbotname, conf.bots->nick, NICKLEN + 1);
   } else
   } else
     mynick = strdup(origbotname);
     mynick = strdup(origbotname);
 
 
-  for (me = conf.bots; me && me->nick; me = me->next)
-    if (!strcmp(me->nick, mynick))
+  for (me = conf.bots; me && me->nick; me = bot->next)
+    if (!egg_strcasecmp(me->nick, mynick))
       break;
       break;
 
 
-  if (me->nick && me->nick[0] == '/')
-    werr(ERR_BOTDISABLED);
-
-  if (!me->nick || (me->nick && strcmp(me->nick, mynick)))
+  if (!me || (me->nick && egg_strcasecmp(me->nick, mynick)))
     werr(ERR_BADBOT);
     werr(ERR_BADBOT);
 
 
   free(mynick);
   free(mynick);
   /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
   /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
-  conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
-  conf_bot_dup(conf.bot, me);
+  if (bot) {
+    conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
+    conf_bot_dup(conf.bot, bot);
+  }
 }
 }
 
 
 void
 void

+ 2 - 1
src/conf.h

@@ -22,6 +22,7 @@ typedef struct conf_bot_b {
   pid_t pid;              /* contains the PID for the bot (read for the pidfile) */
   pid_t pid;              /* contains the PID for the bot (read for the pidfile) */
   int localhub;         /* bot is localhub */
   int localhub;         /* bot is localhub */
   bool hub;		/* should bot behave as a hub? */
   bool hub;		/* should bot behave as a hub? */
+  bool disabled;	/* is bot disabled in the conf? */
   char *nick;
   char *nick;
   char *pid_file;       /* path and filename of the .pid file */
   char *pid_file;       /* path and filename of the .pid file */
 } conf_bot;
 } conf_bot;
@@ -58,7 +59,7 @@ int killbot(char *, 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(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();

+ 7 - 3
src/main.c

@@ -69,7 +69,6 @@ extern int		optind;
 const time_t 	buildts = CVSBUILD;		/* build timestamp (UTC) */
 const time_t 	buildts = CVSBUILD;		/* build timestamp (UTC) */
 const char	*egg_version = "1.2.3-cvs";
 const char	*egg_version = "1.2.3-cvs";
 
 
-bool 	localhub = 1; 		/* we set this to 0 if we get a -B */
 bool	used_B = 0;		/* did we get started with -B? */
 bool	used_B = 0;		/* did we get started with -B? */
 int 	role;
 int 	role;
 bool 	loading = 0;
 bool 	loading = 0;
@@ -303,7 +302,6 @@ static void dtx_arg(int argc, char *argv[])
         unlink(binname);
         unlink(binname);
         exit(0);
         exit(0);
       case 'B':
       case 'B':
-        localhub = 0;
         used_B = 1;
         used_B = 1;
         strlcpy(origbotname, optarg, NICKLEN + 1);
         strlcpy(origbotname, optarg, NICKLEN + 1);
         break;
         break;
@@ -398,7 +396,6 @@ static void dtx_arg(int argc, char *argv[])
           exit(3);
           exit(3);
         else
         else
           sdprintf("Updating...");
           sdprintf("Updating...");
-        localhub = 1;
         updating = UPDATE_AUTO;
         updating = UPDATE_AUTO;
         break;
         break;
       case '?':
       case '?':
@@ -614,6 +611,13 @@ static void startup_checks(int hack) {
       exit(0); /* our job is done! */
       exit(0); /* our job is done! */
     }
     }
   }
   }
+
+  if (!conf.bot)
+    werr(ERR_NOBOT);
+
+  if (conf.bot->disabled)
+    werr(ERR_BOTDISABLED);
+
   if (!conf.bot->localhub)
   if (!conf.bot->localhub)
     free_conf_bots();			/* not a localhub, so no need to store all bot info */
     free_conf_bots();			/* not a localhub, so no need to store all bot info */
 }
 }

+ 1 - 1
src/main.h

@@ -10,7 +10,7 @@ enum {
 
 
 extern int		role, default_flags, default_uflags,
 extern int		role, default_flags, default_uflags,
 			updating;
 			updating;
-extern bool		use_stderr, backgrd, localhub, term_z, loading, have_take;
+extern bool		use_stderr, backgrd, used_B, term_z, loading, have_take;
 extern char		tempdir[], *binname, owner[], version[], ver[], quit_msg[];
 extern char		tempdir[], *binname, owner[], version[], ver[], quit_msg[];
 extern time_t		online_since, now;
 extern time_t		online_since, now;
 extern uid_t		myuid;
 extern uid_t		myuid;

+ 2 - 0
src/shell.c

@@ -648,6 +648,8 @@ char *werr_tostr(int errnum)
     return STR("Bot is disabled, remove '/' in config");
     return STR("Bot is disabled, remove '/' in config");
   case ERR_NOBOTS:
   case ERR_NOBOTS:
     return STR("There are no bots in the binary! Please use ./binary -C to edit");
     return STR("There are no bots in the binary! Please use ./binary -C to edit");
+  case ERR_NOBOT:
+    return STR("I have no bot record but received -B???");
   default:
   default:
     return "Unforseen error";
     return "Unforseen error";
   }
   }

+ 2 - 1
src/shell.h

@@ -18,7 +18,8 @@
 #define ERR_BADPASS	16
 #define ERR_BADPASS	16
 #define ERR_BOTDISABLED	17
 #define ERR_BOTDISABLED	17
 #define ERR_NOBOTS	18
 #define ERR_NOBOTS	18
-#define ERR_MAX         19
+#define ERR_NOBOT	19
+#define ERR_MAX         20
 
 
 #define EMAIL_OWNERS    BIT0
 #define EMAIL_OWNERS    BIT0
 #define EMAIL_TEAM      BIT1
 #define EMAIL_TEAM      BIT1