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 /* Immediately destruct empty -A channels if not using apass. */
292 if (!feature_bool(FEAT_OPLEVELS))
294 destruct_channel(chptr);
298 if (TStime() - chptr->creationtime < 172800) /* Channel younger than 48 hours? */
299 schedule_destruct_event_1m(chptr); /* Get rid of it in approximately 4-5 minutes */
301 schedule_destruct_event_48h(chptr); /* Get rid of it in approximately 48 hours */
306 /** Destroy an empty channel
307 * This function destroys an empty channel, removing it from hashtables,
308 * and removing any resources it may have consumed.
310 * @param chptr The channel to destroy
312 * @returns 0 (success)
314 * FIXME: Change to return void, this function never fails.
316 int destruct_channel(struct Channel* chptr)
318 struct Ban *ban, *next;
320 assert(0 == chptr->members);
323 * Now, find all invite links from channel structure
325 while (chptr->invites)
326 del_invite(chptr->invites->value.cptr, chptr);
328 for (ban = chptr->banlist; ban; ban = next)
334 chptr->prev->next = chptr->next;
336 GlobalChannelList = chptr->next;
338 chptr->next->prev = chptr->prev;
340 --UserStats.channels;
342 * make sure that channel actually got removed from hash table
344 assert(chptr->hnext == chptr);
349 /** returns Membership * if a person is joined and not a zombie
351 * @param chptr Channel
352 * @returns pointer to the client's struct Membership * on the channel if that
353 * user is a full member of the channel, or NULL otherwise.
355 * @see find_member_link()
357 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
359 struct Membership* member;
362 member = find_member_link(chptr, cptr);
363 return (member && !IsZombie(member)) ? member : 0;
366 /** Searches for a ban from a ban list that matches a user.
367 * @param[in] cptr The client to test.
368 * @param[in] banlist The list of bans to test.
369 * @return Pointer to a matching ban, or NULL if none exit.
371 struct Ban *find_ban(struct Client *cptr, struct Ban *banlist)
373 char nu[NICKLEN + USERLEN + 2];
374 char tmphost[HOSTLEN + 1];
375 char iphost[SOCKIPLEN + 1];
380 /* Build nick!user and alternate host names. */
381 ircd_snprintf(0, nu, sizeof(nu), "%s!%s",
382 cli_name(cptr), cli_user(cptr)->username);
383 ircd_ntoa_r(iphost, &cli_ip(cptr));
384 if (!IsAccount(cptr))
386 else if (HasHiddenHost(cptr))
387 sr = cli_user(cptr)->realhost;
390 ircd_snprintf(0, tmphost, HOSTLEN, "%s.%s",
391 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
395 /* Walk through ban list. */
396 for (found = NULL; banlist; banlist = banlist->next) {
398 /* If we have found a positive ban already, only consider exceptions. */
399 if (found && !(banlist->flags & BAN_EXCEPTION))
401 /* Compare nick!user portion of ban. */
402 banlist->banstr[banlist->nu_len] = '\0';
403 res = match(banlist->banstr, nu);
404 banlist->banstr[banlist->nu_len] = '@';
407 /* Compare host portion of ban. */
408 hostmask = banlist->banstr + banlist->nu_len + 1;
409 if (!((banlist->flags & BAN_IPMASK)
410 && ipmask_check(&cli_ip(cptr), &banlist->address, banlist->addrbits))
411 && match(hostmask, cli_user(cptr)->host)
412 && !(sr && !match(hostmask, sr)))
414 /* If an exception matches, no ban can match. */
415 if (banlist->flags & BAN_EXCEPTION)
417 /* Otherwise, remember this ban but keep searching for an exception. */
424 * This function returns true if the user is banned on the said channel.
425 * This function will check the ban cache if applicable, otherwise will
426 * do the comparisons and cache the result.
428 * @param[in] member The Membership to test for banned-ness.
429 * @return Non-zero if the member is banned, zero if not.
431 static int is_banned(struct Membership* member)
433 if (IsBanValid(member))
434 return IsBanned(member);
437 if (find_ban(member->user, member->channel->banlist)) {
446 /** add a user to a channel.
447 * adds a user to a channel by adding another link to the channels member
450 * @param chptr The channel to add to.
451 * @param who The user to add.
452 * @param flags The flags the user gets initially.
453 * @param oplevel The oplevel the user starts with.
455 void add_user_to_channel(struct Channel* chptr, struct Client* who,
456 unsigned int flags, int oplevel)
463 struct Membership* member = membershipFreeList;
465 membershipFreeList = member->next_member;
467 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
468 ++membershipAllocCount;
473 member->channel = chptr;
474 member->status = flags;
475 SetOpLevel(member, oplevel);
477 member->next_member = chptr->members;
478 if (member->next_member)
479 member->next_member->prev_member = member;
480 member->prev_member = 0;
481 chptr->members = member;
483 member->next_channel = (cli_user(who))->channel;
484 if (member->next_channel)
485 member->next_channel->prev_channel = member;
486 member->prev_channel = 0;
487 (cli_user(who))->channel = member;
489 if (chptr->destruct_event)
490 remove_destruct_event(chptr);
492 ++((cli_user(who))->joined);
496 /** Remove a person from a channel, given their Membership*
498 * @param member A member of a channel.
500 * @returns true if there are more people in the channel.
502 static int remove_member_from_channel(struct Membership* member)
504 struct Channel* chptr;
506 chptr = member->channel;
508 * unlink channel member list
510 if (member->next_member)
511 member->next_member->prev_member = member->prev_member;
512 if (member->prev_member)
513 member->prev_member->next_member = member->next_member;
515 member->channel->members = member->next_member;
518 * If this is the last delayed-join user, may have to clear WASDELJOINS.
520 if (IsDelayedJoin(member))
521 CheckDelayedJoins(chptr);
524 * unlink client channel list
526 if (member->next_channel)
527 member->next_channel->prev_channel = member->prev_channel;
528 if (member->prev_channel)
529 member->prev_channel->next_channel = member->next_channel;
531 (cli_user(member->user))->channel = member->next_channel;
533 --(cli_user(member->user))->joined;
535 member->next_member = membershipFreeList;
536 membershipFreeList = member;
538 return sub1_from_channel(chptr);
541 /** Check if all the remaining members on the channel are zombies
543 * @returns False if the channel has any non zombie members, True otherwise.
546 static int channel_all_zombies(struct Channel* chptr)
548 struct Membership* member;
550 for (member = chptr->members; member; member = member->next_member) {
551 if (!IsZombie(member))
558 /** Remove a user from a channel
559 * This is the generic entry point for removing a user from a channel, this
560 * function will remove the client from the channel, and destroy the channel
561 * if there are no more normal users left.
563 * @param cptr The client
564 * @param chptr The channel
566 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
569 struct Membership* member;
572 if ((member = find_member_link(chptr, cptr))) {
573 if (remove_member_from_channel(member)) {
574 if (channel_all_zombies(chptr)) {
576 * XXX - this looks dangerous but isn't if we got the referential
577 * integrity right for channels
579 while (remove_member_from_channel(chptr->members))
586 /** Remove a user from all channels they are on.
588 * This function removes a user from all channels they are on.
590 * @param cptr The client to remove.
592 void remove_user_from_all_channels(struct Client* cptr)
594 struct Membership* chan;
596 assert(0 != cli_user(cptr));
598 while ((chan = (cli_user(cptr))->channel))
599 remove_user_from_channel(cptr, chan->channel);
602 /** Check if this user is a legitimate chanop
604 * @param cptr Client to check
605 * @param chptr Channel to check
607 * @returns True if the user is a chanop (And not a zombie), False otherwise.
610 int is_chan_op(struct Client *cptr, struct Channel *chptr)
612 struct Membership* member;
614 if ((member = find_member_link(chptr, cptr)))
615 return (!IsZombie(member) && IsChanOp(member));
620 /** Check if a user is a Zombie on a specific channel.
622 * @param cptr The client to check.
623 * @param chptr The channel to check.
625 * @returns True if the client (cptr) is a zombie on the channel (chptr),
630 int is_zombie(struct Client *cptr, struct Channel *chptr)
632 struct Membership* member;
636 if ((member = find_member_link(chptr, cptr)))
637 return IsZombie(member);
641 /** Returns if a user has voice on a channel.
643 * @param cptr The client
644 * @param chptr The channel
646 * @returns True if the client (cptr) is voiced on (chptr) and is not a zombie.
649 int has_voice(struct Client* cptr, struct Channel* chptr)
651 struct Membership* member;
654 if ((member = find_member_link(chptr, cptr)))
655 return (!IsZombie(member) && HasVoice(member));
660 /** Can this member send to a channel
662 * A user can speak on a channel iff:
664 * <li> They didn't use the Apass to gain ops.
665 * <li> They are op'd or voice'd.
666 * <li> You aren't banned.
667 * <li> The channel isn't +m
668 * <li> The channel isn't +n or you are on the channel.
671 * This function will optionally reveal a user on a delayed join channel if
672 * they are allowed to send to the channel.
674 * @param member The membership of the user
675 * @param reveal If true, the user will be "revealed" on a delayed
678 * @returns True if the client can speak on the channel.
680 int member_can_send_to_channel(struct Membership* member, int reveal)
684 /* Do not check for users on other servers: This should be a
685 * temporary desynch, or maybe they are on an older server, but
686 * we do not want to send ERR_CANNOTSENDTOCHAN more than once.
688 if (!MyUser(member->user))
690 if (IsDelayedJoin(member) && reveal)
691 RevealDelayedJoin(member);
695 /* Discourage using the Apass to get op. They should use the Upass. */
696 if (IsChannelManager(member) && member->channel->mode.apass[0])
699 /* If you have voice or ops, you can speak. */
700 if (IsVoicedOrOpped(member))
704 * If it's moderated, and you aren't a privileged user, you can't
707 if (member->channel->mode.mode & MODE_MODERATED)
710 /* If only logged in users may join and you're not one, you can't speak. */
711 if (member->channel->mode.mode & MODE_REGONLY && !IsAccount(member->user))
714 /* If you're banned then you can't speak either. */
715 if (is_banned(member))
718 if (IsDelayedJoin(member) && reveal)
719 RevealDelayedJoin(member);
724 /** Check if a client can send to a channel.
726 * Has the added check over member_can_send_to_channel() of servers can
729 * @param cptr The client to check
730 * @param chptr The channel to check
731 * @param reveal If the user should be revealed (see
732 * member_can_send_to_channel())
734 * @returns true if the client is allowed to speak on the channel, false
737 * @see member_can_send_to_channel()
739 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr, int reveal)
741 struct Membership *member;
744 * Servers can always speak on channels.
749 member = find_channel_member(cptr, chptr);
752 * You can't speak if you're off channel, and it is +n (no external messages)
756 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED)) ||
757 ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(cptr)))
760 return !find_ban(cptr, chptr->banlist);
762 return member_can_send_to_channel(member, reveal);
765 /** Returns the name of a channel that prevents the user from changing nick.
766 * if a member and not (opped or voiced) and (banned or moderated), return
767 * the name of the first channel banned on.
769 * @param cptr The client
771 * @returns the name of the first channel banned on, or NULL if the user
774 const char* find_no_nickchange_channel(struct Client* cptr)
777 struct Membership* member;
778 for (member = (cli_user(cptr))->channel; member;
779 member = member->next_channel) {
780 if (IsVoicedOrOpped(member))
782 if ((member->channel->mode.mode & MODE_MODERATED)
783 || (member->channel->mode.mode & MODE_REGONLY && !IsAccount(cptr))
784 || is_banned(member))
785 return member->channel->chname;
792 /** Fill mbuf/pbuf with modes from chptr
793 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
794 * with the parameters in pbuf as visible by cptr.
796 * This function will hide keys from non-op'd, non-server clients.
798 * @param cptr The client to generate the mode for.
799 * @param mbuf The buffer to write the modes into.
800 * @param pbuf The buffer to write the mode parameters into.
801 * @param buflen The length of the buffers.
802 * @param chptr The channel to get the modes from.
803 * @param member The membership of this client on this channel (or NULL
804 * if this client isn't on this channel)
807 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen,
808 struct Channel *chptr, struct Membership *member)
810 int previous_parameter = 0;
817 if (chptr->mode.mode & MODE_SECRET)
819 else if (chptr->mode.mode & MODE_PRIVATE)
821 if (chptr->mode.mode & MODE_MODERATED)
823 if (chptr->mode.mode & MODE_TOPICLIMIT)
825 if (chptr->mode.mode & MODE_INVITEONLY)
827 if (chptr->mode.mode & MODE_NOPRIVMSGS)
829 if (chptr->mode.mode & MODE_REGONLY)
831 if (chptr->mode.mode & MODE_DELJOINS)
833 else if (MyUser(cptr) && (chptr->mode.mode & MODE_WASDELJOINS))
835 if (chptr->mode.mode & MODE_REGISTERED)
837 if (chptr->mode.limit) {
839 ircd_snprintf(0, pbuf, buflen, "%u", chptr->mode.limit);
840 previous_parameter = 1;
843 if (*chptr->mode.key) {
845 if (previous_parameter)
847 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
848 strcat(pbuf, chptr->mode.key);
851 previous_parameter = 1;
853 if (*chptr->mode.apass) {
855 if (previous_parameter)
857 if (IsServer(cptr)) {
858 strcat(pbuf, chptr->mode.apass);
861 previous_parameter = 1;
863 if (*chptr->mode.upass) {
865 if (previous_parameter)
867 if (IsServer(cptr) || (member && IsChanOp(member) && OpLevel(member) == 0)) {
868 strcat(pbuf, chptr->mode.upass);
875 /** Compare two members oplevel
877 * @param mp1 Pointer to a pointer to a membership
878 * @param mp2 Pointer to a pointer to a membership
880 * @returns 0 if equal, -1 if mp1 is lower, +1 otherwise.
884 int compare_member_oplevel(const void *mp1, const void *mp2)
886 struct Membership const* member1 = *(struct Membership const**)mp1;
887 struct Membership const* member2 = *(struct Membership const**)mp2;
888 if (member1->oplevel == member2->oplevel)
890 return (member1->oplevel < member2->oplevel) ? -1 : 1;
893 /* send "cptr" a full list of the modes for channel chptr.
895 * Sends a BURST line to cptr, bursting all the modes for the channel.
897 * @param cptr Client pointer
898 * @param chptr Channel pointer
900 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
902 /* The order in which modes are generated is now mandatory */
903 static unsigned int current_flags[4] =
904 { 0, CHFL_VOICE, CHFL_CHANOP, CHFL_CHANOP | CHFL_VOICE };
910 struct Membership* member;
912 char modebuf[MODEBUFLEN];
913 char parabuf[MODEBUFLEN];
915 int number_of_ops = 0;
916 int opped_members_index = 0;
917 struct Membership** opped_members = NULL;
918 int last_oplevel = 0;
919 int send_oplevels = 0;
924 if (IsLocalChannel(chptr->chname))
927 member = chptr->members;
928 lp2 = chptr->banlist;
930 *modebuf = *parabuf = '\0';
931 channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr, 0);
933 for (first = 1; full; first = 0) /* Loop for multiple messages */
935 full = 0; /* Assume by default we get it
936 all in one message */
938 /* (Continued) prefix: "<Y> B <channel> <TS>" */
939 /* is there any better way we can do this? */
940 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
941 chptr->creationtime);
943 if (first && modebuf[1]) /* Add simple modes (Aiklmnpstu)
946 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
947 msgq_append(&me, mb, " %s", modebuf);
950 msgq_append(&me, mb, " %s", parabuf);
954 * Attach nicks, comma separated " nick[:modes],nick[:modes],..."
956 * First find all opless members.
957 * Run 2 times over all members, to group the members with
958 * and without voice together.
959 * Then run 2 times over all opped members (which are ordered
960 * by op-level) to also group voice and non-voice together.
962 for (first = 1; flag_cnt < 4; new_mode = 1, ++flag_cnt)
966 if (flag_cnt < 2 && IsChanOp(member))
969 * The first loop (to find all non-voice/op), we count the ops.
970 * The second loop (to find all voiced non-ops), store the ops
971 * in a dynamic array.
976 opped_members[opped_members_index++] = member;
977 /* We also send oplevels if anyone is below the weakest level. */
978 if (OpLevel(member) < MAXOPLEVEL)
981 /* Only handle the members with the flags that we are interested in. */
982 if ((member->status & CHFL_VOICED_OR_OPPED) == current_flags[flag_cnt])
984 if (msgq_bufleft(mb) < NUMNICKLEN + 3 + MAXOPLEVELDIGITS)
985 /* The 3 + MAXOPLEVELDIGITS is a possible ",:v999". */
987 full = 1; /* Make sure we continue after
989 /* Ensure the new BURST line contains the current
990 * ":mode", except when there is no mode yet. */
991 new_mode = (flag_cnt > 0) ? 1 : 0;
992 break; /* Do not add this member to this message */
994 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
995 first = 0; /* From now on, use commas to add new nicks */
998 * Do we have a nick with a new mode ?
999 * Or are we starting a new BURST line?
1004 * This means we are at the _first_ member that has only
1005 * voice, or the first member that has only ops, or the
1006 * first member that has voice and ops (so we get here
1007 * at most three times, plus once for every start of
1008 * a continued BURST line where only these modes is current.
1009 * In the two cases where the current mode includes ops,
1010 * we need to add the _absolute_ value of the oplevel to the mode.
1012 char tbuf[3 + MAXOPLEVELDIGITS] = ":";
1015 if (HasVoice(member)) /* flag_cnt == 1 or 3 */
1017 if (IsChanOp(member)) /* flag_cnt == 2 or 3 */
1019 /* append the absolute value of the oplevel */
1021 loc += ircd_snprintf(0, tbuf + loc, sizeof(tbuf) - loc, "%u", last_oplevel = member->oplevel);
1026 msgq_append(&me, mb, tbuf);
1029 else if (send_oplevels && flag_cnt > 1 && last_oplevel != member->oplevel)
1032 * This can't be the first member of a (continued) BURST
1033 * message because then either flag_cnt == 0 or new_mode == 1
1034 * Now we need to append the incremental value of the oplevel.
1036 char tbuf[2 + MAXOPLEVELDIGITS];
1037 ircd_snprintf(0, tbuf, sizeof(tbuf), ":%u", member->oplevel - last_oplevel);
1038 last_oplevel = member->oplevel;
1039 msgq_append(&me, mb, tbuf);
1042 /* Go to the next `member'. */
1044 member = member->next_member;
1046 member = opped_members[++opped_members_index];
1051 /* Point `member' at the start of the list again. */
1054 member = chptr->members;
1055 /* Now, after one loop, we know the number of ops and can
1056 * allocate the dynamic array with pointer to the ops. */
1057 opped_members = (struct Membership**)
1058 MyMalloc((number_of_ops + 1) * sizeof(struct Membership*));
1059 opped_members[number_of_ops] = NULL; /* Needed for loop termination */
1063 /* At the end of the second loop, sort the opped members with
1064 * increasing op-level, so that we will output them in the
1065 * correct order (and all op-level increments stay positive) */
1067 qsort(opped_members, number_of_ops,
1068 sizeof(struct Membership*), compare_member_oplevel);
1069 /* The third and fourth loop run only over the opped members. */
1070 member = opped_members[(opped_members_index = 0)];
1073 } /* loop over 0,+v,+o,+ov */
1077 /* Attach all bans, space separated " :%ban ban ..." */
1078 for (first = 2; lp2; lp2 = lp2->next)
1080 len = strlen(lp2->banstr);
1081 if (msgq_bufleft(mb) < len + 1 + first)
1082 /* The +1 stands for the added ' '.
1083 * The +first stands for the added ":%".
1089 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
1095 send_buffer(cptr, mb, 0); /* Send this message */
1097 } /* Continue when there was something
1098 that didn't fit (full==1) */
1100 MyFree(opped_members);
1101 if (feature_bool(FEAT_TOPIC_BURST) && (chptr->topic[0] != '\0'))
1102 sendcmdto_one(&me, CMD_TOPIC, cptr, "%H %Tu %Tu :%s", chptr,
1103 chptr->creationtime, chptr->topic_time, chptr->topic);
1106 /** Canonify a mask.
1109 * @author Carlo Wood (Run),
1112 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
1113 * When the user name or host name are too long (USERLEN and HOSTLEN
1114 * respectively) then they are cut off at the start with a '*'.
1116 * The following transformations are made:
1118 * 1) xxx -> nick!*@*
1119 * 2) xxx.xxx -> *!*\@host
1120 * 3) xxx\!yyy -> nick!user\@*
1121 * 4) xxx\@yyy -> *!user\@host
1122 * 5) xxx!yyy\@zzz -> nick!user\@host
1124 * @param mask The uncanonified mask.
1125 * @returns The updated mask in a static buffer.
1127 char *pretty_mask(char *mask)
1129 static char star[2] = { '*', 0 };
1130 static char retmask[NICKLEN + USERLEN + HOSTLEN + 3];
1131 char *last_dot = NULL;
1134 /* Case 1: default */
1139 /* Do a _single_ pass through the characters of the mask: */
1140 for (ptr = mask; *ptr; ++ptr)
1144 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1148 else if (*ptr == '@')
1150 /* Case 4: Found last '@' (without finding a '!' yet) */
1155 else if (*ptr == '.' || *ptr == ':')
1157 /* Case 2: Found character specific to IP or hostname (without
1158 * finding a '!' or '@' yet) */
1168 /* Case 4 or 5: Found last '@' */
1174 if (user == star && last_dot)
1184 char *nick_end = (user != star) ? user - 1 : ptr;
1185 if (nick_end - nick > NICKLEN)
1191 char *user_end = (host != star) ? host - 1 : ptr;
1192 if (user_end - user > USERLEN)
1194 user = user_end - USERLEN;
1199 if (host != star && ptr - host > HOSTLEN)
1201 host = ptr - HOSTLEN;
1204 ircd_snprintf(0, retmask, sizeof(retmask), "%s!%s@%s", nick, user, host);
1208 /** send a banlist to a client for a channel
1210 * @param cptr Client to send the banlist to.
1211 * @param chptr Channel whose banlist to send.
1213 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1220 for (lp = chptr->banlist; lp; lp = lp->next)
1221 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->banstr,
1224 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1227 /** Get a channel block, creating if necessary.
1228 * Get Channel block for chname (and allocate a new channel
1229 * block, if it didn't exists before).
1231 * @param cptr Client joining the channel.
1232 * @param chname The name of the channel to join.
1233 * @param flag set to CGT_CREATE to create the channel if it doesn't
1236 * @returns NULL if the channel is invalid, doesn't exist and CGT_CREATE
1237 * wasn't specified or a pointer to the channel structure
1239 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1241 struct Channel *chptr;
1244 if (EmptyString(chname))
1247 len = strlen(chname);
1248 if (MyUser(cptr) && len > CHANNELLEN)
1251 *(chname + CHANNELLEN) = '\0';
1253 if ((chptr = FindChannel(chname)))
1255 if (flag == CGT_CREATE)
1257 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1259 ++UserStats.channels;
1260 memset(chptr, 0, sizeof(struct Channel));
1261 strcpy(chptr->chname, chname);
1262 if (GlobalChannelList)
1263 GlobalChannelList->prev = chptr;
1265 chptr->next = GlobalChannelList;
1266 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1267 GlobalChannelList = chptr;
1273 /** invite a user to a channel.
1275 * Adds an invite for a user to a channel. Limits the number of invites
1276 * to FEAT_MAXCHANNELSPERUSER. Does not sent notification to the user.
1278 * @param cptr The client to be invited.
1279 * @param chptr The channel to be invited to.
1281 void add_invite(struct Client *cptr, struct Channel *chptr)
1283 struct SLink *inv, **tmp;
1285 del_invite(cptr, chptr);
1287 * Delete last link in chain if the list is max length
1289 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1290 if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER))
1291 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1293 * Add client to channel invite list
1296 inv->value.cptr = cptr;
1297 inv->next = chptr->invites;
1298 chptr->invites = inv;
1300 * Add channel to the end of the client invite list
1302 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1304 inv->value.chptr = chptr;
1307 (cli_user(cptr))->invites++;
1310 /** Delete an invite
1311 * Delete Invite block from channel invite list and client invite list
1313 * @param cptr Client pointer
1314 * @param chptr Channel pointer
1316 void del_invite(struct Client *cptr, struct Channel *chptr)
1318 struct SLink **inv, *tmp;
1320 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1321 if (tmp->value.cptr == cptr)
1326 (cli_user(cptr))->invites--;
1330 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1331 if (tmp->value.chptr == chptr)
1340 /** @page zombie Explanation of Zombies
1344 * A channel member is turned into a zombie when he is kicked from a
1345 * channel but his server has not acknowledged the kick. Servers that
1346 * see the member as a zombie can accept actions he performed before
1347 * being kicked, without allowing chanop operations from outsiders or
1348 * desyncing the network.
1356 * X --a--> A --b--> B --d--> D
1361 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1362 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1364 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1365 * Remove the user immediately when no users are left on the channel.
1366 * b) On server B : remove the user (who/lp) from the channel, send a
1367 * PART upstream (to A) and pass on the KICK.
1368 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1369 * channel, and pass on the KICK.
1370 * d) On server D : remove the user (who/lp) from the channel, and pass on
1374 * - Setting the ZOMBIE flag never hurts, we either remove the
1375 * client after that or we don't.
1376 * - The KICK message was already passed on, as should be in all cases.
1377 * - `who' is removed in all cases except case a) when users are left.
1378 * - A PART is only sent upstream in case b).
1384 * 1 --- 2 --- 3 --- 4 --- 5
1389 * We also need to turn 'who' into a zombie on servers 1 and 6,
1390 * because a KICK from 'who' (kicking someone else in that direction)
1391 * can arrive there afterward - which should not be bounced itself.
1392 * Therefore case a) also applies for servers 1 and 6.
1397 /** Turn a user on a channel into a zombie
1398 * This function turns a user into a zombie (see \ref zombie)
1400 * @param member The structure representing this user on this channel.
1401 * @param who The client that is being kicked.
1402 * @param cptr The connection the kick came from.
1403 * @param sptr The client that is doing the kicking.
1404 * @param chptr The channel the user is being kicked from.
1406 void make_zombie(struct Membership* member, struct Client* who,
1407 struct Client* cptr, struct Client* sptr, struct Channel* chptr)
1409 assert(0 != member);
1414 /* Default for case a): */
1417 /* Case b) or c) ?: */
1418 if (MyUser(who)) /* server 4 */
1420 if (IsServer(cptr)) /* Case b) ? */
1421 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1422 remove_user_from_channel(who, chptr);
1425 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1427 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1428 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1429 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1431 remove_user_from_channel(who, chptr);
1436 /* Case a) (servers 1, 2, 3 and 6) */
1437 if (channel_all_zombies(chptr))
1438 remove_user_from_channel(who, chptr);
1440 /* XXX Can't actually call Debug here; if the channel is all zombies,
1441 * chptr will no longer exist when we get here.
1442 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1446 /** returns the number of zombies on a channel
1447 * @param chptr Channel to count zombies in.
1449 * @returns The number of zombies on the channel.
1451 int number_of_zombies(struct Channel *chptr)
1453 struct Membership* member;
1457 for (member = chptr->members; member; member = member->next_member) {
1458 if (IsZombie(member))
1464 /** Concatenate some strings together.
1465 * This helper function builds an argument string in strptr, consisting
1466 * of the original string, a space, and str1 and str2 concatenated (if,
1467 * of course, str2 is not NULL)
1469 * @param strptr The buffer to concatenate into
1470 * @param strptr_i modified offset to the position to modify
1471 * @param str1 The string to concatenate from.
1472 * @param str2 The second string to contatenate from.
1473 * @param c Charactor to separate the string from str1 and str2.
1476 build_string(char *strptr, int *strptr_i, const char *str1,
1477 const char *str2, char c)
1480 strptr[(*strptr_i)++] = c;
1483 strptr[(*strptr_i)++] = *(str1++);
1487 strptr[(*strptr_i)++] = *(str2++);
1489 strptr[(*strptr_i)] = '\0';
1492 /** Check a channel for join-delayed members.
1493 * @param[in] chan Channel to search.
1494 * @return Non-zero if any members are join-delayed; false if none are.
1497 find_delayed_joins(const struct Channel *chan)
1499 const struct Membership *memb;
1500 for (memb = chan->members; memb; memb = memb->next_member)
1501 if (IsDelayedJoin(memb))
1506 /** Flush out the modes
1507 * This is the workhorse of our ModeBuf suite; this actually generates the
1508 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1510 * @param mbuf The mode buffer to flush
1511 * @param all If true, flush all modes, otherwise leave partial modes in the
1517 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1519 /* we only need the flags that don't take args right now */
1520 static int flags[] = {
1521 /* MODE_CHANOP, 'o', */
1522 /* MODE_VOICE, 'v', */
1525 MODE_MODERATED, 'm',
1526 MODE_TOPICLIMIT, 't',
1527 MODE_INVITEONLY, 'i',
1528 MODE_NOPRIVMSGS, 'n',
1531 MODE_REGISTERED, 'R',
1532 /* MODE_KEY, 'k', */
1533 /* MODE_BAN, 'b', */
1535 /* MODE_APASS, 'A', */
1536 /* MODE_UPASS, 'U', */
1539 static int local_flags[] = {
1540 MODE_WASDELJOINS, 'd',
1546 struct Client *app_source; /* where the MODE appears to come from */
1548 char addbuf[20], addbuf_local[20]; /* accumulates +psmtin, etc. */
1549 int addbuf_i = 0, addbuf_local_i = 0;
1550 char rembuf[20], rembuf_local[20]; /* accumulates -psmtin, etc. */
1551 int rembuf_i = 0, rembuf_local_i = 0;
1552 char *bufptr; /* we make use of indirection to simplify the code */
1555 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1557 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1559 char *strptr; /* more indirection to simplify the code */
1562 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1565 char limitbuf[20]; /* convert limits to strings */
1567 unsigned int limitdel = MODE_LIMIT;
1571 /* If the ModeBuf is empty, we have nothing to do */
1572 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1575 /* Ok, if we were given the OPMODE flag, or its a server, hide the source.
1577 if (feature_bool(FEAT_HIS_MODEWHO) &&
1578 (mbuf->mb_dest & MODEBUF_DEST_OPMODE ||
1579 IsServer(mbuf->mb_source) ||
1580 IsMe(mbuf->mb_source)))
1583 app_source = mbuf->mb_source;
1585 /* Must be set if going -D and some clients are hidden */
1586 if ((mbuf->mb_rem & MODE_DELJOINS)
1587 && !(mbuf->mb_channel->mode.mode & (MODE_DELJOINS | MODE_WASDELJOINS))
1588 && find_delayed_joins(mbuf->mb_channel)) {
1589 mbuf->mb_channel->mode.mode |= MODE_WASDELJOINS;
1590 mbuf->mb_add |= MODE_WASDELJOINS;
1591 mbuf->mb_rem &= ~MODE_WASDELJOINS;
1594 /* +d must be cleared if +D is set */
1595 if ((mbuf->mb_add & MODE_DELJOINS)
1596 && (mbuf->mb_channel->mode.mode & MODE_WASDELJOINS)) {
1597 mbuf->mb_channel->mode.mode &= ~MODE_WASDELJOINS;
1598 mbuf->mb_add &= ~MODE_WASDELJOINS;
1599 mbuf->mb_rem |= MODE_WASDELJOINS;
1603 * Account for user we're bouncing; we have to get it in on the first
1604 * bounced MODE, or we could have problems
1606 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1607 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1609 /* Calculate the simple flags */
1610 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1611 if (*flag_p & mbuf->mb_add)
1612 addbuf[addbuf_i++] = flag_p[1];
1613 else if (*flag_p & mbuf->mb_rem)
1614 rembuf[rembuf_i++] = flag_p[1];
1617 /* Some flags may be for local display only. */
1618 for (flag_p = local_flags; flag_p[0]; flag_p += 2) {
1619 if (*flag_p & mbuf->mb_add)
1620 addbuf_local[addbuf_local_i++] = flag_p[1];
1621 else if (*flag_p & mbuf->mb_rem)
1622 rembuf_local[rembuf_local_i++] = flag_p[1];
1625 /* Now go through the modes with arguments... */
1626 for (i = 0; i < mbuf->mb_count; i++) {
1627 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1629 bufptr_i = &addbuf_i;
1632 bufptr_i = &rembuf_i;
1635 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1636 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1638 if ((totalbuflen - IRCD_MAX(9, tmp)) <= 0) /* don't overflow buffer */
1639 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1641 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1642 totalbuflen -= IRCD_MAX(9, tmp) + 1;
1644 } else if (MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS)) {
1645 tmp = strlen(MB_STRING(mbuf, i));
1647 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1648 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1651 switch(MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS))
1663 bufptr[(*bufptr_i)++] = mode_char;
1664 totalbuflen -= tmp + 1;
1666 } else if (MB_TYPE(mbuf, i) & MODE_KEY) {
1667 tmp = (mbuf->mb_dest & MODEBUF_DEST_NOKEY ? 1 :
1668 strlen(MB_STRING(mbuf, i)));
1670 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1671 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1673 bufptr[(*bufptr_i)++] = 'k';
1674 totalbuflen -= tmp + 1;
1676 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1677 /* if it's a limit, we also format the number */
1678 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
1680 tmp = strlen(limitbuf);
1682 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1683 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1685 bufptr[(*bufptr_i)++] = 'l';
1686 totalbuflen -= tmp + 1;
1691 /* terminate the mode strings */
1692 addbuf[addbuf_i] = '\0';
1693 rembuf[rembuf_i] = '\0';
1694 addbuf_local[addbuf_local_i] = '\0';
1695 rembuf_local[rembuf_local_i] = '\0';
1697 /* If we're building a user visible MODE or HACK... */
1698 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1699 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1700 MODEBUF_DEST_LOG)) {
1701 /* Set up the parameter strings */
1707 for (i = 0; i < mbuf->mb_count; i++) {
1708 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1711 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1713 strptr_i = &addstr_i;
1716 strptr_i = &remstr_i;
1719 /* deal with clients... */
1720 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1721 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1723 /* deal with bans... */
1724 else if (MB_TYPE(mbuf, i) & MODE_BAN)
1725 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1727 /* deal with keys... */
1728 else if (MB_TYPE(mbuf, i) & MODE_KEY)
1729 build_string(strptr, strptr_i, mbuf->mb_dest & MODEBUF_DEST_NOKEY ?
1730 "*" : MB_STRING(mbuf, i), 0, ' ');
1732 /* deal with invisible passwords */
1733 else if (MB_TYPE(mbuf, i) & (MODE_APASS | MODE_UPASS))
1734 build_string(strptr, strptr_i, "*", 0, ' ');
1737 * deal with limit; note we cannot include the limit parameter if we're
1740 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1741 (MODE_ADD | MODE_LIMIT))
1742 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1745 /* send the messages off to their destination */
1746 if (mbuf->mb_dest & MODEBUF_DEST_HACK2)
1747 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1749 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1750 mbuf->mb_source : app_source),
1751 mbuf->mb_channel->chname,
1752 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1753 addbuf, remstr, addstr,
1754 mbuf->mb_channel->creationtime);
1756 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1757 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1758 "%s%s%s%s%s%s [%Tu]",
1759 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1760 mbuf->mb_source : app_source),
1761 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1762 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1763 mbuf->mb_channel->creationtime);
1765 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1766 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1768 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1769 mbuf->mb_source : app_source),
1770 mbuf->mb_channel->chname,
1771 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1772 addbuf, remstr, addstr,
1773 mbuf->mb_channel->creationtime);
1775 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1776 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1777 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1778 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1779 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1781 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1782 sendcmdto_channel_butserv_butone(app_source, CMD_MODE, mbuf->mb_channel, NULL, 0,
1783 "%H %s%s%s%s%s%s%s%s", mbuf->mb_channel,
1784 rembuf_i || rembuf_local_i ? "-" : "",
1785 rembuf, rembuf_local,
1786 addbuf_i || addbuf_local_i ? "+" : "",
1787 addbuf, addbuf_local,
1791 /* Now are we supposed to propagate to other servers? */
1792 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1793 /* set up parameter string */
1800 * limit is supressed if we're removing it; we have to figure out which
1801 * direction is the direction for it to be removed, though...
1803 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) ? MODE_DEL : MODE_ADD;
1805 for (i = 0; i < mbuf->mb_count; i++) {
1806 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1809 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1811 strptr_i = &addstr_i;
1814 strptr_i = &remstr_i;
1817 /* if we're changing oplevels and we know the oplevel, pass it on */
1818 if ((MB_TYPE(mbuf, i) & MODE_CHANOP)
1819 && MB_OPLEVEL(mbuf, i) < MAXOPLEVEL)
1820 *strptr_i += ircd_snprintf(0, strptr + *strptr_i, BUFSIZE - *strptr_i,
1822 NumNick(MB_CLIENT(mbuf, i)),
1823 MB_OPLEVEL(mbuf, i));
1825 /* deal with other modes that take clients */
1826 else if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1827 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1829 /* deal with modes that take strings */
1830 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS))
1831 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1834 * deal with the limit. Logic here is complicated; if HACK2 is set,
1835 * we're bouncing the mode, so sense is reversed, and we have to
1836 * include the original limit if it looks like it's being removed
1838 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1839 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1842 /* we were told to deop the source */
1843 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1844 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1845 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1846 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1848 /* mark that we've done this, so we don't do it again */
1849 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1852 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1853 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1854 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1855 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1856 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1857 addbuf, remstr, addstr);
1858 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1860 * If HACK2 was set, we're bouncing; we send the MODE back to
1861 * the connection we got it from with the senses reversed and
1862 * the proper TS; origin is us
1864 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1865 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1866 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1867 mbuf->mb_channel->creationtime);
1870 * We're propagating a normal (or HACK3 or HACK4) MODE command
1871 * to the rest of the network. We send the actual channel TS.
1873 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1874 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1875 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1876 addbuf, remstr, addstr,
1877 mbuf->mb_channel->creationtime);
1881 /* We've drained the ModeBuf... */
1886 /* reinitialize the mode-with-arg slots */
1887 for (i = 0; i < MAXMODEPARAMS; i++) {
1888 /* If we saved any, pack them down */
1889 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1890 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1891 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1893 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1895 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1896 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1898 MB_TYPE(mbuf, i) = 0;
1899 MB_UINT(mbuf, i) = 0;
1902 /* If we're supposed to flush it all, do so--all hail tail recursion */
1903 if (all && mbuf->mb_count)
1904 return modebuf_flush_int(mbuf, 1);
1909 /** Initialise a modebuf
1910 * This routine just initializes a ModeBuf structure with the information
1911 * needed and the options given.
1913 * @param mbuf The mode buffer to initialise.
1914 * @param source The client that is performing the mode.
1916 * @param chan The channel that the mode is being performed upon.
1920 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1921 struct Client *connect, struct Channel *chan, unsigned int dest)
1926 assert(0 != source);
1930 if (IsLocalChannel(chan->chname)) dest &= ~MODEBUF_DEST_SERVER;
1934 mbuf->mb_source = source;
1935 mbuf->mb_connect = connect;
1936 mbuf->mb_channel = chan;
1937 mbuf->mb_dest = dest;
1940 /* clear each mode-with-parameter slot */
1941 for (i = 0; i < MAXMODEPARAMS; i++) {
1942 MB_TYPE(mbuf, i) = 0;
1943 MB_UINT(mbuf, i) = 0;
1947 /** Append a new mode to a modebuf
1948 * This routine simply adds modes to be added or deleted; do a binary OR
1949 * with either MODE_ADD or MODE_DEL
1951 * @param mbuf Mode buffer
1952 * @param mode MODE_ADD or MODE_DEL OR'd with MODE_PRIVATE etc.
1955 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1958 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1960 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1961 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS | MODE_REGONLY |
1962 MODE_DELJOINS | MODE_WASDELJOINS | MODE_REGISTERED);
1964 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1967 if (mode & MODE_ADD) {
1968 mbuf->mb_rem &= ~mode;
1969 mbuf->mb_add |= mode;
1971 mbuf->mb_add &= ~mode;
1972 mbuf->mb_rem |= mode;
1976 /** Append a mode that takes an int argument to the modebuf
1978 * This routine adds a mode to be added or deleted that takes a unsigned
1979 * int parameter; mode may *only* be the relevant mode flag ORed with one
1980 * of MODE_ADD or MODE_DEL
1982 * @param mbuf The mode buffer to append to.
1983 * @param mode The mode to append.
1984 * @param uint The argument to the mode.
1987 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1990 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1992 if (mode == (MODE_LIMIT | MODE_DEL)) {
1993 mbuf->mb_rem |= mode;
1996 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1997 MB_UINT(mbuf, mbuf->mb_count) = uint;
1999 /* when we've reached the maximal count, flush the buffer */
2000 if (++mbuf->mb_count >=
2001 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2002 modebuf_flush_int(mbuf, 0);
2005 /** append a string mode
2006 * This routine adds a mode to be added or deleted that takes a string
2007 * parameter; mode may *only* be the relevant mode flag ORed with one of
2008 * MODE_ADD or MODE_DEL
2010 * @param mbuf The mode buffer to append to.
2011 * @param mode The mode to append.
2012 * @param string The string parameter to append.
2013 * @param free If the string should be free'd later.
2016 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
2020 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2022 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
2023 MB_STRING(mbuf, mbuf->mb_count) = string;
2025 /* when we've reached the maximal count, flush the buffer */
2026 if (++mbuf->mb_count >=
2027 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2028 modebuf_flush_int(mbuf, 0);
2031 /** Append a mode on a client to a modebuf.
2032 * This routine adds a mode to be added or deleted that takes a client
2033 * parameter; mode may *only* be the relevant mode flag ORed with one of
2034 * MODE_ADD or MODE_DEL
2036 * @param mbuf The modebuf to append the mode to.
2037 * @param mode The mode to append.
2038 * @param client The client argument to append.
2039 * @param oplevel The oplevel the user had or will have
2042 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
2043 struct Client *client, int oplevel)
2046 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2048 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2049 MB_CLIENT(mbuf, mbuf->mb_count) = client;
2050 MB_OPLEVEL(mbuf, mbuf->mb_count) = oplevel;
2052 /* when we've reached the maximal count, flush the buffer */
2053 if (++mbuf->mb_count >=
2054 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2055 modebuf_flush_int(mbuf, 0);
2058 /** The exported binding for modebuf_flush()
2060 * @param mbuf The mode buffer to flush.
2062 * @see modebuf_flush_int()
2065 modebuf_flush(struct ModeBuf *mbuf)
2067 return modebuf_flush_int(mbuf, 1);
2070 /* This extracts the simple modes contained in mbuf
2072 * @param mbuf The mode buffer to extract the modes from.
2073 * @param buf The string buffer to write the modes into.
2076 modebuf_extract(struct ModeBuf *mbuf, char *buf)
2078 static int flags[] = {
2079 /* MODE_CHANOP, 'o', */
2080 /* MODE_VOICE, 'v', */
2083 MODE_MODERATED, 'm',
2084 MODE_TOPICLIMIT, 't',
2085 MODE_INVITEONLY, 'i',
2086 MODE_NOPRIVMSGS, 'n',
2090 MODE_REGISTERED, 'R',
2091 /* MODE_BAN, 'b', */
2098 int i, bufpos = 0, len;
2100 char *key = 0, limitbuf[20];
2101 char *apass = 0, *upass = 0;
2110 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
2111 if (MB_TYPE(mbuf, i) & MODE_ADD) {
2112 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT | MODE_APASS | MODE_UPASS);
2114 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
2115 key = MB_STRING(mbuf, i);
2116 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
2117 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
2118 else if (MB_TYPE(mbuf, i) & MODE_UPASS)
2119 upass = MB_STRING(mbuf, i);
2120 else if (MB_TYPE(mbuf, i) & MODE_APASS)
2121 apass = MB_STRING(mbuf, i);
2128 buf[bufpos++] = '+'; /* start building buffer */
2130 for (flag_p = flags; flag_p[0]; flag_p += 2)
2132 buf[bufpos++] = flag_p[1];
2134 for (i = 0, len = bufpos; i < len; i++) {
2136 build_string(buf, &bufpos, key, 0, ' ');
2137 else if (buf[i] == 'l')
2138 build_string(buf, &bufpos, limitbuf, 0, ' ');
2139 else if (buf[i] == 'U')
2140 build_string(buf, &bufpos, upass, 0, ' ');
2141 else if (buf[i] == 'A')
2142 build_string(buf, &bufpos, apass, 0, ' ');
2150 /** Simple function to invalidate a channel's ban cache.
2152 * This function marks all members of the channel as being neither
2153 * banned nor banned.
2155 * @param chan The channel to operate on.
2158 mode_ban_invalidate(struct Channel *chan)
2160 struct Membership *member;
2162 for (member = chan->members; member; member = member->next_member)
2163 ClearBanValid(member);
2166 /** Simple function to drop invite structures
2168 * Remove all the invites on the channel.
2170 * @param chan Channel to remove invites from.
2174 mode_invite_clear(struct Channel *chan)
2176 while (chan->invites)
2177 del_invite(chan->invites->value.cptr, chan);
2180 /* What we've done for mode_parse so far... */
2181 #define DONE_LIMIT 0x01 /**< We've set the limit */
2182 #define DONE_KEY_ADD 0x02 /**< We've set the key */
2183 #define DONE_BANLIST 0x04 /**< We've sent the ban list */
2184 #define DONE_NOTOPER 0x08 /**< We've sent a "Not oper" error */
2185 #define DONE_BANCLEAN 0x10 /**< We've cleaned bans... */
2186 #define DONE_UPASS_ADD 0x20 /**< We've set user pass */
2187 #define DONE_APASS_ADD 0x40 /**< We've set admin pass */
2188 #define DONE_KEY_DEL 0x80 /**< We've removed the key */
2189 #define DONE_UPASS_DEL 0x100 /**< We've removed the user pass */
2190 #define DONE_APASS_DEL 0x200 /**< We've removed the admin pass */
2193 struct ModeBuf *mbuf;
2194 struct Client *cptr;
2195 struct Client *sptr;
2196 struct Channel *chptr;
2197 struct Membership *member;
2208 struct Ban banlist[MAXPARA];
2211 unsigned short oplevel;
2212 struct Client *client;
2213 } cli_change[MAXPARA];
2216 /** Helper function to send "Not oper" or "Not member" messages
2217 * Here's a helper function to deal with sending along "Not oper" or
2218 * "Not member" messages
2220 * @param state Parsing State object
2223 send_notoper(struct ParseState *state)
2225 if (state->done & DONE_NOTOPER)
2228 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
2229 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
2231 state->done |= DONE_NOTOPER;
2235 * Helper function to convert limits
2237 * @param state Parsing state object.
2241 mode_parse_limit(struct ParseState *state, int *flag_p)
2243 unsigned int t_limit;
2245 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
2246 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2249 if (state->parc <= 0) { /* warn if not enough args */
2250 if (MyUser(state->sptr))
2251 need_more_params(state->sptr, "MODE +l");
2255 t_limit = strtoul(state->parv[state->args_used++], 0, 10); /* grab arg */
2259 if ((int)t_limit<0) /* don't permit a negative limit */
2262 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2263 (!t_limit || t_limit == state->chptr->mode.limit))
2266 t_limit = state->chptr->mode.limit;
2268 /* If they're not an oper, they can't change modes */
2269 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2270 send_notoper(state);
2274 /* Can't remove a limit that's not there */
2275 if (state->dir == MODE_DEL && !state->chptr->mode.limit)
2278 /* Skip if this is a burst and a lower limit than this is set already */
2279 if ((state->flags & MODE_PARSE_BURST) &&
2280 (state->chptr->mode.mode & flag_p[0]) &&
2281 (state->chptr->mode.limit < t_limit))
2284 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
2286 state->done |= DONE_LIMIT;
2291 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
2293 if (state->flags & MODE_PARSE_SET) { /* set the limit */
2294 if (state->dir & MODE_ADD) {
2295 state->chptr->mode.mode |= flag_p[0];
2296 state->chptr->mode.limit = t_limit;
2298 state->chptr->mode.mode &= ~flag_p[0];
2299 state->chptr->mode.limit = 0;
2304 /** Helper function to validate key-like parameters.
2306 * @param[in] state Parse state for feedback to user.
2307 * @param[in] s Key to validate.
2308 * @param[in] command String to pass for need_more_params() command.
2309 * @return Zero on an invalid key, non-zero if the key was okay.
2312 is_clean_key(struct ParseState *state, char *s, char *command)
2317 if (MyUser(state->sptr))
2318 need_more_params(state->sptr, command);
2321 else if (s[0] == ':') {
2322 if (MyUser(state->sptr))
2323 send_reply(state->sptr, ERR_INVALIDKEY, state->chptr->chname);
2326 for (ii = 0; (ii <= KEYLEN) && (s[ii] != '\0'); ++ii) {
2327 if ((unsigned char)s[ii] <= ' ' || s[ii] == ',') {
2328 if (MyUser(state->sptr))
2329 send_reply(state->sptr, ERR_INVALIDKEY, state->chptr->chname);
2334 if (MyUser(state->sptr))
2335 send_reply(state->sptr, ERR_INVALIDKEY, state->chptr->chname);
2342 * Helper function to convert keys
2345 mode_parse_key(struct ParseState *state, int *flag_p)
2349 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2352 if (state->parc <= 0) { /* warn if not enough args */
2353 if (MyUser(state->sptr))
2354 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2359 t_str = state->parv[state->args_used++]; /* grab arg */
2363 /* If they're not an oper, they can't change modes */
2364 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2365 send_notoper(state);
2369 /* allow removing and then adding key, but not adding and then removing */
2370 if (state->dir == MODE_ADD)
2372 if (state->done & DONE_KEY_ADD)
2374 state->done |= DONE_KEY_ADD;
2378 if (state->done & (DONE_KEY_ADD | DONE_KEY_DEL))
2380 state->done |= DONE_KEY_DEL;
2383 /* If the key is invalid, tell the user and bail. */
2384 if (!is_clean_key(state, t_str, state->dir == MODE_ADD ? "MODE +k" :
2391 /* Skip if this is a burst, we have a key already and the new key is
2392 * after the old one alphabetically */
2393 if ((state->flags & MODE_PARSE_BURST) &&
2394 *(state->chptr->mode.key) &&
2395 ircd_strcmp(state->chptr->mode.key, t_str) <= 0)
2398 /* can't add a key if one is set, nor can one remove the wrong key */
2399 if (!(state->flags & MODE_PARSE_FORCE))
2400 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2401 (state->dir == MODE_DEL &&
2402 ircd_strcmp(state->chptr->mode.key, t_str))) {
2403 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2407 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2408 !ircd_strcmp(state->chptr->mode.key, t_str))
2409 return; /* no key change */
2411 if (state->flags & MODE_PARSE_BOUNCE) {
2412 if (*state->chptr->mode.key) /* reset old key */
2413 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2414 state->chptr->mode.key, 0);
2415 else /* remove new bogus key */
2416 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2417 } else /* send new key */
2418 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2420 if (state->flags & MODE_PARSE_SET) {
2421 if (state->dir == MODE_DEL) /* remove the old key */
2422 *state->chptr->mode.key = '\0';
2424 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2429 * Helper function to convert user passes
2432 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);
2470 send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname,
2471 (TStime() - state->chptr->creationtime < 172800) ?
2472 "approximately 4-5 minutes" : "approximately 48 hours");
2477 /* allow removing and then adding upass, but not adding and then removing */
2478 if (state->dir == MODE_ADD)
2480 if (state->done & DONE_UPASS_ADD)
2482 state->done |= DONE_UPASS_ADD;
2486 if (state->done & (DONE_UPASS_ADD | DONE_UPASS_DEL))
2488 state->done |= DONE_UPASS_DEL;
2491 /* If the Upass is invalid, tell the user and bail. */
2492 if (!is_clean_key(state, t_str, 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 /* Skip if this is a burst, we have a Upass already and the new Upass is
2524 * after the old one alphabetically */
2525 if ((state->flags & MODE_PARSE_BURST) &&
2526 *(state->chptr->mode.upass) &&
2527 ircd_strcmp(state->chptr->mode.upass, t_str) <= 0)
2530 if (state->flags & MODE_PARSE_BOUNCE) {
2531 if (*state->chptr->mode.upass) /* reset old upass */
2532 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2533 state->chptr->mode.upass, 0);
2534 else /* remove new bogus upass */
2535 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2536 } else /* send new upass */
2537 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2539 if (state->flags & MODE_PARSE_SET) {
2540 if (state->dir == MODE_DEL) /* remove the old upass */
2541 *state->chptr->mode.upass = '\0';
2543 ircd_strncpy(state->chptr->mode.upass, t_str, KEYLEN);
2548 * Helper function to convert admin passes
2551 mode_parse_apass(struct ParseState *state, int *flag_p)
2553 struct Membership *memb;
2556 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2559 if (state->parc <= 0) { /* warn if not enough args */
2560 if (MyUser(state->sptr))
2561 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2566 t_str = state->parv[state->args_used++]; /* grab arg */
2570 /* If they're not an oper, they can't change modes */
2571 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2572 send_notoper(state);
2576 if (MyUser(state->sptr)) {
2577 if (state->flags & MODE_PARSE_FORCE) {
2578 /* If an unprivileged oper is trying to force it, refuse. */
2579 if (!HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
2580 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2581 state->chptr->chname);
2585 /* If they are not the channel manager, they are not allowed to change it. */
2586 if (!IsChannelManager(state->member)) {
2587 if (*state->chptr->mode.apass) {
2588 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2589 state->chptr->chname);
2591 send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname,
2592 (TStime() - state->chptr->creationtime < 172800) ?
2593 "approximately 4-5 minutes" : "approximately 48 hours");
2597 /* Can't remove the Apass while Upass is still set. */
2598 if (state->dir == MODE_DEL && *state->chptr->mode.upass) {
2599 send_reply(state->sptr, ERR_UPASSSET, state->chptr->chname, state->chptr->chname);
2602 /* Can't add an Apass if one is set, nor can one remove the wrong Apass. */
2603 if ((state->dir == MODE_ADD && *state->chptr->mode.apass) ||
2604 (state->dir == MODE_DEL && ircd_strcmp(state->chptr->mode.apass, t_str))) {
2605 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2610 /* Forbid removing the Apass if the channel is older than 48 hours
2611 * unless an oper is doing it. */
2612 if (TStime() - state->chptr->creationtime >= 172800
2613 && state->dir == MODE_DEL
2614 && !IsAnOper(state->sptr)) {
2615 send_reply(state->sptr, ERR_CHANSECURED, state->chptr->chname);
2620 /* allow removing and then adding apass, but not adding and then removing */
2621 if (state->dir == MODE_ADD)
2623 if (state->done & DONE_APASS_ADD)
2625 state->done |= DONE_APASS_ADD;
2629 if (state->done & (DONE_APASS_ADD | DONE_APASS_DEL))
2631 state->done |= DONE_APASS_DEL;
2634 /* If the Apass is invalid, tell the user and bail. */
2635 if (!is_clean_key(state, t_str, state->dir == MODE_ADD ? "MODE +A" :
2642 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2643 !ircd_strcmp(state->chptr->mode.apass, t_str))
2644 return; /* no apass change */
2646 /* Skip if this is a burst, we have an Apass already and the new Apass is
2647 * after the old one alphabetically */
2648 if ((state->flags & MODE_PARSE_BURST) &&
2649 *(state->chptr->mode.apass) &&
2650 ircd_strcmp(state->chptr->mode.apass, t_str) <= 0)
2653 if (state->flags & MODE_PARSE_BOUNCE) {
2654 if (*state->chptr->mode.apass) /* reset old apass */
2655 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2656 state->chptr->mode.apass, 0);
2657 else /* remove new bogus apass */
2658 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2659 } else /* send new apass */
2660 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2662 if (state->flags & MODE_PARSE_SET) {
2663 if (state->dir == MODE_ADD) { /* set the new apass */
2664 /* Only accept the new apass if there is no current apass or
2665 * this is a BURST. */
2666 if (state->chptr->mode.apass[0] == '\0' ||
2667 (state->flags & MODE_PARSE_BURST))
2668 ircd_strncpy(state->chptr->mode.apass, t_str, KEYLEN);
2669 /* Make it VERY clear to the user that this is a one-time password */
2670 if (MyUser(state->sptr)) {
2671 send_reply(state->sptr, RPL_APASSWARN_SET, state->chptr->mode.apass);
2672 send_reply(state->sptr, RPL_APASSWARN_SECRET, state->chptr->chname,
2673 state->chptr->mode.apass);
2675 /* Give the channel manager level 0 ops.
2676 There should not be tested for IsChannelManager here because
2677 on the local server it is impossible to set the apass if one
2678 isn't a channel manager and remote servers might need to sync
2679 the oplevel here: when someone creates a channel (and becomes
2680 channel manager) during a net.break, and only sets the Apass
2681 after the net rejoined, they will have oplevel MAXOPLEVEL on
2682 all remote servers. */
2684 SetOpLevel(state->member, 0);
2685 } else { /* remove the old apass */
2686 *state->chptr->mode.apass = '\0';
2687 /* Clear Upass so that there is never a Upass set when a zannel is burst. */
2688 *state->chptr->mode.upass = '\0';
2689 if (MyUser(state->sptr))
2690 send_reply(state->sptr, RPL_APASSWARN_CLEAR);
2691 /* Revert everyone to MAXOPLEVEL. */
2692 for (memb = state->chptr->members; memb; memb = memb->next_member) {
2693 if (memb->status & MODE_CHANOP)
2694 SetOpLevel(memb, MAXOPLEVEL);
2700 /** Compare one ban's extent to another.
2701 * This works very similarly to mmatch() but it knows about CIDR masks
2702 * and ban exceptions. If both bans are CIDR-based, compare their
2703 * address bits; otherwise, use mmatch().
2704 * @param[in] old_ban One ban.
2705 * @param[in] new_ban Another ban.
2706 * @return Zero if \a old_ban is a superset of \a new_ban, non-zero otherwise.
2709 bmatch(struct Ban *old_ban, struct Ban *new_ban)
2712 assert(old_ban != NULL);
2713 assert(new_ban != NULL);
2714 /* A ban is never treated as a superset of an exception. */
2715 if (!(old_ban->flags & BAN_EXCEPTION)
2716 && (new_ban->flags & BAN_EXCEPTION))
2718 /* If either is not an address mask, match the text masks. */
2719 if ((old_ban->flags & new_ban->flags & BAN_IPMASK) == 0)
2720 return mmatch(old_ban->banstr, new_ban->banstr);
2721 /* If the old ban has a longer prefix than new, it cannot be a superset. */
2722 if (old_ban->addrbits > new_ban->addrbits)
2724 /* Compare the masks before the hostname part. */
2725 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '\0';
2726 res = mmatch(old_ban->banstr, new_ban->banstr);
2727 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '@';
2730 /* If the old ban's mask mismatches, cannot be a superset. */
2731 if (!ipmask_check(&new_ban->address, &old_ban->address, old_ban->addrbits))
2733 /* Otherwise it depends on whether the old ban's text is a superset
2735 return mmatch(old_ban->banstr, new_ban->banstr);
2738 /** Add a ban from a ban list and mark bans that should be removed
2739 * because they overlap.
2741 * There are three invariants for a ban list. First, no ban may be
2742 * more specific than another ban. Second, no exception may be more
2743 * specific than another exception. Finally, no ban may be more
2744 * specific than any exception.
2746 * @param[in,out] banlist Pointer to head of list.
2747 * @param[in] newban Ban (or exception) to add (or remove).
2748 * @param[in] do_free If non-zero, free \a newban on failure.
2749 * @return Zero if \a newban could be applied, non-zero if not.
2751 int apply_ban(struct Ban **banlist, struct Ban *newban, int do_free)
2756 assert(newban->flags & (BAN_ADD|BAN_DEL));
2757 if (newban->flags & BAN_ADD) {
2759 /* If a less specific *active* entry is found, fail. */
2760 for (ban = *banlist; ban; ban = ban->next) {
2761 if (!bmatch(ban, newban) && !(ban->flags & BAN_DEL)) {
2766 if (!(ban->flags & (BAN_OVERLAPPED|BAN_DEL))) {
2768 totlen += strlen(ban->banstr);
2771 /* Mark more specific entries and add this one to the end of the list. */
2772 while ((ban = *banlist) != NULL) {
2773 if (!bmatch(newban, ban)) {
2774 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
2776 banlist = &ban->next;
2780 } else if (newban->flags & BAN_DEL) {
2781 size_t remove_count = 0;
2782 /* Mark more specific entries. */
2783 for (ban = *banlist; ban; ban = ban->next) {
2784 if (!bmatch(newban, ban)) {
2785 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
2791 /* If no matches were found, fail. */
2802 * Helper function to convert bans
2805 mode_parse_ban(struct ParseState *state, int *flag_p)
2808 struct Ban *ban, *newban;
2810 if (state->parc <= 0) { /* Not enough args, send ban list */
2811 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2812 send_ban_list(state->sptr, state->chptr);
2813 state->done |= DONE_BANLIST;
2819 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2822 t_str = state->parv[state->args_used++]; /* grab arg */
2826 /* If they're not an oper, they can't change modes */
2827 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2828 send_notoper(state);
2832 if ((s = strchr(t_str, ' ')))
2835 if (!*t_str || *t_str == ':') { /* warn if empty */
2836 if (MyUser(state->sptr))
2837 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2842 /* Clear all ADD/DEL/OVERLAPPED flags from ban list. */
2843 if (!(state->done & DONE_BANCLEAN)) {
2844 for (ban = state->chptr->banlist; ban; ban = ban->next)
2845 ban->flags &= ~(BAN_ADD | BAN_DEL | BAN_OVERLAPPED);
2846 state->done |= DONE_BANCLEAN;
2849 /* remember the ban for the moment... */
2850 newban = state->banlist + (state->numbans++);
2852 newban->flags = ((state->dir == MODE_ADD) ? BAN_ADD : BAN_DEL)
2853 | (*flag_p == MODE_BAN ? 0 : BAN_EXCEPTION);
2854 set_ban_mask(newban, collapse(pretty_mask(t_str)));
2855 ircd_strncpy(newban->who, IsUser(state->sptr) ? cli_name(state->sptr) : "*", NICKLEN);
2856 newban->when = TStime();
2857 apply_ban(&state->chptr->banlist, newban, 0);
2861 * This is the bottom half of the ban processor
2864 mode_process_bans(struct ParseState *state)
2866 struct Ban *ban, *newban, *prevban, *nextban;
2872 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2874 banlen = strlen(ban->banstr);
2876 nextban = ban->next;
2878 if ((ban->flags & (BAN_DEL | BAN_ADD)) == (BAN_DEL | BAN_ADD)) {
2880 prevban->next = 0; /* Break the list; ban isn't a real ban */
2882 state->chptr->banlist = 0;
2888 } else if (ban->flags & BAN_DEL) { /* Deleted a ban? */
2890 DupString(bandup, ban->banstr);
2891 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2894 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2895 if (prevban) /* clip it out of the list... */
2896 prevban->next = ban->next;
2898 state->chptr->banlist = ban->next;
2905 continue; /* next ban; keep prevban like it is */
2907 ban->flags &= BAN_IPMASK; /* unset other flags */
2908 } else if (ban->flags & BAN_ADD) { /* adding a ban? */
2910 prevban->next = 0; /* Break the list; ban isn't a real ban */
2912 state->chptr->banlist = 0;
2914 /* If we're supposed to ignore it, do so. */
2915 if (ban->flags & BAN_OVERLAPPED &&
2916 !(state->flags & MODE_PARSE_BOUNCE)) {
2920 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2921 !(state->mbuf->mb_dest & MODEBUF_DEST_OPMODE) &&
2922 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2923 count > feature_int(FEAT_MAXBANS))) {
2924 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2930 /* add the ban to the buffer */
2931 DupString(bandup, ban->banstr);
2932 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2935 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2936 newban = make_ban(ban->banstr);
2937 strcpy(newban->who, ban->who);
2938 newban->when = ban->when;
2939 newban->flags = ban->flags & BAN_IPMASK;
2941 newban->next = state->chptr->banlist; /* and link it in */
2942 state->chptr->banlist = newban;
2951 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2953 if (changed) /* if we changed the ban list, we must invalidate the bans */
2954 mode_ban_invalidate(state->chptr);
2958 * Helper function to process client changes
2961 mode_parse_client(struct ParseState *state, int *flag_p)
2965 struct Client *acptr;
2966 struct Membership *member;
2967 int oplevel = MAXOPLEVEL + 1;
2971 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2974 if (state->parc <= 0) /* return if not enough args */
2977 t_str = state->parv[state->args_used++]; /* grab arg */
2981 /* If they're not an oper, they can't change modes */
2982 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2983 send_notoper(state);
2987 if (MyUser(state->sptr)) {
2988 colon = strchr(t_str, ':');
2989 if (colon != NULL) {
2991 req_oplevel = atoi(colon);
2992 if (*flag_p == CHFL_VOICE || state->dir == MODE_DEL) {
2993 /* Ignore the colon and its argument. */
2994 } else if (!(state->flags & MODE_PARSE_FORCE)
2996 && (req_oplevel < OpLevel(state->member)
2997 || (req_oplevel == OpLevel(state->member)
2998 && OpLevel(state->member) < MAXOPLEVEL)
2999 || req_oplevel > MAXOPLEVEL)) {
3000 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
3001 t_str, state->chptr->chname,
3002 OpLevel(state->member), req_oplevel, "op",
3003 OpLevel(state->member) == req_oplevel ? "the same" : "a higher");
3004 } else if (req_oplevel <= MAXOPLEVEL)
3005 oplevel = req_oplevel;
3007 /* find client we're manipulating */
3008 acptr = find_chasing(state->sptr, t_str, NULL);
3010 if (t_str[5] == ':') {
3012 oplevel = atoi(t_str + 6);
3014 acptr = findNUser(t_str);
3018 return; /* find_chasing() already reported an error to the user */
3020 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
3021 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
3022 state->cli_change[i].flag & flag_p[0]))
3023 break; /* found a slot */
3025 /* If we are going to bounce this deop, mark the correct oplevel. */
3026 if (state->flags & MODE_PARSE_BOUNCE
3027 && state->dir == MODE_DEL
3028 && flag_p[0] == MODE_CHANOP
3029 && (member = find_member_link(state->chptr, acptr)))
3030 oplevel = OpLevel(member);
3032 /* Store what we're doing to them */
3033 state->cli_change[i].flag = state->dir | flag_p[0];
3034 state->cli_change[i].oplevel = oplevel;
3035 state->cli_change[i].client = acptr;
3039 * Helper function to process the changed client list
3042 mode_process_clients(struct ParseState *state)
3045 struct Membership *member;
3047 for (i = 0; state->cli_change[i].flag; i++) {
3048 assert(0 != state->cli_change[i].client);
3050 /* look up member link */
3051 if (!(member = find_member_link(state->chptr,
3052 state->cli_change[i].client)) ||
3053 (MyUser(state->sptr) && IsZombie(member))) {
3054 if (MyUser(state->sptr))
3055 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
3056 cli_name(state->cli_change[i].client),
3057 state->chptr->chname);
3061 if ((state->cli_change[i].flag & MODE_ADD &&
3062 (state->cli_change[i].flag & member->status)) ||
3063 (state->cli_change[i].flag & MODE_DEL &&
3064 !(state->cli_change[i].flag & member->status)))
3065 continue; /* no change made, don't do anything */
3067 /* see if the deop is allowed */
3068 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
3069 (MODE_DEL | MODE_CHANOP)) {
3070 /* prevent +k users from being deopped */
3071 if (IsChannelService(state->cli_change[i].client)) {
3072 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
3073 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
3075 (IsServer(state->sptr) ? cli_name(state->sptr) :
3076 cli_name((cli_user(state->sptr))->server)));
3078 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
3079 send_reply(state->sptr, ERR_ISCHANSERVICE,
3080 cli_name(state->cli_change[i].client),
3081 state->chptr->chname);
3086 /* check deop for local user */
3087 if (MyUser(state->sptr)) {
3089 /* don't allow local opers to be deopped on local channels */
3090 if (state->cli_change[i].client != state->sptr &&
3091 IsLocalChannel(state->chptr->chname) &&
3092 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
3093 send_reply(state->sptr, ERR_ISOPERLCHAN,
3094 cli_name(state->cli_change[i].client),
3095 state->chptr->chname);
3099 /* Forbid deopping other members with an oplevel less than
3100 * one's own level, and other members with an oplevel the same
3101 * as one's own unless both are at MAXOPLEVEL. */
3102 if (state->sptr != state->cli_change[i].client
3104 && ((OpLevel(member) < OpLevel(state->member))
3105 || (OpLevel(member) == OpLevel(state->member)
3106 && OpLevel(member) < MAXOPLEVEL))) {
3107 int equal = (OpLevel(member) == OpLevel(state->member));
3108 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
3109 cli_name(state->cli_change[i].client),
3110 state->chptr->chname,
3111 OpLevel(state->member), OpLevel(member),
3112 "deop", equal ? "the same" : "a higher");
3118 /* set op-level of member being opped */
3119 if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
3120 (MODE_ADD | MODE_CHANOP)) {
3121 /* If a valid oplevel was specified, use it.
3122 * Otherwise, if being opped by an outsider, get MAXOPLEVEL.
3123 * Otherwise, if not an apass channel, or state->member has
3124 * MAXOPLEVEL, get oplevel MAXOPLEVEL.
3125 * Otherwise, get state->member's oplevel+1.
3127 if (state->cli_change[i].oplevel <= MAXOPLEVEL)
3128 SetOpLevel(member, state->cli_change[i].oplevel);
3129 else if (!state->member)
3130 SetOpLevel(member, MAXOPLEVEL);
3131 else if (OpLevel(state->member) >= MAXOPLEVEL)
3132 SetOpLevel(member, OpLevel(state->member));
3134 SetOpLevel(member, OpLevel(state->member) + 1);
3137 /* actually effect the change */
3138 if (state->flags & MODE_PARSE_SET) {
3139 if (state->cli_change[i].flag & MODE_ADD) {
3140 if (IsDelayedJoin(member) && !IsZombie(member))
3141 RevealDelayedJoin(member);
3142 member->status |= (state->cli_change[i].flag &
3143 (MODE_CHANOP | MODE_VOICE));
3144 if (state->cli_change[i].flag & MODE_CHANOP)
3145 ClearDeopped(member);
3147 member->status &= ~(state->cli_change[i].flag &
3148 (MODE_CHANOP | MODE_VOICE));
3151 /* accumulate the change */
3152 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
3153 state->cli_change[i].client,
3154 state->cli_change[i].oplevel);
3155 } /* for (i = 0; state->cli_change[i].flags; i++) */
3159 * Helper function to process the simple modes
3162 mode_parse_mode(struct ParseState *state, int *flag_p)
3164 /* If they're not an oper, they can't change modes */
3165 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3166 send_notoper(state);
3173 /* Local users are not permitted to change registration status */
3174 if (flag_p[0] == MODE_REGISTERED && !(state->flags & MODE_PARSE_FORCE) &&
3175 MyUser(state->sptr))
3178 if (state->dir == MODE_ADD) {
3179 state->add |= flag_p[0];
3180 state->del &= ~flag_p[0];
3182 if (flag_p[0] & MODE_SECRET) {
3183 state->add &= ~MODE_PRIVATE;
3184 state->del |= MODE_PRIVATE;
3185 } else if (flag_p[0] & MODE_PRIVATE) {
3186 state->add &= ~MODE_SECRET;
3187 state->del |= MODE_SECRET;
3190 state->add &= ~flag_p[0];
3191 state->del |= flag_p[0];
3194 assert(0 == (state->add & state->del));
3195 assert((MODE_SECRET | MODE_PRIVATE) !=
3196 (state->add & (MODE_SECRET | MODE_PRIVATE)));
3200 * This routine is intended to parse MODE or OPMODE commands and effect the
3201 * changes (or just build the bounce buffer).
3203 * \param[out] mbuf Receives parsed representation of mode change.
3204 * \param[in] cptr Connection that sent the message to this server.
3205 * \param[in] sptr Original source of the message.
3206 * \param[in] chptr Channel whose modes are being changed.
3207 * \param[in] parc Number of valid strings in \a parv.
3208 * \param[in] parv Text arguments representing mode change, with the
3209 * zero'th element containing a string like "+m" or "-o".
3210 * \param[in] flags Set of bitwise MODE_PARSE_* flags.
3211 * \param[in] member If non-null, the channel member attempting to change the modes.
3214 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
3215 struct Channel *chptr, int parc, char *parv[], unsigned int flags,
3216 struct Membership* member)
3218 static int chan_flags[] = {
3223 MODE_MODERATED, 'm',
3224 MODE_TOPICLIMIT, 't',
3225 MODE_INVITEONLY, 'i',
3226 MODE_NOPRIVMSGS, 'n',
3230 MODE_REGISTERED, 'R',
3241 unsigned int t_mode;
3243 struct ParseState state;
3254 state.chptr = chptr;
3255 state.member = member;
3258 state.flags = flags;
3259 state.dir = MODE_ADD;
3263 state.args_used = 0;
3264 state.max_args = MAXMODEPARAMS;
3267 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
3268 state.banlist[i].next = 0;
3269 state.banlist[i].who[0] = '\0';
3270 state.banlist[i].when = 0;
3271 state.banlist[i].flags = 0;
3272 state.cli_change[i].flag = 0;
3273 state.cli_change[i].client = 0;
3276 modestr = state.parv[state.args_used++];
3280 for (; *modestr; modestr++) {
3281 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
3282 if (flag_p[1] == *modestr)
3285 if (!flag_p[0]) { /* didn't find it? complain and continue */
3286 if (MyUser(state.sptr))
3287 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
3292 case '+': /* switch direction to MODE_ADD */
3293 case '-': /* switch direction to MODE_DEL */
3294 state.dir = flag_p[0];
3297 case 'l': /* deal with limits */
3298 mode_parse_limit(&state, flag_p);
3301 case 'k': /* deal with keys */
3302 mode_parse_key(&state, flag_p);
3305 case 'A': /* deal with Admin passes */
3306 if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS))
3307 mode_parse_apass(&state, flag_p);
3310 case 'U': /* deal with user passes */
3311 if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS))
3312 mode_parse_upass(&state, flag_p);
3315 case 'b': /* deal with bans */
3316 mode_parse_ban(&state, flag_p);
3319 case 'o': /* deal with ops/voice */
3321 mode_parse_client(&state, flag_p);
3324 default: /* deal with other modes */
3325 mode_parse_mode(&state, flag_p);
3327 } /* switch (*modestr) */
3328 } /* for (; *modestr; modestr++) */
3330 if (state.flags & MODE_PARSE_BURST)
3331 break; /* don't interpret any more arguments */
3333 if (state.parc > 0) { /* process next argument in string */
3334 modestr = state.parv[state.args_used++];
3338 if (IsServer(state.cptr) && !state.parc && IsDigit(*modestr)) {
3341 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
3342 break; /* we're then going to bounce the mode! */
3344 recv_ts = atoi(modestr);
3346 if (recv_ts && recv_ts < state.chptr->creationtime)
3347 state.chptr->creationtime = recv_ts; /* respect earlier TS */
3348 else if (recv_ts > state.chptr->creationtime) {
3349 struct Client *sserv;
3351 /* Check whether the originating server has fully processed
3352 * the burst to it. */
3354 if (!IsServer(sserv))
3355 sserv = cli_user(sserv)->server;
3356 if (IsBurstOrBurstAck(sserv)) {
3357 /* This is a legal but unusual case; the source server
3358 * probably just has not processed the BURST for this
3359 * channel. It SHOULD wipe out all its modes soon, so
3360 * silently ignore the mode change rather than send a
3361 * bounce that could desync modes from our side (that
3362 * have already been sent).
3364 state.mbuf->mb_add = 0;
3365 state.mbuf->mb_rem = 0;
3366 state.mbuf->mb_count = 0;
3367 return state.args_used;
3369 /* Server is desynced; bounce the mode and deop the source
3371 state.flags &= ~MODE_PARSE_SET;
3372 state.flags |= MODE_PARSE_BOUNCE;
3373 state.mbuf->mb_dest &= ~(MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK4);
3374 state.mbuf->mb_dest |= MODEBUF_DEST_BOUNCE | MODEBUF_DEST_HACK2;
3375 if (!IsServer(state.cptr))
3376 state.mbuf->mb_dest |= MODEBUF_DEST_DEOP;
3380 break; /* break out of while loop */
3381 } else if (state.flags & MODE_PARSE_STRICT ||
3382 (MyUser(state.sptr) && state.max_args <= 0)) {
3383 state.parc++; /* we didn't actually gobble the argument */
3385 break; /* break out of while loop */
3388 } /* while (*modestr) */
3391 * the rest of the function finishes building resultant MODEs; if the
3392 * origin isn't a member or an oper, skip it.
3394 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3395 return state.args_used; /* tell our parent how many args we gobbled */
3397 t_mode = state.chptr->mode.mode;
3399 if (state.del & t_mode) { /* delete any modes to be deleted... */
3400 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
3402 t_mode &= ~state.del;
3404 if (state.add & ~t_mode) { /* add any modes to be added... */
3405 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
3407 t_mode |= state.add;
3410 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
3411 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
3412 !(t_mode & MODE_INVITEONLY))
3413 mode_invite_clear(state.chptr);
3415 state.chptr->mode.mode = t_mode;
3418 if (state.flags & MODE_PARSE_WIPEOUT) {
3419 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
3420 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
3421 state.chptr->mode.limit);
3422 if (*state.chptr->mode.key && !(state.done & DONE_KEY_DEL))
3423 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
3424 state.chptr->mode.key, 0);
3425 if (*state.chptr->mode.upass && !(state.done & DONE_UPASS_DEL))
3426 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_UPASS,
3427 state.chptr->mode.upass, 0);
3428 if (*state.chptr->mode.apass && !(state.done & DONE_APASS_DEL))
3429 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_APASS,
3430 state.chptr->mode.apass, 0);
3433 if (state.done & DONE_BANCLEAN) /* process bans */
3434 mode_process_bans(&state);
3436 /* process client changes */
3437 if (state.cli_change[0].flag)
3438 mode_process_clients(&state);
3440 return state.args_used; /* tell our parent how many args we gobbled */
3444 * Initialize a join buffer
3447 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
3448 struct Client *connect, unsigned int type, char *comment,
3454 assert(0 != source);
3455 assert(0 != connect);
3457 jbuf->jb_source = source; /* just initialize struct JoinBuf */
3458 jbuf->jb_connect = connect;
3459 jbuf->jb_type = type;
3460 jbuf->jb_comment = comment;
3461 jbuf->jb_create = create;
3463 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
3464 type == JOINBUF_TYPE_PART ||
3465 type == JOINBUF_TYPE_PARTALL) ?
3466 STARTJOINLEN : STARTCREATELEN) +
3467 (comment ? strlen(comment) + 2 : 0));
3469 for (i = 0; i < MAXJOINARGS; i++)
3470 jbuf->jb_channels[i] = 0;
3474 * Add a channel to the join buffer
3477 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
3485 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
3489 is_local = IsLocalChannel(chan->chname);
3491 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
3492 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
3493 struct Membership *member = find_member_link(chan, jbuf->jb_source);
3494 if (IsUserParting(member))
3496 SetUserParting(member);
3498 /* Send notification to channel */
3499 if (!(flags & (CHFL_ZOMBIE | CHFL_DELAYED)))
3500 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL, 0,
3501 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3502 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3503 else if (MyUser(jbuf->jb_source))
3504 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
3505 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3506 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3507 /* XXX: Shouldn't we send a PART here anyway? */
3508 /* to users on the channel? Why? From their POV, the user isn't on
3509 * the channel anymore anyway. We don't send to servers until below,
3510 * when we gang all the channel parts together. Note that this is
3511 * exactly the same logic, albeit somewhat more concise, as was in
3512 * the original m_part.c */
3514 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3515 is_local) /* got to remove user here */
3516 remove_user_from_channel(jbuf->jb_source, chan);
3518 int oplevel = !chan->mode.apass[0] ? MAXOPLEVEL
3519 : (flags & CHFL_CHANNEL_MANAGER) ? 0
3521 /* Add user to channel */
3522 if ((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))
3523 add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, oplevel);
3525 add_user_to_channel(chan, jbuf->jb_source, flags, oplevel);
3527 /* send JOIN notification to all servers (CREATE is sent later). */
3528 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local)
3529 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
3530 "%H %Tu", chan, chan->creationtime);
3532 if (!((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))) {
3533 /* Send the notification to the channel */
3534 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, 0, "%H", chan);
3536 /* send an op, too, if needed */
3537 if (flags & CHFL_CHANOP && (oplevel < MAXOPLEVEL || !MyUser(jbuf->jb_source)))
3538 sendcmdto_channel_butserv_butone((chan->mode.apass[0] ? &his : jbuf->jb_source),
3539 CMD_MODE, chan, NULL, 0, "%H +o %C",
3540 chan, jbuf->jb_source);
3541 } else if (MyUser(jbuf->jb_source))
3542 sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan);
3545 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3546 jbuf->jb_type == JOINBUF_TYPE_JOIN || is_local)
3547 return; /* don't send to remote */
3549 /* figure out if channel name will cause buffer to be overflowed */
3550 len = chan ? strlen(chan->chname) + 1 : 2;
3551 if (jbuf->jb_strlen + len > BUFSIZE)
3552 joinbuf_flush(jbuf);
3554 /* add channel to list of channels to send and update counts */
3555 jbuf->jb_channels[jbuf->jb_count++] = chan;
3556 jbuf->jb_strlen += len;
3558 /* if we've used up all slots, flush */
3559 if (jbuf->jb_count >= MAXJOINARGS)
3560 joinbuf_flush(jbuf);
3564 * Flush the channel list to remote servers
3567 joinbuf_flush(struct JoinBuf *jbuf)
3569 char chanlist[BUFSIZE];
3573 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3574 jbuf->jb_type == JOINBUF_TYPE_JOIN)
3575 return 0; /* no joins to process */
3577 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
3578 build_string(chanlist, &chanlist_i,
3579 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
3580 i == 0 ? '\0' : ',');
3581 if (JOINBUF_TYPE_PART == jbuf->jb_type)
3582 /* Remove user from channel */
3583 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
3585 jbuf->jb_channels[i] = 0; /* mark slot empty */
3588 jbuf->jb_count = 0; /* reset base counters */
3589 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
3590 STARTJOINLEN : STARTCREATELEN) +
3591 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
3593 /* and send the appropriate command */
3594 switch (jbuf->jb_type) {
3595 case JOINBUF_TYPE_CREATE:
3596 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
3597 "%s %Tu", chanlist, jbuf->jb_create);
3600 case JOINBUF_TYPE_PART:
3601 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
3602 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,
3610 /* Returns TRUE (1) if client is invited, FALSE (0) if not */
3611 int IsInvited(struct Client* cptr, const void* chptr)
3615 for (lp = (cli_user(cptr))->invited; lp; lp = lp->next)
3616 if (lp->value.chptr == chptr)
3621 /* RevealDelayedJoin: sends a join for a hidden user */
3623 void RevealDelayedJoin(struct Membership *member)
3625 ClearDelayedJoin(member);
3626 sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, 0, ":%H",
3628 CheckDelayedJoins(member->channel);
3631 /* CheckDelayedJoins: checks and clear +d if necessary */
3633 void CheckDelayedJoins(struct Channel *chan)
3635 if ((chan->mode.mode & MODE_WASDELJOINS) && !find_delayed_joins(chan)) {
3636 chan->mode.mode &= ~MODE_WASDELJOINS;
3637 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan, NULL, 0,
3642 /** Send a join for the user if (s)he is a hidden member of the channel.
3644 void RevealDelayedJoinIfNeeded(struct Client *sptr, struct Channel *chptr)
3646 struct Membership *member = find_member_link(chptr, sptr);
3647 if (member && IsDelayedJoin(member))
3648 RevealDelayedJoin(member);