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);
262 * Also channels without Apass set need to be kept alive,
263 * otherwise Bad Guys(tm) would be able to takeover
264 * existing channels too easily, and then set an Apass!
265 * However, if a channel without Apass becomes empty
266 * then we try to be kind to them and remove possible
269 chptr->mode.mode &= ~MODE_INVITEONLY;
270 chptr->mode.limit = 0;
272 * We do NOT reset a possible key or bans because when
273 * the 'channel owners' can't get in because of a key
274 * or ban then apparently there was a fight/takeover
275 * on the channel and we want them to contact IRC opers
276 * who then will educate them on the use of Apass/Upass.
278 if (!chptr->mode.apass[0]) /* If no Apass, reset all modes. */
280 struct Ban *link, *next;
281 chptr->mode.mode = 0;
282 *chptr->mode.key = '\0';
283 while (chptr->invites)
284 del_invite(chptr->invites->value.cptr, chptr);
285 for (link = chptr->banlist; link; link = next) {
289 chptr->banlist = NULL;
291 if (TStime() - chptr->creationtime < 172800) /* Channel younger than 48 hours? */
292 schedule_destruct_event_1m(chptr); /* Get rid of it in approximately 4-5 minutes */
294 schedule_destruct_event_48h(chptr); /* Get rid of it in approximately 48 hours */
299 /** Destroy an empty channel
300 * This function destroys an empty channel, removing it from hashtables,
301 * and removing any resources it may have consumed.
303 * @param chptr The channel to destroy
305 * @returns 0 (success)
307 * FIXME: Change to return void, this function never fails.
309 int destruct_channel(struct Channel* chptr)
311 struct Ban *ban, *next;
313 assert(0 == chptr->members);
316 * Now, find all invite links from channel structure
318 while (chptr->invites)
319 del_invite(chptr->invites->value.cptr, chptr);
321 for (ban = chptr->banlist; ban; ban = next)
327 chptr->prev->next = chptr->next;
329 GlobalChannelList = chptr->next;
331 chptr->next->prev = chptr->prev;
333 --UserStats.channels;
335 * make sure that channel actually got removed from hash table
337 assert(chptr->hnext == chptr);
342 /** returns Membership * if a person is joined and not a zombie
344 * @param chptr Channel
345 * @returns pointer to the client's struct Membership * on the channel if that
346 * user is a full member of the channel, or NULL otherwise.
348 * @see find_member_link()
350 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
352 struct Membership* member;
355 member = find_member_link(chptr, cptr);
356 return (member && !IsZombie(member)) ? member : 0;
359 /** Searches for a ban from a ban list that matches a user.
360 * @param[in] cptr The client to test.
361 * @param[in] banlist The list of bans to test.
362 * @return Pointer to a matching ban, or NULL if none exit.
364 struct Ban *find_ban(struct Client *cptr, struct Ban *banlist)
366 char nu[NICKLEN + USERLEN + 2];
367 char tmphost[HOSTLEN + 1];
368 char iphost[SOCKIPLEN + 1];
373 /* Build nick!user and alternate host names. */
374 ircd_snprintf(0, nu, sizeof(nu), "%s!%s",
375 cli_name(cptr), cli_user(cptr)->username);
376 ircd_ntoa_r(iphost, &cli_ip(cptr));
377 if (!IsAccount(cptr))
379 else if (HasHiddenHost(cptr))
380 sr = cli_user(cptr)->realhost;
383 ircd_snprintf(0, tmphost, HOSTLEN, "%s.%s",
384 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
388 /* Walk through ban list. */
389 for (found = NULL; banlist; banlist = banlist->next) {
391 /* If we have found a positive ban already, only consider exceptions. */
392 if (found && !(banlist->flags & BAN_EXCEPTION))
394 /* Compare nick!user portion of ban. */
395 banlist->banstr[banlist->nu_len] = '\0';
396 res = match(banlist->banstr, nu);
397 banlist->banstr[banlist->nu_len] = '@';
400 /* Compare host portion of ban. */
401 hostmask = banlist->banstr + banlist->nu_len + 1;
402 if (!((banlist->flags & BAN_IPMASK)
403 && ipmask_check(&cli_ip(cptr), &banlist->address, banlist->addrbits))
404 && match(hostmask, cli_user(cptr)->host)
405 && !(sr && !match(hostmask, sr)))
407 /* If an exception matches, no ban can match. */
408 if (banlist->flags & BAN_EXCEPTION)
410 /* Otherwise, remember this ban but keep searching for an exception. */
417 * This function returns true if the user is banned on the said channel.
418 * This function will check the ban cache if applicable, otherwise will
419 * do the comparisons and cache the result.
421 * @param[in] member The Membership to test for banned-ness.
422 * @return Non-zero if the member is banned, zero if not.
424 static int is_banned(struct Membership* member)
426 if (IsBanValid(member))
427 return IsBanned(member);
430 if (find_ban(member->user, member->channel->banlist)) {
439 /** add a user to a channel.
440 * adds a user to a channel by adding another link to the channels member
443 * @param chptr The channel to add to.
444 * @param who The user to add.
445 * @param flags The flags the user gets initially.
446 * @param oplevel The oplevel the user starts with.
448 void add_user_to_channel(struct Channel* chptr, struct Client* who,
449 unsigned int flags, int oplevel)
456 struct Membership* member = membershipFreeList;
458 membershipFreeList = member->next_member;
460 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
461 ++membershipAllocCount;
466 member->channel = chptr;
467 member->status = flags;
468 SetOpLevel(member, oplevel);
470 member->next_member = chptr->members;
471 if (member->next_member)
472 member->next_member->prev_member = member;
473 member->prev_member = 0;
474 chptr->members = member;
476 member->next_channel = (cli_user(who))->channel;
477 if (member->next_channel)
478 member->next_channel->prev_channel = member;
479 member->prev_channel = 0;
480 (cli_user(who))->channel = member;
482 if (chptr->destruct_event)
483 remove_destruct_event(chptr);
485 ++((cli_user(who))->joined);
489 /** Remove a person from a channel, given their Membership*
491 * @param member A member of a channel.
493 * @returns true if there are more people in the channel.
495 static int remove_member_from_channel(struct Membership* member)
497 struct Channel* chptr;
499 chptr = member->channel;
501 * unlink channel member list
503 if (member->next_member)
504 member->next_member->prev_member = member->prev_member;
505 if (member->prev_member)
506 member->prev_member->next_member = member->next_member;
508 member->channel->members = member->next_member;
511 * If this is the last delayed-join user, may have to clear WASDELJOINS.
513 if (IsDelayedJoin(member))
514 CheckDelayedJoins(chptr);
517 * unlink client channel list
519 if (member->next_channel)
520 member->next_channel->prev_channel = member->prev_channel;
521 if (member->prev_channel)
522 member->prev_channel->next_channel = member->next_channel;
524 (cli_user(member->user))->channel = member->next_channel;
526 --(cli_user(member->user))->joined;
528 member->next_member = membershipFreeList;
529 membershipFreeList = member;
531 return sub1_from_channel(chptr);
534 /** Check if all the remaining members on the channel are zombies
536 * @returns False if the channel has any non zombie members, True otherwise.
539 static int channel_all_zombies(struct Channel* chptr)
541 struct Membership* member;
543 for (member = chptr->members; member; member = member->next_member) {
544 if (!IsZombie(member))
551 /** Remove a user from a channel
552 * This is the generic entry point for removing a user from a channel, this
553 * function will remove the client from the channel, and destroy the channel
554 * if there are no more normal users left.
556 * @param cptr The client
557 * @param chptr The channel
559 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
562 struct Membership* member;
565 if ((member = find_member_link(chptr, cptr))) {
566 if (remove_member_from_channel(member)) {
567 if (channel_all_zombies(chptr)) {
569 * XXX - this looks dangerous but isn't if we got the referential
570 * integrity right for channels
572 while (remove_member_from_channel(chptr->members))
579 /** Remove a user from all channels they are on.
581 * This function removes a user from all channels they are on.
583 * @param cptr The client to remove.
585 void remove_user_from_all_channels(struct Client* cptr)
587 struct Membership* chan;
589 assert(0 != cli_user(cptr));
591 while ((chan = (cli_user(cptr))->channel))
592 remove_user_from_channel(cptr, chan->channel);
595 /** Check if this user is a legitimate chanop
597 * @param cptr Client to check
598 * @param chptr Channel to check
600 * @returns True if the user is a chanop (And not a zombie), False otherwise.
603 int is_chan_op(struct Client *cptr, struct Channel *chptr)
605 struct Membership* member;
607 if ((member = find_member_link(chptr, cptr)))
608 return (!IsZombie(member) && IsChanOp(member));
613 /** Check if a user is a Zombie on a specific channel.
615 * @param cptr The client to check.
616 * @param chptr The channel to check.
618 * @returns True if the client (cptr) is a zombie on the channel (chptr),
623 int is_zombie(struct Client *cptr, struct Channel *chptr)
625 struct Membership* member;
629 if ((member = find_member_link(chptr, cptr)))
630 return IsZombie(member);
634 /** Returns if a user has voice on a channel.
636 * @param cptr The client
637 * @param chptr The channel
639 * @returns True if the client (cptr) is voiced on (chptr) and is not a zombie.
642 int has_voice(struct Client* cptr, struct Channel* chptr)
644 struct Membership* member;
647 if ((member = find_member_link(chptr, cptr)))
648 return (!IsZombie(member) && HasVoice(member));
653 /** Can this member send to a channel
655 * A user can speak on a channel iff:
657 * <li> They didn't use the Apass to gain ops.
658 * <li> They are op'd or voice'd.
659 * <li> You aren't banned.
660 * <li> The channel isn't +m
661 * <li> The channel isn't +n or you are on the channel.
664 * This function will optionally reveal a user on a delayed join channel if
665 * they are allowed to send to the channel.
667 * @param member The membership of the user
668 * @param reveal If true, the user will be "revealed" on a delayed
671 * @returns True if the client can speak on the channel.
673 int member_can_send_to_channel(struct Membership* member, int reveal)
677 /* Discourage using the Apass to get op. They should use the upass. */
678 if (IsChannelManager(member) && member->channel->mode.apass[0])
681 if (IsVoicedOrOpped(member))
685 * If it's moderated, and you aren't a privileged user, you can't
688 if (member->channel->mode.mode & MODE_MODERATED)
690 /* If only logged in users may join and you're not one, you can't speak. */
691 if (member->channel->mode.mode & MODE_REGONLY && !IsAccount(member->user))
694 * If you're banned then you can't speak either.
695 * but because of the amount of CPU time that is_banned chews
696 * we only check it for our clients.
698 if (MyUser(member->user) && is_banned(member))
701 if (IsDelayedJoin(member) && reveal)
702 RevealDelayedJoin(member);
707 /** Check if a client can send to a channel.
709 * Has the added check over member_can_send_to_channel() of servers can
712 * @param cptr The client to check
713 * @param chptr The channel to check
714 * @param reveal If the user should be revealed (see
715 * member_can_send_to_channel())
717 * @returns true if the client is allowed to speak on the channel, false
720 * @see member_can_send_to_channel()
722 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr, int reveal)
724 struct Membership *member;
727 * Servers can always speak on channels.
732 member = find_channel_member(cptr, chptr);
735 * You can't speak if you're off channel, and it is +n (no external messages)
739 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED)) ||
740 ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(cptr)))
743 return !find_ban(cptr, chptr->banlist);
745 return member_can_send_to_channel(member, reveal);
748 /** Returns the name of a channel that prevents the user from changing nick.
749 * if a member and not (opped or voiced) and (banned or moderated), return
750 * the name of the first channel banned on.
752 * @param cptr The client
754 * @returns the name of the first channel banned on, or NULL if the user
757 const char* find_no_nickchange_channel(struct Client* cptr)
760 struct Membership* member;
761 for (member = (cli_user(cptr))->channel; member;
762 member = member->next_channel) {
763 if (!IsVoicedOrOpped(member) &&
764 (is_banned(member) ||
765 (member->channel->mode.mode & MODE_MODERATED)))
766 return member->channel->chname;
773 /** Fill mbuf/pbuf with modes from chptr
774 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
775 * with the parameters in pbuf as visible by cptr.
777 * This function will hide keys from non-op'd, non-server clients.
779 * @param cptr The client to generate the mode for.
780 * @param mbuf The buffer to write the modes into.
781 * @param pbuf The buffer to write the mode parameters into.
782 * @param buflen The length of the buffers.
783 * @param chptr The channel to get the modes from.
784 * @param member The membership of this client on this channel (or NULL
785 * if this client isn't on this channel)
788 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen,
789 struct Channel *chptr, struct Membership *member)
791 int previous_parameter = 0;
798 if (chptr->mode.mode & MODE_SECRET)
800 else if (chptr->mode.mode & MODE_PRIVATE)
802 if (chptr->mode.mode & MODE_MODERATED)
804 if (chptr->mode.mode & MODE_TOPICLIMIT)
806 if (chptr->mode.mode & MODE_INVITEONLY)
808 if (chptr->mode.mode & MODE_NOPRIVMSGS)
810 if (chptr->mode.mode & MODE_REGONLY)
812 if (chptr->mode.mode & MODE_DELJOINS)
814 else if (MyUser(cptr) && (chptr->mode.mode & MODE_WASDELJOINS))
816 if (chptr->mode.limit) {
818 ircd_snprintf(0, pbuf, buflen, "%u", chptr->mode.limit);
819 previous_parameter = 1;
822 if (*chptr->mode.key) {
824 if (previous_parameter)
826 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
827 strcat(pbuf, chptr->mode.key);
830 previous_parameter = 1;
832 if (*chptr->mode.apass) {
834 if (previous_parameter)
836 if (IsServer(cptr)) {
837 strcat(pbuf, chptr->mode.apass);
840 previous_parameter = 1;
842 if (*chptr->mode.upass) {
844 if (previous_parameter)
846 if (IsServer(cptr) || (member && IsChanOp(member) && OpLevel(member) == 0)) {
847 strcat(pbuf, chptr->mode.upass);
854 /** Compare two members oplevel
856 * @param mp1 Pointer to a pointer to a membership
857 * @param mp2 Pointer to a pointer to a membership
859 * @returns 0 if equal, -1 if mp1 is lower, +1 otherwise.
863 int compare_member_oplevel(const void *mp1, const void *mp2)
865 struct Membership const* member1 = *(struct Membership const**)mp1;
866 struct Membership const* member2 = *(struct Membership const**)mp2;
867 if (member1->oplevel == member2->oplevel)
869 return (member1->oplevel < member2->oplevel) ? -1 : 1;
872 /* send "cptr" a full list of the modes for channel chptr.
874 * Sends a BURST line to cptr, bursting all the modes for the channel.
876 * @param cptr Client pointer
877 * @param chptr Channel pointer
879 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
881 /* The order in which modes are generated is now mandatory */
882 static unsigned int current_flags[4] =
883 { 0, CHFL_VOICE, CHFL_CHANOP, CHFL_CHANOP | CHFL_VOICE };
889 struct Membership* member;
891 char modebuf[MODEBUFLEN];
892 char parabuf[MODEBUFLEN];
894 int number_of_ops = 0;
895 int opped_members_index = 0;
896 struct Membership** opped_members = NULL;
897 int last_oplevel = 0;
898 int feat_oplevels = (chptr->mode.apass[0]) != '\0';
903 if (IsLocalChannel(chptr->chname))
906 member = chptr->members;
907 lp2 = chptr->banlist;
909 *modebuf = *parabuf = '\0';
910 channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr, 0);
912 for (first = 1; full; first = 0) /* Loop for multiple messages */
914 full = 0; /* Assume by default we get it
915 all in one message */
917 /* (Continued) prefix: "<Y> B <channel> <TS>" */
918 /* is there any better way we can do this? */
919 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
920 chptr->creationtime);
922 if (first && modebuf[1]) /* Add simple modes (Aiklmnpstu)
925 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
926 msgq_append(&me, mb, " %s", modebuf);
929 msgq_append(&me, mb, " %s", parabuf);
933 * Attach nicks, comma separated " nick[:modes],nick[:modes],..."
935 * First find all opless members.
936 * Run 2 times over all members, to group the members with
937 * and without voice together.
938 * Then run 2 times over all opped members (which are ordered
939 * by op-level) to also group voice and non-voice together.
941 for (first = 1; flag_cnt < 4; new_mode = 1, ++flag_cnt)
945 if (flag_cnt < 2 && IsChanOp(member))
948 * The first loop (to find all non-voice/op), we count the ops.
949 * The second loop (to find all voiced non-ops), store the ops
950 * in a dynamic array.
955 opped_members[opped_members_index++] = member;
957 /* Only handle the members with the flags that we are interested in. */
958 if ((member->status & CHFL_VOICED_OR_OPPED) == current_flags[flag_cnt])
960 if (msgq_bufleft(mb) < NUMNICKLEN + 3 + MAXOPLEVELDIGITS)
961 /* The 3 + MAXOPLEVELDIGITS is a possible ",:v999". */
963 full = 1; /* Make sure we continue after
965 /* Ensure the new BURST line contains the current
966 * ":mode", except when there is no mode yet. */
967 new_mode = (flag_cnt > 0) ? 1 : 0;
968 break; /* Do not add this member to this message */
970 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
971 first = 0; /* From now on, use commas to add new nicks */
974 * Do we have a nick with a new mode ?
975 * Or are we starting a new BURST line?
980 * This means we are at the _first_ member that has only
981 * voice, or the first member that has only ops, or the
982 * first member that has voice and ops (so we get here
983 * at most three times, plus once for every start of
984 * a continued BURST line where only these modes is current.
985 * In the two cases where the current mode includes ops,
986 * we need to add the _absolute_ value of the oplevel to the mode.
988 char tbuf[3 + MAXOPLEVELDIGITS] = ":";
991 if (HasVoice(member)) /* flag_cnt == 1 or 3 */
993 if (IsChanOp(member)) /* flag_cnt == 2 or 3 */
995 /* append the absolute value of the oplevel */
997 loc += ircd_snprintf(0, tbuf + loc, sizeof(tbuf) - loc, "%u", last_oplevel = member->oplevel);
1002 msgq_append(&me, mb, tbuf);
1005 else if (feat_oplevels && flag_cnt > 1 && last_oplevel != member->oplevel)
1008 * This can't be the first member of a (continued) BURST
1009 * message because then either flag_cnt == 0 or new_mode == 1
1010 * Now we need to append the incremental value of the oplevel.
1012 char tbuf[2 + MAXOPLEVELDIGITS];
1013 ircd_snprintf(0, tbuf, sizeof(tbuf), ":%u", member->oplevel - last_oplevel);
1014 last_oplevel = member->oplevel;
1015 msgq_append(&me, mb, tbuf);
1018 /* Go to the next `member'. */
1020 member = member->next_member;
1022 member = opped_members[++opped_members_index];
1027 /* Point `member' at the start of the list again. */
1030 member = chptr->members;
1031 /* Now, after one loop, we know the number of ops and can
1032 * allocate the dynamic array with pointer to the ops. */
1033 opped_members = (struct Membership**)
1034 MyMalloc((number_of_ops + 1) * sizeof(struct Membership*));
1035 opped_members[number_of_ops] = NULL; /* Needed for loop termination */
1039 /* At the end of the second loop, sort the opped members with
1040 * increasing op-level, so that we will output them in the
1041 * correct order (and all op-level increments stay positive) */
1043 qsort(opped_members, number_of_ops,
1044 sizeof(struct Membership*), compare_member_oplevel);
1045 /* The third and fourth loop run only over the opped members. */
1046 member = opped_members[(opped_members_index = 0)];
1049 } /* loop over 0,+v,+o,+ov */
1053 /* Attach all bans, space separated " :%ban ban ..." */
1054 for (first = 2; lp2; lp2 = lp2->next)
1056 len = strlen(lp2->banstr);
1057 if (msgq_bufleft(mb) < len + 1 + first)
1058 /* The +1 stands for the added ' '.
1059 * The +first stands for the added ":%".
1065 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
1071 send_buffer(cptr, mb, 0); /* Send this message */
1073 } /* Continue when there was something
1074 that didn't fit (full==1) */
1076 MyFree(opped_members);
1077 if (feature_bool(FEAT_TOPIC_BURST) && (chptr->topic[0] != '\0'))
1078 sendcmdto_one(&me, CMD_TOPIC, cptr, "%H %Tu %Tu :%s", chptr,
1079 chptr->creationtime, chptr->topic_time, chptr->topic);
1082 /** Canonify a mask.
1085 * @author Carlo Wood (Run),
1088 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
1089 * When the user name or host name are too long (USERLEN and HOSTLEN
1090 * respectively) then they are cut off at the start with a '*'.
1092 * The following transformations are made:
1094 * 1) xxx -> nick!*@*
1095 * 2) xxx.xxx -> *!*\@host
1096 * 3) xxx\!yyy -> nick!user\@*
1097 * 4) xxx\@yyy -> *!user\@host
1098 * 5) xxx!yyy\@zzz -> nick!user\@host
1100 * @param mask The uncanonified mask.
1101 * @returns The updated mask in a static buffer.
1103 char *pretty_mask(char *mask)
1105 static char star[2] = { '*', 0 };
1106 static char retmask[NICKLEN + USERLEN + HOSTLEN + 3];
1107 char *last_dot = NULL;
1110 /* Case 1: default */
1115 /* Do a _single_ pass through the characters of the mask: */
1116 for (ptr = mask; *ptr; ++ptr)
1120 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1124 else if (*ptr == '@')
1126 /* Case 4: Found last '@' (without finding a '!' yet) */
1131 else if (*ptr == '.' || *ptr == ':')
1133 /* Case 2: Found character specific to IP or hostname (without
1134 * finding a '!' or '@' yet) */
1144 /* Case 4 or 5: Found last '@' */
1150 if (user == star && last_dot)
1160 char *nick_end = (user != star) ? user - 1 : ptr;
1161 if (nick_end - nick > NICKLEN)
1167 char *user_end = (host != star) ? host - 1 : ptr;
1168 if (user_end - user > USERLEN)
1170 user = user_end - USERLEN;
1175 if (host != star && ptr - host > HOSTLEN)
1177 host = ptr - HOSTLEN;
1180 ircd_snprintf(0, retmask, sizeof(retmask), "%s!%s@%s", nick, user, host);
1184 /** send a banlist to a client for a channel
1186 * @param cptr Client to send the banlist to.
1187 * @param chptr Channel whose banlist to send.
1189 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1196 for (lp = chptr->banlist; lp; lp = lp->next)
1197 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->banstr,
1200 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1203 /** Remove bells and commas from channel name
1205 * @param cn Channel name to clean, modified in place.
1207 void clean_channelname(char *cn)
1211 for (i = 0; cn[i]; i++) {
1212 if (i >= IRCD_MIN(CHANNELLEN, feature_int(FEAT_CHANNELLEN))
1213 || !IsChannelChar(cn[i])) {
1217 if (IsChannelLower(cn[i])) {
1218 cn[i] = ToLower(cn[i]);
1224 if ((unsigned char)(cn[i]) == 0xd0)
1225 cn[i] = (char) 0xf0;
1231 /** Get a channel block, creating if necessary.
1232 * Get Channel block for chname (and allocate a new channel
1233 * block, if it didn't exists before).
1235 * @param cptr Client joining the channel.
1236 * @param chname The name of the channel to join.
1237 * @param flag set to CGT_CREATE to create the channel if it doesn't
1240 * @returns NULL if the channel is invalid, doesn't exist and CGT_CREATE
1241 * wasn't specified or a pointer to the channel structure
1243 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1245 struct Channel *chptr;
1248 if (EmptyString(chname))
1251 len = strlen(chname);
1252 if (MyUser(cptr) && len > CHANNELLEN)
1255 *(chname + CHANNELLEN) = '\0';
1257 if ((chptr = FindChannel(chname)))
1259 if (flag == CGT_CREATE)
1261 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1263 ++UserStats.channels;
1264 memset(chptr, 0, sizeof(struct Channel));
1265 strcpy(chptr->chname, chname);
1266 if (GlobalChannelList)
1267 GlobalChannelList->prev = chptr;
1269 chptr->next = GlobalChannelList;
1270 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1271 GlobalChannelList = chptr;
1277 /** invite a user to a channel.
1279 * Adds an invite for a user to a channel. Limits the number of invites
1280 * to FEAT_MAXCHANNELSPERUSER. Does not sent notification to the user.
1282 * @param cptr The client to be invited.
1283 * @param chptr The channel to be invited to.
1285 void add_invite(struct Client *cptr, struct Channel *chptr)
1287 struct SLink *inv, **tmp;
1289 del_invite(cptr, chptr);
1291 * Delete last link in chain if the list is max length
1293 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1294 if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER))
1295 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1297 * Add client to channel invite list
1300 inv->value.cptr = cptr;
1301 inv->next = chptr->invites;
1302 chptr->invites = inv;
1304 * Add channel to the end of the client invite list
1306 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1308 inv->value.chptr = chptr;
1311 (cli_user(cptr))->invites++;
1314 /** Delete an invite
1315 * Delete Invite block from channel invite list and client invite list
1317 * @param cptr Client pointer
1318 * @param chptr Channel pointer
1320 void del_invite(struct Client *cptr, struct Channel *chptr)
1322 struct SLink **inv, *tmp;
1324 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1325 if (tmp->value.cptr == cptr)
1330 (cli_user(cptr))->invites--;
1334 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1335 if (tmp->value.chptr == chptr)
1344 /** @page zombie Explanation of Zombies
1348 * A channel member is turned into a zombie when he is kicked from a
1349 * channel but his server has not acknowledged the kick. Servers that
1350 * see the member as a zombie can accept actions he performed before
1351 * being kicked, without allowing chanop operations from outsiders or
1352 * desyncing the network.
1360 * X --a--> A --b--> B --d--> D
1365 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1366 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1368 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1369 * Remove the user immediately when no users are left on the channel.
1370 * b) On server B : remove the user (who/lp) from the channel, send a
1371 * PART upstream (to A) and pass on the KICK.
1372 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1373 * channel, and pass on the KICK.
1374 * d) On server D : remove the user (who/lp) from the channel, and pass on
1378 * - Setting the ZOMBIE flag never hurts, we either remove the
1379 * client after that or we don't.
1380 * - The KICK message was already passed on, as should be in all cases.
1381 * - `who' is removed in all cases except case a) when users are left.
1382 * - A PART is only sent upstream in case b).
1388 * 1 --- 2 --- 3 --- 4 --- 5
1393 * We also need to turn 'who' into a zombie on servers 1 and 6,
1394 * because a KICK from 'who' (kicking someone else in that direction)
1395 * can arrive there afterward - which should not be bounced itself.
1396 * Therefore case a) also applies for servers 1 and 6.
1401 /** Turn a user on a channel into a zombie
1402 * This function turns a user into a zombie (see \ref zombie)
1404 * @param member The structure representing this user on this channel.
1405 * @param who The client that is being kicked.
1406 * @param cptr The connection the kick came from.
1407 * @param sptr The client that is doing the kicking.
1408 * @param chptr The channel the user is being kicked from.
1410 void make_zombie(struct Membership* member, struct Client* who,
1411 struct Client* cptr, struct Client* sptr, struct Channel* chptr)
1413 assert(0 != member);
1418 /* Default for case a): */
1421 /* Case b) or c) ?: */
1422 if (MyUser(who)) /* server 4 */
1424 if (IsServer(cptr)) /* Case b) ? */
1425 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1426 remove_user_from_channel(who, chptr);
1429 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1431 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1432 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1433 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1435 remove_user_from_channel(who, chptr);
1440 /* Case a) (servers 1, 2, 3 and 6) */
1441 if (channel_all_zombies(chptr))
1442 remove_user_from_channel(who, chptr);
1444 /* XXX Can't actually call Debug here; if the channel is all zombies,
1445 * chptr will no longer exist when we get here.
1446 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1450 /** returns the number of zombies on a channel
1451 * @param chptr Channel to count zombies in.
1453 * @returns The number of zombies on the channel.
1455 int number_of_zombies(struct Channel *chptr)
1457 struct Membership* member;
1461 for (member = chptr->members; member; member = member->next_member) {
1462 if (IsZombie(member))
1468 /** Concatenate some strings together.
1469 * This helper function builds an argument string in strptr, consisting
1470 * of the original string, a space, and str1 and str2 concatenated (if,
1471 * of course, str2 is not NULL)
1473 * @param strptr The buffer to concatenate into
1474 * @param strptr_i modified offset to the position to modify
1475 * @param str1 The string to concatenate from.
1476 * @param str2 The second string to contatenate from.
1477 * @param c Charactor to separate the string from str1 and str2.
1480 build_string(char *strptr, int *strptr_i, const char *str1,
1481 const char *str2, char c)
1484 strptr[(*strptr_i)++] = c;
1487 strptr[(*strptr_i)++] = *(str1++);
1491 strptr[(*strptr_i)++] = *(str2++);
1493 strptr[(*strptr_i)] = '\0';
1496 /** Flush out the modes
1497 * This is the workhorse of our ModeBuf suite; this actually generates the
1498 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1500 * @param mbuf The mode buffer to flush
1501 * @param all If true, flush all modes, otherwise leave partial modes in the
1507 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1509 /* we only need the flags that don't take args right now */
1510 static int flags[] = {
1511 /* MODE_CHANOP, 'o', */
1512 /* MODE_VOICE, 'v', */
1515 MODE_MODERATED, 'm',
1516 MODE_TOPICLIMIT, 't',
1517 MODE_INVITEONLY, 'i',
1518 MODE_NOPRIVMSGS, 'n',
1521 MODE_WASDELJOINS, 'd',
1522 /* MODE_KEY, 'k', */
1523 /* MODE_BAN, 'b', */
1525 /* MODE_APASS, 'A', */
1526 /* MODE_UPASS, 'U', */
1532 struct Client *app_source; /* where the MODE appears to come from */
1534 char addbuf[20]; /* accumulates +psmtin, etc. */
1536 char rembuf[20]; /* accumulates -psmtin, etc. */
1538 char *bufptr; /* we make use of indirection to simplify the code */
1541 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1543 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1545 char *strptr; /* more indirection to simplify the code */
1548 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1551 char limitbuf[20]; /* convert limits to strings */
1553 unsigned int limitdel = MODE_LIMIT;
1557 /* If the ModeBuf is empty, we have nothing to do */
1558 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1561 /* Ok, if we were given the OPMODE flag, or its a server, hide the source.
1563 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE || IsServer(mbuf->mb_source) || IsMe(mbuf->mb_source))
1566 app_source = mbuf->mb_source;
1569 * Account for user we're bouncing; we have to get it in on the first
1570 * bounced MODE, or we could have problems
1572 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1573 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1575 /* Calculate the simple flags */
1576 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1577 if (*flag_p & mbuf->mb_add)
1578 addbuf[addbuf_i++] = flag_p[1];
1579 else if (*flag_p & mbuf->mb_rem)
1580 rembuf[rembuf_i++] = flag_p[1];
1583 /* Now go through the modes with arguments... */
1584 for (i = 0; i < mbuf->mb_count; i++) {
1585 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1587 bufptr_i = &addbuf_i;
1590 bufptr_i = &rembuf_i;
1593 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1594 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1596 if ((totalbuflen - IRCD_MAX(9, tmp)) <= 0) /* don't overflow buffer */
1597 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1599 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1600 totalbuflen -= IRCD_MAX(9, tmp) + 1;
1602 } else if (MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS)) {
1603 tmp = strlen(MB_STRING(mbuf, i));
1605 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1606 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1609 switch(MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS))
1621 bufptr[(*bufptr_i)++] = mode_char;
1622 totalbuflen -= tmp + 1;
1624 } else if (MB_TYPE(mbuf, i) & MODE_KEY) {
1625 tmp = (mbuf->mb_dest & MODEBUF_DEST_NOKEY ? 1 :
1626 strlen(MB_STRING(mbuf, i)));
1628 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1629 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1631 bufptr[(*bufptr_i)++] = 'k';
1632 totalbuflen -= tmp + 1;
1634 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1635 /* if it's a limit, we also format the number */
1636 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
1638 tmp = strlen(limitbuf);
1640 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1641 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1643 bufptr[(*bufptr_i)++] = 'l';
1644 totalbuflen -= tmp + 1;
1649 /* terminate the mode strings */
1650 addbuf[addbuf_i] = '\0';
1651 rembuf[rembuf_i] = '\0';
1653 /* If we're building a user visible MODE or HACK... */
1654 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1655 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1656 MODEBUF_DEST_LOG)) {
1657 /* Set up the parameter strings */
1663 for (i = 0; i < mbuf->mb_count; i++) {
1664 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1667 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1669 strptr_i = &addstr_i;
1672 strptr_i = &remstr_i;
1675 /* deal with clients... */
1676 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1677 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1679 /* deal with bans... */
1680 else if (MB_TYPE(mbuf, i) & MODE_BAN)
1681 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1683 /* deal with keys... */
1684 else if (MB_TYPE(mbuf, i) & MODE_KEY)
1685 build_string(strptr, strptr_i, mbuf->mb_dest & MODEBUF_DEST_NOKEY ?
1686 "*" : MB_STRING(mbuf, i), 0, ' ');
1688 /* deal with invisible passwords */
1689 else if (MB_TYPE(mbuf, i) & (MODE_APASS | MODE_UPASS))
1690 build_string(strptr, strptr_i, "*", 0, ' ');
1693 * deal with limit; note we cannot include the limit parameter if we're
1696 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1697 (MODE_ADD | MODE_LIMIT))
1698 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1701 /* send the messages off to their destination */
1702 if (mbuf->mb_dest & MODEBUF_DEST_HACK2)
1703 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1705 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1706 mbuf->mb_source : app_source),
1707 mbuf->mb_channel->chname,
1708 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1709 addbuf, remstr, addstr,
1710 mbuf->mb_channel->creationtime);
1712 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1713 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1714 "%s%s%s%s%s%s [%Tu]",
1715 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1716 mbuf->mb_source : app_source),
1717 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1718 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1719 mbuf->mb_channel->creationtime);
1721 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1722 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1724 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1725 mbuf->mb_source : app_source),
1726 mbuf->mb_channel->chname,
1727 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1728 addbuf, remstr, addstr,
1729 mbuf->mb_channel->creationtime);
1731 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1732 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1733 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1734 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1735 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1737 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1738 sendcmdto_channel_butserv_butone(app_source, CMD_MODE, mbuf->mb_channel, NULL, 0,
1739 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1740 rembuf_i ? "-" : "", rembuf,
1741 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1744 /* Now are we supposed to propagate to other servers? */
1745 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1746 /* set up parameter string */
1753 * limit is supressed if we're removing it; we have to figure out which
1754 * direction is the direction for it to be removed, though...
1756 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1758 for (i = 0; i < mbuf->mb_count; i++) {
1759 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1762 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1764 strptr_i = &addstr_i;
1767 strptr_i = &remstr_i;
1770 /* if we're changing oplevels we know the oplevel, pass it on */
1771 if (mbuf->mb_channel->mode.apass[0]
1772 && (MB_TYPE(mbuf, i) & MODE_CHANOP)
1773 && MB_OPLEVEL(mbuf, i) < MAXOPLEVEL)
1774 *strptr_i += ircd_snprintf(0, strptr + *strptr_i, BUFSIZE - *strptr_i,
1776 NumNick(MB_CLIENT(mbuf, i)),
1777 MB_OPLEVEL(mbuf, i));
1779 /* deal with other modes that take clients */
1780 else if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1781 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1783 /* deal with modes that take strings */
1784 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS))
1785 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1788 * deal with the limit. Logic here is complicated; if HACK2 is set,
1789 * we're bouncing the mode, so sense is reversed, and we have to
1790 * include the original limit if it looks like it's being removed
1792 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1793 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1796 /* we were told to deop the source */
1797 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1798 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1799 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1800 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1802 /* mark that we've done this, so we don't do it again */
1803 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1806 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1807 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1808 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1809 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1810 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1811 addbuf, remstr, addstr);
1812 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1814 * If HACK2 was set, we're bouncing; we send the MODE back to the
1815 * connection we got it from with the senses reversed and a TS of 0;
1818 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1819 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1820 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1821 mbuf->mb_channel->creationtime);
1824 * We're propagating a normal MODE command to the rest of the network;
1825 * we send the actual channel TS unless this is a HACK3 or a HACK4
1827 if (IsServer(mbuf->mb_source))
1828 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1829 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1830 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1831 addbuf, remstr, addstr,
1832 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1833 mbuf->mb_channel->creationtime);
1835 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1836 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1837 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1838 addbuf, remstr, addstr);
1842 /* We've drained the ModeBuf... */
1847 /* reinitialize the mode-with-arg slots */
1848 for (i = 0; i < MAXMODEPARAMS; i++) {
1849 /* If we saved any, pack them down */
1850 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1851 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1852 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1854 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1856 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1857 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1859 MB_TYPE(mbuf, i) = 0;
1860 MB_UINT(mbuf, i) = 0;
1863 /* If we're supposed to flush it all, do so--all hail tail recursion */
1864 if (all && mbuf->mb_count)
1865 return modebuf_flush_int(mbuf, 1);
1870 /** Initialise a modebuf
1871 * This routine just initializes a ModeBuf structure with the information
1872 * needed and the options given.
1874 * @param mbuf The mode buffer to initialise.
1875 * @param source The client that is performing the mode.
1877 * @param chan The channel that the mode is being performed upon.
1881 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1882 struct Client *connect, struct Channel *chan, unsigned int dest)
1887 assert(0 != source);
1891 if (IsLocalChannel(chan->chname)) dest &= ~MODEBUF_DEST_SERVER;
1895 mbuf->mb_source = source;
1896 mbuf->mb_connect = connect;
1897 mbuf->mb_channel = chan;
1898 mbuf->mb_dest = dest;
1901 /* clear each mode-with-parameter slot */
1902 for (i = 0; i < MAXMODEPARAMS; i++) {
1903 MB_TYPE(mbuf, i) = 0;
1904 MB_UINT(mbuf, i) = 0;
1908 /** Append a new mode to a modebuf
1909 * This routine simply adds modes to be added or deleted; do a binary OR
1910 * with either MODE_ADD or MODE_DEL
1912 * @param mbuf Mode buffer
1913 * @param mode MODE_ADD or MODE_DEL OR'd with MODE_PRIVATE etc.
1916 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1919 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1921 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1922 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS | MODE_REGONLY |
1923 MODE_DELJOINS | MODE_WASDELJOINS);
1925 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1928 if (mode & MODE_ADD) {
1929 mbuf->mb_rem &= ~mode;
1930 mbuf->mb_add |= mode;
1932 mbuf->mb_add &= ~mode;
1933 mbuf->mb_rem |= mode;
1937 /** Append a mode that takes an int argument to the modebuf
1939 * This routine adds a mode to be added or deleted that takes a unsigned
1940 * int parameter; mode may *only* be the relevant mode flag ORed with one
1941 * of MODE_ADD or MODE_DEL
1943 * @param mbuf The mode buffer to append to.
1944 * @param mode The mode to append.
1945 * @param uint The argument to the mode.
1948 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1951 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1953 if (mode == (MODE_LIMIT | MODE_DEL)) {
1954 mbuf->mb_rem |= mode;
1957 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1958 MB_UINT(mbuf, mbuf->mb_count) = uint;
1960 /* when we've reached the maximal count, flush the buffer */
1961 if (++mbuf->mb_count >=
1962 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1963 modebuf_flush_int(mbuf, 0);
1966 /** append a string mode
1967 * This routine adds a mode to be added or deleted that takes a string
1968 * parameter; mode may *only* be the relevant mode flag ORed with one of
1969 * MODE_ADD or MODE_DEL
1971 * @param mbuf The mode buffer to append to.
1972 * @param mode The mode to append.
1973 * @param string The string parameter to append.
1974 * @param free If the string should be free'd later.
1977 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
1981 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1983 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
1984 MB_STRING(mbuf, mbuf->mb_count) = string;
1986 /* when we've reached the maximal count, flush the buffer */
1987 if (++mbuf->mb_count >=
1988 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1989 modebuf_flush_int(mbuf, 0);
1992 /** Append a mode on a client to a modebuf.
1993 * This routine adds a mode to be added or deleted that takes a client
1994 * parameter; mode may *only* be the relevant mode flag ORed with one of
1995 * MODE_ADD or MODE_DEL
1997 * @param mbuf The modebuf to append the mode to.
1998 * @param mode The mode to append.
1999 * @param client The client argument to append.
2000 * @param oplevel The oplevel the user had or will have
2003 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
2004 struct Client *client, int oplevel)
2007 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2009 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2010 MB_CLIENT(mbuf, mbuf->mb_count) = client;
2011 MB_OPLEVEL(mbuf, mbuf->mb_count) = oplevel;
2013 /* when we've reached the maximal count, flush the buffer */
2014 if (++mbuf->mb_count >=
2015 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2016 modebuf_flush_int(mbuf, 0);
2019 /** The exported binding for modebuf_flush()
2021 * @param mbuf The mode buffer to flush.
2023 * @see modebuf_flush_int()
2026 modebuf_flush(struct ModeBuf *mbuf)
2028 struct Membership *memb;
2030 /* Check if MODE_WASDELJOINS should be set */
2031 if (!(mbuf->mb_channel->mode.mode & (MODE_DELJOINS | MODE_WASDELJOINS))
2032 && (mbuf->mb_rem & MODE_DELJOINS)) {
2033 for (memb = mbuf->mb_channel->members; memb; memb = memb->next_member) {
2034 if (IsDelayedJoin(memb)) {
2035 mbuf->mb_channel->mode.mode |= MODE_WASDELJOINS;
2036 mbuf->mb_add |= MODE_WASDELJOINS;
2037 mbuf->mb_rem &= ~MODE_WASDELJOINS;
2043 return modebuf_flush_int(mbuf, 1);
2046 /* This extracts the simple modes contained in mbuf
2048 * @param mbuf The mode buffer to extract the modes from.
2049 * @param buf The string buffer to write the modes into.
2052 modebuf_extract(struct ModeBuf *mbuf, char *buf)
2054 static int flags[] = {
2055 /* MODE_CHANOP, 'o', */
2056 /* MODE_VOICE, 'v', */
2059 MODE_MODERATED, 'm',
2060 MODE_TOPICLIMIT, 't',
2061 MODE_INVITEONLY, 'i',
2062 MODE_NOPRIVMSGS, 'n',
2066 /* MODE_BAN, 'b', */
2073 int i, bufpos = 0, len;
2075 char *key = 0, limitbuf[20];
2076 char *apass = 0, *upass = 0;
2085 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
2086 if (MB_TYPE(mbuf, i) & MODE_ADD) {
2087 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT | MODE_APASS | MODE_UPASS);
2089 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
2090 key = MB_STRING(mbuf, i);
2091 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
2092 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
2093 else if (MB_TYPE(mbuf, i) & MODE_UPASS)
2094 upass = MB_STRING(mbuf, i);
2095 else if (MB_TYPE(mbuf, i) & MODE_APASS)
2096 apass = MB_STRING(mbuf, i);
2103 buf[bufpos++] = '+'; /* start building buffer */
2105 for (flag_p = flags; flag_p[0]; flag_p += 2)
2107 buf[bufpos++] = flag_p[1];
2109 for (i = 0, len = bufpos; i < len; i++) {
2111 build_string(buf, &bufpos, key, 0, ' ');
2112 else if (buf[i] == 'l')
2113 build_string(buf, &bufpos, limitbuf, 0, ' ');
2114 else if (buf[i] == 'U')
2115 build_string(buf, &bufpos, upass, 0, ' ');
2116 else if (buf[i] == 'A')
2117 build_string(buf, &bufpos, apass, 0, ' ');
2125 /** Simple function to invalidate bans
2127 * This function sets all bans as being valid.
2129 * @param chan The channel to operate on.
2132 mode_ban_invalidate(struct Channel *chan)
2134 struct Membership *member;
2136 for (member = chan->members; member; member = member->next_member)
2137 ClearBanValid(member);
2140 /** Simple function to drop invite structures
2142 * Remove all the invites on the channel.
2144 * @param chan Channel to remove invites from.
2148 mode_invite_clear(struct Channel *chan)
2150 while (chan->invites)
2151 del_invite(chan->invites->value.cptr, chan);
2154 /* What we've done for mode_parse so far... */
2155 #define DONE_LIMIT 0x01 /**< We've set the limit */
2156 #define DONE_KEY 0x02 /**< We've set the key */
2157 #define DONE_BANLIST 0x04 /**< We've sent the ban list */
2158 #define DONE_NOTOPER 0x08 /**< We've sent a "Not oper" error */
2159 #define DONE_BANCLEAN 0x10 /**< We've cleaned bans... */
2160 #define DONE_UPASS 0x20 /**< We've set user pass */
2161 #define DONE_APASS 0x40 /**< We've set admin pass */
2164 struct ModeBuf *mbuf;
2165 struct Client *cptr;
2166 struct Client *sptr;
2167 struct Channel *chptr;
2168 struct Membership *member;
2179 struct Ban banlist[MAXPARA];
2182 unsigned short oplevel;
2183 struct Client *client;
2184 } cli_change[MAXPARA];
2187 /** Helper function to send "Not oper" or "Not member" messages
2188 * Here's a helper function to deal with sending along "Not oper" or
2189 * "Not member" messages
2191 * @param state Parsing State object
2194 send_notoper(struct ParseState *state)
2196 if (state->done & DONE_NOTOPER)
2199 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
2200 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
2202 state->done |= DONE_NOTOPER;
2206 * Helper function to convert limits
2208 * @param state Parsing state object.
2212 mode_parse_limit(struct ParseState *state, int *flag_p)
2214 unsigned int t_limit;
2216 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
2217 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2220 if (state->parc <= 0) { /* warn if not enough args */
2221 if (MyUser(state->sptr))
2222 need_more_params(state->sptr, "MODE +l");
2226 t_limit = strtoul(state->parv[state->args_used++], 0, 10); /* grab arg */
2230 if ((int)t_limit<0) /* don't permit a negative limit */
2233 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2234 (!t_limit || t_limit == state->chptr->mode.limit))
2237 t_limit = state->chptr->mode.limit;
2239 /* If they're not an oper, they can't change modes */
2240 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2241 send_notoper(state);
2245 /* Can't remove a limit that's not there */
2246 if (state->dir == MODE_DEL && !state->chptr->mode.limit)
2249 /* Skip if this is a burst and a lower limit than this is set already */
2250 if ((state->flags & MODE_PARSE_BURST) &&
2251 (state->chptr->mode.mode & flag_p[0]) &&
2252 (state->chptr->mode.limit < t_limit))
2255 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
2257 state->done |= DONE_LIMIT;
2262 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
2264 if (state->flags & MODE_PARSE_SET) { /* set the limit */
2265 if (state->dir & MODE_ADD) {
2266 state->chptr->mode.mode |= flag_p[0];
2267 state->chptr->mode.limit = t_limit;
2269 state->chptr->mode.mode &= ~flag_p[0];
2270 state->chptr->mode.limit = 0;
2275 /** Helper function to clean key-like parameters. */
2281 while (*s > ' ' && *s != ':' && *s != ',' && t_len--)
2287 * Helper function to convert keys
2290 mode_parse_key(struct ParseState *state, int *flag_p)
2294 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2297 if (state->parc <= 0) { /* warn if not enough args */
2298 if (MyUser(state->sptr))
2299 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2304 t_str = state->parv[state->args_used++]; /* grab arg */
2308 /* If they're not an oper, they can't change modes */
2309 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2310 send_notoper(state);
2314 if (state->done & DONE_KEY) /* allow key to be set only once */
2316 state->done |= DONE_KEY;
2318 /* clean up the key string */
2320 if (!*t_str || *t_str == ':') { /* warn if empty */
2321 if (MyUser(state->sptr))
2322 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2330 /* Skip if this is a burst, we have a key already and the new key is
2331 * after the old one alphabetically */
2332 if ((state->flags & MODE_PARSE_BURST) &&
2333 *(state->chptr->mode.key) &&
2334 ircd_strcmp(state->chptr->mode.key, t_str) <= 0)
2337 /* can't add a key if one is set, nor can one remove the wrong key */
2338 if (!(state->flags & MODE_PARSE_FORCE))
2339 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2340 (state->dir == MODE_DEL &&
2341 ircd_strcmp(state->chptr->mode.key, t_str))) {
2342 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2346 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2347 !ircd_strcmp(state->chptr->mode.key, t_str))
2348 return; /* no key change */
2350 if (state->flags & MODE_PARSE_BOUNCE) {
2351 if (*state->chptr->mode.key) /* reset old key */
2352 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2353 state->chptr->mode.key, 0);
2354 else /* remove new bogus key */
2355 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2356 } else /* send new key */
2357 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2359 if (state->flags & MODE_PARSE_SET) {
2360 if (state->dir == MODE_DEL) /* remove the old key */
2361 *state->chptr->mode.key = '\0';
2362 else if (!state->chptr->mode.key[0]
2363 || ircd_strcmp(t_str, state->chptr->mode.key) < 0)
2364 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2369 * Helper function to convert user passes
2372 mode_parse_upass(struct ParseState *state, int *flag_p)
2376 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2379 if (state->parc <= 0) { /* warn if not enough args */
2380 if (MyUser(state->sptr))
2381 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +U" :
2386 t_str = state->parv[state->args_used++]; /* grab arg */
2390 /* If they're not an oper, they can't change modes */
2391 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2392 send_notoper(state);
2396 /* If a non-service user is trying to force it, refuse. */
2397 if (state->flags & MODE_PARSE_FORCE && MyUser(state->sptr)
2398 && !HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
2399 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2400 state->chptr->chname);
2404 /* If they are not the channel manager, they are not allowed to change it */
2405 if (MyUser(state->sptr) && !(state->flags & MODE_PARSE_FORCE || IsChannelManager(state->member))) {
2406 if (*state->chptr->mode.apass) {
2407 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2408 state->chptr->chname);
2410 send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname,
2411 (TStime() - state->chptr->creationtime < 172800) ?
2412 "approximately 4-5 minutes" : "approximately 48 hours");
2417 if (state->done & DONE_UPASS) /* allow upass to be set only once */
2419 state->done |= DONE_UPASS;
2421 /* clean up the upass string */
2423 if (!*t_str || *t_str == ':') { /* warn if empty */
2424 if (MyUser(state->sptr))
2425 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +U" :
2433 if (!(state->flags & MODE_PARSE_FORCE)) {
2434 /* can't add the upass while apass is not set */
2435 if (state->dir == MODE_ADD && !*state->chptr->mode.apass) {
2436 send_reply(state->sptr, ERR_UPASSNOTSET, state->chptr->chname, state->chptr->chname);
2439 /* cannot set a +U password that is the same as +A */
2440 if (state->dir == MODE_ADD && !ircd_strcmp(state->chptr->mode.apass, t_str)) {
2441 send_reply(state->sptr, ERR_UPASS_SAME_APASS, state->chptr->chname);
2444 /* can't add a upass if one is set, nor can one remove the wrong upass */
2445 if ((state->dir == MODE_ADD && *state->chptr->mode.upass) ||
2446 (state->dir == MODE_DEL &&
2447 ircd_strcmp(state->chptr->mode.upass, t_str))) {
2448 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2453 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2454 !ircd_strcmp(state->chptr->mode.upass, t_str))
2455 return; /* no upass change */
2457 if (state->flags & MODE_PARSE_BOUNCE) {
2458 if (*state->chptr->mode.upass) /* reset old upass */
2459 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2460 state->chptr->mode.upass, 0);
2461 else /* remove new bogus upass */
2462 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2463 } else /* send new upass */
2464 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2466 if (state->flags & MODE_PARSE_SET) {
2467 if (state->dir == MODE_DEL) /* remove the old upass */
2468 *state->chptr->mode.upass = '\0';
2469 else if (state->chptr->mode.upass[0] == '\0'
2470 || ircd_strcmp(t_str, state->chptr->mode.upass) < 0)
2471 ircd_strncpy(state->chptr->mode.upass, t_str, KEYLEN);
2476 * Helper function to convert admin passes
2479 mode_parse_apass(struct ParseState *state, int *flag_p)
2481 struct Membership *memb;
2484 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2487 if (state->parc <= 0) { /* warn if not enough args */
2488 if (MyUser(state->sptr))
2489 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2494 t_str = state->parv[state->args_used++]; /* grab arg */
2498 /* If they're not an oper, they can't change modes */
2499 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2500 send_notoper(state);
2504 if (MyUser(state->sptr)) {
2505 if (state->flags & MODE_PARSE_FORCE) {
2506 /* If an unprivileged oper is trying to force it, refuse. */
2507 if (!HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
2508 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2509 state->chptr->chname);
2513 /* If they are not the channel manager, they are not allowed to change it. */
2514 if (!IsChannelManager(state->member)) {
2515 if (*state->chptr->mode.apass) {
2516 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2517 state->chptr->chname);
2519 send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname,
2520 (TStime() - state->chptr->creationtime < 172800) ?
2521 "approximately 4-5 minutes" : "approximately 48 hours");
2525 /* Can't remove the Apass while Upass is still set. */
2526 if (state->dir == MODE_DEL && *state->chptr->mode.upass) {
2527 send_reply(state->sptr, ERR_UPASSSET, state->chptr->chname, state->chptr->chname);
2530 /* Can't add an Apass if one is set, nor can one remove the wrong Apass. */
2531 if ((state->dir == MODE_ADD && *state->chptr->mode.apass) ||
2532 (state->dir == MODE_DEL && ircd_strcmp(state->chptr->mode.apass, t_str))) {
2533 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2538 /* Forbid removing the Apass if the channel is older than 48 hours
2539 * unless an oper is doing it. */
2540 if (TStime() - state->chptr->creationtime >= 172800
2541 && state->dir == MODE_DEL
2542 && !IsAnOper(state->sptr)) {
2543 send_reply(state->sptr, ERR_CHANSECURED, state->chptr->chname);
2548 if (state->done & DONE_APASS) /* allow apass to be set only once */
2550 state->done |= DONE_APASS;
2552 /* clean up the apass string */
2554 if (!*t_str || *t_str == ':') { /* warn if empty */
2555 if (MyUser(state->sptr))
2556 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2564 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2565 !ircd_strcmp(state->chptr->mode.apass, t_str))
2566 return; /* no apass change */
2568 if (state->flags & MODE_PARSE_BOUNCE) {
2569 if (*state->chptr->mode.apass) /* reset old apass */
2570 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2571 state->chptr->mode.apass, 0);
2572 else /* remove new bogus apass */
2573 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2574 } else /* send new apass */
2575 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2577 if (state->flags & MODE_PARSE_SET) {
2578 if (state->dir == MODE_ADD) { /* set the new apass */
2579 /* Only accept the new apass if there is no current apass
2580 * (e.g. when a user sets it) or the new one is "less" than the
2581 * old (for resolving conflicts during burst).
2583 if (state->chptr->mode.apass[0] == '\0'
2584 || ircd_strcmp(t_str, state->chptr->mode.apass) < 0)
2585 ircd_strncpy(state->chptr->mode.apass, t_str, KEYLEN);
2586 /* Make it VERY clear to the user that this is a one-time password */
2587 if (MyUser(state->sptr)) {
2588 send_reply(state->sptr, RPL_APASSWARN_SET, state->chptr->mode.apass);
2589 send_reply(state->sptr, RPL_APASSWARN_SECRET, state->chptr->chname,
2590 state->chptr->mode.apass);
2592 /* Give the channel manager level 0 ops.
2593 There should not be tested for IsChannelManager here because
2594 on the local server it is impossible to set the apass if one
2595 isn't a channel manager and remote servers might need to sync
2596 the oplevel here: when someone creates a channel (and becomes
2597 channel manager) during a net.break, and only sets the Apass
2598 after the net rejoined, they will have oplevel MAXOPLEVEL on
2599 all remote servers. */
2601 SetOpLevel(state->member, 0);
2602 } else { /* remove the old apass */
2603 *state->chptr->mode.apass = '\0';
2604 /* Clear Upass so that there is never a Upass set when a zannel is burst. */
2605 *state->chptr->mode.upass = '\0';
2606 if (MyUser(state->sptr))
2607 send_reply(state->sptr, RPL_APASSWARN_CLEAR);
2608 /* Revert everyone to MAXOPLEVEL. */
2609 for (memb = state->chptr->members; memb; memb = memb->next_member) {
2610 if (memb->status & MODE_CHANOP)
2611 SetOpLevel(memb, MAXOPLEVEL);
2617 /** Compare one ban's extent to another.
2618 * This works very similarly to mmatch() but it knows about CIDR masks
2619 * and ban exceptions. If both bans are CIDR-based, compare their
2620 * address bits; otherwise, use mmatch().
2621 * @param[in] old_ban One ban.
2622 * @param[in] new_ban Another ban.
2623 * @return Zero if \a old_ban is a superset of \a new_ban, non-zero otherwise.
2626 bmatch(struct Ban *old_ban, struct Ban *new_ban)
2629 assert(old_ban != NULL);
2630 assert(new_ban != NULL);
2631 /* A ban is never treated as a superset of an exception. */
2632 if (!(old_ban->flags & BAN_EXCEPTION)
2633 && (new_ban->flags & BAN_EXCEPTION))
2635 /* If either is not an address mask, match the text masks. */
2636 if ((old_ban->flags & new_ban->flags & BAN_IPMASK) == 0)
2637 return mmatch(old_ban->banstr, new_ban->banstr);
2638 /* If the old ban has a longer prefix than new, it cannot be a superset. */
2639 if (old_ban->addrbits > new_ban->addrbits)
2641 /* Compare the masks before the hostname part. */
2642 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '\0';
2643 res = mmatch(old_ban->banstr, new_ban->banstr);
2644 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '@';
2647 /* Compare the addresses. */
2648 return !ipmask_check(&new_ban->address, &old_ban->address, old_ban->addrbits);
2651 /** Add a ban from a ban list and mark bans that should be removed
2652 * because they overlap.
2654 * There are three invariants for a ban list. First, no ban may be
2655 * more specific than another ban. Second, no exception may be more
2656 * specific than another exception. Finally, no ban may be more
2657 * specific than any exception.
2659 * @param[in,out] banlist Pointer to head of list.
2660 * @param[in] newban Ban (or exception) to add (or remove).
2661 * @param[in] do_free If non-zero, free \a newban on failure.
2662 * @return Zero if \a newban could be applied, non-zero if not.
2664 int apply_ban(struct Ban **banlist, struct Ban *newban, int do_free)
2669 assert(newban->flags & (BAN_ADD|BAN_DEL));
2670 if (newban->flags & BAN_ADD) {
2672 /* If a less specific entry is found, fail. */
2673 for (ban = *banlist; ban; ban = ban->next) {
2674 if (!bmatch(ban, newban)) {
2679 if (!(ban->flags & (BAN_OVERLAPPED|BAN_DEL))) {
2681 totlen += strlen(ban->banstr);
2684 /* Mark more specific entries and add this one to the end of the list. */
2685 while ((ban = *banlist) != NULL) {
2686 if (!bmatch(newban, ban)) {
2687 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
2689 banlist = &ban->next;
2693 } else if (newban->flags & BAN_DEL) {
2694 size_t remove_count = 0;
2695 /* Mark more specific entries. */
2696 for (ban = *banlist; ban; ban = ban->next) {
2697 if (!bmatch(newban, ban)) {
2698 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
2704 /* If no matches were found, fail. */
2715 * Helper function to convert bans
2718 mode_parse_ban(struct ParseState *state, int *flag_p)
2721 struct Ban *ban, *newban;
2723 if (state->parc <= 0) { /* Not enough args, send ban list */
2724 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2725 send_ban_list(state->sptr, state->chptr);
2726 state->done |= DONE_BANLIST;
2732 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2735 t_str = state->parv[state->args_used++]; /* grab arg */
2739 /* If they're not an oper, they can't change modes */
2740 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2741 send_notoper(state);
2745 if ((s = strchr(t_str, ' ')))
2748 if (!*t_str || *t_str == ':') { /* warn if empty */
2749 if (MyUser(state->sptr))
2750 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2755 /* Clear all ADD/DEL/OVERLAPPED flags from ban list. */
2756 if (!(state->done & DONE_BANCLEAN)) {
2757 for (ban = state->chptr->banlist; ban; ban = ban->next)
2758 ban->flags &= ~(BAN_ADD | BAN_DEL | BAN_OVERLAPPED);
2759 state->done |= DONE_BANCLEAN;
2762 /* remember the ban for the moment... */
2763 newban = state->banlist + (state->numbans++);
2765 newban->flags = ((state->dir == MODE_ADD) ? BAN_ADD : BAN_DEL)
2766 | (*flag_p == MODE_BAN ? 0 : BAN_EXCEPTION);
2767 set_ban_mask(newban, collapse(pretty_mask(t_str)));
2768 ircd_strncpy(newban->who, IsUser(state->sptr) ? cli_name(state->sptr) : "*", NICKLEN);
2769 newban->when = TStime();
2770 apply_ban(&state->chptr->banlist, newban, 0);
2774 * This is the bottom half of the ban processor
2777 mode_process_bans(struct ParseState *state)
2779 struct Ban *ban, *newban, *prevban, *nextban;
2785 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2787 banlen = strlen(ban->banstr);
2789 nextban = ban->next;
2791 if ((ban->flags & (BAN_DEL | BAN_ADD)) == (BAN_DEL | BAN_ADD)) {
2793 prevban->next = 0; /* Break the list; ban isn't a real ban */
2795 state->chptr->banlist = 0;
2801 } else if (ban->flags & BAN_DEL) { /* Deleted a ban? */
2803 DupString(bandup, ban->banstr);
2804 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2807 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2808 if (prevban) /* clip it out of the list... */
2809 prevban->next = ban->next;
2811 state->chptr->banlist = ban->next;
2818 continue; /* next ban; keep prevban like it is */
2820 ban->flags &= BAN_IPMASK; /* unset other flags */
2821 } else if (ban->flags & BAN_ADD) { /* adding a ban? */
2823 prevban->next = 0; /* Break the list; ban isn't a real ban */
2825 state->chptr->banlist = 0;
2827 /* If we're supposed to ignore it, do so. */
2828 if (ban->flags & BAN_OVERLAPPED &&
2829 !(state->flags & MODE_PARSE_BOUNCE)) {
2833 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2834 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2835 count > feature_int(FEAT_MAXBANS))) {
2836 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2842 /* add the ban to the buffer */
2843 DupString(bandup, ban->banstr);
2844 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2847 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2848 newban = make_ban(ban->banstr);
2849 strcpy(newban->who, ban->who);
2850 newban->when = ban->when;
2851 newban->flags = ban->flags & BAN_IPMASK;
2853 newban->next = state->chptr->banlist; /* and link it in */
2854 state->chptr->banlist = newban;
2863 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2865 if (changed) /* if we changed the ban list, we must invalidate the bans */
2866 mode_ban_invalidate(state->chptr);
2870 * Helper function to process client changes
2873 mode_parse_client(struct ParseState *state, int *flag_p)
2876 struct Client *acptr;
2877 struct Membership *member;
2878 int oplevel = MAXOPLEVEL + 1;
2881 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2884 if (state->parc <= 0) /* return if not enough args */
2887 t_str = state->parv[state->args_used++]; /* grab arg */
2891 /* If they're not an oper, they can't change modes */
2892 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2893 send_notoper(state);
2897 if (MyUser(state->sptr)) /* find client we're manipulating */
2898 acptr = find_chasing(state->sptr, t_str, NULL);
2900 if (t_str[5] == ':') {
2902 oplevel = atoi(t_str + 6);
2904 acptr = findNUser(t_str);
2908 return; /* find_chasing() already reported an error to the user */
2910 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2911 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2912 state->cli_change[i].flag & flag_p[0]))
2913 break; /* found a slot */
2915 /* If we are going to bounce this deop, mark the correct oplevel. */
2916 if (state->flags & MODE_PARSE_BOUNCE
2917 && state->dir == MODE_DEL
2918 && flag_p[0] == MODE_CHANOP
2919 && (member = find_member_link(state->chptr, acptr)))
2920 oplevel = OpLevel(member);
2922 /* Store what we're doing to them */
2923 state->cli_change[i].flag = state->dir | flag_p[0];
2924 state->cli_change[i].oplevel = oplevel;
2925 state->cli_change[i].client = acptr;
2929 * Helper function to process the changed client list
2932 mode_process_clients(struct ParseState *state)
2935 struct Membership *member;
2937 for (i = 0; state->cli_change[i].flag; i++) {
2938 assert(0 != state->cli_change[i].client);
2940 /* look up member link */
2941 if (!(member = find_member_link(state->chptr,
2942 state->cli_change[i].client)) ||
2943 (MyUser(state->sptr) && IsZombie(member))) {
2944 if (MyUser(state->sptr))
2945 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2946 cli_name(state->cli_change[i].client),
2947 state->chptr->chname);
2951 if ((state->cli_change[i].flag & MODE_ADD &&
2952 (state->cli_change[i].flag & member->status)) ||
2953 (state->cli_change[i].flag & MODE_DEL &&
2954 !(state->cli_change[i].flag & member->status)))
2955 continue; /* no change made, don't do anything */
2957 /* see if the deop is allowed */
2958 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2959 (MODE_DEL | MODE_CHANOP)) {
2960 /* prevent +k users from being deopped */
2961 if (IsChannelService(state->cli_change[i].client)) {
2962 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
2963 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
2965 (IsServer(state->sptr) ? cli_name(state->sptr) :
2966 cli_name((cli_user(state->sptr))->server)));
2968 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
2969 send_reply(state->sptr, ERR_ISCHANSERVICE,
2970 cli_name(state->cli_change[i].client),
2971 state->chptr->chname);
2976 /* check deop for local user */
2977 if (MyUser(state->sptr)) {
2979 /* don't allow local opers to be deopped on local channels */
2980 if (state->cli_change[i].client != state->sptr &&
2981 IsLocalChannel(state->chptr->chname) &&
2982 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
2983 send_reply(state->sptr, ERR_ISOPERLCHAN,
2984 cli_name(state->cli_change[i].client),
2985 state->chptr->chname);
2989 /* don't allow to deop members with an op level that is <= our own level */
2990 if (state->sptr != state->cli_change[i].client /* but allow to deop oneself */
2991 && state->chptr->mode.apass[0]
2993 && OpLevel(member) <= OpLevel(state->member)) {
2994 int equal = (OpLevel(member) == OpLevel(state->member));
2995 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
2996 cli_name(state->cli_change[i].client),
2997 state->chptr->chname,
2998 OpLevel(state->member), OpLevel(member),
2999 "deop", equal ? "the same" : "a higher");
3005 /* set op-level of member being opped */
3006 if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
3007 (MODE_ADD | MODE_CHANOP)) {
3008 /* If a valid oplevel was specified, use it.
3009 * Otherwise, if being opped by an outsider, get MAXOPLEVEL.
3010 * Otherwise, if not an apass channel, or state->member has
3011 * MAXOPLEVEL, get oplevel MAXOPLEVEL.
3012 * Otherwise, get state->member's oplevel+1.
3014 if (state->cli_change[i].oplevel <= MAXOPLEVEL)
3015 SetOpLevel(member, state->cli_change[i].oplevel);
3016 else if (!state->member)
3017 SetOpLevel(member, MAXOPLEVEL);
3018 else if (!state->chptr->mode.apass[0] || OpLevel(state->member) == MAXOPLEVEL)
3019 SetOpLevel(member, MAXOPLEVEL);
3021 SetOpLevel(member, OpLevel(state->member) + 1);
3024 /* actually effect the change */
3025 if (state->flags & MODE_PARSE_SET) {
3026 if (state->cli_change[i].flag & MODE_ADD) {
3027 if (IsDelayedJoin(member))
3028 RevealDelayedJoin(member);
3029 member->status |= (state->cli_change[i].flag &
3030 (MODE_CHANOP | MODE_VOICE));
3031 if (state->cli_change[i].flag & MODE_CHANOP)
3032 ClearDeopped(member);
3034 member->status &= ~(state->cli_change[i].flag &
3035 (MODE_CHANOP | MODE_VOICE));
3038 /* accumulate the change */
3039 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
3040 state->cli_change[i].client,
3041 state->cli_change[i].oplevel);
3042 } /* for (i = 0; state->cli_change[i].flags; i++) */
3046 * Helper function to process the simple modes
3049 mode_parse_mode(struct ParseState *state, int *flag_p)
3051 /* If they're not an oper, they can't change modes */
3052 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3053 send_notoper(state);
3060 if (state->dir == MODE_ADD) {
3061 state->add |= flag_p[0];
3062 state->del &= ~flag_p[0];
3064 if (flag_p[0] & MODE_SECRET) {
3065 state->add &= ~MODE_PRIVATE;
3066 state->del |= MODE_PRIVATE;
3067 } else if (flag_p[0] & MODE_PRIVATE) {
3068 state->add &= ~MODE_SECRET;
3069 state->del |= MODE_SECRET;
3071 if (flag_p[0] & MODE_DELJOINS) {
3072 state->add &= ~MODE_WASDELJOINS;
3073 state->del |= MODE_WASDELJOINS;
3076 state->add &= ~flag_p[0];
3077 state->del |= flag_p[0];
3080 assert(0 == (state->add & state->del));
3081 assert((MODE_SECRET | MODE_PRIVATE) !=
3082 (state->add & (MODE_SECRET | MODE_PRIVATE)));
3086 * This routine is intended to parse MODE or OPMODE commands and effect the
3087 * changes (or just build the bounce buffer). We pass the starting offset
3091 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
3092 struct Channel *chptr, int parc, char *parv[], unsigned int flags,
3093 struct Membership* member)
3095 static int chan_flags[] = {
3100 MODE_MODERATED, 'm',
3101 MODE_TOPICLIMIT, 't',
3102 MODE_INVITEONLY, 'i',
3103 MODE_NOPRIVMSGS, 'n',
3117 unsigned int t_mode;
3119 struct ParseState state;
3130 state.chptr = chptr;
3131 state.member = member;
3134 state.flags = flags;
3135 state.dir = MODE_ADD;
3139 state.args_used = 0;
3140 state.max_args = MAXMODEPARAMS;
3143 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
3144 state.banlist[i].next = 0;
3145 state.banlist[i].who[0] = '\0';
3146 state.banlist[i].when = 0;
3147 state.banlist[i].flags = 0;
3148 state.cli_change[i].flag = 0;
3149 state.cli_change[i].client = 0;
3152 modestr = state.parv[state.args_used++];
3156 for (; *modestr; modestr++) {
3157 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
3158 if (flag_p[1] == *modestr)
3161 if (!flag_p[0]) { /* didn't find it? complain and continue */
3162 if (MyUser(state.sptr))
3163 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
3168 case '+': /* switch direction to MODE_ADD */
3169 case '-': /* switch direction to MODE_DEL */
3170 state.dir = flag_p[0];
3173 case 'l': /* deal with limits */
3174 mode_parse_limit(&state, flag_p);
3177 case 'k': /* deal with keys */
3178 mode_parse_key(&state, flag_p);
3181 case 'A': /* deal with Admin passes */
3182 if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS))
3183 mode_parse_apass(&state, flag_p);
3186 case 'U': /* deal with user passes */
3187 if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS))
3188 mode_parse_upass(&state, flag_p);
3191 case 'b': /* deal with bans */
3192 mode_parse_ban(&state, flag_p);
3195 case 'o': /* deal with ops/voice */
3197 mode_parse_client(&state, flag_p);
3200 default: /* deal with other modes */
3201 mode_parse_mode(&state, flag_p);
3203 } /* switch (*modestr) */
3204 } /* for (; *modestr; modestr++) */
3206 if (state.flags & MODE_PARSE_BURST)
3207 break; /* don't interpret any more arguments */
3209 if (state.parc > 0) { /* process next argument in string */
3210 modestr = state.parv[state.args_used++];
3214 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
3217 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
3218 break; /* we're then going to bounce the mode! */
3220 recv_ts = atoi(modestr);
3222 if (recv_ts && recv_ts < state.chptr->creationtime)
3223 state.chptr->creationtime = recv_ts; /* respect earlier TS */
3225 break; /* break out of while loop */
3226 } else if (state.flags & MODE_PARSE_STRICT ||
3227 (MyUser(state.sptr) && state.max_args <= 0)) {
3228 state.parc++; /* we didn't actually gobble the argument */
3230 break; /* break out of while loop */
3233 } /* while (*modestr) */
3236 * the rest of the function finishes building resultant MODEs; if the
3237 * origin isn't a member or an oper, skip it.
3239 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3240 return state.args_used; /* tell our parent how many args we gobbled */
3242 t_mode = state.chptr->mode.mode;
3244 if (state.del & t_mode) { /* delete any modes to be deleted... */
3245 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
3247 t_mode &= ~state.del;
3249 if (state.add & ~t_mode) { /* add any modes to be added... */
3250 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
3252 t_mode |= state.add;
3255 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
3256 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
3257 !(t_mode & MODE_INVITEONLY))
3258 mode_invite_clear(state.chptr);
3260 state.chptr->mode.mode = t_mode;
3263 if (state.flags & MODE_PARSE_WIPEOUT) {
3264 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
3265 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
3266 state.chptr->mode.limit);
3267 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
3268 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
3269 state.chptr->mode.key, 0);
3270 if (*state.chptr->mode.upass && !(state.done & DONE_UPASS))
3271 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_UPASS,
3272 state.chptr->mode.upass, 0);
3273 if (*state.chptr->mode.apass && !(state.done & DONE_APASS))
3274 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_APASS,
3275 state.chptr->mode.apass, 0);
3278 if (state.done & DONE_BANCLEAN) /* process bans */
3279 mode_process_bans(&state);
3281 /* process client changes */
3282 if (state.cli_change[0].flag)
3283 mode_process_clients(&state);
3285 return state.args_used; /* tell our parent how many args we gobbled */
3289 * Initialize a join buffer
3292 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
3293 struct Client *connect, unsigned int type, char *comment,
3299 assert(0 != source);
3300 assert(0 != connect);
3302 jbuf->jb_source = source; /* just initialize struct JoinBuf */
3303 jbuf->jb_connect = connect;
3304 jbuf->jb_type = type;
3305 jbuf->jb_comment = comment;
3306 jbuf->jb_create = create;
3308 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
3309 type == JOINBUF_TYPE_PART ||
3310 type == JOINBUF_TYPE_PARTALL) ?
3311 STARTJOINLEN : STARTCREATELEN) +
3312 (comment ? strlen(comment) + 2 : 0));
3314 for (i = 0; i < MAXJOINARGS; i++)
3315 jbuf->jb_channels[i] = 0;
3319 * Add a channel to the join buffer
3322 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
3330 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
3334 is_local = IsLocalChannel(chan->chname);
3336 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
3337 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
3338 struct Membership *member = find_member_link(chan, jbuf->jb_source);
3339 if (IsUserParting(member))
3341 SetUserParting(member);
3343 /* Send notification to channel */
3344 if (!(flags & (CHFL_ZOMBIE | CHFL_DELAYED)))
3345 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL, 0,
3346 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3347 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3348 else if (MyUser(jbuf->jb_source))
3349 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
3350 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3351 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3352 /* XXX: Shouldn't we send a PART here anyway? */
3353 /* to users on the channel? Why? From their POV, the user isn't on
3354 * the channel anymore anyway. We don't send to servers until below,
3355 * when we gang all the channel parts together. Note that this is
3356 * exactly the same logic, albeit somewhat more concise, as was in
3357 * the original m_part.c */
3359 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3360 is_local) /* got to remove user here */
3361 remove_user_from_channel(jbuf->jb_source, chan);
3363 int oplevel = !chan->mode.apass[0] ? MAXOPLEVEL
3364 : (flags & CHFL_CHANNEL_MANAGER) ? 0
3366 /* Add user to channel */
3367 if ((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))
3368 add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, oplevel);
3370 add_user_to_channel(chan, jbuf->jb_source, flags, oplevel);
3372 /* send JOIN notification to all servers (CREATE is sent later). */
3373 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local)
3374 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
3375 "%H %Tu", chan, chan->creationtime);
3377 if (!((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))) {
3378 /* Send the notification to the channel */
3379 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, 0, "%H", chan);
3381 /* send an op, too, if needed */
3382 if (flags & CHFL_CHANOP && (oplevel < MAXOPLEVEL || !MyUser(jbuf->jb_source)))
3383 sendcmdto_channel_butserv_butone((chan->mode.apass[0] ? &his : jbuf->jb_source),
3384 CMD_MODE, chan, NULL, 0, "%H +o %C",
3385 chan, jbuf->jb_source);
3386 } else if (MyUser(jbuf->jb_source))
3387 sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan);
3390 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3391 jbuf->jb_type == JOINBUF_TYPE_JOIN || is_local)
3392 return; /* don't send to remote */
3394 /* figure out if channel name will cause buffer to be overflowed */
3395 len = chan ? strlen(chan->chname) + 1 : 2;
3396 if (jbuf->jb_strlen + len > BUFSIZE)
3397 joinbuf_flush(jbuf);
3399 /* add channel to list of channels to send and update counts */
3400 jbuf->jb_channels[jbuf->jb_count++] = chan;
3401 jbuf->jb_strlen += len;
3403 /* if we've used up all slots, flush */
3404 if (jbuf->jb_count >= MAXJOINARGS)
3405 joinbuf_flush(jbuf);
3409 * Flush the channel list to remote servers
3412 joinbuf_flush(struct JoinBuf *jbuf)
3414 char chanlist[BUFSIZE];
3418 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3419 jbuf->jb_type == JOINBUF_TYPE_JOIN)
3420 return 0; /* no joins to process */
3422 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
3423 build_string(chanlist, &chanlist_i,
3424 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
3425 i == 0 ? '\0' : ',');
3426 if (JOINBUF_TYPE_PART == jbuf->jb_type)
3427 /* Remove user from channel */
3428 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
3430 jbuf->jb_channels[i] = 0; /* mark slot empty */
3433 jbuf->jb_count = 0; /* reset base counters */
3434 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
3435 STARTJOINLEN : STARTCREATELEN) +
3436 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
3438 /* and send the appropriate command */
3439 switch (jbuf->jb_type) {
3440 case JOINBUF_TYPE_CREATE:
3441 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
3442 "%s %Tu", chanlist, jbuf->jb_create);
3445 case JOINBUF_TYPE_PART:
3446 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
3447 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,
3455 /* Returns TRUE (1) if client is invited, FALSE (0) if not */
3456 int IsInvited(struct Client* cptr, const void* chptr)
3460 for (lp = (cli_user(cptr))->invited; lp; lp = lp->next)
3461 if (lp->value.chptr == chptr)
3466 /* RevealDelayedJoin: sends a join for a hidden user */
3468 void RevealDelayedJoin(struct Membership *member)
3470 ClearDelayedJoin(member);
3471 sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, 0, ":%H",
3473 CheckDelayedJoins(member->channel);
3476 /* CheckDelayedJoins: checks and clear +d if necessary */
3478 void CheckDelayedJoins(struct Channel *chan)
3480 struct Membership *memb2;
3482 if (chan->mode.mode & MODE_WASDELJOINS) {
3483 for (memb2=chan->members;memb2;memb2=memb2->next_member)
3484 if (IsDelayedJoin(memb2))
3489 chan->mode.mode &= ~MODE_WASDELJOINS;
3490 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan, NULL, 0,