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.
28 #include "ircd_alloc.h"
29 #include "ircd_chattr.h"
30 #include "ircd_defs.h"
31 #include "ircd_features.h"
33 #include "ircd_policy.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 static struct SLink *next_overlapped_ban(void);
66 static int del_banid(struct Channel *, char *, int);
67 void del_invite(struct Client *, struct Channel *);
69 const char* const PartFmt1 = ":%s " MSG_PART " %s";
70 const char* const PartFmt2 = ":%s " MSG_PART " %s :%s";
71 const char* const PartFmt1serv = "%s%s " TOK_PART " %s";
72 const char* const PartFmt2serv = "%s%s " TOK_PART " %s :%s";
75 static struct SLink* next_ban;
76 static struct SLink* prev_ban;
77 static struct SLink* removed_bans_list;
80 * Use a global variable to remember if an oper set a mode on a local channel. Ugly,
81 * but the only way to do it without changing set_mode intensively.
83 int LocalChanOperMode = 0;
87 * return the length (>=0) of a chain of links.
89 static int list_length(struct SLink *lp)
93 for (; lp; lp = lp->next)
99 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
101 struct Membership *m;
105 /* Servers don't have member links */
106 if (IsServer(cptr)||IsMe(cptr))
109 /* +k users are typically on a LOT of channels. So we iterate over who
110 * is in the channel. X/W are +k and are in about 5800 channels each.
111 * however there are typically no more than 1000 people in a channel
114 if (IsChannelService(cptr)) {
117 assert(m->channel == chptr);
123 /* Users on the other hand aren't allowed on more than 15 channels. 50%
124 * of users that are on channels are on 2 or less, 95% are on 7 or less,
125 * and 99% are on 10 or less.
128 m = (cli_user(cptr))->channel;
130 assert(m->user == cptr);
131 if (m->channel == chptr)
140 * find_chasing - Find the client structure for a nick name (user)
141 * using history mechanism if necessary. If the client is not found, an error
142 * message (NO SUCH NICK) is generated. If the client was found
143 * through the history, chasing will be 1 and otherwise 0.
145 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
147 struct Client* who = FindClient(user);
154 if (!(who = get_history(user, feature_int(FEAT_KILLCHASETIMELIMIT)))) {
155 send_reply(sptr, ERR_NOSUCHNICK, user);
164 * Create a string of form "foo!bar@fubar" given foo, bar and fubar
165 * as the parameters. If NULL, they become "*".
167 static char *make_nick_user_host(const char *nick, const char *name,
170 static char namebuf[NICKLEN + USERLEN + HOSTLEN + 3];
171 ircd_snprintf(0, namebuf, sizeof(namebuf), "%s!%s@%s", nick, name, host);
176 * Create a string of form "foo!bar@123.456.789.123" given foo, bar and the
177 * IP-number as the parameters. If NULL, they become "*".
179 static char *make_nick_user_ip(char *nick, char *name, struct in_addr ip)
181 static char ipbuf[NICKLEN + USERLEN + 16 + 3];
182 ircd_snprintf(0, ipbuf, sizeof(ipbuf), "%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 exists
191 * false (0) if the channel was destroyed
193 int sub1_from_channel(struct Channel* chptr)
198 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
200 assert(0 != chptr->members);
205 assert(0 == chptr->members);
207 /* Channel became (or was) empty: Remove channel */
208 if (is_listed(chptr))
211 for (i = 0; i <= HighestFd; i++)
213 struct Client *acptr = 0;
214 if ((acptr = LocalClientArray[i]) && cli_listing(acptr) &&
215 (cli_listing(acptr))->chptr == chptr)
217 list_next_channels(acptr, 1);
218 break; /* Only one client can list a channel */
223 * Now, find all invite links from channel structure
225 while ((tmp = chptr->invites))
226 del_invite(tmp->value.cptr, chptr);
228 tmp = chptr->banlist;
233 MyFree(obtmp->value.ban.banstr);
234 MyFree(obtmp->value.ban.who);
238 chptr->prev->next = chptr->next;
240 GlobalChannelList = chptr->next;
242 chptr->next->prev = chptr->prev;
244 --UserStats.channels;
246 * make sure that channel actually got removed from hash table
248 assert(chptr->hnext == chptr);
256 * `cptr' must be the client adding the ban.
258 * If `change' is true then add `banid' to channel `chptr'.
259 * Returns 0 if the ban was added.
260 * Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
261 * Return -1 otherwise.
263 * Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
264 * when `change' is false, otherwise they will be removed from the banlist.
265 * Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
266 * respectively will return these bans until NULL is returned.
268 * If `firsttime' is true, the ban list as returned by next_overlapped_ban()
269 * is reset (unless a non-zero value is returned, in which case the
270 * CHFL_BAN_OVERLAPPED flag might not have been reset!).
274 int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
275 int change, int firsttime)
280 int removed_bans = 0;
281 int len = strlen(banid);
286 assert(0 == prev_ban);
287 assert(0 == removed_bans_list);
291 for (banp = &chptr->banlist; *banp;)
293 len += strlen((*banp)->value.ban.banstr);
295 if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
297 if (!strcmp((*banp)->value.ban.banstr, banid))
299 (*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
303 else if (!mmatch((*banp)->value.ban.banstr, banid))
305 if (!mmatch(banid, (*banp)->value.ban.banstr))
307 struct SLink *tmp = *banp;
313 len -= strlen(tmp->value.ban.banstr);
316 /* These will be sent to the user later as -b */
317 tmp->next = removed_bans_list;
318 removed_bans_list = tmp;
321 else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
323 tmp->flags |= CHFL_BAN_OVERLAPPED;
334 (*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
335 banp = &(*banp)->next;
338 if (MyUser(cptr) && !removed_bans &&
339 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
340 (cnt >= feature_int(FEAT_MAXBANS))))
342 send_reply(cptr, ERR_BANLISTFULL, chptr->chname, banid);
348 struct Membership* member;
350 ban->next = chptr->banlist;
352 ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
353 assert(0 != ban->value.ban.banstr);
354 strcpy(ban->value.ban.banstr, banid);
356 #ifdef HEAD_IN_SAND_BANWHO
358 DupString(ban->value.ban.who, cli_name(&me));
361 DupString(ban->value.ban.who, cli_name(cptr));
362 assert(0 != ban->value.ban.who);
364 ban->value.ban.when = TStime();
365 ban->flags = CHFL_BAN; /* This bit is never used I think... */
366 if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
367 ban->flags |= CHFL_BAN_IPMASK;
368 chptr->banlist = ban;
371 * Erase ban-valid-bit
373 for (member = chptr->members; member; member = member->next_member)
374 ClearBanValid(member); /* `ban' == channel member ! */
379 static struct SLink *next_overlapped_ban(void)
381 struct SLink *tmp = next_ban;
385 for (ban = tmp->next; ban; ban = ban->next)
386 if ((ban->flags & CHFL_BAN_OVERLAPPED))
393 struct SLink *next_removed_overlapped_ban(void)
395 struct SLink *tmp = removed_bans_list;
398 if (prev_ban->value.ban.banstr) /* Can be set to NULL in set_mode() */
399 MyFree(prev_ban->value.ban.banstr);
400 MyFree(prev_ban->value.ban.who);
405 removed_bans_list = removed_bans_list->next;
413 * If `change' is true, delete `banid' from channel `chptr'.
414 * Returns `false' if removal was (or would have been) successful.
416 static int del_banid(struct Channel *chptr, char *banid, int change)
423 for (ban = &(chptr->banlist); *ban; ban = &((*ban)->next)) {
424 if (0 == ircd_strcmp(banid, (*ban)->value.ban.banstr))
429 struct Membership* member;
431 MyFree(tmp->value.ban.banstr);
432 MyFree(tmp->value.ban.who);
435 * Erase ban-valid-bit, for channel members that are banned
437 for (member = chptr->members; member; member = member->next_member)
438 if (CHFL_BANVALIDMASK == (member->status & CHFL_BANVALIDMASK))
439 ClearBanValid(member); /* `tmp' == channel member */
448 * find_channel_member - returns Membership * if a person is joined and not a zombie
450 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
452 struct Membership* member;
455 member = find_member_link(chptr, cptr);
456 return (member && !IsZombie(member)) ? member : 0;
460 * is_banned - a non-zero value if banned else 0.
462 static int is_banned(struct Client *cptr, struct Channel *chptr,
463 struct Membership* member)
472 if (member && IsBanValid(member))
473 return IsBanned(member);
475 s = make_nick_user_host(cli_name(cptr), (cli_user(cptr))->username,
476 (cli_user(cptr))->host);
478 for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
479 if ((tmp->flags & CHFL_BAN_IPMASK)) {
481 ip_s = make_nick_user_ip(cli_name(cptr), (cli_user(cptr))->username,
483 if (match(tmp->value.ban.banstr, ip_s) == 0)
486 else if (match(tmp->value.ban.banstr, s) == 0)
502 return (tmp != NULL);
506 * adds a user to a channel by adding another link to the channels member
509 void add_user_to_channel(struct Channel* chptr, struct Client* who,
517 struct Membership* member = membershipFreeList;
519 membershipFreeList = member->next_member;
521 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
522 ++membershipAllocCount;
527 member->channel = chptr;
528 member->status = flags;
530 member->next_member = chptr->members;
531 if (member->next_member)
532 member->next_member->prev_member = member;
533 member->prev_member = 0;
534 chptr->members = member;
536 member->next_channel = (cli_user(who))->channel;
537 if (member->next_channel)
538 member->next_channel->prev_channel = member;
539 member->prev_channel = 0;
540 (cli_user(who))->channel = member;
543 ++((cli_user(who))->joined);
547 static int remove_member_from_channel(struct Membership* member)
549 struct Channel* chptr;
551 chptr = member->channel;
553 * unlink channel member list
555 if (member->next_member)
556 member->next_member->prev_member = member->prev_member;
557 if (member->prev_member)
558 member->prev_member->next_member = member->next_member;
560 member->channel->members = member->next_member;
563 * unlink client channel list
565 if (member->next_channel)
566 member->next_channel->prev_channel = member->prev_channel;
567 if (member->prev_channel)
568 member->prev_channel->next_channel = member->next_channel;
570 (cli_user(member->user))->channel = member->next_channel;
572 --(cli_user(member->user))->joined;
574 member->next_member = membershipFreeList;
575 membershipFreeList = member;
577 return sub1_from_channel(chptr);
580 static int channel_all_zombies(struct Channel* chptr)
582 struct Membership* member;
584 for (member = chptr->members; member; member = member->next_member) {
585 if (!IsZombie(member))
592 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
595 struct Membership* member;
598 if ((member = find_member_link(chptr, cptr))) {
599 if (remove_member_from_channel(member)) {
600 if (channel_all_zombies(chptr)) {
602 * XXX - this looks dangerous but isn't if we got the referential
603 * integrity right for channels
605 while (remove_member_from_channel(chptr->members))
612 void remove_user_from_all_channels(struct Client* cptr)
614 struct Membership* chan;
616 assert(0 != cli_user(cptr));
618 while ((chan = (cli_user(cptr))->channel))
619 remove_user_from_channel(cptr, chan->channel);
622 int is_chan_op(struct Client *cptr, struct Channel *chptr)
624 struct Membership* member;
626 if ((member = find_member_link(chptr, cptr)))
627 return (!IsZombie(member) && IsChanOp(member));
632 static int is_deopped(struct Client *cptr, struct Channel *chptr)
634 struct Membership* member;
637 if ((member = find_member_link(chptr, cptr)))
638 return IsDeopped(member);
640 return (IsUser(cptr) ? 1 : 0);
643 int is_zombie(struct Client *cptr, struct Channel *chptr)
645 struct Membership* member;
649 if ((member = find_member_link(chptr, cptr)))
650 return IsZombie(member);
654 int has_voice(struct Client* cptr, struct Channel* chptr)
656 struct Membership* member;
659 if ((member = find_member_link(chptr, cptr)))
660 return (!IsZombie(member) && HasVoice(member));
665 int member_can_send_to_channel(struct Membership* member)
669 if (IsVoicedOrOpped(member))
672 * If it's moderated, and you aren't a priviledged user, you can't
675 if (member->channel->mode.mode & MODE_MODERATED)
678 * If you're banned then you can't speak either.
679 * but because of the amount of CPU time that is_banned chews
680 * we only check it for our clients.
682 if (MyUser(member->user) && is_banned(member->user, member->channel, member))
687 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
689 struct Membership *member;
692 * Servers can always speak on channels.
697 member = find_channel_member(cptr, chptr);
700 * You can't speak if your off channel, if the channel is modeless, or
701 * +n (no external messages) or +m (moderated).
704 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED))
705 || IsModelessChannel(chptr->chname))
710 return member_can_send_to_channel(member);
714 * find_no_nickchange_channel
715 * if a member and not opped or voiced and banned
716 * return the name of the first channel banned on
718 const char* find_no_nickchange_channel(struct Client* cptr)
721 struct Membership* member;
722 for (member = (cli_user(cptr))->channel; member;
723 member = member->next_channel) {
724 if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
725 return member->channel->chname;
733 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
734 * with the parameters in pbuf.
736 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
737 struct Channel *chptr)
744 if (chptr->mode.mode & MODE_SECRET)
746 else if (chptr->mode.mode & MODE_PRIVATE)
748 if (chptr->mode.mode & MODE_MODERATED)
750 if (chptr->mode.mode & MODE_TOPICLIMIT)
752 if (chptr->mode.mode & MODE_INVITEONLY)
754 if (chptr->mode.mode & MODE_NOPRIVMSGS)
756 if (chptr->mode.limit) {
758 ircd_snprintf(0, pbuf, sizeof(pbuf), "%d", chptr->mode.limit);
761 if (*chptr->mode.key) {
763 if (chptr->mode.limit)
765 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
766 strcat(pbuf, chptr->mode.key);
774 * send "cptr" a full list of the modes for channel chptr.
776 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
778 static unsigned int current_flags[4] =
779 { 0, CHFL_CHANOP | CHFL_VOICE, CHFL_VOICE, CHFL_CHANOP };
785 struct Membership* member;
787 char modebuf[MODEBUFLEN];
788 char parabuf[MODEBUFLEN];
794 if (IsLocalChannel(chptr->chname))
797 member = chptr->members;
798 lp2 = chptr->banlist;
800 *modebuf = *parabuf = '\0';
801 channel_modes(cptr, modebuf, parabuf, chptr);
803 for (first = 1; full; first = 0) /* Loop for multiple messages */
805 full = 0; /* Assume by default we get it
806 all in one message */
808 /* (Continued) prefix: "<Y> B <channel> <TS>" */
809 /* is there any better way we can do this? */
810 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
811 chptr->creationtime);
813 if (first && modebuf[1]) /* Add simple modes (iklmnpst)
816 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
817 msgq_append(&me, mb, " %s", modebuf);
820 msgq_append(&me, mb, " %s", parabuf);
824 * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
826 * Run 4 times over all members, to group the members with the
829 for (first = 1; flag_cnt < 4;
830 member = chptr->members, new_mode = 1, flag_cnt++)
832 for (; member; member = member->next_member)
834 if ((member->status & CHFL_VOICED_OR_OPPED) !=
835 current_flags[flag_cnt])
836 continue; /* Skip members with different flags */
837 if (msgq_bufleft(mb) < NUMNICKLEN + 4)
838 /* The 4 is a possible ",:ov" */
840 full = 1; /* Make sure we continue after
842 new_mode = 1; /* Ensure the new BURST line contains the current
844 break; /* Do not add this member to this message */
846 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
847 first = 0; /* From now on, us comma's to add new nicks */
850 * Do we have a nick with a new mode ?
851 * Or are we starting a new BURST line?
856 if (IsVoicedOrOpped(member)) {
860 if (IsChanOp(member))
862 if (HasVoice(member))
865 msgq_append(&me, mb, tbuf);
875 /* Attach all bans, space seperated " :%ban ban ..." */
876 for (first = 2; lp2; lp2 = lp2->next)
878 len = strlen(lp2->value.ban.banstr);
879 if (msgq_bufleft(mb) < len + 1 + first)
880 /* The +1 stands for the added ' '.
881 * The +first stands for the added ":%".
887 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
888 lp2->value.ban.banstr);
893 send_buffer(cptr, mb, 0); /* Send this message */
895 } /* Continue when there was something
896 that didn't fit (full==1) */
902 * by Carlo Wood (Run), 05 Oct 1998.
906 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
907 * When the user name or host name are too long (USERLEN and HOSTLEN
908 * respectively) then they are cut off at the start with a '*'.
910 * The following transformations are made:
913 * 2) xxx.xxx -> *!*@host
914 * 3) xxx!yyy -> nick!user@*
915 * 4) xxx@yyy -> *!user@host
916 * 5) xxx!yyy@zzz -> nick!user@host
918 char *pretty_mask(char *mask)
920 static char star[2] = { '*', 0 };
921 char *last_dot = NULL;
924 /* Case 1: default */
929 /* Do a _single_ pass through the characters of the mask: */
930 for (ptr = mask; *ptr; ++ptr)
934 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
938 else if (*ptr == '@')
940 /* Case 4: Found last '@' (without finding a '!' yet) */
945 else if (*ptr == '.')
947 /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
957 /* Case 4 or 5: Found last '@' */
963 if (user == star && last_dot)
973 char *nick_end = (user != star) ? user - 1 : ptr;
974 if (nick_end - nick > NICKLEN)
980 char *user_end = (host != star) ? host - 1 : ptr;
981 if (user_end - user > USERLEN)
983 user = user_end - USERLEN;
988 if (host != star && ptr - host > HOSTLEN)
990 host = ptr - HOSTLEN;
993 return make_nick_user_host(nick, user, host);
996 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1003 for (lp = chptr->banlist; lp; lp = lp->next)
1004 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->value.ban.banstr,
1005 lp->value.ban.who, lp->value.ban.when);
1007 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1010 /* We are now treating the <key> part of /join <channel list> <key> as a key
1011 * ring; that is, we try one key against the actual channel key, and if that
1012 * doesn't work, we try the next one, and so on. -Kev -Texaco
1013 * Returns: 0 on match, 1 otherwise
1014 * This version contributed by SeKs <intru@info.polymtl.ca>
1016 static int compall(char *key, char *keyring)
1021 p1 = key; /* point to the key... */
1022 while (*p1 && *p1 == *keyring)
1023 { /* step through the key and ring until they
1029 if (!*p1 && (!*keyring || *keyring == ','))
1030 /* ok, if we're at the end of the and also at the end of one of the keys
1031 in the keyring, we have a match */
1034 if (!*keyring) /* if we're at the end of the key ring, there
1035 weren't any matches, so we return 1 */
1038 /* Not at the end of the key ring, so step
1039 through to the next key in the ring: */
1040 while (*keyring && *(keyring++) != ',');
1042 goto top; /* and check it against the key */
1045 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
1048 int overrideJoin = 0;
1051 * Now a banned user CAN join if invited -- Nemesi
1052 * Now a user CAN escape channel limit if invited -- bfriendly
1053 * Now a user CAN escape anything if invited -- Isomer
1056 for (lp = (cli_user(sptr))->invited; lp; lp = lp->next)
1057 if (lp->value.chptr == chptr)
1060 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
1061 a HACK(4) notice will be sent if he would not have been supposed
1062 to join normally. */
1063 if (IsLocalChannel(chptr->chname) && HasPriv(sptr, PRIV_WALK_LCHAN) &&
1064 !BadPtr(key) && compall("OVERRIDE",key) == 0)
1065 overrideJoin = MAGIC_OPER_OVERRIDE;
1067 if (chptr->mode.mode & MODE_INVITEONLY)
1068 return overrideJoin + ERR_INVITEONLYCHAN;
1070 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
1071 return overrideJoin + ERR_CHANNELISFULL;
1073 if (is_banned(sptr, chptr, NULL))
1074 return overrideJoin + ERR_BANNEDFROMCHAN;
1077 * now using compall (above) to test against a whole key ring -Kev
1079 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
1080 return overrideJoin + ERR_BADCHANNELKEY;
1083 return ERR_DONTCHEAT;
1089 * Remove bells and commas from channel name
1091 void clean_channelname(char *cn)
1095 for (i = 0; cn[i]; i++) {
1096 if (i >= CHANNELLEN || !IsChannelChar(cn[i])) {
1100 if (IsChannelLower(cn[i])) {
1101 cn[i] = ToLower(cn[i]);
1107 if ((unsigned char)(cn[i]) == 0xd0)
1108 cn[i] = (char) 0xf0;
1115 * Get Channel block for i (and allocate a new channel
1116 * block, if it didn't exists before).
1118 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1120 struct Channel *chptr;
1123 if (EmptyString(chname))
1126 len = strlen(chname);
1127 if (MyUser(cptr) && len > CHANNELLEN)
1130 *(chname + CHANNELLEN) = '\0';
1132 if ((chptr = FindChannel(chname)))
1134 if (flag == CGT_CREATE)
1136 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1138 ++UserStats.channels;
1139 memset(chptr, 0, sizeof(struct Channel));
1140 strcpy(chptr->chname, chname);
1141 if (GlobalChannelList)
1142 GlobalChannelList->prev = chptr;
1144 chptr->next = GlobalChannelList;
1145 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1146 GlobalChannelList = chptr;
1152 void add_invite(struct Client *cptr, struct Channel *chptr)
1154 struct SLink *inv, **tmp;
1156 del_invite(cptr, chptr);
1158 * Delete last link in chain if the list is max length
1160 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1161 if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER))
1162 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1164 * Add client to channel invite list
1167 inv->value.cptr = cptr;
1168 inv->next = chptr->invites;
1169 chptr->invites = inv;
1171 * Add channel to the end of the client invite list
1173 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1175 inv->value.chptr = chptr;
1178 (cli_user(cptr))->invites++;
1182 * Delete Invite block from channel invite list and client invite list
1184 void del_invite(struct Client *cptr, struct Channel *chptr)
1186 struct SLink **inv, *tmp;
1188 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1189 if (tmp->value.cptr == cptr)
1194 (cli_user(cptr))->invites--;
1198 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1199 if (tmp->value.chptr == chptr)
1208 /* List and skip all channels that are listen */
1209 void list_next_channels(struct Client *cptr, int nr)
1211 struct ListingArgs *args = cli_listing(cptr);
1212 struct Channel *chptr = args->chptr;
1213 chptr->mode.mode &= ~MODE_LISTED;
1214 while (is_listed(chptr) || --nr >= 0)
1216 for (; chptr; chptr = chptr->next)
1218 if (!cli_user(cptr) || (SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
1220 if (chptr->users > args->min_users && chptr->users < args->max_users &&
1221 chptr->creationtime > args->min_time &&
1222 chptr->creationtime < args->max_time &&
1223 (!args->topic_limits || (*chptr->topic &&
1224 chptr->topic_time > args->min_topic_time &&
1225 chptr->topic_time < args->max_topic_time)))
1227 if (ShowChannel(cptr,chptr))
1228 send_reply(cptr, RPL_LIST, chptr->chname, chptr->users,
1230 chptr = chptr->next;
1236 MyFree(cli_listing(cptr));
1237 cli_listing(cptr) = NULL;
1238 send_reply(cptr, RPL_LISTEND);
1244 (cli_listing(cptr))->chptr = chptr;
1245 chptr->mode.mode |= MODE_LISTED;
1258 * X --a--> A --b--> B --d--> D
1262 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1263 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1265 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1266 * Remove the user immedeately when no users are left on the channel.
1267 * b) On server B : remove the user (who/lp) from the channel, send a
1268 * PART upstream (to A) and pass on the KICK.
1269 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1270 * channel, and pass on the KICK.
1271 * d) On server D : remove the user (who/lp) from the channel, and pass on
1275 * - Setting the ZOMBIE flag never hurts, we either remove the
1276 * client after that or we don't.
1277 * - The KICK message was already passed on, as should be in all cases.
1278 * - `who' is removed in all cases except case a) when users are left.
1279 * - A PART is only sent upstream in case b).
1285 * 1 --- 2 --- 3 --- 4 --- 5
1289 * We also need to turn 'who' into a zombie on servers 1 and 6,
1290 * because a KICK from 'who' (kicking someone else in that direction)
1291 * can arrive there afterwards - which should not be bounced itself.
1292 * Therefore case a) also applies for servers 1 and 6.
1296 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
1297 struct Client* sptr, struct Channel* chptr)
1299 assert(0 != member);
1304 /* Default for case a): */
1307 /* Case b) or c) ?: */
1308 if (MyUser(who)) /* server 4 */
1310 if (IsServer(cptr)) /* Case b) ? */
1311 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1312 remove_user_from_channel(who, chptr);
1315 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1317 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1318 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1319 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1321 remove_user_from_channel(who, chptr);
1326 /* Case a) (servers 1, 2, 3 and 6) */
1327 if (channel_all_zombies(chptr))
1328 remove_user_from_channel(who, chptr);
1330 /* XXX Can't actually call Debug here; if the channel is all zombies,
1331 * chptr will no longer exist when we get here.
1332 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1336 int number_of_zombies(struct Channel *chptr)
1338 struct Membership* member;
1342 for (member = chptr->members; member; member = member->next_member) {
1343 if (IsZombie(member))
1350 * This helper function builds an argument string in strptr, consisting
1351 * of the original string, a space, and str1 and str2 concatenated (if,
1352 * of course, str2 is not NULL)
1355 build_string(char *strptr, int *strptr_i, char *str1, char *str2, char c)
1358 strptr[(*strptr_i)++] = c;
1361 strptr[(*strptr_i)++] = *(str1++);
1365 strptr[(*strptr_i)++] = *(str2++);
1367 strptr[(*strptr_i)] = '\0';
1371 * This is the workhorse of our ModeBuf suite; this actually generates the
1372 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1375 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1377 /* we only need the flags that don't take args right now */
1378 static int flags[] = {
1379 /* MODE_CHANOP, 'o', */
1380 /* MODE_VOICE, 'v', */
1383 MODE_MODERATED, 'm',
1384 MODE_TOPICLIMIT, 't',
1385 MODE_INVITEONLY, 'i',
1386 MODE_NOPRIVMSGS, 'n',
1387 /* MODE_KEY, 'k', */
1388 /* MODE_BAN, 'b', */
1389 /* MODE_LIMIT, 'l', */
1395 struct Client *app_source; /* where the MODE appears to come from */
1397 char addbuf[20]; /* accumulates +psmtin, etc. */
1399 char rembuf[20]; /* accumulates -psmtin, etc. */
1401 char *bufptr; /* we make use of indirection to simplify the code */
1404 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1406 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1408 char *strptr; /* more indirection to simplify the code */
1411 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1414 char limitbuf[20]; /* convert limits to strings */
1416 unsigned int limitdel = MODE_LIMIT;
1420 /* If the ModeBuf is empty, we have nothing to do */
1421 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1424 /* Ok, if we were given the OPMODE flag, hide the source if its a user */
1425 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
1428 app_source = mbuf->mb_source;
1431 * Account for user we're bouncing; we have to get it in on the first
1432 * bounced MODE, or we could have problems
1434 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1435 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1437 /* Calculate the simple flags */
1438 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1439 if (*flag_p & mbuf->mb_add)
1440 addbuf[addbuf_i++] = flag_p[1];
1441 else if (*flag_p & mbuf->mb_rem)
1442 rembuf[rembuf_i++] = flag_p[1];
1445 /* Now go through the modes with arguments... */
1446 for (i = 0; i < mbuf->mb_count; i++) {
1447 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1449 bufptr_i = &addbuf_i;
1452 bufptr_i = &rembuf_i;
1455 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1456 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1458 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
1459 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1461 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1462 totalbuflen -= IRCD_MAX(5, tmp) + 1;
1464 } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN)) {
1465 tmp = strlen(MB_STRING(mbuf, i));
1467 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1468 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1470 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_KEY ? 'k' : 'b';
1471 totalbuflen -= tmp + 1;
1473 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1474 /* if it's a limit, we also format the number */
1475 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%d", MB_UINT(mbuf, i));
1477 tmp = strlen(limitbuf);
1479 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1480 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1482 bufptr[(*bufptr_i)++] = 'l';
1483 totalbuflen -= tmp + 1;
1488 /* terminate the mode strings */
1489 addbuf[addbuf_i] = '\0';
1490 rembuf[rembuf_i] = '\0';
1492 /* If we're building a user visible MODE or HACK... */
1493 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1494 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1495 MODEBUF_DEST_LOG)) {
1496 /* Set up the parameter strings */
1502 for (i = 0; i < mbuf->mb_count; i++) {
1503 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1506 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1508 strptr_i = &addstr_i;
1511 strptr_i = &remstr_i;
1514 /* deal with clients... */
1515 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1516 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1518 /* deal with strings... */
1519 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1520 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1523 * deal with limit; note we cannot include the limit parameter if we're
1526 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1527 (MODE_ADD | MODE_LIMIT))
1528 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1531 /* send the messages off to their destination */
1532 if (mbuf->mb_dest & MODEBUF_DEST_HACK2) {
1533 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1535 #ifdef HEAD_IN_SAND_SNOTICES
1536 cli_name(mbuf->mb_source),
1538 cli_name(app_source),
1540 mbuf->mb_channel->chname,
1541 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1542 addbuf, remstr, addstr,
1543 mbuf->mb_channel->creationtime);
1544 sendcmdto_serv_butone(&me, CMD_DESYNCH, mbuf->mb_connect,
1545 ":HACK: %s MODE %s %s%s%s%s%s%s [%Tu]",
1546 #ifdef HEAD_IN_SAND_SNOTICES
1547 cli_name(mbuf->mb_source),
1549 cli_name(app_source),
1551 mbuf->mb_channel->chname,
1552 rembuf_i ? "-" : "", rembuf,
1553 addbuf_i ? "+" : "", addbuf, remstr, addstr,
1554 mbuf->mb_channel->creationtime);
1557 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1558 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1559 "%s%s%s%s%s%s [%Tu]",
1560 #ifdef HEAD_IN_SAND_SNOTICES
1561 cli_name(mbuf->mb_source),
1563 cli_name(app_source),
1565 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1566 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1567 mbuf->mb_channel->creationtime);
1569 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1570 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1572 #ifdef HEAD_IN_SAND_SNOTICES
1573 cli_name(mbuf->mb_source),
1575 cli_name(app_source),
1577 mbuf->mb_channel->chname,
1578 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1579 addbuf, remstr, addstr,
1580 mbuf->mb_channel->creationtime);
1582 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1583 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1584 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1585 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1586 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1588 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1589 sendcmdto_channel_butserv(app_source, CMD_MODE, mbuf->mb_channel,
1590 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1591 rembuf_i ? "-" : "", rembuf,
1592 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1595 /* Now are we supposed to propagate to other servers? */
1596 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1597 /* set up parameter string */
1604 * limit is supressed if we're removing it; we have to figure out which
1605 * direction is the direction for it to be removed, though...
1607 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1609 for (i = 0; i < mbuf->mb_count; i++) {
1610 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1613 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1615 strptr_i = &addstr_i;
1618 strptr_i = &remstr_i;
1621 /* deal with modes that take clients */
1622 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1623 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1625 /* deal with modes that take strings */
1626 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1627 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1630 * deal with the limit. Logic here is complicated; if HACK2 is set,
1631 * we're bouncing the mode, so sense is reversed, and we have to
1632 * include the original limit if it looks like it's being removed
1634 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1635 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1638 /* we were told to deop the source */
1639 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1640 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1641 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1642 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1644 /* mark that we've done this, so we don't do it again */
1645 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1648 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1649 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1650 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1651 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1652 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1653 addbuf, remstr, addstr);
1654 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1656 * If HACK2 was set, we're bouncing; we send the MODE back to the
1657 * connection we got it from with the senses reversed and a TS of 0;
1660 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1661 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1662 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1663 mbuf->mb_channel->creationtime);
1666 * We're propagating a normal MODE command to the rest of the network;
1667 * we send the actual channel TS unless this is a HACK3 or a HACK4
1669 if (IsServer(mbuf->mb_source))
1670 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1671 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1672 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1673 addbuf, remstr, addstr,
1674 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1675 mbuf->mb_channel->creationtime);
1677 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1678 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1679 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1680 addbuf, remstr, addstr);
1684 /* We've drained the ModeBuf... */
1689 /* reinitialize the mode-with-arg slots */
1690 for (i = 0; i < MAXMODEPARAMS; i++) {
1691 /* If we saved any, pack them down */
1692 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1693 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1694 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1696 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1698 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1699 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1701 MB_TYPE(mbuf, i) = 0;
1702 MB_UINT(mbuf, i) = 0;
1705 /* If we're supposed to flush it all, do so--all hail tail recursion */
1706 if (all && mbuf->mb_count)
1707 return modebuf_flush_int(mbuf, 1);
1713 * This routine just initializes a ModeBuf structure with the information
1714 * needed and the options given.
1717 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1718 struct Client *connect, struct Channel *chan, unsigned int dest)
1723 assert(0 != source);
1729 mbuf->mb_source = source;
1730 mbuf->mb_connect = connect;
1731 mbuf->mb_channel = chan;
1732 mbuf->mb_dest = dest;
1735 /* clear each mode-with-parameter slot */
1736 for (i = 0; i < MAXMODEPARAMS; i++) {
1737 MB_TYPE(mbuf, i) = 0;
1738 MB_UINT(mbuf, i) = 0;
1743 * This routine simply adds modes to be added or deleted; do a binary OR
1744 * with either MODE_ADD or MODE_DEL
1747 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1750 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1752 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1753 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
1755 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1758 if (mode & MODE_ADD) {
1759 mbuf->mb_rem &= ~mode;
1760 mbuf->mb_add |= mode;
1762 mbuf->mb_add &= ~mode;
1763 mbuf->mb_rem |= mode;
1768 * This routine adds a mode to be added or deleted that takes a unsigned
1769 * int parameter; mode may *only* be the relevant mode flag ORed with one
1770 * of MODE_ADD or MODE_DEL
1773 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1776 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1778 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1779 MB_UINT(mbuf, mbuf->mb_count) = uint;
1781 /* when we've reached the maximal count, flush the buffer */
1782 if (++mbuf->mb_count >=
1783 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1784 modebuf_flush_int(mbuf, 0);
1788 * This routine adds a mode to be added or deleted that takes a string
1789 * parameter; mode may *only* be the relevant mode flag ORed with one of
1790 * MODE_ADD or MODE_DEL
1793 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
1797 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1799 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
1800 MB_STRING(mbuf, mbuf->mb_count) = string;
1802 /* when we've reached the maximal count, flush the buffer */
1803 if (++mbuf->mb_count >=
1804 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1805 modebuf_flush_int(mbuf, 0);
1809 * This routine adds a mode to be added or deleted that takes a client
1810 * parameter; mode may *only* be the relevant mode flag ORed with one of
1811 * MODE_ADD or MODE_DEL
1814 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
1815 struct Client *client)
1818 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1820 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1821 MB_CLIENT(mbuf, mbuf->mb_count) = client;
1823 /* when we've reached the maximal count, flush the buffer */
1824 if (++mbuf->mb_count >=
1825 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1826 modebuf_flush_int(mbuf, 0);
1830 * This is the exported binding for modebuf_flush()
1833 modebuf_flush(struct ModeBuf *mbuf)
1835 return modebuf_flush_int(mbuf, 1);
1839 * This extracts the simple modes contained in mbuf
1842 modebuf_extract(struct ModeBuf *mbuf, char *buf)
1844 static int flags[] = {
1845 /* MODE_CHANOP, 'o', */
1846 /* MODE_VOICE, 'v', */
1849 MODE_MODERATED, 'm',
1850 MODE_TOPICLIMIT, 't',
1851 MODE_INVITEONLY, 'i',
1852 MODE_NOPRIVMSGS, 'n',
1854 /* MODE_BAN, 'b', */
1859 int i, bufpos = 0, len;
1861 char *key = 0, limitbuf[20];
1870 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
1871 if (MB_TYPE(mbuf, i) & MODE_ADD) {
1872 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT);
1874 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
1875 key = MB_STRING(mbuf, i);
1876 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
1877 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%d", MB_UINT(mbuf, i));
1884 buf[bufpos++] = '+'; /* start building buffer */
1886 for (flag_p = flags; flag_p[0]; flag_p += 2)
1888 buf[bufpos++] = flag_p[1];
1890 for (i = 0, len = bufpos; i < len; i++) {
1892 build_string(buf, &bufpos, key, 0, ' ');
1893 else if (buf[i] == 'l')
1894 build_string(buf, &bufpos, limitbuf, 0, ' ');
1903 * Simple function to invalidate bans
1906 mode_ban_invalidate(struct Channel *chan)
1908 struct Membership *member;
1910 for (member = chan->members; member; member = member->next_member)
1911 ClearBanValid(member);
1915 * Simple function to drop invite structures
1918 mode_invite_clear(struct Channel *chan)
1920 while (chan->invites)
1921 del_invite(chan->invites->value.cptr, chan);
1924 /* What we've done for mode_parse so far... */
1925 #define DONE_LIMIT 0x01 /* We've set the limit */
1926 #define DONE_KEY 0x02 /* We've set the key */
1927 #define DONE_BANLIST 0x04 /* We've sent the ban list */
1928 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
1929 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
1932 struct ModeBuf *mbuf;
1933 struct Client *cptr;
1934 struct Client *sptr;
1935 struct Channel *chptr;
1946 struct SLink banlist[MAXPARA];
1949 struct Client *client;
1950 } cli_change[MAXPARA];
1954 * Here's a helper function to deal with sending along "Not oper" or
1955 * "Not member" messages
1958 send_notoper(struct ParseState *state)
1960 if (state->done & DONE_NOTOPER)
1963 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
1964 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
1966 state->done |= DONE_NOTOPER;
1970 * Helper function to convert limits
1973 mode_parse_limit(struct ParseState *state, int *flag_p)
1975 unsigned int t_limit;
1977 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
1978 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
1981 if (state->parc <= 0) { /* warn if not enough args */
1982 if (MyUser(state->sptr))
1983 need_more_params(state->sptr, "MODE +l");
1987 t_limit = atoi(state->parv[state->args_used++]); /* grab arg */
1991 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
1992 (!t_limit || t_limit == state->chptr->mode.limit))
1995 t_limit = state->chptr->mode.limit;
1997 /* If they're not an oper, they can't change modes */
1998 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
1999 send_notoper(state);
2003 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
2005 state->done |= DONE_LIMIT;
2010 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
2012 if (state->flags & MODE_PARSE_SET) { /* set the limit */
2013 if (state->dir & MODE_ADD) {
2014 state->chptr->mode.mode |= flag_p[0];
2015 state->chptr->mode.limit = t_limit;
2017 state->chptr->mode.mode &= ~flag_p[0];
2018 state->chptr->mode.limit = 0;
2024 * Helper function to convert keys
2027 mode_parse_key(struct ParseState *state, int *flag_p)
2032 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2035 if (state->parc <= 0) { /* warn if not enough args */
2036 if (MyUser(state->sptr))
2037 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2042 t_str = state->parv[state->args_used++]; /* grab arg */
2046 /* If they're not an oper, they can't change modes */
2047 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2048 send_notoper(state);
2052 if (state->done & DONE_KEY) /* allow key to be set only once */
2054 state->done |= DONE_KEY;
2058 /* clean up the key string */
2060 while (*++s > ' ' && *s != ':' && --t_len)
2064 if (!*t_str) { /* warn if empty */
2065 if (MyUser(state->sptr))
2066 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2074 /* can't add a key if one is set, nor can one remove the wrong key */
2075 if (!(state->flags & MODE_PARSE_FORCE))
2076 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2077 (state->dir == MODE_DEL &&
2078 ircd_strcmp(state->chptr->mode.key, t_str))) {
2079 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2083 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2084 !ircd_strcmp(state->chptr->mode.key, t_str))
2085 return; /* no key change */
2087 if (state->flags & MODE_PARSE_BOUNCE) {
2088 if (*state->chptr->mode.key) /* reset old key */
2089 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2090 state->chptr->mode.key, 0);
2091 else /* remove new bogus key */
2092 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2093 } else /* send new key */
2094 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2096 if (state->flags & MODE_PARSE_SET) {
2097 if (state->dir == MODE_ADD) /* set the new key */
2098 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2099 else /* remove the old key */
2100 *state->chptr->mode.key = '\0';
2105 * Helper function to convert bans
2108 mode_parse_ban(struct ParseState *state, int *flag_p)
2111 struct SLink *ban, *newban = 0;
2113 if (state->parc <= 0) { /* Not enough args, send ban list */
2114 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2115 send_ban_list(state->sptr, state->chptr);
2116 state->done |= DONE_BANLIST;
2122 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2125 t_str = state->parv[state->args_used++]; /* grab arg */
2129 /* If they're not an oper, they can't change modes */
2130 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2131 send_notoper(state);
2135 if ((s = strchr(t_str, ' ')))
2138 if (!*t_str || *t_str == ':') { /* warn if empty */
2139 if (MyUser(state->sptr))
2140 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2145 t_str = collapse(pretty_mask(t_str));
2147 /* remember the ban for the moment... */
2148 if (state->dir == MODE_ADD) {
2149 newban = state->banlist + (state->numbans++);
2152 DupString(newban->value.ban.banstr, t_str);
2153 newban->value.ban.who = cli_name(state->sptr);
2154 newban->value.ban.when = TStime();
2156 newban->flags = CHFL_BAN | MODE_ADD;
2158 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
2159 newban->flags |= CHFL_BAN_IPMASK;
2162 if (!state->chptr->banlist) {
2163 state->chptr->banlist = newban; /* add our ban with its flags */
2164 state->done |= DONE_BANCLEAN;
2168 /* Go through all bans */
2169 for (ban = state->chptr->banlist; ban; ban = ban->next) {
2170 /* first, clean the ban flags up a bit */
2171 if (!(state->done & DONE_BANCLEAN))
2172 /* Note: We're overloading *lots* of bits here; be careful! */
2173 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
2177 * MODE_ADD - Ban was added; if we're bouncing modes,
2178 * then we'll remove it below; otherwise,
2179 * we'll have to allocate a real ban
2181 * MODE_DEL - Ban was marked for deletion; if we're
2182 * bouncing modes, we'll have to re-add it,
2183 * otherwise, we'll have to remove it
2185 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
2186 * with a ban already set; if we're
2187 * bouncing modes, we'll have to bounce
2188 * this one; otherwise, we'll just ignore
2189 * it when we process added bans
2192 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
2193 ban->flags |= MODE_DEL; /* delete one ban */
2195 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2197 } else if (state->dir == MODE_ADD) {
2198 /* if the ban already exists, don't worry about it */
2199 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
2200 newban->flags &= ~MODE_ADD; /* don't add ban at all */
2201 MyFree(newban->value.ban.banstr); /* stopper a leak */
2202 state->numbans--; /* deallocate last ban */
2203 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2205 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
2206 if (!(ban->flags & MODE_DEL))
2207 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
2208 } else if (!mmatch(t_str, ban->value.ban.banstr))
2209 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
2211 if (!ban->next && (newban->flags & MODE_ADD)) {
2212 ban->next = newban; /* add our ban with its flags */
2213 break; /* get out of loop */
2217 state->done |= DONE_BANCLEAN;
2221 * This is the bottom half of the ban processor
2224 mode_process_bans(struct ParseState *state)
2226 struct SLink *ban, *newban, *prevban, *nextban;
2232 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2234 banlen = strlen(ban->value.ban.banstr);
2236 nextban = ban->next;
2238 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
2240 prevban->next = 0; /* Break the list; ban isn't a real ban */
2242 state->chptr->banlist = 0;
2247 MyFree(ban->value.ban.banstr);
2250 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
2251 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2252 ban->value.ban.banstr,
2253 state->flags & MODE_PARSE_SET);
2255 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2256 if (prevban) /* clip it out of the list... */
2257 prevban->next = ban->next;
2259 state->chptr->banlist = ban->next;
2264 MyFree(ban->value.ban.who);
2268 continue; /* next ban; keep prevban like it is */
2270 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
2271 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
2273 prevban->next = 0; /* Break the list; ban isn't a real ban */
2275 state->chptr->banlist = 0;
2277 /* If we're supposed to ignore it, do so. */
2278 if (ban->flags & CHFL_BAN_OVERLAPPED &&
2279 !(state->flags & MODE_PARSE_BOUNCE)) {
2283 MyFree(ban->value.ban.banstr);
2285 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2286 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2287 count >= feature_int(FEAT_MAXBANS))) {
2288 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2289 ban->value.ban.banstr);
2293 MyFree(ban->value.ban.banstr);
2295 /* add the ban to the buffer */
2296 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2297 ban->value.ban.banstr,
2298 !(state->flags & MODE_PARSE_SET));
2300 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2301 newban = make_link();
2302 newban->value.ban.banstr = ban->value.ban.banstr;
2303 DupString(newban->value.ban.who, ban->value.ban.who);
2304 newban->value.ban.when = ban->value.ban.when;
2305 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
2307 newban->next = state->chptr->banlist; /* and link it in */
2308 state->chptr->banlist = newban;
2317 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2319 if (changed) /* if we changed the ban list, we must invalidate the bans */
2320 mode_ban_invalidate(state->chptr);
2324 * Helper function to process client changes
2327 mode_parse_client(struct ParseState *state, int *flag_p)
2330 struct Client *acptr;
2333 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2336 if (state->parc <= 0) /* return if not enough args */
2339 t_str = state->parv[state->args_used++]; /* grab arg */
2343 /* If they're not an oper, they can't change modes */
2344 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2345 send_notoper(state);
2349 if (MyUser(state->sptr)) /* find client we're manipulating */
2350 acptr = find_chasing(state->sptr, t_str, NULL);
2352 acptr = findNUser(t_str);
2355 return; /* find_chasing() already reported an error to the user */
2357 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2358 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2359 state->cli_change[i].flag & flag_p[0]))
2360 break; /* found a slot */
2362 /* Store what we're doing to them */
2363 state->cli_change[i].flag = state->dir | flag_p[0];
2364 state->cli_change[i].client = acptr;
2368 * Helper function to process the changed client list
2371 mode_process_clients(struct ParseState *state)
2374 struct Membership *member;
2376 for (i = 0; state->cli_change[i].flag; i++) {
2377 assert(0 != state->cli_change[i].client);
2379 /* look up member link */
2380 if (!(member = find_member_link(state->chptr,
2381 state->cli_change[i].client)) ||
2382 (MyUser(state->sptr) && IsZombie(member))) {
2383 if (MyUser(state->sptr))
2384 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2385 cli_name(state->cli_change[i].client),
2386 state->chptr->chname);
2390 if ((state->cli_change[i].flag & MODE_ADD &&
2391 (state->cli_change[i].flag & member->status)) ||
2392 (state->cli_change[i].flag & MODE_DEL &&
2393 !(state->cli_change[i].flag & member->status)))
2394 continue; /* no change made, don't do anything */
2396 /* see if the deop is allowed */
2397 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2398 (MODE_DEL | MODE_CHANOP)) {
2399 /* prevent +k users from being deopped */
2400 if (IsChannelService(state->cli_change[i].client)) {
2401 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
2402 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
2404 (IsServer(state->sptr) ? cli_name(state->sptr) :
2405 cli_name((cli_user(state->sptr))->server)));
2407 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
2408 send_reply(state->sptr, ERR_ISCHANSERVICE,
2409 cli_name(state->cli_change[i].client),
2410 state->chptr->chname);
2415 /* don't allow local opers to be deopped on local channels */
2416 if (MyUser(state->sptr) && state->cli_change[i].client != state->sptr &&
2417 IsLocalChannel(state->chptr->chname) &&
2418 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
2419 send_reply(state->sptr, ERR_ISOPERLCHAN,
2420 cli_name(state->cli_change[i].client),
2421 state->chptr->chname);
2426 /* accumulate the change */
2427 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
2428 state->cli_change[i].client);
2430 /* actually effect the change */
2431 if (state->flags & MODE_PARSE_SET) {
2432 if (state->cli_change[i].flag & MODE_ADD) {
2433 member->status |= (state->cli_change[i].flag &
2434 (MODE_CHANOP | MODE_VOICE));
2435 if (state->cli_change[i].flag & MODE_CHANOP)
2436 ClearDeopped(member);
2438 member->status &= ~(state->cli_change[i].flag &
2439 (MODE_CHANOP | MODE_VOICE));
2441 } /* for (i = 0; state->cli_change[i].flags; i++) { */
2445 * Helper function to process the simple modes
2448 mode_parse_mode(struct ParseState *state, int *flag_p)
2450 /* If they're not an oper, they can't change modes */
2451 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2452 send_notoper(state);
2459 if (state->dir == MODE_ADD) {
2460 state->add |= flag_p[0];
2461 state->del &= ~flag_p[0];
2463 if (flag_p[0] & MODE_SECRET) {
2464 state->add &= ~MODE_PRIVATE;
2465 state->del |= MODE_PRIVATE;
2466 } else if (flag_p[0] & MODE_PRIVATE) {
2467 state->add &= ~MODE_SECRET;
2468 state->del |= MODE_SECRET;
2471 state->add &= ~flag_p[0];
2472 state->del |= flag_p[0];
2475 assert(0 == (state->add & state->del));
2476 assert((MODE_SECRET | MODE_PRIVATE) !=
2477 (state->add & (MODE_SECRET | MODE_PRIVATE)));
2481 * This routine is intended to parse MODE or OPMODE commands and effect the
2482 * changes (or just build the bounce buffer). We pass the starting offset
2486 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
2487 struct Channel *chptr, int parc, char *parv[], unsigned int flags)
2489 static int chan_flags[] = {
2494 MODE_MODERATED, 'm',
2495 MODE_TOPICLIMIT, 't',
2496 MODE_INVITEONLY, 'i',
2497 MODE_NOPRIVMSGS, 'n',
2507 unsigned int t_mode;
2509 struct ParseState state;
2520 state.chptr = chptr;
2523 state.flags = flags;
2524 state.dir = MODE_ADD;
2528 state.args_used = 0;
2529 state.max_args = MAXMODEPARAMS;
2532 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
2533 state.banlist[i].next = 0;
2534 state.banlist[i].value.ban.banstr = 0;
2535 state.banlist[i].value.ban.who = 0;
2536 state.banlist[i].value.ban.when = 0;
2537 state.banlist[i].flags = 0;
2538 state.cli_change[i].flag = 0;
2539 state.cli_change[i].client = 0;
2542 modestr = state.parv[state.args_used++];
2546 for (; *modestr; modestr++) {
2547 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
2548 if (flag_p[1] == *modestr)
2551 if (!flag_p[0]) { /* didn't find it? complain and continue */
2552 if (MyUser(state.sptr))
2553 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
2558 case '+': /* switch direction to MODE_ADD */
2559 case '-': /* switch direction to MODE_DEL */
2560 state.dir = flag_p[0];
2563 case 'l': /* deal with limits */
2564 mode_parse_limit(&state, flag_p);
2567 case 'k': /* deal with keys */
2568 mode_parse_key(&state, flag_p);
2571 case 'b': /* deal with bans */
2572 mode_parse_ban(&state, flag_p);
2575 case 'o': /* deal with ops/voice */
2577 mode_parse_client(&state, flag_p);
2580 default: /* deal with other modes */
2581 mode_parse_mode(&state, flag_p);
2583 } /* switch (*modestr) { */
2584 } /* for (; *modestr; modestr++) { */
2586 if (state.flags & MODE_PARSE_BURST)
2587 break; /* don't interpret any more arguments */
2589 if (state.parc > 0) { /* process next argument in string */
2590 modestr = state.parv[state.args_used++];
2594 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
2597 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
2598 break; /* we're then going to bounce the mode! */
2600 recv_ts = atoi(modestr);
2602 if (recv_ts && recv_ts < state.chptr->creationtime)
2603 state.chptr->creationtime = recv_ts; /* respect earlier TS */
2605 break; /* break out of while loop */
2606 } else if (state.flags & MODE_PARSE_STRICT ||
2607 (MyUser(state.sptr) && state.max_args <= 0)) {
2608 state.parc++; /* we didn't actually gobble the argument */
2610 break; /* break out of while loop */
2613 } /* while (*modestr) { */
2616 * the rest of the function finishes building resultant MODEs; if the
2617 * origin isn't a member or an oper, skip it.
2619 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
2620 return state.args_used; /* tell our parent how many args we gobbled */
2622 t_mode = state.chptr->mode.mode;
2624 if (state.del & t_mode) { /* delete any modes to be deleted... */
2625 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
2627 t_mode &= ~state.del;
2629 if (state.add & ~t_mode) { /* add any modes to be added... */
2630 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
2632 t_mode |= state.add;
2635 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
2636 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
2637 !(t_mode & MODE_INVITEONLY))
2638 mode_invite_clear(state.chptr);
2640 state.chptr->mode.mode = t_mode;
2643 if (state.flags & MODE_PARSE_WIPEOUT) {
2644 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
2645 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
2646 state.chptr->mode.limit);
2647 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
2648 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
2649 state.chptr->mode.key, 0);
2652 if (state.done & DONE_BANCLEAN) /* process bans */
2653 mode_process_bans(&state);
2655 /* process client changes */
2656 if (state.cli_change[0].flag)
2657 mode_process_clients(&state);
2659 return state.args_used; /* tell our parent how many args we gobbled */
2663 * Initialize a join buffer
2666 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
2667 struct Client *connect, unsigned int type, char *comment,
2673 assert(0 != source);
2674 assert(0 != connect);
2676 jbuf->jb_source = source; /* just initialize struct JoinBuf */
2677 jbuf->jb_connect = connect;
2678 jbuf->jb_type = type;
2679 jbuf->jb_comment = comment;
2680 jbuf->jb_create = create;
2682 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
2683 type == JOINBUF_TYPE_PART ||
2684 type == JOINBUF_TYPE_PARTALL) ?
2685 STARTJOINLEN : STARTCREATELEN) +
2686 (comment ? strlen(comment) + 2 : 0));
2688 for (i = 0; i < MAXJOINARGS; i++)
2689 jbuf->jb_channels[i] = 0;
2693 * Add a channel to the join buffer
2696 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
2703 if (jbuf->jb_type == JOINBUF_TYPE_JOIN)
2704 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
2709 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
2710 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
2711 /* Send notification to channel */
2712 if (!(flags & CHFL_ZOMBIE))
2713 sendcmdto_channel_butserv(jbuf->jb_source, CMD_PART, chan,
2714 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2715 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2716 else if (MyUser(jbuf->jb_source))
2717 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
2718 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2719 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2720 /* XXX: Shouldn't we send a PART here anyway? */
2721 /* to users on the channel? Why? From their POV, the user isn't on
2722 * the channel anymore anyway. We don't send to servers until below,
2723 * when we gang all the channel parts together. Note that this is
2724 * exactly the same logic, albeit somewhat more concise, as was in
2725 * the original m_part.c */
2727 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2728 IsLocalChannel(chan->chname)) /* got to remove user here */
2729 remove_user_from_channel(jbuf->jb_source, chan);
2731 /* Add user to channel */
2732 add_user_to_channel(chan, jbuf->jb_source, flags);
2734 /* send notification to all servers */
2735 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !IsLocalChannel(chan->chname))
2736 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
2737 "%H %Tu", chan, chan->creationtime);
2739 /* Send the notification to the channel */
2740 sendcmdto_channel_butserv(jbuf->jb_source, CMD_JOIN, chan, ":%H", chan);
2742 /* send an op, too, if needed */
2743 if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE &&
2744 !IsModelessChannel(chan->chname))
2745 sendcmdto_channel_butserv(jbuf->jb_source, CMD_MODE, chan, "%H +o %C",
2746 chan, jbuf->jb_source);
2749 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || IsLocalChannel(chan->chname))
2750 return; /* don't send to remote */
2752 /* figure out if channel name will cause buffer to be overflowed */
2753 len = chan ? strlen(chan->chname) + 1 : 2;
2754 if (jbuf->jb_strlen + len > BUFSIZE)
2755 joinbuf_flush(jbuf);
2757 /* add channel to list of channels to send and update counts */
2758 jbuf->jb_channels[jbuf->jb_count++] = chan;
2759 jbuf->jb_strlen += len;
2761 /* if we've used up all slots, flush */
2762 if (jbuf->jb_count >= MAXJOINARGS)
2763 joinbuf_flush(jbuf);
2767 * Flush the channel list to remote servers
2770 joinbuf_flush(struct JoinBuf *jbuf)
2772 char chanlist[BUFSIZE];
2776 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2777 jbuf->jb_type == JOINBUF_TYPE_JOIN)
2778 return 0; /* no joins to process */
2780 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
2781 build_string(chanlist, &chanlist_i,
2782 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
2783 i == 0 ? '\0' : ',');
2784 if (JOINBUF_TYPE_PART == jbuf->jb_type)
2785 /* Remove user from channel */
2786 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
2788 jbuf->jb_channels[i] = 0; /* mark slot empty */
2791 jbuf->jb_count = 0; /* reset base counters */
2792 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
2793 STARTJOINLEN : STARTCREATELEN) +
2794 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
2796 /* and send the appropriate command */
2797 switch (jbuf->jb_type) {
2798 case JOINBUF_TYPE_CREATE:
2799 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
2800 "%s %Tu", chanlist, jbuf->jb_create);
2803 case JOINBUF_TYPE_PART:
2804 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
2805 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,