+
+ /* if we're changing oplevels and we know the oplevel, pass it on */
+ if ((MB_TYPE(mbuf, i) & MODE_CHANOP)
+ && MB_OPLEVEL(mbuf, i) < MAXOPLEVEL)
+ *strptr_i += ircd_snprintf(0, strptr + *strptr_i, BUFSIZE - *strptr_i,
+ " %s%s:%d",
+ NumNick(MB_CLIENT(mbuf, i)),
+ MB_OPLEVEL(mbuf, i));
+
+ /* deal with other modes that take clients */
+ else if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
+ build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
+
+ /* deal with modes that take strings */
+ else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS))
+ build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
+
+ /*
+ * deal with the limit. Logic here is complicated; if HACK2 is set,
+ * we're bouncing the mode, so sense is reversed, and we have to
+ * include the original limit if it looks like it's being removed
+ */
+ else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
+ build_string(strptr, strptr_i, limitbuf, 0, ' ');
+ }
+
+ /* we were told to deop the source */
+ if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
+ addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
+ addbuf[addbuf_i] = '\0'; /* terminate the string... */
+ build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
+
+ /* mark that we've done this, so we don't do it again */
+ mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
+ }
+
+ if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
+ /* If OPMODE was set, we're propagating the mode as an OPMODE message */
+ sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
+ "%H %s%s%s%s%s%s", mbuf->mb_channel,
+ rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
+ addbuf, remstr, addstr);
+ } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
+ /*
+ * If HACK2 was set, we're bouncing; we send the MODE back to
+ * the connection we got it from with the senses reversed and
+ * the proper TS; origin is us
+ */
+ sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
+ mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
+ rembuf_i ? "+" : "", rembuf, addstr, remstr,
+ mbuf->mb_channel->creationtime);
+ } else {
+ /*
+ * We're propagating a normal (or HACK3 or HACK4) MODE command
+ * to the rest of the network. We send the actual channel TS.
+ */
+ sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
+ "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
+ rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
+ addbuf, remstr, addstr,
+ mbuf->mb_channel->creationtime);
+ }
+ }
+
+ /* We've drained the ModeBuf... */
+ mbuf->mb_add = 0;
+ mbuf->mb_rem = 0;
+ mbuf->mb_count = 0;
+
+ /* reinitialize the mode-with-arg slots */
+ for (i = 0; i < MAXMODEPARAMS; i++) {
+ /* If we saved any, pack them down */
+ if (MB_TYPE(mbuf, i) & MODE_SAVE) {
+ mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
+ MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
+
+ if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
+ continue;
+ } else if (MB_TYPE(mbuf, i) & MODE_FREE)
+ MyFree(MB_STRING(mbuf, i)); /* free string if needed */
+
+ MB_TYPE(mbuf, i) = 0;
+ MB_UINT(mbuf, i) = 0;
+ }
+
+ /* If we're supposed to flush it all, do so--all hail tail recursion */
+ if (all && mbuf->mb_count)
+ return modebuf_flush_int(mbuf, 1);
+
+ return 0;
+}
+
+/** Initialise a modebuf
+ * This routine just initializes a ModeBuf structure with the information
+ * needed and the options given.
+ *
+ * @param mbuf The mode buffer to initialise.
+ * @param source The client that is performing the mode.
+ * @param connect ?
+ * @param chan The channel that the mode is being performed upon.
+ * @param dest ?
+ */
+void
+modebuf_init(struct ModeBuf *mbuf, struct Client *source,
+ struct Client *connect, struct Channel *chan, unsigned int dest)
+{
+ int i;
+
+ assert(0 != mbuf);
+ assert(0 != 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;
+ mbuf->mb_connect = connect;
+ mbuf->mb_channel = chan;
+ mbuf->mb_dest = dest;
+ mbuf->mb_count = 0;
+
+ /* clear each mode-with-parameter slot */
+ for (i = 0; i < MAXMODEPARAMS; i++) {
+ MB_TYPE(mbuf, i) = 0;
+ MB_UINT(mbuf, i) = 0;
+ }
+}
+
+/** Append a new mode to a modebuf
+ * This routine simply adds modes to be added or deleted; do a binary OR
+ * with either MODE_ADD or MODE_DEL
+ *
+ * @param mbuf Mode buffer
+ * @param mode MODE_ADD or MODE_DEL OR'd with MODE_PRIVATE etc.
+ */
+void
+modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
+{
+ assert(0 != mbuf);
+ assert(0 != (mode & (MODE_ADD | MODE_DEL)));
+
+ mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
+ MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS | MODE_REGONLY |
+ MODE_DELJOINS | MODE_WASDELJOINS | MODE_REGISTERED);
+
+ if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
+ return;
+
+ if (mode & MODE_ADD) {
+ mbuf->mb_rem &= ~mode;
+ mbuf->mb_add |= mode;
+ } else {
+ mbuf->mb_add &= ~mode;
+ mbuf->mb_rem |= mode;
+ }
+}
+
+/** Append a mode that takes an int argument to the modebuf
+ *
+ * This routine adds a mode to be added or deleted that takes a unsigned
+ * int parameter; mode may *only* be the relevant mode flag ORed with one
+ * of MODE_ADD or MODE_DEL
+ *
+ * @param mbuf The mode buffer to append to.
+ * @param mode The mode to append.
+ * @param uint The argument to the mode.
+ */
+void
+modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
+{
+ assert(0 != mbuf);
+ assert(0 != (mode & (MODE_ADD | MODE_DEL)));
+
+ if (mode == (MODE_LIMIT | MODE_DEL)) {
+ mbuf->mb_rem |= mode;
+ return;
+ }
+ MB_TYPE(mbuf, mbuf->mb_count) = mode;
+ MB_UINT(mbuf, mbuf->mb_count) = uint;
+
+ /* when we've reached the maximal count, flush the buffer */
+ if (++mbuf->mb_count >=
+ (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
+ modebuf_flush_int(mbuf, 0);
+}
+
+/** append a string mode
+ * This routine adds a mode to be added or deleted that takes a string
+ * parameter; mode may *only* be the relevant mode flag ORed with one of
+ * MODE_ADD or MODE_DEL
+ *
+ * @param mbuf The mode buffer to append to.
+ * @param mode The mode to append.
+ * @param string The string parameter to append.
+ * @param free If the string should be free'd later.
+ */
+void
+modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
+ int free)
+{
+ assert(0 != mbuf);
+ assert(0 != (mode & (MODE_ADD | MODE_DEL)));
+
+ MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
+ MB_STRING(mbuf, mbuf->mb_count) = string;
+
+ /* when we've reached the maximal count, flush the buffer */
+ if (++mbuf->mb_count >=
+ (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
+ modebuf_flush_int(mbuf, 0);
+}
+
+/** Append a mode on a client to a modebuf.
+ * This routine adds a mode to be added or deleted that takes a client
+ * parameter; mode may *only* be the relevant mode flag ORed with one of
+ * MODE_ADD or MODE_DEL
+ *
+ * @param mbuf The modebuf to append the mode to.
+ * @param mode The mode to append.
+ * @param client The client argument to append.
+ * @param oplevel The oplevel the user had or will have
+ */
+void
+modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
+ struct Client *client, int oplevel)
+{
+ assert(0 != mbuf);
+ assert(0 != (mode & (MODE_ADD | MODE_DEL)));
+
+ MB_TYPE(mbuf, mbuf->mb_count) = mode;
+ MB_CLIENT(mbuf, mbuf->mb_count) = client;
+ MB_OPLEVEL(mbuf, mbuf->mb_count) = oplevel;
+
+ /* when we've reached the maximal count, flush the buffer */
+ if (++mbuf->mb_count >=
+ (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
+ modebuf_flush_int(mbuf, 0);
+}
+
+/** Check a channel for join-delayed members.
+ * @param[in] chan Channel to search.
+ * @return Non-zero if any members are join-delayed; false if none are.
+ */
+static int
+find_delayed_joins(const struct Channel *chan)
+{
+ const struct Membership *memb;
+ for (memb = chan->members; memb; memb = memb->next_member)
+ if (IsDelayedJoin(memb))
+ return 1;
+ return 0;
+}
+
+/** The exported binding for modebuf_flush()
+ *
+ * @param mbuf The mode buffer to flush.
+ *
+ * @see modebuf_flush_int()
+ */
+int
+modebuf_flush(struct ModeBuf *mbuf)
+{
+ /* Check if MODE_WASDELJOINS should be set: */
+ /* Must be set if going -D and some clients are hidden */
+ if ((mbuf->mb_rem & MODE_DELJOINS)
+ && !(mbuf->mb_channel->mode.mode & (MODE_DELJOINS | MODE_WASDELJOINS))
+ && find_delayed_joins(mbuf->mb_channel)) {
+ mbuf->mb_channel->mode.mode |= MODE_WASDELJOINS;
+ mbuf->mb_add |= MODE_WASDELJOINS;
+ mbuf->mb_rem &= ~MODE_WASDELJOINS;
+ }
+ /* Must be cleared if +D is set */
+ if ((mbuf->mb_add & MODE_DELJOINS)
+ && ((mbuf->mb_channel->mode.mode & (MODE_WASDELJOINS | MODE_WASDELJOINS))
+ == (MODE_WASDELJOINS | MODE_WASDELJOINS))) {
+ mbuf->mb_channel->mode.mode &= ~MODE_WASDELJOINS;
+ mbuf->mb_add &= ~MODE_WASDELJOINS;
+ mbuf->mb_rem |= MODE_WASDELJOINS;
+ }
+
+ return modebuf_flush_int(mbuf, 1);
+}
+
+/* This extracts the simple modes contained in mbuf
+ *
+ * @param mbuf The mode buffer to extract the modes from.
+ * @param buf The string buffer to write the modes into.
+ */
+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_APASS, 'A',
+ MODE_UPASS, 'U',
+ MODE_REGISTERED, 'R',
+/* MODE_BAN, 'b', */
+ MODE_LIMIT, 'l',
+ MODE_REGONLY, 'r',
+ MODE_DELJOINS, 'D',
+ 0x0, 0x0
+ };
+ unsigned int add;
+ int i, bufpos = 0, len;
+ int *flag_p;
+ char *key = 0, limitbuf[20];
+ char *apass = 0, *upass = 0;
+
+ 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 | MODE_APASS | MODE_UPASS);
+
+ 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), "%u", MB_UINT(mbuf, i));
+ else if (MB_TYPE(mbuf, i) & MODE_UPASS)
+ upass = MB_STRING(mbuf, i);
+ else if (MB_TYPE(mbuf, i) & MODE_APASS)
+ apass = MB_STRING(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, ' ');
+ else if (buf[i] == 'U')
+ build_string(buf, &bufpos, upass, 0, ' ');
+ else if (buf[i] == 'A')
+ build_string(buf, &bufpos, apass, 0, ' ');
+ }
+
+ buf[bufpos] = '\0';
+
+ return;
+}
+
+/** Simple function to invalidate a channel's ban cache.
+ *
+ * This function marks all members of the channel as being neither
+ * banned nor banned.
+ *
+ * @param chan The channel to operate on.
+ */
+void
+mode_ban_invalidate(struct Channel *chan)
+{
+ struct Membership *member;
+
+ for (member = chan->members; member; member = member->next_member)
+ ClearBanValid(member);
+}
+
+/** Simple function to drop invite structures
+ *
+ * Remove all the invites on the channel.
+ *
+ * @param chan Channel to remove invites from.
+ *
+ */
+void
+mode_invite_clear(struct Channel *chan)
+{
+ while (chan->invites)
+ del_invite(chan->invites->value.cptr, chan);
+}
+
+/* What we've done for mode_parse so far... */
+#define DONE_LIMIT 0x01 /**< We've set the limit */
+#define DONE_KEY_ADD 0x02 /**< We've set the key */
+#define DONE_BANLIST 0x04 /**< We've sent the ban list */
+#define DONE_NOTOPER 0x08 /**< We've sent a "Not oper" error */
+#define DONE_BANCLEAN 0x10 /**< We've cleaned bans... */
+#define DONE_UPASS_ADD 0x20 /**< We've set user pass */
+#define DONE_APASS_ADD 0x40 /**< We've set admin pass */
+#define DONE_KEY_DEL 0x80 /**< We've removed the key */
+#define DONE_UPASS_DEL 0x100 /**< We've removed the user pass */
+#define DONE_APASS_DEL 0x200 /**< We've removed the admin pass */
+
+struct ParseState {
+ struct ModeBuf *mbuf;
+ struct Client *cptr;
+ struct Client *sptr;
+ struct Channel *chptr;
+ struct Membership *member;
+ int parc;
+ char **parv;
+ unsigned int flags;
+ unsigned int dir;
+ unsigned int done;
+ unsigned int add;
+ unsigned int del;
+ int args_used;
+ int max_args;
+ int numbans;
+ struct Ban banlist[MAXPARA];
+ struct {
+ unsigned int flag;
+ unsigned short oplevel;
+ struct Client *client;
+ } cli_change[MAXPARA];
+};
+
+/** Helper function to send "Not oper" or "Not member" messages
+ * Here's a helper function to deal with sending along "Not oper" or
+ * "Not member" messages
+ *
+ * @param state Parsing State object
+ */
+static void
+send_notoper(struct ParseState *state)
+{
+ if (state->done & DONE_NOTOPER)
+ return;
+
+ send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
+ ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
+
+ state->done |= DONE_NOTOPER;
+}
+
+/** Parse a limit
+ * Helper function to convert limits
+ *
+ * @param state Parsing state object.
+ * @param flag_p ?
+ */
+static void
+mode_parse_limit(struct ParseState *state, int *flag_p)
+{
+ unsigned int t_limit;
+
+ if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
+ if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
+ return;
+
+ if (state->parc <= 0) { /* warn if not enough args */
+ if (MyUser(state->sptr))
+ need_more_params(state->sptr, "MODE +l");
+ return;