2004-05-09 Michael Poole <mdpoole@troilus.org>
[ircu2.10.12-pk.git] / ircd / channel.c
index 1c3d491549ff26c32f8a589c74ed680a7cc9be8b..0aa8da8650b4f73dc2e7318fb4301d4da07af950 100644 (file)
@@ -31,7 +31,6 @@
 #include "ircd_defs.h"
 #include "ircd_features.h"
 #include "ircd_log.h"
-#include "ircd_policy.h"
 #include "ircd_reply.h"
 #include "ircd_snprintf.h"
 #include "ircd_string.h"
@@ -219,10 +218,13 @@ int sub1_from_channel(struct Channel* chptr)
    * who then will educate them on the use of Apass/upass.
    */
 
+   if (feature_bool(FEAT_OPLEVELS)) {
   if (TStime() - chptr->creationtime < 172800) /* Channel younger than 48 hours? */
     schedule_destruct_event_1m(chptr);         /* Get rid of it in approximately 4-5 minutes */
   else
     schedule_destruct_event_48h(chptr);                /* Get rid of it in approximately 48 hours */
+   } else
+       destruct_channel(chptr);
 
   return 0;
 }
@@ -383,11 +385,9 @@ int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
     assert(0 != ban->value.ban.banstr);
     strcpy(ban->value.ban.banstr, banid);
 
-#ifdef HEAD_IN_SAND_BANWHO
-    if (IsServer(cptr))
+    if (IsServer(cptr) && feature_bool(FEAT_HIS_BANWHO))
       DupString(ban->value.ban.who, cli_name(&me));
     else
-#endif
       DupString(ban->value.ban.who, cli_name(cptr));
     assert(0 != ban->value.ban.who);
 
@@ -442,6 +442,7 @@ static int is_banned(struct Client *cptr, struct Channel *chptr,
                      struct Membership* member)
 {
   struct SLink* tmp;
+  char          tmphost[HOSTLEN + 1];
   char          nu_host[NUH_BUFSIZE];
   char          nu_realhost[NUH_BUFSIZE];
   char          nu_ip[NUI_BUFSIZE];
@@ -457,10 +458,23 @@ static int is_banned(struct Client *cptr, struct Channel *chptr,
 
   s = make_nick_user_host(nu_host, cli_name(cptr), (cli_user(cptr))->username,
                          (cli_user(cptr))->host);
-  if (HasHiddenHost(cptr))
-    sr = make_nick_user_host(nu_realhost, cli_name(cptr),
-                            (cli_user(cptr))->username,
-                            cli_user(cptr)->realhost);
+  if (IsAccount(cptr))
+  {
+    if (HasHiddenHost(cptr))
+    {
+      sr = make_nick_user_host(nu_realhost, cli_name(cptr),
+                               (cli_user(cptr))->username,
+                               cli_user(cptr)->realhost);
+    }
+    else
+    {
+      ircd_snprintf(0, tmphost, HOSTLEN, "%s.%s",
+                    cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
+      sr = make_nick_user_host(nu_realhost, cli_name(cptr),
+                               cli_user(cptr)->username,
+                               tmphost);      
+    }
+  }
 
   for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
     if ((tmp->flags & CHFL_BAN_IPMASK)) {
@@ -621,11 +635,6 @@ int is_chan_op(struct Client *cptr, struct Channel *chptr)
   return 0;
 }
 
-int is_level0_op(struct Client *cptr, struct Channel *chptr)
-{
-  return 0;
-}
-
 int is_zombie(struct Client *cptr, struct Channel *chptr)
 {
   struct Membership* member;
@@ -652,6 +661,10 @@ int member_can_send_to_channel(struct Membership* member)
 {
   assert(0 != member);
 
+  /* Discourage using the Apass to get op.  They should use the upass. */
+  if (IsChannelManager(member) && *member->channel->mode.upass)
+    return 0;
+
   if (IsVoicedOrOpped(member))
     return 1;
   /*
@@ -683,12 +696,11 @@ int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
   member = find_channel_member(cptr, chptr);
 
   /*
-   * You can't speak if your off channel, if the channel is modeless, or
-   * +n (no external messages) or +m (moderated).
+   * You can't speak if you're off channel, and it is +n (no external messages)
+   * or +m (moderated).
    */
   if (!member) {
     if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED)) ||
-       IsModelessChannel(chptr->chname) ||
        ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(cptr)))
       return 0;
     else
@@ -699,7 +711,7 @@ int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
 
 /*
  * find_no_nickchange_channel
- * if a member and not opped or voiced and banned
+ * if a member and not (opped or voiced) and (banned or moderated)
  * return the name of the first channel banned on
  */
 const char* find_no_nickchange_channel(struct Client* cptr)
@@ -708,7 +720,9 @@ const char* find_no_nickchange_channel(struct Client* cptr)
     struct Membership* member;
     for (member = (cli_user(cptr))->channel; member;
         member = member->next_channel) {
-      if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
+        if (!IsVoicedOrOpped(member) &&
+            (is_banned(cptr, member->channel, member) ||
+             (member->channel->mode.mode & MODE_MODERATED)))
         return member->channel->chname;
     }
   }
@@ -721,7 +735,7 @@ const char* find_no_nickchange_channel(struct Client* cptr)
  * with the parameters in pbuf.
  */
 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen,
-                          struct Channel *chptr)
+                          struct Channel *chptr, struct Membership *member)
 {
   int previous_parameter = 0;
 
@@ -774,7 +788,7 @@ void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen,
     *mbuf++ = 'u';
     if (previous_parameter)
       strcat(pbuf, " ");
-    if (is_level0_op(cptr, chptr) || IsServer(cptr)) {
+    if (IsServer(cptr) || (member && IsChanOp(member) && OpLevel(member) == 0)) {
       strcat(pbuf, chptr->mode.upass);
     } else
       strcat(pbuf, "*");
@@ -824,7 +838,7 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
   lp2 = chptr->banlist;
 
   *modebuf = *parabuf = '\0';
-  channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr);
+  channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr, 0);
 
   for (first = 1; full; first = 0)      /* Loop for multiple messages */
   {
@@ -1157,7 +1171,8 @@ int can_join(struct Client *sptr, struct Channel *chptr, char *key)
      a HACK(4) notice will be sent if he would not have been supposed
      to join normally. */ 
   if (IsLocalChannel(chptr->chname) && HasPriv(sptr, PRIV_WALK_LCHAN) &&
-      !BadPtr(key) && compall("OVERRIDE",key) == 0)
+      !BadPtr(key) && compall("OVERRIDE",chptr->mode.key) != 0 &&
+      compall("OVERRIDE",key) == 0)
     overrideJoin = MAGIC_OPER_OVERRIDE;
 
   if (chptr->mode.mode & MODE_INVITEONLY)
@@ -1314,7 +1329,8 @@ void list_next_channels(struct Client *cptr, int nr)
   {
     for (; chptr; chptr = chptr->next)
     {
-      if (!cli_user(cptr) || (SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
+      if (!cli_user(cptr) || (!(HasPriv(cptr, PRIV_LIST_CHAN) && IsAnOper(cptr)) && 
+          SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
         continue;
       if (chptr->users > args->min_users && chptr->users < args->max_users &&
           chptr->creationtime > args->min_time &&
@@ -1362,7 +1378,7 @@ void list_next_channels(struct Client *cptr, int nr)
  * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
  *
  * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
- *    Remove the user immedeately when no users are left on the channel.
+ *    Remove the user immediately when no users are left on the channel.
  * b) On server B : remove the user (who/lp) from the channel, send a
  *    PART upstream (to A) and pass on the KICK.
  * c) KICKed by `client'; On server B : remove the user (who/lp) from the
@@ -1451,7 +1467,8 @@ int number_of_zombies(struct Channel *chptr)
  * of course, str2 is not NULL)
  */
 static void
-build_string(char *strptr, int *strptr_i, char *str1, char *str2, char c)
+build_string(char *strptr, int *strptr_i, const char *str1,
+             const char *str2, char c)
 {
   if (c)
     strptr[(*strptr_i)++] = c;
@@ -1523,8 +1540,9 @@ modebuf_flush_int(struct ModeBuf *mbuf, int all)
   if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
     return 0;
 
-  /* Ok, if we were given the OPMODE flag, hide the source if its a user */
-  if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
+  /* Ok, if we were given the OPMODE flag, or its a server, hide the source.
+   */
+  if (mbuf->mb_dest & MODEBUF_DEST_OPMODE || IsServer(mbuf->mb_source))
     app_source = &me;
   else
     app_source = mbuf->mb_source;
@@ -1563,14 +1581,14 @@ modebuf_flush_int(struct ModeBuf *mbuf, int all)
        bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
        totalbuflen -= IRCD_MAX(5, tmp) + 1;
       }
-    } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS)) {
+    } else if (MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS)) {
       tmp = strlen(MB_STRING(mbuf, i));
 
       if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
        MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
       else {
        char mode_char;
-       switch(MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS))
+       switch(MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS))
        {
          case MODE_APASS:
            mode_char = 'A';
@@ -1578,9 +1596,6 @@ modebuf_flush_int(struct ModeBuf *mbuf, int all)
          case MODE_UPASS:
            mode_char = 'u';
            break;
-         case MODE_KEY:
-           mode_char = 'k';
-           break;
          default:
            mode_char = 'b';
            break;
@@ -1588,6 +1603,16 @@ modebuf_flush_int(struct ModeBuf *mbuf, int all)
        bufptr[(*bufptr_i)++] = mode_char;
        totalbuflen -= tmp + 1;
       }
+    } else if (MB_TYPE(mbuf, i) & MODE_KEY) {
+      tmp = (mbuf->mb_dest & MODEBUF_DEST_NOKEY ? 1 :
+            strlen(MB_STRING(mbuf, i)));
+
+      if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
+       MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
+      else {
+       bufptr[(*bufptr_i)++] = 'k';
+       totalbuflen -= tmp + 1;
+      }
     } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
       /* if it's a limit, we also format the number */
       ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
@@ -1633,10 +1658,15 @@ modebuf_flush_int(struct ModeBuf *mbuf, int all)
       if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
        build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
 
-      /* deal with strings... */
-      else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
+      /* deal with bans... */
+      else if (MB_TYPE(mbuf, i) & MODE_BAN)
        build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
 
+      /* deal with keys... */
+      else if (MB_TYPE(mbuf, i) & MODE_KEY)
+       build_string(strptr, strptr_i, mbuf->mb_dest & MODEBUF_DEST_NOKEY ?
+                    "*" : MB_STRING(mbuf, i), 0, ' ');
+
       /* deal with invisible passwords */
       else if (MB_TYPE(mbuf, i) & (MODE_APASS | MODE_UPASS))
        build_string(strptr, strptr_i, "*", 0, ' ');
@@ -1654,11 +1684,8 @@ modebuf_flush_int(struct ModeBuf *mbuf, int all)
     if (mbuf->mb_dest & MODEBUF_DEST_HACK2)
       sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
                           "[%Tu]",
-#ifdef HEAD_IN_SAND_SNOTICES
-                          cli_name(mbuf->mb_source),
-#else
-                          cli_name(app_source),
-#endif
+                           cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
+                                    mbuf->mb_source : app_source),
                           mbuf->mb_channel->chname,
                           rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
                           addbuf, remstr, addstr,
@@ -1667,11 +1694,8 @@ modebuf_flush_int(struct ModeBuf *mbuf, int all)
     if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
       sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
                           "%s%s%s%s%s%s [%Tu]",
-#ifdef HEAD_IN_SAND_SNOTICES
-                          cli_name(mbuf->mb_source),
-#else
-                          cli_name(app_source),
-#endif
+                           cli_name(feature_bool(FEAT_HIS_SNOTICES) ? 
+                                    mbuf->mb_source : app_source),
                           mbuf->mb_channel->chname, rembuf_i ? "-" : "",
                           rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
                           mbuf->mb_channel->creationtime);
@@ -1834,6 +1858,8 @@ modebuf_init(struct ModeBuf *mbuf, struct Client *source,
   assert(0 != chan);
   assert(0 != dest);
 
+  if (IsLocalChannel(chan->chname)) dest &= ~MODEBUF_DEST_SERVER;
+
   mbuf->mb_add = 0;
   mbuf->mb_rem = 0;
   mbuf->mb_source = source;
@@ -1972,7 +1998,7 @@ modebuf_extract(struct ModeBuf *mbuf, char *buf)
   int i, bufpos = 0, len;
   int *flag_p;
   char *key = 0, limitbuf[20];
-  char *apass, *upass;
+  char *apass = 0, *upass = 0;
 
   assert(0 != mbuf);
   assert(0 != buf);
@@ -2178,7 +2204,7 @@ mode_parse_key(struct ParseState *state, int *flag_p)
     return;
   state->done |= DONE_KEY;
 
-  t_len = KEYLEN + 1;
+  t_len = KEYLEN;
 
   /* clean up the key string */
   s = t_str;
@@ -2255,6 +2281,20 @@ mode_parse_upass(struct ParseState *state, int *flag_p)
     return;
   }
 
+  /* If they are not the channel manager, they are not allowed to change it */
+  if (MyUser(state->sptr) && !IsChannelManager(state->member)) {
+    if (*state->chptr->mode.apass) {
+      send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
+         "Use /JOIN", state->chptr->chname, "<AdminPass>.");
+    } else {
+      send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
+         "Re-create the channel.  The channel must be *empty* for",
+         TStime() - state->chptr->creationtime >= 171000 ? "48 contiguous hours" : "a minute or two",
+         "before it can be recreated.");
+    }
+    return;
+  }
   if (state->done & DONE_UPASS) /* allow upass to be set only once */
     return;
   state->done |= DONE_UPASS;
@@ -2277,8 +2317,13 @@ mode_parse_upass(struct ParseState *state, int *flag_p)
   if (!state->mbuf)
     return;
 
-  /* can't add a upass if one is set, nor can one remove the wrong upass */
   if (!(state->flags & MODE_PARSE_FORCE))
+    /* can't add the upass while apass is not set */
+    if (state->dir == MODE_ADD && !*state->chptr->mode.apass) {
+      send_reply(state->sptr, ERR_UPASSNOTSET, state->chptr->chname, state->chptr->chname);
+      return;
+    }
+    /* can't add a upass if one is set, nor can one remove the wrong upass */
     if ((state->dir == MODE_ADD && *state->chptr->mode.upass) ||
        (state->dir == MODE_DEL &&
         ircd_strcmp(state->chptr->mode.upass, t_str))) {
@@ -2336,6 +2381,25 @@ mode_parse_apass(struct ParseState *state, int *flag_p)
     return;
   }
 
+  /* Don't allow to change the Apass if the channel is older than 48 hours. */
+  if (TStime() - state->chptr->creationtime >= 172800 && !IsAnOper(state->sptr)) {
+    send_reply(state->sptr, ERR_CHANSECURED, state->chptr->chname);
+    return;
+  }
+
+  /* If they are not the channel manager, they are not allowed to change it */
+  if (MyUser(state->sptr) && !IsChannelManager(state->member)) {
+    if (*state->chptr->mode.apass) {
+      send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
+         "Use /JOIN", state->chptr->chname, "<AdminPass>.");
+    } else {
+      send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
+         "Re-create the channel.  The channel must be *empty* for",
+         "at least a whole minute", "before it can be recreated.");
+    }
+    return;
+  }
   if (state->done & DONE_APASS) /* allow apass to be set only once */
     return;
   state->done |= DONE_APASS;
@@ -2358,14 +2422,19 @@ mode_parse_apass(struct ParseState *state, int *flag_p)
   if (!state->mbuf)
     return;
 
-  /* can't add a apass if one is set, nor can one remove the wrong apass */
-  if (!(state->flags & MODE_PARSE_FORCE))
+  if (!(state->flags & MODE_PARSE_FORCE)) {
+    /* can't remove the apass while upass is still set */
+    if (state->dir == MODE_DEL && *state->chptr->mode.upass) {
+      send_reply(state->sptr, ERR_UPASSSET, state->chptr->chname, state->chptr->chname);
+      return;
+    }
+    /* can't add an apass if one is set, nor can one remove the wrong apass */
     if ((state->dir == MODE_ADD && *state->chptr->mode.apass) ||
-       (state->dir == MODE_DEL &&
-        ircd_strcmp(state->chptr->mode.apass, t_str))) {
+       (state->dir == MODE_DEL && ircd_strcmp(state->chptr->mode.apass, t_str))) {
       send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
       return;
     }
+  }
 
   if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
       !ircd_strcmp(state->chptr->mode.apass, t_str))
@@ -2381,10 +2450,30 @@ mode_parse_apass(struct ParseState *state, int *flag_p)
     modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
 
   if (state->flags & MODE_PARSE_SET) {
-    if (state->dir == MODE_ADD) /* set the new apass */
+    if (state->dir == MODE_ADD) { /* set the new apass */
+      /* Make it VERY clear to the user that this is a one-time password */
       ircd_strncpy(state->chptr->mode.apass, t_str, PASSLEN);
-    else /* remove the old apass */
+      if (MyUser(state->sptr)) {
+       send_reply(state->sptr, RPL_APASSWARN,
+           "Channel Admin password (+A) set to '", state->chptr->mode.apass, "'. ",
+           "Are you SURE you want to use this as Admin password? ",
+           "You will NOT be able to change this password anymore once the channel is more than 48 hours old!");
+       send_reply(state->sptr, RPL_APASSWARN,
+           "Use \"/MODE ", state->chptr->chname, " -A ", state->chptr->mode.apass,
+           "\" to remove the password and then immediately set a new one. "
+           "IMPORTANT: YOU CANNOT RECOVER THIS PASSWORD, EVER; "
+           "WRITE THE PASSWORD DOWN (don't store this rescue password on disk)! "
+           "Now set the channel user password (+u).");
+      }
+    } else { /* remove the old apass */
       *state->chptr->mode.apass = '\0';
+      if (MyUser(state->sptr))
+       send_reply(state->sptr, RPL_APASSWARN,
+           "WARNING: You removed the channel Admin password MODE (+A). ",
+           "If you would disconnect or leave the channel without setting a new password then you will ",
+           "not be able to set it again and lose ownership of this channel! ",
+           "SET A NEW PASSWORD NOW!", "");
+    }
   }
 }
 
@@ -2495,7 +2584,8 @@ mode_parse_ban(struct ParseState *state, int *flag_p)
       } else if (!mmatch(t_str, ban->value.ban.banstr))
        ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
 
-      if (!ban->next && (newban->flags & MODE_ADD)) {
+      if (!ban->next && (newban->flags & MODE_ADD))
+      {
        ban->next = newban; /* add our ban with its flags */
        break; /* get out of loop */
       }
@@ -2571,7 +2661,7 @@ mode_process_bans(struct ParseState *state)
       } else {
        if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
            (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
-            count >= feature_int(FEAT_MAXBANS))) {
+            count > feature_int(FEAT_MAXBANS))) {
          send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
                     ban->value.ban.banstr);
          count--;
@@ -2703,7 +2793,8 @@ mode_process_clients(struct ParseState *state)
       if (MyUser(state->sptr)) {
 
        /* don't allow local opers to be deopped on local channels */
-       if (state->cli_change[i].client != state->sptr &&
+       if (MyUser(state->sptr) &&
+            state->cli_change[i].client != state->sptr &&
            IsLocalChannel(state->chptr->chname) &&
            HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
          send_reply(state->sptr, ERR_ISOPERLCHAN,
@@ -2712,6 +2803,7 @@ mode_process_clients(struct ParseState *state)
          continue;
         }
 
+        if (feature_bool(FEAT_OPLEVELS)) {
        /* don't allow to deop members with an op level that is <= our own level */
        if (state->sptr != state->cli_change[i].client          /* but allow to deop oneself */
                && state->member
@@ -2726,12 +2818,18 @@ mode_process_clients(struct ParseState *state)
        }
       }
     }
+    }
 
     /* set op-level of member being opped */
     if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
        (MODE_ADD | MODE_CHANOP)) {
-      int old_level = (state->member == NULL) ? -1 : OpLevel(state->member);
-      SetOpLevel(member, old_level == MAXOPLEVEL ? MAXOPLEVEL : (old_level + 1));
+      /* If on a channel with upass set, someone with level x gives ops to someone else,
+         then that person gets level x-1.  On other channels, where upass is not set,
+        the level stays the same. */
+      int level_increment = *state->chptr->mode.upass ? 1 : 0;
+      /* Someone being opped by a server gets op-level 0 */
+      int old_level = (state->member == NULL) ? -level_increment : OpLevel(state->member);
+      SetOpLevel(member, old_level == MAXOPLEVEL ? MAXOPLEVEL : (old_level + level_increment));
     }
 
     /* accumulate the change */
@@ -2885,10 +2983,12 @@ mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
        break;
 
       case 'A': /* deal with Admin passes */
+        if (feature_bool(FEAT_OPLEVELS))
        mode_parse_apass(&state, flag_p);
        break;
 
       case 'u': /* deal with user passes */
+        if (feature_bool(FEAT_OPLEVELS))
        mode_parse_upass(&state, flag_p);
        break;
 
@@ -3026,6 +3126,7 @@ void
 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
 {
   unsigned int len;
+  int is_local;
 
   assert(0 != jbuf);
 
@@ -3036,8 +3137,15 @@ joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
     return;
   }
 
+  is_local = IsLocalChannel(chan->chname);
+
   if (jbuf->jb_type == JOINBUF_TYPE_PART ||
       jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
+    struct Membership *member = find_member_link(chan, jbuf->jb_source);
+    if (IsUserParting(member))
+      return;
+    SetUserParting(member);
+
     /* Send notification to channel */
     if (!(flags & CHFL_ZOMBIE))
       sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL,
@@ -3055,14 +3163,14 @@ joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
      * the original m_part.c */
 
     if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
-       IsLocalChannel(chan->chname)) /* got to remove user here */
+       is_local) /* 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, 0);
 
     /* send notification to all servers */
-    if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !IsLocalChannel(chan->chname))
+    if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local)
       sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
                            "%H %Tu", chan, chan->creationtime);
 
@@ -3070,13 +3178,13 @@ joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
     sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, ":%H", chan);
 
     /* send an op, too, if needed */
-    if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE &&
-       !IsModelessChannel(chan->chname))
+    if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE)
       sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_MODE, chan, NULL, "%H +o %C",
                                chan, jbuf->jb_source);
   }
 
-  if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || IsLocalChannel(chan->chname))
+  if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
+      jbuf->jb_type == JOINBUF_TYPE_JOIN || is_local)
     return; /* don't send to remote */
 
   /* figure out if channel name will cause buffer to be overflowed */
@@ -3139,3 +3247,14 @@ joinbuf_flush(struct JoinBuf *jbuf)
 
   return 0;
 }
+
+/* Returns TRUE (1) if client is invited, FALSE (0) if not */
+int IsInvited(struct Client* cptr, const void* chptr)
+{
+  struct SLink *lp;
+
+  for (lp = (cli_user(cptr))->invited; lp; lp = lp->next)
+    if (lp->value.chptr == chptr)
+      return 1;
+  return 0;
+}