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"
30 #include "ircd_reply.h"
31 #include "ircd_snprintf.h"
32 #include "ircd_string.h"
39 #include "querycmds.h"
46 #include "sprintf_irc.h"
57 struct Channel* GlobalChannelList = 0;
59 static unsigned int membershipAllocCount;
60 static struct Membership* membershipFreeList;
62 static struct SLink *next_overlapped_ban(void);
63 static int del_banid(struct Channel *, char *, int);
64 void del_invite(struct Client *, struct Channel *);
66 const char* const PartFmt1 = ":%s " MSG_PART " %s";
67 const char* const PartFmt2 = ":%s " MSG_PART " %s :%s";
68 const char* const PartFmt1serv = "%s%s " TOK_PART " %s";
69 const char* const PartFmt2serv = "%s%s " TOK_PART " %s :%s";
72 static struct SLink* next_ban;
73 static struct SLink* prev_ban;
74 static struct SLink* removed_bans_list;
77 * Use a global variable to remember if an oper set a mode on a local channel. Ugly,
78 * but the only way to do it without changing set_mode intensively.
80 int LocalChanOperMode = 0;
84 * return the length (>=0) of a chain of links.
86 static int list_length(struct SLink *lp)
90 for (; lp; lp = lp->next)
96 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
102 /* Servers don't have member links */
103 if (IsServer(cptr)||IsMe(cptr))
106 /* +k users are typically on a LOT of channels. So we iterate over who
107 * is in the channel. X/W are +k and are in about 5800 channels each.
108 * however there are typically no more than 1000 people in a channel
111 if (IsChannelService(cptr)) {
114 assert(m->channel == chptr);
120 /* Users on the other hand aren't allowed on more than 15 channels. 50%
121 * of users that are on channels are on 2 or less, 95% are on 7 or less,
122 * and 99% are on 10 or less.
125 m = (cli_user(cptr))->channel;
127 assert(m->user == cptr);
128 if (m->channel == chptr)
137 * find_chasing - Find the client structure for a nick name (user)
138 * using history mechanism if necessary. If the client is not found, an error
139 * message (NO SUCH NICK) is generated. If the client was found
140 * through the history, chasing will be 1 and otherwise 0.
142 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
144 struct Client* who = FindClient(user);
151 if (!(who = get_history(user, KILLCHASETIMELIMIT))) {
152 send_reply(sptr, ERR_NOSUCHNICK, user);
161 * Create a string of form "foo!bar@fubar" given foo, bar and fubar
162 * as the parameters. If NULL, they become "*".
164 static char *make_nick_user_host(const char *nick, const char *name,
167 static char namebuf[NICKLEN + USERLEN + HOSTLEN + 3];
168 sprintf_irc(namebuf, "%s!%s@%s", nick, name, host);
173 * Create a string of form "foo!bar@123.456.789.123" given foo, bar and the
174 * IP-number as the parameters. If NULL, they become "*".
176 static char *make_nick_user_ip(char *nick, char *name, struct in_addr ip)
178 static char ipbuf[NICKLEN + USERLEN + 16 + 3];
179 sprintf_irc(ipbuf, "%s!%s@%s", nick, name, ircd_ntoa((const char*) &ip));
184 * Subtract one user from channel i (and free channel
185 * block, if channel became empty).
186 * Returns: true (1) if channel still exists
187 * false (0) if the channel was destroyed
189 int sub1_from_channel(struct Channel* chptr)
194 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
196 assert(0 != chptr->members);
201 assert(0 == chptr->members);
203 /* Channel became (or was) empty: Remove channel */
204 if (is_listed(chptr))
207 for (i = 0; i <= HighestFd; i++)
209 struct Client *acptr = 0;
210 if ((acptr = LocalClientArray[i]) && cli_listing(acptr) &&
211 (cli_listing(acptr))->chptr == chptr)
213 list_next_channels(acptr, 1);
214 break; /* Only one client can list a channel */
219 * Now, find all invite links from channel structure
221 while ((tmp = chptr->invites))
222 del_invite(tmp->value.cptr, chptr);
224 tmp = chptr->banlist;
229 MyFree(obtmp->value.ban.banstr);
230 MyFree(obtmp->value.ban.who);
234 chptr->prev->next = chptr->next;
236 GlobalChannelList = chptr->next;
238 chptr->next->prev = chptr->prev;
240 --UserStats.channels;
242 * make sure that channel actually got removed from hash table
244 assert(chptr->hnext == chptr);
252 * `cptr' must be the client adding the ban.
254 * If `change' is true then add `banid' to channel `chptr'.
255 * Returns 0 if the ban was added.
256 * Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
257 * Return -1 otherwise.
259 * Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
260 * when `change' is false, otherwise they will be removed from the banlist.
261 * Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
262 * respectively will return these bans until NULL is returned.
264 * If `firsttime' is true, the ban list as returned by next_overlapped_ban()
265 * is reset (unless a non-zero value is returned, in which case the
266 * CHFL_BAN_OVERLAPPED flag might not have been reset!).
270 int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
271 int change, int firsttime)
276 int removed_bans = 0;
277 int len = strlen(banid);
282 assert(0 == prev_ban);
283 assert(0 == removed_bans_list);
287 for (banp = &chptr->banlist; *banp;)
289 len += strlen((*banp)->value.ban.banstr);
291 if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
293 if (!strcmp((*banp)->value.ban.banstr, banid))
295 (*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
299 else if (!mmatch((*banp)->value.ban.banstr, banid))
301 if (!mmatch(banid, (*banp)->value.ban.banstr))
303 struct SLink *tmp = *banp;
309 len -= strlen(tmp->value.ban.banstr);
313 /* Silently remove overlapping bans */
314 MyFree(tmp->value.ban.banstr);
315 MyFree(tmp->value.ban.who);
319 /* These will be sent to the user later as -b */
320 tmp->next = removed_bans_list;
321 removed_bans_list = tmp;
325 else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
327 tmp->flags |= CHFL_BAN_OVERLAPPED;
338 (*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
339 banp = &(*banp)->next;
342 if (MyUser(cptr) && !removed_bans && (len > MAXBANLENGTH || (cnt >= MAXBANS)))
344 send_reply(cptr, ERR_BANLISTFULL, chptr->chname, banid);
350 struct Membership* member;
352 ban->next = chptr->banlist;
354 ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
355 assert(0 != ban->value.ban.banstr);
356 strcpy(ban->value.ban.banstr, banid);
358 ban->value.ban.who = (char*) MyMalloc(strlen(cli_name(cptr)) + 1);
359 assert(0 != ban->value.ban.who);
360 strcpy(ban->value.ban.who, cli_name(cptr));
362 ban->value.ban.when = TStime();
363 ban->flags = CHFL_BAN; /* This bit is never used I think... */
364 if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
365 ban->flags |= CHFL_BAN_IPMASK;
366 chptr->banlist = ban;
369 * Erase ban-valid-bit
371 for (member = chptr->members; member; member = member->next_member)
372 ClearBanValid(member); /* `ban' == channel member ! */
377 static struct SLink *next_overlapped_ban(void)
379 struct SLink *tmp = next_ban;
383 for (ban = tmp->next; ban; ban = ban->next)
384 if ((ban->flags & CHFL_BAN_OVERLAPPED))
391 struct SLink *next_removed_overlapped_ban(void)
393 struct SLink *tmp = removed_bans_list;
396 if (prev_ban->value.ban.banstr) /* Can be set to NULL in set_mode() */
397 MyFree(prev_ban->value.ban.banstr);
398 MyFree(prev_ban->value.ban.who);
403 removed_bans_list = removed_bans_list->next;
411 * If `change' is true, delete `banid' from channel `chptr'.
412 * Returns `false' if removal was (or would have been) successful.
414 static int del_banid(struct Channel *chptr, char *banid, int change)
421 for (ban = &(chptr->banlist); *ban; ban = &((*ban)->next)) {
422 if (0 == ircd_strcmp(banid, (*ban)->value.ban.banstr))
427 struct Membership* member;
429 MyFree(tmp->value.ban.banstr);
430 MyFree(tmp->value.ban.who);
433 * Erase ban-valid-bit, for channel members that are banned
435 for (member = chptr->members; member; member = member->next_member)
436 if (CHFL_BANVALIDMASK == (member->status & CHFL_BANVALIDMASK))
437 ClearBanValid(member); /* `tmp' == channel member */
446 * find_channel_member - returns Membership * if a person is joined and not a zombie
448 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
450 struct Membership* member;
453 member = find_member_link(chptr, cptr);
454 return (member && !IsZombie(member)) ? member : 0;
458 * is_banned - a non-zero value if banned else 0.
460 static int is_banned(struct Client *cptr, struct Channel *chptr,
461 struct Membership* member)
470 if (member && IsBanValid(member))
471 return IsBanned(member);
473 s = make_nick_user_host(cli_name(cptr), (cli_user(cptr))->username,
474 (cli_user(cptr))->host);
476 for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
477 if ((tmp->flags & CHFL_BAN_IPMASK)) {
479 ip_s = make_nick_user_ip(cli_name(cptr), (cli_user(cptr))->username,
481 if (match(tmp->value.ban.banstr, ip_s) == 0)
484 else if (match(tmp->value.ban.banstr, s) == 0)
500 return (tmp != NULL);
504 * adds a user to a channel by adding another link to the channels member
507 void add_user_to_channel(struct Channel* chptr, struct Client* who,
515 struct Membership* member = membershipFreeList;
517 membershipFreeList = member->next_member;
519 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
520 ++membershipAllocCount;
525 member->channel = chptr;
526 member->status = flags;
528 member->next_member = chptr->members;
529 if (member->next_member)
530 member->next_member->prev_member = member;
531 member->prev_member = 0;
532 chptr->members = member;
534 member->next_channel = (cli_user(who))->channel;
535 if (member->next_channel)
536 member->next_channel->prev_channel = member;
537 member->prev_channel = 0;
538 (cli_user(who))->channel = member;
541 ++((cli_user(who))->joined);
545 static int remove_member_from_channel(struct Membership* member)
547 struct Channel* chptr;
549 chptr = member->channel;
551 * unlink channel member list
553 if (member->next_member)
554 member->next_member->prev_member = member->prev_member;
555 if (member->prev_member)
556 member->prev_member->next_member = member->next_member;
558 member->channel->members = member->next_member;
561 * unlink client channel list
563 if (member->next_channel)
564 member->next_channel->prev_channel = member->prev_channel;
565 if (member->prev_channel)
566 member->prev_channel->next_channel = member->next_channel;
568 (cli_user(member->user))->channel = member->next_channel;
570 --(cli_user(member->user))->joined;
572 member->next_member = membershipFreeList;
573 membershipFreeList = member;
575 return sub1_from_channel(chptr);
578 static int channel_all_zombies(struct Channel* chptr)
580 struct Membership* member;
582 for (member = chptr->members; member; member = member->next_member) {
583 if (!IsZombie(member))
590 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
593 struct Membership* member;
596 if ((member = find_member_link(chptr, cptr))) {
597 if (remove_member_from_channel(member)) {
598 if (channel_all_zombies(chptr)) {
600 * XXX - this looks dangerous but isn't if we got the referential
601 * integrity right for channels
603 while (remove_member_from_channel(chptr->members))
610 void remove_user_from_all_channels(struct Client* cptr)
612 struct Membership* chan;
614 assert(0 != cli_user(cptr));
616 while ((chan = (cli_user(cptr))->channel))
617 remove_user_from_channel(cptr, chan->channel);
620 int is_chan_op(struct Client *cptr, struct Channel *chptr)
622 struct Membership* member;
624 if ((member = find_member_link(chptr, cptr)))
625 return (!IsZombie(member) && IsChanOp(member));
630 static int is_deopped(struct Client *cptr, struct Channel *chptr)
632 struct Membership* member;
635 if ((member = find_member_link(chptr, cptr)))
636 return IsDeopped(member);
638 return (IsUser(cptr) ? 1 : 0);
641 int is_zombie(struct Client *cptr, struct Channel *chptr)
643 struct Membership* member;
647 if ((member = find_member_link(chptr, cptr)))
648 return IsZombie(member);
652 int has_voice(struct Client* cptr, struct Channel* chptr)
654 struct Membership* member;
657 if ((member = find_member_link(chptr, cptr)))
658 return (!IsZombie(member) && HasVoice(member));
663 int member_can_send_to_channel(struct Membership* member)
667 if (IsVoicedOrOpped(member))
670 * If it's moderated, and you aren't a priviledged user, you can't
673 if (member->channel->mode.mode & MODE_MODERATED)
676 * If you're banned then you can't speak either.
677 * but because of the amount of CPU time that is_banned chews
678 * we only check it for our clients.
680 if (MyUser(member->user) && is_banned(member->user, member->channel, member))
685 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
687 struct Membership *member;
690 * Servers can always speak on channels.
695 member = find_channel_member(cptr, chptr);
698 * You can't speak if your off channel, if the channel is modeless, or
699 * +n (no external messages) or +m (moderated).
702 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED))
703 || IsModelessChannel(chptr->chname))
708 return member_can_send_to_channel(member);
712 * find_no_nickchange_channel
713 * if a member and not opped or voiced and banned
714 * return the name of the first channel banned on
716 const char* find_no_nickchange_channel(struct Client* cptr)
719 struct Membership* member;
720 for (member = (cli_user(cptr))->channel; member;
721 member = member->next_channel) {
722 if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
723 return member->channel->chname;
731 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
732 * with the parameters in pbuf.
734 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
735 struct Channel *chptr)
742 if (chptr->mode.mode & MODE_SECRET)
744 else if (chptr->mode.mode & MODE_PRIVATE)
746 if (chptr->mode.mode & MODE_MODERATED)
748 if (chptr->mode.mode & MODE_TOPICLIMIT)
750 if (chptr->mode.mode & MODE_INVITEONLY)
752 if (chptr->mode.mode & MODE_NOPRIVMSGS)
754 if (chptr->mode.limit) {
756 sprintf_irc(pbuf, "%d", chptr->mode.limit);
759 if (*chptr->mode.key) {
761 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
762 if (chptr->mode.limit)
764 strcat(pbuf, chptr->mode.key);
771 * send "cptr" a full list of the modes for channel chptr.
773 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
775 static unsigned int current_flags[4] =
776 { 0, CHFL_CHANOP | CHFL_VOICE, CHFL_VOICE, CHFL_CHANOP };
782 struct Membership* member;
784 char modebuf[MODEBUFLEN];
785 char parabuf[MODEBUFLEN];
791 if (IsLocalChannel(chptr->chname))
794 member = chptr->members;
795 lp2 = chptr->banlist;
797 *modebuf = *parabuf = '\0';
798 channel_modes(cptr, modebuf, parabuf, chptr);
800 for (first = 1; full; first = 0) /* Loop for multiple messages */
802 full = 0; /* Assume by default we get it
803 all in one message */
805 /* (Continued) prefix: "<Y> B <channel> <TS>" */
806 /* is there any better way we can do this? */
807 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
808 chptr->creationtime);
810 if (first && modebuf[1]) /* Add simple modes (iklmnpst)
813 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
814 msgq_append(&me, mb, " %s", modebuf);
817 msgq_append(&me, mb, " %s", parabuf);
821 * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
823 * Run 4 times over all members, to group the members with the
826 for (first = 1; flag_cnt < 4;
827 member = chptr->members, new_mode = 1, flag_cnt++)
829 for (; member; member = member->next_member)
831 if ((member->status & CHFL_VOICED_OR_OPPED) !=
832 current_flags[flag_cnt])
833 continue; /* Skip members with different flags */
834 if (msgq_bufleft(mb) < NUMNICKLEN + 4)
835 /* The 4 is a possible ",:ov" */
837 full = 1; /* Make sure we continue after
839 new_mode = 1; /* Ensure the new BURST line contains the current
841 break; /* Do not add this member to this message */
843 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
844 first = 0; /* From now on, us comma's to add new nicks */
847 * Do we have a nick with a new mode ?
848 * Or are we starting a new BURST line?
853 if (IsVoicedOrOpped(member)) {
857 if (IsChanOp(member))
859 if (HasVoice(member))
862 msgq_append(&me, mb, tbuf);
872 /* Attach all bans, space seperated " :%ban ban ..." */
873 for (first = 2; lp2; lp2 = lp2->next)
875 len = strlen(lp2->value.ban.banstr);
876 if (msgq_bufleft(mb) < len + 1 + first)
877 /* The +1 stands for the added ' '.
878 * The +first stands for the added ":%".
884 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
885 lp2->value.ban.banstr);
890 send_buffer(cptr, mb, 0); /* Send this message */
892 } /* Continue when there was something
893 that didn't fit (full==1) */
899 * by Carlo Wood (Run), 05 Oct 1998.
903 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
904 * When the user name or host name are too long (USERLEN and HOSTLEN
905 * respectively) then they are cut off at the start with a '*'.
907 * The following transformations are made:
910 * 2) xxx.xxx -> *!*@host
911 * 3) xxx!yyy -> nick!user@*
912 * 4) xxx@yyy -> *!user@host
913 * 5) xxx!yyy@zzz -> nick!user@host
915 char *pretty_mask(char *mask)
917 static char star[2] = { '*', 0 };
918 char *last_dot = NULL;
921 /* Case 1: default */
926 /* Do a _single_ pass through the characters of the mask: */
927 for (ptr = mask; *ptr; ++ptr)
931 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
935 else if (*ptr == '@')
937 /* Case 4: Found last '@' (without finding a '!' yet) */
942 else if (*ptr == '.')
944 /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
954 /* Case 4 or 5: Found last '@' */
960 if (user == star && last_dot)
970 char *nick_end = (user != star) ? user - 1 : ptr;
971 if (nick_end - nick > NICKLEN)
977 char *user_end = (host != star) ? host - 1 : ptr;
978 if (user_end - user > USERLEN)
980 user = user_end - USERLEN;
985 if (host != star && ptr - host > HOSTLEN)
987 host = ptr - HOSTLEN;
990 return make_nick_user_host(nick, user, host);
993 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1000 for (lp = chptr->banlist; lp; lp = lp->next)
1001 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->value.ban.banstr,
1002 lp->value.ban.who, lp->value.ban.when);
1004 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1007 /* We are now treating the <key> part of /join <channel list> <key> as a key
1008 * ring; that is, we try one key against the actual channel key, and if that
1009 * doesn't work, we try the next one, and so on. -Kev -Texaco
1010 * Returns: 0 on match, 1 otherwise
1011 * This version contributed by SeKs <intru@info.polymtl.ca>
1013 static int compall(char *key, char *keyring)
1018 p1 = key; /* point to the key... */
1019 while (*p1 && *p1 == *keyring)
1020 { /* step through the key and ring until they
1026 if (!*p1 && (!*keyring || *keyring == ','))
1027 /* ok, if we're at the end of the and also at the end of one of the keys
1028 in the keyring, we have a match */
1031 if (!*keyring) /* if we're at the end of the key ring, there
1032 weren't any matches, so we return 1 */
1035 /* Not at the end of the key ring, so step
1036 through to the next key in the ring: */
1037 while (*keyring && *(keyring++) != ',');
1039 goto top; /* and check it against the key */
1042 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
1045 int overrideJoin = 0;
1048 * Now a banned user CAN join if invited -- Nemesi
1049 * Now a user CAN escape channel limit if invited -- bfriendly
1050 * Now a user CAN escape anything if invited -- Isomer
1053 for (lp = (cli_user(sptr))->invited; lp; lp = lp->next)
1054 if (lp->value.chptr == chptr)
1057 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
1058 a HACK(4) notice will be sent if he would not have been supposed
1059 to join normally. */
1060 if (IsLocalChannel(chptr->chname) && HasPriv(sptr, PRIV_WALK_LCHAN) &&
1061 !BadPtr(key) && compall("OVERRIDE",key) == 0)
1062 overrideJoin = MAGIC_OPER_OVERRIDE;
1064 if (chptr->mode.mode & MODE_INVITEONLY)
1065 return overrideJoin + ERR_INVITEONLYCHAN;
1067 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
1068 return overrideJoin + ERR_CHANNELISFULL;
1070 if (is_banned(sptr, chptr, NULL))
1071 return overrideJoin + ERR_BANNEDFROMCHAN;
1074 * now using compall (above) to test against a whole key ring -Kev
1076 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
1077 return overrideJoin + ERR_BADCHANNELKEY;
1080 return ERR_DONTCHEAT;
1086 * Remove bells and commas from channel name
1088 void clean_channelname(char *cn)
1092 for (i = 0; cn[i]; i++) {
1093 if (i >= CHANNELLEN || !IsChannelChar(cn[i])) {
1097 if (IsChannelLower(cn[i])) {
1098 cn[i] = ToLower(cn[i]);
1104 if ((unsigned char)(cn[i]) == 0xd0)
1105 cn[i] = (char) 0xf0;
1112 * Get Channel block for i (and allocate a new channel
1113 * block, if it didn't exists before).
1115 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1117 struct Channel *chptr;
1120 if (EmptyString(chname))
1123 len = strlen(chname);
1124 if (MyUser(cptr) && len > CHANNELLEN)
1127 *(chname + CHANNELLEN) = '\0';
1129 if ((chptr = FindChannel(chname)))
1131 if (flag == CGT_CREATE)
1133 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1135 ++UserStats.channels;
1136 memset(chptr, 0, sizeof(struct Channel));
1137 strcpy(chptr->chname, chname);
1138 if (GlobalChannelList)
1139 GlobalChannelList->prev = chptr;
1141 chptr->next = GlobalChannelList;
1142 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1143 GlobalChannelList = chptr;
1149 void add_invite(struct Client *cptr, struct Channel *chptr)
1151 struct SLink *inv, **tmp;
1153 del_invite(cptr, chptr);
1155 * Delete last link in chain if the list is max length
1157 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1158 if ((cli_user(cptr))->invites>=MAXCHANNELSPERUSER)
1159 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1161 * Add client to channel invite list
1164 inv->value.cptr = cptr;
1165 inv->next = chptr->invites;
1166 chptr->invites = inv;
1168 * Add channel to the end of the client invite list
1170 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1172 inv->value.chptr = chptr;
1175 (cli_user(cptr))->invites++;
1179 * Delete Invite block from channel invite list and client invite list
1181 void del_invite(struct Client *cptr, struct Channel *chptr)
1183 struct SLink **inv, *tmp;
1185 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1186 if (tmp->value.cptr == cptr)
1191 (cli_user(cptr))->invites--;
1195 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1196 if (tmp->value.chptr == chptr)
1205 /* List and skip all channels that are listen */
1206 void list_next_channels(struct Client *cptr, int nr)
1208 struct ListingArgs *args = cli_listing(cptr);
1209 struct Channel *chptr = args->chptr;
1210 chptr->mode.mode &= ~MODE_LISTED;
1211 while (is_listed(chptr) || --nr >= 0)
1213 for (; chptr; chptr = chptr->next)
1215 if (!cli_user(cptr) || (SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
1217 if (chptr->users > args->min_users && chptr->users < args->max_users &&
1218 chptr->creationtime > args->min_time &&
1219 chptr->creationtime < args->max_time &&
1220 (!args->topic_limits || (*chptr->topic &&
1221 chptr->topic_time > args->min_topic_time &&
1222 chptr->topic_time < args->max_topic_time)))
1224 if (ShowChannel(cptr,chptr))
1225 send_reply(cptr, RPL_LIST, chptr->chname, chptr->users,
1227 chptr = chptr->next;
1233 MyFree(cli_listing(cptr));
1234 cli_listing(cptr) = NULL;
1235 send_reply(cptr, RPL_LISTEND);
1241 (cli_listing(cptr))->chptr = chptr;
1242 chptr->mode.mode |= MODE_LISTED;
1253 * X --a--> A --b--> B --d--> D
1257 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1258 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1260 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1261 * Remove the user immedeately when no users are left on the channel.
1262 * b) On server B : remove the user (who/lp) from the channel, send a
1263 * PART upstream (to A) and pass on the KICK.
1264 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1265 * channel, and pass on the KICK.
1266 * d) On server D : remove the user (who/lp) from the channel, and pass on
1270 * - Setting the ZOMBIE flag never hurts, we either remove the
1271 * client after that or we don't.
1272 * - The KICK message was already passed on, as should be in all cases.
1273 * - `who' is removed in all cases except case a) when users are left.
1274 * - A PART is only sent upstream in case b).
1280 * 1 --- 2 --- 3 --- 4 --- 5
1284 * We also need to turn 'who' into a zombie on servers 1 and 6,
1285 * because a KICK from 'who' (kicking someone else in that direction)
1286 * can arrive there afterwards - which should not be bounced itself.
1287 * Therefore case a) also applies for servers 1 and 6.
1291 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
1292 struct Client* sptr, struct Channel* chptr)
1294 assert(0 != member);
1299 /* Default for case a): */
1302 /* Case b) or c) ?: */
1303 if (MyUser(who)) /* server 4 */
1305 if (IsServer(cptr)) /* Case b) ? */
1306 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1307 remove_user_from_channel(who, chptr);
1310 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1312 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1313 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1314 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1316 remove_user_from_channel(who, chptr);
1321 /* Case a) (servers 1, 2, 3 and 6) */
1322 if (channel_all_zombies(chptr))
1323 remove_user_from_channel(who, chptr);
1325 /* XXX Can't actually call Debug here; if the channel is all zombies,
1326 * chptr will no longer exist when we get here.
1327 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1331 int number_of_zombies(struct Channel *chptr)
1333 struct Membership* member;
1337 for (member = chptr->members; member; member = member->next_member) {
1338 if (IsZombie(member))
1345 * This helper function builds an argument string in strptr, consisting
1346 * of the original string, a space, and str1 and str2 concatenated (if,
1347 * of course, str2 is not NULL)
1350 build_string(char *strptr, int *strptr_i, char *str1, char *str2, char c)
1353 strptr[(*strptr_i)++] = c;
1356 strptr[(*strptr_i)++] = *(str1++);
1360 strptr[(*strptr_i)++] = *(str2++);
1362 strptr[(*strptr_i)] = '\0';
1366 * This is the workhorse of our ModeBuf suite; this actually generates the
1367 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1370 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1372 /* we only need the flags that don't take args right now */
1373 static int flags[] = {
1374 /* MODE_CHANOP, 'o', */
1375 /* MODE_VOICE, 'v', */
1378 MODE_MODERATED, 'm',
1379 MODE_TOPICLIMIT, 't',
1380 MODE_INVITEONLY, 'i',
1381 MODE_NOPRIVMSGS, 'n',
1382 /* MODE_KEY, 'k', */
1383 /* MODE_BAN, 'b', */
1384 /* MODE_LIMIT, 'l', */
1390 struct Client *app_source; /* where the MODE appears to come from */
1392 char addbuf[20]; /* accumulates +psmtin, etc. */
1394 char rembuf[20]; /* accumulates -psmtin, etc. */
1396 char *bufptr; /* we make use of indirection to simplify the code */
1399 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1401 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1403 char *strptr; /* more indirection to simplify the code */
1406 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1409 char limitbuf[20]; /* convert limits to strings */
1411 unsigned int limitdel = MODE_LIMIT;
1415 /* If the ModeBuf is empty, we have nothing to do */
1416 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1419 /* Ok, if we were given the OPMODE flag, hide the source if its a user */
1420 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
1421 app_source = (cli_user(mbuf->mb_source))->server;
1423 app_source = mbuf->mb_source;
1426 * Account for user we're bouncing; we have to get it in on the first
1427 * bounced MODE, or we could have problems
1429 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1430 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1432 /* Calculate the simple flags */
1433 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1434 if (*flag_p & mbuf->mb_add)
1435 addbuf[addbuf_i++] = flag_p[1];
1436 else if (*flag_p & mbuf->mb_rem)
1437 rembuf[rembuf_i++] = flag_p[1];
1440 /* Now go through the modes with arguments... */
1441 for (i = 0; i < mbuf->mb_count; i++) {
1442 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1444 bufptr_i = &addbuf_i;
1447 bufptr_i = &rembuf_i;
1450 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1451 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1453 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
1454 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1456 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1457 totalbuflen -= IRCD_MAX(5, tmp) + 1;
1459 } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN)) {
1460 tmp = strlen(MB_STRING(mbuf, i));
1462 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1463 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1465 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_KEY ? 'k' : 'b';
1466 totalbuflen -= tmp + 1;
1468 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1469 /* if it's a limit, we also format the number */
1470 sprintf_irc(limitbuf, "%d", MB_UINT(mbuf, i));
1472 tmp = strlen(limitbuf);
1474 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1475 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1477 bufptr[(*bufptr_i)++] = 'l';
1478 totalbuflen -= tmp + 1;
1483 /* terminate the mode strings */
1484 addbuf[addbuf_i] = '\0';
1485 rembuf[rembuf_i] = '\0';
1487 /* If we're building a user visible MODE or HACK... */
1488 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1489 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1490 MODEBUF_DEST_LOG)) {
1491 /* Set up the parameter strings */
1497 for (i = 0; i < mbuf->mb_count; i++) {
1498 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1501 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1503 strptr_i = &addstr_i;
1506 strptr_i = &remstr_i;
1509 /* deal with clients... */
1510 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1511 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1513 /* deal with strings... */
1514 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1515 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1518 * deal with limit; note we cannot include the limit parameter if we're
1521 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1522 (MODE_ADD | MODE_LIMIT))
1523 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1526 /* send the messages off to their destination */
1527 if (mbuf->mb_dest & MODEBUF_DEST_HACK2) {
1528 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1529 "[%Tu]", cli_name(app_source),
1530 mbuf->mb_channel->chname,
1531 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1532 addbuf, remstr, addstr,
1533 mbuf->mb_channel->creationtime);
1534 sendcmdto_serv_butone(&me, CMD_DESYNCH, mbuf->mb_connect,
1535 ":HACK: %s MODE %s %s%s%s%s%s%s [%Tu]",
1536 cli_name(app_source), mbuf->mb_channel->chname,
1537 rembuf_i ? "-" : "", rembuf,
1538 addbuf_i ? "+" : "", addbuf, remstr, addstr,
1539 mbuf->mb_channel->creationtime);
1542 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1543 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1544 "%s%s%s%s%s%s [%Tu]", cli_name(app_source),
1545 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1546 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1547 mbuf->mb_channel->creationtime);
1549 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1550 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1551 "[%Tu]", cli_name(app_source),
1552 mbuf->mb_channel->chname,
1553 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1554 addbuf, remstr, addstr,
1555 mbuf->mb_channel->creationtime);
1557 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1558 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1559 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1560 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1561 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1563 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1564 sendcmdto_channel_butserv(app_source, CMD_MODE, mbuf->mb_channel,
1565 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1566 rembuf_i ? "-" : "", rembuf,
1567 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1570 /* Now are we supposed to propagate to other servers? */
1571 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1572 /* set up parameter string */
1579 * limit is supressed if we're removing it; we have to figure out which
1580 * direction is the direction for it to be removed, though...
1582 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1584 for (i = 0; i < mbuf->mb_count; i++) {
1585 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1588 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1590 strptr_i = &addstr_i;
1593 strptr_i = &remstr_i;
1596 /* deal with modes that take clients */
1597 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1598 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1600 /* deal with modes that take strings */
1601 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1602 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1605 * deal with the limit. Logic here is complicated; if HACK2 is set,
1606 * we're bouncing the mode, so sense is reversed, and we have to
1607 * include the original limit if it looks like it's being removed
1609 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1610 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1613 /* we were told to deop the source */
1614 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1615 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1616 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1617 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1619 /* mark that we've done this, so we don't do it again */
1620 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1623 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1624 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1625 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1626 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1627 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1628 addbuf, remstr, addstr);
1629 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1631 * If HACK2 was set, we're bouncing; we send the MODE back to the
1632 * connection we got it from with the senses reversed and a TS of 0;
1635 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1636 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1637 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1638 mbuf->mb_channel->creationtime);
1641 * We're propagating a normal MODE command to the rest of the network;
1642 * we send the actual channel TS unless this is a HACK3 or a HACK4
1644 if (IsServer(mbuf->mb_source))
1645 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1646 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1647 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1648 addbuf, remstr, addstr,
1649 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1650 mbuf->mb_channel->creationtime);
1652 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1653 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1654 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1655 addbuf, remstr, addstr);
1659 /* We've drained the ModeBuf... */
1664 /* reinitialize the mode-with-arg slots */
1665 for (i = 0; i < MAXMODEPARAMS; i++) {
1666 /* If we saved any, pack them down */
1667 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1668 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1669 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1671 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1673 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1674 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1676 MB_TYPE(mbuf, i) = 0;
1677 MB_UINT(mbuf, i) = 0;
1680 /* If we're supposed to flush it all, do so--all hail tail recursion */
1681 if (all && mbuf->mb_count)
1682 return modebuf_flush_int(mbuf, 1);
1688 * This routine just initializes a ModeBuf structure with the information
1689 * needed and the options given.
1692 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1693 struct Client *connect, struct Channel *chan, unsigned int dest)
1698 assert(0 != source);
1704 mbuf->mb_source = source;
1705 mbuf->mb_connect = connect;
1706 mbuf->mb_channel = chan;
1707 mbuf->mb_dest = dest;
1710 /* clear each mode-with-parameter slot */
1711 for (i = 0; i < MAXMODEPARAMS; i++) {
1712 MB_TYPE(mbuf, i) = 0;
1713 MB_UINT(mbuf, i) = 0;
1718 * This routine simply adds modes to be added or deleted; do a binary OR
1719 * with either MODE_ADD or MODE_DEL
1722 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1725 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1727 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1728 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
1730 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1733 if (mode & MODE_ADD) {
1734 mbuf->mb_rem &= ~mode;
1735 mbuf->mb_add |= mode;
1737 mbuf->mb_add &= ~mode;
1738 mbuf->mb_rem |= mode;
1743 * This routine adds a mode to be added or deleted that takes a unsigned
1744 * int parameter; mode may *only* be the relevant mode flag ORed with one
1745 * of MODE_ADD or MODE_DEL
1748 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1751 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1753 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1754 MB_UINT(mbuf, mbuf->mb_count) = uint;
1756 /* when we've reached the maximal count, flush the buffer */
1757 if (++mbuf->mb_count >=
1758 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1759 modebuf_flush_int(mbuf, 0);
1763 * This routine adds a mode to be added or deleted that takes a string
1764 * parameter; mode may *only* be the relevant mode flag ORed with one of
1765 * MODE_ADD or MODE_DEL
1768 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
1772 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1774 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
1775 MB_STRING(mbuf, mbuf->mb_count) = string;
1777 /* when we've reached the maximal count, flush the buffer */
1778 if (++mbuf->mb_count >=
1779 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1780 modebuf_flush_int(mbuf, 0);
1784 * This routine adds a mode to be added or deleted that takes a client
1785 * parameter; mode may *only* be the relevant mode flag ORed with one of
1786 * MODE_ADD or MODE_DEL
1789 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
1790 struct Client *client)
1793 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1795 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1796 MB_CLIENT(mbuf, mbuf->mb_count) = client;
1798 /* when we've reached the maximal count, flush the buffer */
1799 if (++mbuf->mb_count >=
1800 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1801 modebuf_flush_int(mbuf, 0);
1805 * This is the exported binding for modebuf_flush()
1808 modebuf_flush(struct ModeBuf *mbuf)
1810 return modebuf_flush_int(mbuf, 1);
1814 * This extracts the simple modes contained in mbuf
1817 modebuf_extract(struct ModeBuf *mbuf, char *buf)
1819 static int flags[] = {
1820 /* MODE_CHANOP, 'o', */
1821 /* MODE_VOICE, 'v', */
1824 MODE_MODERATED, 'm',
1825 MODE_TOPICLIMIT, 't',
1826 MODE_INVITEONLY, 'i',
1827 MODE_NOPRIVMSGS, 'n',
1829 /* MODE_BAN, 'b', */
1834 int i, bufpos = 0, len;
1836 char *key = 0, limitbuf[20];
1845 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
1846 if (MB_TYPE(mbuf, i) & MODE_ADD) {
1847 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT);
1849 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
1850 key = MB_STRING(mbuf, i);
1851 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
1852 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%d", MB_UINT(mbuf, i));
1859 buf[bufpos++] = '+'; /* start building buffer */
1861 for (flag_p = flags; flag_p[0]; flag_p += 2)
1863 buf[bufpos++] = flag_p[1];
1865 for (i = 0, len = bufpos; i < len; i++) {
1867 build_string(buf, &bufpos, key, 0, ' ');
1868 else if (buf[i] == 'l')
1869 build_string(buf, &bufpos, limitbuf, 0, ' ');
1878 * Simple function to invalidate bans
1881 mode_ban_invalidate(struct Channel *chan)
1883 struct Membership *member;
1885 for (member = chan->members; member; member = member->next_member)
1886 ClearBanValid(member);
1890 * Simple function to drop invite structures
1893 mode_invite_clear(struct Channel *chan)
1895 while (chan->invites)
1896 del_invite(chan->invites->value.cptr, chan);
1899 /* What we've done for mode_parse so far... */
1900 #define DONE_LIMIT 0x01 /* We've set the limit */
1901 #define DONE_KEY 0x02 /* We've set the key */
1902 #define DONE_BANLIST 0x04 /* We've sent the ban list */
1903 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
1904 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
1907 struct ModeBuf *mbuf;
1908 struct Client *cptr;
1909 struct Client *sptr;
1910 struct Channel *chptr;
1921 struct SLink banlist[MAXPARA];
1924 struct Client *client;
1925 } cli_change[MAXPARA];
1929 * Here's a helper function to deal with sending along "Not oper" or
1930 * "Not member" messages
1933 send_notoper(struct ParseState *state)
1935 if (state->done & DONE_NOTOPER)
1938 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
1939 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
1941 state->done |= DONE_NOTOPER;
1945 * Helper function to convert limits
1948 mode_parse_limit(struct ParseState *state, int *flag_p)
1950 unsigned int t_limit;
1952 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
1953 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
1956 if (state->parc <= 0) { /* warn if not enough args */
1957 if (MyUser(state->sptr))
1958 need_more_params(state->sptr, "MODE +l");
1962 t_limit = atoi(state->parv[state->args_used++]); /* grab arg */
1966 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
1967 (!t_limit || t_limit == state->chptr->mode.limit))
1970 t_limit = state->chptr->mode.limit;
1972 /* If they're not an oper, they can't change modes */
1973 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
1974 send_notoper(state);
1978 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
1980 state->done |= DONE_LIMIT;
1985 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
1987 if (state->flags & MODE_PARSE_SET) { /* set the limit */
1988 if (state->dir & MODE_ADD) {
1989 state->chptr->mode.mode |= flag_p[0];
1990 state->chptr->mode.limit = t_limit;
1992 state->chptr->mode.mode &= ~flag_p[0];
1993 state->chptr->mode.limit = 0;
1999 * Helper function to convert keys
2002 mode_parse_key(struct ParseState *state, int *flag_p)
2007 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2010 if (state->parc <= 0) { /* warn if not enough args */
2011 if (MyUser(state->sptr))
2012 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2017 t_str = state->parv[state->args_used++]; /* grab arg */
2021 /* If they're not an oper, they can't change modes */
2022 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2023 send_notoper(state);
2027 if (state->done & DONE_KEY) /* allow key to be set only once */
2029 state->done |= DONE_KEY;
2033 /* clean up the key string */
2035 while (*++s > ' ' && *s != ':' && --t_len)
2039 if (!*t_str) { /* warn if empty */
2040 if (MyUser(state->sptr))
2041 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2049 /* can't add a key if one is set, nor can one remove the wrong key */
2050 if (!(state->flags & MODE_PARSE_FORCE))
2051 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2052 (state->dir == MODE_DEL &&
2053 ircd_strcmp(state->chptr->mode.key, t_str))) {
2054 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2058 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2059 !ircd_strcmp(state->chptr->mode.key, t_str))
2060 return; /* no key change */
2062 if (state->flags & MODE_PARSE_BOUNCE) {
2063 if (*state->chptr->mode.key) /* reset old key */
2064 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2065 state->chptr->mode.key, 0);
2066 else /* remove new bogus key */
2067 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2068 } else /* send new key */
2069 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2071 if (state->flags & MODE_PARSE_SET) {
2072 if (state->dir == MODE_ADD) /* set the new key */
2073 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2074 else /* remove the old key */
2075 *state->chptr->mode.key = '\0';
2080 * Helper function to convert bans
2083 mode_parse_ban(struct ParseState *state, int *flag_p)
2086 struct SLink *ban, *newban = 0;
2088 if (state->parc <= 0) { /* Not enough args, send ban list */
2089 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2090 send_ban_list(state->sptr, state->chptr);
2091 state->done |= DONE_BANLIST;
2097 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2100 t_str = state->parv[state->args_used++]; /* grab arg */
2104 /* If they're not an oper, they can't change modes */
2105 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2106 send_notoper(state);
2110 if ((s = strchr(t_str, ' ')))
2113 if (!*t_str || *t_str == ':') { /* warn if empty */
2114 if (MyUser(state->sptr))
2115 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2120 t_str = collapse(pretty_mask(t_str));
2122 /* remember the ban for the moment... */
2123 if (state->dir == MODE_ADD) {
2124 newban = state->banlist + (state->numbans++);
2127 DupString(newban->value.ban.banstr, t_str);
2128 newban->value.ban.who = cli_name(state->sptr);
2129 newban->value.ban.when = TStime();
2131 newban->flags = CHFL_BAN | MODE_ADD;
2133 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
2134 newban->flags |= CHFL_BAN_IPMASK;
2137 if (!state->chptr->banlist) {
2138 state->chptr->banlist = newban; /* add our ban with its flags */
2139 state->done |= DONE_BANCLEAN;
2143 /* Go through all bans */
2144 for (ban = state->chptr->banlist; ban; ban = ban->next) {
2145 /* first, clean the ban flags up a bit */
2146 if (!(state->done & DONE_BANCLEAN))
2147 /* Note: We're overloading *lots* of bits here; be careful! */
2148 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
2152 * MODE_ADD - Ban was added; if we're bouncing modes,
2153 * then we'll remove it below; otherwise,
2154 * we'll have to allocate a real ban
2156 * MODE_DEL - Ban was marked for deletion; if we're
2157 * bouncing modes, we'll have to re-add it,
2158 * otherwise, we'll have to remove it
2160 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
2161 * with a ban already set; if we're
2162 * bouncing modes, we'll have to bounce
2163 * this one; otherwise, we'll just ignore
2164 * it when we process added bans
2167 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
2168 ban->flags |= MODE_DEL; /* delete one ban */
2170 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2172 } else if (state->dir == MODE_ADD) {
2173 /* if the ban already exists, don't worry about it */
2174 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
2175 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2178 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
2179 if (!(ban->flags & MODE_DEL))
2180 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
2181 } else if (!mmatch(t_str, ban->value.ban.banstr))
2182 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
2185 ban->next = newban; /* add our ban with its flags */
2186 break; /* get out of loop */
2190 state->done |= DONE_BANCLEAN;
2194 * This is the bottom half of the ban processor
2197 mode_process_bans(struct ParseState *state)
2199 struct SLink *ban, *newban, *prevban, *nextban;
2205 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2207 banlen = strlen(ban->value.ban.banstr);
2209 nextban = ban->next;
2211 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
2213 prevban->next = 0; /* Break the list; ban isn't a real ban */
2215 state->chptr->banlist = 0;
2220 MyFree(ban->value.ban.banstr);
2223 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
2224 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2225 ban->value.ban.banstr,
2226 state->flags & MODE_PARSE_SET);
2228 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2229 if (prevban) /* clip it out of the list... */
2230 prevban->next = ban->next;
2232 state->chptr->banlist = ban->next;
2237 MyFree(ban->value.ban.who);
2241 continue; /* next ban; keep prevban like it is */
2243 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
2244 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
2246 prevban->next = 0; /* Break the list; ban isn't a real ban */
2248 state->chptr->banlist = 0;
2250 /* If we're supposed to ignore it, do so. */
2251 if (ban->flags & CHFL_BAN_OVERLAPPED &&
2252 !(state->flags & MODE_PARSE_BOUNCE)) {
2256 MyFree(ban->value.ban.banstr);
2258 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2259 (len > MAXBANLENGTH || count >= MAXBANS)) {
2260 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2261 ban->value.ban.banstr);
2265 MyFree(ban->value.ban.banstr);
2267 /* add the ban to the buffer */
2268 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2269 ban->value.ban.banstr,
2270 !(state->flags & MODE_PARSE_SET));
2272 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2273 newban = make_link();
2274 newban->value.ban.banstr = ban->value.ban.banstr;
2275 DupString(newban->value.ban.who, ban->value.ban.who);
2276 newban->value.ban.when = ban->value.ban.when;
2277 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
2279 newban->next = state->chptr->banlist; /* and link it in */
2280 state->chptr->banlist = newban;
2289 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2291 if (changed) /* if we changed the ban list, we must invalidate the bans */
2292 mode_ban_invalidate(state->chptr);
2296 * Helper function to process client changes
2299 mode_parse_client(struct ParseState *state, int *flag_p)
2302 struct Client *acptr;
2305 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2308 if (state->parc <= 0) /* return if not enough args */
2311 t_str = state->parv[state->args_used++]; /* grab arg */
2315 /* If they're not an oper, they can't change modes */
2316 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2317 send_notoper(state);
2321 if (MyUser(state->sptr)) /* find client we're manipulating */
2322 acptr = find_chasing(state->sptr, t_str, NULL);
2324 acptr = findNUser(t_str);
2327 return; /* find_chasing() already reported an error to the user */
2329 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2330 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2331 state->cli_change[i].flag & flag_p[0]))
2332 break; /* found a slot */
2334 /* Store what we're doing to them */
2335 state->cli_change[i].flag = state->dir | flag_p[0];
2336 state->cli_change[i].client = acptr;
2340 * Helper function to process the changed client list
2343 mode_process_clients(struct ParseState *state)
2346 struct Membership *member;
2348 for (i = 0; state->cli_change[i].flag; i++) {
2349 assert(0 != state->cli_change[i].client);
2351 /* look up member link */
2352 if (!(member = find_member_link(state->chptr,
2353 state->cli_change[i].client)) ||
2354 (MyUser(state->sptr) && IsZombie(member))) {
2355 if (MyUser(state->sptr))
2356 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2357 cli_name(state->cli_change[i].client),
2358 state->chptr->chname);
2362 if ((state->cli_change[i].flag & MODE_ADD &&
2363 (state->cli_change[i].flag & member->status)) ||
2364 (state->cli_change[i].flag & MODE_DEL &&
2365 !(state->cli_change[i].flag & member->status)))
2366 continue; /* no change made, don't do anything */
2368 /* see if the deop is allowed */
2369 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2370 (MODE_DEL | MODE_CHANOP)) {
2371 /* prevent +k users from being deopped */
2372 if (IsChannelService(state->cli_change[i].client)) {
2373 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
2374 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
2376 (IsServer(state->sptr) ? cli_name(state->sptr) :
2377 cli_name((cli_user(state->sptr))->server)));
2379 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
2380 send_reply(state->sptr, ERR_ISCHANSERVICE,
2381 cli_name(state->cli_change[i].client),
2382 state->chptr->chname);
2387 /* don't allow local opers to be deopped on local channels */
2388 if (MyUser(state->sptr) && state->cli_change[i].client != state->sptr &&
2389 IsLocalChannel(state->chptr->chname) &&
2390 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
2391 send_reply(state->sptr, ERR_ISOPERLCHAN,
2392 cli_name(state->cli_change[i].client),
2393 state->chptr->chname);
2398 /* accumulate the change */
2399 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
2400 state->cli_change[i].client);
2402 /* actually effect the change */
2403 if (state->flags & MODE_PARSE_SET) {
2404 if (state->cli_change[i].flag & MODE_ADD) {
2405 member->status |= (state->cli_change[i].flag &
2406 (MODE_CHANOP | MODE_VOICE));
2407 if (state->cli_change[i].flag & MODE_CHANOP)
2408 ClearDeopped(member);
2410 member->status &= ~(state->cli_change[i].flag &
2411 (MODE_CHANOP | MODE_VOICE));
2413 } /* for (i = 0; state->cli_change[i].flags; i++) { */
2417 * Helper function to process the simple modes
2420 mode_parse_mode(struct ParseState *state, int *flag_p)
2422 /* If they're not an oper, they can't change modes */
2423 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2424 send_notoper(state);
2431 if (state->dir == MODE_ADD) {
2432 state->add |= flag_p[0];
2433 state->del &= ~flag_p[0];
2435 if (flag_p[0] & MODE_SECRET) {
2436 state->add &= ~MODE_PRIVATE;
2437 state->del |= MODE_PRIVATE;
2438 } else if (flag_p[0] & MODE_PRIVATE) {
2439 state->add &= ~MODE_SECRET;
2440 state->del |= MODE_SECRET;
2443 state->add &= ~flag_p[0];
2444 state->del |= flag_p[0];
2447 assert(0 == (state->add & state->del));
2448 assert((MODE_SECRET | MODE_PRIVATE) !=
2449 (state->add & (MODE_SECRET | MODE_PRIVATE)));
2453 * This routine is intended to parse MODE or OPMODE commands and effect the
2454 * changes (or just build the bounce buffer). We pass the starting offset
2458 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
2459 struct Channel *chptr, int parc, char *parv[], unsigned int flags)
2461 static int chan_flags[] = {
2466 MODE_MODERATED, 'm',
2467 MODE_TOPICLIMIT, 't',
2468 MODE_INVITEONLY, 'i',
2469 MODE_NOPRIVMSGS, 'n',
2479 unsigned int t_mode;
2481 struct ParseState state;
2492 state.chptr = chptr;
2495 state.flags = flags;
2496 state.dir = MODE_ADD;
2500 state.args_used = 0;
2501 state.max_args = MAXMODEPARAMS;
2504 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
2505 state.banlist[i].next = 0;
2506 state.banlist[i].value.ban.banstr = 0;
2507 state.banlist[i].value.ban.who = 0;
2508 state.banlist[i].value.ban.when = 0;
2509 state.banlist[i].flags = 0;
2510 state.cli_change[i].flag = 0;
2511 state.cli_change[i].client = 0;
2514 modestr = state.parv[state.args_used++];
2518 for (; *modestr; modestr++) {
2519 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
2520 if (flag_p[1] == *modestr)
2523 if (!flag_p[0]) { /* didn't find it? complain and continue */
2524 if (MyUser(state.sptr))
2525 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
2530 case '+': /* switch direction to MODE_ADD */
2531 case '-': /* switch direction to MODE_DEL */
2532 state.dir = flag_p[0];
2535 case 'l': /* deal with limits */
2536 mode_parse_limit(&state, flag_p);
2539 case 'k': /* deal with keys */
2540 mode_parse_key(&state, flag_p);
2543 case 'b': /* deal with bans */
2544 mode_parse_ban(&state, flag_p);
2547 case 'o': /* deal with ops/voice */
2549 mode_parse_client(&state, flag_p);
2552 default: /* deal with other modes */
2553 mode_parse_mode(&state, flag_p);
2555 } /* switch (*modestr) { */
2556 } /* for (; *modestr; modestr++) { */
2558 if (state.flags & MODE_PARSE_BURST)
2559 break; /* don't interpret any more arguments */
2561 if (state.parc > 0) { /* process next argument in string */
2562 modestr = state.parv[state.args_used++];
2566 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
2569 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
2570 break; /* we're then going to bounce the mode! */
2572 recv_ts = atoi(modestr);
2574 if (recv_ts && recv_ts < state.chptr->creationtime)
2575 state.chptr->creationtime = recv_ts; /* respect earlier TS */
2577 break; /* break out of while loop */
2578 } else if (state.flags & MODE_PARSE_STRICT ||
2579 (MyUser(state.sptr) && state.max_args <= 0)) {
2580 state.parc++; /* we didn't actually gobble the argument */
2582 break; /* break out of while loop */
2585 } /* while (*modestr) { */
2588 * the rest of the function finishes building resultant MODEs; if the
2589 * origin isn't a member or an oper, skip it.
2591 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
2592 return state.args_used; /* tell our parent how many args we gobbled */
2594 t_mode = state.chptr->mode.mode;
2596 if (state.del & t_mode) { /* delete any modes to be deleted... */
2597 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
2599 t_mode &= ~state.del;
2601 if (state.add & ~t_mode) { /* add any modes to be added... */
2602 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
2604 t_mode |= state.add;
2607 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
2608 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
2609 !(t_mode & MODE_INVITEONLY))
2610 mode_invite_clear(state.chptr);
2612 state.chptr->mode.mode = t_mode;
2615 if (state.flags & MODE_PARSE_WIPEOUT) {
2616 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
2617 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
2618 state.chptr->mode.limit);
2619 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
2620 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
2621 state.chptr->mode.key, 0);
2624 if (state.done & DONE_BANCLEAN) /* process bans */
2625 mode_process_bans(&state);
2627 /* process client changes */
2628 if (state.cli_change[0].flag)
2629 mode_process_clients(&state);
2631 return state.args_used; /* tell our parent how many args we gobbled */
2635 * Initialize a join buffer
2638 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
2639 struct Client *connect, unsigned int type, char *comment,
2645 assert(0 != source);
2646 assert(0 != connect);
2648 jbuf->jb_source = source; /* just initialize struct JoinBuf */
2649 jbuf->jb_connect = connect;
2650 jbuf->jb_type = type;
2651 jbuf->jb_comment = comment;
2652 jbuf->jb_create = create;
2654 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
2655 type == JOINBUF_TYPE_PART ||
2656 type == JOINBUF_TYPE_PARTALL) ?
2657 STARTJOINLEN : STARTCREATELEN) +
2658 (comment ? strlen(comment) + 2 : 0));
2660 for (i = 0; i < MAXJOINARGS; i++)
2661 jbuf->jb_channels[i] = 0;
2665 * Add a channel to the join buffer
2668 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
2675 if (jbuf->jb_type == JOINBUF_TYPE_JOIN)
2676 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
2681 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
2682 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
2683 /* Send notification to channel */
2684 if (!(flags & CHFL_ZOMBIE))
2685 sendcmdto_channel_butserv(jbuf->jb_source, CMD_PART, chan,
2686 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2687 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2688 else if (MyUser(jbuf->jb_source))
2689 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
2690 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2691 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2692 /* XXX: Shouldn't we send a PART here anyway? */
2693 /* to users on the channel? Why? From their POV, the user isn't on
2694 * the channel anymore anyway. We don't send to servers until below,
2695 * when we gang all the channel parts together. Note that this is
2696 * exactly the same logic, albeit somewhat more concise, as was in
2697 * the original m_part.c */
2699 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2700 IsLocalChannel(chan->chname)) /* got to remove user here */
2701 remove_user_from_channel(jbuf->jb_source, chan);
2703 /* Add user to channel */
2704 add_user_to_channel(chan, jbuf->jb_source, flags);
2706 /* send notification to all servers */
2707 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !IsLocalChannel(chan->chname))
2708 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
2709 "%H %Tu", chan, chan->creationtime);
2711 /* Send the notification to the channel */
2712 sendcmdto_channel_butserv(jbuf->jb_source, CMD_JOIN, chan, ":%H", chan);
2714 /* send an op, too, if needed */
2715 if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE &&
2716 !IsModelessChannel(chan->chname))
2717 sendcmdto_channel_butserv(jbuf->jb_source, CMD_MODE, chan, "%H +o %C",
2718 chan, jbuf->jb_source);
2721 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || IsLocalChannel(chan->chname))
2722 return; /* don't send to remote */
2724 /* figure out if channel name will cause buffer to be overflowed */
2725 len = chan ? strlen(chan->chname) + 1 : 2;
2726 if (jbuf->jb_strlen + len > BUFSIZE)
2727 joinbuf_flush(jbuf);
2729 /* add channel to list of channels to send and update counts */
2730 jbuf->jb_channels[jbuf->jb_count++] = chan;
2731 jbuf->jb_strlen += len;
2733 /* if we've used up all slots, flush */
2734 if (jbuf->jb_count >= MAXJOINARGS)
2735 joinbuf_flush(jbuf);
2739 * Flush the channel list to remote servers
2742 joinbuf_flush(struct JoinBuf *jbuf)
2744 char chanlist[BUFSIZE];
2748 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2749 jbuf->jb_type == JOINBUF_TYPE_JOIN)
2750 return 0; /* no joins to process */
2752 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
2753 build_string(chanlist, &chanlist_i,
2754 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
2755 i == 0 ? '\0' : ',');
2756 if (JOINBUF_TYPE_PART == jbuf->jb_type)
2757 /* Remove user from channel */
2758 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
2760 jbuf->jb_channels[i] = 0; /* mark slot empty */
2763 jbuf->jb_count = 0; /* reset base counters */
2764 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
2765 STARTJOINLEN : STARTCREATELEN) +
2766 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
2768 /* and send the appropriate command */
2769 switch (jbuf->jb_type) {
2770 case JOINBUF_TYPE_CREATE:
2771 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
2772 "%s %Tu", chanlist, jbuf->jb_create);
2775 case JOINBUF_TYPE_PART:
2776 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
2777 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,