]> jfr.im git - solanum.git/blobdiff - src/chmode.c
Remove s_assert definition from ircd_defs.h and add it to its own header.
[solanum.git] / src / chmode.c
index aae52b880e81585964e27170aa7ed65910f01d8f..9dd562be10037a861d792e2a95af5df8af8b6649 100644 (file)
@@ -42,6 +42,7 @@
 #include "s_newconf.h"
 #include "logger.h"
 #include "chmode.h"
+#include "s_assert.h"
 
 /* bitmasks for error returns, so we send once per call */
 #define SM_ERR_NOTS             0x00000001     /* No TS on channel */
@@ -65,6 +66,7 @@ static int mode_count;
 static int mode_limit;
 static int mode_limit_simple;
 static int mask_pos;
+static int removed_mask_pos;
 
 char cflagsbuf[256];
 char cflagsmyinfo[256];
@@ -103,20 +105,10 @@ construct_cflags_strings(void)
                 
                switch (chmode_flags[i])
                {
-                   case MODE_EXLIMIT:
+                   case MODE_FREETARGET:
                    case MODE_DISFORWARD:
                        if(ConfigChannel.use_forward)
-                       {
-                           *ptr++ = (char) i;
-                       }
-                       
-                       break;
-                   case MODE_REGONLY:
-                       if(rb_dlink_list_length(&service_list))
-                       {
-                           *ptr++ = (char) i;
-                       }
-
+                               *ptr++ = (char) i;
                        break;
                    default:
                        if(chmode_flags[i] != 0)
@@ -187,7 +179,7 @@ cflag_orphan(char c_)
 }
 
 int
-get_channel_access(struct Client *source_p, struct membership *msptr, int role)
+get_channel_access(struct Client *source_p, struct membership *msptr)
 {
        hook_data_channel_approval moduledata;
 
@@ -201,26 +193,57 @@ get_channel_access(struct Client *source_p, struct membership *msptr, int role)
        moduledata.chptr = msptr->chptr;
        moduledata.msptr = msptr;
        moduledata.target = NULL;
-
-       /* Check if they have the proper role */
-       if(HasChanRole(msptr, role))
-               moduledata.approved = CHFL_CHANOP;
-       else
-               moduledata.approved = CHFL_PEON;
+       moduledata.approved = is_chanop(msptr) ? CHFL_CHANOP : CHFL_PEON;
 
        call_hook(h_get_channel_access, &moduledata);
 
        return moduledata.approved;
 }
 
+/* allow_mode_change()
+ *
+ * Checks if mlock and chanops permit a mode change.
+ *
+ * inputs      - client, channel, access level, errors pointer, mode char
+ * outputs     - 0 on failure, 1 on success
+ * side effects - error message sent on failure
+ */
+static int
+allow_mode_change(struct Client *source_p, struct Channel *chptr, int alevel,
+               int *errors, char c)
+{
+       /* If this mode char is locked, don't allow local users to change it. */
+       if (MyClient(source_p) && chptr->mode_lock && strchr(chptr->mode_lock, c))
+       {
+               if (!(*errors & SM_ERR_MLOCK))
+                       sendto_one_numeric(source_p,
+                                       ERR_MLOCKRESTRICTED,
+                                       form_str(ERR_MLOCKRESTRICTED),
+                                       chptr->chname,
+                                       c,
+                                       chptr->mode_lock);
+               *errors |= SM_ERR_MLOCK;
+               return 0;
+       }
+       if(alevel != CHFL_CHANOP)
+       {
+               if(!(*errors & SM_ERR_NOOPS))
+                       sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
+                                  me.name, source_p->name, chptr->chname);
+               *errors |= SM_ERR_NOOPS;
+               return 0;
+       }
+       return 1;
+}
+
 /* add_id()
  *
- * inputs      - client, channel, id to add, type
+ * inputs      - client, channel, id to add, type, forward
  * outputs     - 0 on failure, 1 on success
  * side effects - given id is added to the appropriate list
  */
 int
-add_id(struct Client *source_p, struct Channel *chptr, const char *banid,
+add_id(struct Client *source_p, struct Channel *chptr, const char *banid, const char *forward,
        rb_dlink_list * list, long mode_type)
 {
        struct Ban *actualBan;
@@ -264,7 +287,7 @@ add_id(struct Client *source_p, struct Channel *chptr, const char *banid,
        else
                rb_strlcpy(who, source_p->name, sizeof(who));
 
-       actualBan = allocate_ban(realban, who);
+       actualBan = allocate_ban(realban, who, forward);
        actualBan->when = rb_current_time();
 
        rb_dlinkAdd(actualBan, &actualBan->node, list);
@@ -279,17 +302,17 @@ add_id(struct Client *source_p, struct Channel *chptr, const char *banid,
 /* del_id()
  *
  * inputs      - channel, id to remove, type
- * outputs     - 0 on failure, 1 on success
- * side effects - given id is removed from the appropriate list
+ * outputs     - pointer to ban that was removed, if any
+ * side effects - given id is removed from the appropriate list and returned
  */
-int
+struct Ban *
 del_id(struct Channel *chptr, const char *banid, rb_dlink_list * list, long mode_type)
 {
        rb_dlink_node *ptr;
        struct Ban *banptr;
 
        if(EmptyString(banid))
-               return 0;
+               return NULL;
 
        RB_DLINK_FOREACH(ptr, list->head)
        {
@@ -298,17 +321,16 @@ del_id(struct Channel *chptr, const char *banid, rb_dlink_list * list, long mode
                if(irccmp(banid, banptr->banstr) == 0)
                {
                        rb_dlinkDelete(&banptr->node, list);
-                       free_ban(banptr);
 
                        /* invalidate the can_send() cache */
                        if(mode_type == CHFL_BAN || mode_type == CHFL_QUIET || mode_type == CHFL_EXCEPTION)
                                chptr->bants++;
 
-                       return 1;
+                       return banptr;
                }
        }
 
-       return 0;
+       return NULL;
 }
 
 /* check_string()
@@ -352,10 +374,11 @@ pretty_mask(const char *idmask)
 {
        static char mask_buf[BUFSIZE];
        int old_mask_pos;
-       char *nick, *user, *host;
+       char *nick, *user, *host, *forward = NULL;
        char splat[] = "*";
-       char *t, *at, *ex;
-       char ne = 0, ue = 0, he = 0;    /* save values at nick[NICKLEN], et all */
+       char *t, *at, *ex, *ex2;
+       char ne = 0, ue = 0, he = 0, fe = 0;    /* save values at nick[NICKLEN], et all */
+       char e2 = 0;                            /* save value that delimits forward channel */
        char *mask;
 
        mask = LOCAL_COPY(idmask);
@@ -381,7 +404,7 @@ pretty_mask(const char *idmask)
                return mask_buf + old_mask_pos;
        }
 
-       at = ex = NULL;
+       at = ex = ex2 = NULL;
        if((t = strchr(mask, '@')) != NULL)
        {
                at = t;
@@ -403,6 +426,15 @@ pretty_mask(const char *idmask)
                        if(*mask != '\0')
                                user = mask;
                }
+
+               if((t = strchr(host, '!')) != NULL || (t = strchr(host, '$')) != NULL)
+               {
+                       ex2 = t;
+                       e2 = *t;
+                       *t++= '\0';
+                       if (*t != '\0')
+                               forward = t;
+               }
        }
        else if((t = strchr(mask, '!')) != NULL)
        {
@@ -413,7 +445,7 @@ pretty_mask(const char *idmask)
                if(*t != '\0')
                        user = t;
        }
-       else if(strchr(mask, '.') != NULL || strchr(mask, ':') != NULL || strchr(mask, '/') != NULL)
+       else if(strchr(mask, '.') != NULL || strchr(mask, ':') != NULL)
        {
                if(*mask != '\0')
                        host = mask;
@@ -440,24 +472,81 @@ pretty_mask(const char *idmask)
                he = host[HOSTLEN];
                host[HOSTLEN] = '\0';
        }
+       if(forward && strlen(forward) > CHANNELLEN)
+       {
+               fe = forward[CHANNELLEN];
+               forward[CHANNELLEN] = '\0';
+       }
 
-       mask_pos += rb_sprintf(mask_buf + mask_pos, "%s!%s@%s", nick, user, host) + 1;
+       if (forward)
+               mask_pos += rb_sprintf(mask_buf + mask_pos, "%s!%s@%s$%s", nick, user, host, forward) + 1;
+       else
+               mask_pos += rb_sprintf(mask_buf + mask_pos, "%s!%s@%s", nick, user, host) + 1;
 
        /* restore mask, since we may need to use it again later */
        if(at)
                *at = '@';
        if(ex)
                *ex = '!';
+       if(ex2)
+               *ex2 = e2;
        if(ne)
                nick[NICKLEN - 1] = ne;
        if(ue)
                user[USERLEN] = ue;
        if(he)
                host[HOSTLEN] = he;
+       if(fe)
+               forward[CHANNELLEN] = fe;
 
        return mask_buf + old_mask_pos;
 }
 
+/* check_forward()
+ *
+ * input       - client, channel to set mode on, target channel name
+ * output      - true if forwarding should be allowed
+ * side effects - numeric sent if not allowed
+ */
+static int
+check_forward(struct Client *source_p, struct Channel *chptr,
+               const char *forward)
+{
+       struct Channel *targptr;
+       struct membership *msptr;
+
+       if(!check_channel_name(forward) ||
+                       (MyClient(source_p) && (strlen(forward) > LOC_CHANNELLEN || hash_find_resv(forward))))
+       {
+               sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), forward);
+               return 0;
+       }
+       /* don't forward to inconsistent target -- jilles */
+       if(chptr->chname[0] == '#' && forward[0] == '&')
+       {
+               sendto_one_numeric(source_p, ERR_BADCHANNAME,
+                                  form_str(ERR_BADCHANNAME), forward);
+               return 0;
+       }
+       if(MyClient(source_p) && (targptr = find_channel(forward)) == NULL)
+       {
+               sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
+                                  form_str(ERR_NOSUCHCHANNEL), forward);
+               return 0;
+       }
+       if(MyClient(source_p) && !(targptr->mode.mode & MODE_FREETARGET))
+       {
+               if((msptr = find_channel_membership(targptr, source_p)) == NULL ||
+                       get_channel_access(source_p, msptr) != CHFL_CHANOP)
+               {
+                       sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
+                                  me.name, source_p->name, targptr->chname);
+                       return 0;
+               }
+       }
+       return 1;
+}
+
 /* fix_key()
  *
  * input       - key to fix
@@ -524,14 +613,8 @@ chm_simple(struct Client *source_p, struct Channel *chptr,
           int alevel, int parc, int *parn,
           const char **parv, int *errors, int dir, char c, long mode_type)
 {
-       if(alevel != CHFL_CHANOP)
-       {
-               if(!(*errors & SM_ERR_NOOPS))
-                       sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
-                                  me.name, source_p->name, chptr->chname);
-               *errors |= SM_ERR_NOOPS;
+       if(!allow_mode_change(source_p, chptr, alevel, errors, c))
                return;
-       }
 
        if(MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
                return;
@@ -541,7 +624,7 @@ chm_simple(struct Client *source_p, struct Channel *chptr,
        {
                /* if +f is disabled, ignore an attempt to set +QF locally */
                if(!ConfigChannel.use_forward && MyClient(source_p) &&
-                  (c == 'Q' || c == 'F'))
+                               (c == 'Q' || c == 'F'))
                        return;
 
                chptr->mode.mode |= mode_type;
@@ -658,14 +741,13 @@ chm_ban(struct Client *source_p, struct Channel *chptr,
        int alevel, int parc, int *parn,
        const char **parv, int *errors, int dir, char c, long mode_type)
 {
-       const char *mask;
-       const char *raw_mask;
+       char *mask, *raw_mask, *forward;
        rb_dlink_list *list;
        rb_dlink_node *ptr;
        struct Ban *banptr;
        int errorval;
-       int rpl_list;
-       int rpl_endlist;
+       const char *rpl_list_p;
+       const char *rpl_endlist_p;
        int caps;
        int mems;
 
@@ -674,8 +756,8 @@ chm_ban(struct Client *source_p, struct Channel *chptr,
        case CHFL_BAN:
                list = &chptr->banlist;
                errorval = SM_ERR_RPL_B;
-               rpl_list = RPL_BANLIST;
-               rpl_endlist = RPL_ENDOFBANLIST;
+               rpl_list_p = form_str(RPL_BANLIST);
+               rpl_endlist_p = form_str(RPL_ENDOFBANLIST);
                mems = ALL_MEMBERS;
                caps = 0;
                break;
@@ -688,8 +770,8 @@ chm_ban(struct Client *source_p, struct Channel *chptr,
 
                list = &chptr->exceptlist;
                errorval = SM_ERR_RPL_E;
-               rpl_list = RPL_EXCEPTLIST;
-               rpl_endlist = RPL_ENDOFEXCEPTLIST;
+               rpl_list_p = form_str(RPL_EXCEPTLIST);
+               rpl_endlist_p = form_str(RPL_ENDOFEXCEPTLIST);
                caps = CAP_EX;
 
                if(ConfigChannel.use_except || (dir == MODE_DEL))
@@ -706,8 +788,8 @@ chm_ban(struct Client *source_p, struct Channel *chptr,
 
                list = &chptr->invexlist;
                errorval = SM_ERR_RPL_I;
-               rpl_list = RPL_INVITELIST;
-               rpl_endlist = RPL_ENDOFINVITELIST;
+               rpl_list_p = form_str(RPL_INVITELIST);
+               rpl_endlist_p = form_str(RPL_ENDOFINVITELIST);
                caps = CAP_IE;
 
                if(ConfigChannel.use_invex || (dir == MODE_DEL))
@@ -719,8 +801,8 @@ chm_ban(struct Client *source_p, struct Channel *chptr,
        case CHFL_QUIET:
                list = &chptr->quietlist;
                errorval = SM_ERR_RPL_Q;
-               rpl_list = RPL_QUIETLIST;
-               rpl_endlist = RPL_ENDOFQUIETLIST;
+               rpl_list_p = form_str(RPL_QUIETLIST);
+               rpl_endlist_p = form_str(RPL_ENDOFQUIETLIST);
                mems = ALL_MEMBERS;
                caps = 0;
                break;
@@ -738,6 +820,7 @@ chm_ban(struct Client *source_p, struct Channel *chptr,
                *errors |= errorval;
 
                /* non-ops cant see +eI lists.. */
+               /* note that this is still permitted if +e/+I are mlocked. */
                if(alevel != CHFL_CHANOP && mode_type != CHFL_BAN &&
                                mode_type != CHFL_QUIET)
                {
@@ -750,23 +833,24 @@ chm_ban(struct Client *source_p, struct Channel *chptr,
 
                RB_DLINK_FOREACH(ptr, list->head)
                {
+                       char buf[BANLEN];
                        banptr = ptr->data;
-                       sendto_one(source_p, form_str(rpl_list),
+                       if(banptr->forward)
+                               rb_snprintf(buf, sizeof(buf), "%s$%s", banptr->banstr, banptr->forward);
+                       else
+                               rb_strlcpy(buf, banptr->banstr, sizeof(buf));
+
+                       sendto_one(source_p, rpl_list_p,
                                   me.name, source_p->name, chptr->chname,
-                                  banptr->banstr, banptr->who, banptr->when);
+                                  buf, banptr->who, banptr->when);
                }
-               sendto_one(source_p, form_str(rpl_endlist), me.name, source_p->name, chptr->chname);
+               sendto_one(source_p, rpl_endlist_p, me.name, source_p->name, chptr->chname);
                return;
        }
 
-       if(alevel != CHFL_CHANOP)
-       {
-               if(!(*errors & SM_ERR_NOOPS))
-                       sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
-                                  me.name, source_p->name, chptr->chname);
-               *errors |= SM_ERR_NOOPS;
+       if(!allow_mode_change(source_p, chptr, alevel, errors, c))
                return;
-       }
+
 
        if(MyClient(source_p) && (++mode_limit > MAXMODEPARAMS))
                return;
@@ -793,7 +877,23 @@ chm_ban(struct Client *source_p, struct Channel *chptr,
         * name etc.
         */
        if(strlen(mask) > IRCD_MIN(BANLEN, MODEBUFLEN - 5))
+       {
+               sendto_one_numeric(source_p, ERR_INVALIDBAN,
+                               form_str(ERR_INVALIDBAN),
+                               chptr->chname, c, raw_mask);
                return;
+       }
+
+       /* Look for a $ after the first character.
+        * As the first character, it marks an extban; afterwards
+        * it delimits a forward channel.
+        */
+       if ((forward = strchr(mask+1, '$')) != NULL)
+       {
+               *forward++ = '\0';
+               if (*forward == '\0')
+                       forward = NULL;
+       }
 
        /* if we're adding a NEW id */
        if(dir == MODE_ADD)
@@ -801,16 +901,52 @@ chm_ban(struct Client *source_p, struct Channel *chptr,
                if (*mask == '$' && MyClient(source_p))
                {
                        if (!valid_extban(mask, source_p, chptr, mode_type))
-                               /* XXX perhaps return an error message here */
+                       {
+                               sendto_one_numeric(source_p, ERR_INVALIDBAN,
+                                               form_str(ERR_INVALIDBAN),
+                                               chptr->chname, c, raw_mask);
                                return;
+                       }
+               }
+
+               /* For compatibility, only check the forward channel from
+                * local clients. Accept any forward channel from servers.
+                */
+               if(forward != NULL && MyClient(source_p))
+               {
+                       /* For simplicity and future flexibility, do not
+                        * allow '$' in forwarding targets.
+                        */
+                       if(!ConfigChannel.use_forward ||
+                                       strchr(forward, '$') != NULL)
+                       {
+                               sendto_one_numeric(source_p, ERR_INVALIDBAN,
+                                               form_str(ERR_INVALIDBAN),
+                                               chptr->chname, c, raw_mask);
+                               return;
+                       }
+                       /* check_forward() sends its own error message */
+                       if(!check_forward(source_p, chptr, forward))
+                               return;
+                       /* Forwards only make sense for bans. */
+                       if(mode_type != CHFL_BAN)
+                       {
+                               sendto_one_numeric(source_p, ERR_INVALIDBAN,
+                                               form_str(ERR_INVALIDBAN),
+                                               chptr->chname, c, raw_mask);
+                               return;
+                       }
                }
 
                /* dont allow local clients to overflow the banlist, dont
                 * let remote servers set duplicate bans
                 */
-               if(!add_id(source_p, chptr, mask, list, mode_type))
+               if(!add_id(source_p, chptr, mask, forward, list, mode_type))
                        return;
 
+               if(forward)
+                       forward[-1]= '$';
+
                mode_changes[mode_count].letter = c;
                mode_changes[mode_count].dir = MODE_ADD;
                mode_changes[mode_count].caps = caps;
@@ -821,20 +957,33 @@ chm_ban(struct Client *source_p, struct Channel *chptr,
        }
        else if(dir == MODE_DEL)
        {
-               if(del_id(chptr, mask, list, mode_type) == 0)
+               struct Ban *removed;
+               static char buf[BANLEN * MAXMODEPARAMS];
+               int old_removed_mask_pos = removed_mask_pos;
+               if((removed = del_id(chptr, mask, list, mode_type)) == NULL)
                {
                        /* mask isn't a valid ban, check raw_mask */
-                       if(del_id(chptr, raw_mask, list, mode_type))
+                       if((removed = del_id(chptr, raw_mask, list, mode_type)) != NULL)
                                mask = raw_mask;
                }
 
+               if(removed && removed->forward)
+                       removed_mask_pos += rb_snprintf(buf + old_removed_mask_pos, sizeof(buf), "%s$%s", removed->banstr, removed->forward) + 1;
+               else
+                       removed_mask_pos += rb_strlcpy(buf + old_removed_mask_pos, mask, sizeof(buf)) + 1;
+               if(removed)
+               {
+                       free_ban(removed);
+                       removed = NULL;
+               }
+
                mode_changes[mode_count].letter = c;
                mode_changes[mode_count].dir = MODE_DEL;
                mode_changes[mode_count].caps = caps;
                mode_changes[mode_count].nocaps = 0;
                mode_changes[mode_count].mems = mems;
                mode_changes[mode_count].id = NULL;
-               mode_changes[mode_count++].arg = mask;
+               mode_changes[mode_count++].arg = buf + old_removed_mask_pos;
        }
 }
 
@@ -847,14 +996,8 @@ chm_op(struct Client *source_p, struct Channel *chptr,
        const char *opnick;
        struct Client *targ_p;
 
-       if(alevel != CHFL_CHANOP)
-       {
-               if(!(*errors & SM_ERR_NOOPS))
-                       sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
-                                  me.name, source_p->name, chptr->chname);
-               *errors |= SM_ERR_NOOPS;
+       if(!allow_mode_change(source_p, chptr, alevel, errors, c))
                return;
-       }
 
        if((dir == MODE_QUERY) || (parc <= *parn))
                return;
@@ -903,11 +1046,6 @@ chm_op(struct Client *source_p, struct Channel *chptr,
                mode_changes[mode_count++].client = targ_p;
 
                mstptr->flags |= CHFL_CHANOP;
-               if (mstptr->roles & CHANROLE_UNSET)
-               {
-                       mstptr->roles &= ~CHANROLE_UNSET;
-                       mstptr->roles = CHANROLE_INITIAL | CHANROLE_INHERIT;
-               }
        }
        else
        {
@@ -928,8 +1066,6 @@ chm_op(struct Client *source_p, struct Channel *chptr,
                mode_changes[mode_count++].client = targ_p;
 
                mstptr->flags &= ~CHFL_CHANOP;
-               if (mstptr->roles & CHANROLE_INHERIT)
-                       mstptr->roles = CHANROLE_UNSET;
        }
 }
 
@@ -942,14 +1078,8 @@ chm_voice(struct Client *source_p, struct Channel *chptr,
        const char *opnick;
        struct Client *targ_p;
 
-       if(alevel != CHFL_CHANOP)
-       {
-               if(!(*errors & SM_ERR_NOOPS))
-                       sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
-                                  me.name, source_p->name, chptr->chname);
-               *errors |= SM_ERR_NOOPS;
+       if(!allow_mode_change(source_p, chptr, alevel, errors, c))
                return;
-       }
 
        if((dir == MODE_QUERY) || parc <= *parn)
                return;
@@ -1020,14 +1150,8 @@ chm_limit(struct Client *source_p, struct Channel *chptr,
        static char limitstr[30];
        int limit;
 
-       if(alevel != CHFL_CHANOP)
-       {
-               if(!(*errors & SM_ERR_NOOPS))
-                       sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
-                                  me.name, source_p->name, chptr->chname);
-               *errors |= SM_ERR_NOOPS;
+       if(!allow_mode_change(source_p, chptr, alevel, errors, c))
                return;
-       }
 
        if(dir == MODE_QUERY)
                return;
@@ -1079,14 +1203,8 @@ chm_throttle(struct Client *source_p, struct Channel *chptr,
 {
        int joins = 0, timeslice = 0;
 
-       if(alevel != CHFL_CHANOP)
-       {
-               if(!(*errors & SM_ERR_NOOPS))
-                       sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
-                                  me.name, source_p->name, chptr->chname);
-               *errors |= SM_ERR_NOOPS;
+       if(!allow_mode_change(source_p, chptr, alevel, errors, c))
                return;
-       }
 
        if(dir == MODE_QUERY)
                return;
@@ -1139,8 +1257,6 @@ chm_forward(struct Client *source_p, struct Channel *chptr,
        int alevel, int parc, int *parn,
        const char **parv, int *errors, int dir, char c, long mode_type)
 {
-       struct Channel *targptr = NULL;
-       struct membership *msptr;
        const char *forward;
 
        /* if +f is disabled, ignore local attempts to set it */
@@ -1162,14 +1278,8 @@ chm_forward(struct Client *source_p, struct Channel *chptr,
        }
 
 #ifndef FORWARD_OPERONLY
-       if(alevel != CHFL_CHANOP)
-       {
-               if(!(*errors & SM_ERR_NOOPS))
-                       sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
-                                  me.name, source_p->name, chptr->chname);
-               *errors |= SM_ERR_NOOPS;
+       if(!allow_mode_change(source_p, chptr, alevel, errors, c))
                return;
-       }
 #else
        if(!IsOper(source_p) && !IsServer(source_p))
        {
@@ -1190,35 +1300,9 @@ chm_forward(struct Client *source_p, struct Channel *chptr,
 
                if(EmptyString(forward))
                        return;
-               if(!check_channel_name(forward) ||
-                               (MyClient(source_p) && (strlen(forward) > LOC_CHANNELLEN || hash_find_resv(forward))))
-               {
-                       sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), forward);
-                       return;
-               }
-               /* don't forward to inconsistent target -- jilles */
-               if(chptr->chname[0] == '#' && forward[0] == '&')
-               {
-                       sendto_one_numeric(source_p, ERR_BADCHANNAME,
-                                          form_str(ERR_BADCHANNAME), forward);
-                       return;
-               }
-               if(MyClient(source_p) && (targptr = find_channel(forward)) == NULL)
-               {
-                       sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
-                                          form_str(ERR_NOSUCHCHANNEL), forward);
+
+               if(!check_forward(source_p, chptr, forward))
                        return;
-               }
-               if(MyClient(source_p) && !(targptr->mode.mode & MODE_FREETARGET))
-               {
-                       if((msptr = find_channel_membership(targptr, source_p)) == NULL ||
-                               get_channel_access(source_p, msptr, CHANROLE_MODE) != CHFL_CHANOP)
-                       {
-                               sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
-                                          me.name, source_p->name, targptr->chname);
-                               return;
-                       }
-               }
 
                rb_strlcpy(chptr->mode.forward, forward, sizeof(chptr->mode.forward));
 
@@ -1226,7 +1310,8 @@ chm_forward(struct Client *source_p, struct Channel *chptr,
                mode_changes[mode_count].dir = MODE_ADD;
                mode_changes[mode_count].caps = 0;
                mode_changes[mode_count].nocaps = 0;
-               mode_changes[mode_count].mems = ConfigChannel.use_forward ? ALL_MEMBERS : ONLY_SERVERS;
+               mode_changes[mode_count].mems =
+                       ConfigChannel.use_forward ? ALL_MEMBERS : ONLY_SERVERS;
                mode_changes[mode_count].id = NULL;
                mode_changes[mode_count++].arg = forward;
        }
@@ -1254,14 +1339,8 @@ chm_key(struct Client *source_p, struct Channel *chptr,
 {
        char *key;
 
-       if(alevel != CHFL_CHANOP)
-       {
-               if(!(*errors & SM_ERR_NOOPS))
-                       sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
-                                  me.name, source_p->name, chptr->chname);
-               *errors |= SM_ERR_NOOPS;
+       if(!allow_mode_change(source_p, chptr, alevel, errors, c))
                return;
-       }
 
        if(dir == MODE_QUERY)
                return;
@@ -1397,7 +1476,7 @@ struct ChannelMode chmode_table[256] =
   {chm_nosuch, 0 },                    /* @ */
   {chm_nosuch, 0 },                    /* A */
   {chm_nosuch, 0 },                    /* B */
-  {chm_simple, MODE_NOCTCP },          /* C */
+  {chm_nosuch,  0 },                   /* C */
   {chm_nosuch, 0 },                    /* D */
   {chm_nosuch, 0 },                    /* E */
   {chm_simple, MODE_FREETARGET },      /* F */
@@ -1429,7 +1508,7 @@ struct ChannelMode chmode_table[256] =
   {chm_nosuch, 0 },
   {chm_nosuch, 0 },                    /* a */
   {chm_ban,    CHFL_BAN },             /* b */
-  {chm_simple, MODE_NOCOLOR },         /* c */
+  {chm_nosuch, 0 },                    /* c */
   {chm_nosuch, 0 },                    /* d */
   {chm_ban,    CHFL_EXCEPTION },       /* e */
   {chm_forward,        0 },                    /* f */
@@ -1626,11 +1705,12 @@ set_channel_mode(struct Client *client_p, struct Client *source_p,
        struct Client *fakesource_p;
 
        mask_pos = 0;
+       removed_mask_pos = 0;
        mode_count = 0;
        mode_limit = 0;
        mode_limit_simple = 0;
 
-       alevel = get_channel_access(source_p, msptr, CHANROLE_MODE);
+       alevel = get_channel_access(source_p, msptr);
 
        /* Hide connecting server on netburst -- jilles */
        if (ConfigServerHide.flatten_links && IsServer(source_p) && !has_id(source_p) && !HasSentEob(source_p))
@@ -1652,19 +1732,6 @@ set_channel_mode(struct Client *client_p, struct Client *source_p,
                        dir = MODE_QUERY;
                        break;
                default:
-                       /* If this mode char is locked, don't allow local users to change it. */
-                       if (MyClient(source_p) && chptr->mode_lock && strchr(chptr->mode_lock, c))
-                       {
-                               if (!(errors & SM_ERR_MLOCK))
-                                       sendto_one_numeric(source_p,
-                                                       ERR_MLOCKRESTRICTED,
-                                                       form_str(ERR_MLOCKRESTRICTED),
-                                                       chptr->chname,
-                                                       c,
-                                                       chptr->mode_lock);
-                               errors |= SM_ERR_MLOCK;
-                               continue;
-                       }
                        chmode_table[(unsigned char) c].set_func(fakesource_p, chptr, alevel,
                                       parc, &parn, parv,
                                       &errors, dir, c,