2 * IRC - Internet Relay Chat, ircd/channel.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Co Center
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 1, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * @brief Channel management and maintenance
28 #include "destruct_event.h"
31 #include "ircd_alloc.h"
32 #include "ircd_chattr.h"
33 #include "ircd_defs.h"
34 #include "ircd_features.h"
36 #include "ircd_reply.h"
37 #include "ircd_snprintf.h"
38 #include "ircd_string.h"
45 #include "querycmds.h"
56 /* #include <assert.h> -- Now using assert in ircd_log.h */
61 /** Linked list containing the full list of all channels */
62 struct Channel* GlobalChannelList = 0;
64 /** Number of struct Membership*'s allocated */
65 static unsigned int membershipAllocCount;
66 /** Freelist for struct Membership*'s */
67 static struct Membership* membershipFreeList;
68 /** Freelist for struct Ban*'s */
69 static struct Ban* free_bans;
70 /** Number of ban structures allocated. */
71 static size_t bans_alloc;
72 /** Number of ban structures in use. */
73 static size_t bans_inuse;
76 /** return the length (>=0) of a chain of links.
77 * @param lp pointer to the start of the linked list
78 * @return the number of items in the list
80 static int list_length(struct SLink *lp)
84 for (; lp; lp = lp->next)
90 /** Set the mask for a ban, checking for IP masks.
91 * @param[in,out] ban Ban structure to modify.
92 * @param[in] banstr Mask to ban.
95 set_ban_mask(struct Ban *ban, const char *banstr)
98 assert(banstr != NULL);
99 ircd_strncpy(ban->banstr, banstr, sizeof(ban->banstr) - 1);
100 sep = strrchr(banstr, '@');
102 ban->nu_len = sep - banstr;
103 if (ipmask_parse(sep + 1, &ban->address, &ban->addrbits))
104 ban->flags |= BAN_IPMASK;
108 /** Allocate a new Ban structure.
109 * @param[in] banstr Ban mask to use.
110 * @return Newly allocated ban.
113 make_ban(const char *banstr)
118 free_bans = free_bans->next;
120 else if (!(ban = MyMalloc(sizeof(*ban))))
125 memset(ban, 0, sizeof(*ban));
126 set_ban_mask(ban, banstr);
130 /** Deallocate a ban structure.
131 * @param[in] ban Ban to deallocate.
134 free_ban(struct Ban *ban)
136 ban->next = free_bans;
141 /** Report ban usage to \a cptr.
142 * @param[in] cptr Client requesting information.
144 void bans_send_meminfo(struct Client *cptr)
148 for (num_free = 0, ban = free_bans; ban; ban = ban->next)
150 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Bans: inuse %zu(%zu) free %zu alloc %zu",
151 bans_inuse, bans_inuse * sizeof(*ban), num_free, bans_alloc);
154 /** return the struct Membership* that represents a client on a channel
155 * This function finds a struct Membership* which holds the state about
156 * a client on a specific channel. The code is smart enough to iterate
157 * over the channels a user is in, or the users in a channel to find the
158 * user depending on which is likely to be more efficient.
160 * @param chptr pointer to the channel struct
161 * @param cptr pointer to the client struct
163 * @returns pointer to the struct Membership representing this client on
164 * this channel. Returns NULL if the client is not on the channel.
165 * Returns NULL if the client is actually a server.
166 * @see find_channel_member()
168 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
170 struct Membership *m;
174 /* Servers don't have member links */
175 if (IsServer(cptr)||IsMe(cptr))
178 /* +k users are typically on a LOT of channels. So we iterate over who
179 * is in the channel. X/W are +k and are in about 5800 channels each.
180 * however there are typically no more than 1000 people in a channel
183 if (IsChannelService(cptr)) {
186 assert(m->channel == chptr);
192 /* Users on the other hand aren't allowed on more than 15 channels. 50%
193 * of users that are on channels are on 2 or less, 95% are on 7 or less,
194 * and 99% are on 10 or less.
197 m = (cli_user(cptr))->channel;
199 assert(m->user == cptr);
200 if (m->channel == chptr)
208 /** Find the client structure for a nick name (user)
209 * Find the client structure for a nick name (user)
210 * using history mechanism if necessary. If the client is not found, an error
211 * message (NO SUCH NICK) is generated. If the client was found
212 * through the history, chasing will be 1 and otherwise 0.
214 * This function was used extensively in the P09 days, and since we now have
215 * numeric nicks is no longer quite as important.
217 * @param sptr Pointer to the client that has requested the search
218 * @param user a string representing the client to be found
219 * @param chasing a variable set to 0 if the user was found directly,
221 * @returns a pointer the client, or NULL if the client wasn't found.
223 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
225 struct Client* who = FindClient(user);
232 if (!(who = get_history(user, feature_int(FEAT_KILLCHASETIMELIMIT)))) {
233 send_reply(sptr, ERR_NOSUCHNICK, user);
241 /** Decrement the count of users, and free if empty.
242 * Subtract one user from channel i (and free channel * block, if channel
245 * @param chptr The channel to subtract one from.
247 * @returns true (1) if channel still has members.
248 * false (0) if the channel is now empty.
250 int sub1_from_channel(struct Channel* chptr)
252 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
254 assert(0 != chptr->members);
261 /* There is a semantics problem here: Assuming no fragments across a
262 * split, a channel without Apass could be maliciously destroyed and
263 * recreated, and someone could set apass on the new instance.
265 * This could be fixed by preserving the empty non-Apass channel for
266 * the same time as if it had an Apass (but removing +i and +l), and
267 * reopping the first user to rejoin. However, preventing net rides
268 * requires a backwards-incompatible protocol change..
270 if (!chptr->mode.apass[0]) /* If no Apass, destroy now. */
271 destruct_channel(chptr);
272 else if (TStime() - chptr->creationtime < 172800) /* Channel younger than 48 hours? */
273 schedule_destruct_event_1m(chptr); /* Get rid of it in approximately 4-5 minutes */
275 schedule_destruct_event_48h(chptr); /* Get rid of it in approximately 48 hours */
280 /** Destroy an empty channel
281 * This function destroys an empty channel, removing it from hashtables,
282 * and removing any resources it may have consumed.
284 * @param chptr The channel to destroy
286 * @returns 0 (success)
288 * FIXME: Change to return void, this function never fails.
290 int destruct_channel(struct Channel* chptr)
292 struct Ban *ban, *next;
294 assert(0 == chptr->members);
297 * Now, find all invite links from channel structure
299 while (chptr->invites)
300 del_invite(chptr->invites->value.cptr, chptr);
302 for (ban = chptr->banlist; ban; ban = next)
308 chptr->prev->next = chptr->next;
310 GlobalChannelList = chptr->next;
312 chptr->next->prev = chptr->prev;
314 --UserStats.channels;
316 * make sure that channel actually got removed from hash table
318 assert(chptr->hnext == chptr);
323 /** returns Membership * if a person is joined and not a zombie
325 * @param chptr Channel
326 * @returns pointer to the client's struct Membership * on the channel if that
327 * user is a full member of the channel, or NULL otherwise.
329 * @see find_member_link()
331 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
333 struct Membership* member;
336 member = find_member_link(chptr, cptr);
337 return (member && !IsZombie(member)) ? member : 0;
340 /** Searches for a ban from a ban list that matches a user.
341 * @param[in] cptr The client to test.
342 * @param[in] banlist The list of bans to test.
343 * @return Pointer to a matching ban, or NULL if none exit.
345 struct Ban *find_ban(struct Client *cptr, struct Ban *banlist)
347 char nu[NICKLEN + USERLEN + 2];
348 char tmphost[HOSTLEN + 1];
349 char iphost[SOCKIPLEN + 1];
354 /* Build nick!user and alternate host names. */
355 ircd_snprintf(0, nu, sizeof(nu), "%s!%s",
356 cli_name(cptr), cli_user(cptr)->username);
357 ircd_ntoa_r(iphost, &cli_ip(cptr));
358 if (!IsAccount(cptr))
360 else if (HasHiddenHost(cptr))
361 sr = cli_user(cptr)->realhost;
364 ircd_snprintf(0, tmphost, HOSTLEN, "%s.%s",
365 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
369 /* Walk through ban list. */
370 for (found = NULL; banlist; banlist = banlist->next) {
372 /* If we have found a positive ban already, only consider exceptions. */
373 if (found && !(banlist->flags & BAN_EXCEPTION))
375 /* Compare nick!user portion of ban. */
376 banlist->banstr[banlist->nu_len] = '\0';
377 res = match(banlist->banstr, nu);
378 banlist->banstr[banlist->nu_len] = '@';
381 /* Compare host portion of ban. */
382 hostmask = banlist->banstr + banlist->nu_len + 1;
383 if (((banlist->flags & BAN_IPMASK)
384 ? !ipmask_check(&cli_ip(cptr), &banlist->address, banlist->addrbits)
385 : match(hostmask, iphost))
386 && match(hostmask, cli_user(cptr)->host)
387 && !(sr && match(hostmask, sr) == 0))
389 /* If an exception matches, no ban can match. */
390 if (banlist->flags & BAN_EXCEPTION)
392 /* Otherwise, remember this ban but keep searching for an exception. */
399 * This function returns true if the user is banned on the said channel.
400 * This function will check the ban cache if applicable, otherwise will
401 * do the comparisons and cache the result.
403 * @param[in] member The Membership to test for banned-ness.
404 * @return Non-zero if the member is banned, zero if not.
406 static int is_banned(struct Membership* member)
408 if (IsBanValid(member))
409 return IsBanned(member);
412 if (find_ban(member->user, member->channel->banlist)) {
421 /** add a user to a channel.
422 * adds a user to a channel by adding another link to the channels member
425 * @param chptr The channel to add to.
426 * @param who The user to add.
427 * @param flags The flags the user gets initially.
428 * @param oplevel The oplevel the user starts with.
430 void add_user_to_channel(struct Channel* chptr, struct Client* who,
431 unsigned int flags, int oplevel)
438 struct Membership* member = membershipFreeList;
440 membershipFreeList = member->next_member;
442 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
443 ++membershipAllocCount;
448 member->channel = chptr;
449 member->status = flags;
450 member->oplevel = oplevel;
452 member->next_member = chptr->members;
453 if (member->next_member)
454 member->next_member->prev_member = member;
455 member->prev_member = 0;
456 chptr->members = member;
458 member->next_channel = (cli_user(who))->channel;
459 if (member->next_channel)
460 member->next_channel->prev_channel = member;
461 member->prev_channel = 0;
462 (cli_user(who))->channel = member;
464 if (chptr->destruct_event)
465 remove_destruct_event(chptr);
467 ++((cli_user(who))->joined);
471 /** Remove a person from a channel, given their Membership*
473 * @param member A member of a channel.
475 * @returns true if there are more people in the channel.
477 static int remove_member_from_channel(struct Membership* member)
479 struct Channel* chptr;
481 chptr = member->channel;
483 * unlink channel member list
485 if (member->next_member)
486 member->next_member->prev_member = member->prev_member;
487 if (member->prev_member)
488 member->prev_member->next_member = member->next_member;
490 member->channel->members = member->next_member;
493 * If this is the last delayed-join user, may have to clear WASDELJOINS.
495 if (IsDelayedJoin(member))
496 CheckDelayedJoins(chptr);
499 * unlink client channel list
501 if (member->next_channel)
502 member->next_channel->prev_channel = member->prev_channel;
503 if (member->prev_channel)
504 member->prev_channel->next_channel = member->next_channel;
506 (cli_user(member->user))->channel = member->next_channel;
508 --(cli_user(member->user))->joined;
510 member->next_member = membershipFreeList;
511 membershipFreeList = member;
513 return sub1_from_channel(chptr);
516 /** Check if all the remaining members on the channel are zombies
518 * @returns False if the channel has any non zombie members, True otherwise.
521 static int channel_all_zombies(struct Channel* chptr)
523 struct Membership* member;
525 for (member = chptr->members; member; member = member->next_member) {
526 if (!IsZombie(member))
533 /** Remove a user from a channel
534 * This is the generic entry point for removing a user from a channel, this
535 * function will remove the client from the channel, and destroy the channel
536 * if there are no more normal users left.
538 * @param cptr The client
539 * @param chptr The channel
541 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
544 struct Membership* member;
547 if ((member = find_member_link(chptr, cptr))) {
548 if (remove_member_from_channel(member)) {
549 if (channel_all_zombies(chptr)) {
551 * XXX - this looks dangerous but isn't if we got the referential
552 * integrity right for channels
554 while (remove_member_from_channel(chptr->members))
561 /** Remove a user from all channels they are on.
563 * This function removes a user from all channels they are on.
565 * @param cptr The client to remove.
567 void remove_user_from_all_channels(struct Client* cptr)
569 struct Membership* chan;
571 assert(0 != cli_user(cptr));
573 while ((chan = (cli_user(cptr))->channel))
574 remove_user_from_channel(cptr, chan->channel);
577 /** Check if this user is a legitimate chanop
579 * @param cptr Client to check
580 * @param chptr Channel to check
582 * @returns True if the user is a chanop (And not a zombie), False otherwise.
585 int is_chan_op(struct Client *cptr, struct Channel *chptr)
587 struct Membership* member;
589 if ((member = find_member_link(chptr, cptr)))
590 return (!IsZombie(member) && IsChanOp(member));
595 /** Check if a user is a Zombie on a specific channel.
597 * @param cptr The client to check.
598 * @param chptr The channel to check.
600 * @returns True if the client (cptr) is a zombie on the channel (chptr),
605 int is_zombie(struct Client *cptr, struct Channel *chptr)
607 struct Membership* member;
611 if ((member = find_member_link(chptr, cptr)))
612 return IsZombie(member);
616 /** Returns if a user has voice on a channel.
618 * @param cptr The client
619 * @param chptr The channel
621 * @returns True if the client (cptr) is voiced on (chptr) and is not a zombie.
624 int has_voice(struct Client* cptr, struct Channel* chptr)
626 struct Membership* member;
629 if ((member = find_member_link(chptr, cptr)))
630 return (!IsZombie(member) && HasVoice(member));
635 /** Can this member send to a channel
637 * A user can speak on a channel iff:
639 * <li> They didn't use the Apass to gain ops.
640 * <li> They are op'd or voice'd.
641 * <li> You aren't banned.
642 * <li> The channel isn't +m
643 * <li> The channel isn't +n or you are on the channel.
646 * This function will optionally reveal a user on a delayed join channel if
647 * they are allowed to send to the channel.
649 * @param member The membership of the user
650 * @param reveal If true, the user will be "revealed" on a delayed
653 * @returns True if the client can speak on the channel.
655 int member_can_send_to_channel(struct Membership* member, int reveal)
659 /* Discourage using the Apass to get op. They should use the upass. */
660 if (IsChannelManager(member) && *member->channel->mode.upass)
663 if (IsVoicedOrOpped(member))
666 * If it's moderated, and you aren't a privileged user, you can't
669 if (member->channel->mode.mode & MODE_MODERATED)
671 /* If only logged in users may join and you're not one, you can't speak. */
672 if (member->channel->mode.mode & MODE_REGONLY && !IsAccount(member->user))
675 * If you're banned then you can't speak either.
676 * but because of the amount of CPU time that is_banned chews
677 * we only check it for our clients.
679 if (MyUser(member->user) && is_banned(member))
682 if (IsDelayedJoin(member) && reveal)
683 RevealDelayedJoin(member);
688 /** Check if a client can send to a channel.
690 * Has the added check over member_can_send_to_channel() of servers can
693 * @param cptr The client to check
694 * @param chptr The channel to check
695 * @param reveal If the user should be revealed (see
696 * member_can_send_to_channel())
698 * @returns true if the client is allowed to speak on the channel, false
701 * @see member_can_send_to_channel()
703 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr, int reveal)
705 struct Membership *member;
708 * Servers can always speak on channels.
713 member = find_channel_member(cptr, chptr);
716 * You can't speak if you're off channel, and it is +n (no external messages)
720 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED)) ||
721 ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(cptr)))
724 return !find_ban(cptr, chptr->banlist);
726 return member_can_send_to_channel(member, reveal);
729 /** Returns the name of a channel that prevents the user from changing nick.
730 * if a member and not (opped or voiced) and (banned or moderated), return
731 * the name of the first channel banned on.
733 * @param cptr The client
735 * @returns the name of the first channel banned on, or NULL if the user
738 const char* find_no_nickchange_channel(struct Client* cptr)
741 struct Membership* member;
742 for (member = (cli_user(cptr))->channel; member;
743 member = member->next_channel) {
744 if (!IsVoicedOrOpped(member) &&
745 (is_banned(member) ||
746 (member->channel->mode.mode & MODE_MODERATED)))
747 return member->channel->chname;
754 /** Fill mbuf/pbuf with modes from chptr
755 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
756 * with the parameters in pbuf as visible by cptr.
758 * This function will hide keys from non-op'd, non-server clients.
760 * @param cptr The client to generate the mode for.
761 * @param mbuf The buffer to write the modes into.
762 * @param pbuf The buffer to write the mode parameters into.
763 * @param buflen The length of the buffers.
764 * @param chptr The channel to get the modes from.
765 * @param member The membership of this client on this channel (or NULL
766 * if this client isn't on this channel)
769 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen,
770 struct Channel *chptr, struct Membership *member)
772 int previous_parameter = 0;
779 if (chptr->mode.mode & MODE_SECRET)
781 else if (chptr->mode.mode & MODE_PRIVATE)
783 if (chptr->mode.mode & MODE_MODERATED)
785 if (chptr->mode.mode & MODE_TOPICLIMIT)
787 if (chptr->mode.mode & MODE_INVITEONLY)
789 if (chptr->mode.mode & MODE_NOPRIVMSGS)
791 if (chptr->mode.mode & MODE_REGONLY)
793 if (chptr->mode.mode & MODE_DELJOINS)
795 else if (MyUser(cptr) && (chptr->mode.mode & MODE_WASDELJOINS))
797 if (chptr->mode.limit) {
799 ircd_snprintf(0, pbuf, buflen, "%u", chptr->mode.limit);
800 previous_parameter = 1;
803 if (*chptr->mode.key) {
805 if (previous_parameter)
807 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
808 strcat(pbuf, chptr->mode.key);
811 previous_parameter = 1;
813 if (*chptr->mode.apass) {
815 if (previous_parameter)
817 if (IsServer(cptr)) {
818 strcat(pbuf, chptr->mode.apass);
821 previous_parameter = 1;
823 if (*chptr->mode.upass) {
825 if (previous_parameter)
827 if (IsServer(cptr) || (member && IsChanOp(member) && OpLevel(member) == 0)) {
828 strcat(pbuf, chptr->mode.upass);
835 /** Compare two members oplevel
837 * @param mp1 Pointer to a pointer to a membership
838 * @param mp2 Pointer to a pointer to a membership
840 * @returns 0 if equal, -1 if mp1 is lower, +1 otherwise.
844 int compare_member_oplevel(const void *mp1, const void *mp2)
846 struct Membership const* member1 = *(struct Membership const**)mp1;
847 struct Membership const* member2 = *(struct Membership const**)mp2;
848 if (member1->oplevel == member2->oplevel)
850 return (member1->oplevel < member2->oplevel) ? -1 : 1;
853 /* send "cptr" a full list of the modes for channel chptr.
855 * Sends a BURST line to cptr, bursting all the modes for the channel.
857 * @param cptr Client pointer
858 * @param chptr Channel pointer
860 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
862 /* The order in which modes are generated is now mandatory */
863 static unsigned int current_flags[4] =
864 { 0, CHFL_VOICE, CHFL_CHANOP, CHFL_CHANOP | CHFL_VOICE };
870 struct Membership* member;
872 char modebuf[MODEBUFLEN];
873 char parabuf[MODEBUFLEN];
875 int number_of_ops = 0;
876 int opped_members_index = 0;
877 struct Membership** opped_members = NULL;
878 int last_oplevel = 0;
879 int feat_oplevels = (chptr->mode.apass[0]) != '\0';
884 if (IsLocalChannel(chptr->chname))
887 member = chptr->members;
888 lp2 = chptr->banlist;
890 *modebuf = *parabuf = '\0';
891 channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr, 0);
893 for (first = 1; full; first = 0) /* Loop for multiple messages */
895 full = 0; /* Assume by default we get it
896 all in one message */
898 /* (Continued) prefix: "<Y> B <channel> <TS>" */
899 /* is there any better way we can do this? */
900 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
901 chptr->creationtime);
903 if (first && modebuf[1]) /* Add simple modes (Aiklmnpstu)
906 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
907 msgq_append(&me, mb, " %s", modebuf);
910 msgq_append(&me, mb, " %s", parabuf);
914 * Attach nicks, comma separated " nick[:modes],nick[:modes],..."
916 * First find all opless members.
917 * Run 2 times over all members, to group the members with
918 * and without voice together.
919 * Then run 2 times over all opped members (which are ordered
920 * by op-level) to also group voice and non-voice together.
922 for (first = 1; flag_cnt < 4; new_mode = 1, ++flag_cnt)
926 if (flag_cnt < 2 && IsChanOp(member))
929 * The first loop (to find all non-voice/op), we count the ops.
930 * The second loop (to find all voiced non-ops), store the ops
931 * in a dynamic array.
936 opped_members[opped_members_index++] = member;
938 /* Only handle the members with the flags that we are interested in. */
939 if ((member->status & CHFL_VOICED_OR_OPPED) == current_flags[flag_cnt])
941 if (msgq_bufleft(mb) < NUMNICKLEN + 3 + MAXOPLEVELDIGITS)
942 /* The 3 + MAXOPLEVELDIGITS is a possible ",:v999". */
944 full = 1; /* Make sure we continue after
946 /* Ensure the new BURST line contains the current
947 * ":mode", except when there is no mode yet. */
948 new_mode = (flag_cnt > 0) ? 1 : 0;
949 break; /* Do not add this member to this message */
951 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
952 first = 0; /* From now on, use commas to add new nicks */
955 * Do we have a nick with a new mode ?
956 * Or are we starting a new BURST line?
961 * This means we are at the _first_ member that has only
962 * voice, or the first member that has only ops, or the
963 * first member that has voice and ops (so we get here
964 * at most three times, plus once for every start of
965 * a continued BURST line where only these modes is current.
966 * In the two cases where the current mode includes ops,
967 * we need to add the _absolute_ value of the oplevel to the mode.
969 char tbuf[3 + MAXOPLEVELDIGITS] = ":";
972 if (HasVoice(member)) /* flag_cnt == 1 or 3 */
974 if (IsChanOp(member)) /* flag_cnt == 2 or 3 */
976 /* append the absolute value of the oplevel */
978 loc += ircd_snprintf(0, tbuf + loc, sizeof(tbuf) - loc, "%u", last_oplevel = member->oplevel);
983 msgq_append(&me, mb, tbuf);
986 else if (feat_oplevels && flag_cnt > 1 && last_oplevel != member->oplevel)
989 * This can't be the first member of a (continued) BURST
990 * message because then either flag_cnt == 0 or new_mode == 1
991 * Now we need to append the incremental value of the oplevel.
993 char tbuf[2 + MAXOPLEVELDIGITS];
994 ircd_snprintf(0, tbuf, sizeof(tbuf), ":%u", member->oplevel - last_oplevel);
995 last_oplevel = member->oplevel;
996 msgq_append(&me, mb, tbuf);
999 /* Go to the next `member'. */
1001 member = member->next_member;
1003 member = opped_members[++opped_members_index];
1008 /* Point `member' at the start of the list again. */
1011 member = chptr->members;
1012 /* Now, after one loop, we know the number of ops and can
1013 * allocate the dynamic array with pointer to the ops. */
1014 opped_members = (struct Membership**)
1015 MyMalloc((number_of_ops + 1) * sizeof(struct Membership*));
1016 opped_members[number_of_ops] = NULL; /* Needed for loop termination */
1020 /* At the end of the second loop, sort the opped members with
1021 * increasing op-level, so that we will output them in the
1022 * correct order (and all op-level increments stay positive) */
1024 qsort(opped_members, number_of_ops,
1025 sizeof(struct Membership*), compare_member_oplevel);
1026 /* The third and fourth loop run only over the opped members. */
1027 member = opped_members[(opped_members_index = 0)];
1030 } /* loop over 0,+v,+o,+ov */
1034 /* Attach all bans, space separated " :%ban ban ..." */
1035 for (first = 2; lp2; lp2 = lp2->next)
1037 len = strlen(lp2->banstr);
1038 if (msgq_bufleft(mb) < len + 1 + first)
1039 /* The +1 stands for the added ' '.
1040 * The +first stands for the added ":%".
1046 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
1052 send_buffer(cptr, mb, 0); /* Send this message */
1054 } /* Continue when there was something
1055 that didn't fit (full==1) */
1057 MyFree(opped_members);
1058 if (feature_bool(FEAT_TOPIC_BURST) && (chptr->topic[0] != '\0'))
1059 sendcmdto_one(&me, CMD_TOPIC, cptr, "%H %Tu %Tu :%s", chptr,
1060 chptr->creationtime, chptr->topic_time, chptr->topic);
1063 /** Canonify a mask.
1066 * @author Carlo Wood (Run),
1069 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
1070 * When the user name or host name are too long (USERLEN and HOSTLEN
1071 * respectively) then they are cut off at the start with a '*'.
1073 * The following transformations are made:
1075 * 1) xxx -> nick!*@*
1076 * 2) xxx.xxx -> *!*\@host
1077 * 3) xxx\!yyy -> nick!user\@*
1078 * 4) xxx\@yyy -> *!user\@host
1079 * 5) xxx!yyy\@zzz -> nick!user\@host
1081 * @param mask The uncanonified mask.
1082 * @returns The updated mask in a static buffer.
1084 char *pretty_mask(char *mask)
1086 static char star[2] = { '*', 0 };
1087 static char retmask[NICKLEN + USERLEN + HOSTLEN + 3];
1088 char *last_dot = NULL;
1091 /* Case 1: default */
1096 /* Do a _single_ pass through the characters of the mask: */
1097 for (ptr = mask; *ptr; ++ptr)
1101 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1105 else if (*ptr == '@')
1107 /* Case 4: Found last '@' (without finding a '!' yet) */
1112 else if (*ptr == '.' || *ptr == ':')
1114 /* Case 2: Found character specific to IP or hostname (without
1115 * finding a '!' or '@' yet) */
1125 /* Case 4 or 5: Found last '@' */
1131 if (user == star && last_dot)
1141 char *nick_end = (user != star) ? user - 1 : ptr;
1142 if (nick_end - nick > NICKLEN)
1148 char *user_end = (host != star) ? host - 1 : ptr;
1149 if (user_end - user > USERLEN)
1151 user = user_end - USERLEN;
1156 if (host != star && ptr - host > HOSTLEN)
1158 host = ptr - HOSTLEN;
1161 ircd_snprintf(0, retmask, sizeof(retmask), "%s!%s@%s", nick, user, host);
1165 /** send a banlist to a client for a channel
1167 * @param cptr Client to send the banlist to.
1168 * @param chptr Channel whose banlist to send.
1170 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1177 for (lp = chptr->banlist; lp; lp = lp->next)
1178 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->banstr,
1181 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1184 /** Check a key against a keyring.
1185 * We are now treating the key part of /join channellist key as a key
1186 * ring; that is, we try one key against the actual channel key, and if that
1187 * doesn't work, we try the next one, and so on. -Kev -Texaco
1188 * Returns: 0 on match, 1 otherwise
1189 * This version contributed by SeKs \<intru@info.polymtl.ca\>
1191 * @param key Key to check
1192 * @param keyring Comma separated list of keys
1194 * @returns True if the key was found and matches, false otherwise.
1196 static int compall(char *key, char *keyring)
1201 p1 = key; /* point to the key... */
1202 while (*p1 && *p1 == *keyring)
1203 { /* step through the key and ring until they
1209 if (!*p1 && (!*keyring || *keyring == ','))
1210 /* ok, if we're at the end of the and also at the end of one of the keys
1211 in the keyring, we have a match */
1214 if (!*keyring) /* if we're at the end of the key ring, there
1215 weren't any matches, so we return 1 */
1218 /* Not at the end of the key ring, so step
1219 through to the next key in the ring: */
1220 while (*keyring && *(keyring++) != ',');
1222 goto top; /* and check it against the key */
1225 /** Returns if a user can join a channel with a specific key.
1227 * @param sptr The client trying to join
1228 * @param chptr The channel to join
1229 * @param key The key to use
1231 * @returns any error that occurred bit-wise OR'd with MAGIC_OPER_OVERRIDE
1232 * if the oper used the magic key, 0 if no error occurred.
1234 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
1236 int overrideJoin = 0;
1239 * Now a banned user CAN join if invited -- Nemesi
1240 * Now a user CAN escape channel limit if invited -- bfriendly
1241 * Now a user CAN escape anything if invited -- Isomer
1244 if (IsInvited(sptr, chptr))
1247 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
1248 a HACK(4) notice will be sent if he would not have been supposed
1249 to join normally. */
1250 if (IsLocalChannel(chptr->chname) && HasPriv(sptr, PRIV_WALK_LCHAN) &&
1251 !BadPtr(key) && compall("OVERRIDE",chptr->mode.key) != 0 &&
1252 compall("OVERRIDE",key) == 0)
1253 overrideJoin = MAGIC_OPER_OVERRIDE;
1255 if (chptr->mode.mode & MODE_INVITEONLY)
1256 return overrideJoin + ERR_INVITEONLYCHAN;
1258 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
1259 return overrideJoin + ERR_CHANNELISFULL;
1261 if ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
1262 return overrideJoin + ERR_NEEDREGGEDNICK;
1264 if (find_ban(sptr, chptr->banlist))
1265 return overrideJoin + ERR_BANNEDFROMCHAN;
1268 * now using compall (above) to test against a whole key ring -Kev
1270 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
1271 return overrideJoin + ERR_BADCHANNELKEY;
1274 return ERR_DONTCHEAT;
1279 /** Remove bells and commas from channel name
1281 * @param cn Channel name to clean, modified in place.
1283 void clean_channelname(char *cn)
1287 for (i = 0; cn[i]; i++) {
1288 if (i >= IRCD_MIN(CHANNELLEN, feature_int(FEAT_CHANNELLEN))
1289 || !IsChannelChar(cn[i])) {
1293 if (IsChannelLower(cn[i])) {
1294 cn[i] = ToLower(cn[i]);
1300 if ((unsigned char)(cn[i]) == 0xd0)
1301 cn[i] = (char) 0xf0;
1307 /** Get a channel block, creating if necessary.
1308 * Get Channel block for chname (and allocate a new channel
1309 * block, if it didn't exists before).
1311 * @param cptr Client joining the channel.
1312 * @param chname The name of the channel to join.
1313 * @param flag set to CGT_CREATE to create the channel if it doesn't
1316 * @returns NULL if the channel is invalid, doesn't exist and CGT_CREATE
1317 * wasn't specified or a pointer to the channel structure
1319 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1321 struct Channel *chptr;
1324 if (EmptyString(chname))
1327 len = strlen(chname);
1328 if (MyUser(cptr) && len > CHANNELLEN)
1331 *(chname + CHANNELLEN) = '\0';
1333 if ((chptr = FindChannel(chname)))
1335 if (flag == CGT_CREATE)
1337 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1339 ++UserStats.channels;
1340 memset(chptr, 0, sizeof(struct Channel));
1341 strcpy(chptr->chname, chname);
1342 if (GlobalChannelList)
1343 GlobalChannelList->prev = chptr;
1345 chptr->next = GlobalChannelList;
1346 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1347 GlobalChannelList = chptr;
1353 /** invite a user to a channel.
1355 * Adds an invite for a user to a channel. Limits the number of invites
1356 * to FEAT_MAXCHANNELSPERUSER. Does not sent notification to the user.
1358 * @param cptr The client to be invited.
1359 * @param chptr The channel to be invited to.
1361 void add_invite(struct Client *cptr, struct Channel *chptr)
1363 struct SLink *inv, **tmp;
1365 del_invite(cptr, chptr);
1367 * Delete last link in chain if the list is max length
1369 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1370 if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER))
1371 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1373 * Add client to channel invite list
1376 inv->value.cptr = cptr;
1377 inv->next = chptr->invites;
1378 chptr->invites = inv;
1380 * Add channel to the end of the client invite list
1382 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1384 inv->value.chptr = chptr;
1387 (cli_user(cptr))->invites++;
1390 /** Delete an invite
1391 * Delete Invite block from channel invite list and client invite list
1393 * @param cptr Client pointer
1394 * @param chptr Channel pointer
1396 void del_invite(struct Client *cptr, struct Channel *chptr)
1398 struct SLink **inv, *tmp;
1400 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1401 if (tmp->value.cptr == cptr)
1406 (cli_user(cptr))->invites--;
1410 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1411 if (tmp->value.chptr == chptr)
1420 /** @page zombie Explanation of Zombies
1424 * A channel member is turned into a zombie when he is kicked from a
1425 * channel but his server has not acknowledged the kick. Servers that
1426 * see the member as a zombie can accept actions he performed before
1427 * being kicked, without allowing chanop operations from outsiders or
1428 * desyncing the network.
1436 * X --a--> A --b--> B --d--> D
1441 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1442 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1444 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1445 * Remove the user immediately when no users are left on the channel.
1446 * b) On server B : remove the user (who/lp) from the channel, send a
1447 * PART upstream (to A) and pass on the KICK.
1448 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1449 * channel, and pass on the KICK.
1450 * d) On server D : remove the user (who/lp) from the channel, and pass on
1454 * - Setting the ZOMBIE flag never hurts, we either remove the
1455 * client after that or we don't.
1456 * - The KICK message was already passed on, as should be in all cases.
1457 * - `who' is removed in all cases except case a) when users are left.
1458 * - A PART is only sent upstream in case b).
1464 * 1 --- 2 --- 3 --- 4 --- 5
1469 * We also need to turn 'who' into a zombie on servers 1 and 6,
1470 * because a KICK from 'who' (kicking someone else in that direction)
1471 * can arrive there afterward - which should not be bounced itself.
1472 * Therefore case a) also applies for servers 1 and 6.
1477 /** Turn a user on a channel into a zombie
1478 * This function turns a user into a zombie (see \ref zombie)
1480 * @param member The structure representing this user on this channel.
1481 * @param who The client that is being kicked.
1482 * @param cptr The connection the kick came from.
1483 * @param sptr The client that is doing the kicking.
1484 * @param chptr The channel the user is being kicked from.
1486 void make_zombie(struct Membership* member, struct Client* who,
1487 struct Client* cptr, struct Client* sptr, struct Channel* chptr)
1489 assert(0 != member);
1494 /* Default for case a): */
1497 /* Case b) or c) ?: */
1498 if (MyUser(who)) /* server 4 */
1500 if (IsServer(cptr)) /* Case b) ? */
1501 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1502 remove_user_from_channel(who, chptr);
1505 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1507 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1508 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1509 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1511 remove_user_from_channel(who, chptr);
1516 /* Case a) (servers 1, 2, 3 and 6) */
1517 if (channel_all_zombies(chptr))
1518 remove_user_from_channel(who, chptr);
1520 /* XXX Can't actually call Debug here; if the channel is all zombies,
1521 * chptr will no longer exist when we get here.
1522 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1526 /** returns the number of zombies on a channel
1527 * @param chptr Channel to count zombies in.
1529 * @returns The number of zombies on the channel.
1531 int number_of_zombies(struct Channel *chptr)
1533 struct Membership* member;
1537 for (member = chptr->members; member; member = member->next_member) {
1538 if (IsZombie(member))
1544 /** Concatenate some strings together.
1545 * This helper function builds an argument string in strptr, consisting
1546 * of the original string, a space, and str1 and str2 concatenated (if,
1547 * of course, str2 is not NULL)
1549 * @param strptr The buffer to concatenate into
1550 * @param strptr_i modified offset to the position to modify
1551 * @param str1 The string to concatenate from.
1552 * @param str2 The second string to contatenate from.
1553 * @param c Charactor to separate the string from str1 and str2.
1556 build_string(char *strptr, int *strptr_i, const char *str1,
1557 const char *str2, char c)
1560 strptr[(*strptr_i)++] = c;
1563 strptr[(*strptr_i)++] = *(str1++);
1567 strptr[(*strptr_i)++] = *(str2++);
1569 strptr[(*strptr_i)] = '\0';
1572 /** Flush out the modes
1573 * This is the workhorse of our ModeBuf suite; this actually generates the
1574 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1576 * @param mbuf The mode buffer to flush
1577 * @param all If true, flush all modes, otherwise leave partial modes in the
1583 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1585 /* we only need the flags that don't take args right now */
1586 static int flags[] = {
1587 /* MODE_CHANOP, 'o', */
1588 /* MODE_VOICE, 'v', */
1591 MODE_MODERATED, 'm',
1592 MODE_TOPICLIMIT, 't',
1593 MODE_INVITEONLY, 'i',
1594 MODE_NOPRIVMSGS, 'n',
1597 MODE_WASDELJOINS, 'd',
1598 /* MODE_KEY, 'k', */
1599 /* MODE_BAN, 'b', */
1601 /* MODE_APASS, 'A', */
1602 /* MODE_UPASS, 'U', */
1608 struct Client *app_source; /* where the MODE appears to come from */
1610 char addbuf[20]; /* accumulates +psmtin, etc. */
1612 char rembuf[20]; /* accumulates -psmtin, etc. */
1614 char *bufptr; /* we make use of indirection to simplify the code */
1617 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1619 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1621 char *strptr; /* more indirection to simplify the code */
1624 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1627 char limitbuf[20]; /* convert limits to strings */
1629 unsigned int limitdel = MODE_LIMIT;
1633 /* If the ModeBuf is empty, we have nothing to do */
1634 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1637 /* Ok, if we were given the OPMODE flag, or its a server, hide the source.
1639 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE || IsServer(mbuf->mb_source) || IsMe(mbuf->mb_source))
1642 app_source = mbuf->mb_source;
1645 * Account for user we're bouncing; we have to get it in on the first
1646 * bounced MODE, or we could have problems
1648 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1649 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1651 /* Calculate the simple flags */
1652 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1653 if (*flag_p & mbuf->mb_add)
1654 addbuf[addbuf_i++] = flag_p[1];
1655 else if (*flag_p & mbuf->mb_rem)
1656 rembuf[rembuf_i++] = flag_p[1];
1659 /* Now go through the modes with arguments... */
1660 for (i = 0; i < mbuf->mb_count; i++) {
1661 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1663 bufptr_i = &addbuf_i;
1666 bufptr_i = &rembuf_i;
1669 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1670 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1672 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
1673 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1675 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1676 totalbuflen -= IRCD_MAX(5, tmp) + 1;
1678 } else if (MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS)) {
1679 tmp = strlen(MB_STRING(mbuf, i));
1681 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1682 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1685 switch(MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS))
1697 bufptr[(*bufptr_i)++] = mode_char;
1698 totalbuflen -= tmp + 1;
1700 } else if (MB_TYPE(mbuf, i) & MODE_KEY) {
1701 tmp = (mbuf->mb_dest & MODEBUF_DEST_NOKEY ? 1 :
1702 strlen(MB_STRING(mbuf, i)));
1704 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1705 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1707 bufptr[(*bufptr_i)++] = 'k';
1708 totalbuflen -= tmp + 1;
1710 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1711 /* if it's a limit, we also format the number */
1712 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
1714 tmp = strlen(limitbuf);
1716 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1717 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1719 bufptr[(*bufptr_i)++] = 'l';
1720 totalbuflen -= tmp + 1;
1725 /* terminate the mode strings */
1726 addbuf[addbuf_i] = '\0';
1727 rembuf[rembuf_i] = '\0';
1729 /* If we're building a user visible MODE or HACK... */
1730 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1731 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1732 MODEBUF_DEST_LOG)) {
1733 /* Set up the parameter strings */
1739 for (i = 0; i < mbuf->mb_count; i++) {
1740 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1743 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1745 strptr_i = &addstr_i;
1748 strptr_i = &remstr_i;
1751 /* deal with clients... */
1752 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1753 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1755 /* deal with bans... */
1756 else if (MB_TYPE(mbuf, i) & MODE_BAN)
1757 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1759 /* deal with keys... */
1760 else if (MB_TYPE(mbuf, i) & MODE_KEY)
1761 build_string(strptr, strptr_i, mbuf->mb_dest & MODEBUF_DEST_NOKEY ?
1762 "*" : MB_STRING(mbuf, i), 0, ' ');
1764 /* deal with invisible passwords */
1765 else if (MB_TYPE(mbuf, i) & (MODE_APASS | MODE_UPASS))
1766 build_string(strptr, strptr_i, "*", 0, ' ');
1769 * deal with limit; note we cannot include the limit parameter if we're
1772 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1773 (MODE_ADD | MODE_LIMIT))
1774 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1777 /* send the messages off to their destination */
1778 if (mbuf->mb_dest & MODEBUF_DEST_HACK2)
1779 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1781 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1782 mbuf->mb_source : app_source),
1783 mbuf->mb_channel->chname,
1784 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1785 addbuf, remstr, addstr,
1786 mbuf->mb_channel->creationtime);
1788 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1789 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1790 "%s%s%s%s%s%s [%Tu]",
1791 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1792 mbuf->mb_source : app_source),
1793 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1794 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1795 mbuf->mb_channel->creationtime);
1797 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1798 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1800 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1801 mbuf->mb_source : app_source),
1802 mbuf->mb_channel->chname,
1803 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1804 addbuf, remstr, addstr,
1805 mbuf->mb_channel->creationtime);
1807 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1808 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1809 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1810 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1811 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1813 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1814 sendcmdto_channel_butserv_butone(app_source, CMD_MODE, mbuf->mb_channel, NULL, 0,
1815 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1816 rembuf_i ? "-" : "", rembuf,
1817 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1820 /* Now are we supposed to propagate to other servers? */
1821 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1822 /* set up parameter string */
1829 * limit is supressed if we're removing it; we have to figure out which
1830 * direction is the direction for it to be removed, though...
1832 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1834 for (i = 0; i < mbuf->mb_count; i++) {
1835 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1838 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1840 strptr_i = &addstr_i;
1843 strptr_i = &remstr_i;
1846 /* deal with modes that take clients */
1847 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1848 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1850 /* deal with modes that take strings */
1851 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS))
1852 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1855 * deal with the limit. Logic here is complicated; if HACK2 is set,
1856 * we're bouncing the mode, so sense is reversed, and we have to
1857 * include the original limit if it looks like it's being removed
1859 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1860 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1863 /* we were told to deop the source */
1864 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1865 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1866 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1867 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1869 /* mark that we've done this, so we don't do it again */
1870 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1873 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1874 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1875 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1876 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1877 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1878 addbuf, remstr, addstr);
1879 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1881 * If HACK2 was set, we're bouncing; we send the MODE back to the
1882 * connection we got it from with the senses reversed and a TS of 0;
1885 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1886 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1887 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1888 mbuf->mb_channel->creationtime);
1891 * We're propagating a normal MODE command to the rest of the network;
1892 * we send the actual channel TS unless this is a HACK3 or a HACK4
1894 if (IsServer(mbuf->mb_source))
1895 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1896 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1897 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1898 addbuf, remstr, addstr,
1899 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1900 mbuf->mb_channel->creationtime);
1902 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1903 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1904 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1905 addbuf, remstr, addstr);
1909 /* We've drained the ModeBuf... */
1914 /* reinitialize the mode-with-arg slots */
1915 for (i = 0; i < MAXMODEPARAMS; i++) {
1916 /* If we saved any, pack them down */
1917 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1918 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1919 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1921 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1923 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1924 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1926 MB_TYPE(mbuf, i) = 0;
1927 MB_UINT(mbuf, i) = 0;
1930 /* If we're supposed to flush it all, do so--all hail tail recursion */
1931 if (all && mbuf->mb_count)
1932 return modebuf_flush_int(mbuf, 1);
1937 /** Initialise a modebuf
1938 * This routine just initializes a ModeBuf structure with the information
1939 * needed and the options given.
1941 * @param mbuf The mode buffer to initialise.
1942 * @param source The client that is performing the mode.
1944 * @param chan The channel that the mode is being performed upon.
1948 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1949 struct Client *connect, struct Channel *chan, unsigned int dest)
1954 assert(0 != source);
1958 if (IsLocalChannel(chan->chname)) dest &= ~MODEBUF_DEST_SERVER;
1962 mbuf->mb_source = source;
1963 mbuf->mb_connect = connect;
1964 mbuf->mb_channel = chan;
1965 mbuf->mb_dest = dest;
1968 /* clear each mode-with-parameter slot */
1969 for (i = 0; i < MAXMODEPARAMS; i++) {
1970 MB_TYPE(mbuf, i) = 0;
1971 MB_UINT(mbuf, i) = 0;
1975 /** Append a new mode to a modebuf
1976 * This routine simply adds modes to be added or deleted; do a binary OR
1977 * with either MODE_ADD or MODE_DEL
1979 * @param mbuf Mode buffer
1980 * @param mode MODE_ADD or MODE_DEL OR'd with MODE_PRIVATE etc.
1983 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1986 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1988 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1989 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS | MODE_REGONLY |
1990 MODE_DELJOINS | MODE_WASDELJOINS);
1992 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1995 if (mode & MODE_ADD) {
1996 mbuf->mb_rem &= ~mode;
1997 mbuf->mb_add |= mode;
1999 mbuf->mb_add &= ~mode;
2000 mbuf->mb_rem |= mode;
2004 /** Append a mode that takes an int argument to the modebuf
2006 * This routine adds a mode to be added or deleted that takes a unsigned
2007 * int parameter; mode may *only* be the relevant mode flag ORed with one
2008 * of MODE_ADD or MODE_DEL
2010 * @param mbuf The mode buffer to append to.
2011 * @param mode The mode to append.
2012 * @param uint The argument to the mode.
2015 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
2018 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2020 if (mode == (MODE_LIMIT | MODE_DEL)) {
2021 mbuf->mb_rem |= mode;
2024 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2025 MB_UINT(mbuf, mbuf->mb_count) = uint;
2027 /* when we've reached the maximal count, flush the buffer */
2028 if (++mbuf->mb_count >=
2029 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2030 modebuf_flush_int(mbuf, 0);
2033 /** append a string mode
2034 * This routine adds a mode to be added or deleted that takes a string
2035 * parameter; mode may *only* be the relevant mode flag ORed with one of
2036 * MODE_ADD or MODE_DEL
2038 * @param mbuf The mode buffer to append to.
2039 * @param mode The mode to append.
2040 * @param string The string parameter to append.
2041 * @param free If the string should be free'd later.
2044 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
2048 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2050 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
2051 MB_STRING(mbuf, mbuf->mb_count) = string;
2053 /* when we've reached the maximal count, flush the buffer */
2054 if (++mbuf->mb_count >=
2055 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2056 modebuf_flush_int(mbuf, 0);
2059 /** Append a mode on a client to a modebuf.
2060 * This routine adds a mode to be added or deleted that takes a client
2061 * parameter; mode may *only* be the relevant mode flag ORed with one of
2062 * MODE_ADD or MODE_DEL
2064 * @param mbuf The modebuf to append the mode to.
2065 * @param mode The mode to append.
2066 * @param client The client argument to append.
2069 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
2070 struct Client *client)
2073 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2075 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2076 MB_CLIENT(mbuf, mbuf->mb_count) = client;
2078 /* when we've reached the maximal count, flush the buffer */
2079 if (++mbuf->mb_count >=
2080 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2081 modebuf_flush_int(mbuf, 0);
2084 /** The exported binding for modebuf_flush()
2086 * @param mbuf The mode buffer to flush.
2088 * @see modebuf_flush_int()
2091 modebuf_flush(struct ModeBuf *mbuf)
2093 struct Membership *memb;
2095 /* Check if MODE_WASDELJOINS should be set */
2096 if (!(mbuf->mb_channel->mode.mode & (MODE_DELJOINS | MODE_WASDELJOINS))
2097 && (mbuf->mb_rem & MODE_DELJOINS)) {
2098 for (memb = mbuf->mb_channel->members; memb; memb = memb->next_member) {
2099 if (IsDelayedJoin(memb)) {
2100 mbuf->mb_channel->mode.mode |= MODE_WASDELJOINS;
2101 mbuf->mb_add |= MODE_WASDELJOINS;
2102 mbuf->mb_rem &= ~MODE_WASDELJOINS;
2108 return modebuf_flush_int(mbuf, 1);
2111 /* This extracts the simple modes contained in mbuf
2113 * @param mbuf The mode buffer to extract the modes from.
2114 * @param buf The string buffer to write the modes into.
2117 modebuf_extract(struct ModeBuf *mbuf, char *buf)
2119 static int flags[] = {
2120 /* MODE_CHANOP, 'o', */
2121 /* MODE_VOICE, 'v', */
2124 MODE_MODERATED, 'm',
2125 MODE_TOPICLIMIT, 't',
2126 MODE_INVITEONLY, 'i',
2127 MODE_NOPRIVMSGS, 'n',
2131 /* MODE_BAN, 'b', */
2138 int i, bufpos = 0, len;
2140 char *key = 0, limitbuf[20];
2141 char *apass = 0, *upass = 0;
2150 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
2151 if (MB_TYPE(mbuf, i) & MODE_ADD) {
2152 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT | MODE_APASS | MODE_UPASS);
2154 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
2155 key = MB_STRING(mbuf, i);
2156 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
2157 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
2158 else if (MB_TYPE(mbuf, i) & MODE_UPASS)
2159 upass = MB_STRING(mbuf, i);
2160 else if (MB_TYPE(mbuf, i) & MODE_APASS)
2161 apass = MB_STRING(mbuf, i);
2168 buf[bufpos++] = '+'; /* start building buffer */
2170 for (flag_p = flags; flag_p[0]; flag_p += 2)
2172 buf[bufpos++] = flag_p[1];
2174 for (i = 0, len = bufpos; i < len; i++) {
2176 build_string(buf, &bufpos, key, 0, ' ');
2177 else if (buf[i] == 'l')
2178 build_string(buf, &bufpos, limitbuf, 0, ' ');
2179 else if (buf[i] == 'U')
2180 build_string(buf, &bufpos, upass, 0, ' ');
2181 else if (buf[i] == 'A')
2182 build_string(buf, &bufpos, apass, 0, ' ');
2190 /** Simple function to invalidate bans
2192 * This function sets all bans as being valid.
2194 * @param chan The channel to operate on.
2197 mode_ban_invalidate(struct Channel *chan)
2199 struct Membership *member;
2201 for (member = chan->members; member; member = member->next_member)
2202 ClearBanValid(member);
2205 /** Simple function to drop invite structures
2207 * Remove all the invites on the channel.
2209 * @param chan Channel to remove invites from.
2213 mode_invite_clear(struct Channel *chan)
2215 while (chan->invites)
2216 del_invite(chan->invites->value.cptr, chan);
2219 /* What we've done for mode_parse so far... */
2220 #define DONE_LIMIT 0x01 /**< We've set the limit */
2221 #define DONE_KEY 0x02 /**< We've set the key */
2222 #define DONE_BANLIST 0x04 /**< We've sent the ban list */
2223 #define DONE_NOTOPER 0x08 /**< We've sent a "Not oper" error */
2224 #define DONE_BANCLEAN 0x10 /**< We've cleaned bans... */
2225 #define DONE_UPASS 0x20 /**< We've set user pass */
2226 #define DONE_APASS 0x40 /**< We've set admin pass */
2229 struct ModeBuf *mbuf;
2230 struct Client *cptr;
2231 struct Client *sptr;
2232 struct Channel *chptr;
2233 struct Membership *member;
2244 struct Ban banlist[MAXPARA];
2247 struct Client *client;
2248 } cli_change[MAXPARA];
2251 /** Helper function to send "Not oper" or "Not member" messages
2252 * Here's a helper function to deal with sending along "Not oper" or
2253 * "Not member" messages
2255 * @param state Parsing State object
2258 send_notoper(struct ParseState *state)
2260 if (state->done & DONE_NOTOPER)
2263 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
2264 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
2266 state->done |= DONE_NOTOPER;
2270 * Helper function to convert limits
2272 * @param state Parsing state object.
2276 mode_parse_limit(struct ParseState *state, int *flag_p)
2278 unsigned int t_limit;
2280 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
2281 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2284 if (state->parc <= 0) { /* warn if not enough args */
2285 if (MyUser(state->sptr))
2286 need_more_params(state->sptr, "MODE +l");
2290 t_limit = strtoul(state->parv[state->args_used++], 0, 10); /* grab arg */
2294 if ((int)t_limit<0) /* don't permit a negative limit */
2297 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2298 (!t_limit || t_limit == state->chptr->mode.limit))
2301 t_limit = state->chptr->mode.limit;
2303 /* If they're not an oper, they can't change modes */
2304 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2305 send_notoper(state);
2309 /* Can't remove a limit that's not there */
2310 if (state->dir == MODE_DEL && !state->chptr->mode.limit)
2313 /* Skip if this is a burst and a lower limit than this is set already */
2314 if ((state->flags & MODE_PARSE_BURST) &&
2315 (state->chptr->mode.mode & flag_p[0]) &&
2316 (state->chptr->mode.limit < t_limit))
2319 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
2321 state->done |= DONE_LIMIT;
2326 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
2328 if (state->flags & MODE_PARSE_SET) { /* set the limit */
2329 if (state->dir & MODE_ADD) {
2330 state->chptr->mode.mode |= flag_p[0];
2331 state->chptr->mode.limit = t_limit;
2333 state->chptr->mode.mode &= ~flag_p[0];
2334 state->chptr->mode.limit = 0;
2340 * Helper function to convert keys
2343 mode_parse_key(struct ParseState *state, int *flag_p)
2348 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2351 if (state->parc <= 0) { /* warn if not enough args */
2352 if (MyUser(state->sptr))
2353 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2358 t_str = state->parv[state->args_used++]; /* grab arg */
2362 /* If they're not an oper, they can't change modes */
2363 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2364 send_notoper(state);
2368 if (state->done & DONE_KEY) /* allow key to be set only once */
2370 state->done |= DONE_KEY;
2374 /* clean up the key string */
2376 while (*s > ' ' && *s != ':' && *s != ',' && t_len--)
2380 if (!*t_str) { /* warn if empty */
2381 if (MyUser(state->sptr))
2382 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2390 /* Skip if this is a burst, we have a key already and the new key is
2391 * after the old one alphabetically */
2392 if ((state->flags & MODE_PARSE_BURST) &&
2393 *(state->chptr->mode.key) &&
2394 ircd_strcmp(state->chptr->mode.key, t_str) <= 0)
2397 /* can't add a key if one is set, nor can one remove the wrong key */
2398 if (!(state->flags & MODE_PARSE_FORCE))
2399 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2400 (state->dir == MODE_DEL &&
2401 ircd_strcmp(state->chptr->mode.key, t_str))) {
2402 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2406 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2407 !ircd_strcmp(state->chptr->mode.key, t_str))
2408 return; /* no key change */
2410 if (state->flags & MODE_PARSE_BOUNCE) {
2411 if (*state->chptr->mode.key) /* reset old key */
2412 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2413 state->chptr->mode.key, 0);
2414 else /* remove new bogus key */
2415 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2416 } else /* send new key */
2417 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2419 if (state->flags & MODE_PARSE_SET) {
2420 if (state->dir == MODE_ADD) /* set the new key */
2421 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2422 else /* remove the old key */
2423 *state->chptr->mode.key = '\0';
2428 * Helper function to convert user passes
2431 mode_parse_upass(struct ParseState *state, int *flag_p)
2436 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2439 if (state->parc <= 0) { /* warn if not enough args */
2440 if (MyUser(state->sptr))
2441 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +U" :
2446 t_str = state->parv[state->args_used++]; /* grab arg */
2450 /* If they're not an oper, they can't change modes */
2451 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2452 send_notoper(state);
2456 /* If a non-service user is trying to force it, refuse. */
2457 if (state->flags & MODE_PARSE_FORCE && MyUser(state->sptr)
2458 && !HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
2459 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2460 state->chptr->chname);
2464 /* If they are not the channel manager, they are not allowed to change it */
2465 if (MyUser(state->sptr) && !(state->flags & MODE_PARSE_FORCE || IsChannelManager(state->member))) {
2466 if (*state->chptr->mode.apass) {
2467 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2468 state->chptr->chname);
2469 } else if (TStime() - state->chptr->creationtime >= 171000) {
2470 send_reply(state->sptr, ERR_NOMANAGER_LONG, state->chptr->chname);
2472 send_reply(state->sptr, ERR_NOMANAGER_SHORT, state->chptr->chname);
2477 if (state->done & DONE_UPASS) /* allow upass to be set only once */
2479 state->done |= DONE_UPASS;
2481 t_len = PASSLEN + 1;
2483 /* clean up the upass string */
2485 while (*++s > ' ' && *s != ':' && --t_len)
2489 if (!*t_str) { /* warn if empty */
2490 if (MyUser(state->sptr))
2491 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +U" :
2499 if (!(state->flags & MODE_PARSE_FORCE)) {
2500 /* can't add the upass while apass is not set */
2501 if (state->dir == MODE_ADD && !*state->chptr->mode.apass) {
2502 send_reply(state->sptr, ERR_UPASSNOTSET, state->chptr->chname, state->chptr->chname);
2505 /* cannot set a +U password that is the same as +A */
2506 if (state->dir == MODE_ADD && !ircd_strcmp(state->chptr->mode.apass, t_str)) {
2507 send_reply(state->sptr, ERR_UPASS_SAME_APASS, state->chptr->chname);
2510 /* can't add a upass if one is set, nor can one remove the wrong upass */
2511 if ((state->dir == MODE_ADD && *state->chptr->mode.upass) ||
2512 (state->dir == MODE_DEL &&
2513 ircd_strcmp(state->chptr->mode.upass, t_str))) {
2514 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2519 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2520 !ircd_strcmp(state->chptr->mode.upass, t_str))
2521 return; /* no upass change */
2523 if (state->flags & MODE_PARSE_BOUNCE) {
2524 if (*state->chptr->mode.upass) /* reset old upass */
2525 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2526 state->chptr->mode.upass, 0);
2527 else /* remove new bogus upass */
2528 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2529 } else /* send new upass */
2530 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2532 if (state->flags & MODE_PARSE_SET) {
2533 if (state->dir == MODE_ADD) /* set the new upass */
2534 ircd_strncpy(state->chptr->mode.upass, t_str, PASSLEN);
2535 else /* remove the old upass */
2536 *state->chptr->mode.upass = '\0';
2541 * Helper function to convert admin passes
2544 mode_parse_apass(struct ParseState *state, int *flag_p)
2546 struct Membership *memb;
2550 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2553 if (state->parc <= 0) { /* warn if not enough args */
2554 if (MyUser(state->sptr))
2555 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2560 t_str = state->parv[state->args_used++]; /* grab arg */
2564 /* If they're not an oper, they can't change modes */
2565 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2566 send_notoper(state);
2570 /* If a non-service user is trying to force it, refuse. */
2571 if (state->flags & MODE_PARSE_FORCE && MyUser(state->sptr)
2572 && !HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
2573 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2574 state->chptr->chname);
2578 /* Don't allow to change the Apass if the channel is older than 48 hours. */
2579 if (TStime() - state->chptr->creationtime >= 172800 && !IsAnOper(state->sptr)) {
2580 send_reply(state->sptr, ERR_CHANSECURED, state->chptr->chname);
2584 /* If they are not the channel manager, they are not allowed to change it */
2585 if (MyUser(state->sptr) && !(state->flags & MODE_PARSE_FORCE || IsChannelManager(state->member))) {
2586 if (*state->chptr->mode.apass) {
2587 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2588 state->chptr->chname);
2589 } else if (TStime() - state->chptr->creationtime >= 171000) {
2590 send_reply(state->sptr, ERR_NOMANAGER_LONG, state->chptr->chname);
2592 send_reply(state->sptr, ERR_NOMANAGER_SHORT, state->chptr->chname);
2597 if (state->done & DONE_APASS) /* allow apass to be set only once */
2599 state->done |= DONE_APASS;
2601 t_len = PASSLEN + 1;
2603 /* clean up the apass string */
2605 while (*++s > ' ' && *s != ':' && --t_len)
2609 if (!*t_str) { /* warn if empty */
2610 if (MyUser(state->sptr))
2611 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2619 if (!(state->flags & MODE_PARSE_FORCE)) {
2620 /* can't remove the apass while upass is still set */
2621 if (state->dir == MODE_DEL && *state->chptr->mode.upass) {
2622 send_reply(state->sptr, ERR_UPASSSET, state->chptr->chname, state->chptr->chname);
2625 /* can't add an apass if one is set, nor can one remove the wrong apass */
2626 if ((state->dir == MODE_ADD && *state->chptr->mode.apass) ||
2627 (state->dir == MODE_DEL && ircd_strcmp(state->chptr->mode.apass, t_str))) {
2628 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2633 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2634 !ircd_strcmp(state->chptr->mode.apass, t_str))
2635 return; /* no apass change */
2637 if (state->flags & MODE_PARSE_BOUNCE) {
2638 if (*state->chptr->mode.apass) /* reset old apass */
2639 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2640 state->chptr->mode.apass, 0);
2641 else /* remove new bogus apass */
2642 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2643 } else /* send new apass */
2644 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2646 if (state->flags & MODE_PARSE_SET) {
2647 if (state->dir == MODE_ADD) { /* set the new apass */
2648 /* Make it VERY clear to the user that this is a one-time password */
2649 ircd_strncpy(state->chptr->mode.apass, t_str, PASSLEN);
2650 if (MyUser(state->sptr)) {
2651 send_reply(state->sptr, RPL_APASSWARN_SET, state->chptr->mode.apass);
2652 send_reply(state->sptr, RPL_APASSWARN_SECRET, state->chptr->chname,
2653 state->chptr->mode.apass);
2655 /* Give the channel manager level 0 ops. */
2656 if (!(state->flags & MODE_PARSE_FORCE) && IsChannelManager(state->member))
2657 SetOpLevel(state->member, 0);
2658 } else { /* remove the old apass */
2659 *state->chptr->mode.apass = '\0';
2660 if (MyUser(state->sptr))
2661 send_reply(state->sptr, RPL_APASSWARN_CLEAR);
2662 /* Revert everyone to MAXOPLEVEL. */
2663 for (memb = state->chptr->members; memb; memb = memb->next_member) {
2664 if (memb->status & MODE_CHANOP)
2665 memb->oplevel = MAXOPLEVEL;
2671 /** Compare one ban's extent to another.
2672 * This works very similarly to mmatch() but it knows about CIDR masks
2673 * and ban exceptions. If both bans are CIDR-based, compare their
2674 * address bits; otherwise, use mmatch().
2675 * @param[in] old_ban One ban.
2676 * @param[in] new_ban Another ban.
2677 * @return Zero if \a old_ban is a superset of \a new_ban, non-zero otherwise.
2680 bmatch(struct Ban *old_ban, struct Ban *new_ban)
2683 assert(old_ban != NULL);
2684 assert(new_ban != NULL);
2685 /* A ban is never treated as a superset of an exception. */
2686 if (!(old_ban->flags & BAN_EXCEPTION)
2687 && (new_ban->flags & BAN_EXCEPTION))
2689 /* If either is not an address mask, match the text masks. */
2690 if ((old_ban->flags & new_ban->flags & BAN_IPMASK) == 0)
2691 return mmatch(old_ban->banstr, new_ban->banstr);
2692 /* If the old ban has a longer prefix than new, it cannot be a superset. */
2693 if (old_ban->addrbits > new_ban->addrbits)
2695 /* Compare the masks before the hostname part. */
2696 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '\0';
2697 res = mmatch(old_ban->banstr, new_ban->banstr);
2698 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '@';
2701 /* Compare the addresses. */
2702 return !ipmask_check(&new_ban->address, &old_ban->address, old_ban->addrbits);
2705 /** Add a ban from a ban list and mark bans that should be removed
2706 * because they overlap.
2708 * There are three invariants for a ban list. First, no ban may be
2709 * more specific than another ban. Second, no exception may be more
2710 * specific than another exception. Finally, no ban may be more
2711 * specific than any exception.
2713 * @param[in,out] banlist Pointer to head of list.
2714 * @param[in] newban Ban (or exception) to add (or remove).
2715 * @param[in] do_free If non-zero, free \a newban on failure.
2716 * @return Zero if \a newban could be applied, non-zero if not.
2718 int apply_ban(struct Ban **banlist, struct Ban *newban, int do_free)
2723 assert(newban->flags & (BAN_ADD|BAN_DEL));
2724 if (newban->flags & BAN_ADD) {
2726 /* If a less specific entry is found, fail. */
2727 for (ban = *banlist; ban; ban = ban->next) {
2728 if (!bmatch(ban, newban)) {
2733 if (!(ban->flags & (BAN_OVERLAPPED|BAN_DEL))) {
2735 totlen += strlen(ban->banstr);
2738 /* Mark more specific entries and add this one to the end of the list. */
2739 while ((ban = *banlist) != NULL) {
2740 if (!bmatch(newban, ban)) {
2741 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
2743 banlist = &ban->next;
2747 } else if (newban->flags & BAN_DEL) {
2748 size_t remove_count = 0;
2749 /* Mark more specific entries. */
2750 for (ban = *banlist; ban; ban = ban->next) {
2751 if (!bmatch(newban, ban)) {
2752 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
2758 /* If no matches were found, fail. */
2769 * Helper function to convert bans
2772 mode_parse_ban(struct ParseState *state, int *flag_p)
2775 struct Ban *ban, *newban;
2777 if (state->parc <= 0) { /* Not enough args, send ban list */
2778 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2779 send_ban_list(state->sptr, state->chptr);
2780 state->done |= DONE_BANLIST;
2786 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2789 t_str = state->parv[state->args_used++]; /* grab arg */
2793 /* If they're not an oper, they can't change modes */
2794 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2795 send_notoper(state);
2799 if ((s = strchr(t_str, ' ')))
2802 if (!*t_str || *t_str == ':') { /* warn if empty */
2803 if (MyUser(state->sptr))
2804 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2809 /* Clear all ADD/DEL/OVERLAPPED flags from ban list. */
2810 if (!(state->done & DONE_BANCLEAN)) {
2811 for (ban = state->chptr->banlist; ban; ban = ban->next)
2812 ban->flags &= ~(BAN_ADD | BAN_DEL | BAN_OVERLAPPED);
2813 state->done |= DONE_BANCLEAN;
2816 /* remember the ban for the moment... */
2817 newban = state->banlist + (state->numbans++);
2819 newban->flags = ((state->dir == MODE_ADD) ? BAN_ADD : BAN_DEL)
2820 | (*flag_p == MODE_BAN ? 0 : BAN_EXCEPTION);
2821 set_ban_mask(newban, collapse(pretty_mask(t_str)));
2822 ircd_strncpy(newban->who, cli_name(state->sptr), HOSTLEN);
2823 newban->when = TStime();
2824 apply_ban(&state->chptr->banlist, newban, 0);
2828 * This is the bottom half of the ban processor
2831 mode_process_bans(struct ParseState *state)
2833 struct Ban *ban, *newban, *prevban, *nextban;
2839 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2841 banlen = strlen(ban->banstr);
2843 nextban = ban->next;
2845 if ((ban->flags & (BAN_DEL | BAN_ADD)) == (BAN_DEL | BAN_ADD)) {
2847 prevban->next = 0; /* Break the list; ban isn't a real ban */
2849 state->chptr->banlist = 0;
2855 } else if (ban->flags & BAN_DEL) { /* Deleted a ban? */
2857 DupString(bandup, ban->banstr);
2858 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2861 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2862 if (prevban) /* clip it out of the list... */
2863 prevban->next = ban->next;
2865 state->chptr->banlist = ban->next;
2872 continue; /* next ban; keep prevban like it is */
2874 ban->flags &= BAN_IPMASK; /* unset other flags */
2875 } else if (ban->flags & BAN_ADD) { /* adding a ban? */
2877 prevban->next = 0; /* Break the list; ban isn't a real ban */
2879 state->chptr->banlist = 0;
2881 /* If we're supposed to ignore it, do so. */
2882 if (ban->flags & BAN_OVERLAPPED &&
2883 !(state->flags & MODE_PARSE_BOUNCE)) {
2887 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2888 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2889 count > feature_int(FEAT_MAXBANS))) {
2890 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2896 /* add the ban to the buffer */
2897 DupString(bandup, ban->banstr);
2898 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2901 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2902 newban = make_ban(ban->banstr);
2903 strcpy(newban->who, ban->who);
2904 newban->when = ban->when;
2905 newban->flags = ban->flags & BAN_IPMASK;
2907 newban->next = state->chptr->banlist; /* and link it in */
2908 state->chptr->banlist = newban;
2917 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2919 if (changed) /* if we changed the ban list, we must invalidate the bans */
2920 mode_ban_invalidate(state->chptr);
2924 * Helper function to process client changes
2927 mode_parse_client(struct ParseState *state, int *flag_p)
2930 struct Client *acptr;
2933 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2936 if (state->parc <= 0) /* return if not enough args */
2939 t_str = state->parv[state->args_used++]; /* grab arg */
2943 /* If they're not an oper, they can't change modes */
2944 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2945 send_notoper(state);
2949 if (MyUser(state->sptr)) /* find client we're manipulating */
2950 acptr = find_chasing(state->sptr, t_str, NULL);
2952 acptr = findNUser(t_str);
2955 return; /* find_chasing() already reported an error to the user */
2957 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2958 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2959 state->cli_change[i].flag & flag_p[0]))
2960 break; /* found a slot */
2962 /* Store what we're doing to them */
2963 state->cli_change[i].flag = state->dir | flag_p[0];
2964 state->cli_change[i].client = acptr;
2968 * Helper function to process the changed client list
2971 mode_process_clients(struct ParseState *state)
2974 struct Membership *member;
2976 for (i = 0; state->cli_change[i].flag; i++) {
2977 assert(0 != state->cli_change[i].client);
2979 /* look up member link */
2980 if (!(member = find_member_link(state->chptr,
2981 state->cli_change[i].client)) ||
2982 (MyUser(state->sptr) && IsZombie(member))) {
2983 if (MyUser(state->sptr))
2984 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2985 cli_name(state->cli_change[i].client),
2986 state->chptr->chname);
2990 if ((state->cli_change[i].flag & MODE_ADD &&
2991 (state->cli_change[i].flag & member->status)) ||
2992 (state->cli_change[i].flag & MODE_DEL &&
2993 !(state->cli_change[i].flag & member->status)))
2994 continue; /* no change made, don't do anything */
2996 /* see if the deop is allowed */
2997 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2998 (MODE_DEL | MODE_CHANOP)) {
2999 /* prevent +k users from being deopped */
3000 if (IsChannelService(state->cli_change[i].client)) {
3001 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
3002 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
3004 (IsServer(state->sptr) ? cli_name(state->sptr) :
3005 cli_name((cli_user(state->sptr))->server)));
3007 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
3008 send_reply(state->sptr, ERR_ISCHANSERVICE,
3009 cli_name(state->cli_change[i].client),
3010 state->chptr->chname);
3015 /* check deop for local user */
3016 if (MyUser(state->sptr)) {
3018 /* don't allow local opers to be deopped on local channels */
3019 if (state->cli_change[i].client != state->sptr &&
3020 IsLocalChannel(state->chptr->chname) &&
3021 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
3022 send_reply(state->sptr, ERR_ISOPERLCHAN,
3023 cli_name(state->cli_change[i].client),
3024 state->chptr->chname);
3028 /* don't allow to deop members with an op level that is <= our own level */
3029 if (state->sptr != state->cli_change[i].client /* but allow to deop oneself */
3030 && state->chptr->mode.apass[0]
3032 && OpLevel(member) <= OpLevel(state->member)) {
3033 int equal = (OpLevel(member) == OpLevel(state->member));
3034 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
3035 cli_name(state->cli_change[i].client),
3036 state->chptr->chname,
3037 OpLevel(state->member), OpLevel(member),
3038 "deop", equal ? "the same" : "a higher");
3044 /* set op-level of member being opped */
3045 if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
3046 (MODE_ADD | MODE_CHANOP)) {
3047 /* If being opped by an outsider, get oplevel 0 for an apass
3048 * channel, else MAXOPLEVEL.
3049 * Otherwise, if not an apass channel, or state->member has
3050 * MAXOPLEVEL, get oplevel MAXOPLEVEL.
3051 * Otherwise, get state->member's oplevel+1.
3054 SetOpLevel(member, state->chptr->mode.apass[0] ? 0 : MAXOPLEVEL);
3055 else if (!state->chptr->mode.apass[0] || OpLevel(state->member) == MAXOPLEVEL)
3056 SetOpLevel(member, MAXOPLEVEL);
3058 SetOpLevel(member, OpLevel(state->member) + 1);
3061 /* actually effect the change */
3062 if (state->flags & MODE_PARSE_SET) {
3063 if (state->cli_change[i].flag & MODE_ADD) {
3064 if (IsDelayedJoin(member))
3065 RevealDelayedJoin(member);
3066 member->status |= (state->cli_change[i].flag &
3067 (MODE_CHANOP | MODE_VOICE));
3068 if (state->cli_change[i].flag & MODE_CHANOP)
3069 ClearDeopped(member);
3071 member->status &= ~(state->cli_change[i].flag &
3072 (MODE_CHANOP | MODE_VOICE));
3075 /* accumulate the change */
3076 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
3077 state->cli_change[i].client);
3078 } /* for (i = 0; state->cli_change[i].flags; i++) */
3082 * Helper function to process the simple modes
3085 mode_parse_mode(struct ParseState *state, int *flag_p)
3087 /* If they're not an oper, they can't change modes */
3088 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3089 send_notoper(state);
3096 if (state->dir == MODE_ADD) {
3097 state->add |= flag_p[0];
3098 state->del &= ~flag_p[0];
3100 if (flag_p[0] & MODE_SECRET) {
3101 state->add &= ~MODE_PRIVATE;
3102 state->del |= MODE_PRIVATE;
3103 } else if (flag_p[0] & MODE_PRIVATE) {
3104 state->add &= ~MODE_SECRET;
3105 state->del |= MODE_SECRET;
3107 if (flag_p[0] & MODE_DELJOINS) {
3108 state->add &= ~MODE_WASDELJOINS;
3109 state->del |= MODE_WASDELJOINS;
3112 state->add &= ~flag_p[0];
3113 state->del |= flag_p[0];
3116 assert(0 == (state->add & state->del));
3117 assert((MODE_SECRET | MODE_PRIVATE) !=
3118 (state->add & (MODE_SECRET | MODE_PRIVATE)));
3122 * This routine is intended to parse MODE or OPMODE commands and effect the
3123 * changes (or just build the bounce buffer). We pass the starting offset
3127 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
3128 struct Channel *chptr, int parc, char *parv[], unsigned int flags,
3129 struct Membership* member)
3131 static int chan_flags[] = {
3136 MODE_MODERATED, 'm',
3137 MODE_TOPICLIMIT, 't',
3138 MODE_INVITEONLY, 'i',
3139 MODE_NOPRIVMSGS, 'n',
3153 unsigned int t_mode;
3155 struct ParseState state;
3166 state.chptr = chptr;
3167 state.member = member;
3170 state.flags = flags;
3171 state.dir = MODE_ADD;
3175 state.args_used = 0;
3176 state.max_args = MAXMODEPARAMS;
3179 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
3180 state.banlist[i].next = 0;
3181 state.banlist[i].who[0] = '\0';
3182 state.banlist[i].when = 0;
3183 state.banlist[i].flags = 0;
3184 state.cli_change[i].flag = 0;
3185 state.cli_change[i].client = 0;
3188 modestr = state.parv[state.args_used++];
3192 for (; *modestr; modestr++) {
3193 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
3194 if (flag_p[1] == *modestr)
3197 if (!flag_p[0]) { /* didn't find it? complain and continue */
3198 if (MyUser(state.sptr))
3199 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
3204 case '+': /* switch direction to MODE_ADD */
3205 case '-': /* switch direction to MODE_DEL */
3206 state.dir = flag_p[0];
3209 case 'l': /* deal with limits */
3210 mode_parse_limit(&state, flag_p);
3213 case 'k': /* deal with keys */
3214 mode_parse_key(&state, flag_p);
3217 case 'A': /* deal with Admin passes */
3218 if (feature_bool(FEAT_OPLEVELS))
3219 mode_parse_apass(&state, flag_p);
3222 case 'U': /* deal with user passes */
3223 if (feature_bool(FEAT_OPLEVELS))
3224 mode_parse_upass(&state, flag_p);
3227 case 'b': /* deal with bans */
3228 mode_parse_ban(&state, flag_p);
3231 case 'o': /* deal with ops/voice */
3233 mode_parse_client(&state, flag_p);
3236 default: /* deal with other modes */
3237 mode_parse_mode(&state, flag_p);
3239 } /* switch (*modestr) */
3240 } /* for (; *modestr; modestr++) */
3242 if (state.flags & MODE_PARSE_BURST)
3243 break; /* don't interpret any more arguments */
3245 if (state.parc > 0) { /* process next argument in string */
3246 modestr = state.parv[state.args_used++];
3250 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
3253 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
3254 break; /* we're then going to bounce the mode! */
3256 recv_ts = atoi(modestr);
3258 if (recv_ts && recv_ts < state.chptr->creationtime)
3259 state.chptr->creationtime = recv_ts; /* respect earlier TS */
3261 break; /* break out of while loop */
3262 } else if (state.flags & MODE_PARSE_STRICT ||
3263 (MyUser(state.sptr) && state.max_args <= 0)) {
3264 state.parc++; /* we didn't actually gobble the argument */
3266 break; /* break out of while loop */
3269 } /* while (*modestr) */
3272 * the rest of the function finishes building resultant MODEs; if the
3273 * origin isn't a member or an oper, skip it.
3275 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3276 return state.args_used; /* tell our parent how many args we gobbled */
3278 t_mode = state.chptr->mode.mode;
3280 if (state.del & t_mode) { /* delete any modes to be deleted... */
3281 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
3283 t_mode &= ~state.del;
3285 if (state.add & ~t_mode) { /* add any modes to be added... */
3286 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
3288 t_mode |= state.add;
3291 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
3292 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
3293 !(t_mode & MODE_INVITEONLY))
3294 mode_invite_clear(state.chptr);
3296 state.chptr->mode.mode = t_mode;
3299 if (state.flags & MODE_PARSE_WIPEOUT) {
3300 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
3301 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
3302 state.chptr->mode.limit);
3303 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
3304 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
3305 state.chptr->mode.key, 0);
3306 if (*state.chptr->mode.upass && !(state.done & DONE_UPASS))
3307 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_UPASS,
3308 state.chptr->mode.upass, 0);
3309 if (*state.chptr->mode.apass && !(state.done & DONE_APASS))
3310 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_APASS,
3311 state.chptr->mode.apass, 0);
3314 if (state.done & DONE_BANCLEAN) /* process bans */
3315 mode_process_bans(&state);
3317 /* process client changes */
3318 if (state.cli_change[0].flag)
3319 mode_process_clients(&state);
3321 return state.args_used; /* tell our parent how many args we gobbled */
3325 * Initialize a join buffer
3328 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
3329 struct Client *connect, unsigned int type, char *comment,
3335 assert(0 != source);
3336 assert(0 != connect);
3338 jbuf->jb_source = source; /* just initialize struct JoinBuf */
3339 jbuf->jb_connect = connect;
3340 jbuf->jb_type = type;
3341 jbuf->jb_comment = comment;
3342 jbuf->jb_create = create;
3344 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
3345 type == JOINBUF_TYPE_PART ||
3346 type == JOINBUF_TYPE_PARTALL) ?
3347 STARTJOINLEN : STARTCREATELEN) +
3348 (comment ? strlen(comment) + 2 : 0));
3350 for (i = 0; i < MAXJOINARGS; i++)
3351 jbuf->jb_channels[i] = 0;
3355 * Add a channel to the join buffer
3358 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
3366 if (jbuf->jb_type == JOINBUF_TYPE_JOIN)
3367 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
3372 is_local = IsLocalChannel(chan->chname);
3374 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
3375 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
3376 struct Membership *member = find_member_link(chan, jbuf->jb_source);
3377 if (IsUserParting(member))
3379 SetUserParting(member);
3381 /* Send notification to channel */
3382 if (!(flags & (CHFL_ZOMBIE | CHFL_DELAYED)))
3383 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL, 0,
3384 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3385 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3386 else if (MyUser(jbuf->jb_source))
3387 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
3388 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3389 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3390 /* XXX: Shouldn't we send a PART here anyway? */
3391 /* to users on the channel? Why? From their POV, the user isn't on
3392 * the channel anymore anyway. We don't send to servers until below,
3393 * when we gang all the channel parts together. Note that this is
3394 * exactly the same logic, albeit somewhat more concise, as was in
3395 * the original m_part.c */
3397 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3398 is_local) /* got to remove user here */
3399 remove_user_from_channel(jbuf->jb_source, chan);
3401 int oplevel = !chan->mode.apass[0] ? MAXOPLEVEL
3402 : (flags & CHFL_CHANNEL_MANAGER) ? 0
3404 /* Add user to channel */
3405 if ((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))
3406 add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, oplevel);
3408 add_user_to_channel(chan, jbuf->jb_source, flags, oplevel);
3410 /* send notification to all servers */
3411 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local)
3413 if (flags & CHFL_CHANOP)
3414 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
3415 "%u:%H %Tu", oplevel, chan, chan->creationtime);
3417 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
3418 "%H %Tu", chan, chan->creationtime);
3421 if (!((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))) {
3422 /* Send the notification to the channel */
3423 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, 0, "%H", chan);
3425 /* send an op, too, if needed */
3426 if (flags & CHFL_CHANOP && (oplevel < MAXOPLEVEL || !MyUser(jbuf->jb_source)))
3427 sendcmdto_channel_butserv_butone((chan->mode.apass[0] ? &me : jbuf->jb_source),
3428 CMD_MODE, chan, NULL, 0, "%H +o %C",
3429 chan, jbuf->jb_source);
3430 } else if (MyUser(jbuf->jb_source))
3431 sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan);
3434 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3435 jbuf->jb_type == JOINBUF_TYPE_JOIN || is_local)
3436 return; /* don't send to remote */
3438 /* figure out if channel name will cause buffer to be overflowed */
3439 len = chan ? strlen(chan->chname) + 1 : 2;
3440 if (jbuf->jb_strlen + len > BUFSIZE)
3441 joinbuf_flush(jbuf);
3443 /* add channel to list of channels to send and update counts */
3444 jbuf->jb_channels[jbuf->jb_count++] = chan;
3445 jbuf->jb_strlen += len;
3447 /* if we've used up all slots, flush */
3448 if (jbuf->jb_count >= MAXJOINARGS)
3449 joinbuf_flush(jbuf);
3453 * Flush the channel list to remote servers
3456 joinbuf_flush(struct JoinBuf *jbuf)
3458 char chanlist[BUFSIZE];
3462 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3463 jbuf->jb_type == JOINBUF_TYPE_JOIN)
3464 return 0; /* no joins to process */
3466 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
3467 build_string(chanlist, &chanlist_i,
3468 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
3469 i == 0 ? '\0' : ',');
3470 if (JOINBUF_TYPE_PART == jbuf->jb_type)
3471 /* Remove user from channel */
3472 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
3474 jbuf->jb_channels[i] = 0; /* mark slot empty */
3477 jbuf->jb_count = 0; /* reset base counters */
3478 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
3479 STARTJOINLEN : STARTCREATELEN) +
3480 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
3482 /* and send the appropriate command */
3483 switch (jbuf->jb_type) {
3484 case JOINBUF_TYPE_CREATE:
3485 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
3486 "%s %Tu", chanlist, jbuf->jb_create);
3489 case JOINBUF_TYPE_PART:
3490 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
3491 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,
3499 /* Returns TRUE (1) if client is invited, FALSE (0) if not */
3500 int IsInvited(struct Client* cptr, const void* chptr)
3504 for (lp = (cli_user(cptr))->invited; lp; lp = lp->next)
3505 if (lp->value.chptr == chptr)
3510 /* RevealDelayedJoin: sends a join for a hidden user */
3512 void RevealDelayedJoin(struct Membership *member)
3514 ClearDelayedJoin(member);
3515 sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, 0, ":%H",
3517 CheckDelayedJoins(member->channel);
3520 /* CheckDelayedJoins: checks and clear +d if necessary */
3522 void CheckDelayedJoins(struct Channel *chan)
3524 struct Membership *memb2;
3526 if (chan->mode.mode & MODE_WASDELJOINS) {
3527 for (memb2=chan->members;memb2;memb2=memb2->next_member)
3528 if (IsDelayedJoin(memb2))
3533 chan->mode.mode &= ~MODE_WASDELJOINS;
3534 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan, NULL, 0,