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.
24 #include "gline.h" /* bad_channel */
27 #include "ircd_alloc.h"
28 #include "ircd_chattr.h"
29 #include "ircd_reply.h"
30 #include "ircd_string.h"
36 #include "querycmds.h"
43 #include "sprintf_irc.h"
53 struct Channel* GlobalChannelList = 0;
55 static struct SLink *next_overlapped_ban(void);
56 static int del_banid(struct Channel *, char *, int);
57 void del_invite(struct Client *, struct Channel *);
59 const char* const PartFmt1 = ":%s " MSG_PART " %s";
60 const char* const PartFmt2 = ":%s " MSG_PART " %s :%s";
61 const char* const PartFmt1serv = "%s%s " TOK_PART " %s";
62 const char* const PartFmt2serv = "%s%s " TOK_PART " %s :%s";
65 static struct SLink* next_ban;
66 static struct SLink* prev_ban;
67 static struct SLink* removed_bans_list;
70 * Use a global variable to remember if an oper set a mode on a local channel. Ugly,
71 * but the only way to do it without changing set_mode intensively.
73 int LocalChanOperMode = 0;
77 * return the length (>=0) of a chain of links.
79 static int list_length(struct SLink *lp)
83 for (; lp; lp = lp->next)
89 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
95 /* Servers don't have member links */
99 /* +k users are typically on a LOT of channels. So we iterate over who
100 * is in the channel. X/W are +k and are in about 5800 channels each.
101 * however there are typically no more than 1000 people in a channel
104 if (IsChannelService(cptr)) {
107 assert(m->channel == chptr);
113 /* Users on the other hand aren't allowed on more than 15 channels. 50%
114 * of users that are on channels are on 2 or less, 95% are on 7 or less,
115 * and 99% are on 10 or less.
118 m = cptr->user->channel;
120 assert(m->user == cptr);
121 if (m->channel == chptr)
130 * find_chasing - Find the client structure for a nick name (user)
131 * using history mechanism if necessary. If the client is not found, an error
132 * message (NO SUCH NICK) is generated. If the client was found
133 * through the history, chasing will be 1 and otherwise 0.
135 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
137 struct Client* who = FindClient(user);
144 if (!(who = get_history(user, KILLCHASETIMELIMIT))) {
145 sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, sptr->name, user);
154 * Create a string of form "foo!bar@fubar" given foo, bar and fubar
155 * as the parameters. If NULL, they become "*".
157 static char *make_nick_user_host(const char *nick, const char *name,
160 static char namebuf[NICKLEN + USERLEN + HOSTLEN + 3];
161 sprintf_irc(namebuf, "%s!%s@%s", nick, name, host);
166 * Create a string of form "foo!bar@123.456.789.123" given foo, bar and the
167 * IP-number as the parameters. If NULL, they become "*".
169 static char *make_nick_user_ip(char *nick, char *name, struct in_addr ip)
171 static char ipbuf[NICKLEN + USERLEN + 16 + 3];
172 sprintf_irc(ipbuf, "%s!%s@%s", nick, name, ircd_ntoa((const char*) &ip));
177 static int DoesOp(const char* modebuf)
179 assert(0 != modebuf);
181 if (*modebuf == 'o' || *modebuf == 'v')
189 * This function should be removed when all servers are 2.10
191 static void sendmodeto_one(struct Client* cptr, const char* from,
192 const char* name, const char* mode,
193 const char* param, time_t creationtime)
195 if (IsServer(cptr) && DoesOp(mode) && creationtime)
196 sendto_one(cptr, ":%s MODE %s %s %s " TIME_T_FMT,
197 from, name, mode, param, creationtime);
199 sendto_one(cptr, ":%s MODE %s %s %s", from, name, mode, param);
204 * Subtract one user from channel i (and free channel
205 * block, if channel became empty).
206 * Returns: true (1) if channel still exists
207 * false (0) if the channel was destroyed
209 int sub1_from_channel(struct Channel* chptr)
214 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
216 assert(0 != chptr->members);
221 assert(0 == chptr->members);
223 /* Channel became (or was) empty: Remove channel */
224 if (is_listed(chptr))
227 for (i = 0; i <= HighestFd; i++)
229 struct Client *acptr;
230 if ((acptr = LocalClientArray[i]) && acptr->listing &&
231 acptr->listing->chptr == chptr)
233 list_next_channels(acptr, 1);
234 break; /* Only one client can list a channel */
239 * Now, find all invite links from channel structure
241 while ((tmp = chptr->invites))
242 del_invite(tmp->value.cptr, chptr);
244 tmp = chptr->banlist;
249 MyFree(obtmp->value.ban.banstr);
250 MyFree(obtmp->value.ban.who);
254 chptr->prev->next = chptr->next;
256 GlobalChannelList = chptr->next;
258 chptr->next->prev = chptr->prev;
260 --UserStats.channels;
262 * make sure that channel actually got removed from hash table
264 assert(chptr->hnext == chptr);
272 * `cptr' must be the client adding the ban.
274 * If `change' is true then add `banid' to channel `chptr'.
275 * Returns 0 if the ban was added.
276 * Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
277 * Return -1 otherwise.
279 * Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
280 * when `change' is false, otherwise they will be removed from the banlist.
281 * Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
282 * respectively will return these bans until NULL is returned.
284 * If `firsttime' is true, the ban list as returned by next_overlapped_ban()
285 * is reset (unless a non-zero value is returned, in which case the
286 * CHFL_BAN_OVERLAPPED flag might not have been reset!).
290 int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
291 int change, int firsttime)
296 int removed_bans = 0;
297 int len = strlen(banid);
302 assert(0 == prev_ban);
303 assert(0 == removed_bans_list);
307 for (banp = &chptr->banlist; *banp;)
309 len += strlen((*banp)->value.ban.banstr);
311 if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
313 if (!strcmp((*banp)->value.ban.banstr, banid))
315 (*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
319 else if (!mmatch((*banp)->value.ban.banstr, banid))
321 if (!mmatch(banid, (*banp)->value.ban.banstr))
323 struct SLink *tmp = *banp;
329 len -= strlen(tmp->value.ban.banstr);
333 /* Silently remove overlapping bans */
334 MyFree(tmp->value.ban.banstr);
335 MyFree(tmp->value.ban.who);
339 /* These will be sent to the user later as -b */
340 tmp->next = removed_bans_list;
341 removed_bans_list = tmp;
345 else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
347 tmp->flags |= CHFL_BAN_OVERLAPPED;
358 (*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
359 banp = &(*banp)->next;
362 if (MyUser(cptr) && !removed_bans && (len > MAXBANLENGTH || (cnt >= MAXBANS)))
364 sendto_one(cptr, err_str(ERR_BANLISTFULL), me.name, cptr->name,
365 chptr->chname, banid);
371 struct Membership* member;
373 ban->next = chptr->banlist;
375 ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
376 assert(0 != ban->value.ban.banstr);
377 strcpy(ban->value.ban.banstr, banid);
379 ban->value.ban.who = (char*) MyMalloc(strlen(cptr->name) + 1);
380 assert(0 != ban->value.ban.who);
381 strcpy(ban->value.ban.who, cptr->name);
383 ban->value.ban.when = CurrentTime;
384 ban->flags = CHFL_BAN; /* This bit is never used I think... */
385 if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
386 ban->flags |= CHFL_BAN_IPMASK;
387 chptr->banlist = ban;
390 * Erase ban-valid-bit
392 for (member = chptr->members; member; member = member->next_member)
393 ClearBanValid(member); /* `ban' == channel member ! */
398 static struct SLink *next_overlapped_ban(void)
400 struct SLink *tmp = next_ban;
404 for (ban = tmp->next; ban; ban = ban->next)
405 if ((ban->flags & CHFL_BAN_OVERLAPPED))
412 struct SLink *next_removed_overlapped_ban(void)
414 struct SLink *tmp = removed_bans_list;
417 if (prev_ban->value.ban.banstr) /* Can be set to NULL in set_mode() */
418 MyFree(prev_ban->value.ban.banstr);
419 MyFree(prev_ban->value.ban.who);
424 removed_bans_list = removed_bans_list->next;
432 * If `change' is true, delete `banid' from channel `chptr'.
433 * Returns `false' if removal was (or would have been) successful.
435 static int del_banid(struct Channel *chptr, char *banid, int change)
442 for (ban = &(chptr->banlist); *ban; ban = &((*ban)->next)) {
443 if (0 == ircd_strcmp(banid, (*ban)->value.ban.banstr))
448 struct Membership* member;
450 MyFree(tmp->value.ban.banstr);
451 MyFree(tmp->value.ban.who);
454 * Erase ban-valid-bit, for channel members that are banned
456 for (member = chptr->members; member; member = member->next_member)
457 if (CHFL_BANVALIDMASK == (member->status & CHFL_BANVALIDMASK))
458 ClearBanValid(member); /* `tmp' == channel member */
467 * find_channel_member - returns Membership * if a person is joined and not a zombie
469 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
471 struct Membership* member;
474 member = find_member_link(chptr, cptr);
475 return (member && !IsZombie(member)) ? member : 0;
479 * is_banned - a non-zero value if banned else 0.
481 static int is_banned(struct Client *cptr, struct Channel *chptr,
482 struct Membership* member)
491 if (member && IsBanValid(member))
492 return IsBanned(member);
494 s = make_nick_user_host(cptr->name, cptr->user->username, cptr->user->host);
496 for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
497 if ((tmp->flags & CHFL_BAN_IPMASK)) {
499 ip_s = make_nick_user_ip(cptr->name, cptr->user->username, cptr->ip);
500 if (match(tmp->value.ban.banstr, ip_s) == 0)
503 else if (match(tmp->value.ban.banstr, s) == 0)
519 return (tmp != NULL);
523 * adds a user to a channel by adding another link to the channels member
526 void add_user_to_channel(struct Channel* chptr, struct Client* who,
533 struct Membership* member =
534 (struct Membership*) MyMalloc(sizeof(struct Membership));
537 member->channel = chptr;
538 member->status = flags;
540 member->next_member = chptr->members;
541 if (member->next_member)
542 member->next_member->prev_member = member;
543 member->prev_member = 0;
544 chptr->members = member;
546 member->next_channel = who->user->channel;
547 if (member->next_channel)
548 member->next_channel->prev_channel = member;
549 member->prev_channel = 0;
550 who->user->channel = member;
557 static int remove_member_from_channel(struct Membership* member)
559 struct Channel* chptr;
561 chptr = member->channel;
563 * unlink channel member list
565 if (member->next_member)
566 member->next_member->prev_member = member->prev_member;
567 if (member->prev_member)
568 member->prev_member->next_member = member->next_member;
570 member->channel->members = member->next_member;
573 * unlink client channel list
575 if (member->next_channel)
576 member->next_channel->prev_channel = member->prev_channel;
577 if (member->prev_channel)
578 member->prev_channel->next_channel = member->next_channel;
580 member->user->user->channel = member->next_channel;
582 --member->user->user->joined;
585 return sub1_from_channel(chptr);
588 static int channel_all_zombies(struct Channel* chptr)
590 struct Membership* member;
592 for (member = chptr->members; member; member = member->next_member) {
593 if (!IsZombie(member))
600 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
603 struct Membership* member;
606 if ((member = find_member_link(chptr, cptr))) {
607 if (remove_member_from_channel(member)) {
608 if (channel_all_zombies(chptr)) {
610 * XXX - this looks dangerous but isn't if we got the referential
611 * integrity right for channels
613 while (remove_member_from_channel(chptr->members))
620 void remove_user_from_all_channels(struct Client* cptr)
622 struct Membership* chan;
624 assert(0 != cptr->user);
626 while ((chan = cptr->user->channel))
627 remove_user_from_channel(cptr, chan->channel);
630 int is_chan_op(struct Client *cptr, struct Channel *chptr)
632 struct Membership* member;
634 if ((member = find_member_link(chptr, cptr)))
635 return (!IsZombie(member) && IsChanOp(member));
640 static int is_deopped(struct Client *cptr, struct Channel *chptr)
642 struct Membership* member;
645 if ((member = find_member_link(chptr, cptr)))
646 return IsDeopped(member);
648 return (IsUser(cptr) ? 1 : 0);
651 int is_zombie(struct Client *cptr, struct Channel *chptr)
653 struct Membership* member;
657 if ((member = find_member_link(chptr, cptr)))
658 return IsZombie(member);
662 int has_voice(struct Client* cptr, struct Channel* chptr)
664 struct Membership* member;
667 if ((member = find_member_link(chptr, cptr)))
668 return (!IsZombie(member) && HasVoice(member));
673 int member_can_send_to_channel(struct Membership* member)
677 if (IsVoicedOrOpped(member))
680 * If it's moderated, and you aren't a priviledged user, you can't
683 if (member->channel->mode.mode & MODE_MODERATED)
686 * If you're banned then you can't speak either.
687 * but because of the amount of CPU time that is_banned chews
688 * we only check it for our clients.
690 if (MyUser(member->user) && is_banned(member->user, member->channel, member))
695 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
697 struct Membership *member;
700 * Servers can always speak on channels.
705 member = find_channel_member(cptr, chptr);
708 * You can't speak if your off channel, if the channel is modeless, or
709 * +n.(no external messages)
712 if ((chptr->mode.mode & MODE_NOPRIVMSGS) || IsModelessChannel(chptr->chname))
717 return member_can_send_to_channel(member);
721 * find_no_nickchange_channel
722 * if a member and not opped or voiced and banned
723 * return the name of the first channel banned on
725 const char* find_no_nickchange_channel(struct Client* cptr)
728 struct Membership* member;
729 for (member = cptr->user->channel; member; member = member->next_channel) {
730 if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
731 return member->channel->chname;
739 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
740 * with the parameters in pbuf.
742 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
743 struct Channel *chptr)
750 if (chptr->mode.mode & MODE_SECRET)
752 else if (chptr->mode.mode & MODE_PRIVATE)
754 if (chptr->mode.mode & MODE_MODERATED)
756 if (chptr->mode.mode & MODE_TOPICLIMIT)
758 if (chptr->mode.mode & MODE_INVITEONLY)
760 if (chptr->mode.mode & MODE_NOPRIVMSGS)
762 if (chptr->mode.limit) {
764 sprintf_irc(pbuf, "%d", chptr->mode.limit);
767 if (*chptr->mode.key) {
769 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
770 if (chptr->mode.limit)
772 strcat(pbuf, chptr->mode.key);
779 static int send_mode_list(struct Client *cptr, char *chname,
780 time_t creationtime, struct SLink *top,
790 cp = modebuf + strlen(modebuf);
791 if (*parabuf) /* mode +l or +k xx */
793 for (lp = top; lp; lp = lp->next)
795 if (!(lp->flags & mask))
797 if (mask == CHFL_BAN)
798 name = lp->value.ban.banstr;
800 name = lp->value.cptr->name;
801 if (strlen(parabuf) + strlen(name) + 11 < MODEBUFLEN)
803 strcat(parabuf, " ");
804 strcat(parabuf, name);
815 /* cptr is always a server! So we send creationtimes */
816 sendmodeto_one(cptr, me.name, chname, modebuf, parabuf, creationtime);
824 strcpy(parabuf, name);
837 * send "cptr" a full list of the modes for channel chptr.
839 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
841 static unsigned int current_flags[4] =
842 { 0, CHFL_CHANOP | CHFL_VOICE, CHFL_VOICE, CHFL_CHANOP };
849 struct Membership* member;
851 char modebuf[MODEBUFLEN];
852 char parabuf[MODEBUFLEN];
857 if (IsLocalChannel(chptr->chname))
860 member = chptr->members;
861 lp2 = chptr->banlist;
863 *modebuf = *parabuf = '\0';
864 channel_modes(cptr, modebuf, parabuf, chptr);
866 for (first = 1; full; first = 0) /* Loop for multiple messages */
868 full = 0; /* Assume by default we get it
869 all in one message */
871 /* (Continued) prefix: "<Y> B <channel> <TS>" */
872 sprintf_irc(sendbuf, "%s B %s " TIME_T_FMT, NumServ(&me),
873 chptr->chname, chptr->creationtime);
874 sblen = strlen(sendbuf);
876 if (first && modebuf[1]) /* Add simple modes (iklmnpst)
879 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
880 sendbuf[sblen++] = ' ';
881 strcpy(sendbuf + sblen, modebuf);
882 sblen += strlen(modebuf);
885 sendbuf[sblen++] = ' ';
886 strcpy(sendbuf + sblen, parabuf);
887 sblen += strlen(parabuf);
892 * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
894 * Run 4 times over all members, to group the members with the
897 for (first = 1; flag_cnt < 4;
898 member = chptr->members, new_mode = 1, flag_cnt++)
900 for (; member; member = member->next_member)
902 if ((member->status & CHFL_VOICED_OR_OPPED) !=
903 current_flags[flag_cnt])
904 continue; /* Skip members with different flags */
905 if (sblen + NUMNICKLEN + 4 > BUFSIZE - 3)
906 /* The 4 is a possible ",:ov"
907 The -3 is for the "\r\n\0" that is added in send.c */
909 full = 1; /* Make sure we continue after
911 new_mode = 1; /* Ensure the new BURST line contains the current
913 break; /* Do not add this member to this message */
915 sendbuf[sblen++] = first ? ' ' : ',';
916 first = 0; /* From now on, us comma's to add new nicks */
918 sprintf_irc(sendbuf + sblen, "%s%s", NumNick(member->user));
919 sblen += strlen(sendbuf + sblen);
921 * Do we have a nick with a new mode ?
922 * Or are we starting a new BURST line?
927 if (IsVoicedOrOpped(member)) {
928 sendbuf[sblen++] = ':';
929 if (IsChanOp(member))
930 sendbuf[sblen++] = 'o';
931 if (HasVoice(member))
932 sendbuf[sblen++] = 'v';
942 /* Attach all bans, space seperated " :%ban ban ..." */
943 for (first = 2; lp2; lp2 = lp2->next)
945 len = strlen(lp2->value.ban.banstr);
946 if (sblen + len + 1 + first > BUFSIZE - 3)
947 /* The +1 stands for the added ' '.
948 * The +first stands for the added ":%".
949 * The -3 is for the "\r\n\0" that is added in send.c
958 sendbuf[sblen++] = ' ';
959 sendbuf[sblen++] = ':'; /* Will be last parameter */
960 sendbuf[sblen++] = '%'; /* To tell bans apart */
963 sendbuf[sblen++] = ' ';
964 strcpy(sendbuf + sblen, lp2->value.ban.banstr);
969 sendbuf[sblen] = '\0';
970 sendbufto_one(cptr); /* Send this message */
971 } /* Continue when there was something
972 that didn't fit (full==1) */
978 * by Carlo Wood (Run), 05 Oct 1998.
982 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
983 * When the user name or host name are too long (USERLEN and HOSTLEN
984 * respectively) then they are cut off at the start with a '*'.
986 * The following transformations are made:
989 * 2) xxx.xxx -> *!*@host
990 * 3) xxx!yyy -> nick!user@*
991 * 4) xxx@yyy -> *!user@host
992 * 5) xxx!yyy@zzz -> nick!user@host
994 char *pretty_mask(char *mask)
996 static char star[2] = { '*', 0 };
997 char *last_dot = NULL;
1000 /* Case 1: default */
1005 /* Do a _single_ pass through the characters of the mask: */
1006 for (ptr = mask; *ptr; ++ptr)
1010 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1014 else if (*ptr == '@')
1016 /* Case 4: Found last '@' (without finding a '!' yet) */
1021 else if (*ptr == '.')
1023 /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
1033 /* Case 4 or 5: Found last '@' */
1039 if (user == star && last_dot)
1049 char *nick_end = (user != star) ? user - 1 : ptr;
1050 if (nick_end - nick > NICKLEN)
1056 char *user_end = (host != star) ? host - 1 : ptr;
1057 if (user_end - user > USERLEN)
1059 user = user_end - USERLEN;
1064 if (host != star && ptr - host > HOSTLEN)
1066 host = ptr - HOSTLEN;
1069 return make_nick_user_host(nick, user, host);
1072 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1079 for (lp = chptr->banlist; lp; lp = lp->next)
1080 sendto_one(cptr, rpl_str(RPL_BANLIST), me.name, cptr->name,
1081 chptr->chname, lp->value.ban.banstr, lp->value.ban.who,
1082 lp->value.ban.when);
1083 sendto_one(cptr, rpl_str(RPL_ENDOFBANLIST), me.name, cptr->name,
1088 * Check and try to apply the channel modes passed in the parv array for
1089 * the client ccptr to channel chptr. The resultant changes are printed
1090 * into mbuf and pbuf (if any) and applied to the channel.
1092 int set_mode(struct Client* cptr, struct Client* sptr,
1093 struct Channel* chptr, int parc, char* parv[],
1094 char* mbuf, char* pbuf, char* npbuf, int* badop)
1097 * This size is only needed when a broken
1098 * server sends more then MAXMODEPARAMS
1101 static struct SLink chops[MAXPARA - 2];
1102 static int flags[] = {
1105 MODE_MODERATED, 'm',
1106 MODE_NOPRIVMSGS, 'n',
1107 MODE_TOPICLIMIT, 't',
1108 MODE_INVITEONLY, 'i',
1114 char bmodebuf[MODEBUFLEN];
1115 char bparambuf[MODEBUFLEN];
1116 char nbparambuf[MODEBUFLEN]; /* "Numeric" Bounce Parameter Buffer */
1118 char* curr = parv[0];
1121 struct Membership* member_x;
1122 struct Membership* member_y;
1123 unsigned int whatt = MODE_ADD;
1124 unsigned int bwhatt = 0;
1127 int add_banid_called = 0;
1133 unsigned int nusers = 0;
1134 unsigned int newmode;
1145 static char numeric[16];
1146 char* bmbuf = bmodebuf;
1147 char* bpbuf = bparambuf;
1148 char* nbpbuf = nbparambuf;
1150 struct ConfItem* aconf;
1152 *mbuf = *pbuf = *npbuf = *bmbuf = *bpbuf = *nbpbuf = '\0';
1157 * Mode is accepted when sptr is a channel operator
1158 * but also when the mode is received from a server.
1159 * At this point, let any member pass, so they are allowed
1162 member_y = find_channel_member(sptr, chptr);
1163 if (!(IsServer(cptr) || member_y))
1166 #ifdef OPER_MODE_LCHAN
1167 if (IsOperOnLocalChannel(sptr, chptr->chname) && !IsChanOp(member_y))
1168 LocalChanOperMode = 1;
1171 mode = &(chptr->mode);
1172 memcpy(&oldm, mode, sizeof(struct Mode));
1174 newmode = mode->mode;
1176 while (curr && *curr) {
1189 if (MyUser(sptr) && opcnt >= MAXMODEPARAMS)
1192 * Check for nickname changes and try to follow these
1193 * to make sure the right client is affected by the
1195 * Even if we find a nick with find_chasing() there
1196 * is still a reason to ignore in a special case.
1197 * We need to ignore the mode when:
1198 * - It is part of a net.burst (from a server and
1199 * a MODE_ADD). Ofcourse we don't ignore mode
1200 * changes from Uworld.
1201 * - The found nick is not on the right side off
1203 * This fixes the bug that when someone (tries to)
1204 * ride a net.break and does so with the nick of
1205 * someone on the otherside, that he is nick collided
1206 * (killed) but his +o still ops the other person.
1210 if (!(who = find_chasing(sptr, parv[0], NULL)))
1215 if (!(who = findNUser(parv[0])))
1218 if (whatt == MODE_ADD && IsServer(sptr) && who->from != sptr->from &&
1219 !find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD))
1222 if (!(member_x = find_member_link(chptr, who)) ||
1223 (MyUser(sptr) && IsZombie(member_x)))
1225 sendto_one(cptr, err_str(ERR_USERNOTINCHANNEL),
1226 me.name, cptr->name, who->name, chptr->chname);
1230 * if the user is +k, prevent a deop from local user
1232 if (whatt == MODE_DEL && IsChannelService(who) && *curr == 'o') {
1237 sendto_one(cptr, err_str(ERR_ISCHANSERVICE), me.name,
1238 cptr->name, parv[0], chptr->chname);
1242 sprintf_irc(sendbuf,":%s NOTICE * :*** Notice -- Deop of +k user on %s by %s",
1243 me.name,chptr->chname,cptr->name);
1246 #ifdef NO_OPER_DEOP_LCHAN
1248 * if the user is an oper on a local channel, prevent him
1249 * from being deoped. that oper can deop himself though.
1251 if (whatt == MODE_DEL && IsOperOnLocalChannel(who, chptr->chname) &&
1252 (who != sptr) && MyUser(cptr) && *curr == 'o')
1254 sendto_one(cptr, err_str(ERR_ISOPERLCHAN), me.name,
1255 cptr->name, parv[0], chptr->chname);
1259 if (whatt == MODE_ADD)
1261 lp = &chops[opcnt++];
1262 lp->value.cptr = who;
1263 if (IsServer(sptr) && (!(who->flags & FLAGS_TS8) || ((*curr == 'o') &&
1264 !(member_x->status & (CHFL_SERVOPOK | CHFL_CHANOP)))))
1265 *badop = ((member_x->status & CHFL_DEOPPED) && (*curr == 'o')) ? 2 : 3;
1266 lp->flags = (*curr == 'o') ? MODE_CHANOP : MODE_VOICE;
1267 lp->flags |= MODE_ADD;
1269 else if (whatt == MODE_DEL)
1271 lp = &chops[opcnt++];
1272 lp->value.cptr = who;
1273 doesdeop = 1; /* Also when -v */
1274 lp->flags = (*curr == 'o') ? MODE_CHANOP : MODE_VOICE;
1275 lp->flags |= MODE_DEL;
1284 /* check now so we eat the parameter if present */
1289 char *s = &(*parv)[-1];
1290 unsigned short count = KEYLEN + 1;
1292 while (*++s > ' ' && *s != ':' && --count);
1294 if (!**parv) /* nothing left in key */
1297 if (MyUser(sptr) && opcnt >= MAXMODEPARAMS)
1299 if (whatt == MODE_ADD)
1301 if (*mode->key && !IsServer(cptr))
1302 sendto_one(cptr, err_str(ERR_KEYSET),
1303 me.name, cptr->name, chptr->chname);
1304 else if (!*mode->key || IsServer(cptr))
1306 lp = &chops[opcnt++];
1307 lp->value.cp = *parv;
1308 if (strlen(lp->value.cp) > KEYLEN)
1309 lp->value.cp[KEYLEN] = '\0';
1310 lp->flags = MODE_KEY | MODE_ADD;
1314 else if (whatt == MODE_DEL)
1316 /* Debug((DEBUG_INFO, "removing key: mode->key: >%s< *parv: >%s<", mode->key, *parv)); */
1317 if (0 == ircd_strcmp(mode->key, *parv) || IsServer(cptr))
1319 /* Debug((DEBUG_INFO, "key matched")); */
1320 lp = &chops[opcnt++];
1321 lp->value.cp = mode->key;
1322 lp->flags = MODE_KEY | MODE_DEL;
1329 if (0 == banlsent) {
1333 send_ban_list(cptr, chptr);
1339 if (EmptyString(*parv))
1343 if ((cp = strchr(*parv, ' ')))
1345 if (opcnt >= MAXMODEPARAMS || **parv == ':' || **parv == '\0')
1348 if (whatt == MODE_ADD)
1350 lp = &chops[opcnt++];
1351 lp->value.cp = *parv;
1352 lp->flags = MODE_ADD | MODE_BAN;
1354 else if (whatt == MODE_DEL)
1356 lp = &chops[opcnt++];
1357 lp->value.cp = *parv;
1358 lp->flags = MODE_DEL | MODE_BAN;
1363 * limit 'l' to only *1* change per mode command but
1368 if (whatt == MODE_ADD && --parc > 0)
1372 if (whatt == MODE_DEL)
1380 if (EmptyString(*parv))
1382 if (MyUser(sptr) && opcnt >= MAXMODEPARAMS)
1384 if (!(nusers = atoi(*++parv)))
1386 lp = &chops[opcnt++];
1387 lp->flags = MODE_ADD | MODE_LIMIT;
1391 need_more_params(cptr, "MODE +l");
1393 case 'i': /* falls through for default case */
1394 if (whatt == MODE_DEL)
1395 while ((lp = chptr->invites))
1396 del_invite(lp->value.cptr, chptr);
1398 for (ip = flags; *ip; ip += 2)
1399 if (*(ip + 1) == *curr)
1404 if (whatt == MODE_ADD)
1406 if (*ip == MODE_PRIVATE)
1407 newmode &= ~MODE_SECRET;
1408 else if (*ip == MODE_SECRET)
1409 newmode &= ~MODE_PRIVATE;
1415 else if (!IsServer(cptr))
1416 sendto_one(cptr, err_str(ERR_UNKNOWNMODE),
1417 me.name, cptr->name, *curr);
1422 * Make sure mode strings such as "+m +t +p +i" are parsed
1425 if (!*curr && parc > 0)
1429 /* If this was from a server, and it is the last
1430 * parameter and it starts with a digit, it must
1431 * be the creationtime. --Run
1435 if (parc == 1 && IsDigit(*curr))
1437 newtime = atoi(curr);
1438 if (newtime && chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
1440 chptr->creationtime = newtime;
1449 else if (newtime > chptr->creationtime)
1450 { /* It is a net-break ride if we have ops.
1451 bounce modes if we have ops. --Run */
1454 else if (chptr->creationtime == 0)
1456 if (chptr->creationtime == 0 || doesop)
1457 chptr->creationtime = newtime;
1465 * A legal *badop can occur when two
1466 * people join simultaneously a channel,
1467 * Allow for 10 min of lag (and thus hacking
1468 * on channels younger then 10 min) --Run
1470 else if (*badop == 0 ||
1471 chptr->creationtime > (TStime() - TS_LAG_TIME))
1473 if (newtime < chptr->creationtime)
1474 chptr->creationtime = newtime;
1483 } /* end of while loop for MODE processing */
1485 #ifdef OPER_MODE_LCHAN
1487 * Now reject non chan ops. Accept modes from opers on local channels
1488 * even if they are deopped
1490 if (!IsServer(cptr) &&
1491 (!member_y || !(IsChanOp(member_y) ||
1492 IsOperOnLocalChannel(sptr, chptr->chname))))
1494 if (!IsServer(cptr) && (!member_y || !IsChanOp(member_y)))
1498 return (opcnt || newmode != mode->mode || limitset || keychange) ? 0 : -1;
1501 if (doesop && newtime == 0 && IsServer(sptr))
1505 (aconf = find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD)))
1508 #ifdef OPER_MODE_LCHAN
1509 bounce = (*badop == 1 || *badop == 2 ||
1510 (is_deopped(sptr, chptr) &&
1511 !IsOperOnLocalChannel(sptr, chptr->chname))) ? 1 : 0;
1513 bounce = (*badop == 1 || *badop == 2 || is_deopped(sptr, chptr)) ? 1 : 0;
1517 for (ip = flags; *ip; ip += 2) {
1518 if ((*ip & newmode) && !(*ip & oldm.mode))
1522 if (bwhatt != MODE_DEL)
1527 *bmbuf++ = *(ip + 1);
1531 if (whatt != MODE_ADD)
1537 *mbuf++ = *(ip + 1);
1541 for (ip = flags; *ip; ip += 2) {
1542 if ((*ip & oldm.mode) && !(*ip & newmode))
1546 if (bwhatt != MODE_ADD)
1551 *bmbuf++ = *(ip + 1);
1555 if (whatt != MODE_DEL)
1561 *mbuf++ = *(ip + 1);
1566 if (limitset && !nusers && mode->limit)
1570 if (bwhatt != MODE_ADD)
1576 sprintf(numeric, "%-15d", mode->limit);
1577 if ((cp = strchr(numeric, ' ')))
1579 strcat(bpbuf, numeric);
1580 blen += strlen(numeric);
1582 strcat(nbpbuf, numeric);
1583 nblen += strlen(numeric);
1584 strcat(nbpbuf, " ");
1588 if (whatt != MODE_DEL)
1593 mode->mode &= ~MODE_LIMIT;
1599 * Reconstruct "+bkov" chain.
1605 unsigned int prev_whatt = 0;
1607 for (; i < opcnt; i++)
1611 * make sure we have correct mode change sign
1613 if (whatt != (lp->flags & (MODE_ADD | MODE_DEL)))
1615 if (lp->flags & MODE_ADD)
1629 nlen = strlen(npbuf);
1631 * get c as the mode char and tmp as a pointer to
1632 * the parameter for this mode change.
1634 switch (lp->flags & MODE_WPARAS)
1638 cp = lp->value.cptr->name;
1642 cp = lp->value.cptr->name;
1646 * I made this a bit more user-friendly (tm):
1648 * nick!user = nick!user@*
1649 * user@host = *!user@host
1650 * host.name = *!*@host.name --Run
1653 cp = pretty_mask(lp->value.cp);
1661 sprintf(numeric, "%-15d", nusers);
1662 if ((cp = strchr(numeric, ' ')))
1668 /* What could be added: cp+' '+' '+<TS>+'\0' */
1669 if (len + strlen(cp) + 13 > MODEBUFLEN ||
1670 nlen + strlen(cp) + NUMNICKLEN + 12 > MODEBUFLEN)
1673 switch (lp->flags & MODE_WPARAS)
1676 if (strlen(cp) > KEYLEN)
1677 *(cp + KEYLEN) = '\0';
1678 if ((whatt == MODE_ADD && (*mode->key == '\0' ||
1679 0 != ircd_strcmp(mode->key, cp))) ||
1680 (whatt == MODE_DEL && (*mode->key != '\0')))
1684 if (*mode->key == '\0')
1686 if (bwhatt != MODE_DEL)
1696 nblen += strlen(cp);
1697 strcat(nbpbuf, " ");
1702 if (bwhatt != MODE_ADD)
1707 strcat(bpbuf, mode->key);
1708 blen += strlen(mode->key);
1711 strcat(nbpbuf, mode->key);
1712 nblen += strlen(mode->key);
1713 strcat(nbpbuf, " ");
1718 if (*mbuf != '+' && *mbuf != '-')
1734 if (whatt == MODE_ADD)
1735 ircd_strncpy(mode->key, cp, KEYLEN);
1742 if (nusers && nusers != mode->limit)
1746 if (mode->limit == 0)
1748 if (bwhatt != MODE_DEL)
1756 if (bwhatt != MODE_ADD)
1761 sprintf(numeric, "%-15d", mode->limit);
1762 if ((cp = strchr(numeric, ' ')))
1764 strcat(bpbuf, numeric);
1765 blen += strlen(numeric);
1768 strcat(nbpbuf, numeric);
1769 nblen += strlen(numeric);
1770 strcat(nbpbuf, " ");
1775 if (*mbuf != '+' && *mbuf != '-')
1791 mode->limit = nusers;
1797 member_y = find_member_link(chptr, lp->value.cptr);
1798 if (lp->flags & MODE_ADD)
1800 change = (~member_y->status) & CHFL_VOICED_OR_OPPED & lp->flags;
1801 if (change && bounce)
1803 if (lp->flags & MODE_CHANOP)
1804 SetDeopped(member_y);
1806 if (bwhatt != MODE_DEL)
1812 strcat(bpbuf, lp->value.cptr->name);
1813 blen += strlen(lp->value.cptr->name);
1816 sprintf_irc(nbpbuf + nblen, "%s%s ", NumNick(lp->value.cptr));
1817 nblen += strlen(nbpbuf + nblen);
1822 member_y->status |= lp->flags & CHFL_VOICED_OR_OPPED;
1823 if (IsChanOp(member_y))
1825 ClearDeopped(member_y);
1827 ClearServOpOk(member_y);
1833 change = member_y->status & CHFL_VOICED_OR_OPPED & lp->flags;
1834 if (change && bounce)
1836 if (lp->flags & MODE_CHANOP)
1837 ClearDeopped(member_y);
1838 if (bwhatt != MODE_ADD)
1844 strcat(bpbuf, lp->value.cptr->name);
1845 blen += strlen(lp->value.cptr->name);
1848 sprintf_irc(nbpbuf + nblen, "%s%s ", NumNick(lp->value.cptr));
1849 blen += strlen(bpbuf + blen);
1854 member_y->status &= ~change;
1855 if ((change & MODE_CHANOP) && IsServer(sptr))
1856 SetDeopped(member_y);
1859 if (change || *badop == 2 || *badop == 4)
1866 sprintf_irc(npbuf + nlen, "%s%s ", NumNick(lp->value.cptr));
1867 nlen += strlen(npbuf + nlen);
1868 npbuf[nlen++] = ' ';
1874 if (*mbuf != '+' && *mbuf != '-')
1882 * Only bans aren't bounced, it makes no sense to bounce last second
1883 * bans while propagating bans done before the net.rejoin. The reason
1884 * why I don't bounce net.rejoin bans is because it is too much
1885 * work to take care of too long strings adding the necessary TS to
1886 * net.burst bans -- RunLazy
1887 * We do have to check for *badop==2 now, we don't want HACKs to take
1890 * Since BURST - I *did* implement net.rejoin ban bouncing. So now it
1891 * certainly makes sense to also bounce 'last second' bans (bans done
1892 * after the net.junction). -- RunHardWorker
1894 if ((change = (whatt & MODE_ADD) &&
1895 !add_banid(sptr, chptr, cp, !bounce, !add_banid_called)))
1896 add_banid_called = 1;
1898 change = (whatt & MODE_DEL) && !del_banid(chptr, cp, !bounce);
1900 if (bounce && change)
1903 if ((whatt & MODE_ADD))
1905 if (bwhatt != MODE_DEL)
1911 else if ((whatt & MODE_DEL))
1913 if (bwhatt != MODE_ADD)
1925 nblen += strlen(cp);
1926 strcat(nbpbuf, " ");
1944 if (*mbuf != '+' && *mbuf != '-')
1951 } /* for (; i < opcnt; i++) */
1958 if (!hacknotice && *bmodebuf && chptr->creationtime)
1960 sendto_one(cptr, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
1961 NumServ(&me), chptr->chname, bmodebuf, nbparambuf,
1962 *badop == 2 ? (time_t) 0 : chptr->creationtime);
1964 /* If there are possibly bans to re-add, bounce them now */
1965 if (add_banid_called && bounce)
1967 struct SLink *ban[6]; /* Max 6 bans at a time */
1968 size_t len[6], sblen, total_len;
1969 int cnt, delayed = 0;
1970 while (delayed || (ban[0] = next_overlapped_ban()))
1972 len[0] = strlen(ban[0]->value.ban.banstr);
1973 cnt = 1; /* We already got one ban :) */
1974 sblen = sprintf_irc(sendbuf, ":%s MODE %s +b",
1975 me.name, chptr->chname) - sendbuf;
1976 total_len = sblen + 1 + len[0]; /* 1 = ' ' */
1977 /* Find more bans: */
1979 while (cnt < 6 && (ban[cnt] = next_overlapped_ban()))
1981 len[cnt] = strlen(ban[cnt]->value.ban.banstr);
1982 if (total_len + 5 + len[cnt] > BUFSIZE) /* 5 = "b \r\n\0" */
1984 delayed = cnt + 1; /* != 0 */
1987 sendbuf[sblen++] = 'b';
1988 total_len += 2 + len[cnt++]; /* 2 = "b " */
1992 sendbuf[sblen++] = ' ';
1993 strcpy(sendbuf + sblen, ban[cnt]->value.ban.banstr);
1996 sendbufto_one(cptr); /* Send bounce to uplink */
1998 ban[0] = ban[delayed - 1];
2001 /* Send -b's of overlapped bans to clients to keep them synchronized */
2002 if (add_banid_called && !bounce)
2005 char *banstr[6]; /* Max 6 bans at a time */
2006 size_t len[6], sblen, psblen, total_len;
2007 int cnt, delayed = 0;
2008 struct Membership* member_z;
2009 struct Client *acptr;
2011 psblen = sprintf_irc(sendbuf, ":%s MODE %s -b",
2012 sptr->name, chptr->chname) - sendbuf;
2013 else /* We rely on IsRegistered(sptr) being true for MODE */
2014 psblen = sprintf_irc(sendbuf, ":%s!%s@%s MODE %s -b", sptr->name,
2015 sptr->user->username, sptr->user->host, chptr->chname) - sendbuf;
2016 while (delayed || (ban = next_removed_overlapped_ban()))
2020 len[0] = strlen((banstr[0] = ban->value.ban.banstr));
2021 ban->value.ban.banstr = NULL;
2023 cnt = 1; /* We already got one ban :) */
2025 total_len = sblen + 1 + len[0]; /* 1 = ' ' */
2026 /* Find more bans: */
2028 while (cnt < 6 && (ban = next_removed_overlapped_ban()))
2030 len[cnt] = strlen((banstr[cnt] = ban->value.ban.banstr));
2031 ban->value.ban.banstr = NULL;
2032 if (total_len + 5 + len[cnt] > BUFSIZE) /* 5 = "b \r\n\0" */
2034 delayed = cnt + 1; /* != 0 */
2037 sendbuf[sblen++] = 'b';
2038 total_len += 2 + len[cnt++]; /* 2 = "b " */
2042 sendbuf[sblen++] = ' ';
2043 strcpy(sendbuf + sblen, banstr[cnt]);
2044 MyFree(banstr[cnt]);
2047 for (member_z = chptr->members; member_z; member_z = member_z->next_member) {
2048 acptr = member_z->user;
2049 if (MyConnect(acptr) && !IsZombie(member_z))
2050 sendbufto_one(acptr);
2054 banstr[0] = banstr[delayed - 1];
2055 len[0] = len[delayed - 1];
2060 return gotts ? 1 : -1;
2063 /* We are now treating the <key> part of /join <channel list> <key> as a key
2064 * ring; that is, we try one key against the actual channel key, and if that
2065 * doesn't work, we try the next one, and so on. -Kev -Texaco
2066 * Returns: 0 on match, 1 otherwise
2067 * This version contributed by SeKs <intru@info.polymtl.ca>
2069 static int compall(char *key, char *keyring)
2074 p1 = key; /* point to the key... */
2075 while (*p1 && *p1 == *keyring)
2076 { /* step through the key and ring until they
2082 if (!*p1 && (!*keyring || *keyring == ','))
2083 /* ok, if we're at the end of the and also at the end of one of the keys
2084 in the keyring, we have a match */
2087 if (!*keyring) /* if we're at the end of the key ring, there
2088 weren't any matches, so we return 1 */
2091 /* Not at the end of the key ring, so step
2092 through to the next key in the ring: */
2093 while (*keyring && *(keyring++) != ',');
2095 goto top; /* and check it against the key */
2098 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
2101 int overrideJoin = 0;
2103 #ifdef OPER_WALK_THROUGH_LMODES
2104 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
2105 a HACK(4) notice will be sent if he would not have been supposed
2106 to join normally. */
2107 if (IsOperOnLocalChannel(sptr,chptr->chname) && !BadPtr(key) && compall("OVERRIDE",key) == 0)
2109 overrideJoin = MAGIC_OPER_OVERRIDE;
2113 * Now a banned user CAN join if invited -- Nemesi
2114 * Now a user CAN escape channel limit if invited -- bfriendly
2116 if ((chptr->mode.mode & MODE_INVITEONLY) || (is_banned(sptr, chptr, NULL)
2117 || (chptr->mode.limit && chptr->users >= chptr->mode.limit)))
2119 for (lp = sptr->user->invited; lp; lp = lp->next)
2120 if (lp->value.chptr == chptr)
2124 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
2125 return (overrideJoin + ERR_CHANNELISFULL);
2127 * This can return an "Invite only" msg instead of the "You are banned"
2128 * if _both_ conditions are true, but who can say what is more
2129 * appropriate ? checking again IsBanned would be _SO_ cpu-xpensive !
2131 return overrideJoin + ((chptr->mode.mode & MODE_INVITEONLY) ?
2132 ERR_INVITEONLYCHAN : ERR_BANNEDFROMCHAN);
2137 * now using compall (above) to test against a whole key ring -Kev
2139 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
2140 return overrideJoin + (ERR_BADCHANNELKEY);
2146 * Remove bells and commas from channel name
2148 void clean_channelname(char *cn)
2151 if (!IsChannelChar(*cn)) {
2155 if (IsChannelLower(*cn)) {
2162 if ((unsigned char)(*cn) == 0xd0)
2170 * Get Channel block for i (and allocate a new channel
2171 * block, if it didn't exists before).
2173 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
2175 struct Channel *chptr;
2178 if (EmptyString(chname))
2181 len = strlen(chname);
2182 if (MyUser(cptr) && len > CHANNELLEN)
2185 *(chname + CHANNELLEN) = '\0';
2187 if ((chptr = FindChannel(chname)))
2189 if (flag == CGT_CREATE)
2191 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
2193 ++UserStats.channels;
2194 memset(chptr, 0, sizeof(struct Channel));
2195 strcpy(chptr->chname, chname);
2196 if (GlobalChannelList)
2197 GlobalChannelList->prev = chptr;
2199 chptr->next = GlobalChannelList;
2200 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
2201 GlobalChannelList = chptr;
2207 void add_invite(struct Client *cptr, struct Channel *chptr)
2209 struct SLink *inv, **tmp;
2211 del_invite(cptr, chptr);
2213 * Delete last link in chain if the list is max length
2215 assert(list_length(cptr->user->invited) == cptr->user->invites);
2216 if (cptr->user->invites>=MAXCHANNELSPERUSER)
2217 del_invite(cptr, cptr->user->invited->value.chptr);
2219 * Add client to channel invite list
2222 inv->value.cptr = cptr;
2223 inv->next = chptr->invites;
2224 chptr->invites = inv;
2226 * Add channel to the end of the client invite list
2228 for (tmp = &(cptr->user->invited); *tmp; tmp = &((*tmp)->next));
2230 inv->value.chptr = chptr;
2233 cptr->user->invites++;
2237 * Delete Invite block from channel invite list and client invite list
2239 void del_invite(struct Client *cptr, struct Channel *chptr)
2241 struct SLink **inv, *tmp;
2243 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
2244 if (tmp->value.cptr == cptr)
2249 cptr->user->invites--;
2253 for (inv = &(cptr->user->invited); (tmp = *inv); inv = &tmp->next)
2254 if (tmp->value.chptr == chptr)
2263 /* List and skip all channels that are listen */
2264 void list_next_channels(struct Client *cptr, int nr)
2266 struct ListingArgs *args = cptr->listing;
2267 struct Channel *chptr = args->chptr;
2268 chptr->mode.mode &= ~MODE_LISTED;
2269 while (is_listed(chptr) || --nr >= 0)
2271 for (; chptr; chptr = chptr->next)
2273 if (!cptr->user || (SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
2275 if (chptr->users > args->min_users && chptr->users < args->max_users &&
2276 chptr->creationtime > args->min_time &&
2277 chptr->creationtime < args->max_time &&
2278 (!args->topic_limits || (*chptr->topic &&
2279 chptr->topic_time > args->min_topic_time &&
2280 chptr->topic_time < args->max_topic_time)))
2282 if (ShowChannel(cptr,chptr))
2283 sendto_one(cptr, rpl_str(RPL_LIST), me.name, cptr->name,
2285 chptr->users, chptr->topic);
2286 chptr = chptr->next;
2292 MyFree(cptr->listing);
2293 cptr->listing = NULL;
2294 sendto_one(cptr, rpl_str(RPL_LISTEND), me.name, cptr->name);
2300 cptr->listing->chptr = chptr;
2301 chptr->mode.mode |= MODE_LISTED;
2306 void add_token_to_sendbuf(char *token, size_t *sblenp, int *firstp,
2307 int *send_itp, char is_a_ban, int mode)
2309 int first = *firstp;
2312 * Heh - we do not need to test if it still fits in the buffer, because
2313 * this BURST message is reconstructed from another BURST message, and
2314 * it only can become smaller. --Run
2317 if (*firstp) /* First token in this parameter ? */
2321 *send_itp = 1; /* Buffer contains data to be sent */
2322 sendbuf[(*sblenp)++] = ' ';
2325 sendbuf[(*sblenp)++] = ':'; /* Bans are always the last "parv" */
2326 sendbuf[(*sblenp)++] = is_a_ban;
2329 else /* Of course, 'send_it' is already set here */
2330 /* Seperate banmasks with a space because
2331 they can contain commas themselfs: */
2332 sendbuf[(*sblenp)++] = is_a_ban ? ' ' : ',';
2333 strcpy(sendbuf + *sblenp, token);
2334 *sblenp += strlen(token);
2335 if (!is_a_ban) /* nick list ? Need to take care
2336 of modes for nicks: */
2338 static int last_mode = 0;
2339 mode &= CHFL_CHANOP | CHFL_VOICE;
2342 if (last_mode != mode) /* Append mode like ':ov' if changed */
2345 sendbuf[(*sblenp)++] = ':';
2346 if (mode & CHFL_CHANOP)
2347 sendbuf[(*sblenp)++] = 'o';
2348 if (mode & CHFL_VOICE)
2349 sendbuf[(*sblenp)++] = 'v';
2351 sendbuf[*sblenp] = '\0';
2355 void cancel_mode(struct Client *sptr, struct Channel *chptr, char m,
2356 const char *param, int *count)
2361 int paramdoesntfit = 0;
2362 char parabuf[MODEBUFLEN];
2368 if (*count == -1) /* initialize ? */
2371 sprintf_irc(sendbuf, ":%s MODE %s -", sptr->name, chptr->chname);
2375 /* m == 0 means flush */
2380 size_t nplen = strlen(param);
2381 if (pb - parabuf + nplen + 23 > MODEBUFLEN)
2395 else if (*count == 0)
2397 if (*count == 6 || !m || paramdoesntfit)
2399 struct Membership* member;
2400 strcpy(sbp, parabuf);
2401 for (member = chptr->members; member; member = member->next_member)
2402 if (MyUser(member->user))
2403 sendbufto_one(member->user);
2413 pb += strlen(param);
2426 * X --a--> A --b--> B --d--> D
2430 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
2431 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
2433 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
2434 * Remove the user immedeately when no users are left on the channel.
2435 * b) On server B : remove the user (who/lp) from the channel, send a
2436 * PART upstream (to A) and pass on the KICK.
2437 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
2438 * channel, and pass on the KICK.
2439 * d) On server D : remove the user (who/lp) from the channel, and pass on
2443 * - Setting the ZOMBIE flag never hurts, we either remove the
2444 * client after that or we don't.
2445 * - The KICK message was already passed on, as should be in all cases.
2446 * - `who' is removed in all cases except case a) when users are left.
2447 * - A PART is only sent upstream in case b).
2453 * 1 --- 2 --- 3 --- 4 --- 5
2457 * We also need to turn 'who' into a zombie on servers 1 and 6,
2458 * because a KICK from 'who' (kicking someone else in that direction)
2459 * can arrive there afterwards - which should not be bounced itself.
2460 * Therefore case a) also applies for servers 1 and 6.
2464 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
2465 struct Client* sptr, struct Channel* chptr)
2467 assert(0 != member);
2472 /* Default for case a): */
2475 /* Case b) or c) ?: */
2476 if (MyUser(who)) /* server 4 */
2478 if (IsServer(cptr)) /* Case b) ? */
2479 sendto_one(cptr, PartFmt1, who->name, chptr->chname);
2480 remove_user_from_channel(who, chptr);
2483 if (who->from == cptr) /* True on servers 1, 5 and 6 */
2485 struct Client *acptr = IsServer(sptr) ? sptr : sptr->user->server;
2486 for (; acptr != &me; acptr = acptr->serv->up)
2487 if (acptr == who->user->server) /* Case d) (server 5) */
2489 remove_user_from_channel(who, chptr);
2494 /* Case a) (servers 1, 2, 3 and 6) */
2495 if (channel_all_zombies(chptr))
2496 remove_user_from_channel(who, chptr);
2498 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
2501 int number_of_zombies(struct Channel *chptr)
2503 struct Membership* member;
2507 for (member = chptr->members; member; member = member->next_member) {
2508 if (IsZombie(member))
2514 void send_user_joins(struct Client *cptr, struct Client *user)
2516 struct Membership* chan;
2517 struct Channel* chptr;
2525 strcpy(buf + 1, user->name);
2526 strcat(buf, " JOIN ");
2527 len = strlen(user->name) + 7;
2529 for (chan = user->user->channel; chan; chan = chan->next_channel)
2531 chptr = chan->channel;
2534 if ((mask = strchr(chptr->chname, ':')))
2535 if (match(++mask, cptr->name))
2537 if (*chptr->chname == '&')
2541 clen = strlen(chptr->chname);
2542 if (clen + 1 + len > BUFSIZE - 3)
2546 buf[len - 1] = '\0';
2547 sendto_one(cptr, "%s", buf);
2550 strcpy(buf + 1, user->name);
2551 strcat(buf, " JOIN ");
2552 len = strlen(user->name) + 7;
2555 strcpy(buf + len, chptr->chname);
2558 if (chan->next_channel)
2565 sendto_one(cptr, "%s", buf);
2569 * send_hack_notice()
2571 * parc & parv[] are the same as that of the calling function:
2572 * mtype == 1 is from m_mode, 2 is from m_create, 3 is from m_kick.
2574 * This function prepares sendbuf with the server notices and wallops
2575 * to be sent for all hacks. -Ghostwolf 18-May-97
2578 void send_hack_notice(struct Client *cptr, struct Client *sptr, int parc,
2579 char *parv[], int badop, int mtype)
2581 struct Channel *chptr;
2582 static char params[MODEBUFLEN];
2584 chptr = FindChannel(parv[1]);
2587 /* P10 servers require numeric nick conversion before sending. */
2590 case 1: /* Convert nicks for MODE HACKs here */
2592 char *mode = parv[2];
2595 while (*mode && *mode != 'o' && *mode != 'v')
2597 strcat(params, " ");
2598 if (*mode == 'o' || *mode == 'v')
2601 * blindly stumble through parameter list hoping one of them
2602 * might turn out to be a numeric nick
2603 * NOTE: this should not cause a problem but _may_ end up finding
2604 * something we aren't looking for. findNUser should be able to
2605 * handle any garbage that is thrown at it, but may return a client
2606 * if we happen to get lucky with a mode string or a timestamp
2608 struct Client *acptr;
2609 if ((acptr = findNUser(parv[i])) != NULL) /* Convert nicks here */
2610 strcat(params, acptr->name);
2613 strcat(params, "<");
2614 strcat(params, parv[i]);
2615 strcat(params, ">");
2618 else /* If it isn't a numnick, send it 'as is' */
2619 strcat(params, parv[i]);
2622 sprintf_irc(sendbuf,
2623 ":%s NOTICE * :*** Notice -- %sHACK(%d): %s MODE %s %s%s ["
2624 TIME_T_FMT "]", me.name, (badop == 3) ? "BOUNCE or " : "", badop,
2625 parv[0], parv[1], parv[2], params, chptr->creationtime);
2626 sendbufto_op_mask((badop == 3) ? SNO_HACK3 : (badop ==
2627 4) ? SNO_HACK4 : SNO_HACK2);
2629 if ((IsServer(sptr)) && (badop == 2))
2631 sprintf_irc(sendbuf, ":%s DESYNCH :HACK: %s MODE %s %s%s",
2632 me.name, parv[0], parv[1], parv[2], params);
2633 sendbufto_serv_butone(cptr);
2637 case 2: /* No conversion is needed for CREATE; the only numnick is sptr */
2639 sendto_serv_butone(cptr, ":%s DESYNCH :HACK: %s CREATE %s %s",
2640 me.name, sptr->name, chptr->chname, parv[2]);
2641 sendto_op_mask(SNO_HACK2, "HACK(2): %s CREATE %s %s",
2642 sptr->name, chptr->chname, parv[2]);
2645 case 3: /* Convert nick in KICK message */
2647 struct Client *acptr;
2648 if ((acptr = findNUser(parv[2])) != NULL) /* attempt to convert nick */
2649 sprintf_irc(sendbuf,
2650 ":%s NOTICE * :*** Notice -- HACK: %s KICK %s %s :%s",
2651 me.name, sptr->name, parv[1], acptr->name, parv[3]);
2652 else /* if conversion fails, send it 'as is' in <>'s */
2653 sprintf_irc(sendbuf,
2654 ":%s NOTICE * :*** Notice -- HACK: %s KICK %s <%s> :%s",
2655 me.name, sptr->name, parv[1], parv[2], parv[3]);
2656 sendbufto_op_mask(SNO_HACK4);
2663 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
2664 struct Client *connect, struct Channel *chan, unsigned int dest)
2669 assert(0 != source);
2675 mbuf->mb_source = source;
2676 mbuf->mb_connect = connect;
2677 mbuf->mb_channel = chan;
2678 mbuf->mb_dest = dest;
2681 for (i = 0; i < MAXMODEPARAMS; i++) {
2682 MB_TYPE(mbuf, i) = 0;
2683 MB_UINT(mbuf, i) = 0;
2688 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
2691 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2693 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
2694 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
2696 if (mode & MODE_ADD) {
2697 mbuf->mb_rem &= ~mode;
2698 mbuf->mb_add |= mode;
2700 mbuf->mb_add &= ~mode;
2701 mbuf->mb_rem |= mode;
2706 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
2709 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2711 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2712 MB_UINT(mbuf, mbuf->mb_count) = uint;
2714 if (++mbuf->mb_count >= 6)
2715 modebuf_flush(mbuf);
2719 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string)
2722 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2724 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2725 MB_STRING(mbuf, mbuf->mb_count) = string;
2727 if (++mbuf->mb_count >= 6)
2728 modebuf_flush(mbuf);
2732 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
2733 struct Client *client)
2736 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2738 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2739 MB_CLIENT(mbuf, mbuf->mb_count) = client;
2741 if (++mbuf->mb_count >= 6)
2742 modebuf_flush(mbuf);
2746 build_string(char *strptr, int *strptr_i, char *str)
2748 strptr[(*strptr_i)++] = ' ';
2750 while (strptr[(*strptr_i)++] = *str)
2751 ; /* very simple strcat */
2755 modebuf_flush(struct ModeBuf *mbuf)
2757 static int flags[] = {
2758 /* MODE_CHANOP, 'o', */
2759 /* MODE_VOICE, 'v', */
2762 MODE_MODERATED, 'm',
2763 MODE_TOPICLIMIT, 't',
2764 MODE_INVITEONLY, 'i',
2765 MODE_NOPRIVMSGS, 'n',
2766 /* MODE_KEY, 'k', */
2767 /* MODE_BAN, 'b', */
2768 /* MODE_LIMIT, 'l', */
2774 char addbuf[20] = "+";
2776 char rembuf[20] = "-";
2781 char addstr[MODEBUFLEN] = "";
2783 char remstr[MODEBUFLEN] = "";
2792 for (flag_p = flags; flag_p[0]; flag_p += 2) {
2793 if (*flag_p & mbuf->mb_add)
2794 addbuf[addbuf_i++] = flag_p[1];
2795 else if (*flag_p & mbuf->mb_rem)
2796 rembuf[rembuf_i++] = flag_p[1];
2804 for (i = 0; i < mbuf->mb_count; i++) {
2805 if (MB_TYPE(mbuf, i) & MODE_ADD) {
2807 bufptr_i = &addbuf_i;
2810 bufptr_i = &rembuf_i;
2813 if (MB_TYPE(mbuf, i) & MODE_CHANOP)
2814 bufptr[(*bufptr_i)++] = 'o';
2815 else if (MB_TYPE(mbuf, i) & MODE_VOICE)
2816 bufptr[(*bufptr_i)++] = 'v';
2817 else if (MB_TYPE(mbuf, i) & MODE_KEY)
2818 bufptr[(*bufptr_i)++] = 'k';
2819 else if (MB_TYPE(mbuf, i) & MODE_BAN)
2820 bufptr[(*bufptr_i)++] = 'b';
2821 else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
2822 bufptr[(*bufptr_i)++] = 'l';
2824 sprintf_irc(limitbuf, "%d", MB_UINT(mbuf, i));
2828 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL) {
2829 for (i = 0; i < mbuf->mb_count; i++) {
2830 if (MB_TYPE(mbuf, i) & MODE_ADD) {
2832 strptr_i = &addstr_i;
2835 strptr_i = &remstr_i;
2838 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
2839 build_string(strptr, strptr_i, MB_CLIENT(mbuf, i)->name);
2840 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
2841 build_string(strptr, strptr_i, MB_STRING(mbuf, i));
2842 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
2843 build_string(strptr, strptr_i, limitbuf);
2846 sendto_channel_butserv(mbuf->mb_channel, mbuf->mb_source,
2847 ":%s MODE %s %s%s%s%s", mbuf->mb_source->name,
2848 mbuf->mb_channel->name, addbuf, rembuf, addstr,
2854 for (i = 0; i < MAXMODEPARAMS; i++) {
2855 MB_TYPE(mbuf, i) = 0;
2856 MB_UINT(mbuf, i) = 0;