Author: Isomer <isomer@coders.net>
[ircu2.10.12-pk.git] / ircd / channel.c
index 4927d078b0ed926dd5fcab1cb57e8a419ad724d5..76a20e8e1d0cdffec7a931a2182714cd969b47d6 100644 (file)
@@ -26,6 +26,7 @@
 #include "ircd_alloc.h"
 #include "ircd_chattr.h"
 #include "ircd_reply.h"
+#include "ircd_snprintf.h"
 #include "ircd_string.h"
 #include "list.h"
 #include "match.h"
@@ -226,7 +227,7 @@ int sub1_from_channel(struct Channel* chptr)
     int i;
     for (i = 0; i <= HighestFd; i++)
     {
-      struct Client *acptr;
+      struct Client *acptr = 0;
       if ((acptr = LocalClientArray[i]) && acptr->listing &&
           acptr->listing->chptr == chptr)
       {
@@ -849,6 +850,7 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
   struct SLink*      lp2;
   char modebuf[MODEBUFLEN];
   char parabuf[MODEBUFLEN];
+  char sndbuf[IRC_BUFSIZE];
 
   assert(0 != cptr);
   assert(0 != chptr); 
@@ -868,21 +870,21 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
                                  all in one message */
 
     /* (Continued) prefix: "<Y> B <channel> <TS>" */
-    sprintf_irc(sendbuf, "%s B %s " TIME_T_FMT, NumServ(&me),
-                chptr->chname, chptr->creationtime);
-    sblen = strlen(sendbuf);
+    /* is there any better way we can do this? */
+    sblen = ircd_snprintf(&me, sndbuf, sizeof(sndbuf), "%C " TOK_BURST
+                         " %H %Tu", &me, chptr, chptr->creationtime);
 
     if (first && modebuf[1])    /* Add simple modes (iklmnpst)
                                  if first message */
     {
       /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
-      sendbuf[sblen++] = ' ';
-      strcpy(sendbuf + sblen, modebuf);
+      sndbuf[sblen++] = ' ';
+      strcpy(sndbuf + sblen, modebuf);
       sblen += strlen(modebuf);
       if (*parabuf)
       {
-        sendbuf[sblen++] = ' ';
-        strcpy(sendbuf + sblen, parabuf);
+        sndbuf[sblen++] = ' ';
+        strcpy(sndbuf + sblen, parabuf);
         sblen += strlen(parabuf);
       }
     }
@@ -911,11 +913,11 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
                                  mode. --Gte */
           break;              /* Do not add this member to this message */
         }
-        sendbuf[sblen++] = first ? ' ' : ',';
+        sndbuf[sblen++] = first ? ' ' : ',';
         first = 0;              /* From now on, us comma's to add new nicks */
 
-        sprintf_irc(sendbuf + sblen, "%s%s", NumNick(member->user));
-        sblen += strlen(sendbuf + sblen);
+       sblen += ircd_snprintf(&me, sndbuf + sblen, sizeof(sndbuf) - sblen,
+                              "%C", member->user);
         /*
          * Do we have a nick with a new mode ?
          * Or are we starting a new BURST line?
@@ -924,11 +926,11 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
         {
           new_mode = 0;
           if (IsVoicedOrOpped(member)) {
-            sendbuf[sblen++] = ':';
+            sndbuf[sblen++] = ':';
             if (IsChanOp(member))
-              sendbuf[sblen++] = 'o';
+              sndbuf[sblen++] = 'o';
             if (HasVoice(member))
-              sendbuf[sblen++] = 'v';
+              sndbuf[sblen++] = 'v';
           }
         }
       }
@@ -954,19 +956,19 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
         if (first)
         {
           first = 0;
-          sendbuf[sblen++] = ' ';
-          sendbuf[sblen++] = ':';       /* Will be last parameter */
-          sendbuf[sblen++] = '%';       /* To tell bans apart */
+          sndbuf[sblen++] = ' ';
+          sndbuf[sblen++] = ':';       /* Will be last parameter */
+          sndbuf[sblen++] = '%';       /* To tell bans apart */
         }
         else
-          sendbuf[sblen++] = ' ';
-        strcpy(sendbuf + sblen, lp2->value.ban.banstr);
+          sndbuf[sblen++] = ' ';
+        strcpy(sndbuf + sblen, lp2->value.ban.banstr);
         sblen += len;
       }
     }
 
-    sendbuf[sblen] = '\0';
-    sendbufto_one(cptr);        /* Send this message */
+    sndbuf[sblen] = '\0';
+    send_buffer(cptr, sndbuf);  /* Send this message */
   }                             /* Continue when there was something
                                  that didn't fit (full==1) */
 }
@@ -1235,7 +1237,7 @@ int set_mode(struct Client* cptr, struct Client* sptr,
             break;
            }
            else {
-             sprintf_irc(sendbuf,":%s NOTICE * :*** Notice -- Deop of +k user on %s by %s",
+             sprintf_irc(sendbuf,":%s NOTICE * :*** Notice -- Deop of +k user on %s by %s", /* XXX set_mode only called by old m_mode */
                          me.name,chptr->chname,cptr->name);             
            }
         }
@@ -1964,8 +1966,8 @@ int set_mode(struct Client* cptr, struct Client* sptr,
       len[0] = strlen(ban[0]->value.ban.banstr);
       cnt = 1;                  /* We already got one ban :) */
       /* XXX sendbuf used to send ban bounces! */
-      sblen = sprintf_irc(sendbuf, ":%s MODE %s +b",
-          me.name, chptr->chname) - sendbuf;
+      sblen = sprintf_irc(sendbuf, ":%s MODE %s +b", /* XXX set_mode only called by old m_mode */
+          me.name, chptr->chname) - sendbuf; /* XXX set_mode only called by old m_mode */
       total_len = sblen + 1 + len[0];   /* 1 = ' ' */
       /* Find more bans: */
       delayed = 0;
@@ -1977,16 +1979,16 @@ int set_mode(struct Client* cptr, struct Client* sptr,
           delayed = cnt + 1;    /* != 0 */
           break;                /* Flush */
         }
-        sendbuf[sblen++] = 'b';
+        sendbuf[sblen++] = 'b'; /* XXX set_mode only called by old m_mode */
         total_len += 2 + len[cnt++];    /* 2 = "b " */
       }
       while (cnt--)
       {
-        sendbuf[sblen++] = ' ';
-        strcpy(sendbuf + sblen, ban[cnt]->value.ban.banstr);
+        sendbuf[sblen++] = ' '; /* XXX set_mode only called by old m_mode */
+        strcpy(sendbuf + sblen, ban[cnt]->value.ban.banstr); /* XXX set_mode only called by old m_mode */
         sblen += len[cnt];
       }
-      sendbufto_one(cptr);      /* Send bounce to uplink */
+      sendbufto_one(cptr);      /* Send bounce to uplink */ /* XXX set_mode only called by old m_mode */
       if (delayed)
         ban[0] = ban[delayed - 1];
     }
@@ -2002,11 +2004,11 @@ int set_mode(struct Client* cptr, struct Client* sptr,
     struct Client *acptr;
     if (IsServer(sptr))
       /* XXX sendbuf used to send ban bounces! */
-      psblen = sprintf_irc(sendbuf, ":%s MODE %s -b",
-          sptr->name, chptr->chname) - sendbuf;
+      psblen = sprintf_irc(sendbuf, ":%s MODE %s -b", /* XXX set_mode only called by old m_mode */
+          sptr->name, chptr->chname) - sendbuf; /* XXX set_mode only called by old m_mode */
     else                        /* We rely on IsRegistered(sptr) being true for MODE */
-      psblen = sprintf_irc(sendbuf, ":%s!%s@%s MODE %s -b", sptr->name,
-          sptr->user->username, sptr->user->host, chptr->chname) - sendbuf;
+      psblen = sprintf_irc(sendbuf, ":%s!%s@%s MODE %s -b", sptr->name, /* XXX set_mode only called by old m_mode */
+          sptr->user->username, sptr->user->host, chptr->chname) - sendbuf; /* XXX set_mode only called by old m_mode */
     while (delayed || (ban = next_removed_overlapped_ban()))
     {
       if (!delayed)
@@ -2028,20 +2030,20 @@ int set_mode(struct Client* cptr, struct Client* sptr,
           delayed = cnt + 1;    /* != 0 */
           break;                /* Flush */
         }
-        sendbuf[sblen++] = 'b';
+        sendbuf[sblen++] = 'b'; /* XXX set_mode only called by old m_mode */
         total_len += 2 + len[cnt++];    /* 2 = "b " */
       }
       while (cnt--)
       {
-        sendbuf[sblen++] = ' ';
-        strcpy(sendbuf + sblen, banstr[cnt]);
+        sendbuf[sblen++] = ' '; /* XXX set_mode only called by old m_mode */
+        strcpy(sendbuf + sblen, banstr[cnt]); /* XXX set_mode only called by old m_mode */
         MyFree(banstr[cnt]);
         sblen += len[cnt];
       }
       for (member_z = chptr->members; member_z; member_z = member_z->next_member) {
         acptr = member_z->user;
         if (MyConnect(acptr) && !IsZombie(member_z))
-          sendbufto_one(acptr);
+          sendbufto_one(acptr); /* XXX set_mode only called by old m_mode */
       }
       if (delayed)
       {
@@ -2313,18 +2315,18 @@ void add_token_to_sendbuf(char *token, size_t *sblenp, int *firstp,
     *firstp = 0;
     if (*send_itp == 0)
       *send_itp = 1;            /* Buffer contains data to be sent */
-    sendbuf[(*sblenp)++] = ' ';
+    sendbuf[(*sblenp)++] = ' '; /* XXX add_token_to_sendbuf only called by old m_burst */
     if (is_a_ban)
     {
-      sendbuf[(*sblenp)++] = ':';       /* Bans are always the last "parv" */
-      sendbuf[(*sblenp)++] = is_a_ban;
+      sendbuf[(*sblenp)++] = ':';       /* Bans are always the last "parv" */ /* XXX add_token_to_sendbuf only called by old m_burst */
+      sendbuf[(*sblenp)++] = is_a_ban; /* XXX add_token_to_sendbuf only called by old m_burst */
     }
   }
   else                          /* Of course, 'send_it' is already set here */
     /* Seperate banmasks with a space because
        they can contain commas themselfs: */
-    sendbuf[(*sblenp)++] = is_a_ban ? ' ' : ',';
-  strcpy(sendbuf + *sblenp, token);
+    sendbuf[(*sblenp)++] = is_a_ban ? ' ' : ','; /* XXX add_token_to_sendbuf only called by old m_burst */
+  strcpy(sendbuf + *sblenp, token); /* XXX add_token_to_sendbuf only called by old m_burst */
   *sblenp += strlen(token);
   if (!is_a_ban)                /* nick list ? Need to take care
                                    of modes for nicks: */
@@ -2336,13 +2338,13 @@ void add_token_to_sendbuf(char *token, size_t *sblenp, int *firstp,
     if (last_mode != mode)      /* Append mode like ':ov' if changed */
     {
       last_mode = mode;
-      sendbuf[(*sblenp)++] = ':';
+      sendbuf[(*sblenp)++] = ':'; /* XXX add_token_to_sendbuf only called by old m_burst */
       if (mode & CHFL_CHANOP)
-        sendbuf[(*sblenp)++] = 'o';
+        sendbuf[(*sblenp)++] = 'o'; /* XXX add_token_to_sendbuf only called by old m_burst */
       if (mode & CHFL_VOICE)
-        sendbuf[(*sblenp)++] = 'v';
+        sendbuf[(*sblenp)++] = 'v'; /* XXX add_token_to_sendbuf only called by old m_burst */
     }
-    sendbuf[*sblenp] = '\0';
+    sendbuf[*sblenp] = '\0'; /* XXX add_token_to_sendbuf only called by old m_burst */
   }
 }
 
@@ -2363,7 +2365,7 @@ void cancel_mode(struct Client *sptr, struct Channel *chptr, char m,
   {
     /* XXX sendbuf used! */
     sbp = sbpi =
-        sprintf_irc(sendbuf, ":%s MODE %s -", sptr->name, chptr->chname);
+        sprintf_irc(sendbuf, ":%s MODE %s -", sptr->name, chptr->chname); /* XXX cancel_mode only called from old ms_burst */
     pb = parabuf;
     *count = 0;
   }
@@ -2395,7 +2397,7 @@ void cancel_mode(struct Client *sptr, struct Channel *chptr, char m,
     strcpy(sbp, parabuf);
     for (member = chptr->members; member; member = member->next_member)
       if (MyUser(member->user))
-        sendbufto_one(member->user);
+        sendbufto_one(member->user); /* XXX cancel_mode only called from old ms_burst */
     sbp = sbpi;
     pb = parabuf;
     *count = 0;
@@ -2490,7 +2492,10 @@ void make_zombie(struct Membership* member, struct Client* who, struct Client* c
   if (channel_all_zombies(chptr))
     remove_user_from_channel(who, chptr);
 
+  /* XXX Can't actually call Debug here; if the channel is all zombies,
+   * chptr will no longer exist when we get here.
   Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
+  */
 }
 
 int number_of_zombies(struct Channel *chptr)
@@ -2506,61 +2511,6 @@ int number_of_zombies(struct Channel *chptr)
   return count;
 }
 
-/* XXX we can probably get rid of send_user_joins */
-void send_user_joins(struct Client *cptr, struct Client *user)
-{
-  struct Membership* chan;
-  struct Channel*    chptr;
-  int   cnt = 0;
-  int   len = 0;
-  int   clen;
-  char* mask;
-  char  buf[BUFSIZE];
-
-  *buf = ':';
-  strcpy(buf + 1, user->name);
-  strcat(buf, " JOIN ");
-  len = strlen(user->name) + 7;
-
-  for (chan = user->user->channel; chan; chan = chan->next_channel)
-  {
-    chptr = chan->channel;
-    assert(0 != chptr);
-
-    if ((mask = strchr(chptr->chname, ':')))
-      if (match(++mask, cptr->name))
-        continue;
-    if (*chptr->chname == '&')
-      continue;
-    if (IsZombie(chan))
-      continue;
-    clen = strlen(chptr->chname);
-    if (clen + 1 + len > BUFSIZE - 3)
-    {
-      if (cnt)
-      {
-        buf[len - 1] = '\0';
-        sendto_one(cptr, "%s", buf); /* XXX Possibly DEAD */
-      }
-      *buf = ':';
-      strcpy(buf + 1, user->name);
-      strcat(buf, " JOIN ");
-      len = strlen(user->name) + 7;
-      cnt = 0;
-    }
-    strcpy(buf + len, chptr->chname);
-    cnt++;
-    len += clen;
-    if (chan->next_channel)
-    {
-      len++;
-      strcat(buf, ",");
-    }
-  }
-  if (*buf && cnt)
-    sendto_one(cptr, "%s", buf); /* XXX Possibly DEAD */
-}
-
 /*
  * send_hack_notice()
  *
@@ -2615,18 +2565,18 @@ void send_hack_notice(struct Client *cptr, struct Client *sptr, int parc,
           strcat(params, parv[i]);
         i++;
       }
-      sprintf_irc(sendbuf,
+      sprintf_irc(sendbuf, /* XXX send_hack_notice only called from old m_mode */
           ":%s NOTICE * :*** Notice -- %sHACK(%d): %s MODE %s %s%s ["
           TIME_T_FMT "]", me.name, (badop == 3) ? "BOUNCE or " : "", badop,
           parv[0], parv[1], parv[2], params, chptr->creationtime);
-      sendbufto_op_mask((badop == 3) ? SNO_HACK3 : (badop == /* XXX DYING */
+      sendbufto_op_mask((badop == 3) ? SNO_HACK3 : (badop == /* XXX DYING */ /* XXX send_hack_notice only called from old m_mode */
           4) ? SNO_HACK4 : SNO_HACK2);
 
       if ((IsServer(sptr)) && (badop == 2))
       {
-        sprintf_irc(sendbuf, ":%s DESYNCH :HACK: %s MODE %s %s%s",
+        sprintf_irc(sendbuf, ":%s DESYNCH :HACK: %s MODE %s %s%s", /* XXX send_hack_notice only called from old m_mode */
             me.name, parv[0], parv[1], parv[2], params);
-        sendbufto_serv_butone(cptr); /* XXX DYING */
+        sendbufto_serv_butone(cptr); /* XXX DYING */ /* XXX send_hack_notice only called from old m_mode */
       }
       break;
     }
@@ -2642,14 +2592,14 @@ void send_hack_notice(struct Client *cptr, struct Client *sptr, int parc,
     {
       struct Client *acptr;
       if ((acptr = findNUser(parv[2])) != NULL) /* attempt to convert nick */
-        sprintf_irc(sendbuf,
+        sprintf_irc(sendbuf, /* XXX send_hack_notice only called from old m_mode */
             ":%s NOTICE * :*** Notice -- HACK: %s KICK %s %s :%s",
             me.name, sptr->name, parv[1], acptr->name, parv[3]);
       else                      /* if conversion fails, send it 'as is' in <>'s */
-        sprintf_irc(sendbuf,
+        sprintf_irc(sendbuf, /* XXX send_hack_notice only called from old m_mode */
             ":%s NOTICE * :*** Notice -- HACK: %s KICK %s <%s> :%s",
             me.name, sptr->name, parv[1], parv[2], parv[3]);
-      sendbufto_op_mask(SNO_HACK4); /* XXX DYING */
+      sendbufto_op_mask(SNO_HACK4); /* XXX DYING */ /* XXX send_hack_notice only called from old m_mode */
       break;
     }
   }
@@ -3121,6 +3071,70 @@ modebuf_flush(struct ModeBuf *mbuf)
   return modebuf_flush_int(mbuf, 1);
 }
 
+/*
+ * This extracts the simple modes contained in mbuf
+ */
+void
+modebuf_extract(struct ModeBuf *mbuf, char *buf)
+{
+  static int flags[] = {
+/*  MODE_CHANOP,       'o', */
+/*  MODE_VOICE,                'v', */
+    MODE_PRIVATE,      'p',
+    MODE_SECRET,       's',
+    MODE_MODERATED,    'm',
+    MODE_TOPICLIMIT,   't',
+    MODE_INVITEONLY,   'i',
+    MODE_NOPRIVMSGS,   'n',
+    MODE_KEY,          'k',
+/*  MODE_BAN,          'b', */
+    MODE_LIMIT,                'l',
+    0x0, 0x0
+  };
+  unsigned int add;
+  int i, bufpos = 0, len;
+  int *flag_p;
+  char *key = 0, limitbuf[20];
+
+  assert(0 != mbuf);
+  assert(0 != buf);
+
+  buf[0] = '\0';
+
+  add = mbuf->mb_add;
+
+  for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
+    if (MB_TYPE(mbuf, i) & MODE_ADD) {
+      add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT);
+
+      if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
+       key = MB_STRING(mbuf, i);
+      else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
+       ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%d", MB_UINT(mbuf, i));
+    }
+  }
+
+  if (!add)
+    return;
+
+  buf[bufpos++] = '+'; /* start building buffer */
+
+  for (flag_p = flags; flag_p[0]; flag_p += 2)
+    if (*flag_p & add)
+      buf[bufpos++] = flag_p[1];
+
+  for (i = 0, len = bufpos; i < len; i++) {
+    if (buf[i] == 'k')
+      build_string(buf, &bufpos, key, 0, ' ');
+    else if (buf[i] == 'l')
+      build_string(buf, &bufpos, limitbuf, 0, ' ');
+  }
+
+  buf[bufpos] = '\0';
+
+  return;
+}
+
 /*
  * Simple function to invalidate bans
  */
@@ -3208,7 +3222,8 @@ mode_parse_limit(struct ParseState *state, int *flag_p)
     state->parc--;
     state->max_args--;
 
-    if (!t_limit) /* if it was zero, ignore it */
+    if (!(state->flags & MODE_PARSE_WIPEOUT) &&
+       (!t_limit || t_limit == state->chptr->mode.limit))
       return;
   } else
     t_limit = state->chptr->mode.limit;
@@ -3223,7 +3238,8 @@ mode_parse_limit(struct ParseState *state, int *flag_p)
     return;
   state->done |= DONE_LIMIT;
 
-  assert(0 != state->mbuf);
+  if (!state->mbuf)
+    return;
 
   modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
 
@@ -3286,6 +3302,9 @@ mode_parse_key(struct ParseState *state, int *flag_p)
     return;
   }
 
+  if (!state->mbuf)
+    return;
+
   /* can't add a key if one is set, nor can one remove the wrong key */
   if (!(state->flags & MODE_PARSE_FORCE))
     if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
@@ -3295,7 +3314,9 @@ mode_parse_key(struct ParseState *state, int *flag_p)
       return;
     }
 
-  assert(0 != state->mbuf);
+  if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
+      !ircd_strcmp(state->chptr->mode.key, t_str))
+    return; /* no key change */
 
   if (state->flags & MODE_PARSE_BOUNCE) {
     if (*state->chptr->mode.key) /* reset old key */
@@ -3543,13 +3564,8 @@ mode_parse_client(struct ParseState *state, int *flag_p)
   if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
     return;
 
-  if (state->parc <= 0) { /* warn if not enough args */
-    if (MyUser(state->sptr))
-      need_more_params(state->sptr, state->dir == MODE_ADD ?
-                      (flag_p[0] == MODE_CHANOP ? "MODE +o" : "MODE +v") :
-                      (flag_p[0] == MODE_CHANOP ? "MODE -o" : "MODE -v"));
+  if (state->parc <= 0) /* return if not enough args */
     return;
-  }
 
   t_str = state->parv[state->args_used++]; /* grab arg */
   state->parc--;
@@ -3671,7 +3687,8 @@ mode_parse_mode(struct ParseState *state, int *flag_p)
     return;
   }
 
-  assert(0 != state->mbuf);
+  if (!state->mbuf)
+    return;
 
   modebuf_mode(state->mbuf, state->dir | flag_p[0]);
 
@@ -3808,6 +3825,9 @@ mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
       } /* switch (*modestr) { */
     } /* for (; *modestr; modestr++) { */
 
+    if (state.flags & MODE_PARSE_BURST)
+      break; /* don't interpret any more arguments */
+
     if (state.parc > 0) { /* process next argument in string */
       modestr = state.parv[state.args_used++];
       state.parc--;
@@ -3838,10 +3858,17 @@ mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
    * the rest of the function finishes building resultant MODEs; if the
    * origin isn't a member or an oper, skip it.
    */
-  if (state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
+  if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
     return state.args_used; /* tell our parent how many args we gobbled */
 
-  assert(0 != state.mbuf);
+  if (state.flags & MODE_PARSE_WIPEOUT) {
+    if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
+      modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
+                       state.chptr->mode.limit);
+    if (*state.chptr->mode.key && !(state.done & DONE_KEY))
+      modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
+                         state.chptr->mode.key, 0);
+  }
 
   if (state.done & DONE_BANCLEAN) /* process bans */
     mode_process_bans(&state);
@@ -3905,9 +3932,16 @@ joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
        sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
                      (flags & CHFL_BANNED || !jbuf->jb_comment) ?
                      ":%H" : "%H :%s", chan, jbuf->jb_comment);
-
-      /* Remove user from channel */
-      remove_user_from_channel(jbuf->jb_source, chan);
+      /* XXX: Shouldn't we send a PART here anyway? */
+      /* to users on the channel?  Why?  From their POV, the user isn't on
+       * the channel anymore anyway.  We don't send to servers until below,
+       * when we gang all the channel parts together.  Note that this is
+       * exactly the same logic, albeit somewhat more concise, as was in
+       * the original m_part.c */
+
+      if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
+         IsLocalChannel(chan->chname)) /* got to remove user here */
+       remove_user_from_channel(jbuf->jb_source, chan);
     } else {
       /* Add user to channel */
       add_user_to_channel(chan, jbuf->jb_source, flags);
@@ -3916,7 +3950,7 @@ joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
       sendcmdto_channel_butserv(jbuf->jb_source, CMD_JOIN, chan, ":%H", chan);
 
       /* send an op, too, if needed */
-      if (jbuf->jb_type == JOINBUF_TYPE_CREATE &&
+      if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE &&
          !IsModelessChannel(chan->chname))
        sendcmdto_channel_butserv(jbuf->jb_source, CMD_MODE, chan, "%H +o %C",
                                  chan, jbuf->jb_source);
@@ -3957,6 +3991,9 @@ joinbuf_flush(struct JoinBuf *jbuf)
     build_string(chanlist, &chanlist_i,
                 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
                 i == 0 ? '\0' : ',');
+    if (JOINBUF_TYPE_PART == jbuf->jb_type)
+      /* Remove user from channel */
+      remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
 
     jbuf->jb_channels[i] = 0; /* mark slot empty */
   }