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 "ircd_alloc.h"
27 #include "ircd_chattr.h"
28 #include "ircd_defs.h"
29 #include "ircd_features.h"
31 #include "ircd_reply.h"
32 #include "ircd_snprintf.h"
33 #include "ircd_string.h"
40 #include "querycmds.h"
47 #include "sprintf_irc.h"
58 struct Channel* GlobalChannelList = 0;
60 static unsigned int membershipAllocCount;
61 static struct Membership* membershipFreeList;
63 static struct SLink *next_overlapped_ban(void);
64 static int del_banid(struct Channel *, char *, int);
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 static char *make_nick_user_host(const char *nick, const char *name,
168 static char namebuf[NICKLEN + USERLEN + HOSTLEN + 3];
169 sprintf_irc(namebuf, "%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 static char *make_nick_user_ip(char *nick, char *name, struct in_addr ip)
179 static char ipbuf[NICKLEN + USERLEN + 16 + 3];
180 sprintf_irc(ipbuf, "%s!%s@%s", nick, name, ircd_ntoa((const char*) &ip));
185 * Subtract one user from channel i (and free channel
186 * block, if channel became empty).
187 * Returns: true (1) if channel still exists
188 * false (0) if the channel was destroyed
190 int sub1_from_channel(struct Channel* chptr)
195 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
197 assert(0 != chptr->members);
202 assert(0 == chptr->members);
204 /* Channel became (or was) empty: Remove channel */
205 if (is_listed(chptr))
208 for (i = 0; i <= HighestFd; i++)
210 struct Client *acptr = 0;
211 if ((acptr = LocalClientArray[i]) && cli_listing(acptr) &&
212 (cli_listing(acptr))->chptr == chptr)
214 list_next_channels(acptr, 1);
215 break; /* Only one client can list a channel */
220 * Now, find all invite links from channel structure
222 while ((tmp = chptr->invites))
223 del_invite(tmp->value.cptr, chptr);
225 tmp = chptr->banlist;
230 MyFree(obtmp->value.ban.banstr);
231 MyFree(obtmp->value.ban.who);
235 chptr->prev->next = chptr->next;
237 GlobalChannelList = chptr->next;
239 chptr->next->prev = chptr->prev;
241 --UserStats.channels;
243 * make sure that channel actually got removed from hash table
245 assert(chptr->hnext == chptr);
253 * `cptr' must be the client adding the ban.
255 * If `change' is true then add `banid' to channel `chptr'.
256 * Returns 0 if the ban was added.
257 * Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
258 * Return -1 otherwise.
260 * Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
261 * when `change' is false, otherwise they will be removed from the banlist.
262 * Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
263 * respectively will return these bans until NULL is returned.
265 * If `firsttime' is true, the ban list as returned by next_overlapped_ban()
266 * is reset (unless a non-zero value is returned, in which case the
267 * CHFL_BAN_OVERLAPPED flag might not have been reset!).
271 int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
272 int change, int firsttime)
277 int removed_bans = 0;
278 int len = strlen(banid);
283 assert(0 == prev_ban);
284 assert(0 == removed_bans_list);
288 for (banp = &chptr->banlist; *banp;)
290 len += strlen((*banp)->value.ban.banstr);
292 if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
294 if (!strcmp((*banp)->value.ban.banstr, banid))
296 (*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
300 else if (!mmatch((*banp)->value.ban.banstr, banid))
302 if (!mmatch(banid, (*banp)->value.ban.banstr))
304 struct SLink *tmp = *banp;
310 len -= strlen(tmp->value.ban.banstr);
314 /* Silently remove overlapping bans */
315 MyFree(tmp->value.ban.banstr);
316 MyFree(tmp->value.ban.who);
320 /* These will be sent to the user later as -b */
321 tmp->next = removed_bans_list;
322 removed_bans_list = tmp;
326 else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
328 tmp->flags |= CHFL_BAN_OVERLAPPED;
339 (*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
340 banp = &(*banp)->next;
343 if (MyUser(cptr) && !removed_bans &&
344 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
345 (cnt >= feature_int(FEAT_MAXBANS))))
347 send_reply(cptr, ERR_BANLISTFULL, chptr->chname, banid);
353 struct Membership* member;
355 ban->next = chptr->banlist;
357 ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
358 assert(0 != ban->value.ban.banstr);
359 strcpy(ban->value.ban.banstr, banid);
361 ban->value.ban.who = (char*) MyMalloc(strlen(cli_name(cptr)) + 1);
362 assert(0 != ban->value.ban.who);
363 strcpy(ban->value.ban.who, cli_name(cptr));
365 ban->value.ban.when = TStime();
366 ban->flags = CHFL_BAN; /* This bit is never used I think... */
367 if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
368 ban->flags |= CHFL_BAN_IPMASK;
369 chptr->banlist = ban;
372 * Erase ban-valid-bit
374 for (member = chptr->members; member; member = member->next_member)
375 ClearBanValid(member); /* `ban' == channel member ! */
380 static struct SLink *next_overlapped_ban(void)
382 struct SLink *tmp = next_ban;
386 for (ban = tmp->next; ban; ban = ban->next)
387 if ((ban->flags & CHFL_BAN_OVERLAPPED))
394 struct SLink *next_removed_overlapped_ban(void)
396 struct SLink *tmp = removed_bans_list;
399 if (prev_ban->value.ban.banstr) /* Can be set to NULL in set_mode() */
400 MyFree(prev_ban->value.ban.banstr);
401 MyFree(prev_ban->value.ban.who);
406 removed_bans_list = removed_bans_list->next;
414 * If `change' is true, delete `banid' from channel `chptr'.
415 * Returns `false' if removal was (or would have been) successful.
417 static int del_banid(struct Channel *chptr, char *banid, int change)
424 for (ban = &(chptr->banlist); *ban; ban = &((*ban)->next)) {
425 if (0 == ircd_strcmp(banid, (*ban)->value.ban.banstr))
430 struct Membership* member;
432 MyFree(tmp->value.ban.banstr);
433 MyFree(tmp->value.ban.who);
436 * Erase ban-valid-bit, for channel members that are banned
438 for (member = chptr->members; member; member = member->next_member)
439 if (CHFL_BANVALIDMASK == (member->status & CHFL_BANVALIDMASK))
440 ClearBanValid(member); /* `tmp' == channel member */
449 * find_channel_member - returns Membership * if a person is joined and not a zombie
451 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
453 struct Membership* member;
456 member = find_member_link(chptr, cptr);
457 return (member && !IsZombie(member)) ? member : 0;
461 * is_banned - a non-zero value if banned else 0.
463 static int is_banned(struct Client *cptr, struct Channel *chptr,
464 struct Membership* member)
473 if (member && IsBanValid(member))
474 return IsBanned(member);
476 s = make_nick_user_host(cli_name(cptr), (cli_user(cptr))->username,
477 (cli_user(cptr))->host);
479 for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
480 if ((tmp->flags & CHFL_BAN_IPMASK)) {
482 ip_s = make_nick_user_ip(cli_name(cptr), (cli_user(cptr))->username,
484 if (match(tmp->value.ban.banstr, ip_s) == 0)
487 else if (match(tmp->value.ban.banstr, s) == 0)
503 return (tmp != NULL);
507 * adds a user to a channel by adding another link to the channels member
510 void add_user_to_channel(struct Channel* chptr, struct Client* who,
518 struct Membership* member = membershipFreeList;
520 membershipFreeList = member->next_member;
522 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
523 ++membershipAllocCount;
528 member->channel = chptr;
529 member->status = flags;
531 member->next_member = chptr->members;
532 if (member->next_member)
533 member->next_member->prev_member = member;
534 member->prev_member = 0;
535 chptr->members = member;
537 member->next_channel = (cli_user(who))->channel;
538 if (member->next_channel)
539 member->next_channel->prev_channel = member;
540 member->prev_channel = 0;
541 (cli_user(who))->channel = member;
544 ++((cli_user(who))->joined);
548 static int remove_member_from_channel(struct Membership* member)
550 struct Channel* chptr;
552 chptr = member->channel;
554 * unlink channel member list
556 if (member->next_member)
557 member->next_member->prev_member = member->prev_member;
558 if (member->prev_member)
559 member->prev_member->next_member = member->next_member;
561 member->channel->members = member->next_member;
564 * unlink client channel list
566 if (member->next_channel)
567 member->next_channel->prev_channel = member->prev_channel;
568 if (member->prev_channel)
569 member->prev_channel->next_channel = member->next_channel;
571 (cli_user(member->user))->channel = member->next_channel;
573 --(cli_user(member->user))->joined;
575 member->next_member = membershipFreeList;
576 membershipFreeList = member;
578 return sub1_from_channel(chptr);
581 static int channel_all_zombies(struct Channel* chptr)
583 struct Membership* member;
585 for (member = chptr->members; member; member = member->next_member) {
586 if (!IsZombie(member))
593 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
596 struct Membership* member;
599 if ((member = find_member_link(chptr, cptr))) {
600 if (remove_member_from_channel(member)) {
601 if (channel_all_zombies(chptr)) {
603 * XXX - this looks dangerous but isn't if we got the referential
604 * integrity right for channels
606 while (remove_member_from_channel(chptr->members))
613 void remove_user_from_all_channels(struct Client* cptr)
615 struct Membership* chan;
617 assert(0 != cli_user(cptr));
619 while ((chan = (cli_user(cptr))->channel))
620 remove_user_from_channel(cptr, chan->channel);
623 int is_chan_op(struct Client *cptr, struct Channel *chptr)
625 struct Membership* member;
627 if ((member = find_member_link(chptr, cptr)))
628 return (!IsZombie(member) && IsChanOp(member));
633 static int is_deopped(struct Client *cptr, struct Channel *chptr)
635 struct Membership* member;
638 if ((member = find_member_link(chptr, cptr)))
639 return IsDeopped(member);
641 return (IsUser(cptr) ? 1 : 0);
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)
670 if (IsVoicedOrOpped(member))
673 * If it's moderated, and you aren't a priviledged user, you can't
676 if (member->channel->mode.mode & MODE_MODERATED)
679 * If you're banned then you can't speak either.
680 * but because of the amount of CPU time that is_banned chews
681 * we only check it for our clients.
683 if (MyUser(member->user) && is_banned(member->user, member->channel, member))
688 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
690 struct Membership *member;
693 * Servers can always speak on channels.
698 member = find_channel_member(cptr, chptr);
701 * You can't speak if your off channel, if the channel is modeless, or
702 * +n (no external messages) or +m (moderated).
705 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED))
706 || IsModelessChannel(chptr->chname))
711 return member_can_send_to_channel(member);
715 * find_no_nickchange_channel
716 * if a member and not opped or voiced and banned
717 * return the name of the first channel banned on
719 const char* find_no_nickchange_channel(struct Client* cptr)
722 struct Membership* member;
723 for (member = (cli_user(cptr))->channel; member;
724 member = member->next_channel) {
725 if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
726 return member->channel->chname;
734 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
735 * with the parameters in pbuf.
737 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
738 struct Channel *chptr)
745 if (chptr->mode.mode & MODE_SECRET)
747 else if (chptr->mode.mode & MODE_PRIVATE)
749 if (chptr->mode.mode & MODE_MODERATED)
751 if (chptr->mode.mode & MODE_TOPICLIMIT)
753 if (chptr->mode.mode & MODE_INVITEONLY)
755 if (chptr->mode.mode & MODE_NOPRIVMSGS)
757 if (chptr->mode.limit) {
759 sprintf_irc(pbuf, "%d", chptr->mode.limit);
762 if (*chptr->mode.key) {
764 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
765 if (chptr->mode.limit)
767 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;
1256 * X --a--> A --b--> B --d--> D
1260 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1261 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1263 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1264 * Remove the user immedeately when no users are left on the channel.
1265 * b) On server B : remove the user (who/lp) from the channel, send a
1266 * PART upstream (to A) and pass on the KICK.
1267 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1268 * channel, and pass on the KICK.
1269 * d) On server D : remove the user (who/lp) from the channel, and pass on
1273 * - Setting the ZOMBIE flag never hurts, we either remove the
1274 * client after that or we don't.
1275 * - The KICK message was already passed on, as should be in all cases.
1276 * - `who' is removed in all cases except case a) when users are left.
1277 * - A PART is only sent upstream in case b).
1283 * 1 --- 2 --- 3 --- 4 --- 5
1287 * We also need to turn 'who' into a zombie on servers 1 and 6,
1288 * because a KICK from 'who' (kicking someone else in that direction)
1289 * can arrive there afterwards - which should not be bounced itself.
1290 * Therefore case a) also applies for servers 1 and 6.
1294 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
1295 struct Client* sptr, struct Channel* chptr)
1297 assert(0 != member);
1302 /* Default for case a): */
1305 /* Case b) or c) ?: */
1306 if (MyUser(who)) /* server 4 */
1308 if (IsServer(cptr)) /* Case b) ? */
1309 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1310 remove_user_from_channel(who, chptr);
1313 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1315 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1316 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1317 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1319 remove_user_from_channel(who, chptr);
1324 /* Case a) (servers 1, 2, 3 and 6) */
1325 if (channel_all_zombies(chptr))
1326 remove_user_from_channel(who, chptr);
1328 /* XXX Can't actually call Debug here; if the channel is all zombies,
1329 * chptr will no longer exist when we get here.
1330 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1334 int number_of_zombies(struct Channel *chptr)
1336 struct Membership* member;
1340 for (member = chptr->members; member; member = member->next_member) {
1341 if (IsZombie(member))
1348 * This helper function builds an argument string in strptr, consisting
1349 * of the original string, a space, and str1 and str2 concatenated (if,
1350 * of course, str2 is not NULL)
1353 build_string(char *strptr, int *strptr_i, char *str1, char *str2, char c)
1356 strptr[(*strptr_i)++] = c;
1359 strptr[(*strptr_i)++] = *(str1++);
1363 strptr[(*strptr_i)++] = *(str2++);
1365 strptr[(*strptr_i)] = '\0';
1369 * This is the workhorse of our ModeBuf suite; this actually generates the
1370 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1373 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1375 /* we only need the flags that don't take args right now */
1376 static int flags[] = {
1377 /* MODE_CHANOP, 'o', */
1378 /* MODE_VOICE, 'v', */
1381 MODE_MODERATED, 'm',
1382 MODE_TOPICLIMIT, 't',
1383 MODE_INVITEONLY, 'i',
1384 MODE_NOPRIVMSGS, 'n',
1385 /* MODE_KEY, 'k', */
1386 /* MODE_BAN, 'b', */
1387 /* MODE_LIMIT, 'l', */
1393 struct Client *app_source; /* where the MODE appears to come from */
1395 char addbuf[20]; /* accumulates +psmtin, etc. */
1397 char rembuf[20]; /* accumulates -psmtin, etc. */
1399 char *bufptr; /* we make use of indirection to simplify the code */
1402 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1404 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1406 char *strptr; /* more indirection to simplify the code */
1409 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1412 char limitbuf[20]; /* convert limits to strings */
1414 unsigned int limitdel = MODE_LIMIT;
1418 /* If the ModeBuf is empty, we have nothing to do */
1419 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1422 /* Ok, if we were given the OPMODE flag, hide the source if its a user */
1423 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
1424 app_source = (cli_user(mbuf->mb_source))->server;
1426 app_source = mbuf->mb_source;
1429 * Account for user we're bouncing; we have to get it in on the first
1430 * bounced MODE, or we could have problems
1432 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1433 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1435 /* Calculate the simple flags */
1436 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1437 if (*flag_p & mbuf->mb_add)
1438 addbuf[addbuf_i++] = flag_p[1];
1439 else if (*flag_p & mbuf->mb_rem)
1440 rembuf[rembuf_i++] = flag_p[1];
1443 /* Now go through the modes with arguments... */
1444 for (i = 0; i < mbuf->mb_count; i++) {
1445 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1447 bufptr_i = &addbuf_i;
1450 bufptr_i = &rembuf_i;
1453 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1454 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1456 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
1457 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1459 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1460 totalbuflen -= IRCD_MAX(5, tmp) + 1;
1462 } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN)) {
1463 tmp = strlen(MB_STRING(mbuf, i));
1465 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1466 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1468 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_KEY ? 'k' : 'b';
1469 totalbuflen -= tmp + 1;
1471 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1472 /* if it's a limit, we also format the number */
1473 sprintf_irc(limitbuf, "%d", MB_UINT(mbuf, i));
1475 tmp = strlen(limitbuf);
1477 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1478 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1480 bufptr[(*bufptr_i)++] = 'l';
1481 totalbuflen -= tmp + 1;
1486 /* terminate the mode strings */
1487 addbuf[addbuf_i] = '\0';
1488 rembuf[rembuf_i] = '\0';
1490 /* If we're building a user visible MODE or HACK... */
1491 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1492 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1493 MODEBUF_DEST_LOG)) {
1494 /* Set up the parameter strings */
1500 for (i = 0; i < mbuf->mb_count; i++) {
1501 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1504 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1506 strptr_i = &addstr_i;
1509 strptr_i = &remstr_i;
1512 /* deal with clients... */
1513 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1514 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1516 /* deal with strings... */
1517 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1518 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1521 * deal with limit; note we cannot include the limit parameter if we're
1524 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1525 (MODE_ADD | MODE_LIMIT))
1526 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1529 /* send the messages off to their destination */
1530 if (mbuf->mb_dest & MODEBUF_DEST_HACK2) {
1531 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1532 "[%Tu]", cli_name(app_source),
1533 mbuf->mb_channel->chname,
1534 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1535 addbuf, remstr, addstr,
1536 mbuf->mb_channel->creationtime);
1537 sendcmdto_serv_butone(&me, CMD_DESYNCH, mbuf->mb_connect,
1538 ":HACK: %s MODE %s %s%s%s%s%s%s [%Tu]",
1539 cli_name(app_source), mbuf->mb_channel->chname,
1540 rembuf_i ? "-" : "", rembuf,
1541 addbuf_i ? "+" : "", addbuf, remstr, addstr,
1542 mbuf->mb_channel->creationtime);
1545 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1546 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1547 "%s%s%s%s%s%s [%Tu]", cli_name(app_source),
1548 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1549 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1550 mbuf->mb_channel->creationtime);
1552 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1553 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1554 "[%Tu]", cli_name(app_source),
1555 mbuf->mb_channel->chname,
1556 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1557 addbuf, remstr, addstr,
1558 mbuf->mb_channel->creationtime);
1560 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1561 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1562 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1563 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1564 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1566 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1567 sendcmdto_channel_butserv(app_source, CMD_MODE, mbuf->mb_channel,
1568 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1569 rembuf_i ? "-" : "", rembuf,
1570 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1573 /* Now are we supposed to propagate to other servers? */
1574 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1575 /* set up parameter string */
1582 * limit is supressed if we're removing it; we have to figure out which
1583 * direction is the direction for it to be removed, though...
1585 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1587 for (i = 0; i < mbuf->mb_count; i++) {
1588 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1591 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1593 strptr_i = &addstr_i;
1596 strptr_i = &remstr_i;
1599 /* deal with modes that take clients */
1600 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1601 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1603 /* deal with modes that take strings */
1604 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1605 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1608 * deal with the limit. Logic here is complicated; if HACK2 is set,
1609 * we're bouncing the mode, so sense is reversed, and we have to
1610 * include the original limit if it looks like it's being removed
1612 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1613 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1616 /* we were told to deop the source */
1617 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1618 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1619 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1620 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1622 /* mark that we've done this, so we don't do it again */
1623 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1626 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1627 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1628 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1629 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1630 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1631 addbuf, remstr, addstr);
1632 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1634 * If HACK2 was set, we're bouncing; we send the MODE back to the
1635 * connection we got it from with the senses reversed and a TS of 0;
1638 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1639 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1640 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1641 mbuf->mb_channel->creationtime);
1644 * We're propagating a normal MODE command to the rest of the network;
1645 * we send the actual channel TS unless this is a HACK3 or a HACK4
1647 if (IsServer(mbuf->mb_source))
1648 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1649 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1650 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1651 addbuf, remstr, addstr,
1652 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1653 mbuf->mb_channel->creationtime);
1655 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1656 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1657 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1658 addbuf, remstr, addstr);
1662 /* We've drained the ModeBuf... */
1667 /* reinitialize the mode-with-arg slots */
1668 for (i = 0; i < MAXMODEPARAMS; i++) {
1669 /* If we saved any, pack them down */
1670 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1671 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1672 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1674 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1676 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1677 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1679 MB_TYPE(mbuf, i) = 0;
1680 MB_UINT(mbuf, i) = 0;
1683 /* If we're supposed to flush it all, do so--all hail tail recursion */
1684 if (all && mbuf->mb_count)
1685 return modebuf_flush_int(mbuf, 1);
1691 * This routine just initializes a ModeBuf structure with the information
1692 * needed and the options given.
1695 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1696 struct Client *connect, struct Channel *chan, unsigned int dest)
1701 assert(0 != source);
1707 mbuf->mb_source = source;
1708 mbuf->mb_connect = connect;
1709 mbuf->mb_channel = chan;
1710 mbuf->mb_dest = dest;
1713 /* clear each mode-with-parameter slot */
1714 for (i = 0; i < MAXMODEPARAMS; i++) {
1715 MB_TYPE(mbuf, i) = 0;
1716 MB_UINT(mbuf, i) = 0;
1721 * This routine simply adds modes to be added or deleted; do a binary OR
1722 * with either MODE_ADD or MODE_DEL
1725 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1728 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1730 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1731 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
1733 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1736 if (mode & MODE_ADD) {
1737 mbuf->mb_rem &= ~mode;
1738 mbuf->mb_add |= mode;
1740 mbuf->mb_add &= ~mode;
1741 mbuf->mb_rem |= mode;
1746 * This routine adds a mode to be added or deleted that takes a unsigned
1747 * int parameter; mode may *only* be the relevant mode flag ORed with one
1748 * of MODE_ADD or MODE_DEL
1751 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1754 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1756 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1757 MB_UINT(mbuf, mbuf->mb_count) = uint;
1759 /* when we've reached the maximal count, flush the buffer */
1760 if (++mbuf->mb_count >=
1761 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1762 modebuf_flush_int(mbuf, 0);
1766 * This routine adds a mode to be added or deleted that takes a string
1767 * parameter; mode may *only* be the relevant mode flag ORed with one of
1768 * MODE_ADD or MODE_DEL
1771 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
1775 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1777 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
1778 MB_STRING(mbuf, mbuf->mb_count) = string;
1780 /* when we've reached the maximal count, flush the buffer */
1781 if (++mbuf->mb_count >=
1782 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1783 modebuf_flush_int(mbuf, 0);
1787 * This routine adds a mode to be added or deleted that takes a client
1788 * parameter; mode may *only* be the relevant mode flag ORed with one of
1789 * MODE_ADD or MODE_DEL
1792 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
1793 struct Client *client)
1796 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1798 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1799 MB_CLIENT(mbuf, mbuf->mb_count) = client;
1801 /* when we've reached the maximal count, flush the buffer */
1802 if (++mbuf->mb_count >=
1803 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1804 modebuf_flush_int(mbuf, 0);
1808 * This is the exported binding for modebuf_flush()
1811 modebuf_flush(struct ModeBuf *mbuf)
1813 return modebuf_flush_int(mbuf, 1);
1817 * This extracts the simple modes contained in mbuf
1820 modebuf_extract(struct ModeBuf *mbuf, char *buf)
1822 static int flags[] = {
1823 /* MODE_CHANOP, 'o', */
1824 /* MODE_VOICE, 'v', */
1827 MODE_MODERATED, 'm',
1828 MODE_TOPICLIMIT, 't',
1829 MODE_INVITEONLY, 'i',
1830 MODE_NOPRIVMSGS, 'n',
1832 /* MODE_BAN, 'b', */
1837 int i, bufpos = 0, len;
1839 char *key = 0, limitbuf[20];
1848 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
1849 if (MB_TYPE(mbuf, i) & MODE_ADD) {
1850 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT);
1852 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
1853 key = MB_STRING(mbuf, i);
1854 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
1855 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%d", MB_UINT(mbuf, i));
1862 buf[bufpos++] = '+'; /* start building buffer */
1864 for (flag_p = flags; flag_p[0]; flag_p += 2)
1866 buf[bufpos++] = flag_p[1];
1868 for (i = 0, len = bufpos; i < len; i++) {
1870 build_string(buf, &bufpos, key, 0, ' ');
1871 else if (buf[i] == 'l')
1872 build_string(buf, &bufpos, limitbuf, 0, ' ');
1881 * Simple function to invalidate bans
1884 mode_ban_invalidate(struct Channel *chan)
1886 struct Membership *member;
1888 for (member = chan->members; member; member = member->next_member)
1889 ClearBanValid(member);
1893 * Simple function to drop invite structures
1896 mode_invite_clear(struct Channel *chan)
1898 while (chan->invites)
1899 del_invite(chan->invites->value.cptr, chan);
1902 /* What we've done for mode_parse so far... */
1903 #define DONE_LIMIT 0x01 /* We've set the limit */
1904 #define DONE_KEY 0x02 /* We've set the key */
1905 #define DONE_BANLIST 0x04 /* We've sent the ban list */
1906 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
1907 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
1910 struct ModeBuf *mbuf;
1911 struct Client *cptr;
1912 struct Client *sptr;
1913 struct Channel *chptr;
1924 struct SLink banlist[MAXPARA];
1927 struct Client *client;
1928 } cli_change[MAXPARA];
1932 * Here's a helper function to deal with sending along "Not oper" or
1933 * "Not member" messages
1936 send_notoper(struct ParseState *state)
1938 if (state->done & DONE_NOTOPER)
1941 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
1942 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
1944 state->done |= DONE_NOTOPER;
1948 * Helper function to convert limits
1951 mode_parse_limit(struct ParseState *state, int *flag_p)
1953 unsigned int t_limit;
1955 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
1956 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
1959 if (state->parc <= 0) { /* warn if not enough args */
1960 if (MyUser(state->sptr))
1961 need_more_params(state->sptr, "MODE +l");
1965 t_limit = atoi(state->parv[state->args_used++]); /* grab arg */
1969 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
1970 (!t_limit || t_limit == state->chptr->mode.limit))
1973 t_limit = state->chptr->mode.limit;
1975 /* If they're not an oper, they can't change modes */
1976 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
1977 send_notoper(state);
1981 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
1983 state->done |= DONE_LIMIT;
1988 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
1990 if (state->flags & MODE_PARSE_SET) { /* set the limit */
1991 if (state->dir & MODE_ADD) {
1992 state->chptr->mode.mode |= flag_p[0];
1993 state->chptr->mode.limit = t_limit;
1995 state->chptr->mode.mode &= ~flag_p[0];
1996 state->chptr->mode.limit = 0;
2002 * Helper function to convert keys
2005 mode_parse_key(struct ParseState *state, int *flag_p)
2010 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2013 if (state->parc <= 0) { /* warn if not enough args */
2014 if (MyUser(state->sptr))
2015 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2020 t_str = state->parv[state->args_used++]; /* grab arg */
2024 /* If they're not an oper, they can't change modes */
2025 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2026 send_notoper(state);
2030 if (state->done & DONE_KEY) /* allow key to be set only once */
2032 state->done |= DONE_KEY;
2036 /* clean up the key string */
2038 while (*++s > ' ' && *s != ':' && --t_len)
2042 if (!*t_str) { /* warn if empty */
2043 if (MyUser(state->sptr))
2044 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2052 /* can't add a key if one is set, nor can one remove the wrong key */
2053 if (!(state->flags & MODE_PARSE_FORCE))
2054 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2055 (state->dir == MODE_DEL &&
2056 ircd_strcmp(state->chptr->mode.key, t_str))) {
2057 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2061 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2062 !ircd_strcmp(state->chptr->mode.key, t_str))
2063 return; /* no key change */
2065 if (state->flags & MODE_PARSE_BOUNCE) {
2066 if (*state->chptr->mode.key) /* reset old key */
2067 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2068 state->chptr->mode.key, 0);
2069 else /* remove new bogus key */
2070 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2071 } else /* send new key */
2072 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2074 if (state->flags & MODE_PARSE_SET) {
2075 if (state->dir == MODE_ADD) /* set the new key */
2076 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2077 else /* remove the old key */
2078 *state->chptr->mode.key = '\0';
2083 * Helper function to convert bans
2086 mode_parse_ban(struct ParseState *state, int *flag_p)
2089 struct SLink *ban, *newban = 0;
2091 if (state->parc <= 0) { /* Not enough args, send ban list */
2092 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2093 send_ban_list(state->sptr, state->chptr);
2094 state->done |= DONE_BANLIST;
2100 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2103 t_str = state->parv[state->args_used++]; /* grab arg */
2107 /* If they're not an oper, they can't change modes */
2108 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2109 send_notoper(state);
2113 if ((s = strchr(t_str, ' ')))
2116 if (!*t_str || *t_str == ':') { /* warn if empty */
2117 if (MyUser(state->sptr))
2118 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2123 t_str = collapse(pretty_mask(t_str));
2125 /* remember the ban for the moment... */
2126 if (state->dir == MODE_ADD) {
2127 newban = state->banlist + (state->numbans++);
2130 DupString(newban->value.ban.banstr, t_str);
2131 newban->value.ban.who = cli_name(state->sptr);
2132 newban->value.ban.when = TStime();
2134 newban->flags = CHFL_BAN | MODE_ADD;
2136 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
2137 newban->flags |= CHFL_BAN_IPMASK;
2140 if (!state->chptr->banlist) {
2141 state->chptr->banlist = newban; /* add our ban with its flags */
2142 state->done |= DONE_BANCLEAN;
2146 /* Go through all bans */
2147 for (ban = state->chptr->banlist; ban; ban = ban->next) {
2148 /* first, clean the ban flags up a bit */
2149 if (!(state->done & DONE_BANCLEAN))
2150 /* Note: We're overloading *lots* of bits here; be careful! */
2151 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
2155 * MODE_ADD - Ban was added; if we're bouncing modes,
2156 * then we'll remove it below; otherwise,
2157 * we'll have to allocate a real ban
2159 * MODE_DEL - Ban was marked for deletion; if we're
2160 * bouncing modes, we'll have to re-add it,
2161 * otherwise, we'll have to remove it
2163 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
2164 * with a ban already set; if we're
2165 * bouncing modes, we'll have to bounce
2166 * this one; otherwise, we'll just ignore
2167 * it when we process added bans
2170 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
2171 ban->flags |= MODE_DEL; /* delete one ban */
2173 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2175 } else if (state->dir == MODE_ADD) {
2176 /* if the ban already exists, don't worry about it */
2177 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
2178 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2181 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
2182 if (!(ban->flags & MODE_DEL))
2183 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
2184 } else if (!mmatch(t_str, ban->value.ban.banstr))
2185 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
2188 ban->next = newban; /* add our ban with its flags */
2189 break; /* get out of loop */
2193 state->done |= DONE_BANCLEAN;
2197 * This is the bottom half of the ban processor
2200 mode_process_bans(struct ParseState *state)
2202 struct SLink *ban, *newban, *prevban, *nextban;
2208 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2210 banlen = strlen(ban->value.ban.banstr);
2212 nextban = ban->next;
2214 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
2216 prevban->next = 0; /* Break the list; ban isn't a real ban */
2218 state->chptr->banlist = 0;
2223 MyFree(ban->value.ban.banstr);
2226 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
2227 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2228 ban->value.ban.banstr,
2229 state->flags & MODE_PARSE_SET);
2231 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2232 if (prevban) /* clip it out of the list... */
2233 prevban->next = ban->next;
2235 state->chptr->banlist = ban->next;
2240 MyFree(ban->value.ban.who);
2244 continue; /* next ban; keep prevban like it is */
2246 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
2247 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
2249 prevban->next = 0; /* Break the list; ban isn't a real ban */
2251 state->chptr->banlist = 0;
2253 /* If we're supposed to ignore it, do so. */
2254 if (ban->flags & CHFL_BAN_OVERLAPPED &&
2255 !(state->flags & MODE_PARSE_BOUNCE)) {
2259 MyFree(ban->value.ban.banstr);
2261 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2262 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2263 count >= feature_int(FEAT_MAXBANS))) {
2264 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2265 ban->value.ban.banstr);
2269 MyFree(ban->value.ban.banstr);
2271 /* add the ban to the buffer */
2272 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2273 ban->value.ban.banstr,
2274 !(state->flags & MODE_PARSE_SET));
2276 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2277 newban = make_link();
2278 newban->value.ban.banstr = ban->value.ban.banstr;
2279 DupString(newban->value.ban.who, ban->value.ban.who);
2280 newban->value.ban.when = ban->value.ban.when;
2281 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
2283 newban->next = state->chptr->banlist; /* and link it in */
2284 state->chptr->banlist = newban;
2293 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2295 if (changed) /* if we changed the ban list, we must invalidate the bans */
2296 mode_ban_invalidate(state->chptr);
2300 * Helper function to process client changes
2303 mode_parse_client(struct ParseState *state, int *flag_p)
2306 struct Client *acptr;
2309 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2312 if (state->parc <= 0) /* return if not enough args */
2315 t_str = state->parv[state->args_used++]; /* grab arg */
2319 /* If they're not an oper, they can't change modes */
2320 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2321 send_notoper(state);
2325 if (MyUser(state->sptr)) /* find client we're manipulating */
2326 acptr = find_chasing(state->sptr, t_str, NULL);
2328 acptr = findNUser(t_str);
2331 return; /* find_chasing() already reported an error to the user */
2333 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2334 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2335 state->cli_change[i].flag & flag_p[0]))
2336 break; /* found a slot */
2338 /* Store what we're doing to them */
2339 state->cli_change[i].flag = state->dir | flag_p[0];
2340 state->cli_change[i].client = acptr;
2344 * Helper function to process the changed client list
2347 mode_process_clients(struct ParseState *state)
2350 struct Membership *member;
2352 for (i = 0; state->cli_change[i].flag; i++) {
2353 assert(0 != state->cli_change[i].client);
2355 /* look up member link */
2356 if (!(member = find_member_link(state->chptr,
2357 state->cli_change[i].client)) ||
2358 (MyUser(state->sptr) && IsZombie(member))) {
2359 if (MyUser(state->sptr))
2360 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2361 cli_name(state->cli_change[i].client),
2362 state->chptr->chname);
2366 if ((state->cli_change[i].flag & MODE_ADD &&
2367 (state->cli_change[i].flag & member->status)) ||
2368 (state->cli_change[i].flag & MODE_DEL &&
2369 !(state->cli_change[i].flag & member->status)))
2370 continue; /* no change made, don't do anything */
2372 /* see if the deop is allowed */
2373 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2374 (MODE_DEL | MODE_CHANOP)) {
2375 /* prevent +k users from being deopped */
2376 if (IsChannelService(state->cli_change[i].client)) {
2377 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
2378 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
2380 (IsServer(state->sptr) ? cli_name(state->sptr) :
2381 cli_name((cli_user(state->sptr))->server)));
2383 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
2384 send_reply(state->sptr, ERR_ISCHANSERVICE,
2385 cli_name(state->cli_change[i].client),
2386 state->chptr->chname);
2391 /* don't allow local opers to be deopped on local channels */
2392 if (MyUser(state->sptr) && state->cli_change[i].client != state->sptr &&
2393 IsLocalChannel(state->chptr->chname) &&
2394 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
2395 send_reply(state->sptr, ERR_ISOPERLCHAN,
2396 cli_name(state->cli_change[i].client),
2397 state->chptr->chname);
2402 /* accumulate the change */
2403 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
2404 state->cli_change[i].client);
2406 /* actually effect the change */
2407 if (state->flags & MODE_PARSE_SET) {
2408 if (state->cli_change[i].flag & MODE_ADD) {
2409 member->status |= (state->cli_change[i].flag &
2410 (MODE_CHANOP | MODE_VOICE));
2411 if (state->cli_change[i].flag & MODE_CHANOP)
2412 ClearDeopped(member);
2414 member->status &= ~(state->cli_change[i].flag &
2415 (MODE_CHANOP | MODE_VOICE));
2417 } /* for (i = 0; state->cli_change[i].flags; i++) { */
2421 * Helper function to process the simple modes
2424 mode_parse_mode(struct ParseState *state, int *flag_p)
2426 /* If they're not an oper, they can't change modes */
2427 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2428 send_notoper(state);
2435 if (state->dir == MODE_ADD) {
2436 state->add |= flag_p[0];
2437 state->del &= ~flag_p[0];
2439 if (flag_p[0] & MODE_SECRET) {
2440 state->add &= ~MODE_PRIVATE;
2441 state->del |= MODE_PRIVATE;
2442 } else if (flag_p[0] & MODE_PRIVATE) {
2443 state->add &= ~MODE_SECRET;
2444 state->del |= MODE_SECRET;
2447 state->add &= ~flag_p[0];
2448 state->del |= flag_p[0];
2451 assert(0 == (state->add & state->del));
2452 assert((MODE_SECRET | MODE_PRIVATE) !=
2453 (state->add & (MODE_SECRET | MODE_PRIVATE)));
2457 * This routine is intended to parse MODE or OPMODE commands and effect the
2458 * changes (or just build the bounce buffer). We pass the starting offset
2462 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
2463 struct Channel *chptr, int parc, char *parv[], unsigned int flags)
2465 static int chan_flags[] = {
2470 MODE_MODERATED, 'm',
2471 MODE_TOPICLIMIT, 't',
2472 MODE_INVITEONLY, 'i',
2473 MODE_NOPRIVMSGS, 'n',
2483 unsigned int t_mode;
2485 struct ParseState state;
2496 state.chptr = chptr;
2499 state.flags = flags;
2500 state.dir = MODE_ADD;
2504 state.args_used = 0;
2505 state.max_args = MAXMODEPARAMS;
2508 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
2509 state.banlist[i].next = 0;
2510 state.banlist[i].value.ban.banstr = 0;
2511 state.banlist[i].value.ban.who = 0;
2512 state.banlist[i].value.ban.when = 0;
2513 state.banlist[i].flags = 0;
2514 state.cli_change[i].flag = 0;
2515 state.cli_change[i].client = 0;
2518 modestr = state.parv[state.args_used++];
2522 for (; *modestr; modestr++) {
2523 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
2524 if (flag_p[1] == *modestr)
2527 if (!flag_p[0]) { /* didn't find it? complain and continue */
2528 if (MyUser(state.sptr))
2529 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
2534 case '+': /* switch direction to MODE_ADD */
2535 case '-': /* switch direction to MODE_DEL */
2536 state.dir = flag_p[0];
2539 case 'l': /* deal with limits */
2540 mode_parse_limit(&state, flag_p);
2543 case 'k': /* deal with keys */
2544 mode_parse_key(&state, flag_p);
2547 case 'b': /* deal with bans */
2548 mode_parse_ban(&state, flag_p);
2551 case 'o': /* deal with ops/voice */
2553 mode_parse_client(&state, flag_p);
2556 default: /* deal with other modes */
2557 mode_parse_mode(&state, flag_p);
2559 } /* switch (*modestr) { */
2560 } /* for (; *modestr; modestr++) { */
2562 if (state.flags & MODE_PARSE_BURST)
2563 break; /* don't interpret any more arguments */
2565 if (state.parc > 0) { /* process next argument in string */
2566 modestr = state.parv[state.args_used++];
2570 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
2573 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
2574 break; /* we're then going to bounce the mode! */
2576 recv_ts = atoi(modestr);
2578 if (recv_ts && recv_ts < state.chptr->creationtime)
2579 state.chptr->creationtime = recv_ts; /* respect earlier TS */
2581 break; /* break out of while loop */
2582 } else if (state.flags & MODE_PARSE_STRICT ||
2583 (MyUser(state.sptr) && state.max_args <= 0)) {
2584 state.parc++; /* we didn't actually gobble the argument */
2586 break; /* break out of while loop */
2589 } /* while (*modestr) { */
2592 * the rest of the function finishes building resultant MODEs; if the
2593 * origin isn't a member or an oper, skip it.
2595 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
2596 return state.args_used; /* tell our parent how many args we gobbled */
2598 t_mode = state.chptr->mode.mode;
2600 if (state.del & t_mode) { /* delete any modes to be deleted... */
2601 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
2603 t_mode &= ~state.del;
2605 if (state.add & ~t_mode) { /* add any modes to be added... */
2606 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
2608 t_mode |= state.add;
2611 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
2612 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
2613 !(t_mode & MODE_INVITEONLY))
2614 mode_invite_clear(state.chptr);
2616 state.chptr->mode.mode = t_mode;
2619 if (state.flags & MODE_PARSE_WIPEOUT) {
2620 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
2621 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
2622 state.chptr->mode.limit);
2623 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
2624 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
2625 state.chptr->mode.key, 0);
2628 if (state.done & DONE_BANCLEAN) /* process bans */
2629 mode_process_bans(&state);
2631 /* process client changes */
2632 if (state.cli_change[0].flag)
2633 mode_process_clients(&state);
2635 return state.args_used; /* tell our parent how many args we gobbled */
2639 * Initialize a join buffer
2642 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
2643 struct Client *connect, unsigned int type, char *comment,
2649 assert(0 != source);
2650 assert(0 != connect);
2652 jbuf->jb_source = source; /* just initialize struct JoinBuf */
2653 jbuf->jb_connect = connect;
2654 jbuf->jb_type = type;
2655 jbuf->jb_comment = comment;
2656 jbuf->jb_create = create;
2658 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
2659 type == JOINBUF_TYPE_PART ||
2660 type == JOINBUF_TYPE_PARTALL) ?
2661 STARTJOINLEN : STARTCREATELEN) +
2662 (comment ? strlen(comment) + 2 : 0));
2664 for (i = 0; i < MAXJOINARGS; i++)
2665 jbuf->jb_channels[i] = 0;
2669 * Add a channel to the join buffer
2672 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
2679 if (jbuf->jb_type == JOINBUF_TYPE_JOIN)
2680 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
2685 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
2686 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
2687 /* Send notification to channel */
2688 if (!(flags & CHFL_ZOMBIE))
2689 sendcmdto_channel_butserv(jbuf->jb_source, CMD_PART, chan,
2690 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2691 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2692 else if (MyUser(jbuf->jb_source))
2693 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
2694 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2695 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2696 /* XXX: Shouldn't we send a PART here anyway? */
2697 /* to users on the channel? Why? From their POV, the user isn't on
2698 * the channel anymore anyway. We don't send to servers until below,
2699 * when we gang all the channel parts together. Note that this is
2700 * exactly the same logic, albeit somewhat more concise, as was in
2701 * the original m_part.c */
2703 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2704 IsLocalChannel(chan->chname)) /* got to remove user here */
2705 remove_user_from_channel(jbuf->jb_source, chan);
2707 /* Add user to channel */
2708 add_user_to_channel(chan, jbuf->jb_source, flags);
2710 /* send notification to all servers */
2711 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !IsLocalChannel(chan->chname))
2712 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
2713 "%H %Tu", chan, chan->creationtime);
2715 /* Send the notification to the channel */
2716 sendcmdto_channel_butserv(jbuf->jb_source, CMD_JOIN, chan, ":%H", chan);
2718 /* send an op, too, if needed */
2719 if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE &&
2720 !IsModelessChannel(chan->chname))
2721 sendcmdto_channel_butserv(jbuf->jb_source, CMD_MODE, chan, "%H +o %C",
2722 chan, jbuf->jb_source);
2725 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || IsLocalChannel(chan->chname))
2726 return; /* don't send to remote */
2728 /* figure out if channel name will cause buffer to be overflowed */
2729 len = chan ? strlen(chan->chname) + 1 : 2;
2730 if (jbuf->jb_strlen + len > BUFSIZE)
2731 joinbuf_flush(jbuf);
2733 /* add channel to list of channels to send and update counts */
2734 jbuf->jb_channels[jbuf->jb_count++] = chan;
2735 jbuf->jb_strlen += len;
2737 /* if we've used up all slots, flush */
2738 if (jbuf->jb_count >= MAXJOINARGS)
2739 joinbuf_flush(jbuf);
2743 * Flush the channel list to remote servers
2746 joinbuf_flush(struct JoinBuf *jbuf)
2748 char chanlist[BUFSIZE];
2752 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2753 jbuf->jb_type == JOINBUF_TYPE_JOIN)
2754 return 0; /* no joins to process */
2756 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
2757 build_string(chanlist, &chanlist_i,
2758 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
2759 i == 0 ? '\0' : ',');
2760 if (JOINBUF_TYPE_PART == jbuf->jb_type)
2761 /* Remove user from channel */
2762 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
2764 jbuf->jb_channels[i] = 0; /* mark slot empty */
2767 jbuf->jb_count = 0; /* reset base counters */
2768 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
2769 STARTJOINLEN : STARTCREATELEN) +
2770 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
2772 /* and send the appropriate command */
2773 switch (jbuf->jb_type) {
2774 case JOINBUF_TYPE_CREATE:
2775 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
2776 "%s %Tu", chanlist, jbuf->jb_create);
2779 case JOINBUF_TYPE_PART:
2780 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
2781 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,