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.
26 #include "destruct_event.h"
29 #include "ircd_alloc.h"
30 #include "ircd_chattr.h"
31 #include "ircd_defs.h"
32 #include "ircd_features.h"
34 #include "ircd_policy.h"
35 #include "ircd_reply.h"
36 #include "ircd_snprintf.h"
37 #include "ircd_string.h"
44 #include "querycmds.h"
61 struct Channel* GlobalChannelList = 0;
63 static unsigned int membershipAllocCount;
64 static struct Membership* membershipFreeList;
66 void del_invite(struct Client *, struct Channel *);
68 const char* const PartFmt1 = ":%s " MSG_PART " %s";
69 const char* const PartFmt2 = ":%s " MSG_PART " %s :%s";
70 const char* const PartFmt1serv = "%s%s " TOK_PART " %s";
71 const char* const PartFmt2serv = "%s%s " TOK_PART " %s :%s";
74 static struct SLink* next_ban;
75 static struct SLink* prev_ban;
76 static struct SLink* removed_bans_list;
79 * Use a global variable to remember if an oper set a mode on a local channel. Ugly,
80 * but the only way to do it without changing set_mode intensively.
82 int LocalChanOperMode = 0;
86 * return the length (>=0) of a chain of links.
88 static int list_length(struct SLink *lp)
92 for (; lp; lp = lp->next)
98 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
100 struct Membership *m;
104 /* Servers don't have member links */
105 if (IsServer(cptr)||IsMe(cptr))
108 /* +k users are typically on a LOT of channels. So we iterate over who
109 * is in the channel. X/W are +k and are in about 5800 channels each.
110 * however there are typically no more than 1000 people in a channel
113 if (IsChannelService(cptr)) {
116 assert(m->channel == chptr);
122 /* Users on the other hand aren't allowed on more than 15 channels. 50%
123 * of users that are on channels are on 2 or less, 95% are on 7 or less,
124 * and 99% are on 10 or less.
127 m = (cli_user(cptr))->channel;
129 assert(m->user == cptr);
130 if (m->channel == chptr)
139 * find_chasing - Find the client structure for a nick name (user)
140 * using history mechanism if necessary. If the client is not found, an error
141 * message (NO SUCH NICK) is generated. If the client was found
142 * through the history, chasing will be 1 and otherwise 0.
144 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
146 struct Client* who = FindClient(user);
153 if (!(who = get_history(user, feature_int(FEAT_KILLCHASETIMELIMIT)))) {
154 send_reply(sptr, ERR_NOSUCHNICK, user);
163 * Create a string of form "foo!bar@fubar" given foo, bar and fubar
164 * as the parameters. If NULL, they become "*".
166 #define NUH_BUFSIZE (NICKLEN + USERLEN + HOSTLEN + 3)
167 static char *make_nick_user_host(char *namebuf, const char *nick,
168 const char *name, const char *host)
170 ircd_snprintf(0, namebuf, NUH_BUFSIZE, "%s!%s@%s", nick, name, host);
175 * Create a string of form "foo!bar@123.456.789.123" given foo, bar and the
176 * IP-number as the parameters. If NULL, they become "*".
178 #define NUI_BUFSIZE (NICKLEN + USERLEN + 16 + 3)
179 static char *make_nick_user_ip(char *ipbuf, char *nick, char *name,
182 ircd_snprintf(0, ipbuf, NUI_BUFSIZE, "%s!%s@%s", nick, name,
183 ircd_ntoa((const char*) &ip));
188 * Subtract one user from channel i (and free channel
189 * block, if channel became empty).
190 * Returns: true (1) if channel still has members.
191 * false (0) if the channel is now empty.
193 int sub1_from_channel(struct Channel* chptr)
195 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
197 assert(0 != chptr->members);
205 * Also channels without Apass set need to be kept alive,
206 * otherwise Bad Guys(tm) would be able to takeover
207 * existing channels too easily, and then set an Apass!
208 * However, if a channel without Apass becomes empty
209 * then we try to be kind to them and remove possible
212 chptr->mode.mode &= ~MODE_INVITEONLY;
213 chptr->mode.limit = 0;
215 * We do NOT reset a possible key or bans because when
216 * the 'channel owners' can't get in because of a key
217 * or ban then apparently there was a fight/takeover
218 * on the channel and we want them to contact IRC opers
219 * who then will educate them on the use of Apass/upass.
222 if (TStime() - chptr->creationtime < 172800) /* Channel younger than 48 hours? */
223 schedule_destruct_event_1m(chptr); /* Get rid of it in approximately 4-5 minutes */
225 schedule_destruct_event_48h(chptr); /* Get rid of it in approximately 48 hours */
230 int destruct_channel(struct Channel* chptr)
235 assert(0 == chptr->members);
237 /* Channel became (or was) empty: Remove channel */
238 if (is_listed(chptr))
241 for (i = 0; i <= HighestFd; i++)
243 struct Client *acptr = 0;
244 if ((acptr = LocalClientArray[i]) && cli_listing(acptr) &&
245 (cli_listing(acptr))->chptr == chptr)
247 list_next_channels(acptr, 1);
248 break; /* Only one client can list a channel */
253 * Now, find all invite links from channel structure
255 while ((tmp = chptr->invites))
256 del_invite(tmp->value.cptr, chptr);
258 tmp = chptr->banlist;
263 MyFree(obtmp->value.ban.banstr);
264 MyFree(obtmp->value.ban.who);
268 chptr->prev->next = chptr->next;
270 GlobalChannelList = chptr->next;
272 chptr->next->prev = chptr->prev;
274 --UserStats.channels;
276 * make sure that channel actually got removed from hash table
278 assert(chptr->hnext == chptr);
286 * `cptr' must be the client adding the ban.
288 * If `change' is true then add `banid' to channel `chptr'.
289 * Returns 0 if the ban was added.
290 * Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
291 * Return -1 otherwise.
293 * Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
294 * when `change' is false, otherwise they will be removed from the banlist.
295 * Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
296 * respectively will return these bans until NULL is returned.
298 * If `firsttime' is true, the ban list as returned by next_overlapped_ban()
299 * is reset (unless a non-zero value is returned, in which case the
300 * CHFL_BAN_OVERLAPPED flag might not have been reset!).
304 int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
305 int change, int firsttime)
310 int removed_bans = 0;
311 int len = strlen(banid);
316 assert(0 == prev_ban);
317 assert(0 == removed_bans_list);
321 for (banp = &chptr->banlist; *banp;)
323 len += strlen((*banp)->value.ban.banstr);
325 if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
327 if (!strcmp((*banp)->value.ban.banstr, banid))
329 (*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
333 else if (!mmatch((*banp)->value.ban.banstr, banid))
335 if (!mmatch(banid, (*banp)->value.ban.banstr))
337 struct SLink *tmp = *banp;
343 len -= strlen(tmp->value.ban.banstr);
346 /* These will be sent to the user later as -b */
347 tmp->next = removed_bans_list;
348 removed_bans_list = tmp;
351 else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
353 tmp->flags |= CHFL_BAN_OVERLAPPED;
364 (*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
365 banp = &(*banp)->next;
368 if (MyUser(cptr) && !removed_bans &&
369 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
370 (cnt >= feature_int(FEAT_MAXBANS))))
372 send_reply(cptr, ERR_BANLISTFULL, chptr->chname, banid);
378 struct Membership* member;
380 ban->next = chptr->banlist;
382 ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
383 assert(0 != ban->value.ban.banstr);
384 strcpy(ban->value.ban.banstr, banid);
386 #ifdef HEAD_IN_SAND_BANWHO
388 DupString(ban->value.ban.who, cli_name(&me));
391 DupString(ban->value.ban.who, cli_name(cptr));
392 assert(0 != ban->value.ban.who);
394 ban->value.ban.when = TStime();
395 ban->flags = CHFL_BAN; /* This bit is never used I think... */
396 if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
397 ban->flags |= CHFL_BAN_IPMASK;
398 chptr->banlist = ban;
401 * Erase ban-valid-bit
403 for (member = chptr->members; member; member = member->next_member)
404 ClearBanValid(member); /* `ban' == channel member ! */
409 struct SLink *next_removed_overlapped_ban(void)
411 struct SLink *tmp = removed_bans_list;
414 if (prev_ban->value.ban.banstr) /* Can be set to NULL in set_mode() */
415 MyFree(prev_ban->value.ban.banstr);
416 MyFree(prev_ban->value.ban.who);
421 removed_bans_list = removed_bans_list->next;
427 * find_channel_member - returns Membership * if a person is joined and not a zombie
429 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
431 struct Membership* member;
434 member = find_member_link(chptr, cptr);
435 return (member && !IsZombie(member)) ? member : 0;
439 * is_banned - a non-zero value if banned else 0.
441 static int is_banned(struct Client *cptr, struct Channel *chptr,
442 struct Membership* member)
445 char nu_host[NUH_BUFSIZE];
446 char nu_realhost[NUH_BUFSIZE];
447 char nu_ip[NUI_BUFSIZE];
455 if (member && IsBanValid(member))
456 return IsBanned(member);
458 s = make_nick_user_host(nu_host, cli_name(cptr), (cli_user(cptr))->username,
459 (cli_user(cptr))->host);
460 if (HasHiddenHost(cptr))
461 sr = make_nick_user_host(nu_realhost, cli_name(cptr),
462 (cli_user(cptr))->username,
463 cli_user(cptr)->realhost);
465 for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
466 if ((tmp->flags & CHFL_BAN_IPMASK)) {
468 ip_s = make_nick_user_ip(nu_ip, cli_name(cptr),
469 (cli_user(cptr))->username, cli_ip(cptr));
470 if (match(tmp->value.ban.banstr, ip_s) == 0)
473 else if (match(tmp->value.ban.banstr, s) == 0)
475 else if (sr && match(tmp->value.ban.banstr, sr) == 0)
491 return (tmp != NULL);
495 * adds a user to a channel by adding another link to the channels member
498 void add_user_to_channel(struct Channel* chptr, struct Client* who,
499 unsigned int flags, int oplevel)
506 struct Membership* member = membershipFreeList;
508 membershipFreeList = member->next_member;
510 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
511 ++membershipAllocCount;
516 member->channel = chptr;
517 member->status = flags;
518 member->oplevel = oplevel;
520 member->next_member = chptr->members;
521 if (member->next_member)
522 member->next_member->prev_member = member;
523 member->prev_member = 0;
524 chptr->members = member;
526 member->next_channel = (cli_user(who))->channel;
527 if (member->next_channel)
528 member->next_channel->prev_channel = member;
529 member->prev_channel = 0;
530 (cli_user(who))->channel = member;
532 if (chptr->destruct_event)
533 remove_destruct_event(chptr);
535 ++((cli_user(who))->joined);
539 static int remove_member_from_channel(struct Membership* member)
541 struct Channel* chptr;
543 chptr = member->channel;
545 * unlink channel member list
547 if (member->next_member)
548 member->next_member->prev_member = member->prev_member;
549 if (member->prev_member)
550 member->prev_member->next_member = member->next_member;
552 member->channel->members = member->next_member;
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->next_member = membershipFreeList;
567 membershipFreeList = member;
569 return sub1_from_channel(chptr);
572 static int channel_all_zombies(struct Channel* chptr)
574 struct Membership* member;
576 for (member = chptr->members; member; member = member->next_member) {
577 if (!IsZombie(member))
584 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
587 struct Membership* member;
590 if ((member = find_member_link(chptr, cptr))) {
591 if (remove_member_from_channel(member)) {
592 if (channel_all_zombies(chptr)) {
594 * XXX - this looks dangerous but isn't if we got the referential
595 * integrity right for channels
597 while (remove_member_from_channel(chptr->members))
604 void remove_user_from_all_channels(struct Client* cptr)
606 struct Membership* chan;
608 assert(0 != cli_user(cptr));
610 while ((chan = (cli_user(cptr))->channel))
611 remove_user_from_channel(cptr, chan->channel);
614 int is_chan_op(struct Client *cptr, struct Channel *chptr)
616 struct Membership* member;
618 if ((member = find_member_link(chptr, cptr)))
619 return (!IsZombie(member) && IsChanOp(member));
624 int is_zombie(struct Client *cptr, struct Channel *chptr)
626 struct Membership* member;
630 if ((member = find_member_link(chptr, cptr)))
631 return IsZombie(member);
635 int has_voice(struct Client* cptr, struct Channel* chptr)
637 struct Membership* member;
640 if ((member = find_member_link(chptr, cptr)))
641 return (!IsZombie(member) && HasVoice(member));
646 int member_can_send_to_channel(struct Membership* member)
650 /* Discourage using the Apass to get op. They should use the upass. */
651 if (IsChannelManager(member) && *member->channel->mode.upass)
654 if (IsVoicedOrOpped(member))
657 * If it's moderated, and you aren't a priviledged user, you can't
660 if (member->channel->mode.mode & MODE_MODERATED)
663 * If you're banned then you can't speak either.
664 * but because of the amount of CPU time that is_banned chews
665 * we only check it for our clients.
667 if (MyUser(member->user) && is_banned(member->user, member->channel, member))
672 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
674 struct Membership *member;
677 * Servers can always speak on channels.
682 member = find_channel_member(cptr, chptr);
685 * You can't speak if your off channel, if the channel is modeless, or
686 * +n (no external messages) or +m (moderated).
689 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED)) ||
690 IsModelessChannel(chptr->chname) ||
691 ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(cptr)))
694 return !is_banned(cptr, chptr, NULL);
696 return member_can_send_to_channel(member);
700 * find_no_nickchange_channel
701 * if a member and not opped or voiced and banned
702 * return the name of the first channel banned on
704 const char* find_no_nickchange_channel(struct Client* cptr)
707 struct Membership* member;
708 for (member = (cli_user(cptr))->channel; member;
709 member = member->next_channel) {
710 if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
711 return member->channel->chname;
719 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
720 * with the parameters in pbuf.
722 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen,
723 struct Channel *chptr, struct Membership *member)
725 int previous_parameter = 0;
732 if (chptr->mode.mode & MODE_SECRET)
734 else if (chptr->mode.mode & MODE_PRIVATE)
736 if (chptr->mode.mode & MODE_MODERATED)
738 if (chptr->mode.mode & MODE_TOPICLIMIT)
740 if (chptr->mode.mode & MODE_INVITEONLY)
742 if (chptr->mode.mode & MODE_NOPRIVMSGS)
744 if (chptr->mode.mode & MODE_REGONLY)
746 if (chptr->mode.limit) {
748 ircd_snprintf(0, pbuf, buflen, "%u", chptr->mode.limit);
749 previous_parameter = 1;
752 if (*chptr->mode.key) {
754 if (previous_parameter)
756 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
757 strcat(pbuf, chptr->mode.key);
760 previous_parameter = 1;
762 if (*chptr->mode.apass) {
764 if (previous_parameter)
766 if (IsServer(cptr)) {
767 strcat(pbuf, chptr->mode.apass);
770 previous_parameter = 1;
772 if (*chptr->mode.upass) {
774 if (previous_parameter)
776 if (IsServer(cptr) || (member && IsChanOp(member) && OpLevel(member) == 0)) {
777 strcat(pbuf, chptr->mode.upass);
784 int compare_member_oplevel(const void *mp1, const void *mp2)
786 struct Membership const* member1 = *(struct Membership const**)mp1;
787 struct Membership const* member2 = *(struct Membership const**)mp2;
788 if (member1->oplevel == member2->oplevel)
790 return (member1->oplevel < member2->oplevel) ? -1 : 1;
794 * send "cptr" a full list of the modes for channel chptr.
796 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
798 /* The order in which modes are generated is now mandatory */
799 static unsigned int current_flags[4] =
800 { 0, CHFL_VOICE, CHFL_CHANOP, CHFL_CHANOP | CHFL_VOICE };
806 struct Membership* member;
808 char modebuf[MODEBUFLEN];
809 char parabuf[MODEBUFLEN];
811 int number_of_ops = 0;
812 int opped_members_index = 0;
813 struct Membership** opped_members = NULL;
814 int last_oplevel = 0;
819 if (IsLocalChannel(chptr->chname))
822 member = chptr->members;
823 lp2 = chptr->banlist;
825 *modebuf = *parabuf = '\0';
826 channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr, 0);
828 for (first = 1; full; first = 0) /* Loop for multiple messages */
830 full = 0; /* Assume by default we get it
831 all in one message */
833 /* (Continued) prefix: "<Y> B <channel> <TS>" */
834 /* is there any better way we can do this? */
835 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
836 chptr->creationtime);
838 if (first && modebuf[1]) /* Add simple modes (Aiklmnpstu)
841 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
842 msgq_append(&me, mb, " %s", modebuf);
845 msgq_append(&me, mb, " %s", parabuf);
849 * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
851 * First find all opless members.
852 * Run 2 times over all members, to group the members with
853 * and without voice together.
854 * Then run 2 times over all opped members (which are ordered
855 * by op-level) to also group voice and non-voice together.
857 for (first = 1; flag_cnt < 4; new_mode = 1, ++flag_cnt)
861 if (flag_cnt < 2 && IsChanOp(member))
864 * The first loop (to find all non-voice/op), we count the ops.
865 * The second loop (to find all voiced non-ops), store the ops
866 * in a dynamic array.
871 opped_members[opped_members_index++] = member;
873 /* Only handle the members with the flags that we are interested in. */
874 if ((member->status & CHFL_VOICED_OR_OPPED) == current_flags[flag_cnt])
876 if (msgq_bufleft(mb) < NUMNICKLEN + 3 + MAXOPLEVELDIGITS)
877 /* The 3 + MAXOPLEVELDIGITS is a possible ",:v999". */
879 full = 1; /* Make sure we continue after
881 /* Ensure the new BURST line contains the current
882 * ":mode", except when there is no mode yet. */
883 new_mode = (flag_cnt > 0) ? 1 : 0;
884 break; /* Do not add this member to this message */
886 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
887 first = 0; /* From now on, use commas to add new nicks */
890 * Do we have a nick with a new mode ?
891 * Or are we starting a new BURST line?
896 * This means we are at the _first_ member that has only
897 * voice, or the first member that has only ops, or the
898 * first member that has voice and ops (so we get here
899 * at most three times, plus once for every start of
900 * a continued BURST line where only these modes is current.
901 * In the two cases where the current mode includes ops,
902 * we need to add the _absolute_ value of the oplevel to the mode.
904 char tbuf[3 + MAXOPLEVELDIGITS] = ":";
907 if (HasVoice(member)) /* flag_cnt == 1 or 3 */
909 if (IsChanOp(member)) /* flag_cnt == 2 or 3 */
911 /* append the absolute value of the oplevel */
912 loc += ircd_snprintf(0, tbuf + loc, sizeof(tbuf) - loc, "%u", member->oplevel);
913 last_oplevel = member->oplevel;
916 msgq_append(&me, mb, tbuf);
919 else if (flag_cnt > 1 && last_oplevel != member->oplevel)
922 * This can't be the first member of a (continued) BURST
923 * message because then either flag_cnt == 0 or new_mode == 1
924 * Now we need to append the incremental value of the oplevel.
926 char tbuf[2 + MAXOPLEVELDIGITS];
927 ircd_snprintf(0, tbuf, sizeof(tbuf), ":%u", member->oplevel - last_oplevel);
928 last_oplevel = member->oplevel;
929 msgq_append(&me, mb, tbuf);
932 /* Go to the next `member'. */
934 member = member->next_member;
936 member = opped_members[++opped_members_index];
941 /* Point `member' at the start of the list again. */
944 member = chptr->members;
945 /* Now, after one loop, we know the number of ops and can
946 * allocate the dynamic array with pointer to the ops. */
947 opped_members = (struct Membership**)
948 MyMalloc((number_of_ops + 1) * sizeof(struct Membership*));
949 opped_members[number_of_ops] = NULL; /* Needed for loop termination */
953 /* At the end of the second loop, sort the opped members with
954 * increasing op-level, so that we will output them in the
955 * correct order (and all op-level increments stay positive) */
957 qsort(opped_members, number_of_ops,
958 sizeof(struct Membership*), compare_member_oplevel);
959 /* The third and fourth loop run only over the opped members. */
960 member = opped_members[(opped_members_index = 0)];
963 } /* loop over 0,+v,+o,+ov */
967 /* Attach all bans, space seperated " :%ban ban ..." */
968 for (first = 2; lp2; lp2 = lp2->next)
970 len = strlen(lp2->value.ban.banstr);
971 if (msgq_bufleft(mb) < len + 1 + first)
972 /* The +1 stands for the added ' '.
973 * The +first stands for the added ":%".
979 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
980 lp2->value.ban.banstr);
985 send_buffer(cptr, mb, 0); /* Send this message */
987 } /* Continue when there was something
988 that didn't fit (full==1) */
990 MyFree(opped_members);
996 * by Carlo Wood (Run), 05 Oct 1998.
1000 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
1001 * When the user name or host name are too long (USERLEN and HOSTLEN
1002 * respectively) then they are cut off at the start with a '*'.
1004 * The following transformations are made:
1006 * 1) xxx -> nick!*@*
1007 * 2) xxx.xxx -> *!*@host
1008 * 3) xxx!yyy -> nick!user@*
1009 * 4) xxx@yyy -> *!user@host
1010 * 5) xxx!yyy@zzz -> nick!user@host
1012 char *pretty_mask(char *mask)
1014 static char star[2] = { '*', 0 };
1015 static char retmask[NUH_BUFSIZE];
1016 char *last_dot = NULL;
1019 /* Case 1: default */
1024 /* Do a _single_ pass through the characters of the mask: */
1025 for (ptr = mask; *ptr; ++ptr)
1029 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1033 else if (*ptr == '@')
1035 /* Case 4: Found last '@' (without finding a '!' yet) */
1040 else if (*ptr == '.')
1042 /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
1052 /* Case 4 or 5: Found last '@' */
1058 if (user == star && last_dot)
1068 char *nick_end = (user != star) ? user - 1 : ptr;
1069 if (nick_end - nick > NICKLEN)
1075 char *user_end = (host != star) ? host - 1 : ptr;
1076 if (user_end - user > USERLEN)
1078 user = user_end - USERLEN;
1083 if (host != star && ptr - host > HOSTLEN)
1085 host = ptr - HOSTLEN;
1088 return make_nick_user_host(retmask, nick, user, host);
1091 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1098 for (lp = chptr->banlist; lp; lp = lp->next)
1099 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->value.ban.banstr,
1100 lp->value.ban.who, lp->value.ban.when);
1102 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1105 /* We are now treating the <key> part of /join <channel list> <key> as a key
1106 * ring; that is, we try one key against the actual channel key, and if that
1107 * doesn't work, we try the next one, and so on. -Kev -Texaco
1108 * Returns: 0 on match, 1 otherwise
1109 * This version contributed by SeKs <intru@info.polymtl.ca>
1111 static int compall(char *key, char *keyring)
1116 p1 = key; /* point to the key... */
1117 while (*p1 && *p1 == *keyring)
1118 { /* step through the key and ring until they
1124 if (!*p1 && (!*keyring || *keyring == ','))
1125 /* ok, if we're at the end of the and also at the end of one of the keys
1126 in the keyring, we have a match */
1129 if (!*keyring) /* if we're at the end of the key ring, there
1130 weren't any matches, so we return 1 */
1133 /* Not at the end of the key ring, so step
1134 through to the next key in the ring: */
1135 while (*keyring && *(keyring++) != ',');
1137 goto top; /* and check it against the key */
1140 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
1143 int overrideJoin = 0;
1146 * Now a banned user CAN join if invited -- Nemesi
1147 * Now a user CAN escape channel limit if invited -- bfriendly
1148 * Now a user CAN escape anything if invited -- Isomer
1151 for (lp = (cli_user(sptr))->invited; lp; lp = lp->next)
1152 if (lp->value.chptr == chptr)
1155 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
1156 a HACK(4) notice will be sent if he would not have been supposed
1157 to join normally. */
1158 if (IsLocalChannel(chptr->chname) && HasPriv(sptr, PRIV_WALK_LCHAN) &&
1159 !BadPtr(key) && compall("OVERRIDE",key) == 0)
1160 overrideJoin = MAGIC_OPER_OVERRIDE;
1162 if (chptr->mode.mode & MODE_INVITEONLY)
1163 return overrideJoin + ERR_INVITEONLYCHAN;
1165 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
1166 return overrideJoin + ERR_CHANNELISFULL;
1168 if ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
1169 return overrideJoin + ERR_NEEDREGGEDNICK;
1171 if (is_banned(sptr, chptr, NULL))
1172 return overrideJoin + ERR_BANNEDFROMCHAN;
1175 * now using compall (above) to test against a whole key ring -Kev
1177 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
1178 return overrideJoin + ERR_BADCHANNELKEY;
1181 return ERR_DONTCHEAT;
1187 * Remove bells and commas from channel name
1189 void clean_channelname(char *cn)
1193 for (i = 0; cn[i]; i++) {
1194 if (i >= CHANNELLEN || !IsChannelChar(cn[i])) {
1198 if (IsChannelLower(cn[i])) {
1199 cn[i] = ToLower(cn[i]);
1205 if ((unsigned char)(cn[i]) == 0xd0)
1206 cn[i] = (char) 0xf0;
1213 * Get Channel block for i (and allocate a new channel
1214 * block, if it didn't exists before).
1216 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1218 struct Channel *chptr;
1221 if (EmptyString(chname))
1224 len = strlen(chname);
1225 if (MyUser(cptr) && len > CHANNELLEN)
1228 *(chname + CHANNELLEN) = '\0';
1230 if ((chptr = FindChannel(chname)))
1232 if (flag == CGT_CREATE)
1234 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1236 ++UserStats.channels;
1237 memset(chptr, 0, sizeof(struct Channel));
1238 strcpy(chptr->chname, chname);
1239 if (GlobalChannelList)
1240 GlobalChannelList->prev = chptr;
1242 chptr->next = GlobalChannelList;
1243 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1244 GlobalChannelList = chptr;
1250 void add_invite(struct Client *cptr, struct Channel *chptr)
1252 struct SLink *inv, **tmp;
1254 del_invite(cptr, chptr);
1256 * Delete last link in chain if the list is max length
1258 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1259 if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER))
1260 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1262 * Add client to channel invite list
1265 inv->value.cptr = cptr;
1266 inv->next = chptr->invites;
1267 chptr->invites = inv;
1269 * Add channel to the end of the client invite list
1271 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1273 inv->value.chptr = chptr;
1276 (cli_user(cptr))->invites++;
1280 * Delete Invite block from channel invite list and client invite list
1282 void del_invite(struct Client *cptr, struct Channel *chptr)
1284 struct SLink **inv, *tmp;
1286 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1287 if (tmp->value.cptr == cptr)
1292 (cli_user(cptr))->invites--;
1296 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1297 if (tmp->value.chptr == chptr)
1306 /* List and skip all channels that are listen */
1307 void list_next_channels(struct Client *cptr, int nr)
1309 struct ListingArgs *args = cli_listing(cptr);
1310 struct Channel *chptr = args->chptr;
1311 chptr->mode.mode &= ~MODE_LISTED;
1312 while (is_listed(chptr) || --nr >= 0)
1314 for (; chptr; chptr = chptr->next)
1316 if (!cli_user(cptr) || (!(HasPriv(cptr, PRIV_LIST_CHAN) && IsAnOper(cptr)) &&
1317 SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
1319 if (chptr->users > args->min_users && chptr->users < args->max_users &&
1320 chptr->creationtime > args->min_time &&
1321 chptr->creationtime < args->max_time &&
1322 (!args->topic_limits || (*chptr->topic &&
1323 chptr->topic_time > args->min_topic_time &&
1324 chptr->topic_time < args->max_topic_time)))
1326 if (ShowChannel(cptr,chptr))
1327 send_reply(cptr, RPL_LIST, chptr->chname, chptr->users,
1329 chptr = chptr->next;
1335 MyFree(cli_listing(cptr));
1336 cli_listing(cptr) = NULL;
1337 send_reply(cptr, RPL_LISTEND);
1343 (cli_listing(cptr))->chptr = chptr;
1344 chptr->mode.mode |= MODE_LISTED;
1357 * X --a--> A --b--> B --d--> D
1361 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1362 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1364 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1365 * Remove the user immediately when no users are left on the channel.
1366 * b) On server B : remove the user (who/lp) from the channel, send a
1367 * PART upstream (to A) and pass on the KICK.
1368 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1369 * channel, and pass on the KICK.
1370 * d) On server D : remove the user (who/lp) from the channel, and pass on
1374 * - Setting the ZOMBIE flag never hurts, we either remove the
1375 * client after that or we don't.
1376 * - The KICK message was already passed on, as should be in all cases.
1377 * - `who' is removed in all cases except case a) when users are left.
1378 * - A PART is only sent upstream in case b).
1384 * 1 --- 2 --- 3 --- 4 --- 5
1388 * We also need to turn 'who' into a zombie on servers 1 and 6,
1389 * because a KICK from 'who' (kicking someone else in that direction)
1390 * can arrive there afterwards - which should not be bounced itself.
1391 * Therefore case a) also applies for servers 1 and 6.
1395 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
1396 struct Client* sptr, struct Channel* chptr)
1398 assert(0 != member);
1403 /* Default for case a): */
1406 /* Case b) or c) ?: */
1407 if (MyUser(who)) /* server 4 */
1409 if (IsServer(cptr)) /* Case b) ? */
1410 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1411 remove_user_from_channel(who, chptr);
1414 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1416 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1417 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1418 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1420 remove_user_from_channel(who, chptr);
1425 /* Case a) (servers 1, 2, 3 and 6) */
1426 if (channel_all_zombies(chptr))
1427 remove_user_from_channel(who, chptr);
1429 /* XXX Can't actually call Debug here; if the channel is all zombies,
1430 * chptr will no longer exist when we get here.
1431 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1435 int number_of_zombies(struct Channel *chptr)
1437 struct Membership* member;
1441 for (member = chptr->members; member; member = member->next_member) {
1442 if (IsZombie(member))
1449 * This helper function builds an argument string in strptr, consisting
1450 * of the original string, a space, and str1 and str2 concatenated (if,
1451 * of course, str2 is not NULL)
1454 build_string(char *strptr, int *strptr_i, char *str1, char *str2, char c)
1457 strptr[(*strptr_i)++] = c;
1460 strptr[(*strptr_i)++] = *(str1++);
1464 strptr[(*strptr_i)++] = *(str2++);
1466 strptr[(*strptr_i)] = '\0';
1470 * This is the workhorse of our ModeBuf suite; this actually generates the
1471 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1474 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1476 /* we only need the flags that don't take args right now */
1477 static int flags[] = {
1478 /* MODE_CHANOP, 'o', */
1479 /* MODE_VOICE, 'v', */
1482 MODE_MODERATED, 'm',
1483 MODE_TOPICLIMIT, 't',
1484 MODE_INVITEONLY, 'i',
1485 MODE_NOPRIVMSGS, 'n',
1487 /* MODE_KEY, 'k', */
1488 /* MODE_BAN, 'b', */
1489 /* MODE_LIMIT, 'l', */
1490 /* MODE_APASS, 'A', */
1491 /* MODE_UPASS, 'u', */
1497 struct Client *app_source; /* where the MODE appears to come from */
1499 char addbuf[20]; /* accumulates +psmtin, etc. */
1501 char rembuf[20]; /* accumulates -psmtin, etc. */
1503 char *bufptr; /* we make use of indirection to simplify the code */
1506 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1508 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1510 char *strptr; /* more indirection to simplify the code */
1513 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1516 char limitbuf[20]; /* convert limits to strings */
1518 unsigned int limitdel = MODE_LIMIT;
1522 /* If the ModeBuf is empty, we have nothing to do */
1523 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1526 /* Ok, if we were given the OPMODE flag, hide the source if its a user */
1527 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
1530 app_source = mbuf->mb_source;
1533 * Account for user we're bouncing; we have to get it in on the first
1534 * bounced MODE, or we could have problems
1536 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1537 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1539 /* Calculate the simple flags */
1540 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1541 if (*flag_p & mbuf->mb_add)
1542 addbuf[addbuf_i++] = flag_p[1];
1543 else if (*flag_p & mbuf->mb_rem)
1544 rembuf[rembuf_i++] = flag_p[1];
1547 /* Now go through the modes with arguments... */
1548 for (i = 0; i < mbuf->mb_count; i++) {
1549 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1551 bufptr_i = &addbuf_i;
1554 bufptr_i = &rembuf_i;
1557 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1558 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1560 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
1561 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1563 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1564 totalbuflen -= IRCD_MAX(5, tmp) + 1;
1566 } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS)) {
1567 tmp = strlen(MB_STRING(mbuf, i));
1569 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1570 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1573 switch(MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS))
1588 bufptr[(*bufptr_i)++] = mode_char;
1589 totalbuflen -= tmp + 1;
1591 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1592 /* if it's a limit, we also format the number */
1593 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
1595 tmp = strlen(limitbuf);
1597 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1598 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1600 bufptr[(*bufptr_i)++] = 'l';
1601 totalbuflen -= tmp + 1;
1606 /* terminate the mode strings */
1607 addbuf[addbuf_i] = '\0';
1608 rembuf[rembuf_i] = '\0';
1610 /* If we're building a user visible MODE or HACK... */
1611 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1612 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1613 MODEBUF_DEST_LOG)) {
1614 /* Set up the parameter strings */
1620 for (i = 0; i < mbuf->mb_count; i++) {
1621 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1624 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1626 strptr_i = &addstr_i;
1629 strptr_i = &remstr_i;
1632 /* deal with clients... */
1633 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1634 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1636 /* deal with strings... */
1637 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1638 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1640 /* deal with invisible passwords */
1641 else if (MB_TYPE(mbuf, i) & (MODE_APASS | MODE_UPASS))
1642 build_string(strptr, strptr_i, "*", 0, ' ');
1645 * deal with limit; note we cannot include the limit parameter if we're
1648 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1649 (MODE_ADD | MODE_LIMIT))
1650 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1653 /* send the messages off to their destination */
1654 if (mbuf->mb_dest & MODEBUF_DEST_HACK2)
1655 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1657 #ifdef HEAD_IN_SAND_SNOTICES
1658 cli_name(mbuf->mb_source),
1660 cli_name(app_source),
1662 mbuf->mb_channel->chname,
1663 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1664 addbuf, remstr, addstr,
1665 mbuf->mb_channel->creationtime);
1667 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1668 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1669 "%s%s%s%s%s%s [%Tu]",
1670 #ifdef HEAD_IN_SAND_SNOTICES
1671 cli_name(mbuf->mb_source),
1673 cli_name(app_source),
1675 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1676 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1677 mbuf->mb_channel->creationtime);
1679 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1680 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1682 #ifdef HEAD_IN_SAND_SNOTICES
1683 cli_name(mbuf->mb_source),
1685 cli_name(app_source),
1687 mbuf->mb_channel->chname,
1688 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1689 addbuf, remstr, addstr,
1690 mbuf->mb_channel->creationtime);
1692 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1693 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1694 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1695 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1696 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1698 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1699 sendcmdto_channel_butserv_butone(app_source, CMD_MODE, mbuf->mb_channel, NULL,
1700 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1701 rembuf_i ? "-" : "", rembuf,
1702 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1705 /* Now are we supposed to propagate to other servers? */
1706 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1707 /* set up parameter string */
1714 * limit is supressed if we're removing it; we have to figure out which
1715 * direction is the direction for it to be removed, though...
1717 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1719 for (i = 0; i < mbuf->mb_count; i++) {
1720 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1723 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1725 strptr_i = &addstr_i;
1728 strptr_i = &remstr_i;
1731 /* deal with modes that take clients */
1732 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1733 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1735 /* deal with modes that take strings */
1736 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS))
1737 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1740 * deal with the limit. Logic here is complicated; if HACK2 is set,
1741 * we're bouncing the mode, so sense is reversed, and we have to
1742 * include the original limit if it looks like it's being removed
1744 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1745 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1748 /* we were told to deop the source */
1749 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1750 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1751 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1752 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1754 /* mark that we've done this, so we don't do it again */
1755 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1758 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1759 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1760 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1761 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1762 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1763 addbuf, remstr, addstr);
1764 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1766 * If HACK2 was set, we're bouncing; we send the MODE back to the
1767 * connection we got it from with the senses reversed and a TS of 0;
1770 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1771 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1772 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1773 mbuf->mb_channel->creationtime);
1776 * We're propagating a normal MODE command to the rest of the network;
1777 * we send the actual channel TS unless this is a HACK3 or a HACK4
1779 if (IsServer(mbuf->mb_source))
1780 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1781 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1782 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1783 addbuf, remstr, addstr,
1784 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1785 mbuf->mb_channel->creationtime);
1787 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1788 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1789 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1790 addbuf, remstr, addstr);
1794 /* We've drained the ModeBuf... */
1799 /* reinitialize the mode-with-arg slots */
1800 for (i = 0; i < MAXMODEPARAMS; i++) {
1801 /* If we saved any, pack them down */
1802 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1803 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1804 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1806 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1808 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1809 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1811 MB_TYPE(mbuf, i) = 0;
1812 MB_UINT(mbuf, i) = 0;
1815 /* If we're supposed to flush it all, do so--all hail tail recursion */
1816 if (all && mbuf->mb_count)
1817 return modebuf_flush_int(mbuf, 1);
1823 * This routine just initializes a ModeBuf structure with the information
1824 * needed and the options given.
1827 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1828 struct Client *connect, struct Channel *chan, unsigned int dest)
1833 assert(0 != source);
1839 mbuf->mb_source = source;
1840 mbuf->mb_connect = connect;
1841 mbuf->mb_channel = chan;
1842 mbuf->mb_dest = dest;
1845 /* clear each mode-with-parameter slot */
1846 for (i = 0; i < MAXMODEPARAMS; i++) {
1847 MB_TYPE(mbuf, i) = 0;
1848 MB_UINT(mbuf, i) = 0;
1853 * This routine simply adds modes to be added or deleted; do a binary OR
1854 * with either MODE_ADD or MODE_DEL
1857 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1860 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1862 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1863 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS | MODE_REGONLY);
1865 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1868 if (mode & MODE_ADD) {
1869 mbuf->mb_rem &= ~mode;
1870 mbuf->mb_add |= mode;
1872 mbuf->mb_add &= ~mode;
1873 mbuf->mb_rem |= mode;
1878 * This routine adds a mode to be added or deleted that takes a unsigned
1879 * int parameter; mode may *only* be the relevant mode flag ORed with one
1880 * of MODE_ADD or MODE_DEL
1883 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1886 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1888 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1889 MB_UINT(mbuf, mbuf->mb_count) = uint;
1891 /* when we've reached the maximal count, flush the buffer */
1892 if (++mbuf->mb_count >=
1893 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1894 modebuf_flush_int(mbuf, 0);
1898 * This routine adds a mode to be added or deleted that takes a string
1899 * parameter; mode may *only* be the relevant mode flag ORed with one of
1900 * MODE_ADD or MODE_DEL
1903 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
1907 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1909 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
1910 MB_STRING(mbuf, mbuf->mb_count) = string;
1912 /* when we've reached the maximal count, flush the buffer */
1913 if (++mbuf->mb_count >=
1914 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1915 modebuf_flush_int(mbuf, 0);
1919 * This routine adds a mode to be added or deleted that takes a client
1920 * parameter; mode may *only* be the relevant mode flag ORed with one of
1921 * MODE_ADD or MODE_DEL
1924 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
1925 struct Client *client)
1928 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1930 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1931 MB_CLIENT(mbuf, mbuf->mb_count) = client;
1933 /* when we've reached the maximal count, flush the buffer */
1934 if (++mbuf->mb_count >=
1935 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1936 modebuf_flush_int(mbuf, 0);
1940 * This is the exported binding for modebuf_flush()
1943 modebuf_flush(struct ModeBuf *mbuf)
1945 return modebuf_flush_int(mbuf, 1);
1949 * This extracts the simple modes contained in mbuf
1952 modebuf_extract(struct ModeBuf *mbuf, char *buf)
1954 static int flags[] = {
1955 /* MODE_CHANOP, 'o', */
1956 /* MODE_VOICE, 'v', */
1959 MODE_MODERATED, 'm',
1960 MODE_TOPICLIMIT, 't',
1961 MODE_INVITEONLY, 'i',
1962 MODE_NOPRIVMSGS, 'n',
1966 /* MODE_BAN, 'b', */
1972 int i, bufpos = 0, len;
1974 char *key = 0, limitbuf[20];
1975 char *apass, *upass;
1984 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
1985 if (MB_TYPE(mbuf, i) & MODE_ADD) {
1986 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT | MODE_APASS | MODE_UPASS);
1988 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
1989 key = MB_STRING(mbuf, i);
1990 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
1991 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
1992 else if (MB_TYPE(mbuf, i) & MODE_UPASS)
1993 upass = MB_STRING(mbuf, i);
1994 else if (MB_TYPE(mbuf, i) & MODE_APASS)
1995 apass = MB_STRING(mbuf, i);
2002 buf[bufpos++] = '+'; /* start building buffer */
2004 for (flag_p = flags; flag_p[0]; flag_p += 2)
2006 buf[bufpos++] = flag_p[1];
2008 for (i = 0, len = bufpos; i < len; i++) {
2010 build_string(buf, &bufpos, key, 0, ' ');
2011 else if (buf[i] == 'l')
2012 build_string(buf, &bufpos, limitbuf, 0, ' ');
2013 else if (buf[i] == 'u')
2014 build_string(buf, &bufpos, upass, 0, ' ');
2015 else if (buf[i] == 'A')
2016 build_string(buf, &bufpos, apass, 0, ' ');
2025 * Simple function to invalidate bans
2028 mode_ban_invalidate(struct Channel *chan)
2030 struct Membership *member;
2032 for (member = chan->members; member; member = member->next_member)
2033 ClearBanValid(member);
2037 * Simple function to drop invite structures
2040 mode_invite_clear(struct Channel *chan)
2042 while (chan->invites)
2043 del_invite(chan->invites->value.cptr, chan);
2046 /* What we've done for mode_parse so far... */
2047 #define DONE_LIMIT 0x01 /* We've set the limit */
2048 #define DONE_KEY 0x02 /* We've set the key */
2049 #define DONE_BANLIST 0x04 /* We've sent the ban list */
2050 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
2051 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
2052 #define DONE_UPASS 0x20 /* We've set user pass */
2053 #define DONE_APASS 0x40 /* We've set admin pass */
2056 struct ModeBuf *mbuf;
2057 struct Client *cptr;
2058 struct Client *sptr;
2059 struct Channel *chptr;
2060 struct Membership *member;
2071 struct SLink banlist[MAXPARA];
2074 struct Client *client;
2075 } cli_change[MAXPARA];
2079 * Here's a helper function to deal with sending along "Not oper" or
2080 * "Not member" messages
2083 send_notoper(struct ParseState *state)
2085 if (state->done & DONE_NOTOPER)
2088 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
2089 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
2091 state->done |= DONE_NOTOPER;
2095 * Helper function to convert limits
2098 mode_parse_limit(struct ParseState *state, int *flag_p)
2100 unsigned int t_limit;
2102 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
2103 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2106 if (state->parc <= 0) { /* warn if not enough args */
2107 if (MyUser(state->sptr))
2108 need_more_params(state->sptr, "MODE +l");
2112 t_limit = strtoul(state->parv[state->args_used++], 0, 10); /* grab arg */
2116 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2117 (!t_limit || t_limit == state->chptr->mode.limit))
2120 t_limit = state->chptr->mode.limit;
2122 /* If they're not an oper, they can't change modes */
2123 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2124 send_notoper(state);
2128 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
2130 state->done |= DONE_LIMIT;
2135 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
2137 if (state->flags & MODE_PARSE_SET) { /* set the limit */
2138 if (state->dir & MODE_ADD) {
2139 state->chptr->mode.mode |= flag_p[0];
2140 state->chptr->mode.limit = t_limit;
2142 state->chptr->mode.mode &= ~flag_p[0];
2143 state->chptr->mode.limit = 0;
2149 * Helper function to convert keys
2152 mode_parse_key(struct ParseState *state, int *flag_p)
2157 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2160 if (state->parc <= 0) { /* warn if not enough args */
2161 if (MyUser(state->sptr))
2162 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2167 t_str = state->parv[state->args_used++]; /* grab arg */
2171 /* If they're not an oper, they can't change modes */
2172 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2173 send_notoper(state);
2177 if (state->done & DONE_KEY) /* allow key to be set only once */
2179 state->done |= DONE_KEY;
2183 /* clean up the key string */
2185 while (*++s > ' ' && *s != ':' && --t_len)
2189 if (!*t_str) { /* warn if empty */
2190 if (MyUser(state->sptr))
2191 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2199 /* can't add a key if one is set, nor can one remove the wrong key */
2200 if (!(state->flags & MODE_PARSE_FORCE))
2201 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2202 (state->dir == MODE_DEL &&
2203 ircd_strcmp(state->chptr->mode.key, t_str))) {
2204 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2208 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2209 !ircd_strcmp(state->chptr->mode.key, t_str))
2210 return; /* no key change */
2212 if (state->flags & MODE_PARSE_BOUNCE) {
2213 if (*state->chptr->mode.key) /* reset old key */
2214 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2215 state->chptr->mode.key, 0);
2216 else /* remove new bogus key */
2217 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2218 } else /* send new key */
2219 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2221 if (state->flags & MODE_PARSE_SET) {
2222 if (state->dir == MODE_ADD) /* set the new key */
2223 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2224 else /* remove the old key */
2225 *state->chptr->mode.key = '\0';
2230 * Helper function to convert user passes
2233 mode_parse_upass(struct ParseState *state, int *flag_p)
2238 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2241 if (state->parc <= 0) { /* warn if not enough args */
2242 if (MyUser(state->sptr))
2243 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +u" :
2248 t_str = state->parv[state->args_used++]; /* grab arg */
2252 /* If they're not an oper, they can't change modes */
2253 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2254 send_notoper(state);
2258 /* If they are not the channel manager, they are not allowed to change it */
2259 if (MyUser(state->sptr) && !IsChannelManager(state->member)) {
2260 if (*state->chptr->mode.apass) {
2261 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2262 "Use /JOIN", state->chptr->chname, "<AdminPass>.");
2264 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2265 "Re-create the channel. The channel must be *empty* for",
2266 TStime() - state->chptr->creationtime >= 171000 ? "48 contiguous hours" : "a minute or two",
2267 "before it can be recreated.");
2272 if (state->done & DONE_UPASS) /* allow upass to be set only once */
2274 state->done |= DONE_UPASS;
2276 t_len = PASSLEN + 1;
2278 /* clean up the upass string */
2280 while (*++s > ' ' && *s != ':' && --t_len)
2284 if (!*t_str) { /* warn if empty */
2285 if (MyUser(state->sptr))
2286 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +u" :
2294 if (!(state->flags & MODE_PARSE_FORCE))
2295 /* can't add the upass while apass is not set */
2296 if (state->dir == MODE_ADD && !*state->chptr->mode.apass) {
2297 send_reply(state->sptr, ERR_UPASSNOTSET, state->chptr->chname, state->chptr->chname);
2300 /* can't add a upass if one is set, nor can one remove the wrong upass */
2301 if ((state->dir == MODE_ADD && *state->chptr->mode.upass) ||
2302 (state->dir == MODE_DEL &&
2303 ircd_strcmp(state->chptr->mode.upass, t_str))) {
2304 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2308 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2309 !ircd_strcmp(state->chptr->mode.upass, t_str))
2310 return; /* no upass change */
2312 if (state->flags & MODE_PARSE_BOUNCE) {
2313 if (*state->chptr->mode.upass) /* reset old upass */
2314 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2315 state->chptr->mode.upass, 0);
2316 else /* remove new bogus upass */
2317 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2318 } else /* send new upass */
2319 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2321 if (state->flags & MODE_PARSE_SET) {
2322 if (state->dir == MODE_ADD) /* set the new upass */
2323 ircd_strncpy(state->chptr->mode.upass, t_str, PASSLEN);
2324 else /* remove the old upass */
2325 *state->chptr->mode.upass = '\0';
2330 * Helper function to convert admin passes
2333 mode_parse_apass(struct ParseState *state, int *flag_p)
2338 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2341 if (state->parc <= 0) { /* warn if not enough args */
2342 if (MyUser(state->sptr))
2343 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2348 t_str = state->parv[state->args_used++]; /* grab arg */
2352 /* If they're not an oper, they can't change modes */
2353 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2354 send_notoper(state);
2358 /* Don't allow to change the Apass if the channel is older than 48 hours. */
2359 if (TStime() - state->chptr->creationtime >= 172800 && !IsAnOper(state->sptr)) {
2360 send_reply(state->sptr, ERR_CHANSECURED, state->chptr->chname);
2364 /* If they are not the channel manager, they are not allowed to change it */
2365 if (MyUser(state->sptr) && !IsChannelManager(state->member)) {
2366 if (*state->chptr->mode.apass) {
2367 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2368 "Use /JOIN", state->chptr->chname, "<AdminPass>.");
2370 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2371 "Re-create the channel. The channel must be *empty* for",
2372 "at least a whole minute", "before it can be recreated.");
2377 if (state->done & DONE_APASS) /* allow apass to be set only once */
2379 state->done |= DONE_APASS;
2381 t_len = PASSLEN + 1;
2383 /* clean up the apass string */
2385 while (*++s > ' ' && *s != ':' && --t_len)
2389 if (!*t_str) { /* warn if empty */
2390 if (MyUser(state->sptr))
2391 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2399 if (!(state->flags & MODE_PARSE_FORCE)) {
2400 /* can't remove the apass while upass is still set */
2401 if (state->dir == MODE_DEL && *state->chptr->mode.upass) {
2402 send_reply(state->sptr, ERR_UPASSSET, state->chptr->chname, state->chptr->chname);
2405 /* can't add an apass if one is set, nor can one remove the wrong apass */
2406 if ((state->dir == MODE_ADD && *state->chptr->mode.apass) ||
2407 (state->dir == MODE_DEL && ircd_strcmp(state->chptr->mode.apass, t_str))) {
2408 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2413 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2414 !ircd_strcmp(state->chptr->mode.apass, t_str))
2415 return; /* no apass change */
2417 if (state->flags & MODE_PARSE_BOUNCE) {
2418 if (*state->chptr->mode.apass) /* reset old apass */
2419 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2420 state->chptr->mode.apass, 0);
2421 else /* remove new bogus apass */
2422 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2423 } else /* send new apass */
2424 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2426 if (state->flags & MODE_PARSE_SET) {
2427 if (state->dir == MODE_ADD) { /* set the new apass */
2428 /* Make it VERY clear to the user that this is a one-time password */
2429 ircd_strncpy(state->chptr->mode.apass, t_str, PASSLEN);
2430 if (MyUser(state->sptr)) {
2431 send_reply(state->sptr, RPL_APASSWARN,
2432 "Channel Admin password (+A) set to '", state->chptr->mode.apass, "'. ",
2433 "Are you SURE you want to use this as Admin password? ",
2434 "You will NOT be able to change this password anymore once the channel is more than 48 hours old!");
2435 send_reply(state->sptr, RPL_APASSWARN,
2436 "Use \"/MODE ", state->chptr->chname, " -A ", state->chptr->mode.apass,
2437 "\" to remove the password and then immediately set a new one. "
2438 "IMPORTANT: YOU CANNOT RECOVER THIS PASSWORD, EVER; "
2439 "WRITE THE PASSWORD DOWN (don't store this rescue password on disk)! "
2440 "Now set the channel user password (+u).");
2442 } else { /* remove the old apass */
2443 *state->chptr->mode.apass = '\0';
2444 if (MyUser(state->sptr))
2445 send_reply(state->sptr, RPL_APASSWARN,
2446 "WARNING: You removed the channel Admin password MODE (+A). ",
2447 "If you would disconnect or leave the channel without setting a new password then you will ",
2448 "not be able to set it again and lose ownership of this channel! ",
2449 "SET A NEW PASSWORD NOW!", "");
2455 * Helper function to convert bans
2458 mode_parse_ban(struct ParseState *state, int *flag_p)
2461 struct SLink *ban, *newban = 0;
2463 if (state->parc <= 0) { /* Not enough args, send ban list */
2464 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2465 send_ban_list(state->sptr, state->chptr);
2466 state->done |= DONE_BANLIST;
2472 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2475 t_str = state->parv[state->args_used++]; /* grab arg */
2479 /* If they're not an oper, they can't change modes */
2480 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2481 send_notoper(state);
2485 if ((s = strchr(t_str, ' ')))
2488 if (!*t_str || *t_str == ':') { /* warn if empty */
2489 if (MyUser(state->sptr))
2490 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2495 t_str = collapse(pretty_mask(t_str));
2497 /* remember the ban for the moment... */
2498 if (state->dir == MODE_ADD) {
2499 newban = state->banlist + (state->numbans++);
2502 DupString(newban->value.ban.banstr, t_str);
2503 newban->value.ban.who = cli_name(state->sptr);
2504 newban->value.ban.when = TStime();
2506 newban->flags = CHFL_BAN | MODE_ADD;
2508 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
2509 newban->flags |= CHFL_BAN_IPMASK;
2512 if (!state->chptr->banlist) {
2513 state->chptr->banlist = newban; /* add our ban with its flags */
2514 state->done |= DONE_BANCLEAN;
2518 /* Go through all bans */
2519 for (ban = state->chptr->banlist; ban; ban = ban->next) {
2520 /* first, clean the ban flags up a bit */
2521 if (!(state->done & DONE_BANCLEAN))
2522 /* Note: We're overloading *lots* of bits here; be careful! */
2523 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
2527 * MODE_ADD - Ban was added; if we're bouncing modes,
2528 * then we'll remove it below; otherwise,
2529 * we'll have to allocate a real ban
2531 * MODE_DEL - Ban was marked for deletion; if we're
2532 * bouncing modes, we'll have to re-add it,
2533 * otherwise, we'll have to remove it
2535 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
2536 * with a ban already set; if we're
2537 * bouncing modes, we'll have to bounce
2538 * this one; otherwise, we'll just ignore
2539 * it when we process added bans
2542 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
2543 ban->flags |= MODE_DEL; /* delete one ban */
2545 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2547 } else if (state->dir == MODE_ADD) {
2548 /* if the ban already exists, don't worry about it */
2549 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
2550 newban->flags &= ~MODE_ADD; /* don't add ban at all */
2551 MyFree(newban->value.ban.banstr); /* stopper a leak */
2552 state->numbans--; /* deallocate last ban */
2553 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2555 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
2556 if (!(ban->flags & MODE_DEL))
2557 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
2558 } else if (!mmatch(t_str, ban->value.ban.banstr))
2559 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
2561 if (!ban->next && (newban->flags & MODE_ADD)) {
2562 ban->next = newban; /* add our ban with its flags */
2563 break; /* get out of loop */
2567 state->done |= DONE_BANCLEAN;
2571 * This is the bottom half of the ban processor
2574 mode_process_bans(struct ParseState *state)
2576 struct SLink *ban, *newban, *prevban, *nextban;
2582 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2584 banlen = strlen(ban->value.ban.banstr);
2586 nextban = ban->next;
2588 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
2590 prevban->next = 0; /* Break the list; ban isn't a real ban */
2592 state->chptr->banlist = 0;
2597 MyFree(ban->value.ban.banstr);
2600 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
2601 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2602 ban->value.ban.banstr,
2603 state->flags & MODE_PARSE_SET);
2605 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2606 if (prevban) /* clip it out of the list... */
2607 prevban->next = ban->next;
2609 state->chptr->banlist = ban->next;
2614 MyFree(ban->value.ban.who);
2618 continue; /* next ban; keep prevban like it is */
2620 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
2621 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
2623 prevban->next = 0; /* Break the list; ban isn't a real ban */
2625 state->chptr->banlist = 0;
2627 /* If we're supposed to ignore it, do so. */
2628 if (ban->flags & CHFL_BAN_OVERLAPPED &&
2629 !(state->flags & MODE_PARSE_BOUNCE)) {
2633 MyFree(ban->value.ban.banstr);
2635 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2636 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2637 count >= feature_int(FEAT_MAXBANS))) {
2638 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2639 ban->value.ban.banstr);
2643 MyFree(ban->value.ban.banstr);
2645 /* add the ban to the buffer */
2646 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2647 ban->value.ban.banstr,
2648 !(state->flags & MODE_PARSE_SET));
2650 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2651 newban = make_link();
2652 newban->value.ban.banstr = ban->value.ban.banstr;
2653 DupString(newban->value.ban.who, ban->value.ban.who);
2654 newban->value.ban.when = ban->value.ban.when;
2655 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
2657 newban->next = state->chptr->banlist; /* and link it in */
2658 state->chptr->banlist = newban;
2667 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2669 if (changed) /* if we changed the ban list, we must invalidate the bans */
2670 mode_ban_invalidate(state->chptr);
2674 * Helper function to process client changes
2677 mode_parse_client(struct ParseState *state, int *flag_p)
2680 struct Client *acptr;
2683 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2686 if (state->parc <= 0) /* return if not enough args */
2689 t_str = state->parv[state->args_used++]; /* grab arg */
2693 /* If they're not an oper, they can't change modes */
2694 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2695 send_notoper(state);
2699 if (MyUser(state->sptr)) /* find client we're manipulating */
2700 acptr = find_chasing(state->sptr, t_str, NULL);
2702 acptr = findNUser(t_str);
2705 return; /* find_chasing() already reported an error to the user */
2707 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2708 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2709 state->cli_change[i].flag & flag_p[0]))
2710 break; /* found a slot */
2712 /* Store what we're doing to them */
2713 state->cli_change[i].flag = state->dir | flag_p[0];
2714 state->cli_change[i].client = acptr;
2718 * Helper function to process the changed client list
2721 mode_process_clients(struct ParseState *state)
2724 struct Membership *member;
2726 for (i = 0; state->cli_change[i].flag; i++) {
2727 assert(0 != state->cli_change[i].client);
2729 /* look up member link */
2730 if (!(member = find_member_link(state->chptr,
2731 state->cli_change[i].client)) ||
2732 (MyUser(state->sptr) && IsZombie(member))) {
2733 if (MyUser(state->sptr))
2734 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2735 cli_name(state->cli_change[i].client),
2736 state->chptr->chname);
2740 if ((state->cli_change[i].flag & MODE_ADD &&
2741 (state->cli_change[i].flag & member->status)) ||
2742 (state->cli_change[i].flag & MODE_DEL &&
2743 !(state->cli_change[i].flag & member->status)))
2744 continue; /* no change made, don't do anything */
2746 /* see if the deop is allowed */
2747 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2748 (MODE_DEL | MODE_CHANOP)) {
2749 /* prevent +k users from being deopped */
2750 if (IsChannelService(state->cli_change[i].client)) {
2751 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
2752 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
2754 (IsServer(state->sptr) ? cli_name(state->sptr) :
2755 cli_name((cli_user(state->sptr))->server)));
2757 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
2758 send_reply(state->sptr, ERR_ISCHANSERVICE,
2759 cli_name(state->cli_change[i].client),
2760 state->chptr->chname);
2765 /* check deop for local user */
2766 if (MyUser(state->sptr)) {
2768 /* don't allow local opers to be deopped on local channels */
2769 if (state->cli_change[i].client != state->sptr &&
2770 IsLocalChannel(state->chptr->chname) &&
2771 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
2772 send_reply(state->sptr, ERR_ISOPERLCHAN,
2773 cli_name(state->cli_change[i].client),
2774 state->chptr->chname);
2778 /* don't allow to deop members with an op level that is <= our own level */
2779 if (state->sptr != state->cli_change[i].client /* but allow to deop oneself */
2781 && OpLevel(member) <= OpLevel(state->member)) {
2782 int equal = (OpLevel(member) == OpLevel(state->member));
2783 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
2784 cli_name(state->cli_change[i].client),
2785 state->chptr->chname,
2786 OpLevel(state->member), OpLevel(member),
2787 "deop", equal ? "the same" : "a higher");
2793 /* set op-level of member being opped */
2794 if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
2795 (MODE_ADD | MODE_CHANOP)) {
2796 /* If on a channel with upass set, someone with level x gives ops to someone else,
2797 then that person gets level x-1. On other channels, where upass is not set,
2798 the level stays the same. */
2799 int level_increment = *state->chptr->mode.upass ? 1 : 0;
2800 /* Someone being opped by a server gets op-level 0 */
2801 int old_level = (state->member == NULL) ? -level_increment : OpLevel(state->member);
2802 SetOpLevel(member, old_level == MAXOPLEVEL ? MAXOPLEVEL : (old_level + level_increment));
2805 /* accumulate the change */
2806 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
2807 state->cli_change[i].client);
2809 /* actually effect the change */
2810 if (state->flags & MODE_PARSE_SET) {
2811 if (state->cli_change[i].flag & MODE_ADD) {
2812 member->status |= (state->cli_change[i].flag &
2813 (MODE_CHANOP | MODE_VOICE));
2814 if (state->cli_change[i].flag & MODE_CHANOP)
2815 ClearDeopped(member);
2817 member->status &= ~(state->cli_change[i].flag &
2818 (MODE_CHANOP | MODE_VOICE));
2820 } /* for (i = 0; state->cli_change[i].flags; i++) */
2824 * Helper function to process the simple modes
2827 mode_parse_mode(struct ParseState *state, int *flag_p)
2829 /* If they're not an oper, they can't change modes */
2830 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2831 send_notoper(state);
2838 if (state->dir == MODE_ADD) {
2839 state->add |= flag_p[0];
2840 state->del &= ~flag_p[0];
2842 if (flag_p[0] & MODE_SECRET) {
2843 state->add &= ~MODE_PRIVATE;
2844 state->del |= MODE_PRIVATE;
2845 } else if (flag_p[0] & MODE_PRIVATE) {
2846 state->add &= ~MODE_SECRET;
2847 state->del |= MODE_SECRET;
2850 state->add &= ~flag_p[0];
2851 state->del |= flag_p[0];
2854 assert(0 == (state->add & state->del));
2855 assert((MODE_SECRET | MODE_PRIVATE) !=
2856 (state->add & (MODE_SECRET | MODE_PRIVATE)));
2860 * This routine is intended to parse MODE or OPMODE commands and effect the
2861 * changes (or just build the bounce buffer). We pass the starting offset
2865 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
2866 struct Channel *chptr, int parc, char *parv[], unsigned int flags,
2867 struct Membership* member)
2869 static int chan_flags[] = {
2874 MODE_MODERATED, 'm',
2875 MODE_TOPICLIMIT, 't',
2876 MODE_INVITEONLY, 'i',
2877 MODE_NOPRIVMSGS, 'n',
2890 unsigned int t_mode;
2892 struct ParseState state;
2903 state.chptr = chptr;
2904 state.member = member;
2907 state.flags = flags;
2908 state.dir = MODE_ADD;
2912 state.args_used = 0;
2913 state.max_args = MAXMODEPARAMS;
2916 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
2917 state.banlist[i].next = 0;
2918 state.banlist[i].value.ban.banstr = 0;
2919 state.banlist[i].value.ban.who = 0;
2920 state.banlist[i].value.ban.when = 0;
2921 state.banlist[i].flags = 0;
2922 state.cli_change[i].flag = 0;
2923 state.cli_change[i].client = 0;
2926 modestr = state.parv[state.args_used++];
2930 for (; *modestr; modestr++) {
2931 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
2932 if (flag_p[1] == *modestr)
2935 if (!flag_p[0]) { /* didn't find it? complain and continue */
2936 if (MyUser(state.sptr))
2937 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
2942 case '+': /* switch direction to MODE_ADD */
2943 case '-': /* switch direction to MODE_DEL */
2944 state.dir = flag_p[0];
2947 case 'l': /* deal with limits */
2948 mode_parse_limit(&state, flag_p);
2951 case 'k': /* deal with keys */
2952 mode_parse_key(&state, flag_p);
2955 case 'A': /* deal with Admin passes */
2956 mode_parse_apass(&state, flag_p);
2959 case 'u': /* deal with user passes */
2960 mode_parse_upass(&state, flag_p);
2963 case 'b': /* deal with bans */
2964 mode_parse_ban(&state, flag_p);
2967 case 'o': /* deal with ops/voice */
2969 mode_parse_client(&state, flag_p);
2972 default: /* deal with other modes */
2973 mode_parse_mode(&state, flag_p);
2975 } /* switch (*modestr) */
2976 } /* for (; *modestr; modestr++) */
2978 if (state.flags & MODE_PARSE_BURST)
2979 break; /* don't interpret any more arguments */
2981 if (state.parc > 0) { /* process next argument in string */
2982 modestr = state.parv[state.args_used++];
2986 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
2989 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
2990 break; /* we're then going to bounce the mode! */
2992 recv_ts = atoi(modestr);
2994 if (recv_ts && recv_ts < state.chptr->creationtime)
2995 state.chptr->creationtime = recv_ts; /* respect earlier TS */
2997 break; /* break out of while loop */
2998 } else if (state.flags & MODE_PARSE_STRICT ||
2999 (MyUser(state.sptr) && state.max_args <= 0)) {
3000 state.parc++; /* we didn't actually gobble the argument */
3002 break; /* break out of while loop */
3005 } /* while (*modestr) */
3008 * the rest of the function finishes building resultant MODEs; if the
3009 * origin isn't a member or an oper, skip it.
3011 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3012 return state.args_used; /* tell our parent how many args we gobbled */
3014 t_mode = state.chptr->mode.mode;
3016 if (state.del & t_mode) { /* delete any modes to be deleted... */
3017 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
3019 t_mode &= ~state.del;
3021 if (state.add & ~t_mode) { /* add any modes to be added... */
3022 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
3024 t_mode |= state.add;
3027 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
3028 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
3029 !(t_mode & MODE_INVITEONLY))
3030 mode_invite_clear(state.chptr);
3032 state.chptr->mode.mode = t_mode;
3035 if (state.flags & MODE_PARSE_WIPEOUT) {
3036 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
3037 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
3038 state.chptr->mode.limit);
3039 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
3040 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
3041 state.chptr->mode.key, 0);
3042 if (*state.chptr->mode.upass && !(state.done & DONE_UPASS))
3043 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_UPASS,
3044 state.chptr->mode.upass, 0);
3045 if (*state.chptr->mode.apass && !(state.done & DONE_APASS))
3046 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_APASS,
3047 state.chptr->mode.apass, 0);
3050 if (state.done & DONE_BANCLEAN) /* process bans */
3051 mode_process_bans(&state);
3053 /* process client changes */
3054 if (state.cli_change[0].flag)
3055 mode_process_clients(&state);
3057 return state.args_used; /* tell our parent how many args we gobbled */
3061 * Initialize a join buffer
3064 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
3065 struct Client *connect, unsigned int type, char *comment,
3071 assert(0 != source);
3072 assert(0 != connect);
3074 jbuf->jb_source = source; /* just initialize struct JoinBuf */
3075 jbuf->jb_connect = connect;
3076 jbuf->jb_type = type;
3077 jbuf->jb_comment = comment;
3078 jbuf->jb_create = create;
3080 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
3081 type == JOINBUF_TYPE_PART ||
3082 type == JOINBUF_TYPE_PARTALL) ?
3083 STARTJOINLEN : STARTCREATELEN) +
3084 (comment ? strlen(comment) + 2 : 0));
3086 for (i = 0; i < MAXJOINARGS; i++)
3087 jbuf->jb_channels[i] = 0;
3091 * Add a channel to the join buffer
3094 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
3101 if (jbuf->jb_type == JOINBUF_TYPE_JOIN)
3102 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
3107 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
3108 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
3109 /* Send notification to channel */
3110 if (!(flags & CHFL_ZOMBIE))
3111 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL,
3112 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3113 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3114 else if (MyUser(jbuf->jb_source))
3115 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
3116 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3117 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3118 /* XXX: Shouldn't we send a PART here anyway? */
3119 /* to users on the channel? Why? From their POV, the user isn't on
3120 * the channel anymore anyway. We don't send to servers until below,
3121 * when we gang all the channel parts together. Note that this is
3122 * exactly the same logic, albeit somewhat more concise, as was in
3123 * the original m_part.c */
3125 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3126 IsLocalChannel(chan->chname)) /* got to remove user here */
3127 remove_user_from_channel(jbuf->jb_source, chan);
3129 /* Add user to channel */
3130 add_user_to_channel(chan, jbuf->jb_source, flags, 0);
3132 /* send notification to all servers */
3133 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !IsLocalChannel(chan->chname))
3134 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
3135 "%H %Tu", chan, chan->creationtime);
3137 /* Send the notification to the channel */
3138 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, ":%H", chan);
3140 /* send an op, too, if needed */
3141 if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE &&
3142 !IsModelessChannel(chan->chname))
3143 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_MODE, chan, NULL, "%H +o %C",
3144 chan, jbuf->jb_source);
3147 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || IsLocalChannel(chan->chname))
3148 return; /* don't send to remote */
3150 /* figure out if channel name will cause buffer to be overflowed */
3151 len = chan ? strlen(chan->chname) + 1 : 2;
3152 if (jbuf->jb_strlen + len > BUFSIZE)
3153 joinbuf_flush(jbuf);
3155 /* add channel to list of channels to send and update counts */
3156 jbuf->jb_channels[jbuf->jb_count++] = chan;
3157 jbuf->jb_strlen += len;
3159 /* if we've used up all slots, flush */
3160 if (jbuf->jb_count >= MAXJOINARGS)
3161 joinbuf_flush(jbuf);
3165 * Flush the channel list to remote servers
3168 joinbuf_flush(struct JoinBuf *jbuf)
3170 char chanlist[BUFSIZE];
3174 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3175 jbuf->jb_type == JOINBUF_TYPE_JOIN)
3176 return 0; /* no joins to process */
3178 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
3179 build_string(chanlist, &chanlist_i,
3180 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
3181 i == 0 ? '\0' : ',');
3182 if (JOINBUF_TYPE_PART == jbuf->jb_type)
3183 /* Remove user from channel */
3184 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
3186 jbuf->jb_channels[i] = 0; /* mark slot empty */
3189 jbuf->jb_count = 0; /* reset base counters */
3190 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
3191 STARTJOINLEN : STARTCREATELEN) +
3192 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
3194 /* and send the appropriate command */
3195 switch (jbuf->jb_type) {
3196 case JOINBUF_TYPE_CREATE:
3197 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
3198 "%s %Tu", chanlist, jbuf->jb_create);
3201 case JOINBUF_TYPE_PART:
3202 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
3203 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,