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
22 * @version $Id: channel.c 1906 2009-02-09 03:39:42Z entrope $
29 #include "destruct_event.h"
32 #include "ircd_alloc.h"
33 #include "ircd_chattr.h"
34 #include "ircd_defs.h"
35 #include "ircd_features.h"
37 #include "ircd_reply.h"
38 #include "ircd_snprintf.h"
39 #include "ircd_string.h"
46 #include "querycmds.h"
57 /* #include <assert.h> -- Now using assert in ircd_log.h */
62 /** Linked list containing the full list of all channels */
63 struct Channel* GlobalChannelList = 0;
65 /** Number of struct Membership*'s allocated */
66 static unsigned int membershipAllocCount;
67 /** Freelist for struct Membership*'s */
68 static struct Membership* membershipFreeList;
69 /** Freelist for struct Ban*'s */
70 static struct Ban* free_bans;
71 /** Freelist for struct MemberFlood*'s */
72 static struct MemberFlood* free_MemberFlood;
73 /** Number of ban structures allocated. */
74 static size_t bans_alloc;
75 /** Number of ban structures in use. */
76 static size_t bans_inuse;
79 /** return the length (>=0) of a chain of links.
80 * @param lp pointer to the start of the linked list
81 * @return the number of items in the list
83 static int list_length(struct SLink *lp)
87 for (; lp; lp = lp->next)
93 /** Set the mask for a ban, checking for IP masks.
94 * @param[in,out] ban Ban structure to modify.
95 * @param[in] banstr Mask to ban.
98 set_ban_mask(struct Ban *ban, const char *banstr)
101 assert(banstr != NULL);
102 ircd_strncpy(ban->banstr, banstr, sizeof(ban->banstr) - 1);
103 sep = strrchr(banstr, '@');
105 ban->nu_len = sep - banstr;
106 if (ipmask_parse(sep + 1, &ban->address, &ban->addrbits))
107 ban->flags |= BAN_IPMASK;
111 /** Allocate a new Ban structure.
112 * @param[in] banstr Ban mask to use.
113 * @return Newly allocated ban.
116 make_ban(const char *banstr)
121 free_bans = free_bans->next;
123 else if (!(ban = MyMalloc(sizeof(*ban))))
128 memset(ban, 0, sizeof(*ban));
129 set_ban_mask(ban, banstr);
133 /** Deallocate a ban structure.
134 * @param[in] ban Ban to deallocate.
137 free_ban(struct Ban *ban)
139 ban->next = free_bans;
144 /** Report ban usage to \a cptr.
145 * @param[in] cptr Client requesting information.
147 void bans_send_meminfo(struct Client *cptr)
151 for (num_free = 0, ban = free_bans; ban; ban = ban->next)
153 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Bans: inuse %zu(%zu) free %zu alloc %zu",
154 bans_inuse, bans_inuse * sizeof(*ban), num_free, bans_alloc);
157 /** return the struct Membership* that represents a client on a channel
158 * This function finds a struct Membership* which holds the state about
159 * a client on a specific channel. The code is smart enough to iterate
160 * over the channels a user is in, or the users in a channel to find the
161 * user depending on which is likely to be more efficient.
163 * @param chptr pointer to the channel struct
164 * @param cptr pointer to the client struct
166 * @returns pointer to the struct Membership representing this client on
167 * this channel. Returns NULL if the client is not on the channel.
168 * Returns NULL if the client is actually a server.
169 * @see find_channel_member()
171 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
173 struct Membership *m;
177 /* Servers don't have member links */
178 if (IsServer(cptr)||IsMe(cptr))
181 /* +k users are typically on a LOT of channels. So we iterate over who
182 * is in the channel. X/W are +k and are in about 5800 channels each.
183 * however there are typically no more than 1000 people in a channel
186 if (IsChannelService(cptr)) {
189 assert(m->channel == chptr);
195 /* Users on the other hand aren't allowed on more than 15 channels. 50%
196 * of users that are on channels are on 2 or less, 95% are on 7 or less,
197 * and 99% are on 10 or less.
200 m = (cli_user(cptr))->channel;
202 assert(m->user == cptr);
203 if (m->channel == chptr)
211 /** Find the client structure for a nick name (user)
212 * Find the client structure for a nick name (user)
213 * using history mechanism if necessary. If the client is not found, an error
214 * message (NO SUCH NICK) is generated. If the client was found
215 * through the history, chasing will be 1 and otherwise 0.
217 * This function was used extensively in the P09 days, and since we now have
218 * numeric nicks is no longer quite as important.
220 * @param sptr Pointer to the client that has requested the search
221 * @param user a string representing the client to be found
222 * @param chasing a variable set to 0 if the user was found directly,
224 * @returns a pointer the client, or NULL if the client wasn't found.
226 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
228 struct Client* who = FindClient(user);
235 if (!(who = get_history(user, feature_int(FEAT_KILLCHASETIMELIMIT)))) {
236 send_reply(sptr, ERR_NOSUCHNICK, user);
244 /** Decrement the count of users, and free if empty.
245 * Subtract one user from channel i (and free channel * block, if channel
248 * @param chptr The channel to subtract one from.
250 * @returns true (1) if channel still has members.
251 * false (0) if the channel is now empty.
253 int sub1_from_channel(struct Channel* chptr)
255 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
257 assert(0 != chptr->members);
264 if(chptr->mode.mode & MODE_PERSIST)
268 * Also channels without Apass set need to be kept alive,
269 * otherwise Bad Guys(tm) would be able to takeover
270 * existing channels too easily, and then set an Apass!
271 * However, if a channel without Apass becomes empty
272 * then we try to be kind to them and remove possible
275 chptr->mode.mode &= ~MODE_INVITEONLY;
276 chptr->mode.limit = 0;
278 * We do NOT reset a possible key or bans because when
279 * the 'channel owners' can't get in because of a key
280 * or ban then apparently there was a fight/takeover
281 * on the channel and we want them to contact IRC opers
282 * who then will educate them on the use of Apass/Upass.
284 if (!chptr->mode.apass[0]) /* If no Apass, reset all modes. */
286 struct Ban *link, *next;
287 chptr->mode.mode = 0;
288 *chptr->mode.key = '\0';
289 while (chptr->invites)
290 del_invite(chptr->invites->value.cptr, chptr);
291 for (link = chptr->banlist; link; link = next) {
295 chptr->banlist = NULL;
297 /* Immediately destruct empty -A channels if not using apass. */
298 if (!feature_bool(FEAT_OPLEVELS))
300 destruct_channel(chptr);
304 if (TStime() - chptr->creationtime < 172800) /* Channel younger than 48 hours? */
305 schedule_destruct_event_1m(chptr); /* Get rid of it in approximately 4-5 minutes */
307 schedule_destruct_event_48h(chptr); /* Get rid of it in approximately 48 hours */
312 /** Destroy an empty channel
313 * This function destroys an empty channel, removing it from hashtables,
314 * and removing any resources it may have consumed.
316 * @param chptr The channel to destroy
318 * @returns 0 (success)
320 * FIXME: Change to return void, this function never fails.
322 int destruct_channel(struct Channel* chptr)
324 struct Ban *ban, *next;
326 assert(0 == chptr->members);
329 * Now, find all invite links from channel structure
331 while (chptr->invites)
332 del_invite(chptr->invites->value.cptr, chptr);
334 for (ban = chptr->banlist; ban; ban = next)
340 chptr->prev->next = chptr->next;
342 GlobalChannelList = chptr->next;
344 chptr->next->prev = chptr->prev;
346 --UserStats.channels;
348 * make sure that channel actually got removed from hash table
350 assert(chptr->hnext == chptr);
355 /** returns Membership * if a person is joined and not a zombie
357 * @param chptr Channel
358 * @returns pointer to the client's struct Membership * on the channel if that
359 * user is a full member of the channel, or NULL otherwise.
361 * @see find_member_link()
363 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
365 struct Membership* member;
368 member = find_member_link(chptr, cptr);
369 return (member && !IsZombie(member)) ? member : 0;
372 /** Searches for a ban from a ban list that matches a user.
373 * @param[in] cptr The client to test.
374 * @param[in] banlist The list of bans to test.
375 * @return Pointer to a matching ban, or NULL if none exit.
377 static struct Ban *find_ban_with_nick(struct Client *cptr, const char *nick, struct Ban *banlist)
379 char nu[NICKLEN + USERLEN + 2];
380 char tmphost[HOSTLEN + 1];
381 char iphost[SOCKIPLEN + 1];
384 char *account_host = NULL;
387 /* Build nick!user and alternate host names. */
388 ircd_snprintf(0, nu, sizeof(nu), "%s!%s",
389 (nick ? nick : cli_name(cptr)), cli_user(cptr)->username);
390 ircd_ntoa_r(iphost, &cli_ip(cptr));
392 /* Check for all three possible hosts:
396 * But only if they are set.
398 if(IsAccount(cptr)) {
399 ircd_snprintf(0, tmphost, HOSTLEN, "%s.%s", cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
400 account_host = tmphost;
402 if(IsFakeHost(cptr)) {
403 if(HasHiddenHost(cptr)) {
404 sr = cli_user(cptr)->realhost;
406 else sr = cli_user(cptr)->fakehost;
408 else if(HasHiddenHost(cptr)) {
410 sr = cli_user(cptr)->realhost;
413 /* Walk through ban list. */
414 for (found = NULL; banlist; banlist = banlist->next) {
416 /* If we have found a positive ban already, only consider exceptions. */
417 if (found && !(banlist->flags & BAN_EXCEPTION))
419 /* Compare nick!user portion of ban. */
420 banlist->banstr[banlist->nu_len] = '\0';
421 res = match(banlist->banstr, nu);
422 banlist->banstr[banlist->nu_len] = '@';
425 /* Compare host portion of ban. */
426 hostmask = banlist->banstr + banlist->nu_len + 1;
427 if (!((banlist->flags & BAN_IPMASK)
428 && ipmask_check(&cli_ip(cptr), &banlist->address, banlist->addrbits))
429 && match(hostmask, cli_user(cptr)->host)
430 && !(account_host && !match(hostmask, account_host))
431 && !(sr && !match(hostmask, sr)))
433 /* If an exception matches, no ban can match. */
434 if (banlist->flags & BAN_EXCEPTION)
436 /* Otherwise, remember this ban but keep searching for an exception. */
442 struct Ban *find_ban(struct Client *cptr, struct Ban *banlist) {
443 return find_ban_with_nick(cptr, NULL, banlist);
447 * This function returns true if the user is banned on the said channel.
448 * This function will check the ban cache if applicable, otherwise will
449 * do the comparisons and cache the result.
451 * @param[in] member The Membership to test for banned-ness.
452 * @return Non-zero if the member is banned, zero if not.
454 static int is_banned(struct Membership* member)
456 if (IsBanValid(member))
457 return IsBanned(member);
460 if (find_ban(member->user, member->channel->banlist)) {
469 static int is_banned_with_nick(struct Membership* member, const char *nick) {
470 if (find_ban_with_nick(member->user, nick, member->channel->banlist))
476 /** add a user to a channel.
477 * adds a user to a channel by adding another link to the channels member
480 * @param chptr The channel to add to.
481 * @param who The user to add.
482 * @param flags The flags the user gets initially.
483 * @param oplevel The oplevel the user starts with.
485 void add_user_to_channel(struct Channel* chptr, struct Client* who,
486 unsigned int flags, int oplevel)
493 struct Membership* member = membershipFreeList;
495 membershipFreeList = member->next_member;
497 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
498 ++membershipAllocCount;
503 member->channel = chptr;
504 member->status = flags;
505 member->flood = NULL;
506 SetOpLevel(member, oplevel);
508 member->next_member = chptr->members;
509 if (member->next_member)
510 member->next_member->prev_member = member;
511 member->prev_member = 0;
512 chptr->members = member;
514 member->next_channel = (cli_user(who))->channel;
515 if (member->next_channel)
516 member->next_channel->prev_channel = member;
517 member->prev_channel = 0;
518 (cli_user(who))->channel = member;
520 if (chptr->destruct_event)
521 remove_destruct_event(chptr);
523 ++((cli_user(who))->joined);
527 /** Remove a person from a channel, given their Membership*
529 * @param member A member of a channel.
531 * @returns true if there are more people in the channel.
533 static int remove_member_from_channel(struct Membership* member)
535 struct Channel* chptr;
537 chptr = member->channel;
539 * unlink channel member list
541 if (member->next_member)
542 member->next_member->prev_member = member->prev_member;
543 if (member->prev_member)
544 member->prev_member->next_member = member->next_member;
546 member->channel->members = member->next_member;
549 * If this is the last delayed-join user, may have to clear WASDELJOINS.
551 if (IsDelayedJoin(member))
552 CheckDelayedJoins(chptr);
555 * unlink client channel list
557 if (member->next_channel)
558 member->next_channel->prev_channel = member->prev_channel;
559 if (member->prev_channel)
560 member->prev_channel->next_channel = member->next_channel;
562 (cli_user(member->user))->channel = member->next_channel;
564 --(cli_user(member->user))->joined;
566 member->flood = NULL;
568 member->next_member = membershipFreeList;
569 membershipFreeList = member;
571 return sub1_from_channel(chptr);
574 /** Check if all the remaining members on the channel are zombies
576 * @returns False if the channel has any non zombie members, True otherwise.
579 static int channel_all_zombies(struct Channel* chptr)
581 struct Membership* member;
583 /* Though, there are no real users we pretend this channel to have members
584 * to protect it from being destroyed.
586 if(chptr->mode.mode & MODE_PERSIST)
589 for (member = chptr->members; member; member = member->next_member) {
590 if (!IsZombie(member))
597 /** Remove a user from a channel
598 * This is the generic entry point for removing a user from a channel, this
599 * function will remove the client from the channel, and destroy the channel
600 * if there are no more normal users left.
602 * @param cptr The client
603 * @param chptr The channel
605 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
608 struct Membership* member;
611 if ((member = find_member_link(chptr, cptr))) {
612 if (remove_member_from_channel(member)) {
613 if (channel_all_zombies(chptr)) {
615 * XXX - this looks dangerous but isn't if we got the referential
616 * integrity right for channels
618 while (remove_member_from_channel(chptr->members))
625 /** Remove a user from all channels they are on.
627 * This function removes a user from all channels they are on.
629 * @param cptr The client to remove.
631 void remove_user_from_all_channels(struct Client* cptr)
633 struct Membership* chan;
635 assert(0 != cli_user(cptr));
637 while ((chan = (cli_user(cptr))->channel))
638 remove_user_from_channel(cptr, chan->channel);
641 /** Check if this user is a legitimate chanop
643 * @param cptr Client to check
644 * @param chptr Channel to check
646 * @returns True if the user is a chanop (And not a zombie), False otherwise.
649 int is_chan_op(struct Client *cptr, struct Channel *chptr)
651 struct Membership* member;
653 if ((member = find_member_link(chptr, cptr)))
654 return (!IsZombie(member) && IsChanOp(member));
659 /** Check if this user is a legitimate halfop
661 * @param cptr Client to check
662 * @param chptr Channel to check
664 * @returns True if the user is a halfop (And not a zombie), False otherwise.
667 int is_halfop(struct Client *cptr, struct Channel *chptr)
669 struct Membership* member;
671 if ((member = find_member_link(chptr, cptr)))
672 return (!IsZombie(member) && IsHalfOp(member));
677 /** Check if a user is a Zombie on a specific channel.
679 * @param cptr The client to check.
680 * @param chptr The channel to check.
682 * @returns True if the client (cptr) is a zombie on the channel (chptr),
687 int is_zombie(struct Client *cptr, struct Channel *chptr)
689 struct Membership* member;
693 if ((member = find_member_link(chptr, cptr)))
694 return IsZombie(member);
698 /** Returns if a user has voice on a channel.
700 * @param cptr The client
701 * @param chptr The channel
703 * @returns True if the client (cptr) is voiced on (chptr) and is not a zombie.
706 int has_voice(struct Client* cptr, struct Channel* chptr)
708 struct Membership* member;
711 if ((member = find_member_link(chptr, cptr)))
712 return (!IsZombie(member) && HasVoice(member));
717 /** Can this member send to a channel
719 * A user can speak on a channel iff:
721 * <li> They didn't use the Apass to gain ops.
722 * <li> They are op'd or voice'd.
723 * <li> You aren't banned.
724 * <li> The channel isn't +m
725 * <li> The channel isn't +n or you are on the channel.
728 * This function will optionally reveal a user on a delayed join channel if
729 * they are allowed to send to the channel.
731 * @param member The membership of the user
732 * @param reveal If true, the user will be "revealed" on a delayed
735 * @returns True if the client can speak on the channel.
737 int member_can_send_to_channel(struct Membership* member, int reveal)
741 /* Do not check for users on other servers: This should be a
742 * temporary desynch, or maybe they are on an older server, but
743 * we do not want to send ERR_CANNOTSENDTOCHAN more than once.
744 * Also allow XtraOps to be always able to speak.
746 if (!MyUser(member->user) || IsXtraOp(member->user))
748 if (IsDelayedJoin(member) && !IsInvisibleJoin(member) && reveal)
749 RevealDelayedJoin(member);
753 /* Discourage using the Apass to get op. They should use the Upass. */
754 if (IsChannelManager(member) && member->channel->mode.apass[0])
757 /* If you have voice or ops, you can speak. */
758 if (IsVoicedOrOpped(member))
762 * If it's moderated, and you aren't a privileged user, you can't
765 if (member->channel->mode.mode & MODE_MODERATED)
768 /* If only logged in users may join and you're not one, you can't speak. */
769 if (member->channel->mode.mode & MODE_REGONLY && !IsAccount(member->user))
772 /* If you're banned then you can't speak either. */
773 if (is_banned(member))
776 if (IsDelayedJoin(member) && reveal && !IsInvisibleJoin(member))
777 RevealDelayedJoin(member);
782 /** Check if a client can send to a channel.
784 * Has the added check over member_can_send_to_channel() of servers can
787 * @param cptr The client to check
788 * @param chptr The channel to check
789 * @param reveal If the user should be revealed (see
790 * member_can_send_to_channel())
792 * @returns true if the client is allowed to speak on the channel, false
795 * @see member_can_send_to_channel()
797 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr, int reveal)
799 struct Membership *member;
802 * Servers and extra ops can always speak on channels.
804 if (IsServer(cptr) || IsXtraOp(cptr))
807 member = find_channel_member(cptr, chptr);
810 * You can't speak if you're off channel, and it is +n (no external messages)
814 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED)) ||
815 ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(cptr)) ||
816 ((chptr->mode.mode & MODE_SSLCHAN) && !IsSSL(cptr)))
819 return !find_ban(cptr, chptr->banlist);
821 return member_can_send_to_channel(member, reveal);
824 /** Returns the name of a channel that prevents the user from changing nick.
825 * if a member and not (opped or voiced) and (banned or moderated), return
826 * the name of the first channel banned on.
828 * @param cptr The client
829 * @param new_nick The new nick of the client
831 * @returns the name of the first channel banned on, or NULL if the user
834 const char* find_no_nickchange_channel(struct Client* cptr, const char *new_nick)
837 struct Membership* member;
838 for (member = (cli_user(cptr))->channel; member;
839 member = member->next_channel) {
840 if (IsVoicedOrOpped(member))
842 if ((member->channel->mode.mode & MODE_MODERATED)
843 || (member->channel->mode.mode & MODE_REGONLY && !IsAccount(cptr))
844 || (member->channel->mode.mode & MODE_SSLCHAN && !IsSSL(cptr))
845 || is_banned(member) || (new_nick && is_banned_with_nick(member, new_nick)))
846 return member->channel->chname;
853 /** Fill mbuf/pbuf with modes from chptr
854 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
855 * with the parameters in pbuf as visible by cptr.
857 * This function will hide keys from non-op'd, non-server clients.
859 * @param cptr The client to generate the mode for.
860 * @param mbuf The buffer to write the modes into.
861 * @param pbuf The buffer to write the mode parameters into.
862 * @param buflen The length of the buffers.
863 * @param chptr The channel to get the modes from.
864 * @param member The membership of this client on this channel (or NULL
865 * if this client isn't on this channel)
868 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen,
869 struct Channel *chptr, struct Membership *member)
871 int previous_parameter = 0;
878 if (chptr->mode.mode & MODE_SECRET)
880 else if (chptr->mode.mode & MODE_PRIVATE)
882 if (chptr->mode.mode & MODE_MODERATED)
884 if (chptr->mode.mode & MODE_TOPICLIMIT)
886 if (chptr->mode.mode & MODE_INVITEONLY)
888 if (chptr->mode.mode & MODE_NOPRIVMSGS)
890 if (chptr->mode.mode & MODE_REGONLY)
892 if (chptr->mode.mode & MODE_DELJOINS)
894 else if (MyUser(cptr) && (chptr->mode.mode & MODE_WASDELJOINS))
896 if (chptr->mode.mode & MODE_NOCOLOUR)
898 if (chptr->mode.mode & MODE_NOCTCP)
900 if (chptr->mode.mode & MODE_PERSIST)
902 if (chptr->mode.mode & MODE_REGISTERED)
904 if (chptr->mode.mode & MODE_NONOTICE)
906 if (chptr->mode.mode & MODE_NOAMSGS)
908 if (chptr->mode.mode & MODE_QUARANTINE)
910 if (chptr->mode.mode & MODE_AUDITORIUM)
912 if (chptr->mode.mode & MODE_SSLCHAN)
914 if (chptr->mode.limit) {
916 ircd_snprintf(0, pbuf, buflen, "%u", chptr->mode.limit);
917 previous_parameter = 1;
919 if (chptr->mode.access) {
921 if (previous_parameter)
923 ircd_snprintf(0, pbuf, buflen, "%u", chptr->mode.access);
924 previous_parameter = 1;
926 if (*chptr->mode.altchan) {
928 if (previous_parameter)
930 strcat(pbuf, chptr->mode.altchan);
931 previous_parameter = 1;
933 if (*chptr->mode.noflood) {
935 if (previous_parameter)
937 strcat(pbuf, chptr->mode.noflood);
938 previous_parameter = 1;
940 if (*chptr->mode.key) {
942 if (previous_parameter)
944 if ((member && IsChanOpOrHalfOp(member)) || IsServer(cptr) || IsOper(cptr)) {
945 strcat(pbuf, chptr->mode.key);
948 previous_parameter = 1;
950 if (*chptr->mode.apass) {
952 if (previous_parameter)
954 if (IsServer(cptr) || IsOper(cptr)) {
955 strcat(pbuf, chptr->mode.apass);
958 previous_parameter = 1;
960 if (*chptr->mode.upass) {
962 if (previous_parameter)
964 if (IsServer(cptr) || (member && IsChanOpOrHalfOp(member) && OpLevel(member) == 0) || IsOper(cptr)) {
965 strcat(pbuf, chptr->mode.upass);
972 /** Compare two members oplevel
974 * @param mp1 Pointer to a pointer to a membership
975 * @param mp2 Pointer to a pointer to a membership
977 * @returns 0 if equal, -1 if mp1 is lower, +1 otherwise.
981 int compare_member_oplevel(const void *mp1, const void *mp2)
983 struct Membership const* member1 = *(struct Membership const**)mp1;
984 struct Membership const* member2 = *(struct Membership const**)mp2;
985 if (member1->oplevel == member2->oplevel)
987 return (member1->oplevel < member2->oplevel) ? -1 : 1;
990 /* send "cptr" a full list of the modes for channel chptr.
992 * Sends a BURST line to cptr, bursting all the modes for the channel.
994 * @param cptr Client pointer
995 * @param chptr Channel pointer
997 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
999 /* The order in which modes are generated is now mandatory */
1000 static unsigned int current_flags[8] =
1001 { 0, CHFL_VOICE, CHFL_HALFOP, CHFL_VOICE | CHFL_HALFOP, CHFL_CHANOP, CHFL_CHANOP | CHFL_VOICE, CHFL_CHANOP | CHFL_HALFOP, CHFL_CHANOP | CHFL_VOICE | CHFL_HALFOP };
1007 struct Membership* member;
1009 char modebuf[MODEBUFLEN];
1010 char parabuf[MODEBUFLEN];
1012 int number_of_ops = 0;
1013 int opped_members_index = 0;
1014 struct Membership** opped_members = NULL;
1015 int last_oplevel = 0;
1016 int send_oplevels = 0;
1021 if (IsLocalChannel(chptr->chname))
1024 member = chptr->members;
1025 lp2 = chptr->banlist;
1027 *modebuf = *parabuf = '\0';
1028 channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr, 0);
1030 for (first = 1; full; first = 0) /* Loop for multiple messages */
1032 full = 0; /* Assume by default we get it
1033 all in one message */
1035 /* (Continued) prefix: "<Y> B <channel> <TS>" */
1036 /* is there any better way we can do this? */
1037 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
1038 chptr->creationtime);
1040 if (first && modebuf[1]) /* Add simple modes (Aiklmnpstu)
1043 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
1044 msgq_append(&me, mb, " %s", modebuf);
1047 msgq_append(&me, mb, " %s", parabuf);
1051 * Attach nicks, comma separated " nick[:modes],nick[:modes],..."
1053 * First find all opless members.
1054 * Run 2 times over all members, to group the members with
1055 * and without voice together.
1056 * Then run 2 times over all opped members (which are ordered
1057 * by op-level) to also group voice and non-voice together.
1059 for (first = 1; flag_cnt < 8; new_mode = 1, ++flag_cnt)
1063 if (flag_cnt < 2 && IsChanOp(member))
1066 * The first loop (to find all non-voice/op), we count the ops.
1067 * The second loop (to find all voiced non-ops), store the ops
1068 * in a dynamic array.
1073 opped_members[opped_members_index++] = member;
1074 /* We also send oplevels if anyone is below the weakest level. */
1075 if (OpLevel(member) < MAXOPLEVEL)
1078 /* Only handle the members with the flags that we are interested in. */
1079 if ((member->status & CHFL_VOICED_OR_OPPED) == current_flags[flag_cnt])
1081 if (msgq_bufleft(mb) < NUMNICKLEN + 3 + MAXOPLEVELDIGITS)
1082 /* The 3 + MAXOPLEVELDIGITS is a possible ",:v999". */
1084 full = 1; /* Make sure we continue after
1085 sending it so far */
1086 /* Ensure the new BURST line contains the current
1087 * ":mode", except when there is no mode yet. */
1088 new_mode = (flag_cnt > 0) ? 1 : 0;
1089 break; /* Do not add this member to this message */
1091 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
1092 first = 0; /* From now on, use commas to add new nicks */
1095 * Do we have a nick with a new mode ?
1096 * Or are we starting a new BURST line?
1101 * This means we are at the _first_ member that has only
1102 * voice, or the first member that has only ops, or the
1103 * first member that has voice and ops (so we get here
1104 * at most three times, plus once for every start of
1105 * a continued BURST line where only these modes is current.
1106 * In the two cases where the current mode includes ops,
1107 * we need to add the _absolute_ value of the oplevel to the mode.
1109 char tbuf[3 + MAXOPLEVELDIGITS] = ":";
1112 if (HasVoice(member)) /* flag_cnt == 1 or 3 */
1114 if (IsHalfOp(member))
1116 if (IsChanOp(member)) /* flag_cnt == 2 or 3 */
1118 /* append the absolute value of the oplevel */
1120 loc += ircd_snprintf(0, tbuf + loc, sizeof(tbuf) - loc, "%u", last_oplevel = member->oplevel);
1125 msgq_append(&me, mb, tbuf);
1128 else if (send_oplevels && flag_cnt > 1 && last_oplevel != member->oplevel)
1131 * This can't be the first member of a (continued) BURST
1132 * message because then either flag_cnt == 0 or new_mode == 1
1133 * Now we need to append the incremental value of the oplevel.
1135 char tbuf[2 + MAXOPLEVELDIGITS];
1136 ircd_snprintf(0, tbuf, sizeof(tbuf), ":%u", member->oplevel - last_oplevel);
1137 last_oplevel = member->oplevel;
1138 msgq_append(&me, mb, tbuf);
1141 /* Go to the next `member'. */
1142 if (flag_cnt < 2 || !(current_flags[flag_cnt] & CHFL_CHANOP))
1143 member = member->next_member;
1145 member = opped_members[++opped_members_index];
1150 /* Point `member' at the start of the list again. */
1153 /* Now, after one loop, we know the number of ops and can
1154 * allocate the dynamic array with pointer to the ops. */
1155 opped_members = (struct Membership**)
1156 MyMalloc((number_of_ops + 1) * sizeof(struct Membership*));
1157 opped_members[number_of_ops] = NULL; /* Needed for loop termination */
1161 /* At the end of the second loop, sort the opped members with
1162 * increasing op-level, so that we will output them in the
1163 * correct order (and all op-level increments stay positive) */
1165 qsort(opped_members, number_of_ops,
1166 sizeof(struct Membership*), compare_member_oplevel);
1168 if(!(current_flags[flag_cnt+1] & CHFL_CHANOP)) {
1169 member = chptr->members;
1171 member = opped_members[(opped_members_index = 0)];
1173 } /* loop over 0,+v,+o,+ov */
1177 /* Attach all bans, space separated " :%ban ban ..." */
1178 for (first = 2; lp2; lp2 = lp2->next)
1180 len = strlen(lp2->banstr);
1181 if (msgq_bufleft(mb) < len + 1 + first)
1182 /* The +1 stands for the added ' '.
1183 * The +first stands for the added ":%".
1189 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
1195 send_buffer(cptr, mb, 0); /* Send this message */
1197 } /* Continue when there was something
1198 that didn't fit (full==1) */
1200 MyFree(opped_members);
1201 if (feature_bool(FEAT_TOPIC_BURST) && (chptr->topic[0] != '\0'))
1202 sendcmdto_one(&me, CMD_TOPIC, cptr, "%H %Tu %Tu :%s", chptr,
1203 chptr->creationtime, chptr->topic_time, chptr->topic);
1206 /** Canonify a mask.
1209 * @author Carlo Wood (Run),
1212 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
1213 * When the user name or host name are too long (USERLEN and HOSTLEN
1214 * respectively) then they are cut off at the start with a '*'.
1216 * The following transformations are made:
1218 * 1) xxx -> nick!*@*
1219 * 2) xxx.xxx -> *!*\@host
1220 * 3) xxx\!yyy -> nick!user\@*
1221 * 4) xxx\@yyy -> *!user\@host
1222 * 5) xxx!yyy\@zzz -> nick!user\@host
1224 * @param mask The uncanonified mask.
1225 * @returns The updated mask in a static buffer.
1227 char *pretty_mask(char *mask)
1229 static char star[2] = { '*', 0 };
1230 static char retmask[NICKLEN + USERLEN + HOSTLEN + 3];
1231 char *last_dot = NULL;
1234 /* Case 1: default */
1239 /* Do a _single_ pass through the characters of the mask: */
1240 for (ptr = mask; *ptr; ++ptr)
1244 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1248 else if (*ptr == '@')
1250 /* Case 4: Found last '@' (without finding a '!' yet) */
1255 else if (*ptr == '.' || *ptr == ':')
1257 /* Case 2: Found character specific to IP or hostname (without
1258 * finding a '!' or '@' yet) */
1268 /* Case 4 or 5: Found last '@' */
1274 if (user == star && last_dot)
1284 char *nick_end = (user != star) ? user - 1 : ptr;
1285 if (nick_end - nick > NICKLEN)
1291 char *user_end = (host != star) ? host - 1 : ptr;
1292 if (user_end - user > USERLEN)
1294 user = user_end - USERLEN;
1299 if (host != star && ptr - host > HOSTLEN)
1301 host = ptr - HOSTLEN;
1304 ircd_snprintf(0, retmask, sizeof(retmask), "%s!%s@%s", nick, user, host);
1308 /** send a banlist to a client for a channel
1310 * @param cptr Client to send the banlist to.
1311 * @param chptr Channel whose banlist to send.
1313 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1320 for (lp = chptr->banlist; lp; lp = lp->next)
1321 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->banstr, lp->who, lp->when);
1323 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1326 /** Get a channel block, creating if necessary.
1327 * Get Channel block for chname (and allocate a new channel
1328 * block, if it didn't exists before).
1330 * @param cptr Client joining the channel.
1331 * @param chname The name of the channel to join.
1332 * @param flag set to CGT_CREATE to create the channel if it doesn't
1335 * @returns NULL if the channel is invalid, doesn't exist and CGT_CREATE
1336 * wasn't specified or a pointer to the channel structure
1338 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1340 struct Channel *chptr;
1343 if (EmptyString(chname))
1346 len = strlen(chname);
1347 if (MyUser(cptr) && len > CHANNELLEN)
1350 *(chname + CHANNELLEN) = '\0';
1352 if ((chptr = FindChannel(chname)))
1354 if (flag == CGT_CREATE)
1356 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1358 ++UserStats.channels;
1359 memset(chptr, 0, sizeof(struct Channel));
1360 strcpy(chptr->chname, chname);
1361 if (GlobalChannelList)
1362 GlobalChannelList->prev = chptr;
1364 chptr->next = GlobalChannelList;
1365 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1366 GlobalChannelList = chptr;
1372 /** invite a user to a channel.
1374 * Adds an invite for a user to a channel. Limits the number of invites
1375 * to FEAT_MAXCHANNELSPERUSER. Does not sent notification to the user.
1377 * @param cptr The client to be invited.
1378 * @param chptr The channel to be invited to.
1380 void add_invite(struct Client *cptr, struct Channel *chptr)
1382 struct SLink *inv, **tmp;
1383 unsigned int maxchans;
1385 del_invite(cptr, chptr);
1387 * Delete last link in chain if the list is max length
1389 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1390 maxchans = cli_confs(cptr)->value.aconf ? ConfMaxChannels(cli_confs(cptr)->value.aconf) : feature_int(FEAT_MAXCHANNELSPERUSER);
1391 if (((cli_user(cptr))->invites >= maxchans && !HasPriv(cptr, PRIV_CHAN_LIMIT)) || cli_user(cptr)->invites > 100) /* Hard limit of 100. */
1392 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1394 * Add client to channel invite list
1397 inv->value.cptr = cptr;
1398 inv->next = chptr->invites;
1399 chptr->invites = inv;
1401 * Add channel to the end of the client invite list
1403 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1405 inv->value.chptr = chptr;
1408 (cli_user(cptr))->invites++;
1411 /** Delete an invite
1412 * Delete Invite block from channel invite list and client invite list
1414 * @param cptr Client pointer
1415 * @param chptr Channel pointer
1417 void del_invite(struct Client *cptr, struct Channel *chptr)
1419 struct SLink **inv, *tmp;
1421 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1422 if (tmp->value.cptr == cptr)
1427 (cli_user(cptr))->invites--;
1431 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1432 if (tmp->value.chptr == chptr)
1441 /** @page zombie Explanation of Zombies
1445 * A channel member is turned into a zombie when he is kicked from a
1446 * channel but his server has not acknowledged the kick. Servers that
1447 * see the member as a zombie can accept actions he performed before
1448 * being kicked, without allowing chanop operations from outsiders or
1449 * desyncing the network.
1457 * X --a--> A --b--> B --d--> D
1462 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1463 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1465 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1466 * Remove the user immediately when no users are left on the channel.
1467 * b) On server B : remove the user (who/lp) from the channel, send a
1468 * PART upstream (to A) and pass on the KICK.
1469 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1470 * channel, and pass on the KICK.
1471 * d) On server D : remove the user (who/lp) from the channel, and pass on
1475 * - Setting the ZOMBIE flag never hurts, we either remove the
1476 * client after that or we don't.
1477 * - The KICK message was already passed on, as should be in all cases.
1478 * - `who' is removed in all cases except case a) when users are left.
1479 * - A PART is only sent upstream in case b).
1485 * 1 --- 2 --- 3 --- 4 --- 5
1490 * We also need to turn 'who' into a zombie on servers 1 and 6,
1491 * because a KICK from 'who' (kicking someone else in that direction)
1492 * can arrive there afterward - which should not be bounced itself.
1493 * Therefore case a) also applies for servers 1 and 6.
1498 /** Turn a user on a channel into a zombie
1499 * This function turns a user into a zombie (see \ref zombie)
1501 * @param member The structure representing this user on this channel.
1502 * @param who The client that is being kicked.
1503 * @param cptr The connection the kick came from.
1504 * @param sptr The client that is doing the kicking.
1505 * @param chptr The channel the user is being kicked from.
1507 void make_zombie(struct Membership* member, struct Client* who,
1508 struct Client* cptr, struct Client* sptr, struct Channel* chptr)
1510 assert(0 != member);
1515 /* Default for case a): */
1518 /* Case b) or c) ?: */
1519 if (MyUser(who)) /* server 4 */
1521 if (IsServer(cptr)) /* Case b) ? */
1522 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1523 remove_user_from_channel(who, chptr);
1526 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1528 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1529 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1530 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1532 remove_user_from_channel(who, chptr);
1537 /* Case a) (servers 1, 2, 3 and 6) */
1538 if (channel_all_zombies(chptr))
1539 remove_user_from_channel(who, chptr);
1541 /* XXX Can't actually call Debug here; if the channel is all zombies,
1542 * chptr will no longer exist when we get here.
1543 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1547 /** returns the number of zombies on a channel
1548 * @param chptr Channel to count zombies in.
1550 * @returns The number of zombies on the channel.
1552 int number_of_zombies(struct Channel *chptr)
1554 struct Membership* member;
1558 for (member = chptr->members; member; member = member->next_member) {
1559 if (IsZombie(member))
1565 /** Concatenate some strings together.
1566 * This helper function builds an argument string in strptr, consisting
1567 * of the original string, a space, and str1 and str2 concatenated (if,
1568 * of course, str2 is not NULL)
1570 * @param strptr The buffer to concatenate into
1571 * @param strptr_i modified offset to the position to modify
1572 * @param str1 The string to concatenate from.
1573 * @param str2 The second string to contatenate from.
1574 * @param c Charactor to separate the string from str1 and str2.
1577 build_string(char *strptr, int *strptr_i, const char *str1,
1578 const char *str2, char c)
1581 strptr[(*strptr_i)++] = c;
1584 strptr[(*strptr_i)++] = *(str1++);
1588 strptr[(*strptr_i)++] = *(str2++);
1590 strptr[(*strptr_i)] = '\0';
1593 /** Flush out the modes
1594 * This is the workhorse of our ModeBuf suite; this actually generates the
1595 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1597 * @param mbuf The mode buffer to flush
1598 * @param all If true, flush all modes, otherwise leave partial modes in the
1604 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1606 /* we only need the flags that don't take args right now */
1607 static ulong64 flags[] = {
1608 /* MODE_CHANOP, 'o', */
1609 /* MODE_VOICE, 'v', */
1612 MODE_MODERATED, 'm',
1613 MODE_TOPICLIMIT, 't',
1614 MODE_INVITEONLY, 'i',
1615 MODE_NOPRIVMSGS, 'n',
1618 MODE_REGISTERED, 'R',
1619 /* MODE_KEY, 'k', */
1620 /* MODE_BAN, 'b', */
1622 /* MODE_APASS, 'A', */
1623 /* MODE_UPASS, 'U', */
1627 /* MODE_ALTCHAN, 'F', */
1628 /* MODE_NOFLOOD, 'f', */
1632 MODE_QUARANTINE, 'Q',
1633 MODE_AUDITORIUM, 'u',
1637 static ulong64 local_flags[] = {
1638 MODE_WASDELJOINS, 'd',
1644 struct Client *app_source; /* where the MODE appears to come from */
1646 char addbuf[20], addbuf_local[20]; /* accumulates +psmtin, etc. */
1647 int addbuf_i = 0, addbuf_local_i = 0;
1648 char rembuf[20], rembuf_local[20]; /* accumulates -psmtin, etc. */
1649 int rembuf_i = 0, rembuf_local_i = 0;
1650 char *bufptr; /* we make use of indirection to simplify the code */
1653 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1655 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1657 char *strptr; /* more indirection to simplify the code */
1660 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1663 char limitbuf[20],accessbuf[20]; /* convert limits to strings */
1665 ulong64 limitdel = MODE_LIMIT;
1666 ulong64 accessdel = MODE_ACCESS;
1670 /* If the ModeBuf is empty, we have nothing to do */
1671 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1674 /* Ok, if we were given the OPMODE flag, or its a server, hide the source.
1676 if (feature_bool(FEAT_HIS_MODEWHO) &&
1677 (mbuf->mb_dest & MODEBUF_DEST_OPMODE ||
1678 IsServer(mbuf->mb_source) ||
1679 IsMe(mbuf->mb_source)))
1682 app_source = mbuf->mb_source;
1685 * Account for user we're bouncing; we have to get it in on the first
1686 * bounced MODE, or we could have problems
1688 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1689 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1691 /* Calculate the simple flags */
1692 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1693 if (*flag_p & mbuf->mb_add)
1694 addbuf[addbuf_i++] = flag_p[1];
1695 else if (*flag_p & mbuf->mb_rem)
1696 rembuf[rembuf_i++] = flag_p[1];
1699 /* Some flags may be for local display only. */
1700 for (flag_p = local_flags; flag_p[0]; flag_p += 2) {
1701 if (*flag_p & mbuf->mb_add)
1702 addbuf_local[addbuf_local_i++] = flag_p[1];
1703 else if (*flag_p & mbuf->mb_rem)
1704 rembuf_local[rembuf_local_i++] = flag_p[1];
1707 /* Now go through the modes with arguments... */
1708 for (i = 0; i < mbuf->mb_count; i++) {
1709 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1711 bufptr_i = &addbuf_i;
1714 bufptr_i = &rembuf_i;
1717 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) {
1718 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1720 if ((totalbuflen - IRCD_MAX(9, tmp)) <= 0) /* don't overflow buffer */
1721 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1723 if((MB_TYPE(mbuf, i) & MODE_CHANOP))
1724 bufptr[(*bufptr_i)++] = 'o';
1725 else if((MB_TYPE(mbuf, i) & MODE_HALFOP))
1726 bufptr[(*bufptr_i)++] = 'h';
1728 bufptr[(*bufptr_i)++] = 'v';
1729 totalbuflen -= IRCD_MAX(9, tmp) + 1;
1731 } else if (MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS | MODE_ALTCHAN | MODE_NOFLOOD)) {
1732 tmp = strlen(MB_STRING(mbuf, i));
1734 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1735 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1738 switch(MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS | MODE_ALTCHAN | MODE_NOFLOOD))
1756 bufptr[(*bufptr_i)++] = mode_char;
1757 totalbuflen -= tmp + 1;
1759 } else if (MB_TYPE(mbuf, i) & MODE_KEY) {
1760 tmp = (mbuf->mb_dest & MODEBUF_DEST_NOKEY ? 1 :
1761 strlen(MB_STRING(mbuf, i)));
1763 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1764 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1766 bufptr[(*bufptr_i)++] = 'k';
1767 totalbuflen -= tmp + 1;
1769 } else if (MB_TYPE(mbuf, i) & (MODE_LIMIT)) {
1770 /* if it's a limit, we also format the number */
1771 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
1773 tmp = strlen(limitbuf);
1775 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1776 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1778 bufptr[(*bufptr_i)++] = 'l';
1779 totalbuflen -= tmp + 1;
1781 } else if (MB_TYPE(mbuf, i) & (MODE_ACCESS)) {
1782 /* if it's a limit, we also format the number */
1783 ircd_snprintf(0, accessbuf, sizeof(accessbuf), "%u", MB_UINT(mbuf, i));
1785 tmp = strlen(accessbuf);
1787 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1788 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1790 bufptr[(*bufptr_i)++] = 'a';
1791 totalbuflen -= tmp + 1;
1796 /* terminate the mode strings */
1797 addbuf[addbuf_i] = '\0';
1798 rembuf[rembuf_i] = '\0';
1799 addbuf_local[addbuf_local_i] = '\0';
1800 rembuf_local[rembuf_local_i] = '\0';
1802 /* If we're building a user visible MODE or HACK... */
1803 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1804 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1805 MODEBUF_DEST_LOG)) {
1806 /* Set up the parameter strings */
1812 for (i = 0; i < mbuf->mb_count; i++) {
1813 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1816 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1818 strptr_i = &addstr_i;
1821 strptr_i = &remstr_i;
1824 /* deal with clients... */
1825 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE))
1826 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1828 /* deal with bans... */
1829 else if (MB_TYPE(mbuf, i) & (MODE_BAN))
1830 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1832 /* deal with keys... */
1833 else if (MB_TYPE(mbuf, i) & MODE_KEY)
1834 build_string(strptr, strptr_i, mbuf->mb_dest & MODEBUF_DEST_NOKEY ?
1835 "*" : MB_STRING(mbuf, i), 0, ' ');
1837 /* deal with invisible passwords */
1838 else if (MB_TYPE(mbuf, i) & (MODE_APASS | MODE_UPASS))
1839 build_string(strptr, strptr_i, "*", 0, ' ');
1842 * deal with limit; note we cannot include the limit parameter if we're
1845 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1846 (MODE_ADD | MODE_LIMIT))
1847 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1848 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_ACCESS)) ==
1849 (MODE_ADD | MODE_ACCESS))
1850 build_string(strptr, strptr_i, accessbuf, 0, ' ');
1852 else if (MB_TYPE(mbuf, i) & MODE_ALTCHAN)
1853 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1854 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_NOFLOOD)) ==
1855 (MODE_ADD | MODE_NOFLOOD))
1856 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1859 /* send the messages off to their destination */
1860 if (mbuf->mb_dest & MODEBUF_DEST_HACK2)
1861 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1863 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1864 mbuf->mb_source : app_source),
1865 mbuf->mb_channel->chname,
1866 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1867 addbuf, remstr, addstr,
1868 mbuf->mb_channel->creationtime);
1870 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1871 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1872 "%s%s%s%s%s%s [%Tu]",
1873 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1874 mbuf->mb_source : app_source),
1875 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1876 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1877 mbuf->mb_channel->creationtime);
1879 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1880 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1882 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1883 mbuf->mb_source : app_source),
1884 mbuf->mb_channel->chname,
1885 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1886 addbuf, remstr, addstr,
1887 mbuf->mb_channel->creationtime);
1889 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1890 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1891 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1892 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1893 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1895 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1896 sendcmdto_channel_butserv_butone(app_source, CMD_MODE, mbuf->mb_channel, NULL, 0,
1897 "%H %s%s%s%s%s%s%s%s", mbuf->mb_channel,
1898 rembuf_i || rembuf_local_i ? "-" : "",
1899 rembuf, rembuf_local,
1900 addbuf_i || addbuf_local_i ? "+" : "",
1901 addbuf, addbuf_local,
1905 /* Now are we supposed to propagate to other servers? */
1906 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1907 /* set up parameter string */
1914 * limit is supressed if we're removing it; we have to figure out which
1915 * direction is the direction for it to be removed, though...
1917 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) ? MODE_DEL : MODE_ADD;
1918 accessdel |= (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) ? MODE_DEL : MODE_ADD;
1920 for (i = 0; i < mbuf->mb_count; i++) {
1921 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1924 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1926 strptr_i = &addstr_i;
1929 strptr_i = &remstr_i;
1932 /* if we're changing oplevels and we know the oplevel, pass it on */
1933 if ((MB_TYPE(mbuf, i) & MODE_CHANOP)
1934 && MB_OPLEVEL(mbuf, i) < MAXOPLEVEL)
1935 *strptr_i += ircd_snprintf(0, strptr + *strptr_i, BUFSIZE - *strptr_i,
1937 NumNick(MB_CLIENT(mbuf, i)),
1938 MB_OPLEVEL(mbuf, i));
1940 /* deal with other modes that take clients */
1941 else if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE))
1942 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1944 /* deal with modes that take strings */
1945 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS | MODE_ALTCHAN | MODE_NOFLOOD))
1946 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1949 * deal with the limit. Logic here is complicated; if HACK2 is set,
1950 * we're bouncing the mode, so sense is reversed, and we have to
1951 * include the original limit if it looks like it's being removed
1953 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1954 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1955 else if ((MB_TYPE(mbuf, i) & accessdel) == accessdel)
1956 build_string(strptr, strptr_i, accessbuf, 0, ' ');
1959 /* we were told to deop the source */
1960 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1961 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1962 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1963 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1965 /* mark that we've done this, so we don't do it again */
1966 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1969 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1970 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1971 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1972 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1973 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1974 addbuf, remstr, addstr);
1975 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1977 * If HACK2 was set, we're bouncing; we send the MODE back to
1978 * the connection we got it from with the senses reversed and
1979 * the proper TS; origin is us
1981 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1982 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1983 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1984 mbuf->mb_channel->creationtime);
1987 * We're propagating a normal (or HACK3 or HACK4) MODE command
1988 * to the rest of the network. We send the actual channel TS.
1990 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1991 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1992 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1993 addbuf, remstr, addstr,
1994 mbuf->mb_channel->creationtime);
1998 /* We've drained the ModeBuf... */
2003 /* reinitialize the mode-with-arg slots */
2004 for (i = 0; i < MAXMODEPARAMS; i++) {
2005 /* If we saved any, pack them down */
2006 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
2007 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
2008 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
2010 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
2012 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
2013 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
2015 MB_TYPE(mbuf, i) = 0;
2016 MB_UINT(mbuf, i) = 0;
2019 /* If we're supposed to flush it all, do so--all hail tail recursion */
2020 if (all && mbuf->mb_count)
2021 return modebuf_flush_int(mbuf, 1);
2026 /** Initialise a modebuf
2027 * This routine just initializes a ModeBuf structure with the information
2028 * needed and the options given.
2030 * @param mbuf The mode buffer to initialise.
2031 * @param source The client that is performing the mode.
2033 * @param chan The channel that the mode is being performed upon.
2037 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
2038 struct Client *connect, struct Channel *chan, unsigned int dest)
2043 assert(0 != source);
2047 if (IsLocalChannel(chan->chname)) dest &= ~MODEBUF_DEST_SERVER;
2051 mbuf->mb_source = source;
2052 mbuf->mb_connect = connect;
2053 mbuf->mb_channel = chan;
2054 mbuf->mb_dest = dest;
2057 /* clear each mode-with-parameter slot */
2058 for (i = 0; i < MAXMODEPARAMS; i++) {
2059 MB_TYPE(mbuf, i) = 0;
2060 MB_UINT(mbuf, i) = 0;
2064 /** Append a new mode to a modebuf
2065 * This routine simply adds modes to be added or deleted; do a binary OR
2066 * with either MODE_ADD or MODE_DEL
2068 * @param mbuf Mode buffer
2069 * @param mode MODE_ADD or MODE_DEL OR'd with MODE_PRIVATE etc.
2072 modebuf_mode(struct ModeBuf *mbuf, ulong64 mode)
2075 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2077 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
2078 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS | MODE_REGONLY |
2079 MODE_DELJOINS | MODE_WASDELJOINS | MODE_REGISTERED | MODE_PERSIST |
2080 MODE_NOCOLOUR | MODE_NOCTCP | MODE_NOAMSGS | MODE_NONOTICE |
2081 MODE_QUARANTINE | MODE_AUDITORIUM | MODE_SSLCHAN);
2083 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
2086 if (mode & MODE_ADD) {
2087 mbuf->mb_rem &= ~mode;
2088 mbuf->mb_add |= mode;
2090 mbuf->mb_add &= ~mode;
2091 mbuf->mb_rem |= mode;
2095 /** Append a mode that takes an ulong64 argument to the modebuf
2097 * This routine adds a mode to be added or deleted that takes a unsigned
2098 * ulong64 parameter; mode may *only* be the relevant mode flag ORed with one
2099 * of MODE_ADD or MODE_DEL
2101 * @param mbuf The mode buffer to append to.
2102 * @param mode The mode to append.
2103 * @param uint The argument to the mode.
2106 modebuf_mode_uint(struct ModeBuf *mbuf, ulong64 mode, unsigned int uint)
2109 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2111 if (mode == (MODE_LIMIT | MODE_DEL)) {
2112 mbuf->mb_rem |= mode;
2115 if (mode == (MODE_ACCESS | MODE_DEL)) {
2116 mbuf->mb_rem |= mode;
2119 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2120 MB_UINT(mbuf, mbuf->mb_count) = uint;
2122 /* when we've reached the maximal count, flush the buffer */
2123 if (++mbuf->mb_count >=
2124 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2125 modebuf_flush_int(mbuf, 0);
2128 /** append a string mode
2129 * This routine adds a mode to be added or deleted that takes a string
2130 * parameter; mode may *only* be the relevant mode flag ORed with one of
2131 * MODE_ADD or MODE_DEL
2133 * @param mbuf The mode buffer to append to.
2134 * @param mode The mode to append.
2135 * @param string The string parameter to append.
2136 * @param free If the string should be free'd later.
2139 modebuf_mode_string(struct ModeBuf *mbuf, ulong64 mode, char *string,
2143 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2145 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
2146 MB_STRING(mbuf, mbuf->mb_count) = string;
2148 /* when we've reached the maximal count, flush the buffer */
2149 if (++mbuf->mb_count >=
2150 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2151 modebuf_flush_int(mbuf, 0);
2154 /** Append a mode on a client to a modebuf.
2155 * This routine adds a mode to be added or deleted that takes a client
2156 * parameter; mode may *only* be the relevant mode flag ORed with one of
2157 * MODE_ADD or MODE_DEL
2159 * @param mbuf The modebuf to append the mode to.
2160 * @param mode The mode to append.
2161 * @param client The client argument to append.
2162 * @param oplevel The oplevel the user had or will have
2165 modebuf_mode_client(struct ModeBuf *mbuf, ulong64 mode,
2166 struct Client *client, int oplevel)
2169 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2171 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2172 MB_CLIENT(mbuf, mbuf->mb_count) = client;
2173 MB_OPLEVEL(mbuf, mbuf->mb_count) = oplevel;
2175 /* when we've reached the maximal count, flush the buffer */
2176 if (++mbuf->mb_count >=
2177 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2178 modebuf_flush_int(mbuf, 0);
2181 /** Check a channel for join-delayed members.
2182 * @param[in] chan Channel to search.
2183 * @return Non-zero if any members are join-delayed; false if none are.
2186 find_delayed_joins(const struct Channel *chan)
2188 const struct Membership *memb;
2189 for (memb = chan->members; memb; memb = memb->next_member)
2190 if (IsDelayedJoin(memb) && !IsInvisibleJoin(memb))
2195 /** The exported binding for modebuf_flush()
2197 * @param mbuf The mode buffer to flush.
2199 * @see modebuf_flush_int()
2202 modebuf_flush(struct ModeBuf *mbuf)
2204 /* Check if MODE_WASDELJOINS should be set: */
2205 /* Must be set if going -D and some clients are hidden */
2206 if ((mbuf->mb_rem & MODE_DELJOINS)
2207 && !(mbuf->mb_channel->mode.mode & (MODE_DELJOINS | MODE_WASDELJOINS))
2208 && find_delayed_joins(mbuf->mb_channel)) {
2209 mbuf->mb_channel->mode.mode |= MODE_WASDELJOINS;
2210 mbuf->mb_add |= MODE_WASDELJOINS;
2211 mbuf->mb_rem &= ~MODE_WASDELJOINS;
2213 /* Must be cleared if +D is set */
2214 if ((mbuf->mb_add & MODE_DELJOINS)
2215 && ((mbuf->mb_channel->mode.mode & (MODE_WASDELJOINS | MODE_WASDELJOINS))
2216 == (MODE_WASDELJOINS | MODE_WASDELJOINS))) {
2217 mbuf->mb_channel->mode.mode &= ~MODE_WASDELJOINS;
2218 mbuf->mb_add &= ~MODE_WASDELJOINS;
2219 mbuf->mb_rem |= MODE_WASDELJOINS;
2222 return modebuf_flush_int(mbuf, 1);
2225 /* This extracts the simple modes contained in mbuf
2227 * @param mbuf The mode buffer to extract the modes from.
2228 * @param buf The string buffer to write the modes into.
2231 modebuf_extract(struct ModeBuf *mbuf, char *buf)
2233 static ulong64 flags[] = {
2234 /* MODE_CHANOP, 'o', */
2235 /* MODE_VOICE, 'v', */
2238 MODE_MODERATED, 'm',
2239 MODE_TOPICLIMIT, 't',
2240 MODE_INVITEONLY, 'i',
2241 MODE_NOPRIVMSGS, 'n',
2245 MODE_REGISTERED, 'R',
2246 /* MODE_BAN, 'b', */
2255 MODE_QUARANTINE, 'Q',
2258 MODE_AUDITORIUM, 'u',
2264 int i, bufpos = 0, len;
2266 char *key = 0, limitbuf[20], accessbuf[20];
2267 char *apass = 0, *upass = 0, *altchan = 0, *noflood = 0;
2276 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
2277 if (MB_TYPE(mbuf, i) & MODE_ADD) {
2278 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT | MODE_APASS | MODE_UPASS | MODE_ALTCHAN | MODE_ACCESS | MODE_NOFLOOD);
2280 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
2281 key = MB_STRING(mbuf, i);
2282 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
2283 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
2284 else if (MB_TYPE(mbuf, i) & MODE_ACCESS)
2285 ircd_snprintf(0, accessbuf, sizeof(accessbuf), "%u", MB_UINT(mbuf, i));
2286 else if (MB_TYPE(mbuf, i) & MODE_UPASS)
2287 upass = MB_STRING(mbuf, i);
2288 else if (MB_TYPE(mbuf, i) & MODE_APASS)
2289 apass = MB_STRING(mbuf, i);
2290 else if (MB_TYPE(mbuf, i) & MODE_ALTCHAN)
2291 altchan = MB_STRING(mbuf, i);
2292 else if (MB_TYPE(mbuf, i) & MODE_NOFLOOD)
2293 noflood = MB_STRING(mbuf, i);
2300 buf[bufpos++] = '+'; /* start building buffer */
2302 for (flag_p = flags; flag_p[0]; flag_p += 2)
2304 buf[bufpos++] = flag_p[1];
2306 for (i = 0, len = bufpos; i < len; i++) {
2308 build_string(buf, &bufpos, key, 0, ' ');
2309 else if (buf[i] == 'l')
2310 build_string(buf, &bufpos, limitbuf, 0, ' ');
2311 else if (buf[i] == 'a')
2312 build_string(buf, &bufpos, accessbuf, 0, ' ');
2313 else if (buf[i] == 'U')
2314 build_string(buf, &bufpos, upass, 0, ' ');
2315 else if (buf[i] == 'A')
2316 build_string(buf, &bufpos, apass, 0, ' ');
2317 else if (buf[i] == 'F')
2318 build_string(buf, &bufpos, altchan, 0, ' ');
2319 else if (buf[i] == 'f')
2320 build_string(buf, &bufpos, noflood, 0, ' ');
2328 /** Simple function to invalidate a channel's ban cache.
2330 * This function marks all members of the channel as being neither
2331 * banned nor banned.
2333 * @param chan The channel to operate on.
2336 mode_ban_invalidate(struct Channel *chan)
2338 struct Membership *member;
2340 for (member = chan->members; member; member = member->next_member)
2341 ClearBanValid(member);
2344 /** Simple function to drop invite structures
2346 * Remove all the invites on the channel.
2348 * @param chan Channel to remove invites from.
2352 mode_invite_clear(struct Channel *chan)
2354 while (chan->invites)
2355 del_invite(chan->invites->value.cptr, chan);
2358 /* What we've done for mode_parse so far... */
2359 #define DONE_LIMIT 0x01 /**< We've set the limit */
2360 #define DONE_KEY_ADD 0x02 /**< We've set the key */
2361 #define DONE_BANLIST 0x04 /**< We've sent the ban list */
2362 #define DONE_NOTOPER 0x08 /**< We've sent a "Not oper" error */
2363 #define DONE_BANCLEAN 0x10 /**< We've cleaned bans... */
2364 #define DONE_UPASS_ADD 0x20 /**< We've set user pass */
2365 #define DONE_APASS_ADD 0x40 /**< We've set admin pass */
2366 #define DONE_KEY_DEL 0x80 /**< We've removed the key */
2367 #define DONE_UPASS_DEL 0x100 /**< We've removed the user pass */
2368 #define DONE_APASS_DEL 0x200 /**< We've removed the admin pass */
2369 #define DONE_ALTCHAN 0x800 /**< We've set the altchan */
2370 #define DONE_ACCESS 0x1000 /**< We've set the access */
2371 #define DONE_NOFLOOD 0x2000 /**< We've set the noflood options */
2374 struct ModeBuf *mbuf;
2375 struct Client *cptr;
2376 struct Client *sptr;
2377 struct Channel *chptr;
2378 struct Membership *member;
2389 struct Ban banlist[MAXPARA];
2392 unsigned short oplevel;
2393 struct Client *client;
2394 } cli_change[MAXPARA];
2397 /** Helper function to send "Not oper" or "Not member" messages
2398 * Here's a helper function to deal with sending along "Not oper" or
2399 * "Not member" messages
2401 * @param state Parsing State object
2404 send_notoper(struct ParseState *state)
2406 if (state->done & DONE_NOTOPER)
2409 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
2410 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
2412 state->done |= DONE_NOTOPER;
2416 * Helper function to convert limits
2418 * @param state Parsing state object.
2422 mode_parse_limit(struct ParseState *state, ulong64 *flag_p)
2424 unsigned int t_limit;
2426 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
2427 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2430 if (state->parc <= 0) { /* warn if not enough args */
2431 if (MyUser(state->sptr))
2432 need_more_params(state->sptr, "MODE +l");
2436 t_limit = strtoul(state->parv[state->args_used++], 0, 10); /* grab arg */
2440 if ((int)t_limit<0) /* don't permit a negative limit */
2443 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2444 (!t_limit || t_limit == state->chptr->mode.limit))
2447 t_limit = state->chptr->mode.limit;
2449 /* If they're not an oper, they can't change modes */
2450 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2451 send_notoper(state);
2455 /* Can't remove a limit that's not there */
2456 if (state->dir == MODE_DEL && !state->chptr->mode.limit)
2459 /* Skip if this is a burst and a lower limit than this is set already */
2460 if ((state->flags & MODE_PARSE_BURST) &&
2461 (state->chptr->mode.mode & flag_p[0]) &&
2462 (state->chptr->mode.limit < t_limit))
2465 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
2467 state->done |= DONE_LIMIT;
2472 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
2474 if (state->flags & MODE_PARSE_SET) { /* set the limit */
2475 if (state->dir & MODE_ADD) {
2476 state->chptr->mode.mode |= flag_p[0];
2477 state->chptr->mode.limit = t_limit;
2479 state->chptr->mode.mode &= ~flag_p[0];
2480 state->chptr->mode.limit = 0;
2487 mode_parse_access(struct ParseState *state, ulong64 *flag_p)
2489 unsigned int t_access;
2491 if (state->dir == MODE_ADD) { /* convert arg only if adding access */
2492 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2495 if (state->parc <= 0) { /* warn if not enough args */
2496 if (MyUser(state->sptr))
2497 need_more_params(state->sptr, "MODE +a");
2501 t_access = strtoul(state->parv[state->args_used++], 0, 10); /* grab arg */
2505 if ((int)t_access<1 || (int)t_access>500) /* don't permit a negative access or an access over 500 */
2508 if (!feature_bool(FEAT_CHMODE_A_ENABLE)) { /* don't parse MODE_ACCESS if it's disabled */
2509 if (MyUser(state->sptr)) {
2511 send_reply(state->sptr, ERR_UNKNOWNMODE, mode[0]);
2516 if(feature_bool(FEAT_CHMODE_A_NOSET) && !(state->flags & MODE_PARSE_FORCE)) /* mode can'T be set. */
2519 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2520 (!t_access || t_access == state->chptr->mode.access))
2523 t_access = state->chptr->mode.access;
2525 /* If they're not an oper, they can't change modes */
2526 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2527 send_notoper(state);
2531 /* Can't remove an access that's not there */
2532 if (state->dir == MODE_DEL && !state->chptr->mode.access)
2535 /* Skip if this is a burst and a higher access than this is set already */
2536 if ((state->flags & MODE_PARSE_BURST) &&
2537 (state->chptr->mode.mode & flag_p[0]) &&
2538 (state->chptr->mode.access > t_access))
2541 if (state->done & DONE_ACCESS) /* allow access to be set only once */
2543 state->done |= DONE_ACCESS;
2548 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_access);
2550 if (state->flags & MODE_PARSE_SET) { /* set the access */
2551 if (state->dir & MODE_ADD) {
2552 state->chptr->mode.mode |= flag_p[0];
2553 state->chptr->mode.access = t_access;
2555 state->chptr->mode.mode &= ~flag_p[0];
2556 state->chptr->mode.access = 0;
2563 mode_parse_altchan(struct ParseState *state, ulong64 *flag_p)
2567 if (state->dir == MODE_ADD) { /* convert arg only if adding altchan */
2568 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2571 if (!feature_bool(FEAT_CHMODE_F_ENABLE))
2574 if (state->parc <= 0) { /* warn if not enough args */
2575 if (MyUser(state->sptr))
2576 need_more_params(state->sptr, "MODE +F");
2580 t_str = state->parv[state->args_used++]; /* grab arg */
2584 if (!IsChannelName(t_str) || !strIsIrcCh(t_str) || strlen(t_str) > IRCD_MIN(CHANNELLEN, feature_int(FEAT_CHANNELLEN)) || t_str[0] == '&') /* only parse it if it's a valid channel name! */
2587 if(!(state->flags & MODE_PARSE_FORCE)) {
2588 struct Channel *chptr;
2589 struct Membership *member;
2590 if (!(chptr = FindChannel(t_str)))
2592 if(!(member = find_member_link(chptr, state->sptr)))
2594 if(!IsChanOpOrHalfOp(member)) {
2595 send_notoper(state);
2600 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2601 (!t_str || t_str == state->chptr->mode.altchan))
2604 t_str = state->chptr->mode.altchan;
2606 /* If they're not an oper, they can't change modes */
2607 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2608 send_notoper(state);
2612 /* Can't remove a altchan that's not there */
2613 if (state->dir == MODE_DEL && !*state->chptr->mode.altchan)
2616 /* Skip if this is a burst and a lower altchan than this is set already */
2617 if ((state->flags & MODE_PARSE_BURST) &&
2618 *(state->chptr->mode.altchan))
2621 if (state->done & DONE_ALTCHAN) /* allow altchan to be set only once */
2623 state->done |= DONE_ALTCHAN;
2628 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2629 !ircd_strcmp(state->chptr->mode.altchan, t_str))
2630 return; /* no change */
2632 if (state->flags & MODE_PARSE_BOUNCE) {
2633 if (*state->chptr->mode.altchan) /* reset old altchan */
2634 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0], state->chptr->mode.altchan, 0);
2635 else /* remove new bogus altchan */
2636 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2637 } else /* send new altchan */
2638 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2640 if (state->flags & MODE_PARSE_SET) {
2641 if (state->dir == MODE_DEL) { /* remove the old altchan */
2642 *state->chptr->mode.altchan = '\0';
2643 state->chptr->mode.mode &= ~flag_p[0];
2645 ircd_strncpy(state->chptr->mode.altchan, t_str, CHANNELLEN);
2646 state->chptr->mode.mode |= flag_p[0];
2652 mode_parse_noflood(struct ParseState *state, ulong64 *flag_p)
2656 unsigned int count = 0, time = 0, flags = 0;
2658 if (state->dir == MODE_ADD) { /* convert arg only if adding noflood */
2659 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2662 if (state->parc <= 0) { /* warn if not enough args */
2663 if (MyUser(state->sptr))
2664 need_more_params(state->sptr, "MODE +f");
2668 t_str = state->parv[state->args_used++]; /* grab arg */
2675 if(state->flags & MODE_PARSE_FORCE) flags |= FLFL_NOFLOOD;
2676 else t_str++; //simply ignore if it's not an opmode
2679 if(tmp[0] == '+' || tmp[0] == '%' || tmp[0] == '@') {
2680 if(tmp[0] == '+') flags |= FLFL_VOICE;
2681 if(tmp[0] == '%') flags |= FLFL_HALFOP;
2682 if(tmp[0] == '@') flags |= FLFL_CHANOP;
2686 for(p = tmp; p[0]; p++) {
2688 char tmpchar = p[0];
2690 count = strtoul(tmp,0,10);
2693 time = strtoul(p,0,10);
2697 if(count <= 0 || time <= 0 || count > 100 || time > 600) return;
2699 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2700 (!t_str || t_str == state->chptr->mode.noflood))
2703 t_str = state->chptr->mode.noflood;
2705 /* If they're not an oper, they can't change modes */
2706 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2707 send_notoper(state);
2711 /* Can't remove a noflood that's not there */
2712 if (state->dir == MODE_DEL && !*state->chptr->mode.noflood)
2715 /* Skip if this is a burst and a lower noflood than this is set already */
2716 if ((state->flags & MODE_PARSE_BURST) &&
2717 *(state->chptr->mode.noflood))
2720 if (state->done & DONE_NOFLOOD) /* allow noflood to be set only once */
2722 state->done |= DONE_NOFLOOD;
2727 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2728 !ircd_strcmp(state->chptr->mode.noflood, t_str))
2729 return; /* no change */
2731 if (state->flags & MODE_PARSE_BOUNCE) {
2732 if (*state->chptr->mode.noflood) /* reset old noflood */
2733 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0], state->chptr->mode.noflood, 0);
2734 else /* remove new bogus noflood */
2735 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2736 } else /* send new noflood */
2737 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2739 if (state->flags & MODE_PARSE_SET) {
2740 if (state->dir == MODE_DEL) { /* remove the old noflood */
2741 *state->chptr->mode.noflood = '\0';
2742 state->chptr->mode.mode &= ~flag_p[0];
2744 ircd_strncpy(state->chptr->mode.noflood, t_str, CHANNELLEN);
2745 state->chptr->mode.mode |= flag_p[0];
2749 if (state->dir == MODE_ADD) {
2750 unsigned int noflood_value = time;
2751 noflood_value <<= 10;
2752 noflood_value |= count;
2753 noflood_value <<= 4;
2754 noflood_value |= flags;
2755 state->chptr->mode.noflood_value = noflood_value;
2757 //removed the mode so free all flood objects
2758 state->chptr->mode.noflood_value = 0;
2759 struct Membership *member;
2760 for(member = state->chptr->members; member; member = member->next_member) {
2761 struct MemberFlood *floodnode;
2762 for(floodnode = member->flood;floodnode ; floodnode = floodnode->next_memberflood) {
2763 if(floodnode->next_memberflood == NULL) break;
2764 } //simply walk to the end
2765 if(!floodnode) continue;
2766 floodnode->next_memberflood = free_MemberFlood;
2767 free_MemberFlood = floodnode;
2768 member->flood = NULL;
2774 mode_parse_quarantine(struct ParseState *state, ulong64 *flag_p)
2779 /** Helper function to validate key-like parameters.
2781 * @param[in] state Parse state for feedback to user.
2782 * @param[in] s Key to validate.
2783 * @param[in] command String to pass for need_more_params() command.
2784 * @return Zero on an invalid key, non-zero if the key was okay.
2787 is_clean_key(struct ParseState *state, char *s, char *command)
2792 if (MyUser(state->sptr))
2793 need_more_params(state->sptr, command);
2796 else if (s[0] == ':') {
2797 if (MyUser(state->sptr))
2798 send_reply(state->sptr, ERR_INVALIDKEY, state->chptr->chname);
2801 for (ii = 0; (ii <= KEYLEN) && (s[ii] != '\0'); ++ii) {
2802 if ((unsigned char)s[ii] <= ' ' || s[ii] == ',') {
2803 if (MyUser(state->sptr))
2804 send_reply(state->sptr, ERR_INVALIDKEY, state->chptr->chname);
2809 if (MyUser(state->sptr))
2810 send_reply(state->sptr, ERR_INVALIDKEY, state->chptr->chname);
2817 * Helper function to convert keys
2820 mode_parse_key(struct ParseState *state, ulong64 *flag_p)
2824 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2827 if (state->parc <= 0) { /* warn if not enough args */
2828 if (MyUser(state->sptr))
2829 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2834 t_str = state->parv[state->args_used++]; /* grab arg */
2838 /* If they're not an oper, they can't change modes */
2839 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2840 send_notoper(state);
2844 /* allow removing and then adding key, but not adding and then removing */
2845 if (state->dir == MODE_ADD)
2847 if (state->done & DONE_KEY_ADD)
2849 state->done |= DONE_KEY_ADD;
2853 if (state->done & (DONE_KEY_ADD | DONE_KEY_DEL))
2855 state->done |= DONE_KEY_DEL;
2858 /* If the key is invalid, tell the user and bail. */
2859 if (!is_clean_key(state, t_str, state->dir == MODE_ADD ? "MODE +k" :
2866 /* Skip if this is a burst, we have a key already and the new key is
2867 * after the old one alphabetically */
2868 if ((state->flags & MODE_PARSE_BURST) &&
2869 *(state->chptr->mode.key) &&
2870 ircd_strcmp(state->chptr->mode.key, t_str) <= 0)
2873 /* can't add a key if one is set, nor can one remove the wrong key */
2874 if (!(state->flags & MODE_PARSE_FORCE))
2875 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2876 (state->dir == MODE_DEL &&
2877 ircd_strcmp(state->chptr->mode.key, t_str))) {
2878 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2882 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2883 !ircd_strcmp(state->chptr->mode.key, t_str))
2884 return; /* no key change */
2886 if (state->flags & MODE_PARSE_BOUNCE) {
2887 if (*state->chptr->mode.key) /* reset old key */
2888 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2889 state->chptr->mode.key, 0);
2890 else /* remove new bogus key */
2891 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2892 } else /* send new key */
2893 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2895 if (state->flags & MODE_PARSE_SET) {
2896 if (state->dir == MODE_DEL) /* remove the old key */
2897 *state->chptr->mode.key = '\0';
2899 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2904 * Helper function to convert user passes
2907 mode_parse_upass(struct ParseState *state, ulong64 *flag_p)
2911 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2914 if (state->parc <= 0) { /* warn if not enough args */
2915 if (MyUser(state->sptr))
2916 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +U" :
2921 t_str = state->parv[state->args_used++]; /* grab arg */
2925 /* If they're not an oper, they can't change modes */
2926 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2927 send_notoper(state);
2931 /* If a non-service user is trying to force it, refuse. */
2932 if (state->flags & MODE_PARSE_FORCE && MyUser(state->sptr)
2933 && !HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
2934 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2935 state->chptr->chname);
2939 /* If they are not the channel manager, they are not allowed to change it */
2940 if (MyUser(state->sptr) && !(state->flags & MODE_PARSE_FORCE || IsChannelManager(state->member))) {
2941 if (*state->chptr->mode.apass) {
2942 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2943 state->chptr->chname);
2945 send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname,
2946 (TStime() - state->chptr->creationtime < 172800) ?
2947 "approximately 4-5 minutes" : "approximately 48 hours");
2952 /* allow removing and then adding upass, but not adding and then removing */
2953 if (state->dir == MODE_ADD)
2955 if (state->done & DONE_UPASS_ADD)
2957 state->done |= DONE_UPASS_ADD;
2961 if (state->done & (DONE_UPASS_ADD | DONE_UPASS_DEL))
2963 state->done |= DONE_UPASS_DEL;
2966 /* If the Upass is invalid, tell the user and bail. */
2967 if (!is_clean_key(state, t_str, state->dir == MODE_ADD ? "MODE +U" :
2974 if (!(state->flags & MODE_PARSE_FORCE)) {
2975 /* can't add the upass while apass is not set */
2976 if (state->dir == MODE_ADD && !*state->chptr->mode.apass) {
2977 send_reply(state->sptr, ERR_UPASSNOTSET, state->chptr->chname, state->chptr->chname);
2980 /* cannot set a +U password that is the same as +A */
2981 if (state->dir == MODE_ADD && !ircd_strcmp(state->chptr->mode.apass, t_str)) {
2982 send_reply(state->sptr, ERR_UPASS_SAME_APASS, state->chptr->chname);
2985 /* can't add a upass if one is set, nor can one remove the wrong upass */
2986 if ((state->dir == MODE_ADD && *state->chptr->mode.upass) ||
2987 (state->dir == MODE_DEL &&
2988 ircd_strcmp(state->chptr->mode.upass, t_str))) {
2989 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2994 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2995 !ircd_strcmp(state->chptr->mode.upass, t_str))
2996 return; /* no upass change */
2998 /* Skip if this is a burst, we have a Upass already and the new Upass is
2999 * after the old one alphabetically */
3000 if ((state->flags & MODE_PARSE_BURST) &&
3001 *(state->chptr->mode.upass) &&
3002 ircd_strcmp(state->chptr->mode.upass, t_str) <= 0)
3005 if (state->flags & MODE_PARSE_BOUNCE) {
3006 if (*state->chptr->mode.upass) /* reset old upass */
3007 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
3008 state->chptr->mode.upass, 0);
3009 else /* remove new bogus upass */
3010 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
3011 } else /* send new upass */
3012 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
3014 if (state->flags & MODE_PARSE_SET) {
3015 if (state->dir == MODE_DEL) /* remove the old upass */
3016 *state->chptr->mode.upass = '\0';
3018 ircd_strncpy(state->chptr->mode.upass, t_str, KEYLEN);
3023 * Helper function to convert admin passes
3026 mode_parse_apass(struct ParseState *state, ulong64 *flag_p)
3028 struct Membership *memb;
3031 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3034 if (state->parc <= 0) { /* warn if not enough args */
3035 if (MyUser(state->sptr))
3036 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
3041 t_str = state->parv[state->args_used++]; /* grab arg */
3045 /* If they're not an oper, they can't change modes */
3046 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3047 send_notoper(state);
3051 if (MyUser(state->sptr)) {
3052 if (state->flags & MODE_PARSE_FORCE) {
3053 /* If an unprivileged oper is trying to force it, refuse. */
3054 if (!HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
3055 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
3056 state->chptr->chname);
3060 /* If they are not the channel manager, they are not allowed to change it. */
3061 if (!IsChannelManager(state->member)) {
3062 if (*state->chptr->mode.apass) {
3063 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
3064 state->chptr->chname);
3066 send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname,
3067 (TStime() - state->chptr->creationtime < 172800) ?
3068 "approximately 4-5 minutes" : "approximately 48 hours");
3072 /* Can't remove the Apass while Upass is still set. */
3073 if (state->dir == MODE_DEL && *state->chptr->mode.upass) {
3074 send_reply(state->sptr, ERR_UPASSSET, state->chptr->chname, state->chptr->chname);
3077 /* Can't add an Apass if one is set, nor can one remove the wrong Apass. */
3078 if ((state->dir == MODE_ADD && *state->chptr->mode.apass) ||
3079 (state->dir == MODE_DEL && ircd_strcmp(state->chptr->mode.apass, t_str))) {
3080 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
3085 /* Forbid removing the Apass if the channel is older than 48 hours
3086 * unless an oper is doing it. */
3087 if (TStime() - state->chptr->creationtime >= 172800
3088 && state->dir == MODE_DEL
3089 && !IsAnOper(state->sptr)) {
3090 send_reply(state->sptr, ERR_CHANSECURED, state->chptr->chname);
3095 /* allow removing and then adding apass, but not adding and then removing */
3096 if (state->dir == MODE_ADD)
3098 if (state->done & DONE_APASS_ADD)
3100 state->done |= DONE_APASS_ADD;
3104 if (state->done & (DONE_APASS_ADD | DONE_APASS_DEL))
3106 state->done |= DONE_APASS_DEL;
3109 /* If the Apass is invalid, tell the user and bail. */
3110 if (!is_clean_key(state, t_str, state->dir == MODE_ADD ? "MODE +A" :
3117 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
3118 !ircd_strcmp(state->chptr->mode.apass, t_str))
3119 return; /* no apass change */
3121 /* Skip if this is a burst, we have an Apass already and the new Apass is
3122 * after the old one alphabetically */
3123 if ((state->flags & MODE_PARSE_BURST) &&
3124 *(state->chptr->mode.apass) &&
3125 ircd_strcmp(state->chptr->mode.apass, t_str) <= 0)
3128 if (state->flags & MODE_PARSE_BOUNCE) {
3129 if (*state->chptr->mode.apass) /* reset old apass */
3130 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
3131 state->chptr->mode.apass, 0);
3132 else /* remove new bogus apass */
3133 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
3134 } else /* send new apass */
3135 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
3137 if (state->flags & MODE_PARSE_SET) {
3138 if (state->dir == MODE_ADD) { /* set the new apass */
3139 /* Only accept the new apass if there is no current apass or
3140 * this is a BURST. */
3141 if (state->chptr->mode.apass[0] == '\0' ||
3142 (state->flags & MODE_PARSE_BURST))
3143 ircd_strncpy(state->chptr->mode.apass, t_str, KEYLEN);
3144 /* Make it VERY clear to the user that this is a one-time password */
3145 if (MyUser(state->sptr)) {
3146 send_reply(state->sptr, RPL_APASSWARN_SET, state->chptr->mode.apass);
3147 send_reply(state->sptr, RPL_APASSWARN_SECRET, state->chptr->chname,
3148 state->chptr->mode.apass);
3150 /* Give the channel manager level 0 ops.
3151 There should not be tested for IsChannelManager here because
3152 on the local server it is impossible to set the apass if one
3153 isn't a channel manager and remote servers might need to sync
3154 the oplevel here: when someone creates a channel (and becomes
3155 channel manager) during a net.break, and only sets the Apass
3156 after the net rejoined, they will have oplevel MAXOPLEVEL on
3157 all remote servers. */
3159 SetOpLevel(state->member, 0);
3160 } else { /* remove the old apass */
3161 *state->chptr->mode.apass = '\0';
3162 /* Clear Upass so that there is never a Upass set when a zannel is burst. */
3163 *state->chptr->mode.upass = '\0';
3164 if (MyUser(state->sptr))
3165 send_reply(state->sptr, RPL_APASSWARN_CLEAR);
3166 /* Revert everyone to MAXOPLEVEL. */
3167 for (memb = state->chptr->members; memb; memb = memb->next_member) {
3168 if (memb->status & MODE_CHANOP)
3169 SetOpLevel(memb, MAXOPLEVEL);
3175 /** Compare one ban's extent to another.
3176 * This works very similarly to mmatch() but it knows about CIDR masks
3177 * and ban exceptions. If both bans are CIDR-based, compare their
3178 * address bits; otherwise, use mmatch().
3179 * @param[in] old_ban One ban.
3180 * @param[in] new_ban Another ban.
3181 * @return Zero if \a old_ban is a superset of \a new_ban, non-zero otherwise.
3184 bmatch(struct Ban *old_ban, struct Ban *new_ban)
3187 assert(old_ban != NULL);
3188 assert(new_ban != NULL);
3189 /* A ban is never treated as a superset of an exception and vice versa. */
3190 if(((old_ban->flags & BAN_EXCEPTION) && !(new_ban->flags & BAN_EXCEPTION))
3191 || (!(old_ban->flags & BAN_EXCEPTION) && (new_ban->flags & BAN_EXCEPTION)))
3193 /* If either is not an address mask, match the text masks. */
3194 if ((old_ban->flags & new_ban->flags & BAN_IPMASK) == 0)
3195 return mmatch(old_ban->banstr, new_ban->banstr);
3196 /* If the old ban has a longer prefix than new, it cannot be a superset. */
3197 if (old_ban->addrbits > new_ban->addrbits)
3199 /* Compare the masks before the hostname part. */
3200 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '\0';
3201 res = mmatch(old_ban->banstr, new_ban->banstr);
3202 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '@';
3205 /* If the old ban's mask mismatches, cannot be a superset. */
3206 if (!ipmask_check(&new_ban->address, &old_ban->address, old_ban->addrbits))
3208 /* Otherwise it depends on whether the old ban's text is a superset
3210 return mmatch(old_ban->banstr, new_ban->banstr);
3213 /** Add a ban from a ban list and mark bans that should be removed
3214 * because they overlap.
3216 * There are three invariants for a ban list. First, no ban may be
3217 * more specific than another ban. Second, no exception may be more
3218 * specific than another exception. Finally, no ban may be more
3219 * specific than any exception.
3221 * @param[in,out] banlist Pointer to head of list.
3222 * @param[in] newban Ban (or exception) to add (or remove).
3223 * @param[in] do_free If non-zero, free \a newban on failure.
3224 * @return Zero if \a newban could be applied, non-zero if not.
3226 int apply_ban(struct Ban **banlist, struct Ban *newban, int do_free)
3231 assert(newban->flags & (BAN_ADD|BAN_DEL));
3232 if (newban->flags & BAN_ADD) {
3234 /* If a less specific *active* entry is found, fail. */
3235 for (ban = *banlist; ban; ban = ban->next) {
3236 if (!bmatch(ban, newban) && !(ban->flags & BAN_DEL)) {
3241 if (!(ban->flags & (BAN_OVERLAPPED|BAN_DEL))) {
3243 totlen += strlen(ban->banstr);
3246 /* Mark more specific entries and add this one to the end of the list. */
3247 while ((ban = *banlist) != NULL) {
3248 if (!bmatch(newban, ban)) {
3249 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
3251 banlist = &ban->next;
3255 } else if (newban->flags & BAN_DEL) {
3256 size_t remove_count = 0;
3257 /* Mark more specific entries. */
3258 for (ban = *banlist; ban; ban = ban->next) {
3259 if (!bmatch(newban, ban)) {
3260 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
3266 /* If no matches were found, fail. */
3276 /* Removes MODE_WASDELJOINS in a channel.
3277 * Reveals all hidden users.
3279 static void reveal_hidden_chan_users(struct ParseState *state, ulong64 *flag_p) {
3280 struct Membership *member;
3282 /* If the channel is not +d, do nothing. */
3283 if(!(state->chptr->mode.mode & MODE_WASDELJOINS)) return;
3285 /* Iterate through all members and reveal them if they are hidden. */
3286 for(member = state->chptr->members; member; member = member->next_member) {
3287 if(IsDelayedJoin(member) && !IsInvisibleJoin(member)) {
3288 RevealDelayedJoin(member);
3293 /* Handle MODE_AUDITORIUM changes
3294 * set Delayed for all hidden users on MODE_DEL
3295 * part all nonoped users on MODE_ADD
3297 static void audit_chan_users(struct ParseState *state, ulong64 *flag_p) {
3298 struct Membership *member;
3299 if (state->dir == MODE_ADD) {
3300 for(member = state->chptr->members; member; member = member->next_member) {
3301 if(!IsChanOpOrHalfOp(member) && !HasVoice(member)) {
3302 sendcmdto_channel_butserv_butone(member->user, CMD_PART, member->channel, member->user, SKIP_OPS, "%H :%s", member->channel, "mode +u set.");
3306 for(member = state->chptr->members; member; member = member->next_member) {
3307 if(!IsChanOpOrHalfOp(member) && !HasVoice(member)) {
3308 sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, SKIP_OPS, ":%H", member->channel);
3316 * Helper function to convert bans
3319 mode_parse_ban(struct ParseState *state, ulong64 *flag_p)
3322 struct Ban *ban, *newban;
3324 if (state->parc <= 0) { /* Not enough args, send ban list */
3325 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
3326 send_ban_list(state->sptr, state->chptr);
3327 state->done |= DONE_BANLIST;
3332 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3335 t_str = state->parv[state->args_used++]; /* grab arg */
3339 /* If they're not an oper, they can't change modes */
3340 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3341 send_notoper(state);
3345 if ((s = strchr(t_str, ' ')))
3348 if (!*t_str || *t_str == ':') { /* warn if empty */
3349 if (MyUser(state->sptr))
3350 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
3355 /* Clear all ADD/DEL/OVERLAPPED flags from ban list. */
3356 if (!(state->done & DONE_BANCLEAN)) {
3357 for (ban = state->chptr->banlist; ban; ban = ban->next)
3358 ban->flags &= ~(BAN_ADD | BAN_DEL | BAN_OVERLAPPED);
3359 state->done |= DONE_BANCLEAN;
3362 /* remember the ban for the moment... */
3363 newban = state->banlist + (state->numbans++);
3365 newban->flags = ((state->dir == MODE_ADD) ? BAN_ADD : BAN_DEL)
3366 | (*flag_p == MODE_BAN ? 0 : BAN_EXCEPTION);
3367 set_ban_mask(newban, collapse(pretty_mask(t_str)));
3368 ircd_strncpy(newban->who, IsUser(state->sptr) ? cli_name(state->sptr) : "*", NICKLEN);
3369 newban->when = TStime();
3370 apply_ban(&state->chptr->banlist, newban, 0);
3374 * This is the bottom half of the ban processor
3377 mode_process_bans(struct ParseState *state)
3379 struct Ban *ban, *newban, *prevban, *nextban;
3385 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
3387 banlen = strlen(ban->banstr);
3389 nextban = ban->next;
3391 if ((ban->flags & (BAN_DEL | BAN_ADD)) == (BAN_DEL | BAN_ADD)) {
3393 prevban->next = 0; /* Break the list; ban isn't a real ban */
3395 state->chptr->banlist = 0;
3401 } else if (ban->flags & BAN_DEL) { /* Deleted a ban? */
3403 DupString(bandup, ban->banstr);
3404 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN, bandup, 1);
3406 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
3407 if (prevban) /* clip it out of the list... */
3408 prevban->next = ban->next;
3410 state->chptr->banlist = ban->next;
3417 continue; /* next ban; keep prevban like it is */
3419 ban->flags &= BAN_IPMASK; /* unset other flags */
3420 } else if (ban->flags & BAN_ADD) { /* adding a ban? */
3422 prevban->next = 0; /* Break the list; ban isn't a real ban */
3424 state->chptr->banlist = 0;
3426 /* If we're supposed to ignore it, do so. */
3427 if (ban->flags & BAN_OVERLAPPED &&
3428 !(state->flags & MODE_PARSE_BOUNCE)) {
3432 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
3433 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
3434 count > feature_int(FEAT_MAXBANS))) {
3435 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
3441 /* add the ban to the buffer */
3442 DupString(bandup, ban->banstr);
3443 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN, bandup, 1);
3445 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
3446 newban = make_ban(ban->banstr);
3447 strcpy(newban->who, ban->who);
3448 newban->when = ban->when;
3449 newban->flags = ban->flags & BAN_IPMASK;
3451 newban->next = state->chptr->banlist; /* and link it in */
3452 state->chptr->banlist = newban;
3461 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
3463 if (changed) /* if we changed the ban list, we must invalidate the bans */
3464 mode_ban_invalidate(state->chptr);
3468 * Helper function to process client changes
3471 mode_parse_client(struct ParseState *state, ulong64 *flag_p)
3475 struct Client *acptr;
3476 struct Membership *member;
3477 int oplevel = MAXOPLEVEL + 1;
3481 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3484 if (state->parc <= 0) /* return if not enough args */
3487 t_str = state->parv[state->args_used++]; /* grab arg */
3491 /* If they're not an oper, they can't change modes */
3492 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3493 send_notoper(state);
3497 if (MyUser(state->sptr)) {
3498 colon = strchr(t_str, ':');
3499 if (colon != NULL) {
3501 req_oplevel = atoi(colon);
3502 if (*flag_p == CHFL_VOICE || *flag_p == CHFL_HALFOP || state->dir == MODE_DEL) {
3503 /* Ignore the colon and its argument. */
3504 } else if (!(state->flags & MODE_PARSE_FORCE)
3506 && (req_oplevel < OpLevel(state->member)
3507 || (req_oplevel == OpLevel(state->member)
3508 && OpLevel(state->member) < MAXOPLEVEL)
3509 || req_oplevel > MAXOPLEVEL)) {
3510 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
3511 t_str, state->chptr->chname,
3512 OpLevel(state->member), req_oplevel, "op",
3513 OpLevel(state->member) == req_oplevel ? "the same" : "a higher");
3514 } else if (req_oplevel <= MAXOPLEVEL)
3515 oplevel = req_oplevel;
3517 if(*flag_p == CHFL_CHANOP && state->member && !IsChanOp(state->member) && !(state->flags & MODE_PARSE_FORCE)) {
3518 send_notoper(state);
3521 /* find client we're manipulating */
3522 acptr = find_chasing(state->sptr, t_str, NULL);
3524 if (t_str[5] == ':') {
3526 oplevel = atoi(t_str + 6);
3528 acptr = findNUser(t_str);
3532 return; /* find_chasing() already reported an error to the user */
3534 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
3535 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
3536 state->cli_change[i].flag & flag_p[0]))
3537 break; /* found a slot */
3539 /* If we are going to bounce this deop, mark the correct oplevel. */
3540 if (state->flags & MODE_PARSE_BOUNCE
3541 && state->dir == MODE_DEL
3542 && flag_p[0] == MODE_CHANOP
3543 && (member = find_member_link(state->chptr, acptr)))
3544 oplevel = OpLevel(member);
3546 /* Store what we're doing to them */
3547 state->cli_change[i].flag = state->dir | flag_p[0];
3548 state->cli_change[i].oplevel = oplevel;
3549 state->cli_change[i].client = acptr;
3553 * Helper function to process the changed client list
3556 mode_process_clients(struct ParseState *state)
3559 struct Membership *member;
3561 for (i = 0; state->cli_change[i].flag; i++) {
3562 assert(0 != state->cli_change[i].client);
3564 /* look up member link */
3565 if (!(member = find_member_link(state->chptr,
3566 state->cli_change[i].client)) ||
3567 (MyUser(state->sptr) && IsZombie(member))) {
3568 if (MyUser(state->sptr))
3569 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
3570 cli_name(state->cli_change[i].client),
3571 state->chptr->chname);
3574 if (member && (IsInvisibleJoin(member) && state->cli_change[i].client != state->sptr) && IsDelayedJoin(member)) {
3575 if (MyUser(state->sptr))
3576 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
3577 cli_name(state->cli_change[i].client),
3578 state->chptr->chname);
3582 if ((state->cli_change[i].flag & MODE_ADD &&
3583 (state->cli_change[i].flag & member->status)) ||
3584 (state->cli_change[i].flag & MODE_DEL &&
3585 !(state->cli_change[i].flag & member->status)))
3586 continue; /* no change made, don't do anything */
3588 /* see if the deop is allowed */
3589 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
3590 (MODE_DEL | MODE_CHANOP)) {
3591 /* Prevent +k users from being deopped, but allow extra ops to deop
3592 * +k users. We do not prevent deopping _REAL_ services because /opmode
3593 * will always allow this.
3595 if(IsChannelService(state->cli_change[i].client) && !IsXtraOp(state->sptr) && state->sptr != state->cli_change[i].client) {
3596 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
3597 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
3599 (IsServer(state->sptr) ? cli_name(state->sptr) :
3600 cli_name((cli_user(state->sptr))->server)));
3602 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
3603 send_reply(state->sptr, ERR_ISCHANSERVICE,
3604 cli_name(state->cli_change[i].client),
3605 state->chptr->chname);
3610 /* check deop for local user */
3611 if (MyUser(state->sptr)) {
3613 /* don't allow local opers to be deopped on local channels */
3614 if (state->cli_change[i].client != state->sptr &&
3615 IsLocalChannel(state->chptr->chname) &&
3616 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
3617 send_reply(state->sptr, ERR_ISOPERLCHAN,
3618 cli_name(state->cli_change[i].client),
3619 state->chptr->chname);
3623 /* Forbid deopping other members with an oplevel less than
3624 * one's own level, and other members with an oplevel the same
3625 * as one's own unless both are at MAXOPLEVEL. */
3626 if (state->sptr != state->cli_change[i].client
3628 && ((OpLevel(member) < OpLevel(state->member))
3629 || (OpLevel(member) == OpLevel(state->member)
3630 && OpLevel(member) < MAXOPLEVEL))) {
3631 int equal = (OpLevel(member) == OpLevel(state->member));
3632 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
3633 cli_name(state->cli_change[i].client),
3634 state->chptr->chname,
3635 OpLevel(state->member), OpLevel(member),
3636 "deop", equal ? "the same" : "a higher");
3642 /* set op-level of member being opped */
3643 if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
3644 (MODE_ADD | MODE_CHANOP)) {
3645 /* If a valid oplevel was specified, use it.
3646 * Otherwise, if being opped by an outsider, get MAXOPLEVEL.
3647 * Otherwise, if not an apass channel, or state->member has
3648 * MAXOPLEVEL, get oplevel MAXOPLEVEL.
3649 * Otherwise, get state->member's oplevel+1.
3651 if (state->cli_change[i].oplevel <= MAXOPLEVEL)
3652 SetOpLevel(member, state->cli_change[i].oplevel);
3653 else if (!state->member)
3654 SetOpLevel(member, MAXOPLEVEL);
3655 else if (OpLevel(state->member) >= MAXOPLEVEL)
3656 SetOpLevel(member, OpLevel(state->member));
3658 SetOpLevel(member, OpLevel(state->member) + 1);
3661 int user_visible = (member->status & CHFL_VOICED_OR_OPPED);
3663 /* actually effect the change */
3664 if (state->flags & MODE_PARSE_SET) {
3665 if (state->cli_change[i].flag & MODE_ADD) {
3666 if (IsDelayedJoin(member) && !IsZombie(member))
3667 RevealDelayedJoin(member);
3668 member->status |= (state->cli_change[i].flag &
3669 (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
3670 if (state->cli_change[i].flag & MODE_CHANOP)
3671 ClearDeopped(member);
3673 member->status &= ~(state->cli_change[i].flag &
3674 (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
3677 /* accumulate the change */
3678 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
3679 state->cli_change[i].client,
3680 state->cli_change[i].oplevel);
3682 if((member->channel->mode.mode & MODE_AUDITORIUM)) {
3683 //join or part the user
3684 if((member->status & CHFL_VOICED_OR_OPPED) && !user_visible) {
3685 sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, SKIP_OPS, ":%H", member->channel);
3686 } else if(!(member->status & CHFL_VOICED_OR_OPPED) && user_visible) {
3687 sendcmdto_channel_butserv_butone(member->user, CMD_PART, member->channel, member->user, SKIP_OPS, "%H :%s", member->channel, "user deoped/devoiced on a +u channel.");
3689 if(MyUser(member->user) && (state->cli_change[i].flag & (MODE_CHANOP | MODE_HALFOP))) {
3690 //do_names(member->user, member->channel, NAMES_ALL|NAMES_EON|((member->status & MODE_CHANOP) ? 0 : NAMES_OPS));
3691 //this is not working for all users :( so we have to send join/part events
3692 struct Membership *member2;
3693 if (state->cli_change[i].flag & MODE_ADD) {
3695 for(member2 = state->chptr->members; member2; member2 = member2->next_member) {
3696 if(!IsChanOpOrHalfOp(member2) && !HasVoice(member2)) {
3697 sendcmdto_one(member2->user, CMD_JOIN, member->user, ":%H", member->channel);
3702 for(member2 = state->chptr->members; member2; member2 = member2->next_member) {
3703 if(!IsChanOpOrHalfOp(member2) && !HasVoice(member2) && member != member2) {
3704 sendcmdto_one(member2->user, CMD_PART, member->user, "%H :%s", member->channel, "invisible user on +u channel.");
3710 } /* for (i = 0; state->cli_change[i].flags; i++) */
3714 * Helper function to process the simple modes
3717 mode_parse_mode(struct ParseState *state, ulong64 *flag_p)
3719 /* If they're not an oper, they can't change modes */
3720 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3721 send_notoper(state);
3728 /* Local users are not permitted to change registration status */
3729 if (flag_p[0] == MODE_REGISTERED && !(state->flags & MODE_PARSE_FORCE) && MyUser(state->sptr))
3732 if(flag_p[0] == MODE_AUDITORIUM)
3733 audit_chan_users(state, flag_p);
3735 if (state->dir == MODE_ADD) {
3736 state->add |= flag_p[0];
3737 state->del &= ~flag_p[0];
3739 if (flag_p[0] & MODE_SECRET) {
3740 state->add &= ~MODE_PRIVATE;
3741 state->del |= MODE_PRIVATE;
3742 } else if (flag_p[0] & MODE_PRIVATE) {
3743 state->add &= ~MODE_SECRET;
3744 state->del |= MODE_SECRET;
3747 state->add &= ~flag_p[0];
3748 state->del |= flag_p[0];
3751 assert(0 == (state->add & state->del));
3752 assert((MODE_SECRET | MODE_PRIVATE) !=
3753 (state->add & (MODE_SECRET | MODE_PRIVATE)));
3757 * This routine is intended to parse MODE or OPMODE commands and effect the
3758 * changes (or just build the bounce buffer).
3760 * \param[out] mbuf Receives parsed representation of mode change.
3761 * \param[in] cptr Connection that sent the message to this server.
3762 * \param[in] sptr Original source of the message.
3763 * \param[in] chptr Channel whose modes are being changed.
3764 * \param[in] parc Number of valid strings in \a parv.
3765 * \param[in] parv Text arguments representing mode change, with the
3766 * zero'th element containing a string like "+m" or "-o".
3767 * \param[in] flags Set of bitwise MODE_PARSE_* flags.
3768 * \param[in] member If non-null, the channel member attempting to change the modes.
3771 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
3772 struct Channel *chptr, int parc, char *parv[], unsigned int flags,
3773 struct Membership* member)
3775 static ulong64 chan_flags[] = {
3781 MODE_MODERATED, 'm',
3782 MODE_TOPICLIMIT, 't',
3783 MODE_INVITEONLY, 'i',
3784 MODE_NOPRIVMSGS, 'n',
3788 MODE_REGISTERED, 'R',
3793 MODE_WASDELJOINS, 'd',
3799 MODE_QUARANTINE, 'Q',
3802 MODE_AUDITORIUM, 'u',
3813 struct ParseState state;
3824 state.chptr = chptr;
3825 state.member = member;
3828 state.flags = flags;
3829 state.dir = MODE_ADD;
3833 state.args_used = 0;
3834 state.max_args = MAXMODEPARAMS;
3837 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
3838 state.banlist[i].next = 0;
3839 state.banlist[i].who[0] = '\0';
3840 state.banlist[i].when = 0;
3841 state.banlist[i].flags = 0;
3842 state.cli_change[i].flag = 0;
3843 state.cli_change[i].client = 0;
3846 modestr = state.parv[state.args_used++];
3850 for (; *modestr; modestr++) {
3851 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
3852 if (flag_p[1] == *modestr)
3855 if (!flag_p[0]) { /* didn't find it? complain and continue */
3856 if (MyUser(state.sptr))
3857 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
3862 case '+': /* switch direction to MODE_ADD */
3863 case '-': /* switch direction to MODE_DEL */
3864 state.dir = flag_p[0];
3867 case 'l': /* deal with limits */
3868 mode_parse_limit(&state, flag_p);
3870 case 'a': /* deal with limits */
3871 mode_parse_access(&state, flag_p);
3874 mode_parse_altchan(&state, flag_p);
3877 mode_parse_noflood(&state, flag_p);
3880 if(IsNetServ(state.sptr) && IsSecurityServ(state.sptr))
3881 mode_parse_mode(&state, flag_p);
3883 case 'k': /* deal with keys */
3884 mode_parse_key(&state, flag_p);
3886 case 'A': /* deal with Admin passes */
3887 if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS))
3888 mode_parse_apass(&state, flag_p);
3890 case 'U': /* deal with user passes */
3891 if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS))
3892 mode_parse_upass(&state, flag_p);
3894 case 'b': /* deal with bans */
3895 mode_parse_ban(&state, flag_p);
3898 case 'd': /* deal with hidden members */
3899 reveal_hidden_chan_users(&state, flag_p);
3902 case 'o': /* deal with ops/voice */
3904 mode_parse_client(&state, flag_p);
3907 if (IsServer(cptr) || feature_bool(FEAT_HALFOP))
3908 mode_parse_client(&state, flag_p);
3910 case 'z': /* remote clients are allowed to change +z */
3911 if(!MyUser(state.sptr) || (state.flags & MODE_PARSE_FORCE))
3912 mode_parse_mode(&state, flag_p);
3914 case 'O': /* remote clients are allowed to change +z */
3915 //struct User* user = cli_user(cptr);
3916 //|| (chptr.chanowner && IsAccount(cptr) && chptr.chanowner == user->account)
3917 if(!MyUser(state.sptr) || (state.flags & MODE_PARSE_FORCE) )
3918 mode_parse_mode(&state, flag_p);
3921 default: /* deal with other modes */
3922 mode_parse_mode(&state, flag_p);
3924 } /* switch (*modestr) */
3925 } /* for (; *modestr; modestr++) */
3927 if (state.flags & MODE_PARSE_BURST)
3928 break; /* don't interpret any more arguments */
3930 if (state.parc > 0) { /* process next argument in string */
3931 modestr = state.parv[state.args_used++];
3935 if (IsServer(state.cptr) && !state.parc && IsDigit(*modestr)) {
3938 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
3939 break; /* we're then going to bounce the mode! */
3941 recv_ts = atoi(modestr);
3943 if (recv_ts && recv_ts < state.chptr->creationtime)
3944 state.chptr->creationtime = recv_ts; /* respect earlier TS */
3945 else if (recv_ts > state.chptr->creationtime) {
3946 struct Client *sserv;
3948 /* Check whether the originating server has fully processed
3949 * the burst to it. */
3951 if (!IsServer(sserv))
3952 sserv = cli_user(sserv)->server;
3953 if (IsBurstOrBurstAck(sserv)) {
3954 /* This is a legal but unusual case; the source server
3955 * probably just has not processed the BURST for this
3956 * channel. It SHOULD wipe out all its modes soon, so
3957 * silently ignore the mode change rather than send a
3958 * bounce that could desync modes from our side (that
3959 * have already been sent).
3961 state.mbuf->mb_add = 0;
3962 state.mbuf->mb_rem = 0;
3963 state.mbuf->mb_count = 0;
3964 return state.args_used;
3966 /* Server is desynced; bounce the mode and deop the source
3968 state.flags &= ~MODE_PARSE_SET;
3969 state.flags |= MODE_PARSE_BOUNCE;
3970 state.mbuf->mb_dest &= ~(MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK4);
3971 state.mbuf->mb_dest |= MODEBUF_DEST_BOUNCE | MODEBUF_DEST_HACK2;
3972 if (!IsServer(state.cptr))
3973 state.mbuf->mb_dest |= MODEBUF_DEST_DEOP;
3977 break; /* break out of while loop */
3978 } else if (state.flags & MODE_PARSE_STRICT ||
3979 (MyUser(state.sptr) && state.max_args <= 0)) {
3980 state.parc++; /* we didn't actually gobble the argument */
3982 break; /* break out of while loop */
3985 } /* while (*modestr) */
3988 * the rest of the function finishes building resultant MODEs; if the
3989 * origin isn't a member or an oper, skip it.
3991 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3992 return state.args_used; /* tell our parent how many args we gobbled */
3994 t_mode = state.chptr->mode.mode;
3996 if (state.del & t_mode) { /* delete any modes to be deleted... */
3997 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
3999 t_mode &= ~state.del;
4001 if (state.add & ~t_mode) { /* add any modes to be added... */
4002 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
4004 t_mode |= state.add;
4007 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
4008 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
4009 !(t_mode & MODE_INVITEONLY))
4010 mode_invite_clear(state.chptr);
4012 state.chptr->mode.mode = t_mode;
4015 if (state.flags & MODE_PARSE_WIPEOUT) {
4016 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
4017 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
4018 state.chptr->mode.limit);
4019 if (state.chptr->mode.access && !(state.done & DONE_ACCESS))
4020 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_ACCESS,
4021 state.chptr->mode.access);
4022 if (state.chptr->mode.altchan && !(state.done & DONE_ALTCHAN))
4023 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_ALTCHAN,
4024 state.chptr->mode.altchan, 0);
4025 if (state.chptr->mode.noflood && !(state.done & DONE_NOFLOOD))
4026 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_NOFLOOD,
4027 state.chptr->mode.noflood, 0);
4028 if (*state.chptr->mode.key && !(state.done & DONE_KEY_DEL))
4029 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
4030 state.chptr->mode.key, 0);
4031 if (*state.chptr->mode.upass && !(state.done & DONE_UPASS_DEL))
4032 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_UPASS,
4033 state.chptr->mode.upass, 0);
4034 if (*state.chptr->mode.apass && !(state.done & DONE_APASS_DEL))
4035 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_APASS,
4036 state.chptr->mode.apass, 0);
4039 if (state.done & DONE_BANCLEAN) /* process bans */
4040 mode_process_bans(&state);
4042 /* process client changes */
4043 if (state.cli_change[0].flag)
4044 mode_process_clients(&state);
4046 return state.args_used; /* tell our parent how many args we gobbled */
4050 * Initialize a join buffer
4053 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
4054 struct Client *connect, unsigned int type, char *comment,
4060 assert(0 != source);
4061 assert(0 != connect);
4063 jbuf->jb_source = source; /* just initialize struct JoinBuf */
4064 jbuf->jb_connect = connect;
4065 jbuf->jb_type = type;
4066 jbuf->jb_comment = comment;
4067 jbuf->jb_create = create;
4069 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
4070 type == JOINBUF_TYPE_PART ||
4071 type == JOINBUF_TYPE_PARTALL) ?
4072 STARTJOINLEN : STARTCREATELEN) +
4073 (comment ? strlen(comment) + 2 : 0));
4075 for (i = 0; i < MAXJOINARGS; i++)
4076 jbuf->jb_channels[i] = 0;
4080 * Add a channel to the join buffer
4083 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
4091 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
4095 is_local = IsLocalChannel(chan->chname);
4097 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
4098 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
4099 struct Membership *member = find_member_link(chan, jbuf->jb_source);
4100 if (IsUserParting(member))
4102 SetUserParting(member);
4104 /* Send notification to channel */
4105 if((chan->mode.mode & MODE_AUDITORIUM) && !(member->status & CHFL_VOICED_OR_OPPED)) {
4106 //send part to ops only
4107 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL, SKIP_NONOPS, "%H :%s", chan, jbuf->jb_comment);
4108 if(MyUser(jbuf->jb_source))
4109 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source, "%H :%s", chan, jbuf->jb_comment);
4110 } else if (!(flags & (CHFL_ZOMBIE | CHFL_DELAYED)))
4111 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL, 0,
4112 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
4113 ":%H" : "%H :%s", chan, jbuf->jb_comment);
4114 else if (MyUser(jbuf->jb_source))
4115 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
4116 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
4117 ":%H" : "%H :%s", chan, jbuf->jb_comment);
4118 /* XXX: Shouldn't we send a PART here anyway? */
4119 /* to users on the channel? Why? From their POV, the user isn't on
4120 * the channel anymore anyway. We don't send to servers until below,
4121 * when we gang all the channel parts together. Note that this is
4122 * exactly the same logic, albeit somewhat more concise, as was in
4123 * the original m_part.c */
4125 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
4126 is_local) /* got to remove user here */
4127 remove_user_from_channel(jbuf->jb_source, chan);
4129 int oplevel = !chan->mode.apass[0] ? MAXOPLEVEL
4130 : (flags & CHFL_CHANNEL_MANAGER) ? 0
4132 /* Add user to channel */
4133 if (((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED)) || ((flags & CHFL_INVISIBLE) && !(flags & CHFL_VOICED_OR_OPPED))) {
4134 add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, oplevel);
4136 add_user_to_channel(chan, jbuf->jb_source, flags, oplevel);
4138 /* send JOIN notification to all servers (CREATE is sent later). */
4139 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local) {
4140 if ((flags & CHFL_INVISIBLE) && !(flags & CHFL_VOICED_OR_OPPED)) {
4141 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
4142 "%H %Tu %i", chan, chan->creationtime, 1);
4144 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
4145 "%H %Tu %i", chan, chan->creationtime, 0);
4149 if((chan->mode.mode & MODE_AUDITORIUM) && !(flags & CHFL_VOICED_OR_OPPED)) {
4150 //we have to send this JOIN event to ops only...
4151 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, SKIP_NONOPS, "%H", chan);
4152 if(MyUser(jbuf->jb_source))
4153 sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan);
4155 else if (!((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED)) && !((flags & CHFL_INVISIBLE) && !(flags & CHFL_VOICED_OR_OPPED))) {
4156 /* Send the notification to the channel */
4157 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, 0, "%H", chan);
4159 /* send an op, too, if needed */
4160 if (flags & CHFL_CHANOP && (oplevel < MAXOPLEVEL || !MyUser(jbuf->jb_source)))
4161 sendcmdto_channel_butserv_butone((chan->mode.apass[0] ? &his : jbuf->jb_source),
4162 CMD_MODE, chan, NULL, 0, "%H +o %C",
4163 chan, jbuf->jb_source);
4164 } else if (MyUser(jbuf->jb_source)) {
4165 sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan);
4169 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
4170 jbuf->jb_type == JOINBUF_TYPE_JOIN || is_local)
4171 return; /* don't send to remote */
4173 /* figure out if channel name will cause buffer to be overflowed */
4174 len = chan ? strlen(chan->chname) + 1 : 2;
4175 if (jbuf->jb_strlen + len > BUFSIZE)
4176 joinbuf_flush(jbuf);
4178 /* add channel to list of channels to send and update counts */
4179 jbuf->jb_channels[jbuf->jb_count++] = chan;
4180 jbuf->jb_strlen += len;
4182 /* if we've used up all slots, flush */
4183 if (jbuf->jb_count >= MAXJOINARGS)
4184 joinbuf_flush(jbuf);
4188 * Flush the channel list to remote servers
4191 joinbuf_flush(struct JoinBuf *jbuf)
4193 char chanlist[BUFSIZE];
4197 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
4198 jbuf->jb_type == JOINBUF_TYPE_JOIN)
4199 return 0; /* no joins to process */
4201 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
4202 build_string(chanlist, &chanlist_i,
4203 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
4204 i == 0 ? '\0' : ',');
4205 if (JOINBUF_TYPE_PART == jbuf->jb_type)
4206 /* Remove user from channel */
4207 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
4209 jbuf->jb_channels[i] = 0; /* mark slot empty */
4212 jbuf->jb_count = 0; /* reset base counters */
4213 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
4214 STARTJOINLEN : STARTCREATELEN) +
4215 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
4217 /* and send the appropriate command */
4218 switch (jbuf->jb_type) {
4219 case JOINBUF_TYPE_CREATE:
4220 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
4221 "%s %Tu", chanlist, jbuf->jb_create);
4224 case JOINBUF_TYPE_PART:
4225 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
4226 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,
4234 /* Returns TRUE (1) if client is invited, FALSE (0) if not */
4235 int IsInvited(struct Client* cptr, const void* chptr)
4239 for (lp = (cli_user(cptr))->invited; lp; lp = lp->next)
4240 if (lp->value.chptr == chptr)
4245 /* RevealDelayedJoin: sends a join for a hidden user */
4247 void RevealDelayedJoin(struct Membership *member)
4249 ClearDelayedJoin(member);
4250 sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, 0, ":%H",
4252 CheckDelayedJoins(member->channel);
4255 /* CheckDelayedJoins: checks and clear +d if necessary */
4257 void CheckDelayedJoins(struct Channel *chan)
4259 if ((chan->mode.mode & MODE_WASDELJOINS) && !find_delayed_joins(chan)) {
4260 chan->mode.mode &= ~MODE_WASDELJOINS;
4261 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan, NULL, 0,
4263 sendcmdto_channel_servers_butone(&me, CMD_MODE, chan, NULL, 0,
4268 void CheckEnableDelayedJoins(struct Channel *chan) {
4269 if (!(chan->mode.mode & MODE_WASDELJOINS) && find_delayed_joins(chan)) {
4270 chan->mode.mode |= MODE_WASDELJOINS;
4271 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan, NULL, 0,
4273 sendcmdto_channel_servers_butone(&me, CMD_MODE, chan, NULL, 0,
4278 /* checks whether a channel is nonpersistent with no users and deletes it
4279 * returns 1 if deleted, otherwise 0
4281 signed int destruct_nonpers_channel(struct Channel *chptr) {
4282 if(!(chptr->mode.mode & MODE_PERSIST) && chptr->users == 0) {
4283 if(chptr->destruct_event)
4284 remove_destruct_event(chptr);
4285 destruct_channel(chptr);
4291 /** Send a join for the user if (s)he is a hidden member of the channel.
4293 void RevealDelayedJoinIfNeeded(struct Client *sptr, struct Channel *chptr)
4295 struct Membership *member = find_member_link(chptr, sptr);
4296 if (member && IsDelayedJoin(member) && !IsInvisibleJoin(member))
4297 RevealDelayedJoin(member);
4300 /** Extended multi target message block check.
4301 * The channelmode MODE_NOAMSGS prevents multi-target messages from being sent
4302 * to the channel. Since many clients sent these so called AMSGS manually with
4303 * one PRIVMSG per channel, we have this extended check which prevents messages
4304 * from being sent to multiple channels.
4306 * The check is quite simple: We save the last sent message of every client.
4307 * When the client sends a new PRIVMSG to another channel, we compare the
4308 * message to the previously sent message and if they equal we may drop the new
4309 * message instead of broadcasting it.
4310 * There are several additional checks. That is, the new message must be sent in
4311 * a specific amount of time in which this check works. If the ducplicate
4312 * message is delayed for too long then it will pass this check. Furthermore,
4313 * there is a specific number a message must be already sent to different
4314 * channels until it is blocked.
4315 * Both numbers can be configured by the FEATURES subsystem.
4317 * This function returns 0 if the message may pass and 1 if the message should
4319 * For this function to work properly it must be called on every PRIVMSG which
4320 * is sent by any user.
4322 * Since users are creative and sometimes smart (damnit!) this function can be
4323 * outsourced into a dynamic library so we can adjust this function to protect
4324 * against any user scripts on-the-fly.
4328 int ext_amsg_block(struct Client *cptr, struct Channel *chptr, const char *msg)
4332 /* First on every message we check whether the mechanism is enabled.
4333 * If it is enabled, we check:
4334 * - whether the channel has MODE_NOAMSGS
4335 * - whether it was sent in between the configured time span
4336 * - whether it matches the previous message
4337 * in exactly this order. If at least one test fails, we do not block the
4339 * If at least one test failed we copy the message into the users buffer so
4340 * it will be checked the next time he sends a message. We also update the
4341 * timestamp of the user.
4343 amsg_time = feature_int(FEAT_NOAMSG_TIME);
4345 /* first of all strip the message (filter out invisible content) */
4346 char *stripped_message = MyMalloc(BUFSIZE + 1);
4347 strcpy(stripped_message, msg);
4348 char p = stripped_message[0];
4350 int is_visible = 1, is_ccode = 0, i = 0, j = 0;
4352 for(i = 0; p != '\n' && p != 0; p = stripped_message[++i]) {
4356 } else if(is_ccode) {
4357 if((p >= 48 && p <= 57) || p == 44) {
4367 if(p != codes[j++]) {
4372 //END of color code...
4374 if(is_ccode != 1 && codes[j] != 0) is_ccode = 3;
4378 for(k = 0; k < j-1; k++) {
4379 if(codes[k] != 48) {
4385 } else if(is_ccode == 2) {
4387 } else if(is_ccode == 3) {
4395 stripped_message[p_pos++] = p;
4398 stripped_message[p_pos++] = 0;
4399 /* Allocate a new buffer if there is none, yet. */
4400 if(!cli_user(cptr)->lastmsg) {
4401 cli_user(cptr)->lastmsg = MyMalloc(BUFSIZE + 1);
4402 memset(cli_user(cptr)->lastmsg, 0, BUFSIZE + 1);
4404 if((chptr->mode.mode & MODE_NOAMSGS) &&
4405 ((cli_user(cptr)->lastmsg_time + amsg_time) >= CurrentTime) &&
4406 (strcmp(cli_user(cptr)->lastmsg, stripped_message) == 0)) {
4407 cli_user(cptr)->lastmsg_time = CurrentTime;
4408 cli_user(cptr)->lastmsg_num++;
4409 MyFree(stripped_message);
4410 if(cli_user(cptr)->lastmsg_num >= feature_int(FEAT_NOAMSG_NUM)) return 1;
4413 /* Message did not match so update the data. */
4414 cli_user(cptr)->lastmsg_time = CurrentTime;
4415 cli_user(cptr)->lastmsg_num = 0;
4416 strcpy(cli_user(cptr)->lastmsg, stripped_message);
4417 MyFree(stripped_message);
4422 /** Extended flood check.
4423 * The channelmode MODE_NOFLOOD prevents users from flooding the channel.
4425 * This function returns 0 if the message may pass and 1 if the message should
4427 * For this function to work properly it must be called on every PRIVMSG which
4428 * is sent by any user.
4432 int ext_noflood_block(struct Client *cptr, struct Channel *chptr) {
4433 if(!*chptr->mode.noflood) return 0;
4434 struct Membership *member = find_member_link(chptr, cptr);
4435 if(!member) return 0; //TODO: we've no check for -n channels implemented, yet
4436 //check if this user is really affected by +f
4437 unsigned int flags = (chptr->mode.noflood_value & 0x0000000f); //0000 0000 0000 0000 0000 0000 0000 1111 = 0x0000000f >> 0
4438 unsigned int count = (chptr->mode.noflood_value & 0x00002ff0) >> 4; //0000 0000 0000 0000 0011 1111 1111 0000 = 0x00002ff0 >> 4
4439 int time = (chptr->mode.noflood_value & 0x0fffc000) >> 14; //0000 1111 1111 1111 1100 0000 0000 0000 = 0x0fffc000 >> 14
4440 if(count == 0 || time == 0) return 0;
4441 if(!(flags & FLFL_NOFLOOD) && HasPriv(cptr, PRIV_FLOOD))
4443 if(!(flags & FLFL_CHANOP) && (member->status & CHFL_CHANOP))
4445 if(!(flags & (FLFL_CHANOP | FLFL_HALFOP)) && (member->status & CHFL_HALFOP))
4447 if(!(flags & (FLFL_CHANOP | FLFL_HALFOP | FLFL_VOICE)) && (member->status & CHFL_VOICE))
4450 struct MemberFlood *floodnode, *prev_floodnode;
4451 for (floodnode = member->flood; floodnode; floodnode = floodnode->next_memberflood) {
4452 if(floodnode->time + time > CurrentTime) {
4453 if(floodcount == 0 && floodnode != member->flood) {
4455 prev_floodnode->next_memberflood = free_MemberFlood;
4456 free_MemberFlood = prev_floodnode;
4457 member->flood = floodnode;
4461 prev_floodnode = floodnode;
4463 Debug((DEBUG_INFO, "floodcount: %i", floodcount));
4464 if(floodcount >= count) return 1; //blocked!
4465 //add a new floodnode :)
4466 if(free_MemberFlood) {
4467 floodnode = free_MemberFlood;
4468 free_MemberFlood = floodnode->next_memberflood;
4470 floodnode = (struct MemberFlood*) MyMalloc(sizeof(struct MemberFlood));
4471 floodnode->time = CurrentTime;
4472 floodnode->next_memberflood = NULL;
4474 prev_floodnode->next_memberflood = floodnode;
4476 member->flood = floodnode;