2 * IRC - Internet Relay Chat, ircd/channel.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Co Center
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 1, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "ircd_alloc.h"
29 #include "ircd_chattr.h"
30 #include "ircd_defs.h"
31 #include "ircd_features.h"
33 #include "ircd_reply.h"
34 #include "ircd_snprintf.h"
35 #include "ircd_string.h"
42 #include "querycmds.h"
49 #include "sprintf_irc.h"
60 struct Channel* GlobalChannelList = 0;
62 static unsigned int membershipAllocCount;
63 static struct Membership* membershipFreeList;
65 static struct SLink *next_overlapped_ban(void);
66 static int del_banid(struct Channel *, char *, int);
67 void del_invite(struct Client *, struct Channel *);
69 const char* const PartFmt1 = ":%s " MSG_PART " %s";
70 const char* const PartFmt2 = ":%s " MSG_PART " %s :%s";
71 const char* const PartFmt1serv = "%s%s " TOK_PART " %s";
72 const char* const PartFmt2serv = "%s%s " TOK_PART " %s :%s";
75 static struct SLink* next_ban;
76 static struct SLink* prev_ban;
77 static struct SLink* removed_bans_list;
80 * Use a global variable to remember if an oper set a mode on a local channel. Ugly,
81 * but the only way to do it without changing set_mode intensively.
83 int LocalChanOperMode = 0;
87 * return the length (>=0) of a chain of links.
89 static int list_length(struct SLink *lp)
93 for (; lp; lp = lp->next)
99 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
101 struct Membership *m;
105 /* Servers don't have member links */
106 if (IsServer(cptr)||IsMe(cptr))
109 /* +k users are typically on a LOT of channels. So we iterate over who
110 * is in the channel. X/W are +k and are in about 5800 channels each.
111 * however there are typically no more than 1000 people in a channel
114 if (IsChannelService(cptr)) {
117 assert(m->channel == chptr);
123 /* Users on the other hand aren't allowed on more than 15 channels. 50%
124 * of users that are on channels are on 2 or less, 95% are on 7 or less,
125 * and 99% are on 10 or less.
128 m = (cli_user(cptr))->channel;
130 assert(m->user == cptr);
131 if (m->channel == chptr)
140 * find_chasing - Find the client structure for a nick name (user)
141 * using history mechanism if necessary. If the client is not found, an error
142 * message (NO SUCH NICK) is generated. If the client was found
143 * through the history, chasing will be 1 and otherwise 0.
145 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
147 struct Client* who = FindClient(user);
154 if (!(who = get_history(user, feature_int(FEAT_KILLCHASETIMELIMIT)))) {
155 send_reply(sptr, ERR_NOSUCHNICK, user);
164 * Create a string of form "foo!bar@fubar" given foo, bar and fubar
165 * as the parameters. If NULL, they become "*".
167 static char *make_nick_user_host(const char *nick, const char *name,
170 static char namebuf[NICKLEN + USERLEN + HOSTLEN + 3];
171 sprintf_irc(namebuf, "%s!%s@%s", nick, name, host);
176 * Create a string of form "foo!bar@123.456.789.123" given foo, bar and the
177 * IP-number as the parameters. If NULL, they become "*".
179 static char *make_nick_user_ip(char *nick, char *name, struct in_addr ip)
181 static char ipbuf[NICKLEN + USERLEN + 16 + 3];
182 sprintf_irc(ipbuf, "%s!%s@%s", nick, name, ircd_ntoa((const char*) &ip));
187 * Subtract one user from channel i (and free channel
188 * block, if channel became empty).
189 * Returns: true (1) if channel still exists
190 * false (0) if the channel was destroyed
192 int sub1_from_channel(struct Channel* chptr)
197 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
199 assert(0 != chptr->members);
204 assert(0 == chptr->members);
206 /* Channel became (or was) empty: Remove channel */
207 if (is_listed(chptr))
210 for (i = 0; i <= HighestFd; i++)
212 struct Client *acptr = 0;
213 if ((acptr = LocalClientArray[i]) && cli_listing(acptr) &&
214 (cli_listing(acptr))->chptr == chptr)
216 list_next_channels(acptr, 1);
217 break; /* Only one client can list a channel */
222 * Now, find all invite links from channel structure
224 while ((tmp = chptr->invites))
225 del_invite(tmp->value.cptr, chptr);
227 tmp = chptr->banlist;
232 MyFree(obtmp->value.ban.banstr);
233 MyFree(obtmp->value.ban.who);
237 chptr->prev->next = chptr->next;
239 GlobalChannelList = chptr->next;
241 chptr->next->prev = chptr->prev;
243 --UserStats.channels;
245 * make sure that channel actually got removed from hash table
247 assert(chptr->hnext == chptr);
255 * `cptr' must be the client adding the ban.
257 * If `change' is true then add `banid' to channel `chptr'.
258 * Returns 0 if the ban was added.
259 * Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
260 * Return -1 otherwise.
262 * Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
263 * when `change' is false, otherwise they will be removed from the banlist.
264 * Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
265 * respectively will return these bans until NULL is returned.
267 * If `firsttime' is true, the ban list as returned by next_overlapped_ban()
268 * is reset (unless a non-zero value is returned, in which case the
269 * CHFL_BAN_OVERLAPPED flag might not have been reset!).
273 int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
274 int change, int firsttime)
279 int removed_bans = 0;
280 int len = strlen(banid);
285 assert(0 == prev_ban);
286 assert(0 == removed_bans_list);
290 for (banp = &chptr->banlist; *banp;)
292 len += strlen((*banp)->value.ban.banstr);
294 if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
296 if (!strcmp((*banp)->value.ban.banstr, banid))
298 (*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
302 else if (!mmatch((*banp)->value.ban.banstr, banid))
304 if (!mmatch(banid, (*banp)->value.ban.banstr))
306 struct SLink *tmp = *banp;
312 len -= strlen(tmp->value.ban.banstr);
316 /* Silently remove overlapping bans */
317 MyFree(tmp->value.ban.banstr);
318 MyFree(tmp->value.ban.who);
322 /* These will be sent to the user later as -b */
323 tmp->next = removed_bans_list;
324 removed_bans_list = tmp;
328 else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
330 tmp->flags |= CHFL_BAN_OVERLAPPED;
341 (*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
342 banp = &(*banp)->next;
345 if (MyUser(cptr) && !removed_bans &&
346 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
347 (cnt >= feature_int(FEAT_MAXBANS))))
349 send_reply(cptr, ERR_BANLISTFULL, chptr->chname, banid);
355 struct Membership* member;
357 ban->next = chptr->banlist;
359 ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
360 assert(0 != ban->value.ban.banstr);
361 strcpy(ban->value.ban.banstr, banid);
363 ban->value.ban.who = (char*) MyMalloc(strlen(cli_name(cptr)) + 1);
364 assert(0 != ban->value.ban.who);
365 strcpy(ban->value.ban.who, cli_name(cptr));
367 ban->value.ban.when = TStime();
368 ban->flags = CHFL_BAN; /* This bit is never used I think... */
369 if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
370 ban->flags |= CHFL_BAN_IPMASK;
371 chptr->banlist = ban;
374 * Erase ban-valid-bit
376 for (member = chptr->members; member; member = member->next_member)
377 ClearBanValid(member); /* `ban' == channel member ! */
382 static struct SLink *next_overlapped_ban(void)
384 struct SLink *tmp = next_ban;
388 for (ban = tmp->next; ban; ban = ban->next)
389 if ((ban->flags & CHFL_BAN_OVERLAPPED))
396 struct SLink *next_removed_overlapped_ban(void)
398 struct SLink *tmp = removed_bans_list;
401 if (prev_ban->value.ban.banstr) /* Can be set to NULL in set_mode() */
402 MyFree(prev_ban->value.ban.banstr);
403 MyFree(prev_ban->value.ban.who);
408 removed_bans_list = removed_bans_list->next;
416 * If `change' is true, delete `banid' from channel `chptr'.
417 * Returns `false' if removal was (or would have been) successful.
419 static int del_banid(struct Channel *chptr, char *banid, int change)
426 for (ban = &(chptr->banlist); *ban; ban = &((*ban)->next)) {
427 if (0 == ircd_strcmp(banid, (*ban)->value.ban.banstr))
432 struct Membership* member;
434 MyFree(tmp->value.ban.banstr);
435 MyFree(tmp->value.ban.who);
438 * Erase ban-valid-bit, for channel members that are banned
440 for (member = chptr->members; member; member = member->next_member)
441 if (CHFL_BANVALIDMASK == (member->status & CHFL_BANVALIDMASK))
442 ClearBanValid(member); /* `tmp' == channel member */
451 * find_channel_member - returns Membership * if a person is joined and not a zombie
453 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
455 struct Membership* member;
458 member = find_member_link(chptr, cptr);
459 return (member && !IsZombie(member)) ? member : 0;
463 * is_banned - a non-zero value if banned else 0.
465 static int is_banned(struct Client *cptr, struct Channel *chptr,
466 struct Membership* member)
475 if (member && IsBanValid(member))
476 return IsBanned(member);
478 s = make_nick_user_host(cli_name(cptr), (cli_user(cptr))->username,
479 (cli_user(cptr))->host);
481 for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
482 if ((tmp->flags & CHFL_BAN_IPMASK)) {
484 ip_s = make_nick_user_ip(cli_name(cptr), (cli_user(cptr))->username,
486 if (match(tmp->value.ban.banstr, ip_s) == 0)
489 else if (match(tmp->value.ban.banstr, s) == 0)
505 return (tmp != NULL);
509 * adds a user to a channel by adding another link to the channels member
512 void add_user_to_channel(struct Channel* chptr, struct Client* who,
520 struct Membership* member = membershipFreeList;
522 membershipFreeList = member->next_member;
524 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
525 ++membershipAllocCount;
530 member->channel = chptr;
531 member->status = flags;
533 member->next_member = chptr->members;
534 if (member->next_member)
535 member->next_member->prev_member = member;
536 member->prev_member = 0;
537 chptr->members = member;
539 member->next_channel = (cli_user(who))->channel;
540 if (member->next_channel)
541 member->next_channel->prev_channel = member;
542 member->prev_channel = 0;
543 (cli_user(who))->channel = member;
546 ++((cli_user(who))->joined);
550 static int remove_member_from_channel(struct Membership* member)
552 struct Channel* chptr;
554 chptr = member->channel;
556 * unlink channel member list
558 if (member->next_member)
559 member->next_member->prev_member = member->prev_member;
560 if (member->prev_member)
561 member->prev_member->next_member = member->next_member;
563 member->channel->members = member->next_member;
566 * unlink client channel list
568 if (member->next_channel)
569 member->next_channel->prev_channel = member->prev_channel;
570 if (member->prev_channel)
571 member->prev_channel->next_channel = member->next_channel;
573 (cli_user(member->user))->channel = member->next_channel;
575 --(cli_user(member->user))->joined;
577 member->next_member = membershipFreeList;
578 membershipFreeList = member;
580 return sub1_from_channel(chptr);
583 static int channel_all_zombies(struct Channel* chptr)
585 struct Membership* member;
587 for (member = chptr->members; member; member = member->next_member) {
588 if (!IsZombie(member))
595 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
598 struct Membership* member;
601 if ((member = find_member_link(chptr, cptr))) {
602 if (remove_member_from_channel(member)) {
603 if (channel_all_zombies(chptr)) {
605 * XXX - this looks dangerous but isn't if we got the referential
606 * integrity right for channels
608 while (remove_member_from_channel(chptr->members))
615 void remove_user_from_all_channels(struct Client* cptr)
617 struct Membership* chan;
619 assert(0 != cli_user(cptr));
621 while ((chan = (cli_user(cptr))->channel))
622 remove_user_from_channel(cptr, chan->channel);
625 int is_chan_op(struct Client *cptr, struct Channel *chptr)
627 struct Membership* member;
629 if ((member = find_member_link(chptr, cptr)))
630 return (!IsZombie(member) && IsChanOp(member));
635 static int is_deopped(struct Client *cptr, struct Channel *chptr)
637 struct Membership* member;
640 if ((member = find_member_link(chptr, cptr)))
641 return IsDeopped(member);
643 return (IsUser(cptr) ? 1 : 0);
646 int is_zombie(struct Client *cptr, struct Channel *chptr)
648 struct Membership* member;
652 if ((member = find_member_link(chptr, cptr)))
653 return IsZombie(member);
657 int has_voice(struct Client* cptr, struct Channel* chptr)
659 struct Membership* member;
662 if ((member = find_member_link(chptr, cptr)))
663 return (!IsZombie(member) && HasVoice(member));
668 int member_can_send_to_channel(struct Membership* member)
672 if (IsVoicedOrOpped(member))
675 * If it's moderated, and you aren't a priviledged user, you can't
678 if (member->channel->mode.mode & MODE_MODERATED)
681 * If you're banned then you can't speak either.
682 * but because of the amount of CPU time that is_banned chews
683 * we only check it for our clients.
685 if (MyUser(member->user) && is_banned(member->user, member->channel, member))
690 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
692 struct Membership *member;
695 * Servers can always speak on channels.
700 member = find_channel_member(cptr, chptr);
703 * You can't speak if your off channel, if the channel is modeless, or
704 * +n (no external messages) or +m (moderated).
707 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED))
708 || IsModelessChannel(chptr->chname))
713 return member_can_send_to_channel(member);
717 * find_no_nickchange_channel
718 * if a member and not opped or voiced and banned
719 * return the name of the first channel banned on
721 const char* find_no_nickchange_channel(struct Client* cptr)
724 struct Membership* member;
725 for (member = (cli_user(cptr))->channel; member;
726 member = member->next_channel) {
727 if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
728 return member->channel->chname;
736 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
737 * with the parameters in pbuf.
739 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
740 struct Channel *chptr)
747 if (chptr->mode.mode & MODE_SECRET)
749 else if (chptr->mode.mode & MODE_PRIVATE)
751 if (chptr->mode.mode & MODE_MODERATED)
753 if (chptr->mode.mode & MODE_TOPICLIMIT)
755 if (chptr->mode.mode & MODE_INVITEONLY)
757 if (chptr->mode.mode & MODE_NOPRIVMSGS)
759 if (chptr->mode.limit) {
761 sprintf_irc(pbuf, "%d", chptr->mode.limit);
764 if (*chptr->mode.key) {
766 if (chptr->mode.limit)
768 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
769 strcat(pbuf, chptr->mode.key);
777 * send "cptr" a full list of the modes for channel chptr.
779 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
781 static unsigned int current_flags[4] =
782 { 0, CHFL_CHANOP | CHFL_VOICE, CHFL_VOICE, CHFL_CHANOP };
788 struct Membership* member;
790 char modebuf[MODEBUFLEN];
791 char parabuf[MODEBUFLEN];
797 if (IsLocalChannel(chptr->chname))
800 member = chptr->members;
801 lp2 = chptr->banlist;
803 *modebuf = *parabuf = '\0';
804 channel_modes(cptr, modebuf, parabuf, chptr);
806 for (first = 1; full; first = 0) /* Loop for multiple messages */
808 full = 0; /* Assume by default we get it
809 all in one message */
811 /* (Continued) prefix: "<Y> B <channel> <TS>" */
812 /* is there any better way we can do this? */
813 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
814 chptr->creationtime);
816 if (first && modebuf[1]) /* Add simple modes (iklmnpst)
819 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
820 msgq_append(&me, mb, " %s", modebuf);
823 msgq_append(&me, mb, " %s", parabuf);
827 * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
829 * Run 4 times over all members, to group the members with the
832 for (first = 1; flag_cnt < 4;
833 member = chptr->members, new_mode = 1, flag_cnt++)
835 for (; member; member = member->next_member)
837 if ((member->status & CHFL_VOICED_OR_OPPED) !=
838 current_flags[flag_cnt])
839 continue; /* Skip members with different flags */
840 if (msgq_bufleft(mb) < NUMNICKLEN + 4)
841 /* The 4 is a possible ",:ov" */
843 full = 1; /* Make sure we continue after
845 new_mode = 1; /* Ensure the new BURST line contains the current
847 break; /* Do not add this member to this message */
849 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
850 first = 0; /* From now on, us comma's to add new nicks */
853 * Do we have a nick with a new mode ?
854 * Or are we starting a new BURST line?
859 if (IsVoicedOrOpped(member)) {
863 if (IsChanOp(member))
865 if (HasVoice(member))
868 msgq_append(&me, mb, tbuf);
878 /* Attach all bans, space seperated " :%ban ban ..." */
879 for (first = 2; lp2; lp2 = lp2->next)
881 len = strlen(lp2->value.ban.banstr);
882 if (msgq_bufleft(mb) < len + 1 + first)
883 /* The +1 stands for the added ' '.
884 * The +first stands for the added ":%".
890 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
891 lp2->value.ban.banstr);
896 send_buffer(cptr, mb, 0); /* Send this message */
898 } /* Continue when there was something
899 that didn't fit (full==1) */
905 * by Carlo Wood (Run), 05 Oct 1998.
909 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
910 * When the user name or host name are too long (USERLEN and HOSTLEN
911 * respectively) then they are cut off at the start with a '*'.
913 * The following transformations are made:
916 * 2) xxx.xxx -> *!*@host
917 * 3) xxx!yyy -> nick!user@*
918 * 4) xxx@yyy -> *!user@host
919 * 5) xxx!yyy@zzz -> nick!user@host
921 char *pretty_mask(char *mask)
923 static char star[2] = { '*', 0 };
924 char *last_dot = NULL;
927 /* Case 1: default */
932 /* Do a _single_ pass through the characters of the mask: */
933 for (ptr = mask; *ptr; ++ptr)
937 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
941 else if (*ptr == '@')
943 /* Case 4: Found last '@' (without finding a '!' yet) */
948 else if (*ptr == '.')
950 /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
960 /* Case 4 or 5: Found last '@' */
966 if (user == star && last_dot)
976 char *nick_end = (user != star) ? user - 1 : ptr;
977 if (nick_end - nick > NICKLEN)
983 char *user_end = (host != star) ? host - 1 : ptr;
984 if (user_end - user > USERLEN)
986 user = user_end - USERLEN;
991 if (host != star && ptr - host > HOSTLEN)
993 host = ptr - HOSTLEN;
996 return make_nick_user_host(nick, user, host);
999 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1006 for (lp = chptr->banlist; lp; lp = lp->next)
1007 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->value.ban.banstr,
1008 lp->value.ban.who, lp->value.ban.when);
1010 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1013 /* We are now treating the <key> part of /join <channel list> <key> as a key
1014 * ring; that is, we try one key against the actual channel key, and if that
1015 * doesn't work, we try the next one, and so on. -Kev -Texaco
1016 * Returns: 0 on match, 1 otherwise
1017 * This version contributed by SeKs <intru@info.polymtl.ca>
1019 static int compall(char *key, char *keyring)
1024 p1 = key; /* point to the key... */
1025 while (*p1 && *p1 == *keyring)
1026 { /* step through the key and ring until they
1032 if (!*p1 && (!*keyring || *keyring == ','))
1033 /* ok, if we're at the end of the and also at the end of one of the keys
1034 in the keyring, we have a match */
1037 if (!*keyring) /* if we're at the end of the key ring, there
1038 weren't any matches, so we return 1 */
1041 /* Not at the end of the key ring, so step
1042 through to the next key in the ring: */
1043 while (*keyring && *(keyring++) != ',');
1045 goto top; /* and check it against the key */
1048 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
1051 int overrideJoin = 0;
1054 * Now a banned user CAN join if invited -- Nemesi
1055 * Now a user CAN escape channel limit if invited -- bfriendly
1056 * Now a user CAN escape anything if invited -- Isomer
1059 for (lp = (cli_user(sptr))->invited; lp; lp = lp->next)
1060 if (lp->value.chptr == chptr)
1063 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
1064 a HACK(4) notice will be sent if he would not have been supposed
1065 to join normally. */
1066 if (IsLocalChannel(chptr->chname) && HasPriv(sptr, PRIV_WALK_LCHAN) &&
1067 !BadPtr(key) && compall("OVERRIDE",key) == 0)
1068 overrideJoin = MAGIC_OPER_OVERRIDE;
1070 if (chptr->mode.mode & MODE_INVITEONLY)
1071 return overrideJoin + ERR_INVITEONLYCHAN;
1073 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
1074 return overrideJoin + ERR_CHANNELISFULL;
1076 if (is_banned(sptr, chptr, NULL))
1077 return overrideJoin + ERR_BANNEDFROMCHAN;
1080 * now using compall (above) to test against a whole key ring -Kev
1082 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
1083 return overrideJoin + ERR_BADCHANNELKEY;
1086 return ERR_DONTCHEAT;
1092 * Remove bells and commas from channel name
1094 void clean_channelname(char *cn)
1098 for (i = 0; cn[i]; i++) {
1099 if (i >= CHANNELLEN || !IsChannelChar(cn[i])) {
1103 if (IsChannelLower(cn[i])) {
1104 cn[i] = ToLower(cn[i]);
1110 if ((unsigned char)(cn[i]) == 0xd0)
1111 cn[i] = (char) 0xf0;
1118 * Get Channel block for i (and allocate a new channel
1119 * block, if it didn't exists before).
1121 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1123 struct Channel *chptr;
1126 if (EmptyString(chname))
1129 len = strlen(chname);
1130 if (MyUser(cptr) && len > CHANNELLEN)
1133 *(chname + CHANNELLEN) = '\0';
1135 if ((chptr = FindChannel(chname)))
1137 if (flag == CGT_CREATE)
1139 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1141 ++UserStats.channels;
1142 memset(chptr, 0, sizeof(struct Channel));
1143 strcpy(chptr->chname, chname);
1144 if (GlobalChannelList)
1145 GlobalChannelList->prev = chptr;
1147 chptr->next = GlobalChannelList;
1148 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1149 GlobalChannelList = chptr;
1155 void add_invite(struct Client *cptr, struct Channel *chptr)
1157 struct SLink *inv, **tmp;
1159 del_invite(cptr, chptr);
1161 * Delete last link in chain if the list is max length
1163 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1164 if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER))
1165 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1167 * Add client to channel invite list
1170 inv->value.cptr = cptr;
1171 inv->next = chptr->invites;
1172 chptr->invites = inv;
1174 * Add channel to the end of the client invite list
1176 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1178 inv->value.chptr = chptr;
1181 (cli_user(cptr))->invites++;
1185 * Delete Invite block from channel invite list and client invite list
1187 void del_invite(struct Client *cptr, struct Channel *chptr)
1189 struct SLink **inv, *tmp;
1191 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1192 if (tmp->value.cptr == cptr)
1197 (cli_user(cptr))->invites--;
1201 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1202 if (tmp->value.chptr == chptr)
1211 /* List and skip all channels that are listen */
1212 void list_next_channels(struct Client *cptr, int nr)
1214 struct ListingArgs *args = cli_listing(cptr);
1215 struct Channel *chptr = args->chptr;
1216 chptr->mode.mode &= ~MODE_LISTED;
1217 while (is_listed(chptr) || --nr >= 0)
1219 for (; chptr; chptr = chptr->next)
1221 if (!cli_user(cptr) || (SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
1223 if (chptr->users > args->min_users && chptr->users < args->max_users &&
1224 chptr->creationtime > args->min_time &&
1225 chptr->creationtime < args->max_time &&
1226 (!args->topic_limits || (*chptr->topic &&
1227 chptr->topic_time > args->min_topic_time &&
1228 chptr->topic_time < args->max_topic_time)))
1230 if (ShowChannel(cptr,chptr))
1231 send_reply(cptr, RPL_LIST, chptr->chname, chptr->users,
1233 chptr = chptr->next;
1239 MyFree(cli_listing(cptr));
1240 cli_listing(cptr) = NULL;
1241 send_reply(cptr, RPL_LISTEND);
1247 (cli_listing(cptr))->chptr = chptr;
1248 chptr->mode.mode |= MODE_LISTED;
1261 * X --a--> A --b--> B --d--> D
1265 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1266 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1268 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1269 * Remove the user immedeately when no users are left on the channel.
1270 * b) On server B : remove the user (who/lp) from the channel, send a
1271 * PART upstream (to A) and pass on the KICK.
1272 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1273 * channel, and pass on the KICK.
1274 * d) On server D : remove the user (who/lp) from the channel, and pass on
1278 * - Setting the ZOMBIE flag never hurts, we either remove the
1279 * client after that or we don't.
1280 * - The KICK message was already passed on, as should be in all cases.
1281 * - `who' is removed in all cases except case a) when users are left.
1282 * - A PART is only sent upstream in case b).
1288 * 1 --- 2 --- 3 --- 4 --- 5
1292 * We also need to turn 'who' into a zombie on servers 1 and 6,
1293 * because a KICK from 'who' (kicking someone else in that direction)
1294 * can arrive there afterwards - which should not be bounced itself.
1295 * Therefore case a) also applies for servers 1 and 6.
1299 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
1300 struct Client* sptr, struct Channel* chptr)
1302 assert(0 != member);
1307 /* Default for case a): */
1310 /* Case b) or c) ?: */
1311 if (MyUser(who)) /* server 4 */
1313 if (IsServer(cptr)) /* Case b) ? */
1314 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1315 remove_user_from_channel(who, chptr);
1318 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1320 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1321 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1322 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1324 remove_user_from_channel(who, chptr);
1329 /* Case a) (servers 1, 2, 3 and 6) */
1330 if (channel_all_zombies(chptr))
1331 remove_user_from_channel(who, chptr);
1333 /* XXX Can't actually call Debug here; if the channel is all zombies,
1334 * chptr will no longer exist when we get here.
1335 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1339 int number_of_zombies(struct Channel *chptr)
1341 struct Membership* member;
1345 for (member = chptr->members; member; member = member->next_member) {
1346 if (IsZombie(member))
1353 * This helper function builds an argument string in strptr, consisting
1354 * of the original string, a space, and str1 and str2 concatenated (if,
1355 * of course, str2 is not NULL)
1358 build_string(char *strptr, int *strptr_i, char *str1, char *str2, char c)
1361 strptr[(*strptr_i)++] = c;
1364 strptr[(*strptr_i)++] = *(str1++);
1368 strptr[(*strptr_i)++] = *(str2++);
1370 strptr[(*strptr_i)] = '\0';
1374 * This is the workhorse of our ModeBuf suite; this actually generates the
1375 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1378 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1380 /* we only need the flags that don't take args right now */
1381 static int flags[] = {
1382 /* MODE_CHANOP, 'o', */
1383 /* MODE_VOICE, 'v', */
1386 MODE_MODERATED, 'm',
1387 MODE_TOPICLIMIT, 't',
1388 MODE_INVITEONLY, 'i',
1389 MODE_NOPRIVMSGS, 'n',
1390 /* MODE_KEY, 'k', */
1391 /* MODE_BAN, 'b', */
1392 /* MODE_LIMIT, 'l', */
1398 struct Client *app_source; /* where the MODE appears to come from */
1400 char addbuf[20]; /* accumulates +psmtin, etc. */
1402 char rembuf[20]; /* accumulates -psmtin, etc. */
1404 char *bufptr; /* we make use of indirection to simplify the code */
1407 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1409 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1411 char *strptr; /* more indirection to simplify the code */
1414 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1417 char limitbuf[20]; /* convert limits to strings */
1419 unsigned int limitdel = MODE_LIMIT;
1423 /* If the ModeBuf is empty, we have nothing to do */
1424 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1427 /* Ok, if we were given the OPMODE flag, hide the source if its a user */
1428 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
1429 app_source = (cli_user(mbuf->mb_source))->server;
1431 app_source = mbuf->mb_source;
1434 * Account for user we're bouncing; we have to get it in on the first
1435 * bounced MODE, or we could have problems
1437 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1438 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1440 /* Calculate the simple flags */
1441 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1442 if (*flag_p & mbuf->mb_add)
1443 addbuf[addbuf_i++] = flag_p[1];
1444 else if (*flag_p & mbuf->mb_rem)
1445 rembuf[rembuf_i++] = flag_p[1];
1448 /* Now go through the modes with arguments... */
1449 for (i = 0; i < mbuf->mb_count; i++) {
1450 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1452 bufptr_i = &addbuf_i;
1455 bufptr_i = &rembuf_i;
1458 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1459 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1461 if ((totalbuflen - IRCD_MAX(5, 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_CHANOP ? 'o' : 'v';
1465 totalbuflen -= IRCD_MAX(5, tmp) + 1;
1467 } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN)) {
1468 tmp = strlen(MB_STRING(mbuf, i));
1470 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1471 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1473 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_KEY ? 'k' : 'b';
1474 totalbuflen -= tmp + 1;
1476 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1477 /* if it's a limit, we also format the number */
1478 sprintf_irc(limitbuf, "%d", MB_UINT(mbuf, i));
1480 tmp = strlen(limitbuf);
1482 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1483 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1485 bufptr[(*bufptr_i)++] = 'l';
1486 totalbuflen -= tmp + 1;
1491 /* terminate the mode strings */
1492 addbuf[addbuf_i] = '\0';
1493 rembuf[rembuf_i] = '\0';
1495 /* If we're building a user visible MODE or HACK... */
1496 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1497 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1498 MODEBUF_DEST_LOG)) {
1499 /* Set up the parameter strings */
1505 for (i = 0; i < mbuf->mb_count; i++) {
1506 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1509 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1511 strptr_i = &addstr_i;
1514 strptr_i = &remstr_i;
1517 /* deal with clients... */
1518 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1519 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1521 /* deal with strings... */
1522 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1523 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1526 * deal with limit; note we cannot include the limit parameter if we're
1529 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1530 (MODE_ADD | MODE_LIMIT))
1531 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1534 /* send the messages off to their destination */
1535 if (mbuf->mb_dest & MODEBUF_DEST_HACK2) {
1536 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1537 "[%Tu]", cli_name(app_source),
1538 mbuf->mb_channel->chname,
1539 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1540 addbuf, remstr, addstr,
1541 mbuf->mb_channel->creationtime);
1542 sendcmdto_serv_butone(&me, CMD_DESYNCH, mbuf->mb_connect,
1543 ":HACK: %s MODE %s %s%s%s%s%s%s [%Tu]",
1544 cli_name(app_source), mbuf->mb_channel->chname,
1545 rembuf_i ? "-" : "", rembuf,
1546 addbuf_i ? "+" : "", addbuf, remstr, addstr,
1547 mbuf->mb_channel->creationtime);
1550 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1551 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1552 "%s%s%s%s%s%s [%Tu]", cli_name(app_source),
1553 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1554 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1555 mbuf->mb_channel->creationtime);
1557 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1558 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1559 "[%Tu]", cli_name(app_source),
1560 mbuf->mb_channel->chname,
1561 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1562 addbuf, remstr, addstr,
1563 mbuf->mb_channel->creationtime);
1565 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1566 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1567 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1568 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1569 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1571 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1572 sendcmdto_channel_butserv(app_source, CMD_MODE, mbuf->mb_channel,
1573 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1574 rembuf_i ? "-" : "", rembuf,
1575 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1578 /* Now are we supposed to propagate to other servers? */
1579 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1580 /* set up parameter string */
1587 * limit is supressed if we're removing it; we have to figure out which
1588 * direction is the direction for it to be removed, though...
1590 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1592 for (i = 0; i < mbuf->mb_count; i++) {
1593 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1596 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1598 strptr_i = &addstr_i;
1601 strptr_i = &remstr_i;
1604 /* deal with modes that take clients */
1605 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1606 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1608 /* deal with modes that take strings */
1609 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1610 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1613 * deal with the limit. Logic here is complicated; if HACK2 is set,
1614 * we're bouncing the mode, so sense is reversed, and we have to
1615 * include the original limit if it looks like it's being removed
1617 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1618 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1621 /* we were told to deop the source */
1622 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1623 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1624 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1625 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1627 /* mark that we've done this, so we don't do it again */
1628 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1631 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1632 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1633 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1634 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1635 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1636 addbuf, remstr, addstr);
1637 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1639 * If HACK2 was set, we're bouncing; we send the MODE back to the
1640 * connection we got it from with the senses reversed and a TS of 0;
1643 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1644 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1645 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1646 mbuf->mb_channel->creationtime);
1649 * We're propagating a normal MODE command to the rest of the network;
1650 * we send the actual channel TS unless this is a HACK3 or a HACK4
1652 if (IsServer(mbuf->mb_source))
1653 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1654 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1655 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1656 addbuf, remstr, addstr,
1657 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1658 mbuf->mb_channel->creationtime);
1660 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1661 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1662 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1663 addbuf, remstr, addstr);
1667 /* We've drained the ModeBuf... */
1672 /* reinitialize the mode-with-arg slots */
1673 for (i = 0; i < MAXMODEPARAMS; i++) {
1674 /* If we saved any, pack them down */
1675 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1676 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1677 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1679 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1681 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1682 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1684 MB_TYPE(mbuf, i) = 0;
1685 MB_UINT(mbuf, i) = 0;
1688 /* If we're supposed to flush it all, do so--all hail tail recursion */
1689 if (all && mbuf->mb_count)
1690 return modebuf_flush_int(mbuf, 1);
1696 * This routine just initializes a ModeBuf structure with the information
1697 * needed and the options given.
1700 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1701 struct Client *connect, struct Channel *chan, unsigned int dest)
1706 assert(0 != source);
1712 mbuf->mb_source = source;
1713 mbuf->mb_connect = connect;
1714 mbuf->mb_channel = chan;
1715 mbuf->mb_dest = dest;
1718 /* clear each mode-with-parameter slot */
1719 for (i = 0; i < MAXMODEPARAMS; i++) {
1720 MB_TYPE(mbuf, i) = 0;
1721 MB_UINT(mbuf, i) = 0;
1726 * This routine simply adds modes to be added or deleted; do a binary OR
1727 * with either MODE_ADD or MODE_DEL
1730 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1733 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1735 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1736 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
1738 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1741 if (mode & MODE_ADD) {
1742 mbuf->mb_rem &= ~mode;
1743 mbuf->mb_add |= mode;
1745 mbuf->mb_add &= ~mode;
1746 mbuf->mb_rem |= mode;
1751 * This routine adds a mode to be added or deleted that takes a unsigned
1752 * int parameter; mode may *only* be the relevant mode flag ORed with one
1753 * of MODE_ADD or MODE_DEL
1756 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1759 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1761 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1762 MB_UINT(mbuf, mbuf->mb_count) = uint;
1764 /* when we've reached the maximal count, flush the buffer */
1765 if (++mbuf->mb_count >=
1766 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1767 modebuf_flush_int(mbuf, 0);
1771 * This routine adds a mode to be added or deleted that takes a string
1772 * parameter; mode may *only* be the relevant mode flag ORed with one of
1773 * MODE_ADD or MODE_DEL
1776 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
1780 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1782 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
1783 MB_STRING(mbuf, mbuf->mb_count) = string;
1785 /* when we've reached the maximal count, flush the buffer */
1786 if (++mbuf->mb_count >=
1787 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1788 modebuf_flush_int(mbuf, 0);
1792 * This routine adds a mode to be added or deleted that takes a client
1793 * parameter; mode may *only* be the relevant mode flag ORed with one of
1794 * MODE_ADD or MODE_DEL
1797 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
1798 struct Client *client)
1801 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1803 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1804 MB_CLIENT(mbuf, mbuf->mb_count) = client;
1806 /* when we've reached the maximal count, flush the buffer */
1807 if (++mbuf->mb_count >=
1808 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1809 modebuf_flush_int(mbuf, 0);
1813 * This is the exported binding for modebuf_flush()
1816 modebuf_flush(struct ModeBuf *mbuf)
1818 return modebuf_flush_int(mbuf, 1);
1822 * This extracts the simple modes contained in mbuf
1825 modebuf_extract(struct ModeBuf *mbuf, char *buf)
1827 static int flags[] = {
1828 /* MODE_CHANOP, 'o', */
1829 /* MODE_VOICE, 'v', */
1832 MODE_MODERATED, 'm',
1833 MODE_TOPICLIMIT, 't',
1834 MODE_INVITEONLY, 'i',
1835 MODE_NOPRIVMSGS, 'n',
1837 /* MODE_BAN, 'b', */
1842 int i, bufpos = 0, len;
1844 char *key = 0, limitbuf[20];
1853 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
1854 if (MB_TYPE(mbuf, i) & MODE_ADD) {
1855 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT);
1857 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
1858 key = MB_STRING(mbuf, i);
1859 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
1860 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%d", MB_UINT(mbuf, i));
1867 buf[bufpos++] = '+'; /* start building buffer */
1869 for (flag_p = flags; flag_p[0]; flag_p += 2)
1871 buf[bufpos++] = flag_p[1];
1873 for (i = 0, len = bufpos; i < len; i++) {
1875 build_string(buf, &bufpos, key, 0, ' ');
1876 else if (buf[i] == 'l')
1877 build_string(buf, &bufpos, limitbuf, 0, ' ');
1886 * Simple function to invalidate bans
1889 mode_ban_invalidate(struct Channel *chan)
1891 struct Membership *member;
1893 for (member = chan->members; member; member = member->next_member)
1894 ClearBanValid(member);
1898 * Simple function to drop invite structures
1901 mode_invite_clear(struct Channel *chan)
1903 while (chan->invites)
1904 del_invite(chan->invites->value.cptr, chan);
1907 /* What we've done for mode_parse so far... */
1908 #define DONE_LIMIT 0x01 /* We've set the limit */
1909 #define DONE_KEY 0x02 /* We've set the key */
1910 #define DONE_BANLIST 0x04 /* We've sent the ban list */
1911 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
1912 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
1915 struct ModeBuf *mbuf;
1916 struct Client *cptr;
1917 struct Client *sptr;
1918 struct Channel *chptr;
1929 struct SLink banlist[MAXPARA];
1932 struct Client *client;
1933 } cli_change[MAXPARA];
1937 * Here's a helper function to deal with sending along "Not oper" or
1938 * "Not member" messages
1941 send_notoper(struct ParseState *state)
1943 if (state->done & DONE_NOTOPER)
1946 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
1947 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
1949 state->done |= DONE_NOTOPER;
1953 * Helper function to convert limits
1956 mode_parse_limit(struct ParseState *state, int *flag_p)
1958 unsigned int t_limit;
1960 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
1961 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
1964 if (state->parc <= 0) { /* warn if not enough args */
1965 if (MyUser(state->sptr))
1966 need_more_params(state->sptr, "MODE +l");
1970 t_limit = atoi(state->parv[state->args_used++]); /* grab arg */
1974 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
1975 (!t_limit || t_limit == state->chptr->mode.limit))
1978 t_limit = state->chptr->mode.limit;
1980 /* If they're not an oper, they can't change modes */
1981 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
1982 send_notoper(state);
1986 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
1988 state->done |= DONE_LIMIT;
1993 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
1995 if (state->flags & MODE_PARSE_SET) { /* set the limit */
1996 if (state->dir & MODE_ADD) {
1997 state->chptr->mode.mode |= flag_p[0];
1998 state->chptr->mode.limit = t_limit;
2000 state->chptr->mode.mode &= ~flag_p[0];
2001 state->chptr->mode.limit = 0;
2007 * Helper function to convert keys
2010 mode_parse_key(struct ParseState *state, int *flag_p)
2015 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2018 if (state->parc <= 0) { /* warn if not enough args */
2019 if (MyUser(state->sptr))
2020 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2025 t_str = state->parv[state->args_used++]; /* grab arg */
2029 /* If they're not an oper, they can't change modes */
2030 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2031 send_notoper(state);
2035 if (state->done & DONE_KEY) /* allow key to be set only once */
2037 state->done |= DONE_KEY;
2041 /* clean up the key string */
2043 while (*++s > ' ' && *s != ':' && --t_len)
2047 if (!*t_str) { /* warn if empty */
2048 if (MyUser(state->sptr))
2049 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2057 /* can't add a key if one is set, nor can one remove the wrong key */
2058 if (!(state->flags & MODE_PARSE_FORCE))
2059 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2060 (state->dir == MODE_DEL &&
2061 ircd_strcmp(state->chptr->mode.key, t_str))) {
2062 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2066 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2067 !ircd_strcmp(state->chptr->mode.key, t_str))
2068 return; /* no key change */
2070 if (state->flags & MODE_PARSE_BOUNCE) {
2071 if (*state->chptr->mode.key) /* reset old key */
2072 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2073 state->chptr->mode.key, 0);
2074 else /* remove new bogus key */
2075 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2076 } else /* send new key */
2077 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2079 if (state->flags & MODE_PARSE_SET) {
2080 if (state->dir == MODE_ADD) /* set the new key */
2081 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2082 else /* remove the old key */
2083 *state->chptr->mode.key = '\0';
2088 * Helper function to convert bans
2091 mode_parse_ban(struct ParseState *state, int *flag_p)
2094 struct SLink *ban, *newban = 0;
2096 if (state->parc <= 0) { /* Not enough args, send ban list */
2097 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2098 send_ban_list(state->sptr, state->chptr);
2099 state->done |= DONE_BANLIST;
2105 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2108 t_str = state->parv[state->args_used++]; /* grab arg */
2112 /* If they're not an oper, they can't change modes */
2113 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2114 send_notoper(state);
2118 if ((s = strchr(t_str, ' ')))
2121 if (!*t_str || *t_str == ':') { /* warn if empty */
2122 if (MyUser(state->sptr))
2123 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2128 t_str = collapse(pretty_mask(t_str));
2130 /* remember the ban for the moment... */
2131 if (state->dir == MODE_ADD) {
2132 newban = state->banlist + (state->numbans++);
2135 DupString(newban->value.ban.banstr, t_str);
2136 newban->value.ban.who = cli_name(state->sptr);
2137 newban->value.ban.when = TStime();
2139 newban->flags = CHFL_BAN | MODE_ADD;
2141 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
2142 newban->flags |= CHFL_BAN_IPMASK;
2145 if (!state->chptr->banlist) {
2146 state->chptr->banlist = newban; /* add our ban with its flags */
2147 state->done |= DONE_BANCLEAN;
2151 /* Go through all bans */
2152 for (ban = state->chptr->banlist; ban; ban = ban->next) {
2153 /* first, clean the ban flags up a bit */
2154 if (!(state->done & DONE_BANCLEAN))
2155 /* Note: We're overloading *lots* of bits here; be careful! */
2156 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
2160 * MODE_ADD - Ban was added; if we're bouncing modes,
2161 * then we'll remove it below; otherwise,
2162 * we'll have to allocate a real ban
2164 * MODE_DEL - Ban was marked for deletion; if we're
2165 * bouncing modes, we'll have to re-add it,
2166 * otherwise, we'll have to remove it
2168 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
2169 * with a ban already set; if we're
2170 * bouncing modes, we'll have to bounce
2171 * this one; otherwise, we'll just ignore
2172 * it when we process added bans
2175 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
2176 ban->flags |= MODE_DEL; /* delete one ban */
2178 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2180 } else if (state->dir == MODE_ADD) {
2181 /* if the ban already exists, don't worry about it */
2182 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
2183 newban->flags &= ~MODE_ADD; /* don't add ban at all */
2184 MyFree(newban->value.ban.banstr); /* stopper a leak */
2185 state->numbans--; /* deallocate last ban */
2186 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2188 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
2189 if (!(ban->flags & MODE_DEL))
2190 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
2191 } else if (!mmatch(t_str, ban->value.ban.banstr))
2192 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
2194 if (!ban->next && (newban->flags & MODE_ADD)) {
2195 ban->next = newban; /* add our ban with its flags */
2196 break; /* get out of loop */
2200 state->done |= DONE_BANCLEAN;
2204 * This is the bottom half of the ban processor
2207 mode_process_bans(struct ParseState *state)
2209 struct SLink *ban, *newban, *prevban, *nextban;
2215 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2217 banlen = strlen(ban->value.ban.banstr);
2219 nextban = ban->next;
2221 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
2223 prevban->next = 0; /* Break the list; ban isn't a real ban */
2225 state->chptr->banlist = 0;
2230 MyFree(ban->value.ban.banstr);
2233 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
2234 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2235 ban->value.ban.banstr,
2236 state->flags & MODE_PARSE_SET);
2238 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2239 if (prevban) /* clip it out of the list... */
2240 prevban->next = ban->next;
2242 state->chptr->banlist = ban->next;
2247 MyFree(ban->value.ban.who);
2251 continue; /* next ban; keep prevban like it is */
2253 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
2254 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
2256 prevban->next = 0; /* Break the list; ban isn't a real ban */
2258 state->chptr->banlist = 0;
2260 /* If we're supposed to ignore it, do so. */
2261 if (ban->flags & CHFL_BAN_OVERLAPPED &&
2262 !(state->flags & MODE_PARSE_BOUNCE)) {
2266 MyFree(ban->value.ban.banstr);
2268 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2269 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2270 count >= feature_int(FEAT_MAXBANS))) {
2271 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2272 ban->value.ban.banstr);
2276 MyFree(ban->value.ban.banstr);
2278 /* add the ban to the buffer */
2279 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2280 ban->value.ban.banstr,
2281 !(state->flags & MODE_PARSE_SET));
2283 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2284 newban = make_link();
2285 newban->value.ban.banstr = ban->value.ban.banstr;
2286 DupString(newban->value.ban.who, ban->value.ban.who);
2287 newban->value.ban.when = ban->value.ban.when;
2288 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
2290 newban->next = state->chptr->banlist; /* and link it in */
2291 state->chptr->banlist = newban;
2300 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2302 if (changed) /* if we changed the ban list, we must invalidate the bans */
2303 mode_ban_invalidate(state->chptr);
2307 * Helper function to process client changes
2310 mode_parse_client(struct ParseState *state, int *flag_p)
2313 struct Client *acptr;
2316 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2319 if (state->parc <= 0) /* return if not enough args */
2322 t_str = state->parv[state->args_used++]; /* grab arg */
2326 /* If they're not an oper, they can't change modes */
2327 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2328 send_notoper(state);
2332 if (MyUser(state->sptr)) /* find client we're manipulating */
2333 acptr = find_chasing(state->sptr, t_str, NULL);
2335 acptr = findNUser(t_str);
2338 return; /* find_chasing() already reported an error to the user */
2340 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2341 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2342 state->cli_change[i].flag & flag_p[0]))
2343 break; /* found a slot */
2345 /* Store what we're doing to them */
2346 state->cli_change[i].flag = state->dir | flag_p[0];
2347 state->cli_change[i].client = acptr;
2351 * Helper function to process the changed client list
2354 mode_process_clients(struct ParseState *state)
2357 struct Membership *member;
2359 for (i = 0; state->cli_change[i].flag; i++) {
2360 assert(0 != state->cli_change[i].client);
2362 /* look up member link */
2363 if (!(member = find_member_link(state->chptr,
2364 state->cli_change[i].client)) ||
2365 (MyUser(state->sptr) && IsZombie(member))) {
2366 if (MyUser(state->sptr))
2367 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2368 cli_name(state->cli_change[i].client),
2369 state->chptr->chname);
2373 if ((state->cli_change[i].flag & MODE_ADD &&
2374 (state->cli_change[i].flag & member->status)) ||
2375 (state->cli_change[i].flag & MODE_DEL &&
2376 !(state->cli_change[i].flag & member->status)))
2377 continue; /* no change made, don't do anything */
2379 /* see if the deop is allowed */
2380 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2381 (MODE_DEL | MODE_CHANOP)) {
2382 /* prevent +k users from being deopped */
2383 if (IsChannelService(state->cli_change[i].client)) {
2384 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
2385 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
2387 (IsServer(state->sptr) ? cli_name(state->sptr) :
2388 cli_name((cli_user(state->sptr))->server)));
2390 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
2391 send_reply(state->sptr, ERR_ISCHANSERVICE,
2392 cli_name(state->cli_change[i].client),
2393 state->chptr->chname);
2398 /* don't allow local opers to be deopped on local channels */
2399 if (MyUser(state->sptr) && state->cli_change[i].client != state->sptr &&
2400 IsLocalChannel(state->chptr->chname) &&
2401 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
2402 send_reply(state->sptr, ERR_ISOPERLCHAN,
2403 cli_name(state->cli_change[i].client),
2404 state->chptr->chname);
2409 /* accumulate the change */
2410 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
2411 state->cli_change[i].client);
2413 /* actually effect the change */
2414 if (state->flags & MODE_PARSE_SET) {
2415 if (state->cli_change[i].flag & MODE_ADD) {
2416 member->status |= (state->cli_change[i].flag &
2417 (MODE_CHANOP | MODE_VOICE));
2418 if (state->cli_change[i].flag & MODE_CHANOP)
2419 ClearDeopped(member);
2421 member->status &= ~(state->cli_change[i].flag &
2422 (MODE_CHANOP | MODE_VOICE));
2424 } /* for (i = 0; state->cli_change[i].flags; i++) { */
2428 * Helper function to process the simple modes
2431 mode_parse_mode(struct ParseState *state, int *flag_p)
2433 /* If they're not an oper, they can't change modes */
2434 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2435 send_notoper(state);
2442 if (state->dir == MODE_ADD) {
2443 state->add |= flag_p[0];
2444 state->del &= ~flag_p[0];
2446 if (flag_p[0] & MODE_SECRET) {
2447 state->add &= ~MODE_PRIVATE;
2448 state->del |= MODE_PRIVATE;
2449 } else if (flag_p[0] & MODE_PRIVATE) {
2450 state->add &= ~MODE_SECRET;
2451 state->del |= MODE_SECRET;
2454 state->add &= ~flag_p[0];
2455 state->del |= flag_p[0];
2458 assert(0 == (state->add & state->del));
2459 assert((MODE_SECRET | MODE_PRIVATE) !=
2460 (state->add & (MODE_SECRET | MODE_PRIVATE)));
2464 * This routine is intended to parse MODE or OPMODE commands and effect the
2465 * changes (or just build the bounce buffer). We pass the starting offset
2469 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
2470 struct Channel *chptr, int parc, char *parv[], unsigned int flags)
2472 static int chan_flags[] = {
2477 MODE_MODERATED, 'm',
2478 MODE_TOPICLIMIT, 't',
2479 MODE_INVITEONLY, 'i',
2480 MODE_NOPRIVMSGS, 'n',
2490 unsigned int t_mode;
2492 struct ParseState state;
2503 state.chptr = chptr;
2506 state.flags = flags;
2507 state.dir = MODE_ADD;
2511 state.args_used = 0;
2512 state.max_args = MAXMODEPARAMS;
2515 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
2516 state.banlist[i].next = 0;
2517 state.banlist[i].value.ban.banstr = 0;
2518 state.banlist[i].value.ban.who = 0;
2519 state.banlist[i].value.ban.when = 0;
2520 state.banlist[i].flags = 0;
2521 state.cli_change[i].flag = 0;
2522 state.cli_change[i].client = 0;
2525 modestr = state.parv[state.args_used++];
2529 for (; *modestr; modestr++) {
2530 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
2531 if (flag_p[1] == *modestr)
2534 if (!flag_p[0]) { /* didn't find it? complain and continue */
2535 if (MyUser(state.sptr))
2536 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
2541 case '+': /* switch direction to MODE_ADD */
2542 case '-': /* switch direction to MODE_DEL */
2543 state.dir = flag_p[0];
2546 case 'l': /* deal with limits */
2547 mode_parse_limit(&state, flag_p);
2550 case 'k': /* deal with keys */
2551 mode_parse_key(&state, flag_p);
2554 case 'b': /* deal with bans */
2555 mode_parse_ban(&state, flag_p);
2558 case 'o': /* deal with ops/voice */
2560 mode_parse_client(&state, flag_p);
2563 default: /* deal with other modes */
2564 mode_parse_mode(&state, flag_p);
2566 } /* switch (*modestr) { */
2567 } /* for (; *modestr; modestr++) { */
2569 if (state.flags & MODE_PARSE_BURST)
2570 break; /* don't interpret any more arguments */
2572 if (state.parc > 0) { /* process next argument in string */
2573 modestr = state.parv[state.args_used++];
2577 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
2580 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
2581 break; /* we're then going to bounce the mode! */
2583 recv_ts = atoi(modestr);
2585 if (recv_ts && recv_ts < state.chptr->creationtime)
2586 state.chptr->creationtime = recv_ts; /* respect earlier TS */
2588 break; /* break out of while loop */
2589 } else if (state.flags & MODE_PARSE_STRICT ||
2590 (MyUser(state.sptr) && state.max_args <= 0)) {
2591 state.parc++; /* we didn't actually gobble the argument */
2593 break; /* break out of while loop */
2596 } /* while (*modestr) { */
2599 * the rest of the function finishes building resultant MODEs; if the
2600 * origin isn't a member or an oper, skip it.
2602 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
2603 return state.args_used; /* tell our parent how many args we gobbled */
2605 t_mode = state.chptr->mode.mode;
2607 if (state.del & t_mode) { /* delete any modes to be deleted... */
2608 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
2610 t_mode &= ~state.del;
2612 if (state.add & ~t_mode) { /* add any modes to be added... */
2613 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
2615 t_mode |= state.add;
2618 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
2619 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
2620 !(t_mode & MODE_INVITEONLY))
2621 mode_invite_clear(state.chptr);
2623 state.chptr->mode.mode = t_mode;
2626 if (state.flags & MODE_PARSE_WIPEOUT) {
2627 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
2628 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
2629 state.chptr->mode.limit);
2630 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
2631 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
2632 state.chptr->mode.key, 0);
2635 if (state.done & DONE_BANCLEAN) /* process bans */
2636 mode_process_bans(&state);
2638 /* process client changes */
2639 if (state.cli_change[0].flag)
2640 mode_process_clients(&state);
2642 return state.args_used; /* tell our parent how many args we gobbled */
2646 * Initialize a join buffer
2649 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
2650 struct Client *connect, unsigned int type, char *comment,
2656 assert(0 != source);
2657 assert(0 != connect);
2659 jbuf->jb_source = source; /* just initialize struct JoinBuf */
2660 jbuf->jb_connect = connect;
2661 jbuf->jb_type = type;
2662 jbuf->jb_comment = comment;
2663 jbuf->jb_create = create;
2665 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
2666 type == JOINBUF_TYPE_PART ||
2667 type == JOINBUF_TYPE_PARTALL) ?
2668 STARTJOINLEN : STARTCREATELEN) +
2669 (comment ? strlen(comment) + 2 : 0));
2671 for (i = 0; i < MAXJOINARGS; i++)
2672 jbuf->jb_channels[i] = 0;
2676 * Add a channel to the join buffer
2679 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
2686 if (jbuf->jb_type == JOINBUF_TYPE_JOIN)
2687 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
2692 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
2693 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
2694 /* Send notification to channel */
2695 if (!(flags & CHFL_ZOMBIE))
2696 sendcmdto_channel_butserv(jbuf->jb_source, CMD_PART, chan,
2697 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2698 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2699 else if (MyUser(jbuf->jb_source))
2700 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
2701 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2702 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2703 /* XXX: Shouldn't we send a PART here anyway? */
2704 /* to users on the channel? Why? From their POV, the user isn't on
2705 * the channel anymore anyway. We don't send to servers until below,
2706 * when we gang all the channel parts together. Note that this is
2707 * exactly the same logic, albeit somewhat more concise, as was in
2708 * the original m_part.c */
2710 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2711 IsLocalChannel(chan->chname)) /* got to remove user here */
2712 remove_user_from_channel(jbuf->jb_source, chan);
2714 /* Add user to channel */
2715 add_user_to_channel(chan, jbuf->jb_source, flags);
2717 /* send notification to all servers */
2718 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !IsLocalChannel(chan->chname))
2719 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
2720 "%H %Tu", chan, chan->creationtime);
2722 /* Send the notification to the channel */
2723 sendcmdto_channel_butserv(jbuf->jb_source, CMD_JOIN, chan, ":%H", chan);
2725 /* send an op, too, if needed */
2726 if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE &&
2727 !IsModelessChannel(chan->chname))
2728 sendcmdto_channel_butserv(jbuf->jb_source, CMD_MODE, chan, "%H +o %C",
2729 chan, jbuf->jb_source);
2732 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || IsLocalChannel(chan->chname))
2733 return; /* don't send to remote */
2735 /* figure out if channel name will cause buffer to be overflowed */
2736 len = chan ? strlen(chan->chname) + 1 : 2;
2737 if (jbuf->jb_strlen + len > BUFSIZE)
2738 joinbuf_flush(jbuf);
2740 /* add channel to list of channels to send and update counts */
2741 jbuf->jb_channels[jbuf->jb_count++] = chan;
2742 jbuf->jb_strlen += len;
2744 /* if we've used up all slots, flush */
2745 if (jbuf->jb_count >= MAXJOINARGS)
2746 joinbuf_flush(jbuf);
2750 * Flush the channel list to remote servers
2753 joinbuf_flush(struct JoinBuf *jbuf)
2755 char chanlist[BUFSIZE];
2759 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2760 jbuf->jb_type == JOINBUF_TYPE_JOIN)
2761 return 0; /* no joins to process */
2763 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
2764 build_string(chanlist, &chanlist_i,
2765 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
2766 i == 0 ? '\0' : ',');
2767 if (JOINBUF_TYPE_PART == jbuf->jb_type)
2768 /* Remove user from channel */
2769 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
2771 jbuf->jb_channels[i] = 0; /* mark slot empty */
2774 jbuf->jb_count = 0; /* reset base counters */
2775 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
2776 STARTJOINLEN : STARTCREATELEN) +
2777 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
2779 /* and send the appropriate command */
2780 switch (jbuf->jb_type) {
2781 case JOINBUF_TYPE_CREATE:
2782 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
2783 "%s %Tu", chanlist, jbuf->jb_create);
2786 case JOINBUF_TYPE_PART:
2787 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
2788 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,