Procházet zdrojové kódy

* Rewrote how logs are shared over botnet, now uses much less bw and resources. (#30)
-[will break logging during update]


svn: 2019

Bryan Drewery před 21 roky
rodič
revize
9f0a72eff3
8 změnil soubory, kde provedl 93 přidání a 21 odebrání
  1. 2 0
      doc/UPDATES
  2. 30 0
      src/botcmd.c
  3. 44 1
      src/botmsg.c
  4. 7 0
      src/dcc.c
  5. 2 1
      src/dcc.h
  6. 2 18
      src/log.c
  7. 1 1
      src/sprintf.c
  8. 5 0
      src/tandem.h

+ 2 - 0
doc/UPDATES

@@ -86,6 +86,8 @@ Lines prefixxed with '-' were disabled before release and are not finishsed, or
 * Fixed some missing sanity checking on certain user flags for channel records.
 * cmd_dns wasn't checking for param
 * cmd_getkey over .botcmd was displaying excess newlines.
+* Rewrote how logs are shared over botnet, now uses much less bw and resources. (#30)
+  -[will break logging during update]
 
 1.2.2
 * Don't sanity check flags for users on DCC CHAT if they are on the bot via .botcmd.

+ 30 - 0
src/botcmd.c

@@ -491,6 +491,34 @@ static void bot_link(int idx, char *par)
   }
 }
 
+/*
+	bot_log
+	 - forwards to all hubs linked directly if a hub (excluding the one who sent it)
+	 - leaf bots display and do not pass along.
+*/
+static void bot_log(int idx, char *par)
+{
+  char *from = newsplit(&par);
+  int i = nextbot(from);
+
+  if (i != idx) {
+    fake_alert(idx, "direction", from);
+    return;
+  }
+
+  if (egg_isdigit(par[0])) {
+    int type = atoi(newsplit(&par));
+
+    putlog(type, "@", "(%s) %s", from, par);
+  } else {
+    putlog(LOG_ERRORS, "*", "Malformed HL line from %s: %s", from, par);
+  }
+
+  if (conf.bot->hub)
+    send_hubs_but(idx, par, strlen(par));
+} 
+
+
 /* unlink <from@bot> <linking-bot> <undesired-bot> <reason>
  */
 static void bot_unlink(int idx, char *par)
@@ -816,6 +844,7 @@ static void bot_thisbot(int idx, char *par)
   strcpy(dcc[idx].nick, par);
 }
 
+/* FIXME: remove after 1.2.3 */
 static void bot_hublog(char *botnick, char *code, char *msg)
 {
   char *par = NULL, *parp;
@@ -1119,6 +1148,7 @@ botcmd_t C_bot[] =
   {"i",			bot_idle, 0},
   {"j",			bot_join, 0},
   {"l",			bot_link, 0},
+  {"lo",                bot_log, 0},
   {"n",			bot_nlinked, 0},
   {"nc",		bot_nickchange, 0},
   {"p",			bot_priv, 0},

+ 44 - 1
src/botmsg.c

@@ -28,11 +28,38 @@
 
 static char	OBUF[SGRAB - 110] = "";
 
+void send_uplink(const char *msg, size_t len)
+{
+  if (uplink_idx == -1 || !valid_idx(uplink_idx) || dcc[uplink_idx].sock == -1 || 
+     (dcc[uplink_idx].type != &DCC_BOT) || !dcc[uplink_idx].hub)
+    return;
+
+  tputs(dcc[uplink_idx].sock, (char *) msg, len);
+}
+
+void send_hubs(const char *msg, size_t len)
+{
+  send_hubs_but(-1, msg, len);
+}
+
+void send_hubs_but(int idx, const char *msg, size_t len)
+{
+  int i = 0;
+
+  for (i = 0; i < dcc_total; i++) {
+    if (dcc[i].type && (dcc[i].type == &DCC_BOT) && i != idx && dcc[i].hub) {
+      tputs(dcc[i].sock, (char *) msg, len);
+    }
+  }
+}
+
 /* Ditto for tandem bots
  */
 static void send_tand_but(int x, char *buf, size_t len)
 {
-  for (int i = 0; i < dcc_total; i++) {
+  int i = 0;
+
+  for (i = 0; i < dcc_total; i++) {
     if (dcc[i].type && (dcc[i].type == &DCC_BOT) && i != x) {
       tputs(dcc[i].sock, buf, len);
     }
@@ -265,6 +292,22 @@ void putbot(char *bot, char *par)
   botnet_send_zapf(i, conf.bot->nick, bot, par);
 }
 
+/*
+	botnet_send_log(...)
+	 - sends to uplink if a leaf
+	 - sends to all linked hubs if a hub
+*/
+void botnet_send_log(int type, const char *msg)
+{
+  size_t len = simple_sprintf(OBUF, "lo %s %d %s\n", conf.bot->nick, type, msg);
+
+  if (conf.bot->hub) {
+    send_hubs(OBUF, len);
+  } else {
+    send_uplink(OBUF, len);
+  }
+}
+
 void botnet_send_zapf(int idx, char *a, char *b, char *c)
 {
   size_t len = simple_sprintf(OBUF, "z %s %s %s\n", a, b, c);

+ 7 - 0
src/dcc.c

@@ -45,6 +45,7 @@ struct dcc_t *dcc = NULL;       /* DCC list                                */
 time_t timesync = 0;
 int dcc_total = 0;              /* size of dcc table                             */
 int dccn = 0;			/* actual number of dcc entries */
+int uplink_idx = -1;
 
 static time_t password_timeout = 40;       /* Time to wait for a password from a user */
 static time_t auth_timeout = 80;
@@ -225,10 +226,15 @@ bot_version(int idx, char *par)
     putlog(LOG_BOTS, "*", DCC_LINKED, dcc[idx].nick);
     chatout("*** Linked to %s\n", dcc[idx].nick);
 
+    if (bot_hublevel(dcc[idx].user) < 999)
+      dcc[idx].hub = 1;
+
     botnet_send_nlinked(idx, dcc[idx].nick, conf.bot->nick, '!', vlocalhub, vbuildts, vversion);
   } else {
     putlog(LOG_BOTS, "*", "Linked to botnet.");
     chatout("*** Linked to botnet.\n");
+    uplink_idx = idx;
+    dcc[idx].hub = 1;
   }
 
   dump_links(idx);
@@ -376,6 +382,7 @@ static void
 free_dcc_bot_(int n, void *x)
 {
   if (dcc[n].type == &DCC_BOT) {
+    uplink_idx = -1;
     unvia(n, findbot(dcc[n].nick));
     rembot(dcc[n].nick);
   }

+ 2 - 1
src/dcc.h

@@ -50,6 +50,7 @@ struct dcc_t {
   int ssl;                      /* use ssl on this dcc? */
   int simul;                    /* this will hold the idx on the remote bot to return result. */
   bool irc;			/* forward the output back to irc? */
+  bool hub;			// is this bot a hub?
   int auth;
   int whowas;
   int dns_id;
@@ -216,7 +217,7 @@ struct dupwait_info {
 #define TLN_ECHO_C      "\001"
 
 extern struct dcc_t 		*dcc;
-extern int 			dcc_total, dccn;
+extern int 			dcc_total, dccn, uplink_idx;
 extern time_t			timesync;
 extern char			network[];
 extern bool			protect_telnet;

+ 2 - 18
src/log.c

@@ -173,24 +173,8 @@ void putlog(int type, const char *chname, const char *format, ...)
   /* FIXME: WRITE LOG HERE */
 
   /* broadcast to hubs */
-  if (chname[0] == '*' && conf.bot && conf.bot->nick) {
-    char outbuf[LOGLINEMAX + 1] = "";
-
-    simple_snprintf(outbuf, sizeof outbuf, "hl %d %s", type, out);
-    if (userlist && !loading) {
-      tand_t *bot = NULL;
-      struct userrec *ubot = NULL;
-
-      for (bot = tandbot; bot; bot = bot->next) {
-        if ((ubot = get_user_by_handle(userlist, bot->bot))) {
-          if (bot_hublevel(ubot) < 999)
-            putbot(ubot->handle, outbuf);
-        }
-      }
-    } else {
-      putallbots(outbuf);
-    }
-  }
+  if (chname[0] == '*' && conf.bot && conf.bot->nick)
+    botnet_send_log(type, out);
 
   for (idx = 0; idx < dcc_total; idx++) {
     if (dcc[idx].type && (dcc[idx].type == &DCC_CHAT && !dcc[idx].simul) && (dcc[idx].u.chat->con_flags & type)) {

+ 1 - 1
src/sprintf.c

@@ -101,7 +101,7 @@ size_t simple_vsnprintf(char *buf, size_t size, const char *format, va_list va)
     } else
       buf[c++] = *fp++;
   }
-  buf[c] = 0;
+  buf[c] = '\0';
 
   return c;
 }

+ 5 - 0
src/tandem.h

@@ -58,6 +58,7 @@ void botnet_send_link(int, char *, char *, char *);
 void botnet_send_update(int, tand_t *);
 void botnet_send_nlinked(int, char *, char *, char, int, time_t, char *);
 void botnet_send_reject(int, char *, char *, char *, char *, char *);
+void botnet_send_log(int, const char *);
 void botnet_send_zapf(int, char *, char *, char *);
 void botnet_send_zapf_broad(int, char *, char *, char *);
 void botnet_send_away(int, char *, int, char *, int);
@@ -74,6 +75,10 @@ int users_in_subtree(tand_t *);
 int botnet_send_cmd(char * fbot, char * bot, char *fhnd, int fromidx, char * cmd);
 void botnet_send_cmd_broad(int idx, char * fbot, char *fhnd, int fromidx, char * cmd);
 void botnet_send_cmdreply(char * fbot, char * bot, char * to, char * toidx, char * ln);
+void send_uplink(const char *, size_t);
+inline void send_hubs(const char *, size_t);
+void send_hubs_but(int , const char *, size_t);
+
 
 #define b_status(a)	(dcc[a].status)