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_reply.h"
35 #include "ircd_snprintf.h"
36 #include "ircd_string.h"
43 #include "querycmds.h"
60 struct Channel* GlobalChannelList = 0;
62 static unsigned int membershipAllocCount;
63 static struct Membership* membershipFreeList;
65 void del_invite(struct Client *, struct Channel *);
67 const char* const PartFmt1 = ":%s " MSG_PART " %s";
68 const char* const PartFmt2 = ":%s " MSG_PART " %s :%s";
69 const char* const PartFmt1serv = "%s%s " TOK_PART " %s";
70 const char* const PartFmt2serv = "%s%s " TOK_PART " %s :%s";
73 static struct SLink* next_ban;
74 static struct SLink* prev_ban;
75 static struct SLink* removed_bans_list;
78 * Use a global variable to remember if an oper set a mode on a local channel. Ugly,
79 * but the only way to do it without changing set_mode intensively.
81 int LocalChanOperMode = 0;
85 * return the length (>=0) of a chain of links.
87 static int list_length(struct SLink *lp)
91 for (; lp; lp = lp->next)
97 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
103 /* Servers don't have member links */
104 if (IsServer(cptr)||IsMe(cptr))
107 /* +k users are typically on a LOT of channels. So we iterate over who
108 * is in the channel. X/W are +k and are in about 5800 channels each.
109 * however there are typically no more than 1000 people in a channel
112 if (IsChannelService(cptr)) {
115 assert(m->channel == chptr);
121 /* Users on the other hand aren't allowed on more than 15 channels. 50%
122 * of users that are on channels are on 2 or less, 95% are on 7 or less,
123 * and 99% are on 10 or less.
126 m = (cli_user(cptr))->channel;
128 assert(m->user == cptr);
129 if (m->channel == chptr)
138 * find_chasing - Find the client structure for a nick name (user)
139 * using history mechanism if necessary. If the client is not found, an error
140 * message (NO SUCH NICK) is generated. If the client was found
141 * through the history, chasing will be 1 and otherwise 0.
143 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
145 struct Client* who = FindClient(user);
152 if (!(who = get_history(user, feature_int(FEAT_KILLCHASETIMELIMIT)))) {
153 send_reply(sptr, ERR_NOSUCHNICK, user);
162 * Create a string of form "foo!bar@fubar" given foo, bar and fubar
163 * as the parameters. If NULL, they become "*".
165 #define NUH_BUFSIZE (NICKLEN + USERLEN + HOSTLEN + 3)
166 static char *make_nick_user_host(char *namebuf, const char *nick,
167 const char *name, const char *host)
169 ircd_snprintf(0, namebuf, NUH_BUFSIZE, "%s!%s@%s", nick, name, host);
174 * Create a string of form "foo!bar@123.456.789.123" given foo, bar and the
175 * IP-number as the parameters. If NULL, they become "*".
177 #define NUI_BUFSIZE (NICKLEN + USERLEN + 16 + 3)
178 static char *make_nick_user_ip(char *ipbuf, char *nick, char *name,
181 ircd_snprintf(0, ipbuf, NUI_BUFSIZE, "%s!%s@%s", nick, name,
182 ircd_ntoa((const char*) &ip));
187 * Subtract one user from channel i (and free channel
188 * block, if channel became empty).
189 * Returns: true (1) if channel still has members.
190 * false (0) if the channel is now empty.
192 int sub1_from_channel(struct Channel* chptr)
194 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
196 assert(0 != chptr->members);
204 * Also channels without Apass set need to be kept alive,
205 * otherwise Bad Guys(tm) would be able to takeover
206 * existing channels too easily, and then set an Apass!
207 * However, if a channel without Apass becomes empty
208 * then we try to be kind to them and remove possible
211 chptr->mode.mode &= ~MODE_INVITEONLY;
212 chptr->mode.limit = 0;
214 * We do NOT reset a possible key or bans because when
215 * the 'channel owners' can't get in because of a key
216 * or ban then apparently there was a fight/takeover
217 * on the channel and we want them to contact IRC opers
218 * who then will educate them on the use of Apass/upass.
221 if (feature_bool(FEAT_OPLEVELS)) {
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 */
227 destruct_channel(chptr);
232 int destruct_channel(struct Channel* chptr)
237 assert(0 == chptr->members);
239 /* Channel became (or was) empty: Remove channel */
240 if (is_listed(chptr))
243 for (i = 0; i <= HighestFd; i++)
245 struct Client *acptr = 0;
246 if ((acptr = LocalClientArray[i]) && cli_listing(acptr) &&
247 (cli_listing(acptr))->chptr == chptr)
249 list_next_channels(acptr, 1);
250 break; /* Only one client can list a channel */
255 * Now, find all invite links from channel structure
257 while ((tmp = chptr->invites))
258 del_invite(tmp->value.cptr, chptr);
260 tmp = chptr->banlist;
265 MyFree(obtmp->value.ban.banstr);
266 MyFree(obtmp->value.ban.who);
270 chptr->prev->next = chptr->next;
272 GlobalChannelList = chptr->next;
274 chptr->next->prev = chptr->prev;
276 --UserStats.channels;
278 * make sure that channel actually got removed from hash table
280 assert(chptr->hnext == chptr);
288 * `cptr' must be the client adding the ban.
290 * If `change' is true then add `banid' to channel `chptr'.
291 * Returns 0 if the ban was added.
292 * Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
293 * Return -1 otherwise.
295 * Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
296 * when `change' is false, otherwise they will be removed from the banlist.
297 * Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
298 * respectively will return these bans until NULL is returned.
300 * If `firsttime' is true, the ban list as returned by next_overlapped_ban()
301 * is reset (unless a non-zero value is returned, in which case the
302 * CHFL_BAN_OVERLAPPED flag might not have been reset!).
306 int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
307 int change, int firsttime)
312 int removed_bans = 0;
313 int len = strlen(banid);
318 assert(0 == prev_ban);
319 assert(0 == removed_bans_list);
323 for (banp = &chptr->banlist; *banp;)
325 len += strlen((*banp)->value.ban.banstr);
327 if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
329 if (!strcmp((*banp)->value.ban.banstr, banid))
331 (*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
335 else if (!mmatch((*banp)->value.ban.banstr, banid))
337 if (!mmatch(banid, (*banp)->value.ban.banstr))
339 struct SLink *tmp = *banp;
345 len -= strlen(tmp->value.ban.banstr);
348 /* These will be sent to the user later as -b */
349 tmp->next = removed_bans_list;
350 removed_bans_list = tmp;
353 else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
355 tmp->flags |= CHFL_BAN_OVERLAPPED;
366 (*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
367 banp = &(*banp)->next;
370 if (MyUser(cptr) && !removed_bans &&
371 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
372 (cnt >= feature_int(FEAT_MAXBANS))))
374 send_reply(cptr, ERR_BANLISTFULL, chptr->chname, banid);
380 struct Membership* member;
382 ban->next = chptr->banlist;
384 ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
385 assert(0 != ban->value.ban.banstr);
386 strcpy(ban->value.ban.banstr, banid);
388 if (IsServer(cptr) && feature_bool(FEAT_HIS_BANWHO))
389 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 tmphost[HOSTLEN + 1];
446 char nu_host[NUH_BUFSIZE];
447 char nu_realhost[NUH_BUFSIZE];
448 char nu_ip[NUI_BUFSIZE];
456 if (member && IsBanValid(member))
457 return IsBanned(member);
459 s = make_nick_user_host(nu_host, cli_name(cptr), (cli_user(cptr))->username,
460 (cli_user(cptr))->host);
463 if (HasHiddenHost(cptr))
465 sr = make_nick_user_host(nu_realhost, cli_name(cptr),
466 (cli_user(cptr))->username,
467 cli_user(cptr)->realhost);
471 ircd_snprintf(0, tmphost, HOSTLEN, "%s.%s",
472 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
473 sr = make_nick_user_host(nu_realhost, cli_name(cptr),
474 cli_user(cptr)->username,
479 for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
480 if ((tmp->flags & CHFL_BAN_IPMASK)) {
482 ip_s = make_nick_user_ip(nu_ip, cli_name(cptr),
483 (cli_user(cptr))->username, cli_ip(cptr));
484 if (match(tmp->value.ban.banstr, ip_s) == 0)
487 else if (match(tmp->value.ban.banstr, s) == 0)
489 else if (sr && match(tmp->value.ban.banstr, sr) == 0)
505 return (tmp != NULL);
509 * adds a user to a channel by adding another link to the channels member
512 void add_user_to_channel(struct Channel* chptr, struct Client* who,
513 unsigned int flags, int oplevel)
520 struct Membership* member = membershipFreeList;
522 membershipFreeList = member->next_member;
524 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
525 ++membershipAllocCount;
530 member->channel = chptr;
531 member->status = flags;
532 member->oplevel = oplevel;
534 member->next_member = chptr->members;
535 if (member->next_member)
536 member->next_member->prev_member = member;
537 member->prev_member = 0;
538 chptr->members = member;
540 member->next_channel = (cli_user(who))->channel;
541 if (member->next_channel)
542 member->next_channel->prev_channel = member;
543 member->prev_channel = 0;
544 (cli_user(who))->channel = member;
546 if (chptr->destruct_event)
547 remove_destruct_event(chptr);
549 ++((cli_user(who))->joined);
553 static int remove_member_from_channel(struct Membership* member)
555 struct Channel* chptr;
557 chptr = member->channel;
559 * unlink channel member list
561 if (member->next_member)
562 member->next_member->prev_member = member->prev_member;
563 if (member->prev_member)
564 member->prev_member->next_member = member->next_member;
566 member->channel->members = member->next_member;
569 * If this is the last delayed-join user, may have to clear WASDELJOINS.
571 if (IsDelayedJoin(member))
572 CheckDelayedJoins(chptr);
575 * unlink client channel list
577 if (member->next_channel)
578 member->next_channel->prev_channel = member->prev_channel;
579 if (member->prev_channel)
580 member->prev_channel->next_channel = member->next_channel;
582 (cli_user(member->user))->channel = member->next_channel;
584 --(cli_user(member->user))->joined;
586 member->next_member = membershipFreeList;
587 membershipFreeList = member;
589 return sub1_from_channel(chptr);
592 static int channel_all_zombies(struct Channel* chptr)
594 struct Membership* member;
596 for (member = chptr->members; member; member = member->next_member) {
597 if (!IsZombie(member))
604 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
607 struct Membership* member;
610 if ((member = find_member_link(chptr, cptr))) {
611 if (remove_member_from_channel(member)) {
612 if (channel_all_zombies(chptr)) {
614 * XXX - this looks dangerous but isn't if we got the referential
615 * integrity right for channels
617 while (remove_member_from_channel(chptr->members))
624 void remove_user_from_all_channels(struct Client* cptr)
626 struct Membership* chan;
628 assert(0 != cli_user(cptr));
630 while ((chan = (cli_user(cptr))->channel))
631 remove_user_from_channel(cptr, chan->channel);
634 int is_chan_op(struct Client *cptr, struct Channel *chptr)
636 struct Membership* member;
638 if ((member = find_member_link(chptr, cptr)))
639 return (!IsZombie(member) && IsChanOp(member));
644 int is_zombie(struct Client *cptr, struct Channel *chptr)
646 struct Membership* member;
650 if ((member = find_member_link(chptr, cptr)))
651 return IsZombie(member);
655 int has_voice(struct Client* cptr, struct Channel* chptr)
657 struct Membership* member;
660 if ((member = find_member_link(chptr, cptr)))
661 return (!IsZombie(member) && HasVoice(member));
666 int member_can_send_to_channel(struct Membership* member, int reveal)
670 /* Discourage using the Apass to get op. They should use the upass. */
671 if (IsChannelManager(member) && *member->channel->mode.upass)
674 if (IsVoicedOrOpped(member))
677 * If it's moderated, and you aren't a priviledged user, you can't
680 if (member->channel->mode.mode & MODE_MODERATED)
683 * If you're banned then you can't speak either.
684 * but because of the amount of CPU time that is_banned chews
685 * we only check it for our clients.
687 if (MyUser(member->user) && is_banned(member->user, member->channel, member))
690 if (IsDelayedJoin(member) && reveal)
691 RevealDelayedJoin(member);
696 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr, int reveal)
698 struct Membership *member;
701 * Servers can always speak on channels.
706 member = find_channel_member(cptr, chptr);
709 * You can't speak if you're off channel, and it is +n (no external messages)
713 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED)) ||
714 ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(cptr)))
717 return !is_banned(cptr, chptr, NULL);
719 return member_can_send_to_channel(member, reveal);
723 * find_no_nickchange_channel
724 * if a member and not (opped or voiced) and (banned or moderated)
725 * return the name of the first channel banned on
727 const char* find_no_nickchange_channel(struct Client* cptr)
730 struct Membership* member;
731 for (member = (cli_user(cptr))->channel; member;
732 member = member->next_channel) {
733 if (!IsVoicedOrOpped(member) &&
734 (is_banned(cptr, member->channel, member) ||
735 (member->channel->mode.mode & MODE_MODERATED)))
736 return member->channel->chname;
744 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
745 * with the parameters in pbuf.
747 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen,
748 struct Channel *chptr, struct Membership *member)
750 int previous_parameter = 0;
757 if (chptr->mode.mode & MODE_SECRET)
759 else if (chptr->mode.mode & MODE_PRIVATE)
761 if (chptr->mode.mode & MODE_MODERATED)
763 if (chptr->mode.mode & MODE_TOPICLIMIT)
765 if (chptr->mode.mode & MODE_INVITEONLY)
767 if (chptr->mode.mode & MODE_NOPRIVMSGS)
769 if (chptr->mode.mode & MODE_REGONLY)
771 if (chptr->mode.mode & MODE_DELJOINS)
773 else if (MyUser(cptr) && (chptr->mode.mode & MODE_WASDELJOINS))
775 if (chptr->mode.limit) {
777 ircd_snprintf(0, pbuf, buflen, "%u", chptr->mode.limit);
778 previous_parameter = 1;
781 if (*chptr->mode.key) {
783 if (previous_parameter)
785 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
786 strcat(pbuf, chptr->mode.key);
789 previous_parameter = 1;
791 if (*chptr->mode.apass) {
793 if (previous_parameter)
795 if (IsServer(cptr)) {
796 strcat(pbuf, chptr->mode.apass);
799 previous_parameter = 1;
801 if (*chptr->mode.upass) {
803 if (previous_parameter)
805 if (IsServer(cptr) || (member && IsChanOp(member) && OpLevel(member) == 0)) {
806 strcat(pbuf, chptr->mode.upass);
813 int compare_member_oplevel(const void *mp1, const void *mp2)
815 struct Membership const* member1 = *(struct Membership const**)mp1;
816 struct Membership const* member2 = *(struct Membership const**)mp2;
817 if (member1->oplevel == member2->oplevel)
819 return (member1->oplevel < member2->oplevel) ? -1 : 1;
823 * send "cptr" a full list of the modes for channel chptr.
825 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
827 /* The order in which modes are generated is now mandatory */
828 static unsigned int current_flags[4] =
829 { 0, CHFL_VOICE, CHFL_CHANOP, CHFL_CHANOP | CHFL_VOICE };
835 struct Membership* member;
837 char modebuf[MODEBUFLEN];
838 char parabuf[MODEBUFLEN];
840 int number_of_ops = 0;
841 int opped_members_index = 0;
842 struct Membership** opped_members = NULL;
843 int last_oplevel = 0;
848 if (IsLocalChannel(chptr->chname))
851 member = chptr->members;
852 lp2 = chptr->banlist;
854 *modebuf = *parabuf = '\0';
855 channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr, 0);
857 for (first = 1; full; first = 0) /* Loop for multiple messages */
859 full = 0; /* Assume by default we get it
860 all in one message */
862 /* (Continued) prefix: "<Y> B <channel> <TS>" */
863 /* is there any better way we can do this? */
864 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
865 chptr->creationtime);
867 if (first && modebuf[1]) /* Add simple modes (Aiklmnpstu)
870 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
871 msgq_append(&me, mb, " %s", modebuf);
874 msgq_append(&me, mb, " %s", parabuf);
878 * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
880 * First find all opless members.
881 * Run 2 times over all members, to group the members with
882 * and without voice together.
883 * Then run 2 times over all opped members (which are ordered
884 * by op-level) to also group voice and non-voice together.
886 for (first = 1; flag_cnt < 4; new_mode = 1, ++flag_cnt)
890 if (flag_cnt < 2 && IsChanOp(member))
893 * The first loop (to find all non-voice/op), we count the ops.
894 * The second loop (to find all voiced non-ops), store the ops
895 * in a dynamic array.
900 opped_members[opped_members_index++] = member;
902 /* Only handle the members with the flags that we are interested in. */
903 if ((member->status & CHFL_VOICED_OR_OPPED) == current_flags[flag_cnt])
905 if (msgq_bufleft(mb) < NUMNICKLEN + 3 + MAXOPLEVELDIGITS)
906 /* The 3 + MAXOPLEVELDIGITS is a possible ",:v999". */
908 full = 1; /* Make sure we continue after
910 /* Ensure the new BURST line contains the current
911 * ":mode", except when there is no mode yet. */
912 new_mode = (flag_cnt > 0) ? 1 : 0;
913 break; /* Do not add this member to this message */
915 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
916 first = 0; /* From now on, use commas to add new nicks */
919 * Do we have a nick with a new mode ?
920 * Or are we starting a new BURST line?
925 * This means we are at the _first_ member that has only
926 * voice, or the first member that has only ops, or the
927 * first member that has voice and ops (so we get here
928 * at most three times, plus once for every start of
929 * a continued BURST line where only these modes is current.
930 * In the two cases where the current mode includes ops,
931 * we need to add the _absolute_ value of the oplevel to the mode.
933 char tbuf[3 + MAXOPLEVELDIGITS] = ":";
936 if (HasVoice(member)) /* flag_cnt == 1 or 3 */
938 if (IsChanOp(member)) /* flag_cnt == 2 or 3 */
940 /* append the absolute value of the oplevel */
941 loc += ircd_snprintf(0, tbuf + loc, sizeof(tbuf) - loc, "%u", member->oplevel);
942 last_oplevel = member->oplevel;
945 msgq_append(&me, mb, tbuf);
948 else if (flag_cnt > 1 && last_oplevel != member->oplevel)
951 * This can't be the first member of a (continued) BURST
952 * message because then either flag_cnt == 0 or new_mode == 1
953 * Now we need to append the incremental value of the oplevel.
955 char tbuf[2 + MAXOPLEVELDIGITS];
956 ircd_snprintf(0, tbuf, sizeof(tbuf), ":%u", member->oplevel - last_oplevel);
957 last_oplevel = member->oplevel;
958 msgq_append(&me, mb, tbuf);
961 /* Go to the next `member'. */
963 member = member->next_member;
965 member = opped_members[++opped_members_index];
970 /* Point `member' at the start of the list again. */
973 member = chptr->members;
974 /* Now, after one loop, we know the number of ops and can
975 * allocate the dynamic array with pointer to the ops. */
976 opped_members = (struct Membership**)
977 MyMalloc((number_of_ops + 1) * sizeof(struct Membership*));
978 opped_members[number_of_ops] = NULL; /* Needed for loop termination */
982 /* At the end of the second loop, sort the opped members with
983 * increasing op-level, so that we will output them in the
984 * correct order (and all op-level increments stay positive) */
986 qsort(opped_members, number_of_ops,
987 sizeof(struct Membership*), compare_member_oplevel);
988 /* The third and fourth loop run only over the opped members. */
989 member = opped_members[(opped_members_index = 0)];
992 } /* loop over 0,+v,+o,+ov */
996 /* Attach all bans, space seperated " :%ban ban ..." */
997 for (first = 2; lp2; lp2 = lp2->next)
999 len = strlen(lp2->value.ban.banstr);
1000 if (msgq_bufleft(mb) < len + 1 + first)
1001 /* The +1 stands for the added ' '.
1002 * The +first stands for the added ":%".
1008 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
1009 lp2->value.ban.banstr);
1014 send_buffer(cptr, mb, 0); /* Send this message */
1016 } /* Continue when there was something
1017 that didn't fit (full==1) */
1019 MyFree(opped_members);
1020 if (feature_bool(FEAT_TOPIC_BURST) && (chptr->topic[0] != '\0'))
1021 sendcmdto_one(&me, CMD_TOPIC, cptr, "%H %Tu %Tu :%s", chptr,
1022 chptr->creationtime, chptr->topic_time, chptr->topic);
1028 * by Carlo Wood (Run), 05 Oct 1998.
1032 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
1033 * When the user name or host name are too long (USERLEN and HOSTLEN
1034 * respectively) then they are cut off at the start with a '*'.
1036 * The following transformations are made:
1038 * 1) xxx -> nick!*@*
1039 * 2) xxx.xxx -> *!*@host
1040 * 3) xxx!yyy -> nick!user@*
1041 * 4) xxx@yyy -> *!user@host
1042 * 5) xxx!yyy@zzz -> nick!user@host
1044 char *pretty_mask(char *mask)
1046 static char star[2] = { '*', 0 };
1047 static char retmask[NUH_BUFSIZE];
1048 char *last_dot = NULL;
1051 /* Case 1: default */
1056 /* Do a _single_ pass through the characters of the mask: */
1057 for (ptr = mask; *ptr; ++ptr)
1061 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1065 else if (*ptr == '@')
1067 /* Case 4: Found last '@' (without finding a '!' yet) */
1072 else if (*ptr == '.')
1074 /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
1084 /* Case 4 or 5: Found last '@' */
1090 if (user == star && last_dot)
1100 char *nick_end = (user != star) ? user - 1 : ptr;
1101 if (nick_end - nick > NICKLEN)
1107 char *user_end = (host != star) ? host - 1 : ptr;
1108 if (user_end - user > USERLEN)
1110 user = user_end - USERLEN;
1115 if (host != star && ptr - host > HOSTLEN)
1117 host = ptr - HOSTLEN;
1120 return make_nick_user_host(retmask, nick, user, host);
1123 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1130 for (lp = chptr->banlist; lp; lp = lp->next)
1131 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->value.ban.banstr,
1132 lp->value.ban.who, lp->value.ban.when);
1134 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1137 /* We are now treating the <key> part of /join <channel list> <key> as a key
1138 * ring; that is, we try one key against the actual channel key, and if that
1139 * doesn't work, we try the next one, and so on. -Kev -Texaco
1140 * Returns: 0 on match, 1 otherwise
1141 * This version contributed by SeKs <intru@info.polymtl.ca>
1143 static int compall(char *key, char *keyring)
1148 p1 = key; /* point to the key... */
1149 while (*p1 && *p1 == *keyring)
1150 { /* step through the key and ring until they
1156 if (!*p1 && (!*keyring || *keyring == ','))
1157 /* ok, if we're at the end of the and also at the end of one of the keys
1158 in the keyring, we have a match */
1161 if (!*keyring) /* if we're at the end of the key ring, there
1162 weren't any matches, so we return 1 */
1165 /* Not at the end of the key ring, so step
1166 through to the next key in the ring: */
1167 while (*keyring && *(keyring++) != ',');
1169 goto top; /* and check it against the key */
1172 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
1174 int overrideJoin = 0;
1177 * Now a banned user CAN join if invited -- Nemesi
1178 * Now a user CAN escape channel limit if invited -- bfriendly
1179 * Now a user CAN escape anything if invited -- Isomer
1182 if (IsInvited(sptr, chptr))
1185 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
1186 a HACK(4) notice will be sent if he would not have been supposed
1187 to join normally. */
1188 if (IsLocalChannel(chptr->chname) && HasPriv(sptr, PRIV_WALK_LCHAN) &&
1189 !BadPtr(key) && compall("OVERRIDE",chptr->mode.key) != 0 &&
1190 compall("OVERRIDE",key) == 0)
1191 overrideJoin = MAGIC_OPER_OVERRIDE;
1193 if (chptr->mode.mode & MODE_INVITEONLY)
1194 return overrideJoin + ERR_INVITEONLYCHAN;
1196 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
1197 return overrideJoin + ERR_CHANNELISFULL;
1199 if ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
1200 return overrideJoin + ERR_NEEDREGGEDNICK;
1202 if (is_banned(sptr, chptr, NULL))
1203 return overrideJoin + ERR_BANNEDFROMCHAN;
1206 * now using compall (above) to test against a whole key ring -Kev
1208 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
1209 return overrideJoin + ERR_BADCHANNELKEY;
1212 return ERR_DONTCHEAT;
1218 * Remove bells and commas from channel name
1220 void clean_channelname(char *cn)
1224 for (i = 0; cn[i]; i++) {
1225 if (i >= CHANNELLEN || !IsChannelChar(cn[i])) {
1229 if (IsChannelLower(cn[i])) {
1230 cn[i] = ToLower(cn[i]);
1236 if ((unsigned char)(cn[i]) == 0xd0)
1237 cn[i] = (char) 0xf0;
1244 * Get Channel block for i (and allocate a new channel
1245 * block, if it didn't exists before).
1247 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1249 struct Channel *chptr;
1252 if (EmptyString(chname))
1255 len = strlen(chname);
1256 if (MyUser(cptr) && len > CHANNELLEN)
1259 *(chname + CHANNELLEN) = '\0';
1261 if ((chptr = FindChannel(chname)))
1263 if (flag == CGT_CREATE)
1265 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1267 ++UserStats.channels;
1268 memset(chptr, 0, sizeof(struct Channel));
1269 strcpy(chptr->chname, chname);
1270 if (GlobalChannelList)
1271 GlobalChannelList->prev = chptr;
1273 chptr->next = GlobalChannelList;
1274 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1275 GlobalChannelList = chptr;
1281 void add_invite(struct Client *cptr, struct Channel *chptr)
1283 struct SLink *inv, **tmp;
1285 del_invite(cptr, chptr);
1287 * Delete last link in chain if the list is max length
1289 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1290 if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER))
1291 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1293 * Add client to channel invite list
1296 inv->value.cptr = cptr;
1297 inv->next = chptr->invites;
1298 chptr->invites = inv;
1300 * Add channel to the end of the client invite list
1302 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1304 inv->value.chptr = chptr;
1307 (cli_user(cptr))->invites++;
1311 * Delete Invite block from channel invite list and client invite list
1313 void del_invite(struct Client *cptr, struct Channel *chptr)
1315 struct SLink **inv, *tmp;
1317 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1318 if (tmp->value.cptr == cptr)
1323 (cli_user(cptr))->invites--;
1327 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1328 if (tmp->value.chptr == chptr)
1337 /* List and skip all channels that are listen */
1338 void list_next_channels(struct Client *cptr, int nr)
1340 struct ListingArgs *args = cli_listing(cptr);
1341 struct Channel *chptr = args->chptr;
1342 chptr->mode.mode &= ~MODE_LISTED;
1343 while (is_listed(chptr) || --nr >= 0)
1345 for (; chptr; chptr = chptr->next)
1347 if (!cli_user(cptr))
1349 if (!(HasPriv(cptr, PRIV_LIST_CHAN) && IsAnOper(cptr)) &&
1350 SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
1352 if (chptr->users > args->min_users && chptr->users < args->max_users &&
1353 chptr->creationtime > args->min_time &&
1354 chptr->creationtime < args->max_time &&
1355 (!(args->flags & LISTARG_TOPICLIMITS) || (*chptr->topic &&
1356 chptr->topic_time > args->min_topic_time &&
1357 chptr->topic_time < args->max_topic_time)))
1359 if ((args->flags & LISTARG_SHOWSECRET) || ShowChannel(cptr,chptr))
1360 send_reply(cptr, RPL_LIST, chptr->chname, chptr->users,
1362 chptr = chptr->next;
1368 MyFree(cli_listing(cptr));
1369 cli_listing(cptr) = NULL;
1370 send_reply(cptr, RPL_LISTEND);
1376 (cli_listing(cptr))->chptr = chptr;
1377 chptr->mode.mode |= MODE_LISTED;
1390 * X --a--> A --b--> B --d--> D
1394 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1395 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1397 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1398 * Remove the user immediately when no users are left on the channel.
1399 * b) On server B : remove the user (who/lp) from the channel, send a
1400 * PART upstream (to A) and pass on the KICK.
1401 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1402 * channel, and pass on the KICK.
1403 * d) On server D : remove the user (who/lp) from the channel, and pass on
1407 * - Setting the ZOMBIE flag never hurts, we either remove the
1408 * client after that or we don't.
1409 * - The KICK message was already passed on, as should be in all cases.
1410 * - `who' is removed in all cases except case a) when users are left.
1411 * - A PART is only sent upstream in case b).
1417 * 1 --- 2 --- 3 --- 4 --- 5
1421 * We also need to turn 'who' into a zombie on servers 1 and 6,
1422 * because a KICK from 'who' (kicking someone else in that direction)
1423 * can arrive there afterwards - which should not be bounced itself.
1424 * Therefore case a) also applies for servers 1 and 6.
1428 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
1429 struct Client* sptr, struct Channel* chptr)
1431 assert(0 != member);
1436 /* Default for case a): */
1439 /* Case b) or c) ?: */
1440 if (MyUser(who)) /* server 4 */
1442 if (IsServer(cptr)) /* Case b) ? */
1443 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1444 remove_user_from_channel(who, chptr);
1447 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1449 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1450 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1451 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1453 remove_user_from_channel(who, chptr);
1458 /* Case a) (servers 1, 2, 3 and 6) */
1459 if (channel_all_zombies(chptr))
1460 remove_user_from_channel(who, chptr);
1462 /* XXX Can't actually call Debug here; if the channel is all zombies,
1463 * chptr will no longer exist when we get here.
1464 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1468 int number_of_zombies(struct Channel *chptr)
1470 struct Membership* member;
1474 for (member = chptr->members; member; member = member->next_member) {
1475 if (IsZombie(member))
1482 * This helper function builds an argument string in strptr, consisting
1483 * of the original string, a space, and str1 and str2 concatenated (if,
1484 * of course, str2 is not NULL)
1487 build_string(char *strptr, int *strptr_i, const char *str1,
1488 const char *str2, char c)
1491 strptr[(*strptr_i)++] = c;
1494 strptr[(*strptr_i)++] = *(str1++);
1498 strptr[(*strptr_i)++] = *(str2++);
1500 strptr[(*strptr_i)] = '\0';
1504 * This is the workhorse of our ModeBuf suite; this actually generates the
1505 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1508 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1510 /* we only need the flags that don't take args right now */
1511 static int flags[] = {
1512 /* MODE_CHANOP, 'o', */
1513 /* MODE_VOICE, 'v', */
1516 MODE_MODERATED, 'm',
1517 MODE_TOPICLIMIT, 't',
1518 MODE_INVITEONLY, 'i',
1519 MODE_NOPRIVMSGS, 'n',
1522 MODE_WASDELJOINS, 'd',
1523 /* MODE_KEY, 'k', */
1524 /* MODE_BAN, 'b', */
1525 /* MODE_LIMIT, 'l', */
1526 /* MODE_APASS, 'A', */
1527 /* MODE_UPASS, 'u', */
1533 struct Client *app_source; /* where the MODE appears to come from */
1535 char addbuf[20]; /* accumulates +psmtin, etc. */
1537 char rembuf[20]; /* accumulates -psmtin, etc. */
1539 char *bufptr; /* we make use of indirection to simplify the code */
1542 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1544 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1546 char *strptr; /* more indirection to simplify the code */
1549 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1552 char limitbuf[20]; /* convert limits to strings */
1554 unsigned int limitdel = MODE_LIMIT;
1558 /* If the ModeBuf is empty, we have nothing to do */
1559 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1562 /* Ok, if we were given the OPMODE flag, or its a server, hide the source.
1564 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE || IsServer(mbuf->mb_source))
1567 app_source = mbuf->mb_source;
1570 * Account for user we're bouncing; we have to get it in on the first
1571 * bounced MODE, or we could have problems
1573 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1574 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1576 /* Calculate the simple flags */
1577 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1578 if (*flag_p & mbuf->mb_add)
1579 addbuf[addbuf_i++] = flag_p[1];
1580 else if (*flag_p & mbuf->mb_rem)
1581 rembuf[rembuf_i++] = flag_p[1];
1584 /* Now go through the modes with arguments... */
1585 for (i = 0; i < mbuf->mb_count; i++) {
1586 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1588 bufptr_i = &addbuf_i;
1591 bufptr_i = &rembuf_i;
1594 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1595 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1597 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
1598 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1600 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1601 totalbuflen -= IRCD_MAX(5, tmp) + 1;
1603 } else if (MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS)) {
1604 tmp = strlen(MB_STRING(mbuf, i));
1606 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1607 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1610 switch(MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS))
1622 bufptr[(*bufptr_i)++] = mode_char;
1623 totalbuflen -= tmp + 1;
1625 } else if (MB_TYPE(mbuf, i) & MODE_KEY) {
1626 tmp = (mbuf->mb_dest & MODEBUF_DEST_NOKEY ? 1 :
1627 strlen(MB_STRING(mbuf, i)));
1629 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1630 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1632 bufptr[(*bufptr_i)++] = 'k';
1633 totalbuflen -= tmp + 1;
1635 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1636 /* if it's a limit, we also format the number */
1637 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
1639 tmp = strlen(limitbuf);
1641 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1642 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1644 bufptr[(*bufptr_i)++] = 'l';
1645 totalbuflen -= tmp + 1;
1650 /* terminate the mode strings */
1651 addbuf[addbuf_i] = '\0';
1652 rembuf[rembuf_i] = '\0';
1654 /* If we're building a user visible MODE or HACK... */
1655 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1656 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1657 MODEBUF_DEST_LOG)) {
1658 /* Set up the parameter strings */
1664 for (i = 0; i < mbuf->mb_count; i++) {
1665 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1668 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1670 strptr_i = &addstr_i;
1673 strptr_i = &remstr_i;
1676 /* deal with clients... */
1677 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1678 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1680 /* deal with bans... */
1681 else if (MB_TYPE(mbuf, i) & MODE_BAN)
1682 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1684 /* deal with keys... */
1685 else if (MB_TYPE(mbuf, i) & MODE_KEY)
1686 build_string(strptr, strptr_i, mbuf->mb_dest & MODEBUF_DEST_NOKEY ?
1687 "*" : MB_STRING(mbuf, i), 0, ' ');
1689 /* deal with invisible passwords */
1690 else if (MB_TYPE(mbuf, i) & (MODE_APASS | MODE_UPASS))
1691 build_string(strptr, strptr_i, "*", 0, ' ');
1694 * deal with limit; note we cannot include the limit parameter if we're
1697 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1698 (MODE_ADD | MODE_LIMIT))
1699 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1702 /* send the messages off to their destination */
1703 if (mbuf->mb_dest & MODEBUF_DEST_HACK2)
1704 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1706 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1707 mbuf->mb_source : app_source),
1708 mbuf->mb_channel->chname,
1709 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1710 addbuf, remstr, addstr,
1711 mbuf->mb_channel->creationtime);
1713 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1714 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1715 "%s%s%s%s%s%s [%Tu]",
1716 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1717 mbuf->mb_source : app_source),
1718 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1719 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1720 mbuf->mb_channel->creationtime);
1722 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1723 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1725 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1726 mbuf->mb_source : app_source),
1727 mbuf->mb_channel->chname,
1728 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1729 addbuf, remstr, addstr,
1730 mbuf->mb_channel->creationtime);
1732 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1733 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1734 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1735 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1736 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1738 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1739 sendcmdto_channel_butserv_butone(app_source, CMD_MODE, mbuf->mb_channel, NULL,
1740 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1741 rembuf_i ? "-" : "", rembuf,
1742 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1745 /* Now are we supposed to propagate to other servers? */
1746 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1747 /* set up parameter string */
1754 * limit is supressed if we're removing it; we have to figure out which
1755 * direction is the direction for it to be removed, though...
1757 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1759 for (i = 0; i < mbuf->mb_count; i++) {
1760 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1763 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1765 strptr_i = &addstr_i;
1768 strptr_i = &remstr_i;
1771 /* deal with modes that take clients */
1772 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1773 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1775 /* deal with modes that take strings */
1776 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS))
1777 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1780 * deal with the limit. Logic here is complicated; if HACK2 is set,
1781 * we're bouncing the mode, so sense is reversed, and we have to
1782 * include the original limit if it looks like it's being removed
1784 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1785 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1788 /* we were told to deop the source */
1789 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1790 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1791 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1792 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1794 /* mark that we've done this, so we don't do it again */
1795 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1798 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1799 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1800 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1801 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1802 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1803 addbuf, remstr, addstr);
1804 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1806 * If HACK2 was set, we're bouncing; we send the MODE back to the
1807 * connection we got it from with the senses reversed and a TS of 0;
1810 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1811 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1812 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1813 mbuf->mb_channel->creationtime);
1816 * We're propagating a normal MODE command to the rest of the network;
1817 * we send the actual channel TS unless this is a HACK3 or a HACK4
1819 if (IsServer(mbuf->mb_source))
1820 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1821 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1822 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1823 addbuf, remstr, addstr,
1824 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1825 mbuf->mb_channel->creationtime);
1827 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1828 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1829 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1830 addbuf, remstr, addstr);
1834 /* We've drained the ModeBuf... */
1839 /* reinitialize the mode-with-arg slots */
1840 for (i = 0; i < MAXMODEPARAMS; i++) {
1841 /* If we saved any, pack them down */
1842 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1843 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1844 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1846 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1848 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1849 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1851 MB_TYPE(mbuf, i) = 0;
1852 MB_UINT(mbuf, i) = 0;
1855 /* If we're supposed to flush it all, do so--all hail tail recursion */
1856 if (all && mbuf->mb_count)
1857 return modebuf_flush_int(mbuf, 1);
1863 * This routine just initializes a ModeBuf structure with the information
1864 * needed and the options given.
1867 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1868 struct Client *connect, struct Channel *chan, unsigned int dest)
1873 assert(0 != source);
1877 if (IsLocalChannel(chan->chname)) dest &= ~MODEBUF_DEST_SERVER;
1881 mbuf->mb_source = source;
1882 mbuf->mb_connect = connect;
1883 mbuf->mb_channel = chan;
1884 mbuf->mb_dest = dest;
1887 /* clear each mode-with-parameter slot */
1888 for (i = 0; i < MAXMODEPARAMS; i++) {
1889 MB_TYPE(mbuf, i) = 0;
1890 MB_UINT(mbuf, i) = 0;
1895 * This routine simply adds modes to be added or deleted; do a binary OR
1896 * with either MODE_ADD or MODE_DEL
1899 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1902 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1904 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1905 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS | MODE_REGONLY |
1906 MODE_DELJOINS | MODE_WASDELJOINS);
1908 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1911 if (mode & MODE_ADD) {
1912 mbuf->mb_rem &= ~mode;
1913 mbuf->mb_add |= mode;
1915 mbuf->mb_add &= ~mode;
1916 mbuf->mb_rem |= mode;
1921 * This routine adds a mode to be added or deleted that takes a unsigned
1922 * int parameter; mode may *only* be the relevant mode flag ORed with one
1923 * of MODE_ADD or MODE_DEL
1926 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1929 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1931 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1932 MB_UINT(mbuf, mbuf->mb_count) = uint;
1934 /* when we've reached the maximal count, flush the buffer */
1935 if (++mbuf->mb_count >=
1936 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1937 modebuf_flush_int(mbuf, 0);
1941 * This routine adds a mode to be added or deleted that takes a string
1942 * parameter; mode may *only* be the relevant mode flag ORed with one of
1943 * MODE_ADD or MODE_DEL
1946 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
1950 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1952 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
1953 MB_STRING(mbuf, mbuf->mb_count) = string;
1955 /* when we've reached the maximal count, flush the buffer */
1956 if (++mbuf->mb_count >=
1957 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1958 modebuf_flush_int(mbuf, 0);
1962 * This routine adds a mode to be added or deleted that takes a client
1963 * parameter; mode may *only* be the relevant mode flag ORed with one of
1964 * MODE_ADD or MODE_DEL
1967 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
1968 struct Client *client)
1971 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1973 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1974 MB_CLIENT(mbuf, mbuf->mb_count) = client;
1976 /* when we've reached the maximal count, flush the buffer */
1977 if (++mbuf->mb_count >=
1978 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1979 modebuf_flush_int(mbuf, 0);
1983 * This is the exported binding for modebuf_flush()
1986 modebuf_flush(struct ModeBuf *mbuf)
1988 struct Membership *memb;
1990 /* Check if MODE_WASDELJOINS should be set */
1991 if (!(mbuf->mb_channel->mode.mode & (MODE_DELJOINS | MODE_WASDELJOINS))
1992 && (mbuf->mb_rem & MODE_DELJOINS)) {
1993 for (memb = mbuf->mb_channel->members; memb; memb = memb->next_member) {
1994 if (IsDelayedJoin(memb)) {
1995 mbuf->mb_channel->mode.mode |= MODE_WASDELJOINS;
1996 mbuf->mb_add |= MODE_WASDELJOINS;
1997 mbuf->mb_rem &= ~MODE_WASDELJOINS;
2003 return modebuf_flush_int(mbuf, 1);
2007 * This extracts the simple modes contained in mbuf
2010 modebuf_extract(struct ModeBuf *mbuf, char *buf)
2012 static int flags[] = {
2013 /* MODE_CHANOP, 'o', */
2014 /* MODE_VOICE, 'v', */
2017 MODE_MODERATED, 'm',
2018 MODE_TOPICLIMIT, 't',
2019 MODE_INVITEONLY, 'i',
2020 MODE_NOPRIVMSGS, 'n',
2024 /* MODE_BAN, 'b', */
2031 int i, bufpos = 0, len;
2033 char *key = 0, limitbuf[20];
2034 char *apass = 0, *upass = 0;
2043 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
2044 if (MB_TYPE(mbuf, i) & MODE_ADD) {
2045 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT | MODE_APASS | MODE_UPASS);
2047 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
2048 key = MB_STRING(mbuf, i);
2049 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
2050 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
2051 else if (MB_TYPE(mbuf, i) & MODE_UPASS)
2052 upass = MB_STRING(mbuf, i);
2053 else if (MB_TYPE(mbuf, i) & MODE_APASS)
2054 apass = MB_STRING(mbuf, i);
2061 buf[bufpos++] = '+'; /* start building buffer */
2063 for (flag_p = flags; flag_p[0]; flag_p += 2)
2065 buf[bufpos++] = flag_p[1];
2067 for (i = 0, len = bufpos; i < len; i++) {
2069 build_string(buf, &bufpos, key, 0, ' ');
2070 else if (buf[i] == 'l')
2071 build_string(buf, &bufpos, limitbuf, 0, ' ');
2072 else if (buf[i] == 'u')
2073 build_string(buf, &bufpos, upass, 0, ' ');
2074 else if (buf[i] == 'A')
2075 build_string(buf, &bufpos, apass, 0, ' ');
2084 * Simple function to invalidate bans
2087 mode_ban_invalidate(struct Channel *chan)
2089 struct Membership *member;
2091 for (member = chan->members; member; member = member->next_member)
2092 ClearBanValid(member);
2096 * Simple function to drop invite structures
2099 mode_invite_clear(struct Channel *chan)
2101 while (chan->invites)
2102 del_invite(chan->invites->value.cptr, chan);
2105 /* What we've done for mode_parse so far... */
2106 #define DONE_LIMIT 0x01 /* We've set the limit */
2107 #define DONE_KEY 0x02 /* We've set the key */
2108 #define DONE_BANLIST 0x04 /* We've sent the ban list */
2109 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
2110 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
2111 #define DONE_UPASS 0x20 /* We've set user pass */
2112 #define DONE_APASS 0x40 /* We've set admin pass */
2115 struct ModeBuf *mbuf;
2116 struct Client *cptr;
2117 struct Client *sptr;
2118 struct Channel *chptr;
2119 struct Membership *member;
2130 struct SLink banlist[MAXPARA];
2133 struct Client *client;
2134 } cli_change[MAXPARA];
2138 * Here's a helper function to deal with sending along "Not oper" or
2139 * "Not member" messages
2142 send_notoper(struct ParseState *state)
2144 if (state->done & DONE_NOTOPER)
2147 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
2148 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
2150 state->done |= DONE_NOTOPER;
2154 * Helper function to convert limits
2157 mode_parse_limit(struct ParseState *state, int *flag_p)
2159 unsigned int t_limit;
2161 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
2162 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2165 if (state->parc <= 0) { /* warn if not enough args */
2166 if (MyUser(state->sptr))
2167 need_more_params(state->sptr, "MODE +l");
2171 t_limit = strtoul(state->parv[state->args_used++], 0, 10); /* grab arg */
2175 if ((int)t_limit<0) /* don't permit a negative limit */
2178 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2179 (!t_limit || t_limit == state->chptr->mode.limit))
2182 t_limit = state->chptr->mode.limit;
2184 /* If they're not an oper, they can't change modes */
2185 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2186 send_notoper(state);
2190 /* Can't remove a limit that's not there */
2191 if (state->dir == MODE_DEL && !state->chptr->mode.limit)
2194 /* Skip if this is a burst and a lower limit than this is set already */
2195 if ((state->flags & MODE_PARSE_BURST) &&
2196 (state->chptr->mode.mode & flag_p[0]) &&
2197 (state->chptr->mode.limit < t_limit))
2200 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
2202 state->done |= DONE_LIMIT;
2207 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
2209 if (state->flags & MODE_PARSE_SET) { /* set the limit */
2210 if (state->dir & MODE_ADD) {
2211 state->chptr->mode.mode |= flag_p[0];
2212 state->chptr->mode.limit = t_limit;
2214 state->chptr->mode.mode &= ~flag_p[0];
2215 state->chptr->mode.limit = 0;
2221 * Helper function to convert keys
2224 mode_parse_key(struct ParseState *state, int *flag_p)
2229 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2232 if (state->parc <= 0) { /* warn if not enough args */
2233 if (MyUser(state->sptr))
2234 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2239 t_str = state->parv[state->args_used++]; /* grab arg */
2243 /* If they're not an oper, they can't change modes */
2244 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2245 send_notoper(state);
2249 if (state->done & DONE_KEY) /* allow key to be set only once */
2251 state->done |= DONE_KEY;
2255 /* clean up the key string */
2257 while (*s > ' ' && *s != ':' && *s != ',' && t_len--)
2261 if (!*t_str) { /* warn if empty */
2262 if (MyUser(state->sptr))
2263 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2271 /* Skip if this is a burst, we have a key already and the new key is
2272 * after the old one alphabetically */
2273 if ((state->flags & MODE_PARSE_BURST) &&
2274 *(state->chptr->mode.key) &&
2275 ircd_strcmp(state->chptr->mode.key, t_str) <= 0)
2278 /* can't add a key if one is set, nor can one remove the wrong key */
2279 if (!(state->flags & MODE_PARSE_FORCE))
2280 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2281 (state->dir == MODE_DEL &&
2282 ircd_strcmp(state->chptr->mode.key, t_str))) {
2283 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2287 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2288 !ircd_strcmp(state->chptr->mode.key, t_str))
2289 return; /* no key change */
2291 if (state->flags & MODE_PARSE_BOUNCE) {
2292 if (*state->chptr->mode.key) /* reset old key */
2293 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2294 state->chptr->mode.key, 0);
2295 else /* remove new bogus key */
2296 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2297 } else /* send new key */
2298 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2300 if (state->flags & MODE_PARSE_SET) {
2301 if (state->dir == MODE_ADD) /* set the new key */
2302 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2303 else /* remove the old key */
2304 *state->chptr->mode.key = '\0';
2309 * Helper function to convert user passes
2312 mode_parse_upass(struct ParseState *state, int *flag_p)
2317 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2320 if (state->parc <= 0) { /* warn if not enough args */
2321 if (MyUser(state->sptr))
2322 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +u" :
2327 t_str = state->parv[state->args_used++]; /* grab arg */
2331 /* If they're not an oper, they can't change modes */
2332 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2333 send_notoper(state);
2337 /* If they are not the channel manager, they are not allowed to change it */
2338 if (MyUser(state->sptr) && !IsChannelManager(state->member)) {
2339 if (*state->chptr->mode.apass) {
2340 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2341 "Use /JOIN", state->chptr->chname, "<AdminPass>.");
2343 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2344 "Re-create the channel. The channel must be *empty* for",
2345 TStime() - state->chptr->creationtime >= 171000 ? "48 contiguous hours" : "a minute or two",
2346 "before it can be recreated.");
2351 if (state->done & DONE_UPASS) /* allow upass to be set only once */
2353 state->done |= DONE_UPASS;
2355 t_len = PASSLEN + 1;
2357 /* clean up the upass string */
2359 while (*++s > ' ' && *s != ':' && --t_len)
2363 if (!*t_str) { /* warn if empty */
2364 if (MyUser(state->sptr))
2365 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +u" :
2373 if (!(state->flags & MODE_PARSE_FORCE))
2374 /* can't add the upass while apass is not set */
2375 if (state->dir == MODE_ADD && !*state->chptr->mode.apass) {
2376 send_reply(state->sptr, ERR_UPASSNOTSET, state->chptr->chname, state->chptr->chname);
2379 /* can't add a upass if one is set, nor can one remove the wrong upass */
2380 if ((state->dir == MODE_ADD && *state->chptr->mode.upass) ||
2381 (state->dir == MODE_DEL &&
2382 ircd_strcmp(state->chptr->mode.upass, t_str))) {
2383 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2387 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2388 !ircd_strcmp(state->chptr->mode.upass, t_str))
2389 return; /* no upass change */
2391 if (state->flags & MODE_PARSE_BOUNCE) {
2392 if (*state->chptr->mode.upass) /* reset old upass */
2393 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2394 state->chptr->mode.upass, 0);
2395 else /* remove new bogus upass */
2396 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2397 } else /* send new upass */
2398 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2400 if (state->flags & MODE_PARSE_SET) {
2401 if (state->dir == MODE_ADD) /* set the new upass */
2402 ircd_strncpy(state->chptr->mode.upass, t_str, PASSLEN);
2403 else /* remove the old upass */
2404 *state->chptr->mode.upass = '\0';
2409 * Helper function to convert admin passes
2412 mode_parse_apass(struct ParseState *state, int *flag_p)
2417 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2420 if (state->parc <= 0) { /* warn if not enough args */
2421 if (MyUser(state->sptr))
2422 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2427 t_str = state->parv[state->args_used++]; /* grab arg */
2431 /* If they're not an oper, they can't change modes */
2432 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2433 send_notoper(state);
2437 /* Don't allow to change the Apass if the channel is older than 48 hours. */
2438 if (TStime() - state->chptr->creationtime >= 172800 && !IsAnOper(state->sptr)) {
2439 send_reply(state->sptr, ERR_CHANSECURED, state->chptr->chname);
2443 /* If they are not the channel manager, they are not allowed to change it */
2444 if (MyUser(state->sptr) && !IsChannelManager(state->member)) {
2445 if (*state->chptr->mode.apass) {
2446 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2447 "Use /JOIN", state->chptr->chname, "<AdminPass>.");
2449 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2450 "Re-create the channel. The channel must be *empty* for",
2451 "at least a whole minute", "before it can be recreated.");
2456 if (state->done & DONE_APASS) /* allow apass to be set only once */
2458 state->done |= DONE_APASS;
2460 t_len = PASSLEN + 1;
2462 /* clean up the apass string */
2464 while (*++s > ' ' && *s != ':' && --t_len)
2468 if (!*t_str) { /* warn if empty */
2469 if (MyUser(state->sptr))
2470 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2478 if (!(state->flags & MODE_PARSE_FORCE)) {
2479 /* can't remove the apass while upass is still set */
2480 if (state->dir == MODE_DEL && *state->chptr->mode.upass) {
2481 send_reply(state->sptr, ERR_UPASSSET, state->chptr->chname, state->chptr->chname);
2484 /* can't add an apass if one is set, nor can one remove the wrong apass */
2485 if ((state->dir == MODE_ADD && *state->chptr->mode.apass) ||
2486 (state->dir == MODE_DEL && ircd_strcmp(state->chptr->mode.apass, t_str))) {
2487 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2492 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2493 !ircd_strcmp(state->chptr->mode.apass, t_str))
2494 return; /* no apass change */
2496 if (state->flags & MODE_PARSE_BOUNCE) {
2497 if (*state->chptr->mode.apass) /* reset old apass */
2498 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2499 state->chptr->mode.apass, 0);
2500 else /* remove new bogus apass */
2501 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2502 } else /* send new apass */
2503 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2505 if (state->flags & MODE_PARSE_SET) {
2506 if (state->dir == MODE_ADD) { /* set the new apass */
2507 /* Make it VERY clear to the user that this is a one-time password */
2508 ircd_strncpy(state->chptr->mode.apass, t_str, PASSLEN);
2509 if (MyUser(state->sptr)) {
2510 send_reply(state->sptr, RPL_APASSWARN,
2511 "Channel Admin password (+A) set to '", state->chptr->mode.apass, "'. ",
2512 "Are you SURE you want to use this as Admin password? ",
2513 "You will NOT be able to change this password anymore once the channel is more than 48 hours old!");
2514 send_reply(state->sptr, RPL_APASSWARN,
2515 "Use \"/MODE ", state->chptr->chname, " -A ", state->chptr->mode.apass,
2516 "\" to remove the password and then immediately set a new one. "
2517 "IMPORTANT: YOU CANNOT RECOVER THIS PASSWORD, EVER; "
2518 "WRITE THE PASSWORD DOWN (don't store this rescue password on disk)! "
2519 "Now set the channel user password (+u).");
2521 } else { /* remove the old apass */
2522 *state->chptr->mode.apass = '\0';
2523 if (MyUser(state->sptr))
2524 send_reply(state->sptr, RPL_APASSWARN,
2525 "WARNING: You removed the channel Admin password MODE (+A). ",
2526 "If you would disconnect or leave the channel without setting a new password then you will ",
2527 "not be able to set it again and lose ownership of this channel! ",
2528 "SET A NEW PASSWORD NOW!", "");
2534 * Helper function to convert bans
2537 mode_parse_ban(struct ParseState *state, int *flag_p)
2540 struct SLink *ban, *newban = 0;
2542 if (state->parc <= 0) { /* Not enough args, send ban list */
2543 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2544 send_ban_list(state->sptr, state->chptr);
2545 state->done |= DONE_BANLIST;
2551 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2554 t_str = state->parv[state->args_used++]; /* grab arg */
2558 /* If they're not an oper, they can't change modes */
2559 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2560 send_notoper(state);
2564 if ((s = strchr(t_str, ' ')))
2567 if (!*t_str || *t_str == ':') { /* warn if empty */
2568 if (MyUser(state->sptr))
2569 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2574 t_str = collapse(pretty_mask(t_str));
2576 /* remember the ban for the moment... */
2577 if (state->dir == MODE_ADD) {
2578 newban = state->banlist + (state->numbans++);
2581 DupString(newban->value.ban.banstr, t_str);
2582 newban->value.ban.who = cli_name(state->sptr);
2583 newban->value.ban.when = TStime();
2585 newban->flags = CHFL_BAN | MODE_ADD;
2587 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
2588 newban->flags |= CHFL_BAN_IPMASK;
2591 if (!state->chptr->banlist) {
2592 state->chptr->banlist = newban; /* add our ban with its flags */
2593 state->done |= DONE_BANCLEAN;
2597 /* Go through all bans */
2598 for (ban = state->chptr->banlist; ban; ban = ban->next) {
2599 /* first, clean the ban flags up a bit */
2600 if (!(state->done & DONE_BANCLEAN))
2601 /* Note: We're overloading *lots* of bits here; be careful! */
2602 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
2606 * MODE_ADD - Ban was added; if we're bouncing modes,
2607 * then we'll remove it below; otherwise,
2608 * we'll have to allocate a real ban
2610 * MODE_DEL - Ban was marked for deletion; if we're
2611 * bouncing modes, we'll have to re-add it,
2612 * otherwise, we'll have to remove it
2614 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
2615 * with a ban already set; if we're
2616 * bouncing modes, we'll have to bounce
2617 * this one; otherwise, we'll just ignore
2618 * it when we process added bans
2621 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
2622 ban->flags |= MODE_DEL; /* delete one ban */
2624 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2626 } else if (state->dir == MODE_ADD) {
2627 /* if the ban already exists, don't worry about it */
2628 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
2629 newban->flags &= ~MODE_ADD; /* don't add ban at all */
2630 MyFree(newban->value.ban.banstr); /* stopper a leak */
2631 state->numbans--; /* deallocate last ban */
2632 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2634 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
2635 if (!(ban->flags & MODE_DEL))
2636 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
2637 } else if (!mmatch(t_str, ban->value.ban.banstr))
2638 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
2640 if (!ban->next && (newban->flags & MODE_ADD))
2642 ban->next = newban; /* add our ban with its flags */
2643 break; /* get out of loop */
2647 state->done |= DONE_BANCLEAN;
2651 * This is the bottom half of the ban processor
2654 mode_process_bans(struct ParseState *state)
2656 struct SLink *ban, *newban, *prevban, *nextban;
2662 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2664 banlen = strlen(ban->value.ban.banstr);
2666 nextban = ban->next;
2668 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
2670 prevban->next = 0; /* Break the list; ban isn't a real ban */
2672 state->chptr->banlist = 0;
2677 MyFree(ban->value.ban.banstr);
2680 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
2681 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2682 ban->value.ban.banstr,
2683 state->flags & MODE_PARSE_SET);
2685 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2686 if (prevban) /* clip it out of the list... */
2687 prevban->next = ban->next;
2689 state->chptr->banlist = ban->next;
2694 MyFree(ban->value.ban.who);
2698 continue; /* next ban; keep prevban like it is */
2700 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
2701 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
2703 prevban->next = 0; /* Break the list; ban isn't a real ban */
2705 state->chptr->banlist = 0;
2707 /* If we're supposed to ignore it, do so. */
2708 if (ban->flags & CHFL_BAN_OVERLAPPED &&
2709 !(state->flags & MODE_PARSE_BOUNCE)) {
2713 MyFree(ban->value.ban.banstr);
2715 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2716 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2717 count > feature_int(FEAT_MAXBANS))) {
2718 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2719 ban->value.ban.banstr);
2723 MyFree(ban->value.ban.banstr);
2725 /* add the ban to the buffer */
2726 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2727 ban->value.ban.banstr,
2728 !(state->flags & MODE_PARSE_SET));
2730 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2731 newban = make_link();
2732 newban->value.ban.banstr = ban->value.ban.banstr;
2733 DupString(newban->value.ban.who, ban->value.ban.who);
2734 newban->value.ban.when = ban->value.ban.when;
2735 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
2737 newban->next = state->chptr->banlist; /* and link it in */
2738 state->chptr->banlist = newban;
2747 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2749 if (changed) /* if we changed the ban list, we must invalidate the bans */
2750 mode_ban_invalidate(state->chptr);
2754 * Helper function to process client changes
2757 mode_parse_client(struct ParseState *state, int *flag_p)
2760 struct Client *acptr;
2763 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2766 if (state->parc <= 0) /* return if not enough args */
2769 t_str = state->parv[state->args_used++]; /* grab arg */
2773 /* If they're not an oper, they can't change modes */
2774 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2775 send_notoper(state);
2779 if (MyUser(state->sptr)) /* find client we're manipulating */
2780 acptr = find_chasing(state->sptr, t_str, NULL);
2782 acptr = findNUser(t_str);
2785 return; /* find_chasing() already reported an error to the user */
2787 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2788 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2789 state->cli_change[i].flag & flag_p[0]))
2790 break; /* found a slot */
2792 /* Store what we're doing to them */
2793 state->cli_change[i].flag = state->dir | flag_p[0];
2794 state->cli_change[i].client = acptr;
2798 * Helper function to process the changed client list
2801 mode_process_clients(struct ParseState *state)
2804 struct Membership *member;
2806 for (i = 0; state->cli_change[i].flag; i++) {
2807 assert(0 != state->cli_change[i].client);
2809 /* look up member link */
2810 if (!(member = find_member_link(state->chptr,
2811 state->cli_change[i].client)) ||
2812 (MyUser(state->sptr) && IsZombie(member))) {
2813 if (MyUser(state->sptr))
2814 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2815 cli_name(state->cli_change[i].client),
2816 state->chptr->chname);
2820 if ((state->cli_change[i].flag & MODE_ADD &&
2821 (state->cli_change[i].flag & member->status)) ||
2822 (state->cli_change[i].flag & MODE_DEL &&
2823 !(state->cli_change[i].flag & member->status)))
2824 continue; /* no change made, don't do anything */
2826 /* see if the deop is allowed */
2827 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2828 (MODE_DEL | MODE_CHANOP)) {
2829 /* prevent +k users from being deopped */
2830 if (IsChannelService(state->cli_change[i].client)) {
2831 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
2832 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
2834 (IsServer(state->sptr) ? cli_name(state->sptr) :
2835 cli_name((cli_user(state->sptr))->server)));
2837 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
2838 send_reply(state->sptr, ERR_ISCHANSERVICE,
2839 cli_name(state->cli_change[i].client),
2840 state->chptr->chname);
2845 /* check deop for local user */
2846 if (MyUser(state->sptr)) {
2848 /* don't allow local opers to be deopped on local channels */
2849 if (state->cli_change[i].client != state->sptr &&
2850 IsLocalChannel(state->chptr->chname) &&
2851 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
2852 send_reply(state->sptr, ERR_ISOPERLCHAN,
2853 cli_name(state->cli_change[i].client),
2854 state->chptr->chname);
2858 if (feature_bool(FEAT_OPLEVELS)) {
2859 /* don't allow to deop members with an op level that is <= our own level */
2860 if (state->sptr != state->cli_change[i].client /* but allow to deop oneself */
2862 && OpLevel(member) <= OpLevel(state->member)) {
2863 int equal = (OpLevel(member) == OpLevel(state->member));
2864 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
2865 cli_name(state->cli_change[i].client),
2866 state->chptr->chname,
2867 OpLevel(state->member), OpLevel(member),
2868 "deop", equal ? "the same" : "a higher");
2875 /* set op-level of member being opped */
2876 if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
2877 (MODE_ADD | MODE_CHANOP)) {
2878 /* If on a channel with upass set, someone with level x gives ops to someone else,
2879 then that person gets level x-1. On other channels, where upass is not set,
2880 the level stays the same. */
2881 int level_increment = *state->chptr->mode.upass ? 1 : 0;
2882 /* Someone being opped by a server gets op-level 0 */
2883 int old_level = (state->member == NULL) ? -level_increment : OpLevel(state->member);
2884 SetOpLevel(member, old_level == MAXOPLEVEL ? MAXOPLEVEL : (old_level + level_increment));
2887 /* actually effect the change */
2888 if (state->flags & MODE_PARSE_SET) {
2889 if (state->cli_change[i].flag & MODE_ADD) {
2890 if (IsDelayedJoin(member))
2891 RevealDelayedJoin(member);
2892 member->status |= (state->cli_change[i].flag &
2893 (MODE_CHANOP | MODE_VOICE));
2894 if (state->cli_change[i].flag & MODE_CHANOP)
2895 ClearDeopped(member);
2897 member->status &= ~(state->cli_change[i].flag &
2898 (MODE_CHANOP | MODE_VOICE));
2901 /* accumulate the change */
2902 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
2903 state->cli_change[i].client);
2904 } /* for (i = 0; state->cli_change[i].flags; i++) */
2908 * Helper function to process the simple modes
2911 mode_parse_mode(struct ParseState *state, int *flag_p)
2913 /* If they're not an oper, they can't change modes */
2914 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2915 send_notoper(state);
2922 if (state->dir == MODE_ADD) {
2923 state->add |= flag_p[0];
2924 state->del &= ~flag_p[0];
2926 if (flag_p[0] & MODE_SECRET) {
2927 state->add &= ~MODE_PRIVATE;
2928 state->del |= MODE_PRIVATE;
2929 } else if (flag_p[0] & MODE_PRIVATE) {
2930 state->add &= ~MODE_SECRET;
2931 state->del |= MODE_SECRET;
2933 if (flag_p[0] & MODE_DELJOINS) {
2934 state->add &= ~MODE_WASDELJOINS;
2935 state->del |= MODE_WASDELJOINS;
2938 state->add &= ~flag_p[0];
2939 state->del |= flag_p[0];
2942 assert(0 == (state->add & state->del));
2943 assert((MODE_SECRET | MODE_PRIVATE) !=
2944 (state->add & (MODE_SECRET | MODE_PRIVATE)));
2948 * This routine is intended to parse MODE or OPMODE commands and effect the
2949 * changes (or just build the bounce buffer). We pass the starting offset
2953 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
2954 struct Channel *chptr, int parc, char *parv[], unsigned int flags,
2955 struct Membership* member)
2957 static int chan_flags[] = {
2962 MODE_MODERATED, 'm',
2963 MODE_TOPICLIMIT, 't',
2964 MODE_INVITEONLY, 'i',
2965 MODE_NOPRIVMSGS, 'n',
2979 unsigned int t_mode;
2981 struct ParseState state;
2992 state.chptr = chptr;
2993 state.member = member;
2996 state.flags = flags;
2997 state.dir = MODE_ADD;
3001 state.args_used = 0;
3002 state.max_args = MAXMODEPARAMS;
3005 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
3006 state.banlist[i].next = 0;
3007 state.banlist[i].value.ban.banstr = 0;
3008 state.banlist[i].value.ban.who = 0;
3009 state.banlist[i].value.ban.when = 0;
3010 state.banlist[i].flags = 0;
3011 state.cli_change[i].flag = 0;
3012 state.cli_change[i].client = 0;
3015 modestr = state.parv[state.args_used++];
3019 for (; *modestr; modestr++) {
3020 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
3021 if (flag_p[1] == *modestr)
3024 if (!flag_p[0]) { /* didn't find it? complain and continue */
3025 if (MyUser(state.sptr))
3026 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
3031 case '+': /* switch direction to MODE_ADD */
3032 case '-': /* switch direction to MODE_DEL */
3033 state.dir = flag_p[0];
3036 case 'l': /* deal with limits */
3037 mode_parse_limit(&state, flag_p);
3040 case 'k': /* deal with keys */
3041 mode_parse_key(&state, flag_p);
3044 case 'A': /* deal with Admin passes */
3045 if (feature_bool(FEAT_OPLEVELS))
3046 mode_parse_apass(&state, flag_p);
3049 case 'u': /* deal with user passes */
3050 if (feature_bool(FEAT_OPLEVELS))
3051 mode_parse_upass(&state, flag_p);
3054 case 'b': /* deal with bans */
3055 mode_parse_ban(&state, flag_p);
3058 case 'o': /* deal with ops/voice */
3060 mode_parse_client(&state, flag_p);
3063 default: /* deal with other modes */
3064 mode_parse_mode(&state, flag_p);
3066 } /* switch (*modestr) */
3067 } /* for (; *modestr; modestr++) */
3069 if (state.flags & MODE_PARSE_BURST)
3070 break; /* don't interpret any more arguments */
3072 if (state.parc > 0) { /* process next argument in string */
3073 modestr = state.parv[state.args_used++];
3077 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
3080 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
3081 break; /* we're then going to bounce the mode! */
3083 recv_ts = atoi(modestr);
3085 if (recv_ts && recv_ts < state.chptr->creationtime)
3086 state.chptr->creationtime = recv_ts; /* respect earlier TS */
3088 break; /* break out of while loop */
3089 } else if (state.flags & MODE_PARSE_STRICT ||
3090 (MyUser(state.sptr) && state.max_args <= 0)) {
3091 state.parc++; /* we didn't actually gobble the argument */
3093 break; /* break out of while loop */
3096 } /* while (*modestr) */
3099 * the rest of the function finishes building resultant MODEs; if the
3100 * origin isn't a member or an oper, skip it.
3102 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3103 return state.args_used; /* tell our parent how many args we gobbled */
3105 t_mode = state.chptr->mode.mode;
3107 if (state.del & t_mode) { /* delete any modes to be deleted... */
3108 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
3110 t_mode &= ~state.del;
3112 if (state.add & ~t_mode) { /* add any modes to be added... */
3113 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
3115 t_mode |= state.add;
3118 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
3119 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
3120 !(t_mode & MODE_INVITEONLY))
3121 mode_invite_clear(state.chptr);
3123 state.chptr->mode.mode = t_mode;
3126 if (state.flags & MODE_PARSE_WIPEOUT) {
3127 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
3128 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
3129 state.chptr->mode.limit);
3130 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
3131 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
3132 state.chptr->mode.key, 0);
3133 if (*state.chptr->mode.upass && !(state.done & DONE_UPASS))
3134 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_UPASS,
3135 state.chptr->mode.upass, 0);
3136 if (*state.chptr->mode.apass && !(state.done & DONE_APASS))
3137 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_APASS,
3138 state.chptr->mode.apass, 0);
3141 if (state.done & DONE_BANCLEAN) /* process bans */
3142 mode_process_bans(&state);
3144 /* process client changes */
3145 if (state.cli_change[0].flag)
3146 mode_process_clients(&state);
3148 return state.args_used; /* tell our parent how many args we gobbled */
3152 * Initialize a join buffer
3155 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
3156 struct Client *connect, unsigned int type, char *comment,
3162 assert(0 != source);
3163 assert(0 != connect);
3165 jbuf->jb_source = source; /* just initialize struct JoinBuf */
3166 jbuf->jb_connect = connect;
3167 jbuf->jb_type = type;
3168 jbuf->jb_comment = comment;
3169 jbuf->jb_create = create;
3171 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
3172 type == JOINBUF_TYPE_PART ||
3173 type == JOINBUF_TYPE_PARTALL) ?
3174 STARTJOINLEN : STARTCREATELEN) +
3175 (comment ? strlen(comment) + 2 : 0));
3177 for (i = 0; i < MAXJOINARGS; i++)
3178 jbuf->jb_channels[i] = 0;
3182 * Add a channel to the join buffer
3185 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
3193 if (jbuf->jb_type == JOINBUF_TYPE_JOIN)
3194 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
3199 is_local = IsLocalChannel(chan->chname);
3201 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
3202 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
3203 struct Membership *member = find_member_link(chan, jbuf->jb_source);
3204 if (IsUserParting(member))
3206 SetUserParting(member);
3208 /* Send notification to channel */
3209 if (!(flags & (CHFL_ZOMBIE | CHFL_DELAYED)))
3210 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL,
3211 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3212 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3213 else if (MyUser(jbuf->jb_source))
3214 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
3215 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3216 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3217 /* XXX: Shouldn't we send a PART here anyway? */
3218 /* to users on the channel? Why? From their POV, the user isn't on
3219 * the channel anymore anyway. We don't send to servers until below,
3220 * when we gang all the channel parts together. Note that this is
3221 * exactly the same logic, albeit somewhat more concise, as was in
3222 * the original m_part.c */
3224 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3225 is_local) /* got to remove user here */
3226 remove_user_from_channel(jbuf->jb_source, chan);
3228 /* Add user to channel */
3229 if ((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))
3230 add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, 0);
3232 add_user_to_channel(chan, jbuf->jb_source, flags, 0);
3234 /* send notification to all servers */
3235 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local)
3236 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
3237 "%H %Tu", chan, chan->creationtime);
3239 if (!((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))) {
3240 /* Send the notification to the channel */
3241 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, "%H", chan);
3243 /* send an op, too, if needed */
3244 if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE)
3245 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_MODE, chan, NULL, "%H +o %C",
3246 chan, jbuf->jb_source);
3247 } else if (MyUser(jbuf->jb_source))
3248 sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan);
3251 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3252 jbuf->jb_type == JOINBUF_TYPE_JOIN || is_local)
3253 return; /* don't send to remote */
3255 /* figure out if channel name will cause buffer to be overflowed */
3256 len = chan ? strlen(chan->chname) + 1 : 2;
3257 if (jbuf->jb_strlen + len > BUFSIZE)
3258 joinbuf_flush(jbuf);
3260 /* add channel to list of channels to send and update counts */
3261 jbuf->jb_channels[jbuf->jb_count++] = chan;
3262 jbuf->jb_strlen += len;
3264 /* if we've used up all slots, flush */
3265 if (jbuf->jb_count >= MAXJOINARGS)
3266 joinbuf_flush(jbuf);
3270 * Flush the channel list to remote servers
3273 joinbuf_flush(struct JoinBuf *jbuf)
3275 char chanlist[BUFSIZE];
3279 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3280 jbuf->jb_type == JOINBUF_TYPE_JOIN)
3281 return 0; /* no joins to process */
3283 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
3284 build_string(chanlist, &chanlist_i,
3285 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
3286 i == 0 ? '\0' : ',');
3287 if (JOINBUF_TYPE_PART == jbuf->jb_type)
3288 /* Remove user from channel */
3289 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
3291 jbuf->jb_channels[i] = 0; /* mark slot empty */
3294 jbuf->jb_count = 0; /* reset base counters */
3295 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
3296 STARTJOINLEN : STARTCREATELEN) +
3297 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
3299 /* and send the appropriate command */
3300 switch (jbuf->jb_type) {
3301 case JOINBUF_TYPE_CREATE:
3302 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
3303 "%s %Tu", chanlist, jbuf->jb_create);
3306 case JOINBUF_TYPE_PART:
3307 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
3308 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,
3316 /* Returns TRUE (1) if client is invited, FALSE (0) if not */
3317 int IsInvited(struct Client* cptr, const void* chptr)
3321 for (lp = (cli_user(cptr))->invited; lp; lp = lp->next)
3322 if (lp->value.chptr == chptr)
3327 /* RevealDelayedJoin: sends a join for a hidden user */
3329 void RevealDelayedJoin(struct Membership *member) {
3330 ClearDelayedJoin(member);
3331 sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, ":%H",
3333 CheckDelayedJoins(member->channel);
3336 /* CheckDelayedJoins: checks and clear +d if necessary */
3338 void CheckDelayedJoins(struct Channel *chan) {
3339 struct Membership *memb2;
3341 if (chan->mode.mode & MODE_WASDELJOINS) {
3342 for (memb2=chan->members;memb2;memb2=memb2->next_member)
3343 if (IsDelayedJoin(memb2))
3348 chan->mode.mode &= ~MODE_WASDELJOINS;
3349 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan, NULL,