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_reply.h"
29 #include "ircd_string.h"
35 #include "querycmds.h"
42 #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 = TStime();
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;
2104 * Now a banned user CAN join if invited -- Nemesi
2105 * Now a user CAN escape channel limit if invited -- bfriendly
2106 * Now a user CAN escape anything if invited -- Isomer
2109 for (lp = sptr->user->invited; lp; lp = lp->next)
2110 if (lp->value.chptr == chptr)
2113 #ifdef OPER_WALK_THROUGH_LMODES
2114 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
2115 a HACK(4) notice will be sent if he would not have been supposed
2116 to join normally. */
2117 if (IsOperOnLocalChannel(sptr,chptr->chname) && !BadPtr(key) && compall("OVERRIDE",key) == 0)
2119 overrideJoin = MAGIC_OPER_OVERRIDE;
2123 if (chptr->mode.mode & MODE_INVITEONLY)
2124 return overrideJoin + ERR_INVITEONLYCHAN;
2126 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
2127 return overrideJoin + ERR_CHANNELISFULL;
2129 if (is_banned(sptr, chptr, NULL))
2130 return overrideJoin + ERR_BANNEDFROMCHAN;
2133 * now using compall (above) to test against a whole key ring -Kev
2135 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
2136 return overrideJoin + ERR_BADCHANNELKEY;
2139 return ERR_DONTCHEAT;
2145 * Remove bells and commas from channel name
2147 void clean_channelname(char *cn)
2150 if (!IsChannelChar(*cn)) {
2154 if (IsChannelLower(*cn)) {
2161 if ((unsigned char)(*cn) == 0xd0)
2169 * Get Channel block for i (and allocate a new channel
2170 * block, if it didn't exists before).
2172 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
2174 struct Channel *chptr;
2177 if (EmptyString(chname))
2180 len = strlen(chname);
2181 if (MyUser(cptr) && len > CHANNELLEN)
2184 *(chname + CHANNELLEN) = '\0';
2186 if ((chptr = FindChannel(chname)))
2188 if (flag == CGT_CREATE)
2190 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
2192 ++UserStats.channels;
2193 memset(chptr, 0, sizeof(struct Channel));
2194 strcpy(chptr->chname, chname);
2195 if (GlobalChannelList)
2196 GlobalChannelList->prev = chptr;
2198 chptr->next = GlobalChannelList;
2199 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
2200 GlobalChannelList = chptr;
2206 void add_invite(struct Client *cptr, struct Channel *chptr)
2208 struct SLink *inv, **tmp;
2210 del_invite(cptr, chptr);
2212 * Delete last link in chain if the list is max length
2214 assert(list_length(cptr->user->invited) == cptr->user->invites);
2215 if (cptr->user->invites>=MAXCHANNELSPERUSER)
2216 del_invite(cptr, cptr->user->invited->value.chptr);
2218 * Add client to channel invite list
2221 inv->value.cptr = cptr;
2222 inv->next = chptr->invites;
2223 chptr->invites = inv;
2225 * Add channel to the end of the client invite list
2227 for (tmp = &(cptr->user->invited); *tmp; tmp = &((*tmp)->next));
2229 inv->value.chptr = chptr;
2232 cptr->user->invites++;
2236 * Delete Invite block from channel invite list and client invite list
2238 void del_invite(struct Client *cptr, struct Channel *chptr)
2240 struct SLink **inv, *tmp;
2242 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
2243 if (tmp->value.cptr == cptr)
2248 cptr->user->invites--;
2252 for (inv = &(cptr->user->invited); (tmp = *inv); inv = &tmp->next)
2253 if (tmp->value.chptr == chptr)
2262 /* List and skip all channels that are listen */
2263 void list_next_channels(struct Client *cptr, int nr)
2265 struct ListingArgs *args = cptr->listing;
2266 struct Channel *chptr = args->chptr;
2267 chptr->mode.mode &= ~MODE_LISTED;
2268 while (is_listed(chptr) || --nr >= 0)
2270 for (; chptr; chptr = chptr->next)
2272 if (!cptr->user || (SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
2274 if (chptr->users > args->min_users && chptr->users < args->max_users &&
2275 chptr->creationtime > args->min_time &&
2276 chptr->creationtime < args->max_time &&
2277 (!args->topic_limits || (*chptr->topic &&
2278 chptr->topic_time > args->min_topic_time &&
2279 chptr->topic_time < args->max_topic_time)))
2281 if (ShowChannel(cptr,chptr))
2282 sendto_one(cptr, rpl_str(RPL_LIST), me.name, cptr->name,
2284 chptr->users, chptr->topic);
2285 chptr = chptr->next;
2291 MyFree(cptr->listing);
2292 cptr->listing = NULL;
2293 sendto_one(cptr, rpl_str(RPL_LISTEND), me.name, cptr->name);
2299 cptr->listing->chptr = chptr;
2300 chptr->mode.mode |= MODE_LISTED;
2305 void add_token_to_sendbuf(char *token, size_t *sblenp, int *firstp,
2306 int *send_itp, char is_a_ban, int mode)
2308 int first = *firstp;
2311 * Heh - we do not need to test if it still fits in the buffer, because
2312 * this BURST message is reconstructed from another BURST message, and
2313 * it only can become smaller. --Run
2316 if (*firstp) /* First token in this parameter ? */
2320 *send_itp = 1; /* Buffer contains data to be sent */
2321 sendbuf[(*sblenp)++] = ' ';
2324 sendbuf[(*sblenp)++] = ':'; /* Bans are always the last "parv" */
2325 sendbuf[(*sblenp)++] = is_a_ban;
2328 else /* Of course, 'send_it' is already set here */
2329 /* Seperate banmasks with a space because
2330 they can contain commas themselfs: */
2331 sendbuf[(*sblenp)++] = is_a_ban ? ' ' : ',';
2332 strcpy(sendbuf + *sblenp, token);
2333 *sblenp += strlen(token);
2334 if (!is_a_ban) /* nick list ? Need to take care
2335 of modes for nicks: */
2337 static int last_mode = 0;
2338 mode &= CHFL_CHANOP | CHFL_VOICE;
2341 if (last_mode != mode) /* Append mode like ':ov' if changed */
2344 sendbuf[(*sblenp)++] = ':';
2345 if (mode & CHFL_CHANOP)
2346 sendbuf[(*sblenp)++] = 'o';
2347 if (mode & CHFL_VOICE)
2348 sendbuf[(*sblenp)++] = 'v';
2350 sendbuf[*sblenp] = '\0';
2354 void cancel_mode(struct Client *sptr, struct Channel *chptr, char m,
2355 const char *param, int *count)
2360 int paramdoesntfit = 0;
2361 char parabuf[MODEBUFLEN];
2367 if (*count == -1) /* initialize ? */
2370 sprintf_irc(sendbuf, ":%s MODE %s -", sptr->name, chptr->chname);
2374 /* m == 0 means flush */
2379 size_t nplen = strlen(param);
2380 if (pb - parabuf + nplen + 23 > MODEBUFLEN)
2394 else if (*count == 0)
2396 if (*count == 6 || !m || paramdoesntfit)
2398 struct Membership* member;
2399 strcpy(sbp, parabuf);
2400 for (member = chptr->members; member; member = member->next_member)
2401 if (MyUser(member->user))
2402 sendbufto_one(member->user);
2412 pb += strlen(param);
2425 * X --a--> A --b--> B --d--> D
2429 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
2430 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
2432 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
2433 * Remove the user immedeately when no users are left on the channel.
2434 * b) On server B : remove the user (who/lp) from the channel, send a
2435 * PART upstream (to A) and pass on the KICK.
2436 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
2437 * channel, and pass on the KICK.
2438 * d) On server D : remove the user (who/lp) from the channel, and pass on
2442 * - Setting the ZOMBIE flag never hurts, we either remove the
2443 * client after that or we don't.
2444 * - The KICK message was already passed on, as should be in all cases.
2445 * - `who' is removed in all cases except case a) when users are left.
2446 * - A PART is only sent upstream in case b).
2452 * 1 --- 2 --- 3 --- 4 --- 5
2456 * We also need to turn 'who' into a zombie on servers 1 and 6,
2457 * because a KICK from 'who' (kicking someone else in that direction)
2458 * can arrive there afterwards - which should not be bounced itself.
2459 * Therefore case a) also applies for servers 1 and 6.
2463 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
2464 struct Client* sptr, struct Channel* chptr)
2466 assert(0 != member);
2471 /* Default for case a): */
2474 /* Case b) or c) ?: */
2475 if (MyUser(who)) /* server 4 */
2477 if (IsServer(cptr)) /* Case b) ? */
2478 sendto_one(cptr, PartFmt1, who->name, chptr->chname);
2479 remove_user_from_channel(who, chptr);
2482 if (who->from == cptr) /* True on servers 1, 5 and 6 */
2484 struct Client *acptr = IsServer(sptr) ? sptr : sptr->user->server;
2485 for (; acptr != &me; acptr = acptr->serv->up)
2486 if (acptr == who->user->server) /* Case d) (server 5) */
2488 remove_user_from_channel(who, chptr);
2493 /* Case a) (servers 1, 2, 3 and 6) */
2494 if (channel_all_zombies(chptr))
2495 remove_user_from_channel(who, chptr);
2497 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
2500 int number_of_zombies(struct Channel *chptr)
2502 struct Membership* member;
2506 for (member = chptr->members; member; member = member->next_member) {
2507 if (IsZombie(member))
2513 void send_user_joins(struct Client *cptr, struct Client *user)
2515 struct Membership* chan;
2516 struct Channel* chptr;
2524 strcpy(buf + 1, user->name);
2525 strcat(buf, " JOIN ");
2526 len = strlen(user->name) + 7;
2528 for (chan = user->user->channel; chan; chan = chan->next_channel)
2530 chptr = chan->channel;
2533 if ((mask = strchr(chptr->chname, ':')))
2534 if (match(++mask, cptr->name))
2536 if (*chptr->chname == '&')
2540 clen = strlen(chptr->chname);
2541 if (clen + 1 + len > BUFSIZE - 3)
2545 buf[len - 1] = '\0';
2546 sendto_one(cptr, "%s", buf);
2549 strcpy(buf + 1, user->name);
2550 strcat(buf, " JOIN ");
2551 len = strlen(user->name) + 7;
2554 strcpy(buf + len, chptr->chname);
2557 if (chan->next_channel)
2564 sendto_one(cptr, "%s", buf);
2568 * send_hack_notice()
2570 * parc & parv[] are the same as that of the calling function:
2571 * mtype == 1 is from m_mode, 2 is from m_create, 3 is from m_kick.
2573 * This function prepares sendbuf with the server notices and wallops
2574 * to be sent for all hacks. -Ghostwolf 18-May-97
2577 void send_hack_notice(struct Client *cptr, struct Client *sptr, int parc,
2578 char *parv[], int badop, int mtype)
2580 struct Channel *chptr;
2581 static char params[MODEBUFLEN];
2583 chptr = FindChannel(parv[1]);
2586 /* P10 servers require numeric nick conversion before sending. */
2589 case 1: /* Convert nicks for MODE HACKs here */
2591 char *mode = parv[2];
2594 while (*mode && *mode != 'o' && *mode != 'v')
2596 strcat(params, " ");
2597 if (*mode == 'o' || *mode == 'v')
2600 * blindly stumble through parameter list hoping one of them
2601 * might turn out to be a numeric nick
2602 * NOTE: this should not cause a problem but _may_ end up finding
2603 * something we aren't looking for. findNUser should be able to
2604 * handle any garbage that is thrown at it, but may return a client
2605 * if we happen to get lucky with a mode string or a timestamp
2607 struct Client *acptr;
2608 if ((acptr = findNUser(parv[i])) != NULL) /* Convert nicks here */
2609 strcat(params, acptr->name);
2612 strcat(params, "<");
2613 strcat(params, parv[i]);
2614 strcat(params, ">");
2617 else /* If it isn't a numnick, send it 'as is' */
2618 strcat(params, parv[i]);
2621 sprintf_irc(sendbuf,
2622 ":%s NOTICE * :*** Notice -- %sHACK(%d): %s MODE %s %s%s ["
2623 TIME_T_FMT "]", me.name, (badop == 3) ? "BOUNCE or " : "", badop,
2624 parv[0], parv[1], parv[2], params, chptr->creationtime);
2625 sendbufto_op_mask((badop == 3) ? SNO_HACK3 : (badop ==
2626 4) ? SNO_HACK4 : SNO_HACK2);
2628 if ((IsServer(sptr)) && (badop == 2))
2630 sprintf_irc(sendbuf, ":%s DESYNCH :HACK: %s MODE %s %s%s",
2631 me.name, parv[0], parv[1], parv[2], params);
2632 sendbufto_serv_butone(cptr);
2636 case 2: /* No conversion is needed for CREATE; the only numnick is sptr */
2638 sendto_serv_butone(cptr, ":%s DESYNCH :HACK: %s CREATE %s %s",
2639 me.name, sptr->name, chptr->chname, parv[2]);
2640 sendto_op_mask(SNO_HACK2, "HACK(2): %s CREATE %s %s",
2641 sptr->name, chptr->chname, parv[2]);
2644 case 3: /* Convert nick in KICK message */
2646 struct Client *acptr;
2647 if ((acptr = findNUser(parv[2])) != NULL) /* attempt to convert nick */
2648 sprintf_irc(sendbuf,
2649 ":%s NOTICE * :*** Notice -- HACK: %s KICK %s %s :%s",
2650 me.name, sptr->name, parv[1], acptr->name, parv[3]);
2651 else /* if conversion fails, send it 'as is' in <>'s */
2652 sprintf_irc(sendbuf,
2653 ":%s NOTICE * :*** Notice -- HACK: %s KICK %s <%s> :%s",
2654 me.name, sptr->name, parv[1], parv[2], parv[3]);
2655 sendbufto_op_mask(SNO_HACK4);
2662 * This helper function builds an argument string in strptr, consisting
2663 * of the original string, a space, and str1 and str2 concatenated (if,
2664 * of course, str2 is not NULL)
2667 build_string(char *strptr, int *strptr_i, char *str1, char *str2)
2669 strptr[(*strptr_i)++] = ' ';
2672 strptr[(*strptr_i)++] = *(str1++);
2676 strptr[(*strptr_i)++] = *(str2++);
2678 strptr[(*strptr_i)] = '\0';
2682 * This is the workhorse of our ModeBuf suite; this actually generates the
2683 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
2686 modebuf_flush_int(struct ModeBuf *mbuf, int all)
2688 /* we only need the flags that don't take args right now */
2689 static int flags[] = {
2690 /* MODE_CHANOP, 'o', */
2691 /* MODE_VOICE, 'v', */
2694 MODE_MODERATED, 'm',
2695 MODE_TOPICLIMIT, 't',
2696 MODE_INVITEONLY, 'i',
2697 MODE_NOPRIVMSGS, 'n',
2698 /* MODE_KEY, 'k', */
2699 /* MODE_BAN, 'b', */
2700 /* MODE_LIMIT, 'l', */
2706 struct Client *app_source; /* where the MODE appears to come from */
2708 char addbuf[20]; /* accumulates +psmtin, etc. */
2710 char rembuf[20]; /* accumulates -psmtin, etc. */
2712 char *bufptr; /* we make use of indirection to simplify the code */
2715 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
2717 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
2719 char *strptr; /* more indirection to simplify the code */
2722 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
2725 char limitbuf[20]; /* convert limits to strings */
2727 unsigned int limitdel = MODE_LIMIT;
2731 /* If the ModeBuf is empty, we have nothing to do */
2732 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
2735 /* Ok, if we were given the OPMODE flag, hide the source if its a user */
2736 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
2737 app_source = mbuf->mb_source->user->server;
2739 app_source = mbuf->mb_source;
2742 * Account for user we're bouncing; we have to get it in on the first
2743 * bounced MODE, or we could have problems
2745 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
2746 totalbuflen -= 6; /* numeric nick == 5, plus one space */
2748 /* Calculate the simple flags */
2749 for (flag_p = flags; flag_p[0]; flag_p += 2) {
2750 if (*flag_p & mbuf->mb_add)
2751 addbuf[addbuf_i++] = flag_p[1];
2752 else if (*flag_p & mbuf->mb_rem)
2753 rembuf[rembuf_i++] = flag_p[1];
2756 /* Now go through the modes with arguments... */
2757 for (i = 0; i < mbuf->mb_count; i++) {
2758 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
2760 bufptr_i = &addbuf_i;
2763 bufptr_i = &rembuf_i;
2766 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
2767 tmp = strlen(MB_CLIENT(mbuf, i)->name);
2769 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
2770 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
2772 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
2773 totalbuflen -= IRCD_MAX(5, tmp) + 1;
2775 } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN)) {
2776 tmp = strlen(MB_STRING(mbuf, i));
2778 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
2779 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
2781 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_KEY ? 'k' : 'b';
2782 totalbuflen -= tmp + 1;
2784 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
2785 /* if it's a limit, we also format the number */
2786 sprintf_irc(limitbuf, "%d", MB_UINT(mbuf, i));
2788 tmp = strlen(limitbuf);
2790 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
2791 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
2793 bufptr[(*bufptr_i)++] = 'l';
2794 totalbuflen -= tmp + 1;
2799 /* terminate the mode strings */
2800 addbuf[addbuf_i] = '\0';
2801 rembuf[rembuf_i] = '\0';
2803 /* If we're building a user visible MODE or HACK... */
2804 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
2805 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
2806 MODEBUF_DEST_LOG)) {
2807 /* Set up the parameter strings */
2813 for (i = 0; i < mbuf->mb_count; i++) {
2814 if (MB_TYPE(mbuf, i) & MODE_SAVE)
2817 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
2819 strptr_i = &addstr_i;
2822 strptr_i = &remstr_i;
2825 /* deal with clients... */
2826 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
2827 build_string(strptr, strptr_i, MB_CLIENT(mbuf, i)->name, 0);
2829 /* deal with strings... */
2830 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
2831 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0);
2834 * deal with limit; note we cannot include the limit parameter if we're
2837 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
2838 (MODE_ADD | MODE_LIMIT))
2839 build_string(strptr, strptr_i, limitbuf, 0);
2842 /* send the messages off to their destination */
2843 if (mbuf->mb_dest & MODEBUF_DEST_HACK2) {
2844 sendto_op_mask(SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s [" TIME_T_FMT
2845 "]", app_source->name, mbuf->mb_channel->chname,
2846 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", addbuf,
2847 remstr, addstr, mbuf->mb_channel->creationtime);
2848 sendto_serv_butone(mbuf->mb_connect, "%s " TOK_DESYNCH
2849 " :HACK: %s MODE %s %s%s%s%s%s%s [" TIME_T_FMT "]",
2850 NumServ(&me), app_source->name,
2851 mbuf->mb_channel->chname, rembuf_i ? "-" : "", rembuf,
2852 addbuf_i ? "+" : "", addbuf, remstr, addstr,
2853 mbuf->mb_channel->creationtime);
2856 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
2857 sendto_op_mask(SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s %s%s%s%s%s%s ["
2858 TIME_T_FMT "]", app_source->name,
2859 mbuf->mb_channel->chname, rembuf_i ? "-" : "", rembuf,
2860 addbuf_i ? "+" : "", addbuf, remstr, addstr,
2861 mbuf->mb_channel->creationtime);
2863 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
2864 sendto_op_mask(SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s [" TIME_T_FMT
2865 "]", app_source->name, mbuf->mb_channel->chname,
2866 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", addbuf,
2867 remstr, addstr, mbuf->mb_channel->creationtime);
2870 if (mbuf->mb_dest & MODEBUF_DEST_LOG) {
2871 if (IsServer(mbuf->mb_source))
2872 write_log(OPATH, TIME_T_FMT " %s OPMODE %s %s%s%s%s%s%s\n", TStime(),
2873 mbuf->mb_source->name, mbuf->mb_channel->chname,
2874 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", addbuf,
2877 write_log(OPATH, TIME_T_FMT " %s!%s@%s OPMODE %s %s%s%s%s%s%s\n",
2878 TStime(), mbuf->mb_source->name,
2879 mbuf->mb_source->user->username, mbuf->mb_source->user->host,
2880 mbuf->mb_channel->chname, rembuf_i ? "-" : "", rembuf,
2881 addbuf_i ? "+" : "", addbuf, remstr, addstr);
2885 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
2886 sendto_channel_butserv(mbuf->mb_channel, app_source,
2887 ":%s MODE %s %s%s%s%s%s%s", app_source->name,
2888 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2889 rembuf, addbuf_i ? "+" : "", addbuf, remstr,
2893 /* Now are we supposed to propagate to other servers? */
2894 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
2895 /* set up parameter string */
2902 * limit is supressed if we're removing it; we have to figure out which
2903 * direction is the direction for it to be removed, though...
2905 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
2907 for (i = 0; i < mbuf->mb_count; i++) {
2908 if (MB_TYPE(mbuf, i) & MODE_SAVE)
2911 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
2913 strptr_i = &addstr_i;
2916 strptr_i = &remstr_i;
2919 /* deal with modes that take clients */
2920 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
2921 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)));
2923 /* deal with modes that take strings */
2924 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
2925 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0);
2928 * deal with the limit. Logic here is complicated; if HACK2 is set,
2929 * we're bouncing the mode, so sense is reversed, and we have to
2930 * include the original limit if it looks like it's being removed
2932 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
2933 build_string(strptr, strptr_i, limitbuf, 0);
2936 /* we were told to deop the source */
2937 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
2938 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
2939 addbuf[addbuf_i] = '\0'; /* terminate the string... */
2940 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source)); /* add user */
2942 /* mark that we've done this, so we don't do it again */
2943 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
2946 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
2947 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
2948 if (IsServer(mbuf->mb_source))
2949 sendto_serv_butone(mbuf->mb_connect, "%s " TOK_OPMODE
2950 " %s %s%s%s%s%s%s", NumServ(mbuf->mb_source),
2951 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2952 rembuf, addbuf_i ? "+" : "", addbuf, remstr,
2955 sendto_serv_butone(mbuf->mb_connect, "%s%s " TOK_OPMODE
2956 " %s %s%s%s%s%s%s", NumNick(mbuf->mb_source),
2957 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2958 rembuf, addbuf_i ? "+" : "", addbuf, remstr,
2960 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
2962 * If HACK2 was set, we're bouncing; we send the MODE back to the
2963 * connection we got it from with the senses reversed and a TS of 0;
2966 sendto_one(mbuf->mb_connect, "%s " TOK_MODE " %s %s%s%s%s%s%s "
2967 TIME_T_FMT, NumServ(&me), mbuf->mb_channel->chname,
2968 addbuf_i ? "-" : "", addbuf, rembuf_i ? "+" : "", rembuf,
2969 addstr, remstr, mbuf->mb_channel->creationtime);
2972 * We're propagating a normal MODE command to the rest of the network;
2973 * we send the actual channel TS unless this is a HACK3 or a HACK4
2975 if (IsServer(mbuf->mb_source))
2976 sendto_serv_butone(mbuf->mb_connect, "%s " TOK_MODE " %s %s%s%s%s%s%s "
2977 TIME_T_FMT, NumServ(mbuf->mb_source),
2978 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2979 rembuf, addbuf_i ? "+" : "", addbuf, remstr,
2980 addstr, (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
2981 mbuf->mb_channel->creationtime);
2983 sendto_serv_butone(mbuf->mb_connect, "%s%s " TOK_MODE
2984 " %s %s%s%s%s%s%s", NumNick(mbuf->mb_source),
2985 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2986 rembuf, addbuf_i ? "+" : "", addbuf, remstr,
2991 /* We've drained the ModeBuf... */
2996 /* reinitialize the mode-with-arg slots */
2997 for (i = 0; i < MAXMODEPARAMS; i++) {
2998 /* If we saved any, pack them down */
2999 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
3000 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
3001 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
3003 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
3005 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
3006 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
3008 MB_TYPE(mbuf, i) = 0;
3009 MB_UINT(mbuf, i) = 0;
3012 /* If we're supposed to flush it all, do so--all hail tail recursion */
3013 if (all && mbuf->mb_count)
3014 return modebuf_flush_int(mbuf, 1);
3020 * This routine just initializes a ModeBuf structure with the information
3021 * needed and the options given.
3024 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
3025 struct Client *connect, struct Channel *chan, unsigned int dest)
3030 assert(0 != source);
3036 mbuf->mb_source = source;
3037 mbuf->mb_connect = connect;
3038 mbuf->mb_channel = chan;
3039 mbuf->mb_dest = dest;
3042 /* clear each mode-with-parameter slot */
3043 for (i = 0; i < MAXMODEPARAMS; i++) {
3044 MB_TYPE(mbuf, i) = 0;
3045 MB_UINT(mbuf, i) = 0;
3050 * This routine simply adds modes to be added or deleted; do a binary OR
3051 * with either MODE_ADD or MODE_DEL
3054 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
3057 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3059 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
3060 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
3062 if (mode & MODE_ADD) {
3063 mbuf->mb_rem &= ~mode;
3064 mbuf->mb_add |= mode;
3066 mbuf->mb_add &= ~mode;
3067 mbuf->mb_rem |= mode;
3072 * This routine adds a mode to be added or deleted that takes a unsigned
3073 * int parameter; mode may *only* be the relevant mode flag ORed with one
3074 * of MODE_ADD or MODE_DEL
3077 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
3080 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3082 MB_TYPE(mbuf, mbuf->mb_count) = mode;
3083 MB_UINT(mbuf, mbuf->mb_count) = uint;
3085 /* when we've reached the maximal count, flush the buffer */
3086 if (++mbuf->mb_count >=
3087 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
3088 modebuf_flush_int(mbuf, 0);
3092 * This routine adds a mode to be added or deleted that takes a string
3093 * parameter; mode may *only* be the relevant mode flag ORed with one of
3094 * MODE_ADD or MODE_DEL
3097 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
3101 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3103 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
3104 MB_STRING(mbuf, mbuf->mb_count) = string;
3106 /* when we've reached the maximal count, flush the buffer */
3107 if (++mbuf->mb_count >=
3108 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
3109 modebuf_flush_int(mbuf, 0);
3113 * This routine adds a mode to be added or deleted that takes a client
3114 * parameter; mode may *only* be the relevant mode flag ORed with one of
3115 * MODE_ADD or MODE_DEL
3118 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
3119 struct Client *client)
3122 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3124 MB_TYPE(mbuf, mbuf->mb_count) = mode;
3125 MB_CLIENT(mbuf, mbuf->mb_count) = client;
3127 /* when we've reached the maximal count, flush the buffer */
3128 if (++mbuf->mb_count >=
3129 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
3130 modebuf_flush_int(mbuf, 0);
3134 * This is the exported binding for modebuf_flush()
3137 modebuf_flush(struct ModeBuf *mbuf)
3139 return modebuf_flush_int(mbuf, 1);
3143 * Simple function to invalidate bans
3146 mode_ban_invalidate(struct Channel *chan)
3148 struct Membership *member;
3150 for (member = chan->members; member; member = member->next_member)
3151 ClearBanValid(member);
3155 * Simple function to drop invite structures
3158 mode_invite_clear(struct Channel *chan)
3160 while (chan->invites)
3161 del_invite(chan->invites->value.cptr, chan);
3164 /* What we've done for mode_parse so far... */
3165 #define DONE_LIMIT 0x01 /* We've set the limit */
3166 #define DONE_KEY 0x02 /* We've set the key */
3167 #define DONE_BANLIST 0x04 /* We've sent the ban list */
3168 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
3169 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
3172 struct ModeBuf *mbuf;
3173 struct Client *cptr;
3174 struct Client *sptr;
3175 struct Channel *chptr;
3184 struct SLink banlist[MAXPARA];
3187 struct Client *client;
3188 } cli_change[MAXPARA];
3192 * Here's a helper function to deal with sending along "Not oper" or
3193 * "Not member" messages
3196 send_notoper(struct ParseState *state)
3198 if (state->done & DONE_NOTOPER)
3201 sendto_one(state->sptr, err_str(state->flags & MODE_PARSE_NOTOPER ?
3202 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL),
3203 me.name, state->sptr->name, state->chptr->chname);
3205 state->done |= DONE_NOTOPER;
3209 * Helper function to convert limits
3212 mode_parse_limit(struct ParseState *state, int *flag_p)
3214 unsigned int t_limit;
3216 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
3217 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
3220 if (state->parc <= 0) { /* warn if not enough args */
3221 if (MyUser(state->sptr))
3222 need_more_params(state->sptr, "MODE +l");
3226 t_limit = atoi(state->parv[state->args_used++]); /* grab arg */
3230 if (!t_limit) /* if it was zero, ignore it */
3233 t_limit = state->chptr->mode.limit;
3235 /* If they're not an oper, they can't change modes */
3236 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3237 send_notoper(state);
3241 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
3243 state->done |= DONE_LIMIT;
3245 assert(0 != state->mbuf);
3247 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
3249 if (state->flags & MODE_PARSE_SET) { /* set the limit */
3250 if (state->dir & MODE_ADD) {
3251 state->chptr->mode.mode |= flag_p[0];
3252 state->chptr->mode.limit = t_limit;
3254 state->chptr->mode.mode &= flag_p[0];
3255 state->chptr->mode.limit = 0;
3261 * Helper function to convert keys
3264 mode_parse_key(struct ParseState *state, int *flag_p)
3269 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3272 if (state->parc <= 0) { /* warn if not enough args */
3273 if (MyUser(state->sptr))
3274 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
3279 t_str = state->parv[state->args_used++]; /* grab arg */
3283 /* If they're not an oper, they can't change modes */
3284 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3285 send_notoper(state);
3289 if (state->done & DONE_KEY) /* allow key to be set only once */
3291 state->done |= DONE_KEY;
3295 /* clean up the key string */
3297 while (*++s > ' ' && *s != ':' && --t_len)
3301 if (!*t_str) { /* warn if empty */
3302 if (MyUser(state->sptr))
3303 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
3308 /* can't add a key if one is set, nor can one remove the wrong key */
3309 if (!(state->flags & MODE_PARSE_FORCE))
3310 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
3311 (state->dir == MODE_DEL &&
3312 ircd_strcmp(state->chptr->mode.key, t_str))) {
3313 sendto_one(state->sptr, err_str(ERR_KEYSET), me.name, state->sptr->name,
3314 state->chptr->chname);
3318 assert(0 != state->mbuf);
3320 if (state->flags & MODE_PARSE_BOUNCE) {
3321 if (*state->chptr->mode.key) /* reset old key */
3322 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
3323 state->chptr->mode.key, 0);
3324 else /* remove new bogus key */
3325 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
3326 } else /* send new key */
3327 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
3329 if (state->flags & MODE_PARSE_SET) {
3330 if (state->dir == MODE_ADD) /* set the new key */
3331 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
3332 else /* remove the old key */
3333 *state->chptr->mode.key = '\0';
3338 * Helper function to convert bans
3341 mode_parse_ban(struct ParseState *state, int *flag_p)
3344 struct SLink *ban, *newban = 0;
3346 if (state->parc <= 0) { /* Not enough args, send ban list */
3347 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
3348 send_ban_list(state->sptr, state->chptr);
3349 state->done |= DONE_BANLIST;
3355 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3358 t_str = state->parv[state->args_used++]; /* grab arg */
3362 /* If they're not an oper, they can't change modes */
3363 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3364 send_notoper(state);
3368 if ((s = strchr(t_str, ' ')))
3371 if (!*t_str || *t_str == ':') { /* warn if empty */
3372 if (MyUser(state->sptr))
3373 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
3378 t_str = collapse(pretty_mask(t_str));
3380 /* remember the ban for the moment... */
3381 if (state->dir == MODE_ADD) {
3382 newban = state->banlist + (state->numbans++);
3385 DupString(newban->value.ban.banstr, t_str);
3386 newban->value.ban.who = state->sptr->name;
3387 newban->value.ban.when = TStime();
3389 newban->flags = CHFL_BAN | MODE_ADD;
3391 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
3392 newban->flags |= CHFL_BAN_IPMASK;
3395 if (!state->chptr->banlist) {
3396 state->chptr->banlist = newban; /* add our ban with its flags */
3397 state->done |= DONE_BANCLEAN;
3401 /* Go through all bans */
3402 for (ban = state->chptr->banlist; ban; ban = ban->next) {
3403 /* first, clean the ban flags up a bit */
3404 if (!(state->done & DONE_BANCLEAN))
3405 /* Note: We're overloading *lots* of bits here; be careful! */
3406 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
3410 * MODE_ADD - Ban was added; if we're bouncing modes,
3411 * then we'll remove it below; otherwise,
3412 * we'll have to allocate a real ban
3414 * MODE_DEL - Ban was marked for deletion; if we're
3415 * bouncing modes, we'll have to re-add it,
3416 * otherwise, we'll have to remove it
3418 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
3419 * with a ban already set; if we're
3420 * bouncing modes, we'll have to bounce
3421 * this one; otherwise, we'll just ignore
3422 * it when we process added bans
3425 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
3426 ban->flags |= MODE_DEL; /* delete one ban */
3428 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
3430 } else if (state->dir == MODE_ADD) {
3431 /* if the ban already exists, don't worry about it */
3432 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
3433 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
3436 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
3437 if (!(ban->flags & MODE_DEL))
3438 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
3439 } else if (!mmatch(t_str, ban->value.ban.banstr))
3440 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
3443 ban->next = newban; /* add our ban with its flags */
3444 break; /* get out of loop */
3448 state->done |= DONE_BANCLEAN;
3452 * This is the bottom half of the ban processor
3455 mode_process_bans(struct ParseState *state)
3457 struct SLink *ban, *newban, *prevban, *nextban;
3463 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
3465 banlen = strlen(ban->value.ban.banstr);
3467 nextban = ban->next;
3469 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
3471 prevban->next = 0; /* Break the list; ban isn't a real ban */
3473 state->chptr->banlist = 0;
3478 MyFree(ban->value.ban.banstr);
3481 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
3482 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
3483 ban->value.ban.banstr,
3484 state->flags & MODE_PARSE_SET);
3486 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
3487 if (prevban) /* clip it out of the list... */
3488 prevban->next = ban->next;
3490 state->chptr->banlist = ban->next;
3495 MyFree(ban->value.ban.who);
3499 continue; /* next ban; keep prevban like it is */
3501 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
3502 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
3504 prevban->next = 0; /* Break the list; ban isn't a real ban */
3506 state->chptr->banlist = 0;
3508 /* If we're supposed to ignore it, do so. */
3509 if (ban->flags & CHFL_BAN_OVERLAPPED &&
3510 !(state->flags & MODE_PARSE_BOUNCE)) {
3514 MyFree(ban->value.ban.banstr);
3516 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
3517 (len > MAXBANLENGTH || count >= MAXBANS)) {
3518 send_error_to_client(state->sptr, ERR_BANLISTFULL,
3519 state->chptr->chname, ban->value.ban.banstr);
3523 MyFree(ban->value.ban.banstr);
3525 /* add the ban to the buffer */
3526 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
3527 ban->value.ban.banstr,
3528 !(state->flags & MODE_PARSE_SET));
3530 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
3531 newban = make_link();
3532 newban->value.ban.banstr = ban->value.ban.banstr;
3533 DupString(newban->value.ban.who, ban->value.ban.who);
3534 newban->value.ban.when = ban->value.ban.when;
3535 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
3537 newban->next = state->chptr->banlist; /* and link it in */
3538 state->chptr->banlist = newban;
3547 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
3549 if (changed) /* if we changed the ban list, we must invalidate the bans */
3550 mode_ban_invalidate(state->chptr);
3554 * Helper function to process client changes
3557 mode_parse_client(struct ParseState *state, int *flag_p)
3560 struct Client *acptr;
3563 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3566 if (state->parc <= 0) { /* warn if not enough args */
3567 if (MyUser(state->sptr))
3568 need_more_params(state->sptr, state->dir == MODE_ADD ?
3569 (flag_p[0] == MODE_CHANOP ? "MODE +o" : "MODE +v") :
3570 (flag_p[0] == MODE_CHANOP ? "MODE -o" : "MODE -v"));
3574 t_str = state->parv[state->args_used++]; /* grab arg */
3578 /* If they're not an oper, they can't change modes */
3579 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3580 send_notoper(state);
3584 if (MyUser(state->sptr)) /* find client we're manipulating */
3585 acptr = find_chasing(state->sptr, t_str, NULL);
3587 acptr = findNUser(t_str);
3590 return; /* find_chasing() already reported an error to the user */
3592 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
3593 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
3594 state->cli_change[i].flag & flag_p[0]))
3595 break; /* found a slot */
3597 /* Store what we're doing to them */
3598 state->cli_change[i].flag = state->dir | flag_p[0];
3599 state->cli_change[i].client = acptr;
3603 * Helper function to process the changed client list
3606 mode_process_clients(struct ParseState *state)
3609 struct Membership *member;
3611 for (i = 0; state->cli_change[i].flag; i++) {
3612 assert(0 != state->cli_change[i].client);
3614 /* look up member link */
3615 if (!(member = find_member_link(state->chptr,
3616 state->cli_change[i].client)) ||
3617 (MyUser(state->sptr) && IsZombie(member))) {
3618 if (MyUser(state->sptr))
3619 sendto_one(state->sptr, err_str(ERR_USERNOTINCHANNEL), me.name,
3620 state->sptr->name, state->cli_change[i].client->name,
3621 state->chptr->chname);
3625 if ((state->cli_change[i].flag & MODE_ADD &&
3626 (state->cli_change[i].flag & member->status)) ||
3627 (state->cli_change[i].flag & MODE_DEL &&
3628 !(state->cli_change[i].flag & member->status)))
3629 continue; /* no change made, don't do anything */
3631 /* see if the deop is allowed */
3632 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
3633 (MODE_DEL | MODE_CHANOP)) {
3634 /* prevent +k users from being deopped */
3635 if (IsChannelService(state->cli_change[i].client)) {
3636 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
3637 sendto_op_mask(SNO_HACK4, "Deop of +k user on %s by %s",
3638 state->chptr->chname,
3639 (IsServer(state->sptr) ? state->sptr->name :
3640 state->sptr->user->server->name));
3642 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
3643 sendto_one(state->sptr, err_str(ERR_ISCHANSERVICE), me.name,
3644 state->sptr->name, state->cli_change[i].client->name,
3645 state->chptr->chname);
3650 #ifdef NO_OPER_DEOP_LCHAN
3651 /* don't allow local opers to be deopped on local channels */
3652 if (MyUser(state->sptr) && state->cli_change[i].client != state->sptr &&
3653 IsOperOnLocalChannel(state->cli_change[i].client,
3654 state->chptr->chname)) {
3655 sendto_one(state->sptr, err_str(ERR_ISOPERLCHAN), me.name,
3656 state->sptr->name, state->cli_change[i].client->name,
3657 state->chptr->chname);
3663 /* accumulate the change */
3664 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
3665 state->cli_change[i].client);
3667 /* actually effect the change */
3668 if (state->flags & MODE_PARSE_SET) {
3669 if (state->cli_change[i].flag & MODE_ADD) {
3670 member->status |= (state->cli_change[i].flag &
3671 (MODE_CHANOP | MODE_VOICE));
3672 if (state->cli_change[i].flag & MODE_CHANOP)
3673 ClearDeopped(member);
3675 member->status &= ~(state->cli_change[i].flag &
3676 (MODE_CHANOP | MODE_VOICE));
3678 } /* for (i = 0; state->cli_change[i].flags; i++) { */
3682 * Helper function to process the simple modes
3685 mode_parse_mode(struct ParseState *state, int *flag_p)
3687 if ((state->dir == MODE_ADD && (flag_p[0] & state->chptr->mode.mode)) ||
3688 (state->dir == MODE_DEL && !(flag_p[0] & state->chptr->mode.mode)))
3689 return; /* no change */
3691 /* If they're not an oper, they can't change modes */
3692 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3693 send_notoper(state);
3697 assert(0 != state->mbuf);
3699 modebuf_mode(state->mbuf, state->dir | flag_p[0]);
3701 /* make +p and +s mutually exclusive */
3702 if (state->dir == MODE_ADD && flag_p[0] & (MODE_SECRET | MODE_PRIVATE)) {
3703 if (flag_p[0] == MODE_SECRET && (state->chptr->mode.mode & MODE_PRIVATE))
3704 modebuf_mode(state->mbuf, MODE_DEL | MODE_PRIVATE);
3705 else if (flag_p[0] == MODE_PRIVATE &&
3706 (state->chptr->mode.mode & MODE_SECRET))
3707 modebuf_mode(state->mbuf, MODE_DEL | MODE_SECRET);
3710 if (state->flags & MODE_PARSE_SET) { /* set the flags */
3711 if (state->dir == MODE_ADD) { /* add the mode to the channel */
3712 state->chptr->mode.mode |= flag_p[0];
3714 /* make +p and +s mutually exclusive */
3715 if (state->dir == MODE_ADD && flag_p[0] & (MODE_SECRET | MODE_PRIVATE)) {
3716 if (flag_p[0] == MODE_PRIVATE)
3717 state->chptr->mode.mode &= ~MODE_SECRET;
3719 state->chptr->mode.mode &= ~MODE_PRIVATE;
3721 } else /* remove the mode from the channel */
3722 state->chptr->mode.mode &= ~flag_p[0];
3725 /* Clear out invite structures if we're removing invites */
3726 if (state->flags & MODE_PARSE_SET && state->dir == MODE_DEL &&
3727 flag_p[0] == MODE_INVITEONLY)
3728 mode_invite_clear(state->chptr);
3732 * This routine is intended to parse MODE or OPMODE commands and effect the
3733 * changes (or just build the bounce buffer). We pass the starting offset
3737 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
3738 struct Channel *chptr, int parc, char *parv[], unsigned int flags)
3740 static int chan_flags[] = {
3745 MODE_MODERATED, 'm',
3746 MODE_TOPICLIMIT, 't',
3747 MODE_INVITEONLY, 'i',
3748 MODE_NOPRIVMSGS, 'n',
3759 struct ParseState state;
3770 state.chptr = chptr;
3773 state.flags = flags;
3774 state.dir = MODE_ADD;
3776 state.args_used = 0;
3777 state.max_args = MAXMODEPARAMS;
3780 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
3781 state.banlist[i].next = 0;
3782 state.banlist[i].value.ban.banstr = 0;
3783 state.banlist[i].value.ban.who = 0;
3784 state.banlist[i].value.ban.when = 0;
3785 state.banlist[i].flags = 0;
3786 state.cli_change[i].flag = 0;
3787 state.cli_change[i].client = 0;
3790 modestr = state.parv[state.args_used++];
3794 for (; *modestr; modestr++) {
3795 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
3796 if (flag_p[1] == *modestr)
3799 if (!flag_p[0]) { /* didn't find it? complain and continue */
3800 if (MyUser(state.sptr))
3801 sendto_one(state.sptr, err_str(ERR_UNKNOWNMODE), me.name,
3802 state.sptr->name, *modestr);
3807 case '+': /* switch direction to MODE_ADD */
3808 case '-': /* switch direction to MODE_DEL */
3809 state.dir = flag_p[0];
3812 case 'l': /* deal with limits */
3813 mode_parse_limit(&state, flag_p);
3816 case 'k': /* deal with keys */
3817 mode_parse_key(&state, flag_p);
3820 case 'b': /* deal with bans */
3821 mode_parse_ban(&state, flag_p);
3824 case 'o': /* deal with ops/voice */
3826 mode_parse_client(&state, flag_p);
3829 default: /* deal with other modes */
3830 mode_parse_mode(&state, flag_p);
3832 } /* switch (*modestr) { */
3833 } /* for (; *modestr; modestr++) { */
3835 if (state.parc > 0) { /* process next argument in string */
3836 modestr = state.parv[state.args_used++];
3840 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
3843 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
3844 break; /* we're then going to bounce the mode! */
3846 recv_ts = atoi(modestr);
3848 if (recv_ts && recv_ts < state.chptr->creationtime)
3849 state.chptr->creationtime = recv_ts; /* respect earlier TS */
3851 break; /* break out of while loop */
3852 } else if (state.flags & MODE_PARSE_STRICT ||
3853 (MyUser(state.sptr) && state.max_args <= 0)) {
3854 state.parc++; /* we didn't actually gobble the argument */
3856 break; /* break out of while loop */
3859 } /* while (*modestr) { */
3862 * the rest of the function finishes building resultant MODEs; if the
3863 * origin isn't a member or an oper, skip it.
3865 if (state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3866 return state.args_used; /* tell our parent how many args we gobbled */
3868 assert(0 != state.mbuf);
3870 if (state.done & DONE_BANCLEAN) /* process bans */
3871 mode_process_bans(&state);
3873 /* process client changes */
3874 if (state.cli_change[0].flag)
3875 mode_process_clients(&state);
3877 return state.args_used; /* tell our parent how many args we gobbled */