2 * IRC - Internet Relay Chat, ircd/channel.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Co Center
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 1, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "ircd_alloc.h"
27 #include "ircd_chattr.h"
28 #include "ircd_defs.h"
29 #include "ircd_features.h"
31 #include "ircd_reply.h"
32 #include "ircd_snprintf.h"
33 #include "ircd_string.h"
40 #include "querycmds.h"
47 #include "sprintf_irc.h"
58 struct Channel* GlobalChannelList = 0;
60 static unsigned int membershipAllocCount;
61 static struct Membership* membershipFreeList;
63 static struct SLink *next_overlapped_ban(void);
64 static int del_banid(struct Channel *, char *, int);
65 void del_invite(struct Client *, struct Channel *);
67 const char* const PartFmt1 = ":%s " MSG_PART " %s";
68 const char* const PartFmt2 = ":%s " MSG_PART " %s :%s";
69 const char* const PartFmt1serv = "%s%s " TOK_PART " %s";
70 const char* const PartFmt2serv = "%s%s " TOK_PART " %s :%s";
73 static struct SLink* next_ban;
74 static struct SLink* prev_ban;
75 static struct SLink* removed_bans_list;
78 * Use a global variable to remember if an oper set a mode on a local channel. Ugly,
79 * but the only way to do it without changing set_mode intensively.
81 int LocalChanOperMode = 0;
85 * return the length (>=0) of a chain of links.
87 static int list_length(struct SLink *lp)
91 for (; lp; lp = lp->next)
97 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
103 /* Servers don't have member links */
104 if (IsServer(cptr)||IsMe(cptr))
107 /* +k users are typically on a LOT of channels. So we iterate over who
108 * is in the channel. X/W are +k and are in about 5800 channels each.
109 * however there are typically no more than 1000 people in a channel
112 if (IsChannelService(cptr)) {
115 assert(m->channel == chptr);
121 /* Users on the other hand aren't allowed on more than 15 channels. 50%
122 * of users that are on channels are on 2 or less, 95% are on 7 or less,
123 * and 99% are on 10 or less.
126 m = (cli_user(cptr))->channel;
128 assert(m->user == cptr);
129 if (m->channel == chptr)
138 * find_chasing - Find the client structure for a nick name (user)
139 * using history mechanism if necessary. If the client is not found, an error
140 * message (NO SUCH NICK) is generated. If the client was found
141 * through the history, chasing will be 1 and otherwise 0.
143 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
145 struct Client* who = FindClient(user);
152 if (!(who = get_history(user, feature_int(FEAT_KILLCHASETIMELIMIT)))) {
153 send_reply(sptr, ERR_NOSUCHNICK, user);
162 * Create a string of form "foo!bar@fubar" given foo, bar and fubar
163 * as the parameters. If NULL, they become "*".
165 static char *make_nick_user_host(const char *nick, const char *name,
168 static char namebuf[NICKLEN + USERLEN + HOSTLEN + 3];
169 sprintf_irc(namebuf, "%s!%s@%s", nick, name, host);
174 * Create a string of form "foo!bar@123.456.789.123" given foo, bar and the
175 * IP-number as the parameters. If NULL, they become "*".
177 static char *make_nick_user_ip(char *nick, char *name, struct in_addr ip)
179 static char ipbuf[NICKLEN + USERLEN + 16 + 3];
180 sprintf_irc(ipbuf, "%s!%s@%s", nick, name, ircd_ntoa((const char*) &ip));
185 * Subtract one user from channel i (and free channel
186 * block, if channel became empty).
187 * Returns: true (1) if channel still exists
188 * false (0) if the channel was destroyed
190 int sub1_from_channel(struct Channel* chptr)
195 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
197 assert(0 != chptr->members);
202 assert(0 == chptr->members);
204 /* Channel became (or was) empty: Remove channel */
205 if (is_listed(chptr))
208 for (i = 0; i <= HighestFd; i++)
210 struct Client *acptr = 0;
211 if ((acptr = LocalClientArray[i]) && cli_listing(acptr) &&
212 (cli_listing(acptr))->chptr == chptr)
214 list_next_channels(acptr, 1);
215 break; /* Only one client can list a channel */
220 * Now, find all invite links from channel structure
222 while ((tmp = chptr->invites))
223 del_invite(tmp->value.cptr, chptr);
225 tmp = chptr->banlist;
230 MyFree(obtmp->value.ban.banstr);
231 MyFree(obtmp->value.ban.who);
235 chptr->prev->next = chptr->next;
237 GlobalChannelList = chptr->next;
239 chptr->next->prev = chptr->prev;
241 --UserStats.channels;
243 * make sure that channel actually got removed from hash table
245 assert(chptr->hnext == chptr);
253 * `cptr' must be the client adding the ban.
255 * If `change' is true then add `banid' to channel `chptr'.
256 * Returns 0 if the ban was added.
257 * Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
258 * Return -1 otherwise.
260 * Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
261 * when `change' is false, otherwise they will be removed from the banlist.
262 * Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
263 * respectively will return these bans until NULL is returned.
265 * If `firsttime' is true, the ban list as returned by next_overlapped_ban()
266 * is reset (unless a non-zero value is returned, in which case the
267 * CHFL_BAN_OVERLAPPED flag might not have been reset!).
271 int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
272 int change, int firsttime)
277 int removed_bans = 0;
278 int len = strlen(banid);
283 assert(0 == prev_ban);
284 assert(0 == removed_bans_list);
288 for (banp = &chptr->banlist; *banp;)
290 len += strlen((*banp)->value.ban.banstr);
292 if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
294 if (!strcmp((*banp)->value.ban.banstr, banid))
296 (*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
300 else if (!mmatch((*banp)->value.ban.banstr, banid))
302 if (!mmatch(banid, (*banp)->value.ban.banstr))
304 struct SLink *tmp = *banp;
310 len -= strlen(tmp->value.ban.banstr);
314 /* Silently remove overlapping bans */
315 MyFree(tmp->value.ban.banstr);
316 MyFree(tmp->value.ban.who);
320 /* These will be sent to the user later as -b */
321 tmp->next = removed_bans_list;
322 removed_bans_list = tmp;
326 else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
328 tmp->flags |= CHFL_BAN_OVERLAPPED;
339 (*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
340 banp = &(*banp)->next;
343 if (MyUser(cptr) && !removed_bans &&
344 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
345 (cnt >= feature_int(FEAT_MAXBANS))))
347 send_reply(cptr, ERR_BANLISTFULL, chptr->chname, banid);
353 struct Membership* member;
355 ban->next = chptr->banlist;
357 ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
358 assert(0 != ban->value.ban.banstr);
359 strcpy(ban->value.ban.banstr, banid);
361 ban->value.ban.who = (char*) MyMalloc(strlen(cli_name(cptr)) + 1);
362 assert(0 != ban->value.ban.who);
363 strcpy(ban->value.ban.who, cli_name(cptr));
365 ban->value.ban.when = TStime();
366 ban->flags = CHFL_BAN; /* This bit is never used I think... */
367 if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
368 ban->flags |= CHFL_BAN_IPMASK;
369 chptr->banlist = ban;
372 * Erase ban-valid-bit
374 for (member = chptr->members; member; member = member->next_member)
375 ClearBanValid(member); /* `ban' == channel member ! */
380 static struct SLink *next_overlapped_ban(void)
382 struct SLink *tmp = next_ban;
386 for (ban = tmp->next; ban; ban = ban->next)
387 if ((ban->flags & CHFL_BAN_OVERLAPPED))
394 struct SLink *next_removed_overlapped_ban(void)
396 struct SLink *tmp = removed_bans_list;
399 if (prev_ban->value.ban.banstr) /* Can be set to NULL in set_mode() */
400 MyFree(prev_ban->value.ban.banstr);
401 MyFree(prev_ban->value.ban.who);
406 removed_bans_list = removed_bans_list->next;
414 * If `change' is true, delete `banid' from channel `chptr'.
415 * Returns `false' if removal was (or would have been) successful.
417 static int del_banid(struct Channel *chptr, char *banid, int change)
424 for (ban = &(chptr->banlist); *ban; ban = &((*ban)->next)) {
425 if (0 == ircd_strcmp(banid, (*ban)->value.ban.banstr))
430 struct Membership* member;
432 MyFree(tmp->value.ban.banstr);
433 MyFree(tmp->value.ban.who);
436 * Erase ban-valid-bit, for channel members that are banned
438 for (member = chptr->members; member; member = member->next_member)
439 if (CHFL_BANVALIDMASK == (member->status & CHFL_BANVALIDMASK))
440 ClearBanValid(member); /* `tmp' == channel member */
449 * find_channel_member - returns Membership * if a person is joined and not a zombie
451 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
453 struct Membership* member;
456 member = find_member_link(chptr, cptr);
457 return (member && !IsZombie(member)) ? member : 0;
461 * is_banned - a non-zero value if banned else 0.
463 static int is_banned(struct Client *cptr, struct Channel *chptr,
464 struct Membership* member)
473 if (member && IsBanValid(member))
474 return IsBanned(member);
476 s = make_nick_user_host(cli_name(cptr), (cli_user(cptr))->username,
477 (cli_user(cptr))->host);
479 for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
480 if ((tmp->flags & CHFL_BAN_IPMASK)) {
482 ip_s = make_nick_user_ip(cli_name(cptr), (cli_user(cptr))->username,
484 if (match(tmp->value.ban.banstr, ip_s) == 0)
487 else if (match(tmp->value.ban.banstr, s) == 0)
503 return (tmp != NULL);
507 * adds a user to a channel by adding another link to the channels member
510 void add_user_to_channel(struct Channel* chptr, struct Client* who,
518 struct Membership* member = membershipFreeList;
520 membershipFreeList = member->next_member;
522 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
523 ++membershipAllocCount;
528 member->channel = chptr;
529 member->status = flags;
531 member->next_member = chptr->members;
532 if (member->next_member)
533 member->next_member->prev_member = member;
534 member->prev_member = 0;
535 chptr->members = member;
537 member->next_channel = (cli_user(who))->channel;
538 if (member->next_channel)
539 member->next_channel->prev_channel = member;
540 member->prev_channel = 0;
541 (cli_user(who))->channel = member;
544 ++((cli_user(who))->joined);
548 static int remove_member_from_channel(struct Membership* member)
550 struct Channel* chptr;
552 chptr = member->channel;
554 * unlink channel member list
556 if (member->next_member)
557 member->next_member->prev_member = member->prev_member;
558 if (member->prev_member)
559 member->prev_member->next_member = member->next_member;
561 member->channel->members = member->next_member;
564 * unlink client channel list
566 if (member->next_channel)
567 member->next_channel->prev_channel = member->prev_channel;
568 if (member->prev_channel)
569 member->prev_channel->next_channel = member->next_channel;
571 (cli_user(member->user))->channel = member->next_channel;
573 --(cli_user(member->user))->joined;
575 member->next_member = membershipFreeList;
576 membershipFreeList = member;
578 return sub1_from_channel(chptr);
581 static int channel_all_zombies(struct Channel* chptr)
583 struct Membership* member;
585 for (member = chptr->members; member; member = member->next_member) {
586 if (!IsZombie(member))
593 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
596 struct Membership* member;
599 if ((member = find_member_link(chptr, cptr))) {
600 if (remove_member_from_channel(member)) {
601 if (channel_all_zombies(chptr)) {
603 * XXX - this looks dangerous but isn't if we got the referential
604 * integrity right for channels
606 while (remove_member_from_channel(chptr->members))
613 void remove_user_from_all_channels(struct Client* cptr)
615 struct Membership* chan;
617 assert(0 != cli_user(cptr));
619 while ((chan = (cli_user(cptr))->channel))
620 remove_user_from_channel(cptr, chan->channel);
623 int is_chan_op(struct Client *cptr, struct Channel *chptr)
625 struct Membership* member;
627 if ((member = find_member_link(chptr, cptr)))
628 return (!IsZombie(member) && IsChanOp(member));
633 static int is_deopped(struct Client *cptr, struct Channel *chptr)
635 struct Membership* member;
638 if ((member = find_member_link(chptr, cptr)))
639 return IsDeopped(member);
641 return (IsUser(cptr) ? 1 : 0);
644 int is_zombie(struct Client *cptr, struct Channel *chptr)
646 struct Membership* member;
650 if ((member = find_member_link(chptr, cptr)))
651 return IsZombie(member);
655 int has_voice(struct Client* cptr, struct Channel* chptr)
657 struct Membership* member;
660 if ((member = find_member_link(chptr, cptr)))
661 return (!IsZombie(member) && HasVoice(member));
666 int member_can_send_to_channel(struct Membership* member)
670 if (IsVoicedOrOpped(member))
673 * If it's moderated, and you aren't a priviledged user, you can't
676 if (member->channel->mode.mode & MODE_MODERATED)
679 * If you're banned then you can't speak either.
680 * but because of the amount of CPU time that is_banned chews
681 * we only check it for our clients.
683 if (MyUser(member->user) && is_banned(member->user, member->channel, member))
688 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
690 struct Membership *member;
693 * Servers can always speak on channels.
698 member = find_channel_member(cptr, chptr);
701 * You can't speak if your off channel, if the channel is modeless, or
702 * +n (no external messages) or +m (moderated).
705 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED))
706 || IsModelessChannel(chptr->chname))
711 return member_can_send_to_channel(member);
715 * find_no_nickchange_channel
716 * if a member and not opped or voiced and banned
717 * return the name of the first channel banned on
719 const char* find_no_nickchange_channel(struct Client* cptr)
722 struct Membership* member;
723 for (member = (cli_user(cptr))->channel; member;
724 member = member->next_channel) {
725 if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
726 return member->channel->chname;
734 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
735 * with the parameters in pbuf.
737 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
738 struct Channel *chptr)
745 if (chptr->mode.mode & MODE_SECRET)
747 else if (chptr->mode.mode & MODE_PRIVATE)
749 if (chptr->mode.mode & MODE_MODERATED)
751 if (chptr->mode.mode & MODE_TOPICLIMIT)
753 if (chptr->mode.mode & MODE_INVITEONLY)
755 if (chptr->mode.mode & MODE_NOPRIVMSGS)
757 if (chptr->mode.limit) {
759 sprintf_irc(pbuf, "%d", chptr->mode.limit);
762 if (*chptr->mode.key) {
764 if (chptr->mode.limit)
766 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
767 strcat(pbuf, chptr->mode.key);
775 * send "cptr" a full list of the modes for channel chptr.
777 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
779 static unsigned int current_flags[4] =
780 { 0, CHFL_CHANOP | CHFL_VOICE, CHFL_VOICE, CHFL_CHANOP };
786 struct Membership* member;
788 char modebuf[MODEBUFLEN];
789 char parabuf[MODEBUFLEN];
795 if (IsLocalChannel(chptr->chname))
798 member = chptr->members;
799 lp2 = chptr->banlist;
801 *modebuf = *parabuf = '\0';
802 channel_modes(cptr, modebuf, parabuf, chptr);
804 for (first = 1; full; first = 0) /* Loop for multiple messages */
806 full = 0; /* Assume by default we get it
807 all in one message */
809 /* (Continued) prefix: "<Y> B <channel> <TS>" */
810 /* is there any better way we can do this? */
811 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
812 chptr->creationtime);
814 if (first && modebuf[1]) /* Add simple modes (iklmnpst)
817 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
818 msgq_append(&me, mb, " %s", modebuf);
821 msgq_append(&me, mb, " %s", parabuf);
825 * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
827 * Run 4 times over all members, to group the members with the
830 for (first = 1; flag_cnt < 4;
831 member = chptr->members, new_mode = 1, flag_cnt++)
833 for (; member; member = member->next_member)
835 if ((member->status & CHFL_VOICED_OR_OPPED) !=
836 current_flags[flag_cnt])
837 continue; /* Skip members with different flags */
838 if (msgq_bufleft(mb) < NUMNICKLEN + 4)
839 /* The 4 is a possible ",:ov" */
841 full = 1; /* Make sure we continue after
843 new_mode = 1; /* Ensure the new BURST line contains the current
845 break; /* Do not add this member to this message */
847 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
848 first = 0; /* From now on, us comma's to add new nicks */
851 * Do we have a nick with a new mode ?
852 * Or are we starting a new BURST line?
857 if (IsVoicedOrOpped(member)) {
861 if (IsChanOp(member))
863 if (HasVoice(member))
866 msgq_append(&me, mb, tbuf);
876 /* Attach all bans, space seperated " :%ban ban ..." */
877 for (first = 2; lp2; lp2 = lp2->next)
879 len = strlen(lp2->value.ban.banstr);
880 if (msgq_bufleft(mb) < len + 1 + first)
881 /* The +1 stands for the added ' '.
882 * The +first stands for the added ":%".
888 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
889 lp2->value.ban.banstr);
894 send_buffer(cptr, mb, 0); /* Send this message */
896 } /* Continue when there was something
897 that didn't fit (full==1) */
903 * by Carlo Wood (Run), 05 Oct 1998.
907 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
908 * When the user name or host name are too long (USERLEN and HOSTLEN
909 * respectively) then they are cut off at the start with a '*'.
911 * The following transformations are made:
914 * 2) xxx.xxx -> *!*@host
915 * 3) xxx!yyy -> nick!user@*
916 * 4) xxx@yyy -> *!user@host
917 * 5) xxx!yyy@zzz -> nick!user@host
919 char *pretty_mask(char *mask)
921 static char star[2] = { '*', 0 };
922 char *last_dot = NULL;
925 /* Case 1: default */
930 /* Do a _single_ pass through the characters of the mask: */
931 for (ptr = mask; *ptr; ++ptr)
935 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
939 else if (*ptr == '@')
941 /* Case 4: Found last '@' (without finding a '!' yet) */
946 else if (*ptr == '.')
948 /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
958 /* Case 4 or 5: Found last '@' */
964 if (user == star && last_dot)
974 char *nick_end = (user != star) ? user - 1 : ptr;
975 if (nick_end - nick > NICKLEN)
981 char *user_end = (host != star) ? host - 1 : ptr;
982 if (user_end - user > USERLEN)
984 user = user_end - USERLEN;
989 if (host != star && ptr - host > HOSTLEN)
991 host = ptr - HOSTLEN;
994 return make_nick_user_host(nick, user, host);
997 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1004 for (lp = chptr->banlist; lp; lp = lp->next)
1005 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->value.ban.banstr,
1006 lp->value.ban.who, lp->value.ban.when);
1008 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1011 /* We are now treating the <key> part of /join <channel list> <key> as a key
1012 * ring; that is, we try one key against the actual channel key, and if that
1013 * doesn't work, we try the next one, and so on. -Kev -Texaco
1014 * Returns: 0 on match, 1 otherwise
1015 * This version contributed by SeKs <intru@info.polymtl.ca>
1017 static int compall(char *key, char *keyring)
1022 p1 = key; /* point to the key... */
1023 while (*p1 && *p1 == *keyring)
1024 { /* step through the key and ring until they
1030 if (!*p1 && (!*keyring || *keyring == ','))
1031 /* ok, if we're at the end of the and also at the end of one of the keys
1032 in the keyring, we have a match */
1035 if (!*keyring) /* if we're at the end of the key ring, there
1036 weren't any matches, so we return 1 */
1039 /* Not at the end of the key ring, so step
1040 through to the next key in the ring: */
1041 while (*keyring && *(keyring++) != ',');
1043 goto top; /* and check it against the key */
1046 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
1049 int overrideJoin = 0;
1052 * Now a banned user CAN join if invited -- Nemesi
1053 * Now a user CAN escape channel limit if invited -- bfriendly
1054 * Now a user CAN escape anything if invited -- Isomer
1057 for (lp = (cli_user(sptr))->invited; lp; lp = lp->next)
1058 if (lp->value.chptr == chptr)
1061 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
1062 a HACK(4) notice will be sent if he would not have been supposed
1063 to join normally. */
1064 if (IsLocalChannel(chptr->chname) && HasPriv(sptr, PRIV_WALK_LCHAN) &&
1065 !BadPtr(key) && compall("OVERRIDE",key) == 0)
1066 overrideJoin = MAGIC_OPER_OVERRIDE;
1068 if (chptr->mode.mode & MODE_INVITEONLY)
1069 return overrideJoin + ERR_INVITEONLYCHAN;
1071 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
1072 return overrideJoin + ERR_CHANNELISFULL;
1074 if (is_banned(sptr, chptr, NULL))
1075 return overrideJoin + ERR_BANNEDFROMCHAN;
1078 * now using compall (above) to test against a whole key ring -Kev
1080 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
1081 return overrideJoin + ERR_BADCHANNELKEY;
1084 return ERR_DONTCHEAT;
1090 * Remove bells and commas from channel name
1092 void clean_channelname(char *cn)
1096 for (i = 0; cn[i]; i++) {
1097 if (i >= CHANNELLEN || !IsChannelChar(cn[i])) {
1101 if (IsChannelLower(cn[i])) {
1102 cn[i] = ToLower(cn[i]);
1108 if ((unsigned char)(cn[i]) == 0xd0)
1109 cn[i] = (char) 0xf0;
1116 * Get Channel block for i (and allocate a new channel
1117 * block, if it didn't exists before).
1119 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1121 struct Channel *chptr;
1124 if (EmptyString(chname))
1127 len = strlen(chname);
1128 if (MyUser(cptr) && len > CHANNELLEN)
1131 *(chname + CHANNELLEN) = '\0';
1133 if ((chptr = FindChannel(chname)))
1135 if (flag == CGT_CREATE)
1137 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1139 ++UserStats.channels;
1140 memset(chptr, 0, sizeof(struct Channel));
1141 strcpy(chptr->chname, chname);
1142 if (GlobalChannelList)
1143 GlobalChannelList->prev = chptr;
1145 chptr->next = GlobalChannelList;
1146 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1147 GlobalChannelList = chptr;
1153 void add_invite(struct Client *cptr, struct Channel *chptr)
1155 struct SLink *inv, **tmp;
1157 del_invite(cptr, chptr);
1159 * Delete last link in chain if the list is max length
1161 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1162 if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER))
1163 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1165 * Add client to channel invite list
1168 inv->value.cptr = cptr;
1169 inv->next = chptr->invites;
1170 chptr->invites = inv;
1172 * Add channel to the end of the client invite list
1174 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1176 inv->value.chptr = chptr;
1179 (cli_user(cptr))->invites++;
1183 * Delete Invite block from channel invite list and client invite list
1185 void del_invite(struct Client *cptr, struct Channel *chptr)
1187 struct SLink **inv, *tmp;
1189 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1190 if (tmp->value.cptr == cptr)
1195 (cli_user(cptr))->invites--;
1199 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1200 if (tmp->value.chptr == chptr)
1209 /* List and skip all channels that are listen */
1210 void list_next_channels(struct Client *cptr, int nr)
1212 struct ListingArgs *args = cli_listing(cptr);
1213 struct Channel *chptr = args->chptr;
1214 chptr->mode.mode &= ~MODE_LISTED;
1215 while (is_listed(chptr) || --nr >= 0)
1217 for (; chptr; chptr = chptr->next)
1219 if (!cli_user(cptr) || (SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
1221 if (chptr->users > args->min_users && chptr->users < args->max_users &&
1222 chptr->creationtime > args->min_time &&
1223 chptr->creationtime < args->max_time &&
1224 (!args->topic_limits || (*chptr->topic &&
1225 chptr->topic_time > args->min_topic_time &&
1226 chptr->topic_time < args->max_topic_time)))
1228 if (ShowChannel(cptr,chptr))
1229 send_reply(cptr, RPL_LIST, chptr->chname, chptr->users,
1231 chptr = chptr->next;
1237 MyFree(cli_listing(cptr));
1238 cli_listing(cptr) = NULL;
1239 send_reply(cptr, RPL_LISTEND);
1245 (cli_listing(cptr))->chptr = chptr;
1246 chptr->mode.mode |= MODE_LISTED;
1257 * X --a--> A --b--> B --d--> D
1261 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1262 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1264 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1265 * Remove the user immedeately when no users are left on the channel.
1266 * b) On server B : remove the user (who/lp) from the channel, send a
1267 * PART upstream (to A) and pass on the KICK.
1268 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1269 * channel, and pass on the KICK.
1270 * d) On server D : remove the user (who/lp) from the channel, and pass on
1274 * - Setting the ZOMBIE flag never hurts, we either remove the
1275 * client after that or we don't.
1276 * - The KICK message was already passed on, as should be in all cases.
1277 * - `who' is removed in all cases except case a) when users are left.
1278 * - A PART is only sent upstream in case b).
1284 * 1 --- 2 --- 3 --- 4 --- 5
1288 * We also need to turn 'who' into a zombie on servers 1 and 6,
1289 * because a KICK from 'who' (kicking someone else in that direction)
1290 * can arrive there afterwards - which should not be bounced itself.
1291 * Therefore case a) also applies for servers 1 and 6.
1295 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
1296 struct Client* sptr, struct Channel* chptr)
1298 assert(0 != member);
1303 /* Default for case a): */
1306 /* Case b) or c) ?: */
1307 if (MyUser(who)) /* server 4 */
1309 if (IsServer(cptr)) /* Case b) ? */
1310 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1311 remove_user_from_channel(who, chptr);
1314 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1316 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1317 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1318 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1320 remove_user_from_channel(who, chptr);
1325 /* Case a) (servers 1, 2, 3 and 6) */
1326 if (channel_all_zombies(chptr))
1327 remove_user_from_channel(who, chptr);
1329 /* XXX Can't actually call Debug here; if the channel is all zombies,
1330 * chptr will no longer exist when we get here.
1331 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1335 int number_of_zombies(struct Channel *chptr)
1337 struct Membership* member;
1341 for (member = chptr->members; member; member = member->next_member) {
1342 if (IsZombie(member))
1349 * This helper function builds an argument string in strptr, consisting
1350 * of the original string, a space, and str1 and str2 concatenated (if,
1351 * of course, str2 is not NULL)
1354 build_string(char *strptr, int *strptr_i, char *str1, char *str2, char c)
1357 strptr[(*strptr_i)++] = c;
1360 strptr[(*strptr_i)++] = *(str1++);
1364 strptr[(*strptr_i)++] = *(str2++);
1366 strptr[(*strptr_i)] = '\0';
1370 * This is the workhorse of our ModeBuf suite; this actually generates the
1371 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1374 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1376 /* we only need the flags that don't take args right now */
1377 static int flags[] = {
1378 /* MODE_CHANOP, 'o', */
1379 /* MODE_VOICE, 'v', */
1382 MODE_MODERATED, 'm',
1383 MODE_TOPICLIMIT, 't',
1384 MODE_INVITEONLY, 'i',
1385 MODE_NOPRIVMSGS, 'n',
1386 /* MODE_KEY, 'k', */
1387 /* MODE_BAN, 'b', */
1388 /* MODE_LIMIT, 'l', */
1394 struct Client *app_source; /* where the MODE appears to come from */
1396 char addbuf[20]; /* accumulates +psmtin, etc. */
1398 char rembuf[20]; /* accumulates -psmtin, etc. */
1400 char *bufptr; /* we make use of indirection to simplify the code */
1403 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1405 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1407 char *strptr; /* more indirection to simplify the code */
1410 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1413 char limitbuf[20]; /* convert limits to strings */
1415 unsigned int limitdel = MODE_LIMIT;
1419 /* If the ModeBuf is empty, we have nothing to do */
1420 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1423 /* Ok, if we were given the OPMODE flag, hide the source if its a user */
1424 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
1425 app_source = (cli_user(mbuf->mb_source))->server;
1427 app_source = mbuf->mb_source;
1430 * Account for user we're bouncing; we have to get it in on the first
1431 * bounced MODE, or we could have problems
1433 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1434 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1436 /* Calculate the simple flags */
1437 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1438 if (*flag_p & mbuf->mb_add)
1439 addbuf[addbuf_i++] = flag_p[1];
1440 else if (*flag_p & mbuf->mb_rem)
1441 rembuf[rembuf_i++] = flag_p[1];
1444 /* Now go through the modes with arguments... */
1445 for (i = 0; i < mbuf->mb_count; i++) {
1446 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1448 bufptr_i = &addbuf_i;
1451 bufptr_i = &rembuf_i;
1454 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1455 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1457 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
1458 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1460 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1461 totalbuflen -= IRCD_MAX(5, tmp) + 1;
1463 } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN)) {
1464 tmp = strlen(MB_STRING(mbuf, i));
1466 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1467 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1469 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_KEY ? 'k' : 'b';
1470 totalbuflen -= tmp + 1;
1472 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1473 /* if it's a limit, we also format the number */
1474 sprintf_irc(limitbuf, "%d", MB_UINT(mbuf, i));
1476 tmp = strlen(limitbuf);
1478 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1479 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1481 bufptr[(*bufptr_i)++] = 'l';
1482 totalbuflen -= tmp + 1;
1487 /* terminate the mode strings */
1488 addbuf[addbuf_i] = '\0';
1489 rembuf[rembuf_i] = '\0';
1491 /* If we're building a user visible MODE or HACK... */
1492 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1493 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1494 MODEBUF_DEST_LOG)) {
1495 /* Set up the parameter strings */
1501 for (i = 0; i < mbuf->mb_count; i++) {
1502 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1505 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1507 strptr_i = &addstr_i;
1510 strptr_i = &remstr_i;
1513 /* deal with clients... */
1514 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1515 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1517 /* deal with strings... */
1518 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1519 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1522 * deal with limit; note we cannot include the limit parameter if we're
1525 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1526 (MODE_ADD | MODE_LIMIT))
1527 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1530 /* send the messages off to their destination */
1531 if (mbuf->mb_dest & MODEBUF_DEST_HACK2) {
1532 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1533 "[%Tu]", cli_name(app_source),
1534 mbuf->mb_channel->chname,
1535 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1536 addbuf, remstr, addstr,
1537 mbuf->mb_channel->creationtime);
1538 sendcmdto_serv_butone(&me, CMD_DESYNCH, mbuf->mb_connect,
1539 ":HACK: %s MODE %s %s%s%s%s%s%s [%Tu]",
1540 cli_name(app_source), mbuf->mb_channel->chname,
1541 rembuf_i ? "-" : "", rembuf,
1542 addbuf_i ? "+" : "", addbuf, remstr, addstr,
1543 mbuf->mb_channel->creationtime);
1546 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1547 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1548 "%s%s%s%s%s%s [%Tu]", cli_name(app_source),
1549 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1550 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1551 mbuf->mb_channel->creationtime);
1553 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1554 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1555 "[%Tu]", cli_name(app_source),
1556 mbuf->mb_channel->chname,
1557 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1558 addbuf, remstr, addstr,
1559 mbuf->mb_channel->creationtime);
1561 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1562 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1563 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1564 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1565 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1567 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1568 sendcmdto_channel_butserv(app_source, CMD_MODE, mbuf->mb_channel,
1569 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1570 rembuf_i ? "-" : "", rembuf,
1571 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1574 /* Now are we supposed to propagate to other servers? */
1575 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1576 /* set up parameter string */
1583 * limit is supressed if we're removing it; we have to figure out which
1584 * direction is the direction for it to be removed, though...
1586 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1588 for (i = 0; i < mbuf->mb_count; i++) {
1589 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1592 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1594 strptr_i = &addstr_i;
1597 strptr_i = &remstr_i;
1600 /* deal with modes that take clients */
1601 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1602 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1604 /* deal with modes that take strings */
1605 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1606 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1609 * deal with the limit. Logic here is complicated; if HACK2 is set,
1610 * we're bouncing the mode, so sense is reversed, and we have to
1611 * include the original limit if it looks like it's being removed
1613 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1614 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1617 /* we were told to deop the source */
1618 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1619 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1620 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1621 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1623 /* mark that we've done this, so we don't do it again */
1624 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1627 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1628 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1629 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1630 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1631 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1632 addbuf, remstr, addstr);
1633 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1635 * If HACK2 was set, we're bouncing; we send the MODE back to the
1636 * connection we got it from with the senses reversed and a TS of 0;
1639 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1640 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1641 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1642 mbuf->mb_channel->creationtime);
1645 * We're propagating a normal MODE command to the rest of the network;
1646 * we send the actual channel TS unless this is a HACK3 or a HACK4
1648 if (IsServer(mbuf->mb_source))
1649 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1650 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1651 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1652 addbuf, remstr, addstr,
1653 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1654 mbuf->mb_channel->creationtime);
1656 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1657 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1658 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1659 addbuf, remstr, addstr);
1663 /* We've drained the ModeBuf... */
1668 /* reinitialize the mode-with-arg slots */
1669 for (i = 0; i < MAXMODEPARAMS; i++) {
1670 /* If we saved any, pack them down */
1671 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1672 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1673 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1675 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1677 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1678 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1680 MB_TYPE(mbuf, i) = 0;
1681 MB_UINT(mbuf, i) = 0;
1684 /* If we're supposed to flush it all, do so--all hail tail recursion */
1685 if (all && mbuf->mb_count)
1686 return modebuf_flush_int(mbuf, 1);
1692 * This routine just initializes a ModeBuf structure with the information
1693 * needed and the options given.
1696 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1697 struct Client *connect, struct Channel *chan, unsigned int dest)
1702 assert(0 != source);
1708 mbuf->mb_source = source;
1709 mbuf->mb_connect = connect;
1710 mbuf->mb_channel = chan;
1711 mbuf->mb_dest = dest;
1714 /* clear each mode-with-parameter slot */
1715 for (i = 0; i < MAXMODEPARAMS; i++) {
1716 MB_TYPE(mbuf, i) = 0;
1717 MB_UINT(mbuf, i) = 0;
1722 * This routine simply adds modes to be added or deleted; do a binary OR
1723 * with either MODE_ADD or MODE_DEL
1726 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1729 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1731 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1732 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
1734 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1737 if (mode & MODE_ADD) {
1738 mbuf->mb_rem &= ~mode;
1739 mbuf->mb_add |= mode;
1741 mbuf->mb_add &= ~mode;
1742 mbuf->mb_rem |= mode;
1747 * This routine adds a mode to be added or deleted that takes a unsigned
1748 * int parameter; mode may *only* be the relevant mode flag ORed with one
1749 * of MODE_ADD or MODE_DEL
1752 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1755 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1757 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1758 MB_UINT(mbuf, mbuf->mb_count) = uint;
1760 /* when we've reached the maximal count, flush the buffer */
1761 if (++mbuf->mb_count >=
1762 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1763 modebuf_flush_int(mbuf, 0);
1767 * This routine adds a mode to be added or deleted that takes a string
1768 * parameter; mode may *only* be the relevant mode flag ORed with one of
1769 * MODE_ADD or MODE_DEL
1772 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
1776 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1778 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
1779 MB_STRING(mbuf, mbuf->mb_count) = string;
1781 /* when we've reached the maximal count, flush the buffer */
1782 if (++mbuf->mb_count >=
1783 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1784 modebuf_flush_int(mbuf, 0);
1788 * This routine adds a mode to be added or deleted that takes a client
1789 * parameter; mode may *only* be the relevant mode flag ORed with one of
1790 * MODE_ADD or MODE_DEL
1793 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
1794 struct Client *client)
1797 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1799 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1800 MB_CLIENT(mbuf, mbuf->mb_count) = client;
1802 /* when we've reached the maximal count, flush the buffer */
1803 if (++mbuf->mb_count >=
1804 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1805 modebuf_flush_int(mbuf, 0);
1809 * This is the exported binding for modebuf_flush()
1812 modebuf_flush(struct ModeBuf *mbuf)
1814 return modebuf_flush_int(mbuf, 1);
1818 * This extracts the simple modes contained in mbuf
1821 modebuf_extract(struct ModeBuf *mbuf, char *buf)
1823 static int flags[] = {
1824 /* MODE_CHANOP, 'o', */
1825 /* MODE_VOICE, 'v', */
1828 MODE_MODERATED, 'm',
1829 MODE_TOPICLIMIT, 't',
1830 MODE_INVITEONLY, 'i',
1831 MODE_NOPRIVMSGS, 'n',
1833 /* MODE_BAN, 'b', */
1838 int i, bufpos = 0, len;
1840 char *key = 0, limitbuf[20];
1849 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
1850 if (MB_TYPE(mbuf, i) & MODE_ADD) {
1851 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT);
1853 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
1854 key = MB_STRING(mbuf, i);
1855 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
1856 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%d", MB_UINT(mbuf, i));
1863 buf[bufpos++] = '+'; /* start building buffer */
1865 for (flag_p = flags; flag_p[0]; flag_p += 2)
1867 buf[bufpos++] = flag_p[1];
1869 for (i = 0, len = bufpos; i < len; i++) {
1871 build_string(buf, &bufpos, key, 0, ' ');
1872 else if (buf[i] == 'l')
1873 build_string(buf, &bufpos, limitbuf, 0, ' ');
1882 * Simple function to invalidate bans
1885 mode_ban_invalidate(struct Channel *chan)
1887 struct Membership *member;
1889 for (member = chan->members; member; member = member->next_member)
1890 ClearBanValid(member);
1894 * Simple function to drop invite structures
1897 mode_invite_clear(struct Channel *chan)
1899 while (chan->invites)
1900 del_invite(chan->invites->value.cptr, chan);
1903 /* What we've done for mode_parse so far... */
1904 #define DONE_LIMIT 0x01 /* We've set the limit */
1905 #define DONE_KEY 0x02 /* We've set the key */
1906 #define DONE_BANLIST 0x04 /* We've sent the ban list */
1907 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
1908 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
1911 struct ModeBuf *mbuf;
1912 struct Client *cptr;
1913 struct Client *sptr;
1914 struct Channel *chptr;
1925 struct SLink banlist[MAXPARA];
1928 struct Client *client;
1929 } cli_change[MAXPARA];
1933 * Here's a helper function to deal with sending along "Not oper" or
1934 * "Not member" messages
1937 send_notoper(struct ParseState *state)
1939 if (state->done & DONE_NOTOPER)
1942 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
1943 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
1945 state->done |= DONE_NOTOPER;
1949 * Helper function to convert limits
1952 mode_parse_limit(struct ParseState *state, int *flag_p)
1954 unsigned int t_limit;
1956 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
1957 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
1960 if (state->parc <= 0) { /* warn if not enough args */
1961 if (MyUser(state->sptr))
1962 need_more_params(state->sptr, "MODE +l");
1966 t_limit = atoi(state->parv[state->args_used++]); /* grab arg */
1970 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
1971 (!t_limit || t_limit == state->chptr->mode.limit))
1974 t_limit = state->chptr->mode.limit;
1976 /* If they're not an oper, they can't change modes */
1977 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
1978 send_notoper(state);
1982 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
1984 state->done |= DONE_LIMIT;
1989 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
1991 if (state->flags & MODE_PARSE_SET) { /* set the limit */
1992 if (state->dir & MODE_ADD) {
1993 state->chptr->mode.mode |= flag_p[0];
1994 state->chptr->mode.limit = t_limit;
1996 state->chptr->mode.mode &= ~flag_p[0];
1997 state->chptr->mode.limit = 0;
2003 * Helper function to convert keys
2006 mode_parse_key(struct ParseState *state, int *flag_p)
2011 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2014 if (state->parc <= 0) { /* warn if not enough args */
2015 if (MyUser(state->sptr))
2016 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2021 t_str = state->parv[state->args_used++]; /* grab arg */
2025 /* If they're not an oper, they can't change modes */
2026 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2027 send_notoper(state);
2031 if (state->done & DONE_KEY) /* allow key to be set only once */
2033 state->done |= DONE_KEY;
2037 /* clean up the key string */
2039 while (*++s > ' ' && *s != ':' && --t_len)
2043 if (!*t_str) { /* warn if empty */
2044 if (MyUser(state->sptr))
2045 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2053 /* can't add a key if one is set, nor can one remove the wrong key */
2054 if (!(state->flags & MODE_PARSE_FORCE))
2055 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2056 (state->dir == MODE_DEL &&
2057 ircd_strcmp(state->chptr->mode.key, t_str))) {
2058 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2062 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2063 !ircd_strcmp(state->chptr->mode.key, t_str))
2064 return; /* no key change */
2066 if (state->flags & MODE_PARSE_BOUNCE) {
2067 if (*state->chptr->mode.key) /* reset old key */
2068 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2069 state->chptr->mode.key, 0);
2070 else /* remove new bogus key */
2071 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2072 } else /* send new key */
2073 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2075 if (state->flags & MODE_PARSE_SET) {
2076 if (state->dir == MODE_ADD) /* set the new key */
2077 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2078 else /* remove the old key */
2079 *state->chptr->mode.key = '\0';
2084 * Helper function to convert bans
2087 mode_parse_ban(struct ParseState *state, int *flag_p)
2090 struct SLink *ban, *newban = 0;
2092 if (state->parc <= 0) { /* Not enough args, send ban list */
2093 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2094 send_ban_list(state->sptr, state->chptr);
2095 state->done |= DONE_BANLIST;
2101 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2104 t_str = state->parv[state->args_used++]; /* grab arg */
2108 /* If they're not an oper, they can't change modes */
2109 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2110 send_notoper(state);
2114 if ((s = strchr(t_str, ' ')))
2117 if (!*t_str || *t_str == ':') { /* warn if empty */
2118 if (MyUser(state->sptr))
2119 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2124 t_str = collapse(pretty_mask(t_str));
2126 /* remember the ban for the moment... */
2127 if (state->dir == MODE_ADD) {
2128 newban = state->banlist + (state->numbans++);
2131 DupString(newban->value.ban.banstr, t_str);
2132 newban->value.ban.who = cli_name(state->sptr);
2133 newban->value.ban.when = TStime();
2135 newban->flags = CHFL_BAN | MODE_ADD;
2137 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
2138 newban->flags |= CHFL_BAN_IPMASK;
2141 if (!state->chptr->banlist) {
2142 state->chptr->banlist = newban; /* add our ban with its flags */
2143 state->done |= DONE_BANCLEAN;
2147 /* Go through all bans */
2148 for (ban = state->chptr->banlist; ban; ban = ban->next) {
2149 /* first, clean the ban flags up a bit */
2150 if (!(state->done & DONE_BANCLEAN))
2151 /* Note: We're overloading *lots* of bits here; be careful! */
2152 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
2156 * MODE_ADD - Ban was added; if we're bouncing modes,
2157 * then we'll remove it below; otherwise,
2158 * we'll have to allocate a real ban
2160 * MODE_DEL - Ban was marked for deletion; if we're
2161 * bouncing modes, we'll have to re-add it,
2162 * otherwise, we'll have to remove it
2164 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
2165 * with a ban already set; if we're
2166 * bouncing modes, we'll have to bounce
2167 * this one; otherwise, we'll just ignore
2168 * it when we process added bans
2171 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
2172 ban->flags |= MODE_DEL; /* delete one ban */
2174 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2176 } else if (state->dir == MODE_ADD) {
2177 /* if the ban already exists, don't worry about it */
2178 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
2179 newban->flags &= ~MODE_ADD; /* don't add ban at all */
2180 MyFree(newban->value.ban.banstr); /* stopper a leak */
2181 state->numbans--; /* deallocate last ban */
2182 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2184 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
2185 if (!(ban->flags & MODE_DEL))
2186 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
2187 } else if (!mmatch(t_str, ban->value.ban.banstr))
2188 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
2190 if (!ban->next && (newban->flags & MODE_ADD)) {
2191 ban->next = newban; /* add our ban with its flags */
2192 break; /* get out of loop */
2196 state->done |= DONE_BANCLEAN;
2200 * This is the bottom half of the ban processor
2203 mode_process_bans(struct ParseState *state)
2205 struct SLink *ban, *newban, *prevban, *nextban;
2211 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2213 banlen = strlen(ban->value.ban.banstr);
2215 nextban = ban->next;
2217 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
2219 prevban->next = 0; /* Break the list; ban isn't a real ban */
2221 state->chptr->banlist = 0;
2226 MyFree(ban->value.ban.banstr);
2229 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
2230 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2231 ban->value.ban.banstr,
2232 state->flags & MODE_PARSE_SET);
2234 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2235 if (prevban) /* clip it out of the list... */
2236 prevban->next = ban->next;
2238 state->chptr->banlist = ban->next;
2243 MyFree(ban->value.ban.who);
2247 continue; /* next ban; keep prevban like it is */
2249 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
2250 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
2252 prevban->next = 0; /* Break the list; ban isn't a real ban */
2254 state->chptr->banlist = 0;
2256 /* If we're supposed to ignore it, do so. */
2257 if (ban->flags & CHFL_BAN_OVERLAPPED &&
2258 !(state->flags & MODE_PARSE_BOUNCE)) {
2262 MyFree(ban->value.ban.banstr);
2264 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2265 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2266 count >= feature_int(FEAT_MAXBANS))) {
2267 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2268 ban->value.ban.banstr);
2272 MyFree(ban->value.ban.banstr);
2274 /* add the ban to the buffer */
2275 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2276 ban->value.ban.banstr,
2277 !(state->flags & MODE_PARSE_SET));
2279 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2280 newban = make_link();
2281 newban->value.ban.banstr = ban->value.ban.banstr;
2282 DupString(newban->value.ban.who, ban->value.ban.who);
2283 newban->value.ban.when = ban->value.ban.when;
2284 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
2286 newban->next = state->chptr->banlist; /* and link it in */
2287 state->chptr->banlist = newban;
2296 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2298 if (changed) /* if we changed the ban list, we must invalidate the bans */
2299 mode_ban_invalidate(state->chptr);
2303 * Helper function to process client changes
2306 mode_parse_client(struct ParseState *state, int *flag_p)
2309 struct Client *acptr;
2312 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2315 if (state->parc <= 0) /* return if not enough args */
2318 t_str = state->parv[state->args_used++]; /* grab arg */
2322 /* If they're not an oper, they can't change modes */
2323 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2324 send_notoper(state);
2328 if (MyUser(state->sptr)) /* find client we're manipulating */
2329 acptr = find_chasing(state->sptr, t_str, NULL);
2331 acptr = findNUser(t_str);
2334 return; /* find_chasing() already reported an error to the user */
2336 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2337 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2338 state->cli_change[i].flag & flag_p[0]))
2339 break; /* found a slot */
2341 /* Store what we're doing to them */
2342 state->cli_change[i].flag = state->dir | flag_p[0];
2343 state->cli_change[i].client = acptr;
2347 * Helper function to process the changed client list
2350 mode_process_clients(struct ParseState *state)
2353 struct Membership *member;
2355 for (i = 0; state->cli_change[i].flag; i++) {
2356 assert(0 != state->cli_change[i].client);
2358 /* look up member link */
2359 if (!(member = find_member_link(state->chptr,
2360 state->cli_change[i].client)) ||
2361 (MyUser(state->sptr) && IsZombie(member))) {
2362 if (MyUser(state->sptr))
2363 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2364 cli_name(state->cli_change[i].client),
2365 state->chptr->chname);
2369 if ((state->cli_change[i].flag & MODE_ADD &&
2370 (state->cli_change[i].flag & member->status)) ||
2371 (state->cli_change[i].flag & MODE_DEL &&
2372 !(state->cli_change[i].flag & member->status)))
2373 continue; /* no change made, don't do anything */
2375 /* see if the deop is allowed */
2376 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2377 (MODE_DEL | MODE_CHANOP)) {
2378 /* prevent +k users from being deopped */
2379 if (IsChannelService(state->cli_change[i].client)) {
2380 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
2381 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
2383 (IsServer(state->sptr) ? cli_name(state->sptr) :
2384 cli_name((cli_user(state->sptr))->server)));
2386 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
2387 send_reply(state->sptr, ERR_ISCHANSERVICE,
2388 cli_name(state->cli_change[i].client),
2389 state->chptr->chname);
2394 /* don't allow local opers to be deopped on local channels */
2395 if (MyUser(state->sptr) && state->cli_change[i].client != state->sptr &&
2396 IsLocalChannel(state->chptr->chname) &&
2397 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
2398 send_reply(state->sptr, ERR_ISOPERLCHAN,
2399 cli_name(state->cli_change[i].client),
2400 state->chptr->chname);
2405 /* accumulate the change */
2406 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
2407 state->cli_change[i].client);
2409 /* actually effect the change */
2410 if (state->flags & MODE_PARSE_SET) {
2411 if (state->cli_change[i].flag & MODE_ADD) {
2412 member->status |= (state->cli_change[i].flag &
2413 (MODE_CHANOP | MODE_VOICE));
2414 if (state->cli_change[i].flag & MODE_CHANOP)
2415 ClearDeopped(member);
2417 member->status &= ~(state->cli_change[i].flag &
2418 (MODE_CHANOP | MODE_VOICE));
2420 } /* for (i = 0; state->cli_change[i].flags; i++) { */
2424 * Helper function to process the simple modes
2427 mode_parse_mode(struct ParseState *state, int *flag_p)
2429 /* If they're not an oper, they can't change modes */
2430 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2431 send_notoper(state);
2438 if (state->dir == MODE_ADD) {
2439 state->add |= flag_p[0];
2440 state->del &= ~flag_p[0];
2442 if (flag_p[0] & MODE_SECRET) {
2443 state->add &= ~MODE_PRIVATE;
2444 state->del |= MODE_PRIVATE;
2445 } else if (flag_p[0] & MODE_PRIVATE) {
2446 state->add &= ~MODE_SECRET;
2447 state->del |= MODE_SECRET;
2450 state->add &= ~flag_p[0];
2451 state->del |= flag_p[0];
2454 assert(0 == (state->add & state->del));
2455 assert((MODE_SECRET | MODE_PRIVATE) !=
2456 (state->add & (MODE_SECRET | MODE_PRIVATE)));
2460 * This routine is intended to parse MODE or OPMODE commands and effect the
2461 * changes (or just build the bounce buffer). We pass the starting offset
2465 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
2466 struct Channel *chptr, int parc, char *parv[], unsigned int flags)
2468 static int chan_flags[] = {
2473 MODE_MODERATED, 'm',
2474 MODE_TOPICLIMIT, 't',
2475 MODE_INVITEONLY, 'i',
2476 MODE_NOPRIVMSGS, 'n',
2486 unsigned int t_mode;
2488 struct ParseState state;
2499 state.chptr = chptr;
2502 state.flags = flags;
2503 state.dir = MODE_ADD;
2507 state.args_used = 0;
2508 state.max_args = MAXMODEPARAMS;
2511 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
2512 state.banlist[i].next = 0;
2513 state.banlist[i].value.ban.banstr = 0;
2514 state.banlist[i].value.ban.who = 0;
2515 state.banlist[i].value.ban.when = 0;
2516 state.banlist[i].flags = 0;
2517 state.cli_change[i].flag = 0;
2518 state.cli_change[i].client = 0;
2521 modestr = state.parv[state.args_used++];
2525 for (; *modestr; modestr++) {
2526 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
2527 if (flag_p[1] == *modestr)
2530 if (!flag_p[0]) { /* didn't find it? complain and continue */
2531 if (MyUser(state.sptr))
2532 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
2537 case '+': /* switch direction to MODE_ADD */
2538 case '-': /* switch direction to MODE_DEL */
2539 state.dir = flag_p[0];
2542 case 'l': /* deal with limits */
2543 mode_parse_limit(&state, flag_p);
2546 case 'k': /* deal with keys */
2547 mode_parse_key(&state, flag_p);
2550 case 'b': /* deal with bans */
2551 mode_parse_ban(&state, flag_p);
2554 case 'o': /* deal with ops/voice */
2556 mode_parse_client(&state, flag_p);
2559 default: /* deal with other modes */
2560 mode_parse_mode(&state, flag_p);
2562 } /* switch (*modestr) { */
2563 } /* for (; *modestr; modestr++) { */
2565 if (state.flags & MODE_PARSE_BURST)
2566 break; /* don't interpret any more arguments */
2568 if (state.parc > 0) { /* process next argument in string */
2569 modestr = state.parv[state.args_used++];
2573 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
2576 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
2577 break; /* we're then going to bounce the mode! */
2579 recv_ts = atoi(modestr);
2581 if (recv_ts && recv_ts < state.chptr->creationtime)
2582 state.chptr->creationtime = recv_ts; /* respect earlier TS */
2584 break; /* break out of while loop */
2585 } else if (state.flags & MODE_PARSE_STRICT ||
2586 (MyUser(state.sptr) && state.max_args <= 0)) {
2587 state.parc++; /* we didn't actually gobble the argument */
2589 break; /* break out of while loop */
2592 } /* while (*modestr) { */
2595 * the rest of the function finishes building resultant MODEs; if the
2596 * origin isn't a member or an oper, skip it.
2598 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
2599 return state.args_used; /* tell our parent how many args we gobbled */
2601 t_mode = state.chptr->mode.mode;
2603 if (state.del & t_mode) { /* delete any modes to be deleted... */
2604 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
2606 t_mode &= ~state.del;
2608 if (state.add & ~t_mode) { /* add any modes to be added... */
2609 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
2611 t_mode |= state.add;
2614 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
2615 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
2616 !(t_mode & MODE_INVITEONLY))
2617 mode_invite_clear(state.chptr);
2619 state.chptr->mode.mode = t_mode;
2622 if (state.flags & MODE_PARSE_WIPEOUT) {
2623 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
2624 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
2625 state.chptr->mode.limit);
2626 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
2627 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
2628 state.chptr->mode.key, 0);
2631 if (state.done & DONE_BANCLEAN) /* process bans */
2632 mode_process_bans(&state);
2634 /* process client changes */
2635 if (state.cli_change[0].flag)
2636 mode_process_clients(&state);
2638 return state.args_used; /* tell our parent how many args we gobbled */
2642 * Initialize a join buffer
2645 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
2646 struct Client *connect, unsigned int type, char *comment,
2652 assert(0 != source);
2653 assert(0 != connect);
2655 jbuf->jb_source = source; /* just initialize struct JoinBuf */
2656 jbuf->jb_connect = connect;
2657 jbuf->jb_type = type;
2658 jbuf->jb_comment = comment;
2659 jbuf->jb_create = create;
2661 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
2662 type == JOINBUF_TYPE_PART ||
2663 type == JOINBUF_TYPE_PARTALL) ?
2664 STARTJOINLEN : STARTCREATELEN) +
2665 (comment ? strlen(comment) + 2 : 0));
2667 for (i = 0; i < MAXJOINARGS; i++)
2668 jbuf->jb_channels[i] = 0;
2672 * Add a channel to the join buffer
2675 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
2682 if (jbuf->jb_type == JOINBUF_TYPE_JOIN)
2683 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
2688 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
2689 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
2690 /* Send notification to channel */
2691 if (!(flags & CHFL_ZOMBIE))
2692 sendcmdto_channel_butserv(jbuf->jb_source, CMD_PART, chan,
2693 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2694 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2695 else if (MyUser(jbuf->jb_source))
2696 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
2697 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2698 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2699 /* XXX: Shouldn't we send a PART here anyway? */
2700 /* to users on the channel? Why? From their POV, the user isn't on
2701 * the channel anymore anyway. We don't send to servers until below,
2702 * when we gang all the channel parts together. Note that this is
2703 * exactly the same logic, albeit somewhat more concise, as was in
2704 * the original m_part.c */
2706 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2707 IsLocalChannel(chan->chname)) /* got to remove user here */
2708 remove_user_from_channel(jbuf->jb_source, chan);
2710 /* Add user to channel */
2711 add_user_to_channel(chan, jbuf->jb_source, flags);
2713 /* send notification to all servers */
2714 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !IsLocalChannel(chan->chname))
2715 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
2716 "%H %Tu", chan, chan->creationtime);
2718 /* Send the notification to the channel */
2719 sendcmdto_channel_butserv(jbuf->jb_source, CMD_JOIN, chan, ":%H", chan);
2721 /* send an op, too, if needed */
2722 if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE &&
2723 !IsModelessChannel(chan->chname))
2724 sendcmdto_channel_butserv(jbuf->jb_source, CMD_MODE, chan, "%H +o %C",
2725 chan, jbuf->jb_source);
2728 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || IsLocalChannel(chan->chname))
2729 return; /* don't send to remote */
2731 /* figure out if channel name will cause buffer to be overflowed */
2732 len = chan ? strlen(chan->chname) + 1 : 2;
2733 if (jbuf->jb_strlen + len > BUFSIZE)
2734 joinbuf_flush(jbuf);
2736 /* add channel to list of channels to send and update counts */
2737 jbuf->jb_channels[jbuf->jb_count++] = chan;
2738 jbuf->jb_strlen += len;
2740 /* if we've used up all slots, flush */
2741 if (jbuf->jb_count >= MAXJOINARGS)
2742 joinbuf_flush(jbuf);
2746 * Flush the channel list to remote servers
2749 joinbuf_flush(struct JoinBuf *jbuf)
2751 char chanlist[BUFSIZE];
2755 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2756 jbuf->jb_type == JOINBUF_TYPE_JOIN)
2757 return 0; /* no joins to process */
2759 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
2760 build_string(chanlist, &chanlist_i,
2761 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
2762 i == 0 ? '\0' : ',');
2763 if (JOINBUF_TYPE_PART == jbuf->jb_type)
2764 /* Remove user from channel */
2765 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
2767 jbuf->jb_channels[i] = 0; /* mark slot empty */
2770 jbuf->jb_count = 0; /* reset base counters */
2771 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
2772 STARTJOINLEN : STARTCREATELEN) +
2773 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
2775 /* and send the appropriate command */
2776 switch (jbuf->jb_type) {
2777 case JOINBUF_TYPE_CREATE:
2778 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
2779 "%s %Tu", chanlist, jbuf->jb_create);
2782 case JOINBUF_TYPE_PART:
2783 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
2784 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,