Fix ability to kick and deop users on -A channels when OPLEVELS enabled.
[ircu2.10.12-pk.git] / ircd / channel.c
index b335f93bdfff3f21704af597e9b8d4d258ea43a0..4821c4eb878e37603c8a74ca2a799b5ab39c15f5 100644 (file)
@@ -18,7 +18,7 @@
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 /** @file
- * @brief Channel management and maintanance
+ * @brief Channel management and maintenance
  * @version $Id$
  */
 #include "config.h"
@@ -198,7 +198,7 @@ struct Membership* find_member_link(struct Channel* chptr, const struct Client*
  * numeric nicks is no longer quite as important.
  *
  * @param sptr Pointer to the client that has requested the search
- * @param user a string represeting the client to be found
+ * @param user a string representing the client to be found
  * @param chasing a variable set to 0 if the user was found directly, 
  *             1 otherwise
  * @returns a pointer the client, or NULL if the client wasn't found.
@@ -259,7 +259,7 @@ int sub1_from_channel(struct Channel* chptr)
    * who then will educate them on the use of Apass/upass.
    */
 
-  if (!(chptr->mode.mode & MODE_APASS))         /* If no Apass, destroy now. */
+  if (!chptr->mode.apass[0])         /* If no Apass, destroy now. */
     destruct_channel(chptr);
   else if (TStime() - chptr->creationtime < 172800)    /* Channel younger than 48 hours? */
     schedule_destruct_event_1m(chptr);         /* Get rid of it in approximately 4-5 minutes */
@@ -329,7 +329,7 @@ struct Membership* find_channel_member(struct Client* cptr, struct Channel* chpt
   return (member && !IsZombie(member)) ? member : 0;
 }
 
-/** Searches for a ban from a banlist that matches a user.
+/** Searches for a ban from a ban list that matches a user.
  * @param[in] cptr The client to test.
  * @param[in] banlist The list of bans to test.
  * @return Pointer to a matching ban, or NULL if none exit.
@@ -519,7 +519,7 @@ static int channel_all_zombies(struct Channel* chptr)
 
 /** Remove a user from a channel
  * This is the generic entry point for removing a user from a channel, this
- * function will remove the client from the channel, and destory the channel
+ * function will remove the client from the channel, and destroy the channel
  * if there are no more normal users left.
  *
  * @param cptr         The client
@@ -650,7 +650,7 @@ int member_can_send_to_channel(struct Membership* member, int reveal)
   if (IsVoicedOrOpped(member))
     return 1;
   /*
-   * If it's moderated, and you aren't a priviledged user, you can't
+   * If it's moderated, and you aren't a privileged user, you can't
    * speak.  
    */
   if (member->channel->mode.mode & MODE_MODERATED)
@@ -860,7 +860,7 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
   int                 opped_members_index = 0;
   struct Membership** opped_members = NULL;
   int                 last_oplevel = 0;
-  int                 feat_oplevels = (chptr->mode.mode & MODE_APASS) != 0;
+  int                 feat_oplevels = (chptr->mode.apass[0]) != '\0';
 
   assert(0 != cptr);
   assert(0 != chptr); 
@@ -895,7 +895,7 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
     }
 
     /*
-     * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
+     * Attach nicks, comma separated " nick[:modes],nick[:modes],..."
      *
      * First find all opless members.
      * Run 2 times over all members, to group the members with
@@ -939,7 +939,7 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
           * Do we have a nick with a new mode ?
           * Or are we starting a new BURST line?
           */
-         if (new_mode || !feat_oplevels)
+         if (new_mode)
          {
            /*
             * This means we are at the _first_ member that has only
@@ -967,7 +967,7 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
            msgq_append(&me, mb, tbuf);
            new_mode = 0;
          }
-         else if (flag_cnt > 1 && last_oplevel != member->oplevel)
+         else if (feat_oplevels && flag_cnt > 1 && last_oplevel != member->oplevel)
          {
            /*
             * This can't be the first member of a (continued) BURST
@@ -1015,7 +1015,7 @@ void send_channel_modes(struct Client *cptr, struct Channel *chptr)
 
     if (!full)
     {
-      /* Attach all bans, space seperated " :%ban ban ..." */
+      /* Attach all bans, space separated " :%ban ban ..." */
       for (first = 2; lp2; lp2 = lp2->next)
       {
         len = strlen(lp2->banstr);
@@ -1172,7 +1172,7 @@ static void send_ban_list(struct Client* cptr, struct Channel* chptr)
  * This version contributed by SeKs \<intru@info.polymtl.ca\>
  *
  * @param key          Key to check
- * @param keyring      Comma seperated list of keys
+ * @param keyring      Comma separated list of keys
  *
  * @returns True if the key was found and matches, false otherwise.
  */
@@ -1211,8 +1211,8 @@ top:
  * @param chptr        The channel to join
  * @param key  The key to use
  *
- * @returns any error that occured bitwised OR'd with MAGIC_OPER_OVERRIDE
- *         if the oper used the magic key, 0 if no error occured.
+ * @returns any error that occurred bit-wise OR'd with MAGIC_OPER_OVERRIDE
+ *         if the oper used the magic key, 0 if no error occurred.
  */
 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
 {
@@ -1399,7 +1399,15 @@ void del_invite(struct Client *cptr, struct Channel *chptr)
     }
 }
 
-/** @page zombie Explaination of Zombies
+/** @page zombie Explanation of Zombies
+ *
+ * Synopsis:
+ *
+ * A channel member is turned into a zombie when he is kicked from a
+ * channel but his server has not acknowledged the kick.  Servers that
+ * see the member as a zombie can accept actions he performed before
+ * being kicked, without allowing chanop operations from outsiders or
+ * desyncing the network.
  *
  * Consider:
  * <pre>
@@ -1442,7 +1450,7 @@ void del_invite(struct Client *cptr, struct Channel *chptr)
  *
  * We also need to turn 'who' into a zombie on servers 1 and 6,
  * because a KICK from 'who' (kicking someone else in that direction)
- * can arrive there afterwards - which should not be bounced itself.
+ * can arrive there afterward - which should not be bounced itself.
  * Therefore case a) also applies for servers 1 and 6.
  *
  * --Run
@@ -1522,9 +1530,9 @@ int number_of_zombies(struct Channel *chptr)
  *
  * @param strptr       The buffer to concatenate into
  * @param strptr_i     modified offset to the position to modify
- * @param str1         The string to contatenate from.
+ * @param str1         The string to concatenate from.
  * @param str2         The second string to contatenate from.
- * @param c            Charactor to seperate the string from str1 and str2.
+ * @param c            Charactor to separate the string from str1 and str2.
  */
 static void
 build_string(char *strptr, int *strptr_i, const char *str1,
@@ -2428,9 +2436,10 @@ mode_parse_upass(struct ParseState *state, int *flag_p)
   }
 
   /* If a non-service user is trying to force it, refuse. */
-  if (state->flags & MODE_PARSE_FORCE && !IsChannelService(state->sptr)) {
+  if (state->flags & MODE_PARSE_FORCE && MyUser(state->sptr)
+      && !HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
     send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
-               "Use /JOIN", state->chptr->chname, " <AdminPass>.");
+               state->chptr->chname);
     return;
   }
 
@@ -2438,16 +2447,15 @@ mode_parse_upass(struct ParseState *state, int *flag_p)
   if (MyUser(state->sptr) && !(state->flags & MODE_PARSE_FORCE || IsChannelManager(state->member))) {
     if (*state->chptr->mode.apass) {
       send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
-         "Use /JOIN", state->chptr->chname, "<AdminPass>.");
+                 state->chptr->chname);
+    } else if (TStime() - state->chptr->creationtime >= 171000) {
+      send_reply(state->sptr, ERR_NOMANAGER_LONG, state->chptr->chname);
     } 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.");
+      send_reply(state->sptr, ERR_NOMANAGER_SHORT, state->chptr->chname);
     }
     return;
   }
+
   if (state->done & DONE_UPASS) /* allow upass to be set only once */
     return;
   state->done |= DONE_UPASS;
@@ -2470,7 +2478,7 @@ mode_parse_upass(struct ParseState *state, int *flag_p)
   if (!state->mbuf)
     return;
 
-  if (!(state->flags & MODE_PARSE_FORCE))
+  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);
@@ -2483,6 +2491,7 @@ mode_parse_upass(struct ParseState *state, int *flag_p)
       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.upass, t_str))
@@ -2535,9 +2544,10 @@ mode_parse_apass(struct ParseState *state, int *flag_p)
   }
 
   /* If a non-service user is trying to force it, refuse. */
-  if (state->flags & MODE_PARSE_FORCE && !IsChannelService(state->sptr)) {
+  if (state->flags & MODE_PARSE_FORCE && MyUser(state->sptr)
+      && !HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
     send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
-               "Use /JOIN", state->chptr->chname, " <AdminPass>.");
+               state->chptr->chname);
     return;
   }
 
@@ -2551,15 +2561,15 @@ mode_parse_apass(struct ParseState *state, int *flag_p)
   if (MyUser(state->sptr) && !(state->flags & MODE_PARSE_FORCE || IsChannelManager(state->member))) {
     if (*state->chptr->mode.apass) {
       send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
-         "Use /JOIN", state->chptr->chname, "<AdminPass>.");
+                 state->chptr->chname);
+    } else if (TStime() - state->chptr->creationtime >= 171000) {
+      send_reply(state->sptr, ERR_NOMANAGER_LONG, state->chptr->chname);
     } 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.");
+      send_reply(state->sptr, ERR_NOMANAGER_SHORT, state->chptr->chname);
     }
     return;
   }
+
   if (state->done & DONE_APASS) /* allow apass to be set only once */
     return;
   state->done |= DONE_APASS;
@@ -2614,25 +2624,14 @@ mode_parse_apass(struct ParseState *state, int *flag_p)
       /* Make it VERY clear to the user that this is a one-time password */
       ircd_strncpy(state->chptr->mode.apass, t_str, PASSLEN);
       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).");
+       send_reply(state->sptr, RPL_APASSWARN_SET, state->chptr->mode.apass);
+       send_reply(state->sptr, RPL_APASSWARN_SECRET, state->chptr->chname,
+                   state->chptr->mode.apass);
       }
     } 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!", "");
+        send_reply(state->sptr, RPL_APASSWARN_CLEAR);
     }
   }
 }
@@ -2681,9 +2680,10 @@ bmatch(struct Ban *old_ban, struct Ban *new_ban)
  *
  * @param[in,out] banlist Pointer to head of list.
  * @param[in] newban Ban (or exception) to add (or remove).
+ * @param[in] do_free If non-zero, free \a newban on failure.
  * @return Zero if \a newban could be applied, non-zero if not.
  */
-int apply_ban(struct Ban **banlist, struct Ban *newban, int free)
+int apply_ban(struct Ban **banlist, struct Ban *newban, int do_free)
 {
   struct Ban *ban;
   size_t count = 0;
@@ -2694,7 +2694,7 @@ int apply_ban(struct Ban **banlist, struct Ban *newban, int free)
     /* If a less specific entry is found, fail.  */
     for (ban = *banlist; ban; ban = ban->next) {
       if (!bmatch(ban, newban)) {
-        if (free)
+        if (do_free)
           free_ban(newban);
         return 1;
       }
@@ -2721,14 +2721,17 @@ int apply_ban(struct Ban **banlist, struct Ban *newban, int free)
         remove_count++;
       }
     }
-    if (free)
+    if (remove_count)
+        return 0;
+    /* If no matches were found, fail. */
+    if (do_free)
       free_ban(newban);
     else
       MyFree(newban->banstr);
-    /* If no matches were found, fail. */
-    return remove_count ? 0 : 3;
+    return 3;
   }
-  free_ban(newban);
+  if (do_free)
+    free_ban(newban);
   return 4;
 }
 
@@ -2996,11 +2999,11 @@ 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
-               && OpLevel(member) <= OpLevel(state->member)) {
+            && state->chptr->mode.apass[0]
+            && state->member
+            && OpLevel(member) <= OpLevel(state->member)) {
            int equal = (OpLevel(member) == OpLevel(state->member));
            send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
                       cli_name(state->cli_change[i].client),
@@ -3009,20 +3012,24 @@ mode_process_clients(struct ParseState *state)
                       "deop", equal ? "the same" : "a higher");
          continue;
        }
-      }
     }
     }
 
     /* set op-level of member being opped */
     if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
        (MODE_ADD | MODE_CHANOP)) {
-      /* 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));
+      /* If being opped by an outsider, get oplevel 0 for an apass
+       *   channel, else MAXOPLEVEL.
+       * Otherwise, if not an apass channel, or state->member has
+       *   MAXOPLEVEL, get oplevel MAXOPLEVEL.
+       * Otherwise, get state->member's oplevel+1.
+       */
+      if (!state->member)
+        SetOpLevel(member, state->chptr->mode.apass[0] ? 0 : MAXOPLEVEL);
+      else if (!state->chptr->mode.apass[0] || OpLevel(state->member) == MAXOPLEVEL)
+        SetOpLevel(member, MAXOPLEVEL);
+      else
+        SetOpLevel(member, OpLevel(state->member) + 1);
     }
 
     /* actually effect the change */
@@ -3365,11 +3372,12 @@ joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
        is_local) /* got to remove user here */
       remove_user_from_channel(jbuf->jb_source, chan);
   } else {
+    int oplevel = chan->mode.apass[0] ? 0 : MAXOPLEVEL;
     /* Add user to channel */
     if ((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))
-      add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, 0);
+      add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, oplevel);
     else
-      add_user_to_channel(chan, jbuf->jb_source, flags, 0);
+      add_user_to_channel(chan, jbuf->jb_source, flags, oplevel);
 
     /* send notification to all servers */
     if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local)