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"
54 struct Channel* GlobalChannelList = 0;
56 static struct SLink *next_overlapped_ban(void);
57 static int del_banid(struct Channel *, char *, int);
58 void del_invite(struct Client *, struct Channel *);
60 const char* const PartFmt1 = ":%s " MSG_PART " %s";
61 const char* const PartFmt2 = ":%s " MSG_PART " %s :%s";
62 const char* const PartFmt1serv = "%s%s " TOK_PART " %s";
63 const char* const PartFmt2serv = "%s%s " TOK_PART " %s :%s";
66 static struct SLink* next_ban;
67 static struct SLink* prev_ban;
68 static struct SLink* removed_bans_list;
71 * Use a global variable to remember if an oper set a mode on a local channel. Ugly,
72 * but the only way to do it without changing set_mode intensively.
74 int LocalChanOperMode = 0;
78 * return the length (>=0) of a chain of links.
80 static int list_length(struct SLink *lp)
84 for (; lp; lp = lp->next)
90 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
96 /* Servers don't have member links */
100 /* +k users are typically on a LOT of channels. So we iterate over who
101 * is in the channel. X/W are +k and are in about 5800 channels each.
102 * however there are typically no more than 1000 people in a channel
105 if (IsChannelService(cptr)) {
108 assert(m->channel == chptr);
114 /* Users on the other hand aren't allowed on more than 15 channels. 50%
115 * of users that are on channels are on 2 or less, 95% are on 7 or less,
116 * and 99% are on 10 or less.
119 m = cptr->user->channel;
121 assert(m->user == cptr);
122 if (m->channel == chptr)
131 * find_chasing - Find the client structure for a nick name (user)
132 * using history mechanism if necessary. If the client is not found, an error
133 * message (NO SUCH NICK) is generated. If the client was found
134 * through the history, chasing will be 1 and otherwise 0.
136 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
138 struct Client* who = FindClient(user);
145 if (!(who = get_history(user, KILLCHASETIMELIMIT))) {
146 sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, sptr->name, user);
155 * Create a string of form "foo!bar@fubar" given foo, bar and fubar
156 * as the parameters. If NULL, they become "*".
158 static char *make_nick_user_host(const char *nick, const char *name,
161 static char namebuf[NICKLEN + USERLEN + HOSTLEN + 3];
162 sprintf_irc(namebuf, "%s!%s@%s", nick, name, host);
167 * Create a string of form "foo!bar@123.456.789.123" given foo, bar and the
168 * IP-number as the parameters. If NULL, they become "*".
170 static char *make_nick_user_ip(char *nick, char *name, struct in_addr ip)
172 static char ipbuf[NICKLEN + USERLEN + 16 + 3];
173 sprintf_irc(ipbuf, "%s!%s@%s", nick, name, ircd_ntoa((const char*) &ip));
178 static int DoesOp(const char* modebuf)
180 assert(0 != modebuf);
182 if (*modebuf == 'o' || *modebuf == 'v')
190 * This function should be removed when all servers are 2.10
192 static void sendmodeto_one(struct Client* cptr, const char* from,
193 const char* name, const char* mode,
194 const char* param, time_t creationtime)
196 if (IsServer(cptr) && DoesOp(mode) && creationtime)
197 sendto_one(cptr, ":%s MODE %s %s %s " TIME_T_FMT,
198 from, name, mode, param, creationtime);
200 sendto_one(cptr, ":%s MODE %s %s %s", from, name, mode, param);
205 * Subtract one user from channel i (and free channel
206 * block, if channel became empty).
207 * Returns: true (1) if channel still exists
208 * false (0) if the channel was destroyed
210 int sub1_from_channel(struct Channel* chptr)
215 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
217 assert(0 != chptr->members);
222 assert(0 == chptr->members);
224 /* Channel became (or was) empty: Remove channel */
225 if (is_listed(chptr))
228 for (i = 0; i <= HighestFd; i++)
230 struct Client *acptr;
231 if ((acptr = LocalClientArray[i]) && acptr->listing &&
232 acptr->listing->chptr == chptr)
234 list_next_channels(acptr, 1);
235 break; /* Only one client can list a channel */
240 * Now, find all invite links from channel structure
242 while ((tmp = chptr->invites))
243 del_invite(tmp->value.cptr, chptr);
245 tmp = chptr->banlist;
250 MyFree(obtmp->value.ban.banstr);
251 MyFree(obtmp->value.ban.who);
255 chptr->prev->next = chptr->next;
257 GlobalChannelList = chptr->next;
259 chptr->next->prev = chptr->prev;
261 --UserStats.channels;
263 * make sure that channel actually got removed from hash table
265 assert(chptr->hnext == chptr);
273 * `cptr' must be the client adding the ban.
275 * If `change' is true then add `banid' to channel `chptr'.
276 * Returns 0 if the ban was added.
277 * Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
278 * Return -1 otherwise.
280 * Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
281 * when `change' is false, otherwise they will be removed from the banlist.
282 * Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
283 * respectively will return these bans until NULL is returned.
285 * If `firsttime' is true, the ban list as returned by next_overlapped_ban()
286 * is reset (unless a non-zero value is returned, in which case the
287 * CHFL_BAN_OVERLAPPED flag might not have been reset!).
291 int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
292 int change, int firsttime)
297 int removed_bans = 0;
298 int len = strlen(banid);
303 assert(0 == prev_ban);
304 assert(0 == removed_bans_list);
308 for (banp = &chptr->banlist; *banp;)
310 len += strlen((*banp)->value.ban.banstr);
312 if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
314 if (!strcmp((*banp)->value.ban.banstr, banid))
316 (*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
320 else if (!mmatch((*banp)->value.ban.banstr, banid))
322 if (!mmatch(banid, (*banp)->value.ban.banstr))
324 struct SLink *tmp = *banp;
330 len -= strlen(tmp->value.ban.banstr);
334 /* Silently remove overlapping bans */
335 MyFree(tmp->value.ban.banstr);
336 MyFree(tmp->value.ban.who);
340 /* These will be sent to the user later as -b */
341 tmp->next = removed_bans_list;
342 removed_bans_list = tmp;
346 else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
348 tmp->flags |= CHFL_BAN_OVERLAPPED;
359 (*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
360 banp = &(*banp)->next;
363 if (MyUser(cptr) && !removed_bans && (len > MAXBANLENGTH || (cnt >= MAXBANS)))
365 sendto_one(cptr, err_str(ERR_BANLISTFULL), me.name, cptr->name,
366 chptr->chname, banid);
372 struct Membership* member;
374 ban->next = chptr->banlist;
376 ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
377 assert(0 != ban->value.ban.banstr);
378 strcpy(ban->value.ban.banstr, banid);
380 ban->value.ban.who = (char*) MyMalloc(strlen(cptr->name) + 1);
381 assert(0 != ban->value.ban.who);
382 strcpy(ban->value.ban.who, cptr->name);
384 ban->value.ban.when = TStime();
385 ban->flags = CHFL_BAN; /* This bit is never used I think... */
386 if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
387 ban->flags |= CHFL_BAN_IPMASK;
388 chptr->banlist = ban;
391 * Erase ban-valid-bit
393 for (member = chptr->members; member; member = member->next_member)
394 ClearBanValid(member); /* `ban' == channel member ! */
399 static struct SLink *next_overlapped_ban(void)
401 struct SLink *tmp = next_ban;
405 for (ban = tmp->next; ban; ban = ban->next)
406 if ((ban->flags & CHFL_BAN_OVERLAPPED))
413 struct SLink *next_removed_overlapped_ban(void)
415 struct SLink *tmp = removed_bans_list;
418 if (prev_ban->value.ban.banstr) /* Can be set to NULL in set_mode() */
419 MyFree(prev_ban->value.ban.banstr);
420 MyFree(prev_ban->value.ban.who);
425 removed_bans_list = removed_bans_list->next;
433 * If `change' is true, delete `banid' from channel `chptr'.
434 * Returns `false' if removal was (or would have been) successful.
436 static int del_banid(struct Channel *chptr, char *banid, int change)
443 for (ban = &(chptr->banlist); *ban; ban = &((*ban)->next)) {
444 if (0 == ircd_strcmp(banid, (*ban)->value.ban.banstr))
449 struct Membership* member;
451 MyFree(tmp->value.ban.banstr);
452 MyFree(tmp->value.ban.who);
455 * Erase ban-valid-bit, for channel members that are banned
457 for (member = chptr->members; member; member = member->next_member)
458 if (CHFL_BANVALIDMASK == (member->status & CHFL_BANVALIDMASK))
459 ClearBanValid(member); /* `tmp' == channel member */
468 * find_channel_member - returns Membership * if a person is joined and not a zombie
470 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
472 struct Membership* member;
475 member = find_member_link(chptr, cptr);
476 return (member && !IsZombie(member)) ? member : 0;
480 * is_banned - a non-zero value if banned else 0.
482 static int is_banned(struct Client *cptr, struct Channel *chptr,
483 struct Membership* member)
492 if (member && IsBanValid(member))
493 return IsBanned(member);
495 s = make_nick_user_host(cptr->name, cptr->user->username, cptr->user->host);
497 for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
498 if ((tmp->flags & CHFL_BAN_IPMASK)) {
500 ip_s = make_nick_user_ip(cptr->name, cptr->user->username, cptr->ip);
501 if (match(tmp->value.ban.banstr, ip_s) == 0)
504 else if (match(tmp->value.ban.banstr, s) == 0)
520 return (tmp != NULL);
524 * adds a user to a channel by adding another link to the channels member
527 void add_user_to_channel(struct Channel* chptr, struct Client* who,
534 struct Membership* member =
535 (struct Membership*) MyMalloc(sizeof(struct Membership));
538 member->channel = chptr;
539 member->status = flags;
541 member->next_member = chptr->members;
542 if (member->next_member)
543 member->next_member->prev_member = member;
544 member->prev_member = 0;
545 chptr->members = member;
547 member->next_channel = who->user->channel;
548 if (member->next_channel)
549 member->next_channel->prev_channel = member;
550 member->prev_channel = 0;
551 who->user->channel = member;
558 static int remove_member_from_channel(struct Membership* member)
560 struct Channel* chptr;
562 chptr = member->channel;
564 * unlink channel member list
566 if (member->next_member)
567 member->next_member->prev_member = member->prev_member;
568 if (member->prev_member)
569 member->prev_member->next_member = member->next_member;
571 member->channel->members = member->next_member;
574 * unlink client channel list
576 if (member->next_channel)
577 member->next_channel->prev_channel = member->prev_channel;
578 if (member->prev_channel)
579 member->prev_channel->next_channel = member->next_channel;
581 member->user->user->channel = member->next_channel;
583 --member->user->user->joined;
586 return sub1_from_channel(chptr);
589 static int channel_all_zombies(struct Channel* chptr)
591 struct Membership* member;
593 for (member = chptr->members; member; member = member->next_member) {
594 if (!IsZombie(member))
601 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
604 struct Membership* member;
607 if ((member = find_member_link(chptr, cptr))) {
608 if (remove_member_from_channel(member)) {
609 if (channel_all_zombies(chptr)) {
611 * XXX - this looks dangerous but isn't if we got the referential
612 * integrity right for channels
614 while (remove_member_from_channel(chptr->members))
621 void remove_user_from_all_channels(struct Client* cptr)
623 struct Membership* chan;
625 assert(0 != cptr->user);
627 while ((chan = cptr->user->channel))
628 remove_user_from_channel(cptr, chan->channel);
631 int is_chan_op(struct Client *cptr, struct Channel *chptr)
633 struct Membership* member;
635 if ((member = find_member_link(chptr, cptr)))
636 return (!IsZombie(member) && IsChanOp(member));
641 static int is_deopped(struct Client *cptr, struct Channel *chptr)
643 struct Membership* member;
646 if ((member = find_member_link(chptr, cptr)))
647 return IsDeopped(member);
649 return (IsUser(cptr) ? 1 : 0);
652 int is_zombie(struct Client *cptr, struct Channel *chptr)
654 struct Membership* member;
658 if ((member = find_member_link(chptr, cptr)))
659 return IsZombie(member);
663 int has_voice(struct Client* cptr, struct Channel* chptr)
665 struct Membership* member;
668 if ((member = find_member_link(chptr, cptr)))
669 return (!IsZombie(member) && HasVoice(member));
674 int member_can_send_to_channel(struct Membership* member)
678 if (IsVoicedOrOpped(member))
681 * If it's moderated, and you aren't a priviledged user, you can't
684 if (member->channel->mode.mode & MODE_MODERATED)
687 * If you're banned then you can't speak either.
688 * but because of the amount of CPU time that is_banned chews
689 * we only check it for our clients.
691 if (MyUser(member->user) && is_banned(member->user, member->channel, member))
696 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
698 struct Membership *member;
701 * Servers can always speak on channels.
706 member = find_channel_member(cptr, chptr);
709 * You can't speak if your off channel, if the channel is modeless, or
710 * +n.(no external messages)
713 if ((chptr->mode.mode & MODE_NOPRIVMSGS) || IsModelessChannel(chptr->chname))
718 return member_can_send_to_channel(member);
722 * find_no_nickchange_channel
723 * if a member and not opped or voiced and banned
724 * return the name of the first channel banned on
726 const char* find_no_nickchange_channel(struct Client* cptr)
729 struct Membership* member;
730 for (member = cptr->user->channel; member; member = member->next_channel) {
731 if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
732 return member->channel->chname;
740 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
741 * with the parameters in pbuf.
743 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
744 struct Channel *chptr)
751 if (chptr->mode.mode & MODE_SECRET)
753 else if (chptr->mode.mode & MODE_PRIVATE)
755 if (chptr->mode.mode & MODE_MODERATED)
757 if (chptr->mode.mode & MODE_TOPICLIMIT)
759 if (chptr->mode.mode & MODE_INVITEONLY)
761 if (chptr->mode.mode & MODE_NOPRIVMSGS)
763 if (chptr->mode.limit) {
765 sprintf_irc(pbuf, "%d", chptr->mode.limit);
768 if (*chptr->mode.key) {
770 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
771 if (chptr->mode.limit)
773 strcat(pbuf, chptr->mode.key);
780 static int send_mode_list(struct Client *cptr, char *chname,
781 time_t creationtime, struct SLink *top,
791 cp = modebuf + strlen(modebuf);
792 if (*parabuf) /* mode +l or +k xx */
794 for (lp = top; lp; lp = lp->next)
796 if (!(lp->flags & mask))
798 if (mask == CHFL_BAN)
799 name = lp->value.ban.banstr;
801 name = lp->value.cptr->name;
802 if (strlen(parabuf) + strlen(name) + 11 < MODEBUFLEN)
804 strcat(parabuf, " ");
805 strcat(parabuf, name);
816 /* cptr is always a server! So we send creationtimes */
817 sendmodeto_one(cptr, me.name, chname, modebuf, parabuf, creationtime);
825 strcpy(parabuf, name);
838 * send "cptr" a full list of the modes for channel chptr.
840 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
842 static unsigned int current_flags[4] =
843 { 0, CHFL_CHANOP | CHFL_VOICE, CHFL_VOICE, CHFL_CHANOP };
850 struct Membership* member;
852 char modebuf[MODEBUFLEN];
853 char parabuf[MODEBUFLEN];
858 if (IsLocalChannel(chptr->chname))
861 member = chptr->members;
862 lp2 = chptr->banlist;
864 *modebuf = *parabuf = '\0';
865 channel_modes(cptr, modebuf, parabuf, chptr);
867 for (first = 1; full; first = 0) /* Loop for multiple messages */
869 full = 0; /* Assume by default we get it
870 all in one message */
872 /* (Continued) prefix: "<Y> B <channel> <TS>" */
873 sprintf_irc(sendbuf, "%s B %s " TIME_T_FMT, NumServ(&me),
874 chptr->chname, chptr->creationtime);
875 sblen = strlen(sendbuf);
877 if (first && modebuf[1]) /* Add simple modes (iklmnpst)
880 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
881 sendbuf[sblen++] = ' ';
882 strcpy(sendbuf + sblen, modebuf);
883 sblen += strlen(modebuf);
886 sendbuf[sblen++] = ' ';
887 strcpy(sendbuf + sblen, parabuf);
888 sblen += strlen(parabuf);
893 * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
895 * Run 4 times over all members, to group the members with the
898 for (first = 1; flag_cnt < 4;
899 member = chptr->members, new_mode = 1, flag_cnt++)
901 for (; member; member = member->next_member)
903 if ((member->status & CHFL_VOICED_OR_OPPED) !=
904 current_flags[flag_cnt])
905 continue; /* Skip members with different flags */
906 if (sblen + NUMNICKLEN + 4 > BUFSIZE - 3)
907 /* The 4 is a possible ",:ov"
908 The -3 is for the "\r\n\0" that is added in send.c */
910 full = 1; /* Make sure we continue after
912 new_mode = 1; /* Ensure the new BURST line contains the current
914 break; /* Do not add this member to this message */
916 sendbuf[sblen++] = first ? ' ' : ',';
917 first = 0; /* From now on, us comma's to add new nicks */
919 sprintf_irc(sendbuf + sblen, "%s%s", NumNick(member->user));
920 sblen += strlen(sendbuf + sblen);
922 * Do we have a nick with a new mode ?
923 * Or are we starting a new BURST line?
928 if (IsVoicedOrOpped(member)) {
929 sendbuf[sblen++] = ':';
930 if (IsChanOp(member))
931 sendbuf[sblen++] = 'o';
932 if (HasVoice(member))
933 sendbuf[sblen++] = 'v';
943 /* Attach all bans, space seperated " :%ban ban ..." */
944 for (first = 2; lp2; lp2 = lp2->next)
946 len = strlen(lp2->value.ban.banstr);
947 if (sblen + len + 1 + first > BUFSIZE - 3)
948 /* The +1 stands for the added ' '.
949 * The +first stands for the added ":%".
950 * The -3 is for the "\r\n\0" that is added in send.c
959 sendbuf[sblen++] = ' ';
960 sendbuf[sblen++] = ':'; /* Will be last parameter */
961 sendbuf[sblen++] = '%'; /* To tell bans apart */
964 sendbuf[sblen++] = ' ';
965 strcpy(sendbuf + sblen, lp2->value.ban.banstr);
970 sendbuf[sblen] = '\0';
971 sendbufto_one(cptr); /* Send this message */
972 } /* Continue when there was something
973 that didn't fit (full==1) */
979 * by Carlo Wood (Run), 05 Oct 1998.
983 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
984 * When the user name or host name are too long (USERLEN and HOSTLEN
985 * respectively) then they are cut off at the start with a '*'.
987 * The following transformations are made:
990 * 2) xxx.xxx -> *!*@host
991 * 3) xxx!yyy -> nick!user@*
992 * 4) xxx@yyy -> *!user@host
993 * 5) xxx!yyy@zzz -> nick!user@host
995 char *pretty_mask(char *mask)
997 static char star[2] = { '*', 0 };
998 char *last_dot = NULL;
1001 /* Case 1: default */
1006 /* Do a _single_ pass through the characters of the mask: */
1007 for (ptr = mask; *ptr; ++ptr)
1011 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1015 else if (*ptr == '@')
1017 /* Case 4: Found last '@' (without finding a '!' yet) */
1022 else if (*ptr == '.')
1024 /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
1034 /* Case 4 or 5: Found last '@' */
1040 if (user == star && last_dot)
1050 char *nick_end = (user != star) ? user - 1 : ptr;
1051 if (nick_end - nick > NICKLEN)
1057 char *user_end = (host != star) ? host - 1 : ptr;
1058 if (user_end - user > USERLEN)
1060 user = user_end - USERLEN;
1065 if (host != star && ptr - host > HOSTLEN)
1067 host = ptr - HOSTLEN;
1070 return make_nick_user_host(nick, user, host);
1073 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1080 for (lp = chptr->banlist; lp; lp = lp->next)
1081 sendto_one(cptr, rpl_str(RPL_BANLIST), me.name, cptr->name,
1082 chptr->chname, lp->value.ban.banstr, lp->value.ban.who,
1083 lp->value.ban.when);
1084 sendto_one(cptr, rpl_str(RPL_ENDOFBANLIST), me.name, cptr->name,
1089 * Check and try to apply the channel modes passed in the parv array for
1090 * the client ccptr to channel chptr. The resultant changes are printed
1091 * into mbuf and pbuf (if any) and applied to the channel.
1093 int set_mode(struct Client* cptr, struct Client* sptr,
1094 struct Channel* chptr, int parc, char* parv[],
1095 char* mbuf, char* pbuf, char* npbuf, int* badop)
1098 * This size is only needed when a broken
1099 * server sends more then MAXMODEPARAMS
1102 static struct SLink chops[MAXPARA - 2];
1103 static int flags[] = {
1106 MODE_MODERATED, 'm',
1107 MODE_NOPRIVMSGS, 'n',
1108 MODE_TOPICLIMIT, 't',
1109 MODE_INVITEONLY, 'i',
1115 char bmodebuf[MODEBUFLEN];
1116 char bparambuf[MODEBUFLEN];
1117 char nbparambuf[MODEBUFLEN]; /* "Numeric" Bounce Parameter Buffer */
1119 char* curr = parv[0];
1122 struct Membership* member_x;
1123 struct Membership* member_y;
1124 unsigned int whatt = MODE_ADD;
1125 unsigned int bwhatt = 0;
1128 int add_banid_called = 0;
1134 unsigned int nusers = 0;
1135 unsigned int newmode;
1146 static char numeric[16];
1147 char* bmbuf = bmodebuf;
1148 char* bpbuf = bparambuf;
1149 char* nbpbuf = nbparambuf;
1151 struct ConfItem* aconf;
1153 *mbuf = *pbuf = *npbuf = *bmbuf = *bpbuf = *nbpbuf = '\0';
1158 * Mode is accepted when sptr is a channel operator
1159 * but also when the mode is received from a server.
1160 * At this point, let any member pass, so they are allowed
1163 member_y = find_channel_member(sptr, chptr);
1164 if (!(IsServer(cptr) || member_y))
1167 #ifdef OPER_MODE_LCHAN
1168 if (IsOperOnLocalChannel(sptr, chptr->chname) && !IsChanOp(member_y))
1169 LocalChanOperMode = 1;
1172 mode = &(chptr->mode);
1173 memcpy(&oldm, mode, sizeof(struct Mode));
1175 newmode = mode->mode;
1177 while (curr && *curr) {
1190 if (MyUser(sptr) && opcnt >= MAXMODEPARAMS)
1193 * Check for nickname changes and try to follow these
1194 * to make sure the right client is affected by the
1196 * Even if we find a nick with find_chasing() there
1197 * is still a reason to ignore in a special case.
1198 * We need to ignore the mode when:
1199 * - It is part of a net.burst (from a server and
1200 * a MODE_ADD). Ofcourse we don't ignore mode
1201 * changes from Uworld.
1202 * - The found nick is not on the right side off
1204 * This fixes the bug that when someone (tries to)
1205 * ride a net.break and does so with the nick of
1206 * someone on the otherside, that he is nick collided
1207 * (killed) but his +o still ops the other person.
1211 if (!(who = find_chasing(sptr, parv[0], NULL)))
1216 if (!(who = findNUser(parv[0])))
1219 if (whatt == MODE_ADD && IsServer(sptr) && who->from != sptr->from &&
1220 !find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD))
1223 if (!(member_x = find_member_link(chptr, who)) ||
1224 (MyUser(sptr) && IsZombie(member_x)))
1226 sendto_one(cptr, err_str(ERR_USERNOTINCHANNEL),
1227 me.name, cptr->name, who->name, chptr->chname);
1231 * if the user is +k, prevent a deop from local user
1233 if (whatt == MODE_DEL && IsChannelService(who) && *curr == 'o') {
1238 sendto_one(cptr, err_str(ERR_ISCHANSERVICE), me.name,
1239 cptr->name, parv[0], chptr->chname);
1243 sprintf_irc(sendbuf,":%s NOTICE * :*** Notice -- Deop of +k user on %s by %s",
1244 me.name,chptr->chname,cptr->name);
1247 #ifdef NO_OPER_DEOP_LCHAN
1249 * if the user is an oper on a local channel, prevent him
1250 * from being deoped. that oper can deop himself though.
1252 if (whatt == MODE_DEL && IsOperOnLocalChannel(who, chptr->chname) &&
1253 (who != sptr) && MyUser(cptr) && *curr == 'o')
1255 sendto_one(cptr, err_str(ERR_ISOPERLCHAN), me.name,
1256 cptr->name, parv[0], chptr->chname);
1260 if (whatt == MODE_ADD)
1262 lp = &chops[opcnt++];
1263 lp->value.cptr = who;
1264 if (IsServer(sptr) && (!(who->flags & FLAGS_TS8) || ((*curr == 'o') &&
1265 !(member_x->status & (CHFL_SERVOPOK | CHFL_CHANOP)))))
1266 *badop = ((member_x->status & CHFL_DEOPPED) && (*curr == 'o')) ? 2 : 3;
1267 lp->flags = (*curr == 'o') ? MODE_CHANOP : MODE_VOICE;
1268 lp->flags |= MODE_ADD;
1270 else if (whatt == MODE_DEL)
1272 lp = &chops[opcnt++];
1273 lp->value.cptr = who;
1274 doesdeop = 1; /* Also when -v */
1275 lp->flags = (*curr == 'o') ? MODE_CHANOP : MODE_VOICE;
1276 lp->flags |= MODE_DEL;
1285 /* check now so we eat the parameter if present */
1290 char *s = &(*parv)[-1];
1291 unsigned short count = KEYLEN + 1;
1293 while (*++s > ' ' && *s != ':' && --count);
1295 if (!**parv) /* nothing left in key */
1298 if (MyUser(sptr) && opcnt >= MAXMODEPARAMS)
1300 if (whatt == MODE_ADD)
1302 if (*mode->key && !IsServer(cptr))
1303 sendto_one(cptr, err_str(ERR_KEYSET),
1304 me.name, cptr->name, chptr->chname);
1305 else if (!*mode->key || IsServer(cptr))
1307 lp = &chops[opcnt++];
1308 lp->value.cp = *parv;
1309 if (strlen(lp->value.cp) > KEYLEN)
1310 lp->value.cp[KEYLEN] = '\0';
1311 lp->flags = MODE_KEY | MODE_ADD;
1315 else if (whatt == MODE_DEL)
1317 /* Debug((DEBUG_INFO, "removing key: mode->key: >%s< *parv: >%s<", mode->key, *parv)); */
1318 if (0 == ircd_strcmp(mode->key, *parv) || IsServer(cptr))
1320 /* Debug((DEBUG_INFO, "key matched")); */
1321 lp = &chops[opcnt++];
1322 lp->value.cp = mode->key;
1323 lp->flags = MODE_KEY | MODE_DEL;
1330 if (0 == banlsent) {
1334 send_ban_list(cptr, chptr);
1340 if (EmptyString(*parv))
1344 if ((cp = strchr(*parv, ' ')))
1346 if (opcnt >= MAXMODEPARAMS || **parv == ':' || **parv == '\0')
1349 if (whatt == MODE_ADD)
1351 lp = &chops[opcnt++];
1352 lp->value.cp = *parv;
1353 lp->flags = MODE_ADD | MODE_BAN;
1355 else if (whatt == MODE_DEL)
1357 lp = &chops[opcnt++];
1358 lp->value.cp = *parv;
1359 lp->flags = MODE_DEL | MODE_BAN;
1364 * limit 'l' to only *1* change per mode command but
1369 if (whatt == MODE_ADD && --parc > 0)
1373 if (whatt == MODE_DEL)
1381 if (EmptyString(*parv))
1383 if (MyUser(sptr) && opcnt >= MAXMODEPARAMS)
1385 if (!(nusers = atoi(*++parv)))
1387 lp = &chops[opcnt++];
1388 lp->flags = MODE_ADD | MODE_LIMIT;
1392 need_more_params(cptr, "MODE +l");
1394 case 'i': /* falls through for default case */
1395 if (whatt == MODE_DEL)
1396 while ((lp = chptr->invites))
1397 del_invite(lp->value.cptr, chptr);
1399 for (ip = flags; *ip; ip += 2)
1400 if (*(ip + 1) == *curr)
1405 if (whatt == MODE_ADD)
1407 if (*ip == MODE_PRIVATE)
1408 newmode &= ~MODE_SECRET;
1409 else if (*ip == MODE_SECRET)
1410 newmode &= ~MODE_PRIVATE;
1416 else if (!IsServer(cptr))
1417 sendto_one(cptr, err_str(ERR_UNKNOWNMODE),
1418 me.name, cptr->name, *curr);
1423 * Make sure mode strings such as "+m +t +p +i" are parsed
1426 if (!*curr && parc > 0)
1430 /* If this was from a server, and it is the last
1431 * parameter and it starts with a digit, it must
1432 * be the creationtime. --Run
1436 if (parc == 1 && IsDigit(*curr))
1438 newtime = atoi(curr);
1439 if (newtime && chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
1441 chptr->creationtime = newtime;
1450 else if (newtime > chptr->creationtime)
1451 { /* It is a net-break ride if we have ops.
1452 bounce modes if we have ops. --Run */
1455 else if (chptr->creationtime == 0)
1457 if (chptr->creationtime == 0 || doesop)
1458 chptr->creationtime = newtime;
1466 * A legal *badop can occur when two
1467 * people join simultaneously a channel,
1468 * Allow for 10 min of lag (and thus hacking
1469 * on channels younger then 10 min) --Run
1471 else if (*badop == 0 ||
1472 chptr->creationtime > (TStime() - TS_LAG_TIME))
1474 if (newtime < chptr->creationtime)
1475 chptr->creationtime = newtime;
1484 } /* end of while loop for MODE processing */
1486 #ifdef OPER_MODE_LCHAN
1488 * Now reject non chan ops. Accept modes from opers on local channels
1489 * even if they are deopped
1491 if (!IsServer(cptr) &&
1492 (!member_y || !(IsChanOp(member_y) ||
1493 IsOperOnLocalChannel(sptr, chptr->chname))))
1495 if (!IsServer(cptr) && (!member_y || !IsChanOp(member_y)))
1499 return (opcnt || newmode != mode->mode || limitset || keychange) ? 0 : -1;
1502 if (doesop && newtime == 0 && IsServer(sptr))
1506 (aconf = find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD)))
1509 #ifdef OPER_MODE_LCHAN
1510 bounce = (*badop == 1 || *badop == 2 ||
1511 (is_deopped(sptr, chptr) &&
1512 !IsOperOnLocalChannel(sptr, chptr->chname))) ? 1 : 0;
1514 bounce = (*badop == 1 || *badop == 2 || is_deopped(sptr, chptr)) ? 1 : 0;
1518 for (ip = flags; *ip; ip += 2) {
1519 if ((*ip & newmode) && !(*ip & oldm.mode))
1523 if (bwhatt != MODE_DEL)
1528 *bmbuf++ = *(ip + 1);
1532 if (whatt != MODE_ADD)
1538 *mbuf++ = *(ip + 1);
1542 for (ip = flags; *ip; ip += 2) {
1543 if ((*ip & oldm.mode) && !(*ip & newmode))
1547 if (bwhatt != MODE_ADD)
1552 *bmbuf++ = *(ip + 1);
1556 if (whatt != MODE_DEL)
1562 *mbuf++ = *(ip + 1);
1567 if (limitset && !nusers && mode->limit)
1571 if (bwhatt != MODE_ADD)
1577 sprintf(numeric, "%-15d", mode->limit);
1578 if ((cp = strchr(numeric, ' ')))
1580 strcat(bpbuf, numeric);
1581 blen += strlen(numeric);
1583 strcat(nbpbuf, numeric);
1584 nblen += strlen(numeric);
1585 strcat(nbpbuf, " ");
1589 if (whatt != MODE_DEL)
1594 mode->mode &= ~MODE_LIMIT;
1600 * Reconstruct "+bkov" chain.
1606 unsigned int prev_whatt = 0;
1608 for (; i < opcnt; i++)
1612 * make sure we have correct mode change sign
1614 if (whatt != (lp->flags & (MODE_ADD | MODE_DEL)))
1616 if (lp->flags & MODE_ADD)
1630 nlen = strlen(npbuf);
1632 * get c as the mode char and tmp as a pointer to
1633 * the parameter for this mode change.
1635 switch (lp->flags & MODE_WPARAS)
1639 cp = lp->value.cptr->name;
1643 cp = lp->value.cptr->name;
1647 * I made this a bit more user-friendly (tm):
1649 * nick!user = nick!user@*
1650 * user@host = *!user@host
1651 * host.name = *!*@host.name --Run
1654 cp = pretty_mask(lp->value.cp);
1662 sprintf(numeric, "%-15d", nusers);
1663 if ((cp = strchr(numeric, ' ')))
1669 /* What could be added: cp+' '+' '+<TS>+'\0' */
1670 if (len + strlen(cp) + 13 > MODEBUFLEN ||
1671 nlen + strlen(cp) + NUMNICKLEN + 12 > MODEBUFLEN)
1674 switch (lp->flags & MODE_WPARAS)
1677 if (strlen(cp) > KEYLEN)
1678 *(cp + KEYLEN) = '\0';
1679 if ((whatt == MODE_ADD && (*mode->key == '\0' ||
1680 0 != ircd_strcmp(mode->key, cp))) ||
1681 (whatt == MODE_DEL && (*mode->key != '\0')))
1685 if (*mode->key == '\0')
1687 if (bwhatt != MODE_DEL)
1697 nblen += strlen(cp);
1698 strcat(nbpbuf, " ");
1703 if (bwhatt != MODE_ADD)
1708 strcat(bpbuf, mode->key);
1709 blen += strlen(mode->key);
1712 strcat(nbpbuf, mode->key);
1713 nblen += strlen(mode->key);
1714 strcat(nbpbuf, " ");
1719 if (*mbuf != '+' && *mbuf != '-')
1735 if (whatt == MODE_ADD)
1736 ircd_strncpy(mode->key, cp, KEYLEN);
1743 if (nusers && nusers != mode->limit)
1747 if (mode->limit == 0)
1749 if (bwhatt != MODE_DEL)
1757 if (bwhatt != MODE_ADD)
1762 sprintf(numeric, "%-15d", mode->limit);
1763 if ((cp = strchr(numeric, ' ')))
1765 strcat(bpbuf, numeric);
1766 blen += strlen(numeric);
1769 strcat(nbpbuf, numeric);
1770 nblen += strlen(numeric);
1771 strcat(nbpbuf, " ");
1776 if (*mbuf != '+' && *mbuf != '-')
1792 mode->limit = nusers;
1798 member_y = find_member_link(chptr, lp->value.cptr);
1799 if (lp->flags & MODE_ADD)
1801 change = (~member_y->status) & CHFL_VOICED_OR_OPPED & lp->flags;
1802 if (change && bounce)
1804 if (lp->flags & MODE_CHANOP)
1805 SetDeopped(member_y);
1807 if (bwhatt != MODE_DEL)
1813 strcat(bpbuf, lp->value.cptr->name);
1814 blen += strlen(lp->value.cptr->name);
1817 sprintf_irc(nbpbuf + nblen, "%s%s ", NumNick(lp->value.cptr));
1818 nblen += strlen(nbpbuf + nblen);
1823 member_y->status |= lp->flags & CHFL_VOICED_OR_OPPED;
1824 if (IsChanOp(member_y))
1826 ClearDeopped(member_y);
1828 ClearServOpOk(member_y);
1834 change = member_y->status & CHFL_VOICED_OR_OPPED & lp->flags;
1835 if (change && bounce)
1837 if (lp->flags & MODE_CHANOP)
1838 ClearDeopped(member_y);
1839 if (bwhatt != MODE_ADD)
1845 strcat(bpbuf, lp->value.cptr->name);
1846 blen += strlen(lp->value.cptr->name);
1849 sprintf_irc(nbpbuf + nblen, "%s%s ", NumNick(lp->value.cptr));
1850 blen += strlen(bpbuf + blen);
1855 member_y->status &= ~change;
1856 if ((change & MODE_CHANOP) && IsServer(sptr))
1857 SetDeopped(member_y);
1860 if (change || *badop == 2 || *badop == 4)
1867 sprintf_irc(npbuf + nlen, "%s%s ", NumNick(lp->value.cptr));
1868 nlen += strlen(npbuf + nlen);
1869 npbuf[nlen++] = ' ';
1875 if (*mbuf != '+' && *mbuf != '-')
1883 * Only bans aren't bounced, it makes no sense to bounce last second
1884 * bans while propagating bans done before the net.rejoin. The reason
1885 * why I don't bounce net.rejoin bans is because it is too much
1886 * work to take care of too long strings adding the necessary TS to
1887 * net.burst bans -- RunLazy
1888 * We do have to check for *badop==2 now, we don't want HACKs to take
1891 * Since BURST - I *did* implement net.rejoin ban bouncing. So now it
1892 * certainly makes sense to also bounce 'last second' bans (bans done
1893 * after the net.junction). -- RunHardWorker
1895 if ((change = (whatt & MODE_ADD) &&
1896 !add_banid(sptr, chptr, cp, !bounce, !add_banid_called)))
1897 add_banid_called = 1;
1899 change = (whatt & MODE_DEL) && !del_banid(chptr, cp, !bounce);
1901 if (bounce && change)
1904 if ((whatt & MODE_ADD))
1906 if (bwhatt != MODE_DEL)
1912 else if ((whatt & MODE_DEL))
1914 if (bwhatt != MODE_ADD)
1926 nblen += strlen(cp);
1927 strcat(nbpbuf, " ");
1945 if (*mbuf != '+' && *mbuf != '-')
1952 } /* for (; i < opcnt; i++) */
1959 if (!hacknotice && *bmodebuf && chptr->creationtime)
1961 sendto_one(cptr, "%s " TOK_MODE " %s %s %s " TIME_T_FMT,
1962 NumServ(&me), chptr->chname, bmodebuf, nbparambuf,
1963 *badop == 2 ? (time_t) 0 : chptr->creationtime);
1965 /* If there are possibly bans to re-add, bounce them now */
1966 if (add_banid_called && bounce)
1968 struct SLink *ban[6]; /* Max 6 bans at a time */
1969 size_t len[6], sblen, total_len;
1970 int cnt, delayed = 0;
1971 while (delayed || (ban[0] = next_overlapped_ban()))
1973 len[0] = strlen(ban[0]->value.ban.banstr);
1974 cnt = 1; /* We already got one ban :) */
1975 sblen = sprintf_irc(sendbuf, ":%s MODE %s +b",
1976 me.name, chptr->chname) - sendbuf;
1977 total_len = sblen + 1 + len[0]; /* 1 = ' ' */
1978 /* Find more bans: */
1980 while (cnt < 6 && (ban[cnt] = next_overlapped_ban()))
1982 len[cnt] = strlen(ban[cnt]->value.ban.banstr);
1983 if (total_len + 5 + len[cnt] > BUFSIZE) /* 5 = "b \r\n\0" */
1985 delayed = cnt + 1; /* != 0 */
1988 sendbuf[sblen++] = 'b';
1989 total_len += 2 + len[cnt++]; /* 2 = "b " */
1993 sendbuf[sblen++] = ' ';
1994 strcpy(sendbuf + sblen, ban[cnt]->value.ban.banstr);
1997 sendbufto_one(cptr); /* Send bounce to uplink */
1999 ban[0] = ban[delayed - 1];
2002 /* Send -b's of overlapped bans to clients to keep them synchronized */
2003 if (add_banid_called && !bounce)
2006 char *banstr[6]; /* Max 6 bans at a time */
2007 size_t len[6], sblen, psblen, total_len;
2008 int cnt, delayed = 0;
2009 struct Membership* member_z;
2010 struct Client *acptr;
2012 psblen = sprintf_irc(sendbuf, ":%s MODE %s -b",
2013 sptr->name, chptr->chname) - sendbuf;
2014 else /* We rely on IsRegistered(sptr) being true for MODE */
2015 psblen = sprintf_irc(sendbuf, ":%s!%s@%s MODE %s -b", sptr->name,
2016 sptr->user->username, sptr->user->host, chptr->chname) - sendbuf;
2017 while (delayed || (ban = next_removed_overlapped_ban()))
2021 len[0] = strlen((banstr[0] = ban->value.ban.banstr));
2022 ban->value.ban.banstr = NULL;
2024 cnt = 1; /* We already got one ban :) */
2026 total_len = sblen + 1 + len[0]; /* 1 = ' ' */
2027 /* Find more bans: */
2029 while (cnt < 6 && (ban = next_removed_overlapped_ban()))
2031 len[cnt] = strlen((banstr[cnt] = ban->value.ban.banstr));
2032 ban->value.ban.banstr = NULL;
2033 if (total_len + 5 + len[cnt] > BUFSIZE) /* 5 = "b \r\n\0" */
2035 delayed = cnt + 1; /* != 0 */
2038 sendbuf[sblen++] = 'b';
2039 total_len += 2 + len[cnt++]; /* 2 = "b " */
2043 sendbuf[sblen++] = ' ';
2044 strcpy(sendbuf + sblen, banstr[cnt]);
2045 MyFree(banstr[cnt]);
2048 for (member_z = chptr->members; member_z; member_z = member_z->next_member) {
2049 acptr = member_z->user;
2050 if (MyConnect(acptr) && !IsZombie(member_z))
2051 sendbufto_one(acptr);
2055 banstr[0] = banstr[delayed - 1];
2056 len[0] = len[delayed - 1];
2061 return gotts ? 1 : -1;
2064 /* We are now treating the <key> part of /join <channel list> <key> as a key
2065 * ring; that is, we try one key against the actual channel key, and if that
2066 * doesn't work, we try the next one, and so on. -Kev -Texaco
2067 * Returns: 0 on match, 1 otherwise
2068 * This version contributed by SeKs <intru@info.polymtl.ca>
2070 static int compall(char *key, char *keyring)
2075 p1 = key; /* point to the key... */
2076 while (*p1 && *p1 == *keyring)
2077 { /* step through the key and ring until they
2083 if (!*p1 && (!*keyring || *keyring == ','))
2084 /* ok, if we're at the end of the and also at the end of one of the keys
2085 in the keyring, we have a match */
2088 if (!*keyring) /* if we're at the end of the key ring, there
2089 weren't any matches, so we return 1 */
2092 /* Not at the end of the key ring, so step
2093 through to the next key in the ring: */
2094 while (*keyring && *(keyring++) != ',');
2096 goto top; /* and check it against the key */
2099 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
2102 int overrideJoin = 0;
2105 * Now a banned user CAN join if invited -- Nemesi
2106 * Now a user CAN escape channel limit if invited -- bfriendly
2107 * Now a user CAN escape anything if invited -- Isomer
2110 for (lp = sptr->user->invited; lp; lp = lp->next)
2111 if (lp->value.chptr == chptr)
2114 #ifdef OPER_WALK_THROUGH_LMODES
2115 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
2116 a HACK(4) notice will be sent if he would not have been supposed
2117 to join normally. */
2118 if (IsOperOnLocalChannel(sptr,chptr->chname) && !BadPtr(key) && compall("OVERRIDE",key) == 0)
2120 overrideJoin = MAGIC_OPER_OVERRIDE;
2124 if (chptr->mode.mode & MODE_INVITEONLY)
2125 return overrideJoin + ERR_INVITEONLYCHAN;
2127 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
2128 return overrideJoin + ERR_CHANNELISFULL;
2130 if (is_banned(sptr, chptr, NULL))
2131 return overrideJoin + ERR_BANNEDFROMCHAN;
2134 * now using compall (above) to test against a whole key ring -Kev
2136 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
2137 return overrideJoin + ERR_BADCHANNELKEY;
2140 return ERR_DONTCHEAT;
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 * This helper function builds an argument string in strptr, consisting
2664 * of the original string, a space, and str1 and str2 concatenated (if,
2665 * of course, str2 is not NULL)
2668 build_string(char *strptr, int *strptr_i, char *str1, char *str2)
2670 strptr[(*strptr_i)++] = ' ';
2673 strptr[(*strptr_i)++] = *(str1++);
2677 strptr[(*strptr_i)++] = *(str2++);
2679 strptr[(*strptr_i)] = '\0';
2683 * This is the workhorse of our ModeBuf suite; this actually generates the
2684 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
2687 modebuf_flush_int(struct ModeBuf *mbuf, int all)
2689 /* we only need the flags that don't take args right now */
2690 static int flags[] = {
2691 /* MODE_CHANOP, 'o', */
2692 /* MODE_VOICE, 'v', */
2695 MODE_MODERATED, 'm',
2696 MODE_TOPICLIMIT, 't',
2697 MODE_INVITEONLY, 'i',
2698 MODE_NOPRIVMSGS, 'n',
2699 /* MODE_KEY, 'k', */
2700 /* MODE_BAN, 'b', */
2701 /* MODE_LIMIT, 'l', */
2707 struct Client *app_source; /* where the MODE appears to come from */
2709 char addbuf[20]; /* accumulates +psmtin, etc. */
2711 char rembuf[20]; /* accumulates -psmtin, etc. */
2713 char *bufptr; /* we make use of indirection to simplify the code */
2716 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
2718 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
2720 char *strptr; /* more indirection to simplify the code */
2723 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
2726 char limitbuf[20]; /* convert limits to strings */
2728 unsigned int limitdel = MODE_LIMIT;
2732 /* If the ModeBuf is empty, we have nothing to do */
2733 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
2736 /* Ok, if we were given the OPMODE flag, hide the source if its a user */
2737 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
2738 app_source = mbuf->mb_source->user->server;
2740 app_source = mbuf->mb_source;
2743 * Account for user we're bouncing; we have to get it in on the first
2744 * bounced MODE, or we could have problems
2746 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
2747 totalbuflen -= 6; /* numeric nick == 5, plus one space */
2749 /* Calculate the simple flags */
2750 for (flag_p = flags; flag_p[0]; flag_p += 2) {
2751 if (*flag_p & mbuf->mb_add)
2752 addbuf[addbuf_i++] = flag_p[1];
2753 else if (*flag_p & mbuf->mb_rem)
2754 rembuf[rembuf_i++] = flag_p[1];
2757 /* Now go through the modes with arguments... */
2758 for (i = 0; i < mbuf->mb_count; i++) {
2759 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
2761 bufptr_i = &addbuf_i;
2764 bufptr_i = &rembuf_i;
2767 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
2768 tmp = strlen(MB_CLIENT(mbuf, i)->name);
2770 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
2771 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
2773 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
2774 totalbuflen -= IRCD_MAX(5, tmp) + 1;
2776 } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN)) {
2777 tmp = strlen(MB_STRING(mbuf, i));
2779 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
2780 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
2782 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_KEY ? 'k' : 'b';
2783 totalbuflen -= tmp + 1;
2785 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
2786 /* if it's a limit, we also format the number */
2787 sprintf_irc(limitbuf, "%d", MB_UINT(mbuf, i));
2789 tmp = strlen(limitbuf);
2791 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
2792 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
2794 bufptr[(*bufptr_i)++] = 'l';
2795 totalbuflen -= tmp + 1;
2800 /* terminate the mode strings */
2801 addbuf[addbuf_i] = '\0';
2802 rembuf[rembuf_i] = '\0';
2804 /* If we're building a user visible MODE or HACK... */
2805 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
2806 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
2807 MODEBUF_DEST_LOG)) {
2808 /* Set up the parameter strings */
2814 for (i = 0; i < mbuf->mb_count; i++) {
2815 if (MB_TYPE(mbuf, i) & MODE_SAVE)
2818 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
2820 strptr_i = &addstr_i;
2823 strptr_i = &remstr_i;
2826 /* deal with clients... */
2827 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
2828 build_string(strptr, strptr_i, MB_CLIENT(mbuf, i)->name, 0);
2830 /* deal with strings... */
2831 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
2832 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0);
2835 * deal with limit; note we cannot include the limit parameter if we're
2838 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
2839 (MODE_ADD | MODE_LIMIT))
2840 build_string(strptr, strptr_i, limitbuf, 0);
2843 /* send the messages off to their destination */
2844 if (mbuf->mb_dest & MODEBUF_DEST_HACK2) {
2845 sendto_op_mask(SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s [" TIME_T_FMT
2846 "]", app_source->name, mbuf->mb_channel->chname,
2847 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", addbuf,
2848 remstr, addstr, mbuf->mb_channel->creationtime);
2849 sendto_serv_butone(mbuf->mb_connect, "%s " TOK_DESYNCH
2850 " :HACK: %s MODE %s %s%s%s%s%s%s [" TIME_T_FMT "]",
2851 NumServ(&me), app_source->name,
2852 mbuf->mb_channel->chname, rembuf_i ? "-" : "", rembuf,
2853 addbuf_i ? "+" : "", addbuf, remstr, addstr,
2854 mbuf->mb_channel->creationtime);
2857 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
2858 sendto_op_mask(SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s %s%s%s%s%s%s ["
2859 TIME_T_FMT "]", app_source->name,
2860 mbuf->mb_channel->chname, rembuf_i ? "-" : "", rembuf,
2861 addbuf_i ? "+" : "", addbuf, remstr, addstr,
2862 mbuf->mb_channel->creationtime);
2864 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
2865 sendto_op_mask(SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s [" TIME_T_FMT
2866 "]", app_source->name, mbuf->mb_channel->chname,
2867 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", addbuf,
2868 remstr, addstr, mbuf->mb_channel->creationtime);
2871 if (mbuf->mb_dest & MODEBUF_DEST_LOG) {
2872 if (IsServer(mbuf->mb_source))
2873 write_log(OPATH, TIME_T_FMT " %s OPMODE %s %s%s%s%s%s%s\n", TStime(),
2874 mbuf->mb_source->name, mbuf->mb_channel->chname,
2875 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "", addbuf,
2878 write_log(OPATH, TIME_T_FMT " %s!%s@%s OPMODE %s %s%s%s%s%s%s\n",
2879 TStime(), mbuf->mb_source->name,
2880 mbuf->mb_source->user->username, mbuf->mb_source->user->host,
2881 mbuf->mb_channel->chname, rembuf_i ? "-" : "", rembuf,
2882 addbuf_i ? "+" : "", addbuf, remstr, addstr);
2886 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
2887 sendto_channel_butserv(mbuf->mb_channel, app_source,
2888 ":%s MODE %s %s%s%s%s%s%s", app_source->name,
2889 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2890 rembuf, addbuf_i ? "+" : "", addbuf, remstr,
2894 /* Now are we supposed to propagate to other servers? */
2895 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
2896 /* set up parameter string */
2903 * limit is supressed if we're removing it; we have to figure out which
2904 * direction is the direction for it to be removed, though...
2906 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
2908 for (i = 0; i < mbuf->mb_count; i++) {
2909 if (MB_TYPE(mbuf, i) & MODE_SAVE)
2912 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
2914 strptr_i = &addstr_i;
2917 strptr_i = &remstr_i;
2920 /* deal with modes that take clients */
2921 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
2922 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)));
2924 /* deal with modes that take strings */
2925 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
2926 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0);
2929 * deal with the limit. Logic here is complicated; if HACK2 is set,
2930 * we're bouncing the mode, so sense is reversed, and we have to
2931 * include the original limit if it looks like it's being removed
2933 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
2934 build_string(strptr, strptr_i, limitbuf, 0);
2937 /* we were told to deop the source */
2938 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
2939 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
2940 addbuf[addbuf_i] = '\0'; /* terminate the string... */
2941 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source)); /* add user */
2943 /* mark that we've done this, so we don't do it again */
2944 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
2947 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
2948 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
2949 if (IsServer(mbuf->mb_source))
2950 sendto_serv_butone(mbuf->mb_connect, "%s " TOK_OPMODE
2951 " %s %s%s%s%s%s%s", NumServ(mbuf->mb_source),
2952 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2953 rembuf, addbuf_i ? "+" : "", addbuf, remstr,
2956 sendto_serv_butone(mbuf->mb_connect, "%s%s " TOK_OPMODE
2957 " %s %s%s%s%s%s%s", NumNick(mbuf->mb_source),
2958 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2959 rembuf, addbuf_i ? "+" : "", addbuf, remstr,
2961 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
2963 * If HACK2 was set, we're bouncing; we send the MODE back to the
2964 * connection we got it from with the senses reversed and a TS of 0;
2967 sendto_one(mbuf->mb_connect, "%s " TOK_MODE " %s %s%s%s%s%s%s "
2968 TIME_T_FMT, NumServ(&me), mbuf->mb_channel->chname,
2969 addbuf_i ? "-" : "", addbuf, rembuf_i ? "+" : "", rembuf,
2970 addstr, remstr, mbuf->mb_channel->creationtime);
2973 * We're propagating a normal MODE command to the rest of the network;
2974 * we send the actual channel TS unless this is a HACK3 or a HACK4
2976 if (IsServer(mbuf->mb_source))
2977 sendto_serv_butone(mbuf->mb_connect, "%s " TOK_MODE " %s %s%s%s%s%s%s "
2978 TIME_T_FMT, NumServ(mbuf->mb_source),
2979 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2980 rembuf, addbuf_i ? "+" : "", addbuf, remstr,
2981 addstr, (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
2982 mbuf->mb_channel->creationtime);
2984 sendto_serv_butone(mbuf->mb_connect, "%s%s " TOK_MODE
2985 " %s %s%s%s%s%s%s", NumNick(mbuf->mb_source),
2986 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2987 rembuf, addbuf_i ? "+" : "", addbuf, remstr,
2992 /* We've drained the ModeBuf... */
2997 /* reinitialize the mode-with-arg slots */
2998 for (i = 0; i < MAXMODEPARAMS; i++) {
2999 /* If we saved any, pack them down */
3000 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
3001 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
3002 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
3004 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
3006 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
3007 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
3009 MB_TYPE(mbuf, i) = 0;
3010 MB_UINT(mbuf, i) = 0;
3013 /* If we're supposed to flush it all, do so--all hail tail recursion */
3014 if (all && mbuf->mb_count)
3015 return modebuf_flush_int(mbuf, 1);
3021 * This routine just initializes a ModeBuf structure with the information
3022 * needed and the options given.
3025 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
3026 struct Client *connect, struct Channel *chan, unsigned int dest)
3031 assert(0 != source);
3037 mbuf->mb_source = source;
3038 mbuf->mb_connect = connect;
3039 mbuf->mb_channel = chan;
3040 mbuf->mb_dest = dest;
3043 /* clear each mode-with-parameter slot */
3044 for (i = 0; i < MAXMODEPARAMS; i++) {
3045 MB_TYPE(mbuf, i) = 0;
3046 MB_UINT(mbuf, i) = 0;
3051 * This routine simply adds modes to be added or deleted; do a binary OR
3052 * with either MODE_ADD or MODE_DEL
3055 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
3058 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3060 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
3061 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
3063 if (mode & MODE_ADD) {
3064 mbuf->mb_rem &= ~mode;
3065 mbuf->mb_add |= mode;
3067 mbuf->mb_add &= ~mode;
3068 mbuf->mb_rem |= mode;
3073 * This routine adds a mode to be added or deleted that takes a unsigned
3074 * int parameter; mode may *only* be the relevant mode flag ORed with one
3075 * of MODE_ADD or MODE_DEL
3078 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
3081 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3083 MB_TYPE(mbuf, mbuf->mb_count) = mode;
3084 MB_UINT(mbuf, mbuf->mb_count) = uint;
3086 /* when we've reached the maximal count, flush the buffer */
3087 if (++mbuf->mb_count >=
3088 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
3089 modebuf_flush_int(mbuf, 0);
3093 * This routine adds a mode to be added or deleted that takes a string
3094 * parameter; mode may *only* be the relevant mode flag ORed with one of
3095 * MODE_ADD or MODE_DEL
3098 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
3102 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3104 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
3105 MB_STRING(mbuf, mbuf->mb_count) = string;
3107 /* when we've reached the maximal count, flush the buffer */
3108 if (++mbuf->mb_count >=
3109 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
3110 modebuf_flush_int(mbuf, 0);
3114 * This routine adds a mode to be added or deleted that takes a client
3115 * parameter; mode may *only* be the relevant mode flag ORed with one of
3116 * MODE_ADD or MODE_DEL
3119 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
3120 struct Client *client)
3123 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3125 MB_TYPE(mbuf, mbuf->mb_count) = mode;
3126 MB_CLIENT(mbuf, mbuf->mb_count) = client;
3128 /* when we've reached the maximal count, flush the buffer */
3129 if (++mbuf->mb_count >=
3130 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
3131 modebuf_flush_int(mbuf, 0);
3135 * This is the exported binding for modebuf_flush()
3138 modebuf_flush(struct ModeBuf *mbuf)
3140 return modebuf_flush_int(mbuf, 1);
3144 * Simple function to invalidate bans
3147 mode_ban_invalidate(struct Channel *chan)
3149 struct Membership *member;
3151 for (member = chan->members; member; member = member->next_member)
3152 ClearBanValid(member);
3156 * Simple function to drop invite structures
3159 mode_invite_clear(struct Channel *chan)
3161 while (chan->invites)
3162 del_invite(chan->invites->value.cptr, chan);
3165 /* What we've done for mode_parse so far... */
3166 #define DONE_LIMIT 0x01 /* We've set the limit */
3167 #define DONE_KEY 0x02 /* We've set the key */
3168 #define DONE_BANLIST 0x04 /* We've sent the ban list */
3169 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
3170 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
3173 struct ModeBuf *mbuf;
3174 struct Client *cptr;
3175 struct Client *sptr;
3176 struct Channel *chptr;
3185 struct SLink banlist[MAXPARA];
3188 struct Client *client;
3189 } cli_change[MAXPARA];
3193 * Here's a helper function to deal with sending along "Not oper" or
3194 * "Not member" messages
3197 send_notoper(struct ParseState *state)
3199 if (state->done & DONE_NOTOPER)
3202 sendto_one(state->sptr, err_str(state->flags & MODE_PARSE_NOTOPER ?
3203 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL),
3204 me.name, state->sptr->name, state->chptr->chname);
3206 state->done |= DONE_NOTOPER;
3210 * Helper function to convert limits
3213 mode_parse_limit(struct ParseState *state, int *flag_p)
3215 unsigned int t_limit;
3217 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
3218 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
3221 if (state->parc <= 0) { /* warn if not enough args */
3222 if (MyUser(state->sptr))
3223 need_more_params(state->sptr, "MODE +l");
3227 t_limit = atoi(state->parv[state->args_used++]); /* grab arg */
3231 if (!t_limit) /* if it was zero, ignore it */
3234 t_limit = state->chptr->mode.limit;
3236 /* If they're not an oper, they can't change modes */
3237 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3238 send_notoper(state);
3242 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
3244 state->done |= DONE_LIMIT;
3246 assert(0 != state->mbuf);
3248 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
3250 if (state->flags & MODE_PARSE_SET) { /* set the limit */
3251 if (state->dir & MODE_ADD) {
3252 state->chptr->mode.mode |= flag_p[0];
3253 state->chptr->mode.limit = t_limit;
3255 state->chptr->mode.mode &= flag_p[0];
3256 state->chptr->mode.limit = 0;
3262 * Helper function to convert keys
3265 mode_parse_key(struct ParseState *state, int *flag_p)
3270 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3273 if (state->parc <= 0) { /* warn if not enough args */
3274 if (MyUser(state->sptr))
3275 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
3280 t_str = state->parv[state->args_used++]; /* grab arg */
3284 /* If they're not an oper, they can't change modes */
3285 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3286 send_notoper(state);
3290 if (state->done & DONE_KEY) /* allow key to be set only once */
3292 state->done |= DONE_KEY;
3296 /* clean up the key string */
3298 while (*++s > ' ' && *s != ':' && --t_len)
3302 if (!*t_str) { /* warn if empty */
3303 if (MyUser(state->sptr))
3304 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
3309 /* can't add a key if one is set, nor can one remove the wrong key */
3310 if (!(state->flags & MODE_PARSE_FORCE))
3311 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
3312 (state->dir == MODE_DEL &&
3313 ircd_strcmp(state->chptr->mode.key, t_str))) {
3314 sendto_one(state->sptr, err_str(ERR_KEYSET), me.name, state->sptr->name,
3315 state->chptr->chname);
3319 assert(0 != state->mbuf);
3321 if (state->flags & MODE_PARSE_BOUNCE) {
3322 if (*state->chptr->mode.key) /* reset old key */
3323 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
3324 state->chptr->mode.key, 0);
3325 else /* remove new bogus key */
3326 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
3327 } else /* send new key */
3328 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
3330 if (state->flags & MODE_PARSE_SET) {
3331 if (state->dir == MODE_ADD) /* set the new key */
3332 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
3333 else /* remove the old key */
3334 *state->chptr->mode.key = '\0';
3339 * Helper function to convert bans
3342 mode_parse_ban(struct ParseState *state, int *flag_p)
3345 struct SLink *ban, *newban = 0;
3347 if (state->parc <= 0) { /* Not enough args, send ban list */
3348 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
3349 send_ban_list(state->sptr, state->chptr);
3350 state->done |= DONE_BANLIST;
3356 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3359 t_str = state->parv[state->args_used++]; /* grab arg */
3363 /* If they're not an oper, they can't change modes */
3364 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3365 send_notoper(state);
3369 if ((s = strchr(t_str, ' ')))
3372 if (!*t_str || *t_str == ':') { /* warn if empty */
3373 if (MyUser(state->sptr))
3374 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
3379 t_str = collapse(pretty_mask(t_str));
3381 /* remember the ban for the moment... */
3382 if (state->dir == MODE_ADD) {
3383 newban = state->banlist + (state->numbans++);
3386 DupString(newban->value.ban.banstr, t_str);
3387 newban->value.ban.who = state->sptr->name;
3388 newban->value.ban.when = TStime();
3390 newban->flags = CHFL_BAN | MODE_ADD;
3392 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
3393 newban->flags |= CHFL_BAN_IPMASK;
3396 if (!state->chptr->banlist) {
3397 state->chptr->banlist = newban; /* add our ban with its flags */
3398 state->done |= DONE_BANCLEAN;
3402 /* Go through all bans */
3403 for (ban = state->chptr->banlist; ban; ban = ban->next) {
3404 /* first, clean the ban flags up a bit */
3405 if (!(state->done & DONE_BANCLEAN))
3406 /* Note: We're overloading *lots* of bits here; be careful! */
3407 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
3411 * MODE_ADD - Ban was added; if we're bouncing modes,
3412 * then we'll remove it below; otherwise,
3413 * we'll have to allocate a real ban
3415 * MODE_DEL - Ban was marked for deletion; if we're
3416 * bouncing modes, we'll have to re-add it,
3417 * otherwise, we'll have to remove it
3419 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
3420 * with a ban already set; if we're
3421 * bouncing modes, we'll have to bounce
3422 * this one; otherwise, we'll just ignore
3423 * it when we process added bans
3426 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
3427 ban->flags |= MODE_DEL; /* delete one ban */
3429 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
3431 } else if (state->dir == MODE_ADD) {
3432 /* if the ban already exists, don't worry about it */
3433 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
3434 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
3437 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
3438 if (!(ban->flags & MODE_DEL))
3439 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
3440 } else if (!mmatch(t_str, ban->value.ban.banstr))
3441 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
3444 ban->next = newban; /* add our ban with its flags */
3445 break; /* get out of loop */
3449 state->done |= DONE_BANCLEAN;
3453 * This is the bottom half of the ban processor
3456 mode_process_bans(struct ParseState *state)
3458 struct SLink *ban, *newban, *prevban, *nextban;
3464 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
3466 banlen = strlen(ban->value.ban.banstr);
3468 nextban = ban->next;
3470 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
3472 prevban->next = 0; /* Break the list; ban isn't a real ban */
3474 state->chptr->banlist = 0;
3479 MyFree(ban->value.ban.banstr);
3482 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
3483 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
3484 ban->value.ban.banstr,
3485 state->flags & MODE_PARSE_SET);
3487 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
3488 if (prevban) /* clip it out of the list... */
3489 prevban->next = ban->next;
3491 state->chptr->banlist = ban->next;
3496 MyFree(ban->value.ban.who);
3500 continue; /* next ban; keep prevban like it is */
3502 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
3503 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
3505 prevban->next = 0; /* Break the list; ban isn't a real ban */
3507 state->chptr->banlist = 0;
3509 /* If we're supposed to ignore it, do so. */
3510 if (ban->flags & CHFL_BAN_OVERLAPPED &&
3511 !(state->flags & MODE_PARSE_BOUNCE)) {
3515 MyFree(ban->value.ban.banstr);
3517 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
3518 (len > MAXBANLENGTH || count >= MAXBANS)) {
3519 send_error_to_client(state->sptr, ERR_BANLISTFULL,
3520 state->chptr->chname, ban->value.ban.banstr);
3524 MyFree(ban->value.ban.banstr);
3526 /* add the ban to the buffer */
3527 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
3528 ban->value.ban.banstr,
3529 !(state->flags & MODE_PARSE_SET));
3531 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
3532 newban = make_link();
3533 newban->value.ban.banstr = ban->value.ban.banstr;
3534 DupString(newban->value.ban.who, ban->value.ban.who);
3535 newban->value.ban.when = ban->value.ban.when;
3536 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
3538 newban->next = state->chptr->banlist; /* and link it in */
3539 state->chptr->banlist = newban;
3548 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
3550 if (changed) /* if we changed the ban list, we must invalidate the bans */
3551 mode_ban_invalidate(state->chptr);
3555 * Helper function to process client changes
3558 mode_parse_client(struct ParseState *state, int *flag_p)
3561 struct Client *acptr;
3564 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3567 if (state->parc <= 0) { /* warn if not enough args */
3568 if (MyUser(state->sptr))
3569 need_more_params(state->sptr, state->dir == MODE_ADD ?
3570 (flag_p[0] == MODE_CHANOP ? "MODE +o" : "MODE +v") :
3571 (flag_p[0] == MODE_CHANOP ? "MODE -o" : "MODE -v"));
3575 t_str = state->parv[state->args_used++]; /* grab arg */
3579 /* If they're not an oper, they can't change modes */
3580 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3581 send_notoper(state);
3585 if (MyUser(state->sptr)) /* find client we're manipulating */
3586 acptr = find_chasing(state->sptr, t_str, NULL);
3588 acptr = findNUser(t_str);
3590 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
3591 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
3592 state->cli_change[i].flag & flag_p[0]))
3593 break; /* found a slot */
3595 /* Store what we're doing to them */
3596 state->cli_change[i].flag = state->dir | flag_p[0];
3597 state->cli_change[i].client = acptr;
3601 * Helper function to process the changed client list
3604 mode_process_clients(struct ParseState *state)
3607 struct Membership *member;
3609 for (i = 0; state->cli_change[i].flag; i++) {
3610 assert(0 != state->cli_change[i].client);
3612 /* look up member link */
3613 if (!(member = find_member_link(state->chptr,
3614 state->cli_change[i].client)) ||
3615 (MyUser(state->sptr) && IsZombie(member))) {
3616 if (MyUser(state->sptr))
3617 sendto_one(state->sptr, err_str(ERR_USERNOTINCHANNEL), me.name,
3618 state->sptr->name, state->cli_change[i].client->name,
3619 state->chptr->chname);
3623 if ((state->cli_change[i].flag & MODE_ADD &&
3624 (state->cli_change[i].flag & member->status)) ||
3625 (state->cli_change[i].flag & MODE_DEL &&
3626 !(state->cli_change[i].flag & member->status)))
3627 continue; /* no change made, don't do anything */
3629 /* see if the deop is allowed */
3630 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
3631 (MODE_DEL | MODE_CHANOP)) {
3632 /* prevent +k users from being deopped */
3633 if (IsChannelService(state->cli_change[i].client)) {
3634 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
3635 sendto_op_mask(SNO_HACK4, ":%s NOTICE * :*** Notice -- "
3636 "Deop of +k user on %s by %s",me.name,
3637 state->chptr->chname,
3638 (IsServer(state->sptr) ? state->sptr->name :
3639 state->sptr->user->server->name));
3641 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
3642 sendto_one(state->sptr, err_str(ERR_ISCHANSERVICE), me.name,
3643 state->sptr->name, state->cli_change[i].client->name,
3644 state->chptr->chname);
3649 #ifdef NO_OPER_DEOP_LCHAN
3650 /* don't allow local opers to be deopped on local channels */
3651 if (MyUser(state->sptr) && state->cli_change[i].client != state->sptr &&
3652 IsOperOnLocalChannel(state->cli_change[i].client,
3653 state->chptr->chname)) {
3654 sendto_one(state->sptr, err_str(ERR_ISOPERLCHAN), me.name,
3655 state->sptr->name, state->cli_change[i].client->name,
3656 state->chptr->chname);
3662 /* accumulate the change */
3663 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
3664 state->cli_change[i].client);
3666 /* actually effect the change */
3667 if (state->flags & MODE_PARSE_SET) {
3668 if (state->cli_change[i].flag & MODE_ADD) {
3669 member->status |= (state->cli_change[i].flag &
3670 (MODE_CHANOP | MODE_VOICE));
3671 if (state->cli_change[i].flag & MODE_CHANOP)
3672 ClearDeopped(member);
3674 member->status &= ~(state->cli_change[i].flag &
3675 (MODE_CHANOP | MODE_VOICE));
3677 } /* for (i = 0; state->cli_change[i].flags; i++) { */
3681 * Helper function to process the simple modes
3684 mode_parse_mode(struct ParseState *state, int *flag_p)
3686 if ((state->dir == MODE_ADD && (flag_p[0] & state->chptr->mode.mode)) ||
3687 (state->dir == MODE_DEL && !(flag_p[0] & state->chptr->mode.mode)))
3688 return; /* no change */
3690 /* If they're not an oper, they can't change modes */
3691 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3692 send_notoper(state);
3696 assert(0 != state->mbuf);
3698 modebuf_mode(state->mbuf, state->dir | flag_p[0]);
3700 /* make +p and +s mutually exclusive */
3701 if (state->dir == MODE_ADD && flag_p[0] & (MODE_SECRET | MODE_PRIVATE)) {
3702 if (flag_p[0] == MODE_SECRET && (state->chptr->mode.mode & MODE_PRIVATE))
3703 modebuf_mode(state->mbuf, MODE_DEL | MODE_PRIVATE);
3704 else if (flag_p[0] == MODE_PRIVATE &&
3705 (state->chptr->mode.mode & MODE_SECRET))
3706 modebuf_mode(state->mbuf, MODE_DEL | MODE_SECRET);
3709 if (state->flags & MODE_PARSE_SET) { /* set the flags */
3710 if (state->dir == MODE_ADD) { /* add the mode to the channel */
3711 state->chptr->mode.mode |= flag_p[0];
3713 /* make +p and +s mutually exclusive */
3714 if (state->dir == MODE_ADD && flag_p[0] & (MODE_SECRET | MODE_PRIVATE)) {
3715 if (flag_p[0] == MODE_PRIVATE)
3716 state->chptr->mode.mode &= ~MODE_SECRET;
3718 state->chptr->mode.mode &= ~MODE_PRIVATE;
3720 } else /* remove the mode from the channel */
3721 state->chptr->mode.mode &= ~flag_p[0];
3724 /* Clear out invite structures if we're removing invites */
3725 if (state->flags & MODE_PARSE_SET && state->dir == MODE_DEL &&
3726 flag_p[0] == MODE_INVITEONLY)
3727 mode_invite_clear(state->chptr);
3731 * This routine is intended to parse MODE or OPMODE commands and effect the
3732 * changes (or just build the bounce buffer). We pass the starting offset
3736 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
3737 struct Channel *chptr, int parc, char *parv[], unsigned int flags)
3739 static int chan_flags[] = {
3744 MODE_MODERATED, 'm',
3745 MODE_TOPICLIMIT, 't',
3746 MODE_INVITEONLY, 'i',
3747 MODE_NOPRIVMSGS, 'n',
3758 struct ParseState state;
3769 state.chptr = chptr;
3772 state.flags = flags;
3773 state.dir = MODE_ADD;
3775 state.args_used = 0;
3776 state.max_args = MAXMODEPARAMS;
3779 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
3780 state.banlist[i].next = 0;
3781 state.banlist[i].value.ban.banstr = 0;
3782 state.banlist[i].value.ban.who = 0;
3783 state.banlist[i].value.ban.when = 0;
3784 state.banlist[i].flags = 0;
3785 state.cli_change[i].flag = 0;
3786 state.cli_change[i].client = 0;
3789 modestr = state.parv[state.args_used++];
3793 for (; *modestr; modestr++) {
3794 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
3795 if (flag_p[1] == *modestr)
3798 if (!flag_p[0]) { /* didn't find it? complain and continue */
3799 if (MyUser(state.sptr))
3800 sendto_one(state.sptr, err_str(ERR_UNKNOWNMODE), me.name,
3801 state.sptr->name, *modestr);
3806 case '+': /* switch direction to MODE_ADD */
3807 case '-': /* switch direction to MODE_DEL */
3808 state.dir = flag_p[0];
3811 case 'l': /* deal with limits */
3812 mode_parse_limit(&state, flag_p);
3815 case 'k': /* deal with keys */
3816 mode_parse_key(&state, flag_p);
3819 case 'b': /* deal with bans */
3820 mode_parse_ban(&state, flag_p);
3823 case 'o': /* deal with ops/voice */
3825 mode_parse_client(&state, flag_p);
3828 default: /* deal with other modes */
3829 mode_parse_mode(&state, flag_p);
3831 } /* switch (*modestr) { */
3832 } /* for (; *modestr; modestr++) { */
3834 if (state.parc > 0) { /* process next argument in string */
3835 modestr = state.parv[state.args_used++];
3839 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
3842 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
3843 break; /* we're then going to bounce the mode! */
3845 recv_ts = atoi(modestr);
3847 if (recv_ts && recv_ts < state.chptr->creationtime)
3848 state.chptr->creationtime = recv_ts; /* respect earlier TS */
3850 break; /* break out of while loop */
3851 } else if (state.flags & MODE_PARSE_STRICT ||
3852 (MyUser(state.sptr) && state.max_args <= 0)) {
3853 state.parc++; /* we didn't actually gobble the argument */
3855 break; /* break out of while loop */
3858 } /* while (*modestr) { */
3861 * the rest of the function finishes building resultant MODEs; if the
3862 * origin isn't a member or an oper, skip it.
3864 if (state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3865 return state.args_used; /* tell our parent how many args we gobbled */
3867 assert(0 != state.mbuf);
3869 if (state.done & DONE_BANCLEAN) /* process bans */
3870 mode_process_bans(&state);
3872 /* process client changes */
3873 if (state.cli_change[0].flag)
3874 mode_process_clients(&state);
3876 return state.args_used; /* tell our parent how many args we gobbled */