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)
702 if ((chptr->mode.mode & MODE_NOPRIVMSGS) || IsModelessChannel(chptr->chname))
707 return member_can_send_to_channel(member);
711 * find_no_nickchange_channel
712 * if a member and not opped or voiced and banned
713 * return the name of the first channel banned on
715 const char* find_no_nickchange_channel(struct Client* cptr)
718 struct Membership* member;
719 for (member = (cli_user(cptr))->channel; member;
720 member = member->next_channel) {
721 if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
722 return member->channel->chname;
730 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
731 * with the parameters in pbuf.
733 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
734 struct Channel *chptr)
741 if (chptr->mode.mode & MODE_SECRET)
743 else if (chptr->mode.mode & MODE_PRIVATE)
745 if (chptr->mode.mode & MODE_MODERATED)
747 if (chptr->mode.mode & MODE_TOPICLIMIT)
749 if (chptr->mode.mode & MODE_INVITEONLY)
751 if (chptr->mode.mode & MODE_NOPRIVMSGS)
753 if (chptr->mode.limit) {
755 sprintf_irc(pbuf, "%d", chptr->mode.limit);
758 if (*chptr->mode.key) {
760 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
761 if (chptr->mode.limit)
763 strcat(pbuf, chptr->mode.key);
770 * send "cptr" a full list of the modes for channel chptr.
772 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
774 static unsigned int current_flags[4] =
775 { 0, CHFL_CHANOP | CHFL_VOICE, CHFL_VOICE, CHFL_CHANOP };
781 struct Membership* member;
783 char modebuf[MODEBUFLEN];
784 char parabuf[MODEBUFLEN];
790 if (IsLocalChannel(chptr->chname))
793 member = chptr->members;
794 lp2 = chptr->banlist;
796 *modebuf = *parabuf = '\0';
797 channel_modes(cptr, modebuf, parabuf, chptr);
799 for (first = 1; full; first = 0) /* Loop for multiple messages */
801 full = 0; /* Assume by default we get it
802 all in one message */
804 /* (Continued) prefix: "<Y> B <channel> <TS>" */
805 /* is there any better way we can do this? */
806 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
807 chptr->creationtime);
809 if (first && modebuf[1]) /* Add simple modes (iklmnpst)
812 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
813 msgq_append(&me, mb, " %s", modebuf);
816 msgq_append(&me, mb, " %s", parabuf);
820 * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
822 * Run 4 times over all members, to group the members with the
825 for (first = 1; flag_cnt < 4;
826 member = chptr->members, new_mode = 1, flag_cnt++)
828 for (; member; member = member->next_member)
830 if ((member->status & CHFL_VOICED_OR_OPPED) !=
831 current_flags[flag_cnt])
832 continue; /* Skip members with different flags */
833 if (msgq_bufleft(mb) < NUMNICKLEN + 4)
834 /* The 4 is a possible ",:ov" */
836 full = 1; /* Make sure we continue after
838 new_mode = 1; /* Ensure the new BURST line contains the current
840 break; /* Do not add this member to this message */
842 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
843 first = 0; /* From now on, us comma's to add new nicks */
846 * Do we have a nick with a new mode ?
847 * Or are we starting a new BURST line?
852 if (IsVoicedOrOpped(member)) {
856 if (IsChanOp(member))
858 if (HasVoice(member))
861 msgq_append(&me, mb, tbuf);
871 /* Attach all bans, space seperated " :%ban ban ..." */
872 for (first = 2; lp2; lp2 = lp2->next)
874 len = strlen(lp2->value.ban.banstr);
875 if (msgq_bufleft(mb) < len + 1 + first)
876 /* The +1 stands for the added ' '.
877 * The +first stands for the added ":%".
883 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
884 lp2->value.ban.banstr);
889 send_buffer(cptr, mb, 0); /* Send this message */
891 } /* Continue when there was something
892 that didn't fit (full==1) */
898 * by Carlo Wood (Run), 05 Oct 1998.
902 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
903 * When the user name or host name are too long (USERLEN and HOSTLEN
904 * respectively) then they are cut off at the start with a '*'.
906 * The following transformations are made:
909 * 2) xxx.xxx -> *!*@host
910 * 3) xxx!yyy -> nick!user@*
911 * 4) xxx@yyy -> *!user@host
912 * 5) xxx!yyy@zzz -> nick!user@host
914 char *pretty_mask(char *mask)
916 static char star[2] = { '*', 0 };
917 char *last_dot = NULL;
920 /* Case 1: default */
925 /* Do a _single_ pass through the characters of the mask: */
926 for (ptr = mask; *ptr; ++ptr)
930 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
934 else if (*ptr == '@')
936 /* Case 4: Found last '@' (without finding a '!' yet) */
941 else if (*ptr == '.')
943 /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
953 /* Case 4 or 5: Found last '@' */
959 if (user == star && last_dot)
969 char *nick_end = (user != star) ? user - 1 : ptr;
970 if (nick_end - nick > NICKLEN)
976 char *user_end = (host != star) ? host - 1 : ptr;
977 if (user_end - user > USERLEN)
979 user = user_end - USERLEN;
984 if (host != star && ptr - host > HOSTLEN)
986 host = ptr - HOSTLEN;
989 return make_nick_user_host(nick, user, host);
992 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
999 for (lp = chptr->banlist; lp; lp = lp->next)
1000 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->value.ban.banstr,
1001 lp->value.ban.who, lp->value.ban.when);
1003 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1006 /* We are now treating the <key> part of /join <channel list> <key> as a key
1007 * ring; that is, we try one key against the actual channel key, and if that
1008 * doesn't work, we try the next one, and so on. -Kev -Texaco
1009 * Returns: 0 on match, 1 otherwise
1010 * This version contributed by SeKs <intru@info.polymtl.ca>
1012 static int compall(char *key, char *keyring)
1017 p1 = key; /* point to the key... */
1018 while (*p1 && *p1 == *keyring)
1019 { /* step through the key and ring until they
1025 if (!*p1 && (!*keyring || *keyring == ','))
1026 /* ok, if we're at the end of the and also at the end of one of the keys
1027 in the keyring, we have a match */
1030 if (!*keyring) /* if we're at the end of the key ring, there
1031 weren't any matches, so we return 1 */
1034 /* Not at the end of the key ring, so step
1035 through to the next key in the ring: */
1036 while (*keyring && *(keyring++) != ',');
1038 goto top; /* and check it against the key */
1041 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
1044 int overrideJoin = 0;
1047 * Now a banned user CAN join if invited -- Nemesi
1048 * Now a user CAN escape channel limit if invited -- bfriendly
1049 * Now a user CAN escape anything if invited -- Isomer
1052 for (lp = (cli_user(sptr))->invited; lp; lp = lp->next)
1053 if (lp->value.chptr == chptr)
1056 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
1057 a HACK(4) notice will be sent if he would not have been supposed
1058 to join normally. */
1059 if (IsLocalChannel(chptr->chname) && HasPriv(sptr, PRIV_WALK_LCHAN) &&
1060 !BadPtr(key) && compall("OVERRIDE",key) == 0)
1061 overrideJoin = MAGIC_OPER_OVERRIDE;
1063 if (chptr->mode.mode & MODE_INVITEONLY)
1064 return overrideJoin + ERR_INVITEONLYCHAN;
1066 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
1067 return overrideJoin + ERR_CHANNELISFULL;
1069 if (is_banned(sptr, chptr, NULL))
1070 return overrideJoin + ERR_BANNEDFROMCHAN;
1073 * now using compall (above) to test against a whole key ring -Kev
1075 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
1076 return overrideJoin + ERR_BADCHANNELKEY;
1079 return ERR_DONTCHEAT;
1085 * Remove bells and commas from channel name
1087 void clean_channelname(char *cn)
1091 for (i = 0; cn[i]; i++) {
1092 if (i >= CHANNELLEN || !IsChannelChar(cn[i])) {
1096 if (IsChannelLower(cn[i])) {
1097 cn[i] = ToLower(cn[i]);
1103 if ((unsigned char)(cn[i]) == 0xd0)
1104 cn[i] = (char) 0xf0;
1111 * Get Channel block for i (and allocate a new channel
1112 * block, if it didn't exists before).
1114 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1116 struct Channel *chptr;
1119 if (EmptyString(chname))
1122 len = strlen(chname);
1123 if (MyUser(cptr) && len > CHANNELLEN)
1126 *(chname + CHANNELLEN) = '\0';
1128 if ((chptr = FindChannel(chname)))
1130 if (flag == CGT_CREATE)
1132 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1134 ++UserStats.channels;
1135 memset(chptr, 0, sizeof(struct Channel));
1136 strcpy(chptr->chname, chname);
1137 if (GlobalChannelList)
1138 GlobalChannelList->prev = chptr;
1140 chptr->next = GlobalChannelList;
1141 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1142 GlobalChannelList = chptr;
1148 void add_invite(struct Client *cptr, struct Channel *chptr)
1150 struct SLink *inv, **tmp;
1152 del_invite(cptr, chptr);
1154 * Delete last link in chain if the list is max length
1156 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1157 if ((cli_user(cptr))->invites>=MAXCHANNELSPERUSER)
1158 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1160 * Add client to channel invite list
1163 inv->value.cptr = cptr;
1164 inv->next = chptr->invites;
1165 chptr->invites = inv;
1167 * Add channel to the end of the client invite list
1169 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1171 inv->value.chptr = chptr;
1174 (cli_user(cptr))->invites++;
1178 * Delete Invite block from channel invite list and client invite list
1180 void del_invite(struct Client *cptr, struct Channel *chptr)
1182 struct SLink **inv, *tmp;
1184 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1185 if (tmp->value.cptr == cptr)
1190 (cli_user(cptr))->invites--;
1194 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1195 if (tmp->value.chptr == chptr)
1204 /* List and skip all channels that are listen */
1205 void list_next_channels(struct Client *cptr, int nr)
1207 struct ListingArgs *args = cli_listing(cptr);
1208 struct Channel *chptr = args->chptr;
1209 chptr->mode.mode &= ~MODE_LISTED;
1210 while (is_listed(chptr) || --nr >= 0)
1212 for (; chptr; chptr = chptr->next)
1214 if (!cli_user(cptr) || (SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
1216 if (chptr->users > args->min_users && chptr->users < args->max_users &&
1217 chptr->creationtime > args->min_time &&
1218 chptr->creationtime < args->max_time &&
1219 (!args->topic_limits || (*chptr->topic &&
1220 chptr->topic_time > args->min_topic_time &&
1221 chptr->topic_time < args->max_topic_time)))
1223 if (ShowChannel(cptr,chptr))
1224 send_reply(cptr, RPL_LIST, chptr->chname, chptr->users,
1226 chptr = chptr->next;
1232 MyFree(cli_listing(cptr));
1233 cli_listing(cptr) = NULL;
1234 send_reply(cptr, RPL_LISTEND);
1240 (cli_listing(cptr))->chptr = chptr;
1241 chptr->mode.mode |= MODE_LISTED;
1252 * X --a--> A --b--> B --d--> D
1256 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1257 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1259 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1260 * Remove the user immedeately when no users are left on the channel.
1261 * b) On server B : remove the user (who/lp) from the channel, send a
1262 * PART upstream (to A) and pass on the KICK.
1263 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1264 * channel, and pass on the KICK.
1265 * d) On server D : remove the user (who/lp) from the channel, and pass on
1269 * - Setting the ZOMBIE flag never hurts, we either remove the
1270 * client after that or we don't.
1271 * - The KICK message was already passed on, as should be in all cases.
1272 * - `who' is removed in all cases except case a) when users are left.
1273 * - A PART is only sent upstream in case b).
1279 * 1 --- 2 --- 3 --- 4 --- 5
1283 * We also need to turn 'who' into a zombie on servers 1 and 6,
1284 * because a KICK from 'who' (kicking someone else in that direction)
1285 * can arrive there afterwards - which should not be bounced itself.
1286 * Therefore case a) also applies for servers 1 and 6.
1290 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
1291 struct Client* sptr, struct Channel* chptr)
1293 assert(0 != member);
1298 /* Default for case a): */
1301 /* Case b) or c) ?: */
1302 if (MyUser(who)) /* server 4 */
1304 if (IsServer(cptr)) /* Case b) ? */
1305 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1306 remove_user_from_channel(who, chptr);
1309 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1311 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1312 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1313 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1315 remove_user_from_channel(who, chptr);
1320 /* Case a) (servers 1, 2, 3 and 6) */
1321 if (channel_all_zombies(chptr))
1322 remove_user_from_channel(who, chptr);
1324 /* XXX Can't actually call Debug here; if the channel is all zombies,
1325 * chptr will no longer exist when we get here.
1326 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1330 int number_of_zombies(struct Channel *chptr)
1332 struct Membership* member;
1336 for (member = chptr->members; member; member = member->next_member) {
1337 if (IsZombie(member))
1344 * This helper function builds an argument string in strptr, consisting
1345 * of the original string, a space, and str1 and str2 concatenated (if,
1346 * of course, str2 is not NULL)
1349 build_string(char *strptr, int *strptr_i, char *str1, char *str2, char c)
1352 strptr[(*strptr_i)++] = c;
1355 strptr[(*strptr_i)++] = *(str1++);
1359 strptr[(*strptr_i)++] = *(str2++);
1361 strptr[(*strptr_i)] = '\0';
1365 * This is the workhorse of our ModeBuf suite; this actually generates the
1366 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1369 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1371 /* we only need the flags that don't take args right now */
1372 static int flags[] = {
1373 /* MODE_CHANOP, 'o', */
1374 /* MODE_VOICE, 'v', */
1377 MODE_MODERATED, 'm',
1378 MODE_TOPICLIMIT, 't',
1379 MODE_INVITEONLY, 'i',
1380 MODE_NOPRIVMSGS, 'n',
1381 /* MODE_KEY, 'k', */
1382 /* MODE_BAN, 'b', */
1383 /* MODE_LIMIT, 'l', */
1389 struct Client *app_source; /* where the MODE appears to come from */
1391 char addbuf[20]; /* accumulates +psmtin, etc. */
1393 char rembuf[20]; /* accumulates -psmtin, etc. */
1395 char *bufptr; /* we make use of indirection to simplify the code */
1398 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1400 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1402 char *strptr; /* more indirection to simplify the code */
1405 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1408 char limitbuf[20]; /* convert limits to strings */
1410 unsigned int limitdel = MODE_LIMIT;
1414 /* If the ModeBuf is empty, we have nothing to do */
1415 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1418 /* Ok, if we were given the OPMODE flag, hide the source if its a user */
1419 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
1420 app_source = (cli_user(mbuf->mb_source))->server;
1422 app_source = mbuf->mb_source;
1425 * Account for user we're bouncing; we have to get it in on the first
1426 * bounced MODE, or we could have problems
1428 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1429 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1431 /* Calculate the simple flags */
1432 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1433 if (*flag_p & mbuf->mb_add)
1434 addbuf[addbuf_i++] = flag_p[1];
1435 else if (*flag_p & mbuf->mb_rem)
1436 rembuf[rembuf_i++] = flag_p[1];
1439 /* Now go through the modes with arguments... */
1440 for (i = 0; i < mbuf->mb_count; i++) {
1441 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1443 bufptr_i = &addbuf_i;
1446 bufptr_i = &rembuf_i;
1449 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1450 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1452 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
1453 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1455 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1456 totalbuflen -= IRCD_MAX(5, tmp) + 1;
1458 } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN)) {
1459 tmp = strlen(MB_STRING(mbuf, i));
1461 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1462 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1464 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_KEY ? 'k' : 'b';
1465 totalbuflen -= tmp + 1;
1467 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1468 /* if it's a limit, we also format the number */
1469 sprintf_irc(limitbuf, "%d", MB_UINT(mbuf, i));
1471 tmp = strlen(limitbuf);
1473 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1474 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1476 bufptr[(*bufptr_i)++] = 'l';
1477 totalbuflen -= tmp + 1;
1482 /* terminate the mode strings */
1483 addbuf[addbuf_i] = '\0';
1484 rembuf[rembuf_i] = '\0';
1486 /* If we're building a user visible MODE or HACK... */
1487 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1488 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1489 MODEBUF_DEST_LOG)) {
1490 /* Set up the parameter strings */
1496 for (i = 0; i < mbuf->mb_count; i++) {
1497 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1500 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1502 strptr_i = &addstr_i;
1505 strptr_i = &remstr_i;
1508 /* deal with clients... */
1509 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1510 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1512 /* deal with strings... */
1513 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1514 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1517 * deal with limit; note we cannot include the limit parameter if we're
1520 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1521 (MODE_ADD | MODE_LIMIT))
1522 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1525 /* send the messages off to their destination */
1526 if (mbuf->mb_dest & MODEBUF_DEST_HACK2) {
1527 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1528 "[%Tu]", cli_name(app_source),
1529 mbuf->mb_channel->chname,
1530 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1531 addbuf, remstr, addstr,
1532 mbuf->mb_channel->creationtime);
1533 sendcmdto_serv_butone(&me, CMD_DESYNCH, mbuf->mb_connect,
1534 ":HACK: %s MODE %s %s%s%s%s%s%s [%Tu]",
1535 cli_name(app_source), mbuf->mb_channel->chname,
1536 rembuf_i ? "-" : "", rembuf,
1537 addbuf_i ? "+" : "", addbuf, remstr, addstr,
1538 mbuf->mb_channel->creationtime);
1541 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1542 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1543 "%s%s%s%s%s%s [%Tu]", cli_name(app_source),
1544 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1545 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1546 mbuf->mb_channel->creationtime);
1548 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1549 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1550 "[%Tu]", cli_name(app_source),
1551 mbuf->mb_channel->chname,
1552 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1553 addbuf, remstr, addstr,
1554 mbuf->mb_channel->creationtime);
1556 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1557 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1558 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1559 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1560 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1562 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1563 sendcmdto_channel_butserv(app_source, CMD_MODE, mbuf->mb_channel,
1564 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1565 rembuf_i ? "-" : "", rembuf,
1566 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1569 /* Now are we supposed to propagate to other servers? */
1570 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1571 /* set up parameter string */
1578 * limit is supressed if we're removing it; we have to figure out which
1579 * direction is the direction for it to be removed, though...
1581 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1583 for (i = 0; i < mbuf->mb_count; i++) {
1584 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1587 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1589 strptr_i = &addstr_i;
1592 strptr_i = &remstr_i;
1595 /* deal with modes that take clients */
1596 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1597 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1599 /* deal with modes that take strings */
1600 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1601 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1604 * deal with the limit. Logic here is complicated; if HACK2 is set,
1605 * we're bouncing the mode, so sense is reversed, and we have to
1606 * include the original limit if it looks like it's being removed
1608 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1609 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1612 /* we were told to deop the source */
1613 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1614 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1615 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1616 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1618 /* mark that we've done this, so we don't do it again */
1619 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1622 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1623 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1624 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1625 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1626 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1627 addbuf, remstr, addstr);
1628 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1630 * If HACK2 was set, we're bouncing; we send the MODE back to the
1631 * connection we got it from with the senses reversed and a TS of 0;
1634 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1635 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1636 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1637 mbuf->mb_channel->creationtime);
1640 * We're propagating a normal MODE command to the rest of the network;
1641 * we send the actual channel TS unless this is a HACK3 or a HACK4
1643 if (IsServer(mbuf->mb_source))
1644 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1645 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1646 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1647 addbuf, remstr, addstr,
1648 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1649 mbuf->mb_channel->creationtime);
1651 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1652 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1653 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1654 addbuf, remstr, addstr);
1658 /* We've drained the ModeBuf... */
1663 /* reinitialize the mode-with-arg slots */
1664 for (i = 0; i < MAXMODEPARAMS; i++) {
1665 /* If we saved any, pack them down */
1666 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1667 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1668 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1670 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1672 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1673 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1675 MB_TYPE(mbuf, i) = 0;
1676 MB_UINT(mbuf, i) = 0;
1679 /* If we're supposed to flush it all, do so--all hail tail recursion */
1680 if (all && mbuf->mb_count)
1681 return modebuf_flush_int(mbuf, 1);
1687 * This routine just initializes a ModeBuf structure with the information
1688 * needed and the options given.
1691 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1692 struct Client *connect, struct Channel *chan, unsigned int dest)
1697 assert(0 != source);
1703 mbuf->mb_source = source;
1704 mbuf->mb_connect = connect;
1705 mbuf->mb_channel = chan;
1706 mbuf->mb_dest = dest;
1709 /* clear each mode-with-parameter slot */
1710 for (i = 0; i < MAXMODEPARAMS; i++) {
1711 MB_TYPE(mbuf, i) = 0;
1712 MB_UINT(mbuf, i) = 0;
1717 * This routine simply adds modes to be added or deleted; do a binary OR
1718 * with either MODE_ADD or MODE_DEL
1721 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1724 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1726 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1727 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
1729 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1732 if (mode & MODE_ADD) {
1733 mbuf->mb_rem &= ~mode;
1734 mbuf->mb_add |= mode;
1736 mbuf->mb_add &= ~mode;
1737 mbuf->mb_rem |= mode;
1742 * This routine adds a mode to be added or deleted that takes a unsigned
1743 * int parameter; mode may *only* be the relevant mode flag ORed with one
1744 * of MODE_ADD or MODE_DEL
1747 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1750 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1752 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1753 MB_UINT(mbuf, mbuf->mb_count) = uint;
1755 /* when we've reached the maximal count, flush the buffer */
1756 if (++mbuf->mb_count >=
1757 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1758 modebuf_flush_int(mbuf, 0);
1762 * This routine adds a mode to be added or deleted that takes a string
1763 * parameter; mode may *only* be the relevant mode flag ORed with one of
1764 * MODE_ADD or MODE_DEL
1767 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
1771 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1773 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
1774 MB_STRING(mbuf, mbuf->mb_count) = string;
1776 /* when we've reached the maximal count, flush the buffer */
1777 if (++mbuf->mb_count >=
1778 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1779 modebuf_flush_int(mbuf, 0);
1783 * This routine adds a mode to be added or deleted that takes a client
1784 * parameter; mode may *only* be the relevant mode flag ORed with one of
1785 * MODE_ADD or MODE_DEL
1788 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
1789 struct Client *client)
1792 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1794 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1795 MB_CLIENT(mbuf, mbuf->mb_count) = client;
1797 /* when we've reached the maximal count, flush the buffer */
1798 if (++mbuf->mb_count >=
1799 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1800 modebuf_flush_int(mbuf, 0);
1804 * This is the exported binding for modebuf_flush()
1807 modebuf_flush(struct ModeBuf *mbuf)
1809 return modebuf_flush_int(mbuf, 1);
1813 * This extracts the simple modes contained in mbuf
1816 modebuf_extract(struct ModeBuf *mbuf, char *buf)
1818 static int flags[] = {
1819 /* MODE_CHANOP, 'o', */
1820 /* MODE_VOICE, 'v', */
1823 MODE_MODERATED, 'm',
1824 MODE_TOPICLIMIT, 't',
1825 MODE_INVITEONLY, 'i',
1826 MODE_NOPRIVMSGS, 'n',
1828 /* MODE_BAN, 'b', */
1833 int i, bufpos = 0, len;
1835 char *key = 0, limitbuf[20];
1844 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
1845 if (MB_TYPE(mbuf, i) & MODE_ADD) {
1846 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT);
1848 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
1849 key = MB_STRING(mbuf, i);
1850 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
1851 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%d", MB_UINT(mbuf, i));
1858 buf[bufpos++] = '+'; /* start building buffer */
1860 for (flag_p = flags; flag_p[0]; flag_p += 2)
1862 buf[bufpos++] = flag_p[1];
1864 for (i = 0, len = bufpos; i < len; i++) {
1866 build_string(buf, &bufpos, key, 0, ' ');
1867 else if (buf[i] == 'l')
1868 build_string(buf, &bufpos, limitbuf, 0, ' ');
1877 * Simple function to invalidate bans
1880 mode_ban_invalidate(struct Channel *chan)
1882 struct Membership *member;
1884 for (member = chan->members; member; member = member->next_member)
1885 ClearBanValid(member);
1889 * Simple function to drop invite structures
1892 mode_invite_clear(struct Channel *chan)
1894 while (chan->invites)
1895 del_invite(chan->invites->value.cptr, chan);
1898 /* What we've done for mode_parse so far... */
1899 #define DONE_LIMIT 0x01 /* We've set the limit */
1900 #define DONE_KEY 0x02 /* We've set the key */
1901 #define DONE_BANLIST 0x04 /* We've sent the ban list */
1902 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
1903 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
1906 struct ModeBuf *mbuf;
1907 struct Client *cptr;
1908 struct Client *sptr;
1909 struct Channel *chptr;
1920 struct SLink banlist[MAXPARA];
1923 struct Client *client;
1924 } cli_change[MAXPARA];
1928 * Here's a helper function to deal with sending along "Not oper" or
1929 * "Not member" messages
1932 send_notoper(struct ParseState *state)
1934 if (state->done & DONE_NOTOPER)
1937 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
1938 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
1940 state->done |= DONE_NOTOPER;
1944 * Helper function to convert limits
1947 mode_parse_limit(struct ParseState *state, int *flag_p)
1949 unsigned int t_limit;
1951 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
1952 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
1955 if (state->parc <= 0) { /* warn if not enough args */
1956 if (MyUser(state->sptr))
1957 need_more_params(state->sptr, "MODE +l");
1961 t_limit = atoi(state->parv[state->args_used++]); /* grab arg */
1965 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
1966 (!t_limit || t_limit == state->chptr->mode.limit))
1969 t_limit = state->chptr->mode.limit;
1971 /* If they're not an oper, they can't change modes */
1972 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
1973 send_notoper(state);
1977 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
1979 state->done |= DONE_LIMIT;
1984 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
1986 if (state->flags & MODE_PARSE_SET) { /* set the limit */
1987 if (state->dir & MODE_ADD) {
1988 state->chptr->mode.mode |= flag_p[0];
1989 state->chptr->mode.limit = t_limit;
1991 state->chptr->mode.mode &= ~flag_p[0];
1992 state->chptr->mode.limit = 0;
1998 * Helper function to convert keys
2001 mode_parse_key(struct ParseState *state, int *flag_p)
2006 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2009 if (state->parc <= 0) { /* warn if not enough args */
2010 if (MyUser(state->sptr))
2011 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2016 t_str = state->parv[state->args_used++]; /* grab arg */
2020 /* If they're not an oper, they can't change modes */
2021 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2022 send_notoper(state);
2026 if (state->done & DONE_KEY) /* allow key to be set only once */
2028 state->done |= DONE_KEY;
2032 /* clean up the key string */
2034 while (*++s > ' ' && *s != ':' && --t_len)
2038 if (!*t_str) { /* warn if empty */
2039 if (MyUser(state->sptr))
2040 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2048 /* can't add a key if one is set, nor can one remove the wrong key */
2049 if (!(state->flags & MODE_PARSE_FORCE))
2050 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2051 (state->dir == MODE_DEL &&
2052 ircd_strcmp(state->chptr->mode.key, t_str))) {
2053 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2057 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2058 !ircd_strcmp(state->chptr->mode.key, t_str))
2059 return; /* no key change */
2061 if (state->flags & MODE_PARSE_BOUNCE) {
2062 if (*state->chptr->mode.key) /* reset old key */
2063 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2064 state->chptr->mode.key, 0);
2065 else /* remove new bogus key */
2066 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2067 } else /* send new key */
2068 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2070 if (state->flags & MODE_PARSE_SET) {
2071 if (state->dir == MODE_ADD) /* set the new key */
2072 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2073 else /* remove the old key */
2074 *state->chptr->mode.key = '\0';
2079 * Helper function to convert bans
2082 mode_parse_ban(struct ParseState *state, int *flag_p)
2085 struct SLink *ban, *newban = 0;
2087 if (state->parc <= 0) { /* Not enough args, send ban list */
2088 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2089 send_ban_list(state->sptr, state->chptr);
2090 state->done |= DONE_BANLIST;
2096 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2099 t_str = state->parv[state->args_used++]; /* grab arg */
2103 /* If they're not an oper, they can't change modes */
2104 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2105 send_notoper(state);
2109 if ((s = strchr(t_str, ' ')))
2112 if (!*t_str || *t_str == ':') { /* warn if empty */
2113 if (MyUser(state->sptr))
2114 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2119 t_str = collapse(pretty_mask(t_str));
2121 /* remember the ban for the moment... */
2122 if (state->dir == MODE_ADD) {
2123 newban = state->banlist + (state->numbans++);
2126 DupString(newban->value.ban.banstr, t_str);
2127 newban->value.ban.who = cli_name(state->sptr);
2128 newban->value.ban.when = TStime();
2130 newban->flags = CHFL_BAN | MODE_ADD;
2132 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
2133 newban->flags |= CHFL_BAN_IPMASK;
2136 if (!state->chptr->banlist) {
2137 state->chptr->banlist = newban; /* add our ban with its flags */
2138 state->done |= DONE_BANCLEAN;
2142 /* Go through all bans */
2143 for (ban = state->chptr->banlist; ban; ban = ban->next) {
2144 /* first, clean the ban flags up a bit */
2145 if (!(state->done & DONE_BANCLEAN))
2146 /* Note: We're overloading *lots* of bits here; be careful! */
2147 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
2151 * MODE_ADD - Ban was added; if we're bouncing modes,
2152 * then we'll remove it below; otherwise,
2153 * we'll have to allocate a real ban
2155 * MODE_DEL - Ban was marked for deletion; if we're
2156 * bouncing modes, we'll have to re-add it,
2157 * otherwise, we'll have to remove it
2159 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
2160 * with a ban already set; if we're
2161 * bouncing modes, we'll have to bounce
2162 * this one; otherwise, we'll just ignore
2163 * it when we process added bans
2166 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
2167 ban->flags |= MODE_DEL; /* delete one ban */
2169 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2171 } else if (state->dir == MODE_ADD) {
2172 /* if the ban already exists, don't worry about it */
2173 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
2174 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2177 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
2178 if (!(ban->flags & MODE_DEL))
2179 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
2180 } else if (!mmatch(t_str, ban->value.ban.banstr))
2181 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
2184 ban->next = newban; /* add our ban with its flags */
2185 break; /* get out of loop */
2189 state->done |= DONE_BANCLEAN;
2193 * This is the bottom half of the ban processor
2196 mode_process_bans(struct ParseState *state)
2198 struct SLink *ban, *newban, *prevban, *nextban;
2204 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2206 banlen = strlen(ban->value.ban.banstr);
2208 nextban = ban->next;
2210 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
2212 prevban->next = 0; /* Break the list; ban isn't a real ban */
2214 state->chptr->banlist = 0;
2219 MyFree(ban->value.ban.banstr);
2222 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
2223 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2224 ban->value.ban.banstr,
2225 state->flags & MODE_PARSE_SET);
2227 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2228 if (prevban) /* clip it out of the list... */
2229 prevban->next = ban->next;
2231 state->chptr->banlist = ban->next;
2236 MyFree(ban->value.ban.who);
2240 continue; /* next ban; keep prevban like it is */
2242 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
2243 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
2245 prevban->next = 0; /* Break the list; ban isn't a real ban */
2247 state->chptr->banlist = 0;
2249 /* If we're supposed to ignore it, do so. */
2250 if (ban->flags & CHFL_BAN_OVERLAPPED &&
2251 !(state->flags & MODE_PARSE_BOUNCE)) {
2255 MyFree(ban->value.ban.banstr);
2257 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2258 (len > MAXBANLENGTH || count >= MAXBANS)) {
2259 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2260 ban->value.ban.banstr);
2264 MyFree(ban->value.ban.banstr);
2266 /* add the ban to the buffer */
2267 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2268 ban->value.ban.banstr,
2269 !(state->flags & MODE_PARSE_SET));
2271 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2272 newban = make_link();
2273 newban->value.ban.banstr = ban->value.ban.banstr;
2274 DupString(newban->value.ban.who, ban->value.ban.who);
2275 newban->value.ban.when = ban->value.ban.when;
2276 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
2278 newban->next = state->chptr->banlist; /* and link it in */
2279 state->chptr->banlist = newban;
2288 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2290 if (changed) /* if we changed the ban list, we must invalidate the bans */
2291 mode_ban_invalidate(state->chptr);
2295 * Helper function to process client changes
2298 mode_parse_client(struct ParseState *state, int *flag_p)
2301 struct Client *acptr;
2304 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2307 if (state->parc <= 0) /* return if not enough args */
2310 t_str = state->parv[state->args_used++]; /* grab arg */
2314 /* If they're not an oper, they can't change modes */
2315 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2316 send_notoper(state);
2320 if (MyUser(state->sptr)) /* find client we're manipulating */
2321 acptr = find_chasing(state->sptr, t_str, NULL);
2323 acptr = findNUser(t_str);
2326 return; /* find_chasing() already reported an error to the user */
2328 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2329 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2330 state->cli_change[i].flag & flag_p[0]))
2331 break; /* found a slot */
2333 /* Store what we're doing to them */
2334 state->cli_change[i].flag = state->dir | flag_p[0];
2335 state->cli_change[i].client = acptr;
2339 * Helper function to process the changed client list
2342 mode_process_clients(struct ParseState *state)
2345 struct Membership *member;
2347 for (i = 0; state->cli_change[i].flag; i++) {
2348 assert(0 != state->cli_change[i].client);
2350 /* look up member link */
2351 if (!(member = find_member_link(state->chptr,
2352 state->cli_change[i].client)) ||
2353 (MyUser(state->sptr) && IsZombie(member))) {
2354 if (MyUser(state->sptr))
2355 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2356 cli_name(state->cli_change[i].client),
2357 state->chptr->chname);
2361 if ((state->cli_change[i].flag & MODE_ADD &&
2362 (state->cli_change[i].flag & member->status)) ||
2363 (state->cli_change[i].flag & MODE_DEL &&
2364 !(state->cli_change[i].flag & member->status)))
2365 continue; /* no change made, don't do anything */
2367 /* see if the deop is allowed */
2368 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2369 (MODE_DEL | MODE_CHANOP)) {
2370 /* prevent +k users from being deopped */
2371 if (IsChannelService(state->cli_change[i].client)) {
2372 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
2373 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
2375 (IsServer(state->sptr) ? cli_name(state->sptr) :
2376 cli_name((cli_user(state->sptr))->server)));
2378 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
2379 send_reply(state->sptr, ERR_ISCHANSERVICE,
2380 cli_name(state->cli_change[i].client),
2381 state->chptr->chname);
2386 /* don't allow local opers to be deopped on local channels */
2387 if (MyUser(state->sptr) && state->cli_change[i].client != state->sptr &&
2388 IsLocalChannel(state->chptr->chname) &&
2389 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
2390 send_reply(state->sptr, ERR_ISOPERLCHAN,
2391 cli_name(state->cli_change[i].client),
2392 state->chptr->chname);
2397 /* accumulate the change */
2398 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
2399 state->cli_change[i].client);
2401 /* actually effect the change */
2402 if (state->flags & MODE_PARSE_SET) {
2403 if (state->cli_change[i].flag & MODE_ADD) {
2404 member->status |= (state->cli_change[i].flag &
2405 (MODE_CHANOP | MODE_VOICE));
2406 if (state->cli_change[i].flag & MODE_CHANOP)
2407 ClearDeopped(member);
2409 member->status &= ~(state->cli_change[i].flag &
2410 (MODE_CHANOP | MODE_VOICE));
2412 } /* for (i = 0; state->cli_change[i].flags; i++) { */
2416 * Helper function to process the simple modes
2419 mode_parse_mode(struct ParseState *state, int *flag_p)
2421 /* If they're not an oper, they can't change modes */
2422 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2423 send_notoper(state);
2430 if (state->dir == MODE_ADD) {
2431 state->add |= flag_p[0];
2432 state->del &= ~flag_p[0];
2434 if (flag_p[0] & MODE_SECRET) {
2435 state->add &= ~MODE_PRIVATE;
2436 state->del |= MODE_PRIVATE;
2437 } else if (flag_p[0] & MODE_PRIVATE) {
2438 state->add &= ~MODE_SECRET;
2439 state->del |= MODE_SECRET;
2442 state->add &= ~flag_p[0];
2443 state->del |= flag_p[0];
2446 assert(0 == (state->add & state->del));
2447 assert((MODE_SECRET | MODE_PRIVATE) !=
2448 (state->add & (MODE_SECRET | MODE_PRIVATE)));
2452 * This routine is intended to parse MODE or OPMODE commands and effect the
2453 * changes (or just build the bounce buffer). We pass the starting offset
2457 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
2458 struct Channel *chptr, int parc, char *parv[], unsigned int flags)
2460 static int chan_flags[] = {
2465 MODE_MODERATED, 'm',
2466 MODE_TOPICLIMIT, 't',
2467 MODE_INVITEONLY, 'i',
2468 MODE_NOPRIVMSGS, 'n',
2478 unsigned int t_mode;
2480 struct ParseState state;
2491 state.chptr = chptr;
2494 state.flags = flags;
2495 state.dir = MODE_ADD;
2499 state.args_used = 0;
2500 state.max_args = MAXMODEPARAMS;
2503 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
2504 state.banlist[i].next = 0;
2505 state.banlist[i].value.ban.banstr = 0;
2506 state.banlist[i].value.ban.who = 0;
2507 state.banlist[i].value.ban.when = 0;
2508 state.banlist[i].flags = 0;
2509 state.cli_change[i].flag = 0;
2510 state.cli_change[i].client = 0;
2513 modestr = state.parv[state.args_used++];
2517 for (; *modestr; modestr++) {
2518 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
2519 if (flag_p[1] == *modestr)
2522 if (!flag_p[0]) { /* didn't find it? complain and continue */
2523 if (MyUser(state.sptr))
2524 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
2529 case '+': /* switch direction to MODE_ADD */
2530 case '-': /* switch direction to MODE_DEL */
2531 state.dir = flag_p[0];
2534 case 'l': /* deal with limits */
2535 mode_parse_limit(&state, flag_p);
2538 case 'k': /* deal with keys */
2539 mode_parse_key(&state, flag_p);
2542 case 'b': /* deal with bans */
2543 mode_parse_ban(&state, flag_p);
2546 case 'o': /* deal with ops/voice */
2548 mode_parse_client(&state, flag_p);
2551 default: /* deal with other modes */
2552 mode_parse_mode(&state, flag_p);
2554 } /* switch (*modestr) { */
2555 } /* for (; *modestr; modestr++) { */
2557 if (state.flags & MODE_PARSE_BURST)
2558 break; /* don't interpret any more arguments */
2560 if (state.parc > 0) { /* process next argument in string */
2561 modestr = state.parv[state.args_used++];
2565 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
2568 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
2569 break; /* we're then going to bounce the mode! */
2571 recv_ts = atoi(modestr);
2573 if (recv_ts && recv_ts < state.chptr->creationtime)
2574 state.chptr->creationtime = recv_ts; /* respect earlier TS */
2576 break; /* break out of while loop */
2577 } else if (state.flags & MODE_PARSE_STRICT ||
2578 (MyUser(state.sptr) && state.max_args <= 0)) {
2579 state.parc++; /* we didn't actually gobble the argument */
2581 break; /* break out of while loop */
2584 } /* while (*modestr) { */
2587 * the rest of the function finishes building resultant MODEs; if the
2588 * origin isn't a member or an oper, skip it.
2590 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
2591 return state.args_used; /* tell our parent how many args we gobbled */
2593 t_mode = state.chptr->mode.mode;
2595 if (state.del & t_mode) { /* delete any modes to be deleted... */
2596 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
2598 t_mode &= ~state.del;
2600 if (state.add & ~t_mode) { /* add any modes to be added... */
2601 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
2603 t_mode |= state.add;
2606 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
2607 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
2608 !(t_mode & MODE_INVITEONLY))
2609 mode_invite_clear(state.chptr);
2611 state.chptr->mode.mode = t_mode;
2614 if (state.flags & MODE_PARSE_WIPEOUT) {
2615 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
2616 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
2617 state.chptr->mode.limit);
2618 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
2619 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
2620 state.chptr->mode.key, 0);
2623 if (state.done & DONE_BANCLEAN) /* process bans */
2624 mode_process_bans(&state);
2626 /* process client changes */
2627 if (state.cli_change[0].flag)
2628 mode_process_clients(&state);
2630 return state.args_used; /* tell our parent how many args we gobbled */
2634 * Initialize a join buffer
2637 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
2638 struct Client *connect, unsigned int type, char *comment,
2644 assert(0 != source);
2645 assert(0 != connect);
2647 jbuf->jb_source = source; /* just initialize struct JoinBuf */
2648 jbuf->jb_connect = connect;
2649 jbuf->jb_type = type;
2650 jbuf->jb_comment = comment;
2651 jbuf->jb_create = create;
2653 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
2654 type == JOINBUF_TYPE_PART ||
2655 type == JOINBUF_TYPE_PARTALL) ?
2656 STARTJOINLEN : STARTCREATELEN) +
2657 (comment ? strlen(comment) + 2 : 0));
2659 for (i = 0; i < MAXJOINARGS; i++)
2660 jbuf->jb_channels[i] = 0;
2664 * Add a channel to the join buffer
2667 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
2674 if (jbuf->jb_type == JOINBUF_TYPE_JOIN)
2675 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
2680 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
2681 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
2682 /* Send notification to channel */
2683 if (!(flags & CHFL_ZOMBIE))
2684 sendcmdto_channel_butserv(jbuf->jb_source, CMD_PART, chan,
2685 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2686 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2687 else if (MyUser(jbuf->jb_source))
2688 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
2689 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2690 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2691 /* XXX: Shouldn't we send a PART here anyway? */
2692 /* to users on the channel? Why? From their POV, the user isn't on
2693 * the channel anymore anyway. We don't send to servers until below,
2694 * when we gang all the channel parts together. Note that this is
2695 * exactly the same logic, albeit somewhat more concise, as was in
2696 * the original m_part.c */
2698 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2699 IsLocalChannel(chan->chname)) /* got to remove user here */
2700 remove_user_from_channel(jbuf->jb_source, chan);
2702 /* Add user to channel */
2703 add_user_to_channel(chan, jbuf->jb_source, flags);
2705 /* send notification to all servers */
2706 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !IsLocalChannel(chan->chname))
2707 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
2708 "%H %Tu", chan, chan->creationtime);
2710 /* Send the notification to the channel */
2711 sendcmdto_channel_butserv(jbuf->jb_source, CMD_JOIN, chan, ":%H", chan);
2713 /* send an op, too, if needed */
2714 if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE &&
2715 !IsModelessChannel(chan->chname))
2716 sendcmdto_channel_butserv(jbuf->jb_source, CMD_MODE, chan, "%H +o %C",
2717 chan, jbuf->jb_source);
2720 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || IsLocalChannel(chan->chname))
2721 return; /* don't send to remote */
2723 /* figure out if channel name will cause buffer to be overflowed */
2724 len = chan ? strlen(chan->chname) + 1 : 2;
2725 if (jbuf->jb_strlen + len > BUFSIZE)
2726 joinbuf_flush(jbuf);
2728 /* add channel to list of channels to send and update counts */
2729 jbuf->jb_channels[jbuf->jb_count++] = chan;
2730 jbuf->jb_strlen += len;
2732 /* if we've used up all slots, flush */
2733 if (jbuf->jb_count >= MAXJOINARGS)
2734 joinbuf_flush(jbuf);
2738 * Flush the channel list to remote servers
2741 joinbuf_flush(struct JoinBuf *jbuf)
2743 char chanlist[BUFSIZE];
2747 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2748 jbuf->jb_type == JOINBUF_TYPE_JOIN)
2749 return 0; /* no joins to process */
2751 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
2752 build_string(chanlist, &chanlist_i,
2753 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
2754 i == 0 ? '\0' : ',');
2755 if (JOINBUF_TYPE_PART == jbuf->jb_type)
2756 /* Remove user from channel */
2757 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
2759 jbuf->jb_channels[i] = 0; /* mark slot empty */
2762 jbuf->jb_count = 0; /* reset base counters */
2763 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
2764 STARTJOINLEN : STARTCREATELEN) +
2765 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
2767 /* and send the appropriate command */
2768 switch (jbuf->jb_type) {
2769 case JOINBUF_TYPE_CREATE:
2770 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
2771 "%s %Tu", chanlist, jbuf->jb_create);
2774 case JOINBUF_TYPE_PART:
2775 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
2776 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,