Przeglądaj źródła

* Read/Write hubs to conf

Bryan Drewery 14 lat temu
rodzic
commit
cf726cf0c6
3 zmienionych plików z 42 dodań i 4 usunięć
  1. 1 3
      src/binary.c
  2. 39 1
      src/conf.c
  3. 2 0
      src/conf.h

+ 1 - 3
src/binary.c

@@ -674,7 +674,6 @@ void conf_to_bin(conf_t *in, bool move, int die)
 {
   conf_bot *bot = NULL;
   char *newbin = NULL;
-  char *hubs = strdup(settings.hubs);
 
   clear_settings();
   sdprintf("converting conf to bin\n");
@@ -700,8 +699,7 @@ void conf_to_bin(conf_t *in, bool move, int die)
                            bot->net.ip6 ? bot->net.ip6 : "");
     }
 
-  simple_snprintf(settings.hubs, sizeof(settings.hubs), "%s", hubs);
-  free(hubs);
+  simple_snprintf(settings.hubs, sizeof(settings.hubs), in->hubs.join(',').c_str());
 
   newbin = binname;
 //  tellconfig(&settings); 

+ 39 - 1
src/conf.c

@@ -322,6 +322,7 @@ init_conf()
 //  conf.bots->next = NULL;
   conf.bots = NULL;
   conf.bot = NULL;
+  conf.hubs = bd::String(settings.hubs).split(',');
 
   conf.localhub = NULL;
   conf.autocron = 1;
@@ -465,7 +466,7 @@ conf_addbot(const char *nick, const char *ip, const char *host, const char *ip6)
 
 //  bot->pid = checkpid(nick, bot);
 
-  if (settings.hubs && is_hub(bot->nick))
+  if (is_hub(bot->nick))
     bot->hub = 1;
 
   /* not a hub 
@@ -536,6 +537,7 @@ free_conf()
     free(conf.datadir);
   if (conf.homedir)
     free(conf.homedir);
+  conf.hubs.clear();
   init_conf();
 }
 
@@ -602,9 +604,11 @@ readconf(const char *fname, int bits)
     fatal(STR("Cannot read config"), 0);
   }
 
+  conf.hubs.clear();
   free_conf_bots(conf.bots);
 
   bd::String line, option;
+  unsigned short hublevel = 0;
 
   while (stream->tell() < stream->length()) {
     line = stream->getline().chomp().trim();
@@ -654,6 +658,11 @@ readconf(const char *fname, int bits)
         if (str_isdigit(line.c_str()))
           conf.uid = atoi(line.c_str());
 
+      } else if (option == STR("hub")) {
+        if (line.split(' ').length() == 3) {
+          conf.hubs << bd::String::printf("%s %d", line.c_str(), ++hublevel);
+        }
+
       } else {
         putlog(LOG_MISC, "*", STR("Unrecognized config option '%s'"), option.c_str());
 
@@ -687,6 +696,20 @@ readconf(const char *fname, int bits)
 
 char s1_9[3] = "",s1_5[3] = "",s1_1[3] = "";
 
+bool hubSort (bd::String hub1, bd::String hub2) {
+  bd::Array<bd::String> hub1params(static_cast<bd::String>(hub1).split(' '));
+  bd::Array<bd::String> hub2params(static_cast<bd::String>(hub2).split(' '));
+
+  unsigned short hub1level = 99, hub2level = 99;
+  if (hub1params.length() == 4) {
+    hub1level = atoi(static_cast<bd::String>(hub1params[3]).c_str());
+  }
+  if (hub2params.length() == 4) {
+    hub2level = atoi(static_cast<bd::String>(hub2params[3]).c_str());
+  }
+  return hub1level < hub2level;
+}
+
 int
 writeconf(char *filename, int fd, int bits)
 {
@@ -774,6 +797,21 @@ writeconf(char *filename, int fd, int bits)
     comment("");
   }
 
+  if (conf.hubs.length()) {
+    comment("# Hubs this bot will connect to");
+    // Sort hub list by hublevel
+    bd::Array<bd::String> sortedhubs(conf.hubs);
+    std::sort(sortedhubs.begin(), sortedhubs.end(), hubSort);
+
+    for (size_t idx = 0; idx < sortedhubs.length(); ++idx) {
+      bd::Array<bd::String> hubparams(static_cast<bd::String>(sortedhubs[idx]).split(' '));
+      bd::String hubnick(hubparams[0]), address(hubparams[1]);
+      port_t port = atoi(static_cast<bd::String>(hubparams[2]).c_str());
+      *stream << bd::String::printf(STR("! hub %s %s %d\n"), hubnick.c_str(), address.c_str(), port);
+    }
+    comment("");
+  }
+
   comment("# '|' means OR, [] means the enclosed is optional");
   comment("# A '+' in front of HOST means the HOST is ipv6");
   comment("# A '/' in front of BOT will disable that bot.");

+ 2 - 0
src/conf.h

@@ -6,6 +6,7 @@
 #include "types.h"
 #include "eggdrop.h"
 #include "settings.h"
+#include <bdlib/src/Array.h>
 
 typedef struct conf_net_b {
   char *host;
@@ -28,6 +29,7 @@ typedef struct conf_bot_b {
 } conf_bot;
 
 typedef struct conf_b {
+  bd::Array<bd::String> hubs;
   conf_bot *bots;       /* the list of bots */
   conf_bot *bot;        /* single bot (me) */
   int features;		/* Pack features (take, mdop, beta... etc..) */