added basic ssl support to ircu
[ircu2.10.12-pk.git] / ircd / send.c
index 787eeae01cc57134a982c45d55314eb2ff3e5465..b9309b85337fb67f7a5a41f3459c759721648ea3 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id$
  */
+/** @file
+ * @brief Send messages to certain targets.
+ * @version $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_log.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"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <stdio.h>
 #include <string.h>
 
-
-char sendbuf[2048];
-static int sentalong[MAXCONNECTIONS];
+/** Last used marker value. */
 static int sentalong_marker;
+/** Array of users with the corresponding server notice mask bit set. */
 struct SLink *opsarray[32];     /* don't use highest bit unless you change
-                                   atoi to strtoul in sendto_op_mask() */
-#ifdef GODMODE
-char sendbuf2[2048];
-int sdbflag;
-#endif /* GODMODE */
+                                  atoi to strtoul in sendto_op_mask() */
+/** Linked list of all connections with data queued to send. */
+static struct Connection *send_queues;
 
 /*
  * 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
- * for a message to local opers. I can contain only one
+ * for a message to local opers. It can contain only one
  * '%s', which will be replaced by the sockhost field of
  * the failing link.
  *
  * Also, the notice is skipped for "uninteresting" cases,
  * like Persons and yet unknown connections...
  */
-
+/** Mark a client as dead, even if they are not the current message source.
+ * This is done by setting the DEADSOCKET flag on the user and letting the
+ * main loop perform the actual exit logic.
+ * @param[in,out] to Client being killed.
+ * @param[in] notice Message for local opers.
+ */
 static void dead_link(struct Client *to, char *notice)
 {
-  to->flags |= 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(&to->recvQ);
-  DBufClear(&to->sendQ);
+  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(LastDeadComment(to), notice, sizeof(LastDeadComment(to) - 1));
-  LastDeadComment(to)[sizeof(LastDeadComment(to)) - 1] = '\0';
+  /*
+   * Keep a copy of the last comment, for later use...
+   */
+  ircd_strncpy(cli_info(to), notice, REALLEN);
 
-  if (!IsUser(to) && !IsUnknown(to) && !(to->flags & FLAGS_CLOSING))
-    sendto_ops("%s for %s", LastDeadComment(to), to->name);
-  Debug((DEBUG_ERROR, LastDeadComment(to)));
+  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)));
 }
 
+/** Test whether we can send to a client.
+ * @param[in] to Client we want to send to.
+ * @return Non-zero if we can send to the client.
+ */
 static int can_send(struct Client* to)
 {
   assert(0 != to);
-  return (IsDead(to) || IsMe(to) || -1 == to->fd) ? 0 : 1;
+  return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
+}
+
+/** Close the connection with the highest sendq.
+ * This should be called when we need to free buffer memory.
+ * @param[in] servers_too If non-zero, consider killing servers, too.
+ */
+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");
 }
 
 /*
@@ -104,34 +147,24 @@ static int can_send(struct Client* to)
  * client and try to send it. if we cant send it, it goes into the sendQ
  * -avalon
  */
+/** Flush data queued for one or all connections.
+ * @param[in] cptr Client to flush (if NULL, do all).
+ */
 void flush_connections(struct Client* cptr)
 {
+  struct SSLConnection *ssl = cli_connect(cptr)->con_ssl;
   if (cptr) {
+    if(ssl)
+      ssl_connection_flush(ssl);
     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(const struct DBuf* one)
-{
-  int i;
-  struct Client* cptr;
-  for (i = HighestFd; i >= 0; i--) {
-    if ( (cptr = LocalClientArray[i]) && one != &cptr->sendQ)
-      send_queued(cptr);
+    ssl_connection_flush(NULL);
   }
 }
 
@@ -142,83 +175,55 @@ void flush_sendq_except(const struct DBuf* one)
  * when there is a chance that some output would be possible. This
  * attempts to empty the send queue as far as possible...
  */
+/** Attempt to send data queued for a client.
+ * @param[in] to Client to send data to.
+ */
 void send_queued(struct Client *to)
 {
   assert(0 != to);
-  assert(0 != to->local);
+  assert(0 != cli_local(to));
 
   if (IsBlocked(to) || !can_send(to))
     return;                     /* Don't bother */
 
-  while (DBufLength(&to->sendQ) > 0) {
+  while (MsgQLength(&(cli_sendQ(to))) > 0) {
     unsigned int len;
-    const char* msg = dbuf_map(&to->sendQ, &len);
 
-    if ((len = deliver_it(to, msg, len))) {
-      dbuf_delete(&to->sendQ, len);
-      to->lastsq = DBufLength(&to->sendQ) / 1024;
-      if (IsBlocked(to))
-        break;
+    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)) {
+       update_write(to);
+        return;
+      }
     }
     else {
-      if (IsDead(to))
-        dead_link(to, "Write error, closing link");
-      break;
+      if (IsDead(to)) {
+        char tmp[512];
+        sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
+        dead_link(to, tmp);
+      }
+      return;
     }
   }
-}
 
-/*
- *  send message to single client
- */
-void sendto_one(struct Client *to, const char* pattern, ...)
-{
-  va_list vl;
-  va_start(vl, pattern);
-  vsendto_one(to, pattern, vl);
-  va_end(vl);
+  /* Ok, sendq is now empty... */
+  client_drop_sendq(cli_connect(to));
+  update_write(to);
 }
 
-
-void vsendto_one(struct Client *to, const char* pattern, va_list vl)
-{
-  vsprintf_irc(sendbuf, pattern, vl);
-  sendbufto_one(to);
-}
-
-#ifdef GODMODE
-static void send_to_god(struct Client* to, const char* buf)
-{
-  if (!sdbflag && !IsUser(to)) {
-    char sbuf2[BUFSIZE + 1];
-    unsigned int len = strlen(buf) - 2;   /* Remove "\r\n" */
-
-    sdbflag = 1;
-    len = IRCD_MIN(len, BUFSIZE);
-    ircd_strncpy(sbuf2, buf, len);
-    sbuf2[len] = '\0';
-
-    if (len > 402) {
-      char c = sbuf2[200];
-      sbuf2[200] = '\0';
-      sendto_ops("SND:%-8.8s(%.4d): \"%s...%s\"",
-                 to->name, len, sbuf2, &sbuf2[len - 200]);
-    }
-    else
-      sendto_ops("SND:%-8.8s(%.4d): \"%s\"", to->name, len, sbuf2);
-    sdbflag = 0;
-  }
-}
-#endif /* GODMODE */
-
-void send_buffer(struct Client* to, char* buf)
+/** Try to send a buffer to a client, queueing it if needed.
+ * @param[in,out] to Client to send message to.
+ * @param[in] buf Message to send.
+ * @param[in] prio If non-zero, send as high priority.
+ */
+void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
 {
-  unsigned int len;
   assert(0 != to);
   assert(0 != buf);
 
-  if (to->from)
-    to = to->from;
+  if (cli_from(to))
+    to = cli_from(to);
 
   if (!can_send(to))
     /*
@@ -226,40 +231,28 @@ void send_buffer(struct Client* to, char* buf)
      */
     return;
 
-  if (DBufLength(&to->sendQ) > get_sendq(to)) {
+  if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
     if (IsServer(to))
-      sendto_ops("Max SendQ limit exceeded for %s: " SIZE_T_FMT " > " SIZE_T_FMT,
-                 to->name, DBufLength(&to->sendQ), get_sendq(to));
+      sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
+                          "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
+                          get_sendq(to));
     dead_link(to, "Max sendQ exceeded");
     return;
   }
 
-  Debug((DEBUG_SEND, "Sending [%s] to %s", buf, to->name));
+  Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
 
-  len = strlen(buf);
-  if (buf[len - 1] != '\n') {
-    if (len > 510)
-      len = 510;
-    buf[len++] = '\r';
-    buf[len++] = '\n';
-    buf[len] = '\0';
-  }
+  msgq_add(&(cli_sendQ(to)), buf, prio);
+  client_add_sendq(cli_connect(to), &send_queues);
+  update_write(to);
 
-  if (0 == dbuf_put(&to->sendQ, buf, len)) {
-    dead_link(to, "Buffer allocation error");
-    return;
-  }
-
-#ifdef GODMODE
-  send_to_god(to, buf);
-#endif /* GODMODE */
   /*
    * Update statistics. The following is slightly incorrect
    * because it counts messages even if queued, but bytes
    * only really sent. Queued bytes get updated in SendQueued.
    */
-  ++to->sendM;
-  ++me.sendM;
+  ++(cli_sendM(to));
+  ++(cli_sendM(&me));
   /*
    * This little bit is to stop the sendQ from growing too large when
    * there is no need for it to. Thus we call send_queued() every time
@@ -268,693 +261,598 @@ void send_buffer(struct Client* to, char* buf)
    * trying to flood that link with data (possible during the net
    * relinking done by servers with a large load).
    */
-  if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
+  if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
     send_queued(to);
 }
 
-void sendbufto_one(struct Client* to)
-{
-  send_buffer(to, sendbuf);
-}
+/*
+ * Send a msg to all ppl on servers/hosts that match a specified mask
+ * (used for enhanced PRIVMSGs)
+ *
+ *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
+ */
 
-static void vsendto_prefix_one(struct Client *to, struct Client *from,
-    const char* pattern, va_list vl)
+/** Check whether a client matches a target mask.
+ * @param[in] from Client trying to send a message (ignored).
+ * @param[in] one Client being considered as a target.
+ * @param[in] mask Mask for matching against.
+ * @param[in] what Type of match (either MATCH_HOST or MATCH_SERVER).
+ * @return Non-zero if \a one matches, zero if not.
+ */
+static int match_it(struct Client *from, struct Client *one, const char *mask, int what)
 {
-  if (to && from && MyUser(to) && IsUser(from))
+  switch (what)
   {
-    static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
-    char *par;
-    int flag = 0;
-    struct User *user = from->user;
-
-    par = va_arg(vl, char *);
-    strcpy(sender, from->name);
-    if (user)
-    {
-      if (*user->username)
-      {
-        strcat(sender, "!");
-        strcat(sender, user->username);
-      }
-      if (*user->host && !MyConnect(from))
-      {
-        strcat(sender, "@");
-        strcat(sender, user->host);
-        flag = 1;
-      }
-    }
-    /*
-     * Flag is used instead of strchr(sender, '@') for speed and
-     * also since username/nick may have had a '@' in them. -avalon
-     */
-    if (!flag && MyConnect(from) && *user->host)
-    {
-      strcat(sender, "@");
-      strcat(sender, from->sockhost);
-    }
-    *sendbuf = ':';
-    strcpy(&sendbuf[1], sender);
-    /* Assuming 'pattern' always starts with ":%s ..." */
-    vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
+    case MATCH_HOST:
+      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);
   }
-  else
-    vsprintf_irc(sendbuf, pattern, vl);
-  sendbufto_one(to);
 }
 
-void sendto_channel_butone(struct Client *one, struct Client *from, struct Channel *chptr,
-    const char* pattern, ...)
+/** Send an unprefixed line to a client.
+ * @param[in] to Client receiving message.
+ * @param[in] pattern Format string of message.
+ */
+void sendrawto_one(struct Client *to, const char *pattern, ...)
 {
+  struct MsgBuf *mb;
   va_list vl;
-  struct Membership* member;
-  struct Client *acptr;
-  int i;
 
   va_start(vl, pattern);
-
-  ++sentalong_marker;
-  for (member = chptr->members; member; member = member->next_member)
-  {
-    acptr = member->user;
-    /* ...was the one I should skip */
-    if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
-      continue;
-    if (MyConnect(acptr))       /* (It is always a client) */
-      vsendto_prefix_one(acptr, from, pattern, vl);
-    else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
-    {
-      sentalong[i] = sentalong_marker;
-      /*
-       * Don't send channel messages to links that are still eating
-       * the net.burst: -- Run 2/1/1997
-       */
-      if (!IsBurstOrBurstAck(acptr->from))
-        vsendto_prefix_one(acptr, from, pattern, vl);
-    }
-  }
+  mb = msgq_vmake(to, pattern, vl);
   va_end(vl);
-}
 
+  send_buffer(to, mb, 0);
 
-void sendmsgto_channel_butone(struct Client *one, struct Client *from,
-                              struct Channel *chptr, const char *sender,
-                              const char *cmd, const char *chname, const char *msg)
+  msgq_clean(mb);
+}
+
+/** Send a (prefixed) command to a single client.
+ * @param[in] from Client sending the command.
+ * @param[in] cmd Long name of command (used if \a to is a user).
+ * @param[in] tok Short name of command (used if \a to is a server).
+ * @param[in] to Destination of command.
+ * @param[in] pattern Format string for command arguments.
+ */
+void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
+                  struct Client *to, const char *pattern, ...)
 {
- /*
-  * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
-  * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
-  */
-  struct Membership* member;
-  struct Client *acptr;
-  char userbuf[2048];
-  char servbuf[2048];
-  int i;
-  int flag=-1;
+  struct VarData vd;
+  struct MsgBuf *mb;
 
-  /* 
-   * Precalculate the buffers we sent to the clients instead of doing an
-   * expensive sprintf() per member that we send to.  We still have to
-   * use strcpy() which is evil.
-   */
-  if (IsServer(from)) {
-    sprintf(userbuf,":%s %s %s :%s",
-        from->name, 
-        ((cmd[0] == 'P') ? MSG_PRIVATE : MSG_NOTICE),
-        chname, msg);
-    sprintf(servbuf,"%s " TOK_PRIVATE " %s :%s",
-        NumServ(from), chname, msg);
-  } else {
-    sprintf(userbuf,":%s!%s@%s %s %s :%s",
-      from->name, from->username, from->user->host,
-      ((cmd[0] == 'P') ? MSG_PRIVATE : MSG_NOTICE),
-      chname, msg);
-    sprintf(servbuf,"%s%s %s %s :%s",
-      NumNick(from), 
-       ((cmd[0] == 'P') ? TOK_PRIVATE : TOK_NOTICE),
-     chname, msg);
-  }
+  to = cli_from(to);
 
-  ++sentalong_marker;
-  for (member = chptr->members; member; member = member->next_member)
-  {
-    acptr = member->user;
-    /* ...was the one I should skip */
-    if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
-      continue;
-    if (MyConnect(acptr)) {      /* (It is always a client) */
-        if (flag!=0)
-          strcpy(sendbuf,userbuf);
-        flag=0;
-        sendbufto_one(acptr);
-    }
-    else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
-    {
-      sentalong[i] = sentalong_marker;
-      if (!IsBurstOrBurstAck(acptr->from)) {
-        if (flag != 1)
-          strcpy(sendbuf,servbuf);
-        flag = 1;
-        sendbufto_one(acptr);
-  }
-    } /* of if MyConnect() */
-  } /* of for(members) */
+  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 sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
-    const char* pattern, ...)
+/**
+ * Send a (prefixed) command to a single client in the priority queue.
+ * @param[in] from Client sending the command.
+ * @param[in] cmd Long name of command (used if \a to is a user).
+ * @param[in] tok Short name of command (used if \a to is a server).
+ * @param[in] to Destination of command.
+ * @param[in] pattern Format string for command arguments.
+ */
+void sendcmdto_prio_one(struct Client *from, const char *cmd, const char *tok,
+                       struct Client *to, const char *pattern, ...)
 {
-  va_list vl;
-  struct Membership* member;
-  struct Client *acptr;
+  struct VarData vd;
+  struct MsgBuf *mb;
 
-  va_start(vl, pattern);
+  to = cli_from(to);
 
-  for (member = chptr->members; member; member = member->next_member)
-  {
-    acptr = member->user;
-    /* ...was the one I should skip */
-    if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
-      continue;
-    if (MyConnect(acptr))       /* (It is always a client) */
-      vsendto_prefix_one(acptr, from, pattern, vl);
-  }
-  va_end(vl);
-  return;
+  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, 1);
+
+  msgq_clean(mb);
 }
 
-void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
-    const char* pattern, ...)
+/**
+ * Send a (prefixed) command to all servers matching or not matching a
+ * flag but one.
+ * @param[in] from Client sending the command.
+ * @param[in] cmd Long name of command (ignored).
+ * @param[in] tok Short name of command.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] require Only send to servers with this Flag bit set.
+ * @param[in] forbid Do not send to servers with this Flag bit set.
+ * @param[in] pattern Format string for command arguments.
+ */
+void sendcmdto_flag_serv_butone(struct Client *from, const char *cmd,
+                                const char *tok, struct Client *one,
+                                int require, int forbid,
+                                const char *pattern, ...)
 {
-  va_list vl;
-  struct Membership* member;
-  struct Client *acptr;
-  int i;
-#ifndef NO_PROTOCOL9
-  char  target[128];
-  char* source;
-  char* tp;
-  char* msg;
-#endif
+  struct VarData vd;
+  struct MsgBuf *mb;
+  struct DLink *lp;
 
-  va_start(vl, pattern);
+  vd.vd_format = pattern; /* set up the struct VarData for %v */
+  va_start(vd.vd_args, pattern);
 
-  ++sentalong_marker;
-  for (member = chptr->members; member; member = member->next_member)
-  {
-    acptr = member->user;
-    if (acptr->from == acptr || /* Skip local clients */
-#ifndef NO_PROTOCOL9
-        Protocol(acptr->from) < 10 ||   /* Skip P09 links */
-#endif
-        acptr->from == one ||   /* ...was the one I should skip */
-        !IsChanOp(member) ||   /* Skip non chanops */
-        IsZombie(member) || IsDeaf(acptr))
-      continue;
-    if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
-    {
-      sentalong[i] = sentalong_marker;
-      /* Don't send channel messages to links that are
-         still eating the net.burst: -- Run 2/1/1997 */
-      if (!IsBurstOrBurstAck(acptr->from))
-        vsendto_prefix_one(acptr, from, pattern, vl);
-    }
-  }
+  /* use token */
+  mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
+  va_end(vd.vd_args);
 
-#ifndef NO_PROTOCOL9
-  /* Send message to all 2.9 servers */
-  /* This is a hack, because it assumes that we know how `vl' is build up */
-  source = va_arg(vl, char *);
-  tp = va_arg(vl, char *);      /* Channel */
-  msg = va_arg(vl, char *);
-  for (member = chptr->members; member; member = member->next_member)
-  {
-    acptr = member->user;
-    if (acptr->from == acptr || /* Skip local clients */
-        Protocol(acptr->from) > 9 ||    /* Skip P10 servers */
-        acptr->from == one ||   /* ...was the one I should skip */
-        !IsChanOp(member) ||   /* Skip non chanops */
-        IsZombie(member) || IsDeaf(acptr))
+  /* send it to our downlinks */
+  for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
+    if (one && lp->value.cptr == cli_from(one))
       continue;
-    if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
-    {
-      sentalong[i] = sentalong_marker;
-      /* Don't send channel messages to links that are
-         still eating the net.burst: -- Run 2/1/1997 */
-      if (!IsBurstOrBurstAck(acptr->from))
-      {
-        struct Membership* other_member;
-        struct Client* acptr2;
-        tp = target;
-        *tp = 0;
-        /* Find all chanops in this direction: */
-        for (other_member = chptr->members; other_member; other_member = other_member->next_member)
-        {
-          acptr2 = other_member->user;
-          if (acptr2->from == acptr->from && acptr2->from != one &&
-              IsChanOp(other_member) && !IsZombie(other_member) &&
-              !IsDeaf(acptr2))
-          {
-            int len = strlen(acptr2->name);
-            if (tp + len + 2 > target + sizeof(target))
-            {
-              sendto_prefix_one(acptr, from,
-                  ":%s NOTICE %s :%s", source, target, msg);
-              tp = target;
-              *tp = 0;
-            }
-            if (*target)
-              strcpy(tp++, ",");
-            strcpy(tp, acptr2->name);
-            tp += len;
-          }
-        }
-        sendto_prefix_one(acptr, from,
-            ":%s NOTICE %s :%s", source, target, msg);
-      }
-    }
+    if ((require < FLAG_LAST_FLAG) && !HasFlag(lp->value.cptr, require))
+      continue;
+    if ((forbid < FLAG_LAST_FLAG) && HasFlag(lp->value.cptr, forbid))
+      continue;
+    send_buffer(lp->value.cptr, mb, 0);
   }
-#endif
 
-  va_end(vl);
-  return;
+  msgq_clean(mb);
 }
 
-/*
- * sendto_server_butone
- *
- * Send a message to all connected servers except the client 'one'.
+/**
+ * Send a (prefixed) command to all servers but one.
+ * @param[in] from Client sending the command.
+ * @param[in] cmd Long name of command (ignored).
+ * @param[in] tok Short name of command.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] pattern Format string for command arguments.
  */
-void sendto_serv_butone(struct Client *one, const char* pattern, ...)
+void sendcmdto_serv_butone(struct Client *from, const char *cmd,
+                          const char *tok, struct Client *one,
+                          const char *pattern, ...)
 {
-  va_list vl;
+  struct VarData vd;
+  struct MsgBuf *mb;
   struct DLink *lp;
 
-  va_start(vl, pattern);
-  vsprintf_irc(sendbuf, pattern, vl);
-  va_end(vl);
+  vd.vd_format = pattern; /* set up the struct VarData for %v */
+  va_start(vd.vd_args, pattern);
 
-  for (lp = me.serv->down; lp; lp = lp->next)
-  {
-    if (one && lp->value.cptr == one->from)
+  /* use token */
+  mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
+  va_end(vd.vd_args);
+
+  /* send it to our downlinks */
+  for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
+    if (one && lp->value.cptr == cli_from(one))
       continue;
-    sendbufto_one(lp->value.cptr);
+    send_buffer(lp->value.cptr, mb, 0);
   }
 
+  msgq_clean(mb);
 }
 
-/*
- * sendbufto_serv_butone()
- *
- * Send prepared sendbuf to all connected servers except the client 'one'
- *  -Ghostwolf 18-May-97
+/** Safely increment the sentalong marker.
+ * This increments the sentalong marker.  Since new connections will
+ * have con_sentalong() == 0, and to avoid confusion when the counter
+ * wraps, we reset all sentalong markers to zero when the sentalong
+ * marker hits zero.
+ * @param[in,out] one Client to mark with new sentalong marker (if any).
  */
-void sendbufto_serv_butone(struct Client *one)
+static void
+bump_sentalong(struct Client *one)
 {
-  struct DLink *lp;
-
-  for (lp = me.serv->down; lp; lp = lp->next)
+  if (!++sentalong_marker)
   {
-    if (one && lp->value.cptr == one->from)
-      continue;
-    sendbufto_one(lp->value.cptr);
+    int ii;
+    for (ii = 0; ii < HighestFd; ++ii)
+      if (LocalClientArray[ii])
+        cli_sentalong(LocalClientArray[ii]) = 0;
+    ++sentalong_marker;
   }
+  if (one)
+    cli_sentalong(one) = sentalong_marker;
 }
 
-
-/*
- * sendto_common_channels()
- *
- * Sends a message to all people (inclusing `acptr') on local server
- * who are in same channel with client `acptr'.
+/** Send a (prefixed) command to all channels that \a from is on.
+ * @param[in] from Client originating the command.
+ * @param[in] cmd Long name of command.
+ * @param[in] tok Short name of command.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] pattern Format string for command arguments.
  */
-void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
+void sendcmdto_common_channels_butone(struct Client *from, const char *cmd,
+                                     const char *tok, struct Client *one,
+                                     const char *pattern, ...)
 {
-  va_list vl;
-  struct Membership* chan;
-  struct Membership* member;
+  struct VarData vd;
+  struct MsgBuf *mb;
+  struct Membership *chan;
+  struct Membership *member;
 
-  assert(0 != acptr);
-  assert(0 != acptr->from);
+  assert(0 != from);
+  assert(0 != cli_from(from));
   assert(0 != pattern);
+  assert(!IsServer(from) && !IsMe(from));
 
-  va_start(vl, pattern);
+  vd.vd_format = pattern; /* set up the struct VarData for %v */
 
-  ++sentalong_marker;
-  if (-1 < acptr->from->fd)
-    sentalong[acptr->from->fd] = sentalong_marker;
+  va_start(vd.vd_args, pattern);
+
+  /* build the buffer */
+  mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
+  va_end(vd.vd_args);
+
+  bump_sentalong(from);
   /*
-   * loop through acptr's channels, and the members on their channels
+   * loop through from's channels, and the members on their channels
    */
-  if (acptr->user) {
-    for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
-      for (member = chan->channel->members; member; member = member->next_member) {
-        struct Client *cptr = member->user;
-        int    i;
-        if (MyConnect(cptr) && 
-            -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
-          sentalong[i] = sentalong_marker;
-          vsendto_prefix_one(cptr, acptr, pattern, vl);
-        }
+  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
+          && cli_sentalong(member->user) != sentalong_marker) {
+       cli_sentalong(member->user) = sentalong_marker;
+       send_buffer(member->user, mb, 0);
       }
-    }
   }
-  if (MyConnect(acptr))
-    vsendto_prefix_one(acptr, acptr, pattern, vl);
-  va_end(vl);
-  return;
-}
 
-/*
- * sendto_channel_butserv
- *
- * Send a message to all members of a channel that
- * are connected to this server.
- */
-void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
-{
-  va_list vl;
-  struct Membership* member;
-  struct Client *acptr;
-  
-  va_start(vl, pattern);
-
-  for (member = chptr->members; member; member = member->next_member) {
-    acptr = member->user;
-    if (MyConnect(acptr) && !IsZombie(member))
-      vsendto_prefix_one(acptr, from, pattern, vl);
-  }
-  va_end(vl);
-  return;
-}
+  if (MyConnect(from) && from != one)
+    send_buffer(from, mb, 0);
 
-/*
- * Send a msg to all ppl on servers/hosts that match a specified mask
- * (used for enhanced PRIVMSGs)
- *
- *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
- */
-
-static int match_it(struct Client *one, const char *mask, int what)
-{
-  switch (what)
-  {
-    case MATCH_HOST:
-      return (match(mask, one->user->host) == 0);
-    case MATCH_SERVER:
-    default:
-      return (match(mask, one->user->server->name) == 0);
-  }
+  msgq_clean(mb);
 }
 
-/*
- * sendto_match_butone
- *
- * Send to all clients which match the mask in a way defined on 'what';
- * either by user hostname or user servername.
+/** Send a (prefixed) command to all local users on a channel.
+ * @param[in] from Client originating the command.
+ * @param[in] cmd Long name of command.
+ * @param[in] tok Short name of command (ignored).
+ * @param[in] to Destination channel.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] skip Bitmask of SKIP_DEAF, SKIP_NONOPS, SKIP_NONVOICES indicating which clients to skip.
+ * @param[in] pattern Format string for command arguments.
  */
-void sendto_match_butone(struct Client *one, struct Client *from,
-    const char *mask, int what, const char* pattern, ...)
-{
-  va_list vl;
-  int i;
-  struct Client *cptr, *acptr;
-
-  va_start(vl, pattern);
-  for (i = 0; i <= HighestFd; i++)
-  {
-    if (!(cptr = LocalClientArray[i]))
-      continue;                 /* that clients are not mine */
-    if (cptr == one)            /* must skip the origin !! */
-      continue;
-    if (IsServer(cptr))
-    {
-      for (acptr = GlobalClientList; acptr; acptr = acptr->next)
-        if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
-          break;
-      /* a person on that server matches the mask, so we
-       *  send *one* msg to that server ...
-       */
-      if (acptr == NULL)
+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;
+  struct Membership *member;
+
+  vd.vd_format = pattern; /* set up the struct VarData for %v */
+  va_start(vd.vd_args, pattern);
+
+  /* build the buffer */
+  mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
+  va_end(vd.vd_args);
+
+  /* send the buffer to each local channel member */
+  for (member = to->members; member; member = member->next_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;
-      /* ... but only if there *IS* a matching person */
-    }
-    /* my client, does he match ? */
-    else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
-      continue;
-    vsendto_prefix_one(cptr, from, pattern, vl);
+      send_buffer(member->user, mb, 0);
   }
-  va_end(vl);
 
-  return;
+  msgq_clean(mb);
 }
 
-/*
- * sendto_lops_butone
- *
- * Send to *local* ops but one.
+/** Send a (prefixed) command to all servers with users on \a to.
+ * Skip \a from and \a one plus those indicated in \a skip.
+ * @param[in] from Client originating the command.
+ * @param[in] cmd Long name of command (ignored).
+ * @param[in] tok Short name of command.
+ * @param[in] to Destination channel.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] skip Bitmask of SKIP_NONOPS and SKIP_NONVOICES indicating which clients to skip.
+ * @param[in] pattern Format string for command arguments.
  */
-void sendto_lops_butone(struct Client* one, const char* pattern, ...)
-{
-  va_list         vl;
-  struct Client*  cptr;
-  struct Client** clients = me.serv->client_list;
-  int             i;
-  char            nbuf[1024];
-
-  assert(0 != clients);
-
-  sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
-  va_start(vl, pattern);
-  vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
-  va_end(vl);
-
-  for (i = 0; i <= me.serv->nn_mask; ++i) {
-    if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
-      sprintf_irc(sendbuf, nbuf, cptr->name);
-      sendbufto_one(cptr);
-    }
+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 */
+  bump_sentalong(one);
+  cli_sentalong(from) = sentalong_marker;
+  for (member = to->members; member; member = member->next_member) {
+    if (MyConnect(member->user)
+        || IsZombie(member)
+        || cli_fd(cli_from(member->user)) < 0
+        || cli_sentalong(member->user) == sentalong_marker
+        || (skip & SKIP_NONOPS && !IsChanOp(member))
+        || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
+      continue;
+    cli_sentalong(member->user) = sentalong_marker;
+    send_buffer(member->user, serv_mb, 0);
   }
+  msgq_clean(serv_mb);
 }
 
-/*
- * sendto_op_mask
- *
- * Sends message to the list indicated by the bitmask field.
- * Don't try to send to more than one list! That is not supported.
- * Xorath 5/1/97
- */
-void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
-{
-  static char fmt[1024];
-  char *fmt_target;
-  int i = 0;            /* so that 1 points to opsarray[0] */
-  struct SLink *opslist;
 
-  while ((mask >>= 1))
-    i++;
-  if (!(opslist = opsarray[i]))
-    return;
+/** Send a (prefixed) command to all users on this channel, except for
+ * \a one and those matching \a skip.
+ * @warning \a pattern must not contain %v.
+ * @param[in] from Client originating the command.
+ * @param[in] cmd Long name of command.
+ * @param[in] tok Short name of command.
+ * @param[in] to Destination channel.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] skip Bitmask of SKIP_NONOPS, SKIP_NONVOICES, SKIP_DEAF, SKIP_BURST.
+ * @param[in] pattern Format string for command arguments.
+ */
+void sendcmdto_channel_butone(struct Client *from, const char *cmd,
+                             const char *tok, struct Channel *to,
+                             struct Client *one, unsigned int skip,
+                             const char *pattern, ...)
+{
+  struct Membership *member;
+  struct VarData vd;
+  struct MsgBuf *user_mb;
+  struct MsgBuf *serv_mb;
+
+  vd.vd_format = pattern;
+
+  /* Build buffer to send to users */
+  va_start(vd.vd_args, pattern);
+  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 */
+  va_start(vd.vd_args, pattern);
+  serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
+  va_end(vd.vd_args);
+
+  /* send buffer along! */
+  bump_sentalong(one);
+  for (member = to->members; member; member = member->next_member) {
+    /* skip one, zombies, and deaf users... */
+    if (IsZombie(member) ||
+        (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 ||
+        cli_sentalong(member->user) == sentalong_marker)
+      continue;
+    cli_sentalong(member->user) = sentalong_marker;
 
-  fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
-  do
-  {
-    strcpy(fmt_target, opslist->value.cptr->name);
-    strcat(fmt_target, " :*** Notice -- ");
-    strcat(fmt_target, pattern);
-    vsendto_one(opslist->value.cptr, fmt, vl);
-    opslist = opslist->next;
+    if (MyConnect(member->user)) /* pick right buffer to send */
+      send_buffer(member->user, user_mb, 0);
+    else
+      send_buffer(member->user, serv_mb, 0);
   }
-  while (opslist);
-}
 
-/*
- * sendbufto_op_mask
- *
- * Send a prepared sendbuf to the list indicated by the bitmask field.
- * Ghostwolf 16-May-97
- */
-void sendbufto_op_mask(unsigned int mask)
-{
-  int i = 0;            /* so that 1 points to opsarray[0] */
-  struct SLink *opslist;
-  while ((mask >>= 1))
-    i++;
-  if (!(opslist = opsarray[i]))
-    return;
-  do
-  {
-    sendbufto_one(opslist->value.cptr);
-    opslist = opslist->next;
-  }
-  while (opslist);
+  msgq_clean(user_mb);
+  msgq_clean(serv_mb);
 }
 
-
-/*
- * sendto_ops
- *
- * Send to *local* ops only.
+/** Send a (prefixed) WALL of type \a type to all users except \a one.
+ * @warning \a pattern must not contain %v.
+ * @param[in] from Source of the command.
+ * @param[in] type One of WALL_DESYNCH, WALL_WALLOPS or WALL_WALLUSERS.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] pattern Format string for command arguments.
  */
-void vsendto_ops(const char *pattern, va_list vl)
+void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
+                            const char *pattern, ...)
 {
+  struct VarData vd;
   struct Client *cptr;
+  struct MsgBuf *mb;
+  struct DLink *lp;
+  char *prefix=NULL;
+  char *tok=NULL;
+  int his_wallops;
   int i;
-  char fmt[1024];
-  char *fmt_target;
 
-  fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
+  vd.vd_format = pattern;
+
+  /* Build buffer to send to users */
+  va_start(vd.vd_args, pattern);
+  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! */
+  his_wallops = feature_bool(FEAT_HIS_WALLOPS);
   for (i = 0; i <= HighestFd; i++)
-    if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
-        SendServNotice(cptr))
-    {
-      strcpy(fmt_target, cptr->name);
-      strcat(fmt_target, " :*** Notice -- ");
-      strcat(fmt_target, pattern);
-      vsendto_one(cptr, fmt, vl);
-    }
-}
+  {
+    if (!(cptr = LocalClientArray[i]) ||
+       (cli_fd(cli_from(cptr)) < 0) ||
+       (type == WALL_DESYNCH && !SendDebug(cptr)) ||
+       (type == WALL_WALLOPS &&
+         (!SendWallops(cptr) || (his_wallops && !IsAnOper(cptr)))) ||
+        (type == WALL_WALLUSERS && !SendWallops(cptr)))
+      continue; /* skip it */
+    send_buffer(cptr, mb, 1);
+  }
 
-void sendto_op_mask(unsigned int mask, const char *pattern, ...)
-{
-  va_list vl;
-  va_start(vl, pattern);
-  vsendto_op_mask(mask, pattern, vl);
-  va_end(vl);
-}
+  msgq_clean(mb);
 
-void sendto_ops(const char *pattern, ...)
-{
-  va_list vl;
-  va_start(vl, pattern);
-  vsendto_op_mask(SNO_OLDSNO, pattern, vl);
-  va_end(vl);
-}
-
-/*
- * sendto_ops_butone
- *
- * Send message to all operators.
- * one - client not to send message to
- * from- client which message is from *NEVER* NULL!!
- */
-void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
-{
-  va_list vl;
-  int i;
-  struct Client *cptr;
+  /* Build buffer to send to servers */
+  va_start(vd.vd_args, pattern);
+  mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
+  va_end(vd.vd_args);
 
-  va_start(vl, pattern);
-  ++sentalong_marker;
-  for (cptr = GlobalClientList; cptr; cptr = cptr->next)
-  {
-    if (!SendWallops(cptr))
+  /* send buffer along! */
+  for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
+    if (one && lp->value.cptr == cli_from(one))
       continue;
-    i = cptr->from->fd;         /* find connection oper is on */
-    if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
-      continue;
-    if (cptr->from == one)
-      continue;                 /* ...was the one I should skip */
-    sentalong[i] = sentalong_marker;
-    vsendto_prefix_one(cptr->from, from, pattern, vl);
+    send_buffer(lp->value.cptr, mb, 1);
   }
-  va_end(vl);
 
-  return;
+  msgq_clean(mb);
 }
 
-/*
- * sendto_g_serv_butone
- *
- * Send message to all remote +g users (server links).
- *
- * one - server not to send message to.
+/** Send a (prefixed) command to all users matching \a to as \a who.
+ * @warning \a pattern must not contain %v.
+ * @param[in] from Source of the command.
+ * @param[in] cmd Long name of command.
+ * @param[in] tok Short name of command.
+ * @param[in] to Destination host/server mask.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] who Type of match for \a to (either MATCH_HOST or MATCH_SERVER).
+ * @param[in] pattern Format string for command arguments.
  */
-void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
+void sendcmdto_match_butone(struct Client *from, const char *cmd,
+                           const char *tok, const char *to,
+                           struct Client *one, unsigned int who,
+                           const char *pattern, ...)
 {
-  va_list vl;
+  struct VarData vd;
   struct Client *cptr;
-  int i;
-
-  va_start(vl, pattern);
-  ++sentalong_marker;
-  vsprintf_irc(sendbuf, pattern, vl);
-  for (cptr = GlobalClientList; cptr; cptr = cptr->next)
-  {
-    if (!SendDebug(cptr))
-      continue;
-    i = cptr->from->fd;         /* find connection user is on */
-    if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
-      continue;
-    if (MyConnect(cptr))
-      continue;
-    sentalong[i] = sentalong_marker;
-    if (cptr->from == one)
-      continue;
-    sendbufto_one(cptr);
+  struct MsgBuf *user_mb;
+  struct MsgBuf *serv_mb;
+
+  vd.vd_format = pattern;
+
+  /* Build buffer to send to users */
+  va_start(vd.vd_args, pattern);
+  user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
+  va_end(vd.vd_args);
+
+  /* Build buffer to send to servers */
+  va_start(vd.vd_args, pattern);
+  serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
+  va_end(vd.vd_args);
+
+  /* send buffer along */
+  bump_sentalong(one);
+  for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
+    if (!IsRegistered(cptr) || IsServer(cptr) || cli_fd(cli_from(cptr)) < 0 ||
+        cli_sentalong(cptr) == sentalong_marker ||
+        !match_it(from, cptr, to, who))
+      continue; /* skip it */
+    cli_sentalong(cptr) = sentalong_marker;
+
+    if (MyConnect(cptr)) /* send right buffer */
+      send_buffer(cptr, user_mb, 0);
+    else
+      send_buffer(cptr, serv_mb, 0);
   }
-  va_end(vl);
 
-  return;
+  msgq_clean(user_mb);
+  msgq_clean(serv_mb);
 }
 
-/*
- * sendto_prefix_one
- *
- * to - destination client
- * from - client which message is from
- *
- * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
- * -avalon
+/** Send a server notice to all users subscribing to the indicated \a
+ * mask except for \a one.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] mask One of the SNO_* constants.
+ * @param[in] pattern Format string for server notice.
  */
-void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
+void sendto_opmask_butone(struct Client *one, unsigned int mask,
+                         const char *pattern, ...)
 {
   va_list vl;
+
   va_start(vl, pattern);
-  vsendto_prefix_one(to, from, pattern, vl);
+  vsendto_opmask_butone(one, mask, pattern, vl);
   va_end(vl);
 }
 
-/*
- * sendto_realops
- *
- * Send to *local* ops only but NOT +s nonopers.
+/** Send a server notice to all users subscribing to the indicated \a
+ * mask except for \a one, rate-limited to once per 30 seconds.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] mask One of the SNO_* constants.
+ * @param[in,out] rate Pointer to the last time the message was sent.
+ * @param[in] pattern Format string for server notice.
  */
-void sendto_realops(const char *pattern, ...)
+void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
+                                     time_t *rate, const char *pattern, ...)
 {
   va_list vl;
 
-  va_start(vl, pattern);
-  vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
-
-  va_end(vl);
-  return;
-}
+  if ((CurrentTime - *rate) < 30)
+    return;
+  else
+    *rate = CurrentTime;
 
-/*
- * Send message to all servers of protocol 'p' and lower.
- */
-void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
-{
-  va_list vl;
-  struct DLink *lp;
   va_start(vl, pattern);
-  for (lp = me.serv->down; lp; lp = lp->next)
-    if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
-      vsendto_one(lp->value.cptr, pattern, vl);
+  vsendto_opmask_butone(one, mask, pattern, vl);
   va_end(vl);
 }
 
-/*
- * Send message to all servers of protocol 'p' and higher.
+
+/** Send a server notice to all users subscribing to the indicated \a
+ * mask except for \a one.
+ * @param[in] one Client direction to skip (or NULL).
+ * @param[in] mask One of the SNO_* constants.
+ * @param[in] pattern Format string for server notice.
+ * @param[in] vl Argument list for format string.
  */
-void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
+void vsendto_opmask_butone(struct Client *one, unsigned int mask,
+                          const char *pattern, va_list vl)
 {
-  va_list vl;
-  struct DLink *lp;
-  va_start(vl, pattern);
-  for (lp = me.serv->down; lp; lp = lp->next)
-    if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
-      vsendto_one(lp->value.cptr, pattern, vl);
-  va_end(vl);
+  struct VarData vd;
+  struct MsgBuf *mb;
+  int i = 0; /* so that 1 points to opsarray[0] */
+  struct SLink *opslist;
+
+  while ((mask >>= 1))
+    i++;
+
+  if (!(opslist = opsarray[i]))
+    return;
+
+  /*
+   * build string; I don't want to bother with client nicknames, so I hope
+   * this is ok...
+   */
+  vd.vd_format = pattern;
+  va_copy(vd.vd_args, vl);
+  mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
+                &vd);
+
+  for (; opslist; opslist = opslist->next)
+    if (opslist->value.cptr != one)
+      send_buffer(opslist->value.cptr, mb, 0);
+
+  msgq_clean(mb);
 }