Apply minor fixes from patches@, SF tracker, and others.
[ircu2.10.12-pk.git] / ircd / send.c
index 828c4e099278327654a6948811b2e8836400763a..7a79fbaa493818eea10d1ba4fd11f76749a527bd 100644 (file)
  *
  * $Id$
  */
+#include "config.h"
+
 #include "send.h"
 #include "channel.h"
 #include "class.h"
 #include "client.h"
 #include "ircd.h"
+#include "ircd_features.h"
 #include "ircd_snprintf.h"
 #include "ircd_string.h"
 #include "list.h"
 #include "match.h"
 #include "msg.h"
 #include "numnicks.h"
+#include "parse.h"
 #include "s_bsd.h"
 #include "s_debug.h"
 #include "s_misc.h"
 #include "s_user.h"
-#include "sprintf_irc.h"
 #include "struct.h"
 #include "sys.h"
 
@@ -47,12 +50,14 @@ static int sentalong[MAXCONNECTIONS];
 static int sentalong_marker;
 struct SLink *opsarray[32];     /* don't use highest bit unless you change
                                   atoi to strtoul in sendto_op_mask() */
+static struct Connection *send_queues = 0;
+
 /*
  * dead_link
  *
  * An error has been detected. The link *must* be closed,
  * but *cannot* call ExitClient (m_bye) from here.
- * Instead, mark it with FLAGS_DEADSOCKET. This should
+ * Instead, mark it with FLAG_DEADSOCKET. This should
  * generate ExitClient from the main loop.
  *
  * If 'notice' is not NULL, it is assumed to be a format
@@ -66,20 +71,21 @@ struct SLink *opsarray[32];     /* don't use highest bit unless you change
 
 static void dead_link(struct Client *to, char *notice)
 {
-  cli_flags(to) |= FLAGS_DEADSOCKET;
+  SetFlag(to, FLAG_DEADSOCKET);
   /*
    * If because of BUFFERPOOL problem then clean dbuf's now so that
    * notices don't hurt operators below.
    */
   DBufClear(&(cli_recvQ(to)));
   MsgQClear(&(cli_sendQ(to)));
+  client_drop_sendq(cli_connect(to));
 
   /*
    * Keep a copy of the last comment, for later use...
    */
   ircd_strncpy(cli_info(to), notice, REALLEN);
 
-  if (!IsUser(to) && !IsUnknown(to) && !(cli_flags(to) & FLAGS_CLOSING))
+  if (!IsUser(to) && !IsUnknown(to) && !HasFlag(to, FLAG_CLOSING))
     sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
   Debug((DEBUG_ERROR, cli_info(to)));
 }
@@ -90,6 +96,33 @@ static int can_send(struct Client* to)
   return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
 }
 
+/* This helper routine kills the connection with the highest sendq, to
+ * try to free up some buffer memory.
+ */
+void
+kill_highest_sendq(int servers_too)
+{
+  int i;
+  unsigned int highest_sendq = 0;
+  struct Client *highest_client = 0;
+
+  for (i = HighestFd; i >= 0; i--)
+  {
+    if (!LocalClientArray[i] || (!servers_too && cli_serv(LocalClientArray[i])))
+      continue; /* skip servers */
+    
+    /* If this sendq is higher than one we last saw, remember it */
+    if (MsgQLength(&(cli_sendQ(LocalClientArray[i]))) > highest_sendq)
+    {
+      highest_client = LocalClientArray[i];
+      highest_sendq = MsgQLength(&(cli_sendQ(highest_client)));
+    }
+  }
+
+  if (highest_client)
+    dead_link(highest_client, "Buffer allocation error");
+}
+
 /*
  * flush_connections
  *
@@ -106,31 +139,14 @@ void flush_connections(struct Client* cptr)
     send_queued(cptr);
   }
   else {
-    int i;
-    for (i = HighestFd; i >= 0; i--) {
-      if ((cptr = LocalClientArray[i]))
-        send_queued(cptr);
+    struct Connection* con;
+    for (con = send_queues; con; con = con_next(con)) {
+      assert(0 < MsgQLength(&(con_sendQ(con))));
+      send_queued(con_client(con));
     }
   }
 }
 
-/*
- * flush_sendq_except - run through local client array and flush
- * the sendq for each client, if the address of the client sendq
- * is the same as the one specified, it is skipped. This is used
- * by dbuf_put to try to get some more memory before bailing and
- * causing the client to be disconnected.
- */
-void flush_sendq_except(void)
-{
-  int i;
-  struct Client* cptr;
-  for (i = HighestFd; i >= 0; i--) {
-    if ( (cptr = LocalClientArray[i]))
-      send_queued(cptr);
-  }
-}
-
 /*
  * send_queued
  *
@@ -152,8 +168,10 @@ void send_queued(struct Client *to)
     if ((len = deliver_it(to, &(cli_sendQ(to))))) {
       msgq_delete(&(cli_sendQ(to)), len);
       cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
-      if (IsBlocked(to))
-        break;
+      if (IsBlocked(to)) {
+       update_write(to);
+        return;
+      }
     }
     else {
       if (IsDead(to)) {
@@ -161,9 +179,13 @@ void send_queued(struct Client *to)
         sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
         dead_link(to, tmp);
       }
-      break;
+      return;
     }
   }
+
+  /* Ok, sendq is now empty... */
+  client_drop_sendq(cli_connect(to));
+  update_write(to);
 }
 
 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
@@ -192,6 +214,8 @@ void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
   Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
 
   msgq_add(&(cli_sendQ(to)), buf, prio);
+  client_add_sendq(cli_connect(to), &send_queues);
+  update_write(to);
 
   /*
    * Update statistics. The following is slightly incorrect
@@ -219,12 +243,13 @@ void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
  *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
  */
 
-static int match_it(struct Client *one, const char *mask, int what)
+static int match_it(struct Client *from, struct Client *one, const char *mask, int what)
 {
   switch (what)
   {
     case MATCH_HOST:
-      return (match(mask, cli_user(one)->host) == 0);
+      return (match(mask, cli_user(one)->host) == 0 ||
+        (HasHiddenHost(one) && match(mask, cli_user(one)->realhost) == 0));
     case MATCH_SERVER:
     default:
       return (match(mask, cli_name(cli_user(one)->server)) == 0);
@@ -257,15 +282,31 @@ void sendrawto_one(struct Client *to, const char *pattern, ...)
 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
                   struct Client *to, const char *pattern, ...)
 {
-  va_list vl;
+  struct VarData vd;
+  struct MsgBuf *mb;
 
-  va_start(vl, pattern);
-  vsendcmdto_one(from, cmd, tok, to, pattern, vl);
-  va_end(vl);
+  to = cli_from(to);
+
+  vd.vd_format = pattern; /* set up the struct VarData for %v */
+  va_start(vd.vd_args, pattern);
+
+  mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
+                &vd);
+
+  va_end(vd.vd_args);
+
+  send_buffer(to, mb, 0);
+
+  msgq_clean(mb);
 }
 
-void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
-                   struct Client *to, const char *pattern, va_list vl)
+/*
+ * Send a (prefixed) command to a single client in the priority queue;
+ * select  which of <cmd> <tok> to use depending on if to is a server
+ *or not.  <from> is the originator of the command.
+ */
+void sendcmdto_prio_one(struct Client *from, const char *cmd, const char *tok,
+                       struct Client *to, const char *pattern, ...)
 {
   struct VarData vd;
   struct MsgBuf *mb;
@@ -273,12 +314,14 @@ void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
   to = cli_from(to);
 
   vd.vd_format = pattern; /* set up the struct VarData for %v */
-  vd.vd_args = vl;
+  va_start(vd.vd_args, pattern);
 
   mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
                 &vd);
 
-  send_buffer(to, mb, 0);
+  va_end(vd.vd_args);
+
+  send_buffer(to, mb, 1);
 
   msgq_clean(mb);
 }
@@ -316,6 +359,8 @@ void sendcmdto_serv_butone(struct Client *from, const char *cmd,
  * Send a (prefix) command originating from <from> to all channels
  * <from> is locally on.  <from> must be a user. <tok> is ignored in
  * this function.
+ *
+ * Update: don't send to 'one', if any. --Vampire
  */
 /* XXX sentalong_marker used XXX
  *
@@ -329,8 +374,9 @@ void sendcmdto_serv_butone(struct Client *from, const char *cmd,
  * message to, and then a final loop through the connected servers
  * to delete the flag. -Kev
  */
-void sendcmdto_common_channels(struct Client *from, const char *cmd,
-                              const char *tok, const char *pattern, ...)
+void sendcmdto_common_channels_butone(struct Client *from, const char *cmd,
+                                     const char *tok, struct Client *one,
+                                     const char *pattern, ...)
 {
   struct VarData vd;
   struct MsgBuf *mb;
@@ -356,16 +402,20 @@ void sendcmdto_common_channels(struct Client *from, const char *cmd,
   /*
    * loop through from's channels, and the members on their channels
    */
-  for (chan = cli_user(from)->channel; chan; chan = chan->next_channel)
+  for (chan = cli_user(from)->channel; chan; chan = chan->next_channel) {
+    if (IsZombie(chan) || IsDelayedJoin(chan))
+      continue;
     for (member = chan->channel->members; member;
         member = member->next_member)
       if (MyConnect(member->user) && -1 < cli_fd(cli_from(member->user)) &&
+          member->user != one &&
          sentalong[cli_fd(cli_from(member->user))] != sentalong_marker) {
        sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
        send_buffer(member->user, mb, 0);
       }
+  }
 
-  if (MyConnect(from))
+  if (MyConnect(from) && from != one)
     send_buffer(from, mb, 0);
 
   msgq_clean(mb);
@@ -374,10 +424,14 @@ void sendcmdto_common_channels(struct Client *from, const char *cmd,
 /*
  * Send a (prefixed) command to all local users on the channel specified
  * by <to>; <tok> is ignored by this function
+ *
+ * Update: don't send to 'one', if any. --Vampire
+ * Update: use 'skip' like sendcmdto_channel_butone. --Entrope
  */
-void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
-                              const char *tok, struct Channel *to,
-                              const char *pattern, ...)
+void sendcmdto_channel_butserv_butone(struct Client *from, const char *cmd,
+                                     const char *tok, struct Channel *to,
+                                     struct Client *one, unsigned int skip,
+                                      const char *pattern, ...)
 {
   struct VarData vd;
   struct MsgBuf *mb;
@@ -392,13 +446,56 @@ void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
 
   /* send the buffer to each local channel member */
   for (member = to->members; member; member = member->next_member) {
-    if (MyConnect(member->user) && !IsZombie(member))
+    if (!MyConnect(member->user)
+        || member->user == one 
+        || IsZombie(member)
+        || (skip & SKIP_DEAF && IsDeaf(member->user))
+        || (skip & SKIP_NONOPS && !IsChanOp(member))
+        || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
+        continue;
       send_buffer(member->user, mb, 0);
   }
 
   msgq_clean(mb);
 }
 
+/*
+ * Send a (prefixed) command to all servers with users on the channel
+ * specified by <to>; <cmd> and <skip> are ignored by this function.
+ *
+ * XXX sentalong_marker used XXX
+ */
+void sendcmdto_channel_servers_butone(struct Client *from, const char *cmd,
+                                      const char *tok, struct Channel *to,
+                                      struct Client *one, unsigned int skip,
+                                      const char *pattern, ...)
+{
+  struct VarData vd;
+  struct MsgBuf *serv_mb;
+  struct Membership *member;
+
+  /* build the buffer */
+  vd.vd_format = pattern;
+  va_start(vd.vd_args, pattern);
+  serv_mb = msgq_make(&me, "%:#C %s %v", from, tok, &vd);
+  va_end(vd.vd_args);
+
+  /* send the buffer to each server */
+  sentalong_marker++;
+  for (member = to->members; member; member = member->next_member) {
+    if (cli_from(member->user) == one
+        || MyConnect(member->user)
+        || IsZombie(member)
+        || cli_fd(cli_from(member->user)) < 0
+        || sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
+      continue;
+    sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
+    send_buffer(member->user, serv_mb, 0);
+  }
+  msgq_clean(serv_mb);
+}
+
+
 /*
  * Send a (prefixed) command to all users on this channel, including
  * remote users; users to skip may be specified by setting appropriate
@@ -430,8 +527,8 @@ void sendcmdto_channel_butone(struct Client *from, const char *cmd,
 
   /* Build buffer to send to users */
   va_start(vd.vd_args, pattern);
-  user_mb = msgq_make(0, skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v",
-                     from, skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
+  user_mb = msgq_make(0, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? "%:#C %s @%v" : "%:#C %s %v",
+                      from, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? MSG_NOTICE : cmd, &vd);
   va_end(vd.vd_args);
 
   /* Build buffer to send to servers */
@@ -444,11 +541,12 @@ void sendcmdto_channel_butone(struct Client *from, const char *cmd,
   for (member = to->members; member; member = member->next_member) {
     /* skip one, zombies, and deaf users... */
     if (cli_from(member->user) == one || IsZombie(member) ||
-       (skip & SKIP_DEAF && IsDeaf(member->user)) ||
-       (skip & SKIP_NONOPS && !IsChanOp(member)) ||
-       (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
-       cli_fd(cli_from(member->user)) < 0 ||
-       sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
+        (skip & SKIP_DEAF && IsDeaf(member->user)) ||
+        (skip & SKIP_NONOPS && !IsChanOp(member)) ||
+        (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)) ||
+        (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
+        cli_fd(cli_from(member->user)) < 0 ||
+        sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
       continue;
     sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
 
@@ -466,54 +564,69 @@ void sendcmdto_channel_butone(struct Client *from, const char *cmd,
  * Send a (prefixed) command to all users except <one> that have
  * <flag> set.
  */
-/* XXX sentalong_marker used XXX
- *
- * Again, we can solve this use of sentalong_marker by adding a field
- * to connections--a count of the number of +w users, and another count
- * of +g users.  Then, just walk through the local clients to send
- * those messages, and another walk through the connected servers list,
- * sending only if there's a non-zero count.  No caveats here, either,
- * beyond remembering to decrement the count when a user /quit's or is
- * killed, or a server is squit. -Kev
- */
-void sendcmdto_flag_butone(struct Client *from, const char *cmd,
-                          const char *tok, struct Client *one,
-                          unsigned int flag, const char *pattern, ...)
+void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
+                            const char *pattern, ...)
 {
   struct VarData vd;
   struct Client *cptr;
-  struct MsgBuf *user_mb;
-  struct MsgBuf *serv_mb;
+  struct MsgBuf *mb;
+  struct DLink *lp;
+  char *prefix=NULL;
+  char *tok=NULL;
+  int i;
 
   vd.vd_format = pattern;
 
   /* Build buffer to send to users */
   va_start(vd.vd_args, pattern);
-  user_mb = msgq_make(0, "%:#C " MSG_WALLOPS " %v", from, &vd);
+  switch (type) {
+       case WALL_DESYNCH:
+               prefix="";
+               tok=TOK_DESYNCH;
+               break;
+       case WALL_WALLOPS:
+               prefix="* ";
+               tok=TOK_WALLOPS;
+               break;
+       case WALL_WALLUSERS:
+               prefix="$ ";
+               tok=TOK_WALLUSERS;
+               break;
+       default:
+               assert(0);
+  }
+  mb = msgq_make(0, "%:#C " MSG_WALLOPS " :%s%v", from, prefix,&vd);
   va_end(vd.vd_args);
 
+  /* send buffer along! */
+  for (i = 0; i <= HighestFd; i++)
+  {
+    if (!(cptr = LocalClientArray[i]) ||
+       (cli_fd(cli_from(cptr)) < 0) ||
+       (type == WALL_DESYNCH && !HasFlag(cptr, FLAG_DEBUG)) ||
+       (type == WALL_WALLOPS &&
+         (!HasFlag(cptr, FLAG_WALLOP) || (feature_bool(FEAT_HIS_WALLOPS) &&
+                                          !IsAnOper(cptr)))) ||
+        (type == WALL_WALLUSERS && !HasFlag(cptr, FLAG_WALLOP)))
+      continue; /* skip it */
+    send_buffer(cptr, mb, 1);
+  }
+
+  msgq_clean(mb);
+
   /* Build buffer to send to servers */
   va_start(vd.vd_args, pattern);
-  serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
+  mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
   va_end(vd.vd_args);
 
   /* send buffer along! */
-  sentalong_marker++;
-  for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
-    if (cli_from(cptr) == one || IsServer(cptr) || !(cli_flags(cptr) & flag) ||
-       cli_fd(cli_from(cptr)) < 0 ||
-       sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
-      continue; /* skip it */
-    sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
-
-    if (MyConnect(cptr)) /* send right buffer */
-      send_buffer(cptr, user_mb, 1);
-    else
-      send_buffer(cptr, serv_mb, 1);
+  for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
+    if (one && lp->value.cptr == cli_from(one))
+      continue;
+    send_buffer(lp->value.cptr, mb, 1);
   }
 
-  msgq_clean(user_mb);
-  msgq_clean(serv_mb);
+  msgq_clean(mb);
 }
 
 /*
@@ -559,8 +672,8 @@ void sendcmdto_match_butone(struct Client *from, const char *cmd,
   /* send buffer along */
   sentalong_marker++;
   for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
-    if (cli_from(cptr) == one || IsServer(cptr) || IsMe(cptr) ||
-       !match_it(cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
+    if (!IsRegistered(cptr) || cli_from(cptr) == one || IsServer(cptr) ||
+       IsMe(cptr) || !match_it(from, cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
        sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
       continue; /* skip it */
     sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
@@ -589,6 +702,27 @@ void sendto_opmask_butone(struct Client *one, unsigned int mask,
   va_end(vl);
 }
 
+/*
+ * Send a server notice to all users subscribing to the indicated <mask>
+ * except for <one> - Ratelimited 1 / 30sec
+ */
+void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
+                                     time_t *rate, const char *pattern, ...)
+{
+  va_list vl;
+
+  if ((CurrentTime - *rate) < 30) 
+    return;
+  else 
+    *rate = CurrentTime;
+
+  va_start(vl, pattern);
+  vsendto_opmask_butone(one, mask, pattern, vl);
+  va_end(vl);
+    
+}
+
+
 /*
  * Same as above, except called with a variable argument list
  */
@@ -611,12 +745,13 @@ void vsendto_opmask_butone(struct Client *one, unsigned int mask,
    * this is ok...
    */
   vd.vd_format = pattern;
-  vd.vd_args = vl;
+  va_copy(vd.vd_args, vl);
   mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
                 &vd);
 
   for (; opslist; opslist = opslist->next)
-    send_buffer(opslist->value.cptr, mb, 0);
+    if (opslist->value.cptr != one)
+      send_buffer(opslist->value.cptr, mb, 0);
 
   msgq_clean(mb);
 }