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 send_reply(sptr, ERR_NOSUCHNICK, 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, /* XXX DEAD */
197 from, name, mode, param, creationtime);
199 sendto_one(cptr, ":%s MODE %s %s %s", from, name, mode, param); /* XXX DEAD */
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 send_reply(cptr, ERR_BANLISTFULL, chptr->chname, banid);
370 struct Membership* member;
372 ban->next = chptr->banlist;
374 ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
375 assert(0 != ban->value.ban.banstr);
376 strcpy(ban->value.ban.banstr, banid);
378 ban->value.ban.who = (char*) MyMalloc(strlen(cptr->name) + 1);
379 assert(0 != ban->value.ban.who);
380 strcpy(ban->value.ban.who, cptr->name);
382 ban->value.ban.when = TStime();
383 ban->flags = CHFL_BAN; /* This bit is never used I think... */
384 if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
385 ban->flags |= CHFL_BAN_IPMASK;
386 chptr->banlist = ban;
389 * Erase ban-valid-bit
391 for (member = chptr->members; member; member = member->next_member)
392 ClearBanValid(member); /* `ban' == channel member ! */
397 static struct SLink *next_overlapped_ban(void)
399 struct SLink *tmp = next_ban;
403 for (ban = tmp->next; ban; ban = ban->next)
404 if ((ban->flags & CHFL_BAN_OVERLAPPED))
411 struct SLink *next_removed_overlapped_ban(void)
413 struct SLink *tmp = removed_bans_list;
416 if (prev_ban->value.ban.banstr) /* Can be set to NULL in set_mode() */
417 MyFree(prev_ban->value.ban.banstr);
418 MyFree(prev_ban->value.ban.who);
423 removed_bans_list = removed_bans_list->next;
431 * If `change' is true, delete `banid' from channel `chptr'.
432 * Returns `false' if removal was (or would have been) successful.
434 static int del_banid(struct Channel *chptr, char *banid, int change)
441 for (ban = &(chptr->banlist); *ban; ban = &((*ban)->next)) {
442 if (0 == ircd_strcmp(banid, (*ban)->value.ban.banstr))
447 struct Membership* member;
449 MyFree(tmp->value.ban.banstr);
450 MyFree(tmp->value.ban.who);
453 * Erase ban-valid-bit, for channel members that are banned
455 for (member = chptr->members; member; member = member->next_member)
456 if (CHFL_BANVALIDMASK == (member->status & CHFL_BANVALIDMASK))
457 ClearBanValid(member); /* `tmp' == channel member */
466 * find_channel_member - returns Membership * if a person is joined and not a zombie
468 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
470 struct Membership* member;
473 member = find_member_link(chptr, cptr);
474 return (member && !IsZombie(member)) ? member : 0;
478 * is_banned - a non-zero value if banned else 0.
480 static int is_banned(struct Client *cptr, struct Channel *chptr,
481 struct Membership* member)
490 if (member && IsBanValid(member))
491 return IsBanned(member);
493 s = make_nick_user_host(cptr->name, cptr->user->username, cptr->user->host);
495 for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
496 if ((tmp->flags & CHFL_BAN_IPMASK)) {
498 ip_s = make_nick_user_ip(cptr->name, cptr->user->username, cptr->ip);
499 if (match(tmp->value.ban.banstr, ip_s) == 0)
502 else if (match(tmp->value.ban.banstr, s) == 0)
518 return (tmp != NULL);
522 * adds a user to a channel by adding another link to the channels member
525 void add_user_to_channel(struct Channel* chptr, struct Client* who,
532 struct Membership* member =
533 (struct Membership*) MyMalloc(sizeof(struct Membership));
536 member->channel = chptr;
537 member->status = flags;
539 member->next_member = chptr->members;
540 if (member->next_member)
541 member->next_member->prev_member = member;
542 member->prev_member = 0;
543 chptr->members = member;
545 member->next_channel = who->user->channel;
546 if (member->next_channel)
547 member->next_channel->prev_channel = member;
548 member->prev_channel = 0;
549 who->user->channel = member;
556 static int remove_member_from_channel(struct Membership* member)
558 struct Channel* chptr;
560 chptr = member->channel;
562 * unlink channel member list
564 if (member->next_member)
565 member->next_member->prev_member = member->prev_member;
566 if (member->prev_member)
567 member->prev_member->next_member = member->next_member;
569 member->channel->members = member->next_member;
572 * unlink client channel list
574 if (member->next_channel)
575 member->next_channel->prev_channel = member->prev_channel;
576 if (member->prev_channel)
577 member->prev_channel->next_channel = member->next_channel;
579 member->user->user->channel = member->next_channel;
581 --member->user->user->joined;
584 return sub1_from_channel(chptr);
587 static int channel_all_zombies(struct Channel* chptr)
589 struct Membership* member;
591 for (member = chptr->members; member; member = member->next_member) {
592 if (!IsZombie(member))
599 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
602 struct Membership* member;
605 if ((member = find_member_link(chptr, cptr))) {
606 if (remove_member_from_channel(member)) {
607 if (channel_all_zombies(chptr)) {
609 * XXX - this looks dangerous but isn't if we got the referential
610 * integrity right for channels
612 while (remove_member_from_channel(chptr->members))
619 void remove_user_from_all_channels(struct Client* cptr)
621 struct Membership* chan;
623 assert(0 != cptr->user);
625 while ((chan = cptr->user->channel))
626 remove_user_from_channel(cptr, chan->channel);
629 int is_chan_op(struct Client *cptr, struct Channel *chptr)
631 struct Membership* member;
633 if ((member = find_member_link(chptr, cptr)))
634 return (!IsZombie(member) && IsChanOp(member));
639 static int is_deopped(struct Client *cptr, struct Channel *chptr)
641 struct Membership* member;
644 if ((member = find_member_link(chptr, cptr)))
645 return IsDeopped(member);
647 return (IsUser(cptr) ? 1 : 0);
650 int is_zombie(struct Client *cptr, struct Channel *chptr)
652 struct Membership* member;
656 if ((member = find_member_link(chptr, cptr)))
657 return IsZombie(member);
661 int has_voice(struct Client* cptr, struct Channel* chptr)
663 struct Membership* member;
666 if ((member = find_member_link(chptr, cptr)))
667 return (!IsZombie(member) && HasVoice(member));
672 int member_can_send_to_channel(struct Membership* member)
676 if (IsVoicedOrOpped(member))
679 * If it's moderated, and you aren't a priviledged user, you can't
682 if (member->channel->mode.mode & MODE_MODERATED)
685 * If you're banned then you can't speak either.
686 * but because of the amount of CPU time that is_banned chews
687 * we only check it for our clients.
689 if (MyUser(member->user) && is_banned(member->user, member->channel, member))
694 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
696 struct Membership *member;
699 * Servers can always speak on channels.
704 member = find_channel_member(cptr, chptr);
707 * You can't speak if your off channel, if the channel is modeless, or
708 * +n.(no external messages)
711 if ((chptr->mode.mode & MODE_NOPRIVMSGS) || IsModelessChannel(chptr->chname))
716 return member_can_send_to_channel(member);
720 * find_no_nickchange_channel
721 * if a member and not opped or voiced and banned
722 * return the name of the first channel banned on
724 const char* find_no_nickchange_channel(struct Client* cptr)
727 struct Membership* member;
728 for (member = cptr->user->channel; member; member = member->next_channel) {
729 if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
730 return member->channel->chname;
738 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
739 * with the parameters in pbuf.
741 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
742 struct Channel *chptr)
749 if (chptr->mode.mode & MODE_SECRET)
751 else if (chptr->mode.mode & MODE_PRIVATE)
753 if (chptr->mode.mode & MODE_MODERATED)
755 if (chptr->mode.mode & MODE_TOPICLIMIT)
757 if (chptr->mode.mode & MODE_INVITEONLY)
759 if (chptr->mode.mode & MODE_NOPRIVMSGS)
761 if (chptr->mode.limit) {
763 sprintf_irc(pbuf, "%d", chptr->mode.limit);
766 if (*chptr->mode.key) {
768 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
769 if (chptr->mode.limit)
771 strcat(pbuf, chptr->mode.key);
778 static int send_mode_list(struct Client *cptr, char *chname,
779 time_t creationtime, struct SLink *top,
789 cp = modebuf + strlen(modebuf);
790 if (*parabuf) /* mode +l or +k xx */
792 for (lp = top; lp; lp = lp->next)
794 if (!(lp->flags & mask))
796 if (mask == CHFL_BAN)
797 name = lp->value.ban.banstr;
799 name = lp->value.cptr->name;
800 if (strlen(parabuf) + strlen(name) + 11 < MODEBUFLEN)
802 strcat(parabuf, " ");
803 strcat(parabuf, name);
814 /* cptr is always a server! So we send creationtimes */
815 sendmodeto_one(cptr, me.name, chname, modebuf, parabuf, creationtime);
823 strcpy(parabuf, name);
836 * send "cptr" a full list of the modes for channel chptr.
838 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
840 static unsigned int current_flags[4] =
841 { 0, CHFL_CHANOP | CHFL_VOICE, CHFL_VOICE, CHFL_CHANOP };
848 struct Membership* member;
850 char modebuf[MODEBUFLEN];
851 char parabuf[MODEBUFLEN];
856 if (IsLocalChannel(chptr->chname))
859 member = chptr->members;
860 lp2 = chptr->banlist;
862 *modebuf = *parabuf = '\0';
863 channel_modes(cptr, modebuf, parabuf, chptr);
865 for (first = 1; full; first = 0) /* Loop for multiple messages */
867 full = 0; /* Assume by default we get it
868 all in one message */
870 /* (Continued) prefix: "<Y> B <channel> <TS>" */
871 sprintf_irc(sendbuf, "%s B %s " TIME_T_FMT, NumServ(&me),
872 chptr->chname, chptr->creationtime);
873 sblen = strlen(sendbuf);
875 if (first && modebuf[1]) /* Add simple modes (iklmnpst)
878 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
879 sendbuf[sblen++] = ' ';
880 strcpy(sendbuf + sblen, modebuf);
881 sblen += strlen(modebuf);
884 sendbuf[sblen++] = ' ';
885 strcpy(sendbuf + sblen, parabuf);
886 sblen += strlen(parabuf);
891 * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
893 * Run 4 times over all members, to group the members with the
896 for (first = 1; flag_cnt < 4;
897 member = chptr->members, new_mode = 1, flag_cnt++)
899 for (; member; member = member->next_member)
901 if ((member->status & CHFL_VOICED_OR_OPPED) !=
902 current_flags[flag_cnt])
903 continue; /* Skip members with different flags */
904 if (sblen + NUMNICKLEN + 4 > BUFSIZE - 3)
905 /* The 4 is a possible ",:ov"
906 The -3 is for the "\r\n\0" that is added in send.c */
908 full = 1; /* Make sure we continue after
910 new_mode = 1; /* Ensure the new BURST line contains the current
912 break; /* Do not add this member to this message */
914 sendbuf[sblen++] = first ? ' ' : ',';
915 first = 0; /* From now on, us comma's to add new nicks */
917 sprintf_irc(sendbuf + sblen, "%s%s", NumNick(member->user));
918 sblen += strlen(sendbuf + sblen);
920 * Do we have a nick with a new mode ?
921 * Or are we starting a new BURST line?
926 if (IsVoicedOrOpped(member)) {
927 sendbuf[sblen++] = ':';
928 if (IsChanOp(member))
929 sendbuf[sblen++] = 'o';
930 if (HasVoice(member))
931 sendbuf[sblen++] = 'v';
941 /* Attach all bans, space seperated " :%ban ban ..." */
942 for (first = 2; lp2; lp2 = lp2->next)
944 len = strlen(lp2->value.ban.banstr);
945 if (sblen + len + 1 + first > BUFSIZE - 3)
946 /* The +1 stands for the added ' '.
947 * The +first stands for the added ":%".
948 * The -3 is for the "\r\n\0" that is added in send.c
957 sendbuf[sblen++] = ' ';
958 sendbuf[sblen++] = ':'; /* Will be last parameter */
959 sendbuf[sblen++] = '%'; /* To tell bans apart */
962 sendbuf[sblen++] = ' ';
963 strcpy(sendbuf + sblen, lp2->value.ban.banstr);
968 sendbuf[sblen] = '\0';
969 sendbufto_one(cptr); /* Send this message */
970 } /* Continue when there was something
971 that didn't fit (full==1) */
977 * by Carlo Wood (Run), 05 Oct 1998.
981 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
982 * When the user name or host name are too long (USERLEN and HOSTLEN
983 * respectively) then they are cut off at the start with a '*'.
985 * The following transformations are made:
988 * 2) xxx.xxx -> *!*@host
989 * 3) xxx!yyy -> nick!user@*
990 * 4) xxx@yyy -> *!user@host
991 * 5) xxx!yyy@zzz -> nick!user@host
993 char *pretty_mask(char *mask)
995 static char star[2] = { '*', 0 };
996 char *last_dot = NULL;
999 /* Case 1: default */
1004 /* Do a _single_ pass through the characters of the mask: */
1005 for (ptr = mask; *ptr; ++ptr)
1009 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1013 else if (*ptr == '@')
1015 /* Case 4: Found last '@' (without finding a '!' yet) */
1020 else if (*ptr == '.')
1022 /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
1032 /* Case 4 or 5: Found last '@' */
1038 if (user == star && last_dot)
1048 char *nick_end = (user != star) ? user - 1 : ptr;
1049 if (nick_end - nick > NICKLEN)
1055 char *user_end = (host != star) ? host - 1 : ptr;
1056 if (user_end - user > USERLEN)
1058 user = user_end - USERLEN;
1063 if (host != star && ptr - host > HOSTLEN)
1065 host = ptr - HOSTLEN;
1068 return make_nick_user_host(nick, user, host);
1071 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1078 for (lp = chptr->banlist; lp; lp = lp->next)
1079 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->value.ban.banstr,
1080 lp->value.ban.who, lp->value.ban.when);
1082 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1086 * Check and try to apply the channel modes passed in the parv array for
1087 * the client ccptr to channel chptr. The resultant changes are printed
1088 * into mbuf and pbuf (if any) and applied to the channel.
1090 int set_mode(struct Client* cptr, struct Client* sptr,
1091 struct Channel* chptr, int parc, char* parv[],
1092 char* mbuf, char* pbuf, char* npbuf, int* badop)
1095 * This size is only needed when a broken
1096 * server sends more then MAXMODEPARAMS
1099 static struct SLink chops[MAXPARA - 2];
1100 static int flags[] = {
1103 MODE_MODERATED, 'm',
1104 MODE_NOPRIVMSGS, 'n',
1105 MODE_TOPICLIMIT, 't',
1106 MODE_INVITEONLY, 'i',
1112 char bmodebuf[MODEBUFLEN];
1113 char bparambuf[MODEBUFLEN];
1114 char nbparambuf[MODEBUFLEN]; /* "Numeric" Bounce Parameter Buffer */
1116 char* curr = parv[0];
1119 struct Membership* member_x;
1120 struct Membership* member_y;
1121 unsigned int whatt = MODE_ADD;
1122 unsigned int bwhatt = 0;
1125 int add_banid_called = 0;
1131 unsigned int nusers = 0;
1132 unsigned int newmode;
1143 static char numeric[16];
1144 char* bmbuf = bmodebuf;
1145 char* bpbuf = bparambuf;
1146 char* nbpbuf = nbparambuf;
1148 struct ConfItem* aconf;
1150 *mbuf = *pbuf = *npbuf = *bmbuf = *bpbuf = *nbpbuf = '\0';
1155 * Mode is accepted when sptr is a channel operator
1156 * but also when the mode is received from a server.
1157 * At this point, let any member pass, so they are allowed
1160 member_y = find_channel_member(sptr, chptr);
1161 if (!(IsServer(cptr) || member_y))
1164 #ifdef OPER_MODE_LCHAN
1165 if (IsOperOnLocalChannel(sptr, chptr->chname) && !IsChanOp(member_y))
1166 LocalChanOperMode = 1;
1169 mode = &(chptr->mode);
1170 memcpy(&oldm, mode, sizeof(struct Mode));
1172 newmode = mode->mode;
1174 while (curr && *curr) {
1187 if (MyUser(sptr) && opcnt >= MAXMODEPARAMS)
1190 * Check for nickname changes and try to follow these
1191 * to make sure the right client is affected by the
1193 * Even if we find a nick with find_chasing() there
1194 * is still a reason to ignore in a special case.
1195 * We need to ignore the mode when:
1196 * - It is part of a net.burst (from a server and
1197 * a MODE_ADD). Ofcourse we don't ignore mode
1198 * changes from Uworld.
1199 * - The found nick is not on the right side off
1201 * This fixes the bug that when someone (tries to)
1202 * ride a net.break and does so with the nick of
1203 * someone on the otherside, that he is nick collided
1204 * (killed) but his +o still ops the other person.
1208 if (!(who = find_chasing(sptr, parv[0], NULL)))
1213 if (!(who = findNUser(parv[0])))
1216 if (whatt == MODE_ADD && IsServer(sptr) && who->from != sptr->from &&
1217 !find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD))
1220 if (!(member_x = find_member_link(chptr, who)) ||
1221 (MyUser(sptr) && IsZombie(member_x)))
1223 send_reply(cptr, ERR_USERNOTINCHANNEL, who->name, chptr->chname);
1227 * if the user is +k, prevent a deop from local user
1229 if (whatt == MODE_DEL && IsChannelService(who) && *curr == 'o') {
1234 send_reply(cptr, ERR_ISCHANSERVICE, parv[0], chptr->chname);
1238 sprintf_irc(sendbuf,":%s NOTICE * :*** Notice -- Deop of +k user on %s by %s",
1239 me.name,chptr->chname,cptr->name);
1242 #ifdef NO_OPER_DEOP_LCHAN
1244 * if the user is an oper on a local channel, prevent him
1245 * from being deoped. that oper can deop himself though.
1247 if (whatt == MODE_DEL && IsOperOnLocalChannel(who, chptr->chname) &&
1248 (who != sptr) && MyUser(cptr) && *curr == 'o')
1250 send_reply(cptr, ERR_ISOPERLCHAN, parv[0], chptr->chname);
1254 if (whatt == MODE_ADD)
1256 lp = &chops[opcnt++];
1257 lp->value.cptr = who;
1258 if (IsServer(sptr) && (!(who->flags & FLAGS_TS8) || ((*curr == 'o') &&
1259 !(member_x->status & (CHFL_SERVOPOK | CHFL_CHANOP)))))
1260 *badop = ((member_x->status & CHFL_DEOPPED) && (*curr == 'o')) ? 2 : 3;
1261 lp->flags = (*curr == 'o') ? MODE_CHANOP : MODE_VOICE;
1262 lp->flags |= MODE_ADD;
1264 else if (whatt == MODE_DEL)
1266 lp = &chops[opcnt++];
1267 lp->value.cptr = who;
1268 doesdeop = 1; /* Also when -v */
1269 lp->flags = (*curr == 'o') ? MODE_CHANOP : MODE_VOICE;
1270 lp->flags |= MODE_DEL;
1279 /* check now so we eat the parameter if present */
1284 char *s = &(*parv)[-1];
1285 unsigned short count = KEYLEN + 1;
1287 while (*++s > ' ' && *s != ':' && --count);
1289 if (!**parv) /* nothing left in key */
1292 if (MyUser(sptr) && opcnt >= MAXMODEPARAMS)
1294 if (whatt == MODE_ADD)
1296 if (*mode->key && !IsServer(cptr))
1297 send_reply(cptr, ERR_KEYSET, chptr->chname);
1298 else if (!*mode->key || IsServer(cptr))
1300 lp = &chops[opcnt++];
1301 lp->value.cp = *parv;
1302 if (strlen(lp->value.cp) > KEYLEN)
1303 lp->value.cp[KEYLEN] = '\0';
1304 lp->flags = MODE_KEY | MODE_ADD;
1308 else if (whatt == MODE_DEL)
1310 /* Debug((DEBUG_INFO, "removing key: mode->key: >%s< *parv: >%s<", mode->key, *parv)); */
1311 if (0 == ircd_strcmp(mode->key, *parv) || IsServer(cptr))
1313 /* Debug((DEBUG_INFO, "key matched")); */
1314 lp = &chops[opcnt++];
1315 lp->value.cp = mode->key;
1316 lp->flags = MODE_KEY | MODE_DEL;
1323 if (0 == banlsent) {
1327 send_ban_list(cptr, chptr);
1333 if (EmptyString(*parv))
1337 if ((cp = strchr(*parv, ' ')))
1339 if (opcnt >= MAXMODEPARAMS || **parv == ':' || **parv == '\0')
1342 if (whatt == MODE_ADD)
1344 lp = &chops[opcnt++];
1345 lp->value.cp = *parv;
1346 lp->flags = MODE_ADD | MODE_BAN;
1348 else if (whatt == MODE_DEL)
1350 lp = &chops[opcnt++];
1351 lp->value.cp = *parv;
1352 lp->flags = MODE_DEL | MODE_BAN;
1357 * limit 'l' to only *1* change per mode command but
1362 if (whatt == MODE_ADD && --parc > 0)
1366 if (whatt == MODE_DEL)
1374 if (EmptyString(*parv))
1376 if (MyUser(sptr) && opcnt >= MAXMODEPARAMS)
1378 if (!(nusers = atoi(*++parv)))
1380 lp = &chops[opcnt++];
1381 lp->flags = MODE_ADD | MODE_LIMIT;
1385 need_more_params(cptr, "MODE +l");
1387 case 'i': /* falls through for default case */
1388 if (whatt == MODE_DEL)
1389 while ((lp = chptr->invites))
1390 del_invite(lp->value.cptr, chptr);
1392 for (ip = flags; *ip; ip += 2)
1393 if (*(ip + 1) == *curr)
1398 if (whatt == MODE_ADD)
1400 if (*ip == MODE_PRIVATE)
1401 newmode &= ~MODE_SECRET;
1402 else if (*ip == MODE_SECRET)
1403 newmode &= ~MODE_PRIVATE;
1409 else if (!IsServer(cptr))
1410 send_reply(cptr, ERR_UNKNOWNMODE, *curr);
1415 * Make sure mode strings such as "+m +t +p +i" are parsed
1418 if (!*curr && parc > 0)
1422 /* If this was from a server, and it is the last
1423 * parameter and it starts with a digit, it must
1424 * be the creationtime. --Run
1428 if (parc == 1 && IsDigit(*curr))
1430 newtime = atoi(curr);
1431 if (newtime && chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
1433 chptr->creationtime = newtime;
1442 else if (newtime > chptr->creationtime)
1443 { /* It is a net-break ride if we have ops.
1444 bounce modes if we have ops. --Run */
1447 else if (chptr->creationtime == 0)
1449 if (chptr->creationtime == 0 || doesop)
1450 chptr->creationtime = newtime;
1458 * A legal *badop can occur when two
1459 * people join simultaneously a channel,
1460 * Allow for 10 min of lag (and thus hacking
1461 * on channels younger then 10 min) --Run
1463 else if (*badop == 0 ||
1464 chptr->creationtime > (TStime() - TS_LAG_TIME))
1466 if (newtime < chptr->creationtime)
1467 chptr->creationtime = newtime;
1476 } /* end of while loop for MODE processing */
1478 #ifdef OPER_MODE_LCHAN
1480 * Now reject non chan ops. Accept modes from opers on local channels
1481 * even if they are deopped
1483 if (!IsServer(cptr) &&
1484 (!member_y || !(IsChanOp(member_y) ||
1485 IsOperOnLocalChannel(sptr, chptr->chname))))
1487 if (!IsServer(cptr) && (!member_y || !IsChanOp(member_y)))
1491 return (opcnt || newmode != mode->mode || limitset || keychange) ? 0 : -1;
1494 if (doesop && newtime == 0 && IsServer(sptr))
1498 (aconf = find_conf_byhost(cptr->confs, sptr->name, CONF_UWORLD)))
1501 #ifdef OPER_MODE_LCHAN
1502 bounce = (*badop == 1 || *badop == 2 ||
1503 (is_deopped(sptr, chptr) &&
1504 !IsOperOnLocalChannel(sptr, chptr->chname))) ? 1 : 0;
1506 bounce = (*badop == 1 || *badop == 2 || is_deopped(sptr, chptr)) ? 1 : 0;
1510 for (ip = flags; *ip; ip += 2) {
1511 if ((*ip & newmode) && !(*ip & oldm.mode))
1515 if (bwhatt != MODE_DEL)
1520 *bmbuf++ = *(ip + 1);
1524 if (whatt != MODE_ADD)
1530 *mbuf++ = *(ip + 1);
1534 for (ip = flags; *ip; ip += 2) {
1535 if ((*ip & oldm.mode) && !(*ip & newmode))
1539 if (bwhatt != MODE_ADD)
1544 *bmbuf++ = *(ip + 1);
1548 if (whatt != MODE_DEL)
1554 *mbuf++ = *(ip + 1);
1559 if (limitset && !nusers && mode->limit)
1563 if (bwhatt != MODE_ADD)
1569 sprintf(numeric, "%-15d", mode->limit);
1570 if ((cp = strchr(numeric, ' ')))
1572 strcat(bpbuf, numeric);
1573 blen += strlen(numeric);
1575 strcat(nbpbuf, numeric);
1576 nblen += strlen(numeric);
1577 strcat(nbpbuf, " ");
1581 if (whatt != MODE_DEL)
1586 mode->mode &= ~MODE_LIMIT;
1592 * Reconstruct "+bkov" chain.
1598 unsigned int prev_whatt = 0;
1600 for (; i < opcnt; i++)
1604 * make sure we have correct mode change sign
1606 if (whatt != (lp->flags & (MODE_ADD | MODE_DEL)))
1608 if (lp->flags & MODE_ADD)
1622 nlen = strlen(npbuf);
1624 * get c as the mode char and tmp as a pointer to
1625 * the parameter for this mode change.
1627 switch (lp->flags & MODE_WPARAS)
1631 cp = lp->value.cptr->name;
1635 cp = lp->value.cptr->name;
1639 * I made this a bit more user-friendly (tm):
1641 * nick!user = nick!user@*
1642 * user@host = *!user@host
1643 * host.name = *!*@host.name --Run
1646 cp = pretty_mask(lp->value.cp);
1654 sprintf(numeric, "%-15d", nusers);
1655 if ((cp = strchr(numeric, ' ')))
1661 /* What could be added: cp+' '+' '+<TS>+'\0' */
1662 if (len + strlen(cp) + 13 > MODEBUFLEN ||
1663 nlen + strlen(cp) + NUMNICKLEN + 12 > MODEBUFLEN)
1666 switch (lp->flags & MODE_WPARAS)
1669 if (strlen(cp) > KEYLEN)
1670 *(cp + KEYLEN) = '\0';
1671 if ((whatt == MODE_ADD && (*mode->key == '\0' ||
1672 0 != ircd_strcmp(mode->key, cp))) ||
1673 (whatt == MODE_DEL && (*mode->key != '\0')))
1677 if (*mode->key == '\0')
1679 if (bwhatt != MODE_DEL)
1689 nblen += strlen(cp);
1690 strcat(nbpbuf, " ");
1695 if (bwhatt != MODE_ADD)
1700 strcat(bpbuf, mode->key);
1701 blen += strlen(mode->key);
1704 strcat(nbpbuf, mode->key);
1705 nblen += strlen(mode->key);
1706 strcat(nbpbuf, " ");
1711 if (*mbuf != '+' && *mbuf != '-')
1727 if (whatt == MODE_ADD)
1728 ircd_strncpy(mode->key, cp, KEYLEN);
1735 if (nusers && nusers != mode->limit)
1739 if (mode->limit == 0)
1741 if (bwhatt != MODE_DEL)
1749 if (bwhatt != MODE_ADD)
1754 sprintf(numeric, "%-15d", mode->limit);
1755 if ((cp = strchr(numeric, ' ')))
1757 strcat(bpbuf, numeric);
1758 blen += strlen(numeric);
1761 strcat(nbpbuf, numeric);
1762 nblen += strlen(numeric);
1763 strcat(nbpbuf, " ");
1768 if (*mbuf != '+' && *mbuf != '-')
1784 mode->limit = nusers;
1790 member_y = find_member_link(chptr, lp->value.cptr);
1791 if (lp->flags & MODE_ADD)
1793 change = (~member_y->status) & CHFL_VOICED_OR_OPPED & lp->flags;
1794 if (change && bounce)
1796 if (lp->flags & MODE_CHANOP)
1797 SetDeopped(member_y);
1799 if (bwhatt != MODE_DEL)
1805 strcat(bpbuf, lp->value.cptr->name);
1806 blen += strlen(lp->value.cptr->name);
1809 sprintf_irc(nbpbuf + nblen, "%s%s ", NumNick(lp->value.cptr));
1810 nblen += strlen(nbpbuf + nblen);
1815 member_y->status |= lp->flags & CHFL_VOICED_OR_OPPED;
1816 if (IsChanOp(member_y))
1818 ClearDeopped(member_y);
1820 ClearServOpOk(member_y);
1826 change = member_y->status & CHFL_VOICED_OR_OPPED & lp->flags;
1827 if (change && bounce)
1829 if (lp->flags & MODE_CHANOP)
1830 ClearDeopped(member_y);
1831 if (bwhatt != MODE_ADD)
1837 strcat(bpbuf, lp->value.cptr->name);
1838 blen += strlen(lp->value.cptr->name);
1841 sprintf_irc(nbpbuf + nblen, "%s%s ", NumNick(lp->value.cptr));
1842 blen += strlen(bpbuf + blen);
1847 member_y->status &= ~change;
1848 if ((change & MODE_CHANOP) && IsServer(sptr))
1849 SetDeopped(member_y);
1852 if (change || *badop == 2 || *badop == 4)
1859 sprintf_irc(npbuf + nlen, "%s%s ", NumNick(lp->value.cptr));
1860 nlen += strlen(npbuf + nlen);
1861 npbuf[nlen++] = ' ';
1867 if (*mbuf != '+' && *mbuf != '-')
1875 * Only bans aren't bounced, it makes no sense to bounce last second
1876 * bans while propagating bans done before the net.rejoin. The reason
1877 * why I don't bounce net.rejoin bans is because it is too much
1878 * work to take care of too long strings adding the necessary TS to
1879 * net.burst bans -- RunLazy
1880 * We do have to check for *badop==2 now, we don't want HACKs to take
1883 * Since BURST - I *did* implement net.rejoin ban bouncing. So now it
1884 * certainly makes sense to also bounce 'last second' bans (bans done
1885 * after the net.junction). -- RunHardWorker
1887 if ((change = (whatt & MODE_ADD) &&
1888 !add_banid(sptr, chptr, cp, !bounce, !add_banid_called)))
1889 add_banid_called = 1;
1891 change = (whatt & MODE_DEL) && !del_banid(chptr, cp, !bounce);
1893 if (bounce && change)
1896 if ((whatt & MODE_ADD))
1898 if (bwhatt != MODE_DEL)
1904 else if ((whatt & MODE_DEL))
1906 if (bwhatt != MODE_ADD)
1918 nblen += strlen(cp);
1919 strcat(nbpbuf, " ");
1937 if (*mbuf != '+' && *mbuf != '-')
1944 } /* for (; i < opcnt; i++) */
1951 if (!hacknotice && *bmodebuf && chptr->creationtime)
1953 sendcmdto_one(&me, CMD_MODE, cptr, "%H %s %s %Tu", chptr, bmodebuf,
1954 nbparambuf, *badop == 2 ? (time_t) 0 : chptr->creationtime);
1956 /* If there are possibly bans to re-add, bounce them now */
1957 if (add_banid_called && bounce)
1959 struct SLink *ban[6]; /* Max 6 bans at a time */
1960 size_t len[6], sblen, total_len;
1961 int cnt, delayed = 0;
1962 while (delayed || (ban[0] = next_overlapped_ban()))
1964 len[0] = strlen(ban[0]->value.ban.banstr);
1965 cnt = 1; /* We already got one ban :) */
1966 /* XXX sendbuf used to send ban bounces! */
1967 sblen = sprintf_irc(sendbuf, ":%s MODE %s +b",
1968 me.name, chptr->chname) - sendbuf;
1969 total_len = sblen + 1 + len[0]; /* 1 = ' ' */
1970 /* Find more bans: */
1972 while (cnt < 6 && (ban[cnt] = next_overlapped_ban()))
1974 len[cnt] = strlen(ban[cnt]->value.ban.banstr);
1975 if (total_len + 5 + len[cnt] > BUFSIZE) /* 5 = "b \r\n\0" */
1977 delayed = cnt + 1; /* != 0 */
1980 sendbuf[sblen++] = 'b';
1981 total_len += 2 + len[cnt++]; /* 2 = "b " */
1985 sendbuf[sblen++] = ' ';
1986 strcpy(sendbuf + sblen, ban[cnt]->value.ban.banstr);
1989 sendbufto_one(cptr); /* Send bounce to uplink */
1991 ban[0] = ban[delayed - 1];
1994 /* Send -b's of overlapped bans to clients to keep them synchronized */
1995 if (add_banid_called && !bounce)
1998 char *banstr[6]; /* Max 6 bans at a time */
1999 size_t len[6], sblen, psblen, total_len;
2000 int cnt, delayed = 0;
2001 struct Membership* member_z;
2002 struct Client *acptr;
2004 /* XXX sendbuf used to send ban bounces! */
2005 psblen = sprintf_irc(sendbuf, ":%s MODE %s -b",
2006 sptr->name, chptr->chname) - sendbuf;
2007 else /* We rely on IsRegistered(sptr) being true for MODE */
2008 psblen = sprintf_irc(sendbuf, ":%s!%s@%s MODE %s -b", sptr->name,
2009 sptr->user->username, sptr->user->host, chptr->chname) - sendbuf;
2010 while (delayed || (ban = next_removed_overlapped_ban()))
2014 len[0] = strlen((banstr[0] = ban->value.ban.banstr));
2015 ban->value.ban.banstr = NULL;
2017 cnt = 1; /* We already got one ban :) */
2019 total_len = sblen + 1 + len[0]; /* 1 = ' ' */
2020 /* Find more bans: */
2022 while (cnt < 6 && (ban = next_removed_overlapped_ban()))
2024 len[cnt] = strlen((banstr[cnt] = ban->value.ban.banstr));
2025 ban->value.ban.banstr = NULL;
2026 if (total_len + 5 + len[cnt] > BUFSIZE) /* 5 = "b \r\n\0" */
2028 delayed = cnt + 1; /* != 0 */
2031 sendbuf[sblen++] = 'b';
2032 total_len += 2 + len[cnt++]; /* 2 = "b " */
2036 sendbuf[sblen++] = ' ';
2037 strcpy(sendbuf + sblen, banstr[cnt]);
2038 MyFree(banstr[cnt]);
2041 for (member_z = chptr->members; member_z; member_z = member_z->next_member) {
2042 acptr = member_z->user;
2043 if (MyConnect(acptr) && !IsZombie(member_z))
2044 sendbufto_one(acptr);
2048 banstr[0] = banstr[delayed - 1];
2049 len[0] = len[delayed - 1];
2054 return gotts ? 1 : -1;
2057 /* We are now treating the <key> part of /join <channel list> <key> as a key
2058 * ring; that is, we try one key against the actual channel key, and if that
2059 * doesn't work, we try the next one, and so on. -Kev -Texaco
2060 * Returns: 0 on match, 1 otherwise
2061 * This version contributed by SeKs <intru@info.polymtl.ca>
2063 static int compall(char *key, char *keyring)
2068 p1 = key; /* point to the key... */
2069 while (*p1 && *p1 == *keyring)
2070 { /* step through the key and ring until they
2076 if (!*p1 && (!*keyring || *keyring == ','))
2077 /* ok, if we're at the end of the and also at the end of one of the keys
2078 in the keyring, we have a match */
2081 if (!*keyring) /* if we're at the end of the key ring, there
2082 weren't any matches, so we return 1 */
2085 /* Not at the end of the key ring, so step
2086 through to the next key in the ring: */
2087 while (*keyring && *(keyring++) != ',');
2089 goto top; /* and check it against the key */
2092 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
2095 int overrideJoin = 0;
2098 * Now a banned user CAN join if invited -- Nemesi
2099 * Now a user CAN escape channel limit if invited -- bfriendly
2100 * Now a user CAN escape anything if invited -- Isomer
2103 for (lp = sptr->user->invited; lp; lp = lp->next)
2104 if (lp->value.chptr == chptr)
2107 #ifdef OPER_WALK_THROUGH_LMODES
2108 /* An oper can force a join on a local channel using "OVERRIDE" as the key.
2109 a HACK(4) notice will be sent if he would not have been supposed
2110 to join normally. */
2111 if (IsOperOnLocalChannel(sptr,chptr->chname) && !BadPtr(key) && compall("OVERRIDE",key) == 0)
2113 overrideJoin = MAGIC_OPER_OVERRIDE;
2117 if (chptr->mode.mode & MODE_INVITEONLY)
2118 return overrideJoin + ERR_INVITEONLYCHAN;
2120 if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
2121 return overrideJoin + ERR_CHANNELISFULL;
2123 if (is_banned(sptr, chptr, NULL))
2124 return overrideJoin + ERR_BANNEDFROMCHAN;
2127 * now using compall (above) to test against a whole key ring -Kev
2129 if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
2130 return overrideJoin + ERR_BADCHANNELKEY;
2133 return ERR_DONTCHEAT;
2139 * Remove bells and commas from channel name
2141 void clean_channelname(char *cn)
2145 for (i = 0; cn[i]; i++) {
2146 if (i >= CHANNELLEN || !IsChannelChar(cn[i])) {
2150 if (IsChannelLower(cn[i])) {
2151 cn[i] = ToLower(cn[i]);
2157 if ((unsigned char)(cn[i]) == 0xd0)
2158 cn[i] = (char) 0xf0;
2165 * Get Channel block for i (and allocate a new channel
2166 * block, if it didn't exists before).
2168 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
2170 struct Channel *chptr;
2173 if (EmptyString(chname))
2176 len = strlen(chname);
2177 if (MyUser(cptr) && len > CHANNELLEN)
2180 *(chname + CHANNELLEN) = '\0';
2182 if ((chptr = FindChannel(chname)))
2184 if (flag == CGT_CREATE)
2186 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
2188 ++UserStats.channels;
2189 memset(chptr, 0, sizeof(struct Channel));
2190 strcpy(chptr->chname, chname);
2191 if (GlobalChannelList)
2192 GlobalChannelList->prev = chptr;
2194 chptr->next = GlobalChannelList;
2195 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
2196 GlobalChannelList = chptr;
2202 void add_invite(struct Client *cptr, struct Channel *chptr)
2204 struct SLink *inv, **tmp;
2206 del_invite(cptr, chptr);
2208 * Delete last link in chain if the list is max length
2210 assert(list_length(cptr->user->invited) == cptr->user->invites);
2211 if (cptr->user->invites>=MAXCHANNELSPERUSER)
2212 del_invite(cptr, cptr->user->invited->value.chptr);
2214 * Add client to channel invite list
2217 inv->value.cptr = cptr;
2218 inv->next = chptr->invites;
2219 chptr->invites = inv;
2221 * Add channel to the end of the client invite list
2223 for (tmp = &(cptr->user->invited); *tmp; tmp = &((*tmp)->next));
2225 inv->value.chptr = chptr;
2228 cptr->user->invites++;
2232 * Delete Invite block from channel invite list and client invite list
2234 void del_invite(struct Client *cptr, struct Channel *chptr)
2236 struct SLink **inv, *tmp;
2238 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
2239 if (tmp->value.cptr == cptr)
2244 cptr->user->invites--;
2248 for (inv = &(cptr->user->invited); (tmp = *inv); inv = &tmp->next)
2249 if (tmp->value.chptr == chptr)
2258 /* List and skip all channels that are listen */
2259 void list_next_channels(struct Client *cptr, int nr)
2261 struct ListingArgs *args = cptr->listing;
2262 struct Channel *chptr = args->chptr;
2263 chptr->mode.mode &= ~MODE_LISTED;
2264 while (is_listed(chptr) || --nr >= 0)
2266 for (; chptr; chptr = chptr->next)
2268 if (!cptr->user || (SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
2270 if (chptr->users > args->min_users && chptr->users < args->max_users &&
2271 chptr->creationtime > args->min_time &&
2272 chptr->creationtime < args->max_time &&
2273 (!args->topic_limits || (*chptr->topic &&
2274 chptr->topic_time > args->min_topic_time &&
2275 chptr->topic_time < args->max_topic_time)))
2277 if (ShowChannel(cptr,chptr))
2278 send_reply(cptr, RPL_LIST, chptr->chname, chptr->users,
2280 chptr = chptr->next;
2286 MyFree(cptr->listing);
2287 cptr->listing = NULL;
2288 send_reply(cptr, RPL_LISTEND);
2294 cptr->listing->chptr = chptr;
2295 chptr->mode.mode |= MODE_LISTED;
2299 /* XXX AIEEEE! sendbuf is an institution here :( */
2300 void add_token_to_sendbuf(char *token, size_t *sblenp, int *firstp,
2301 int *send_itp, char is_a_ban, int mode)
2303 int first = *firstp;
2306 * Heh - we do not need to test if it still fits in the buffer, because
2307 * this BURST message is reconstructed from another BURST message, and
2308 * it only can become smaller. --Run
2311 if (*firstp) /* First token in this parameter ? */
2315 *send_itp = 1; /* Buffer contains data to be sent */
2316 sendbuf[(*sblenp)++] = ' ';
2319 sendbuf[(*sblenp)++] = ':'; /* Bans are always the last "parv" */
2320 sendbuf[(*sblenp)++] = is_a_ban;
2323 else /* Of course, 'send_it' is already set here */
2324 /* Seperate banmasks with a space because
2325 they can contain commas themselfs: */
2326 sendbuf[(*sblenp)++] = is_a_ban ? ' ' : ',';
2327 strcpy(sendbuf + *sblenp, token);
2328 *sblenp += strlen(token);
2329 if (!is_a_ban) /* nick list ? Need to take care
2330 of modes for nicks: */
2332 static int last_mode = 0;
2333 mode &= CHFL_CHANOP | CHFL_VOICE;
2336 if (last_mode != mode) /* Append mode like ':ov' if changed */
2339 sendbuf[(*sblenp)++] = ':';
2340 if (mode & CHFL_CHANOP)
2341 sendbuf[(*sblenp)++] = 'o';
2342 if (mode & CHFL_VOICE)
2343 sendbuf[(*sblenp)++] = 'v';
2345 sendbuf[*sblenp] = '\0';
2349 void cancel_mode(struct Client *sptr, struct Channel *chptr, char m,
2350 const char *param, int *count)
2355 int paramdoesntfit = 0;
2356 char parabuf[MODEBUFLEN];
2362 if (*count == -1) /* initialize ? */
2364 /* XXX sendbuf used! */
2366 sprintf_irc(sendbuf, ":%s MODE %s -", sptr->name, chptr->chname);
2370 /* m == 0 means flush */
2375 size_t nplen = strlen(param);
2376 if (pb - parabuf + nplen + 23 > MODEBUFLEN)
2390 else if (*count == 0)
2392 if (*count == 6 || !m || paramdoesntfit)
2394 struct Membership* member;
2395 strcpy(sbp, parabuf);
2396 for (member = chptr->members; member; member = member->next_member)
2397 if (MyUser(member->user))
2398 sendbufto_one(member->user);
2408 pb += strlen(param);
2421 * X --a--> A --b--> B --d--> D
2425 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
2426 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
2428 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
2429 * Remove the user immedeately when no users are left on the channel.
2430 * b) On server B : remove the user (who/lp) from the channel, send a
2431 * PART upstream (to A) and pass on the KICK.
2432 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
2433 * channel, and pass on the KICK.
2434 * d) On server D : remove the user (who/lp) from the channel, and pass on
2438 * - Setting the ZOMBIE flag never hurts, we either remove the
2439 * client after that or we don't.
2440 * - The KICK message was already passed on, as should be in all cases.
2441 * - `who' is removed in all cases except case a) when users are left.
2442 * - A PART is only sent upstream in case b).
2448 * 1 --- 2 --- 3 --- 4 --- 5
2452 * We also need to turn 'who' into a zombie on servers 1 and 6,
2453 * because a KICK from 'who' (kicking someone else in that direction)
2454 * can arrive there afterwards - which should not be bounced itself.
2455 * Therefore case a) also applies for servers 1 and 6.
2459 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
2460 struct Client* sptr, struct Channel* chptr)
2462 assert(0 != member);
2467 /* Default for case a): */
2470 /* Case b) or c) ?: */
2471 if (MyUser(who)) /* server 4 */
2473 if (IsServer(cptr)) /* Case b) ? */
2474 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
2475 remove_user_from_channel(who, chptr);
2478 if (who->from == cptr) /* True on servers 1, 5 and 6 */
2480 struct Client *acptr = IsServer(sptr) ? sptr : sptr->user->server;
2481 for (; acptr != &me; acptr = acptr->serv->up)
2482 if (acptr == who->user->server) /* Case d) (server 5) */
2484 remove_user_from_channel(who, chptr);
2489 /* Case a) (servers 1, 2, 3 and 6) */
2490 if (channel_all_zombies(chptr))
2491 remove_user_from_channel(who, chptr);
2493 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
2496 int number_of_zombies(struct Channel *chptr)
2498 struct Membership* member;
2502 for (member = chptr->members; member; member = member->next_member) {
2503 if (IsZombie(member))
2509 /* XXX we can probably get rid of send_user_joins */
2510 void send_user_joins(struct Client *cptr, struct Client *user)
2512 struct Membership* chan;
2513 struct Channel* chptr;
2521 strcpy(buf + 1, user->name);
2522 strcat(buf, " JOIN ");
2523 len = strlen(user->name) + 7;
2525 for (chan = user->user->channel; chan; chan = chan->next_channel)
2527 chptr = chan->channel;
2530 if ((mask = strchr(chptr->chname, ':')))
2531 if (match(++mask, cptr->name))
2533 if (*chptr->chname == '&')
2537 clen = strlen(chptr->chname);
2538 if (clen + 1 + len > BUFSIZE - 3)
2542 buf[len - 1] = '\0';
2543 sendto_one(cptr, "%s", buf); /* XXX Possibly DEAD */
2546 strcpy(buf + 1, user->name);
2547 strcat(buf, " JOIN ");
2548 len = strlen(user->name) + 7;
2551 strcpy(buf + len, chptr->chname);
2554 if (chan->next_channel)
2561 sendto_one(cptr, "%s", buf); /* XXX Possibly DEAD */
2565 * send_hack_notice()
2567 * parc & parv[] are the same as that of the calling function:
2568 * mtype == 1 is from m_mode, 2 is from m_create, 3 is from m_kick.
2570 * This function prepares sendbuf with the server notices and wallops
2571 * to be sent for all hacks. -Ghostwolf 18-May-97
2573 /* XXX let's get rid of this if we can */
2574 void send_hack_notice(struct Client *cptr, struct Client *sptr, int parc,
2575 char *parv[], int badop, int mtype)
2577 struct Channel *chptr;
2578 static char params[MODEBUFLEN];
2580 chptr = FindChannel(parv[1]);
2583 /* P10 servers require numeric nick conversion before sending. */
2586 case 1: /* Convert nicks for MODE HACKs here */
2588 char *mode = parv[2];
2591 while (*mode && *mode != 'o' && *mode != 'v')
2593 strcat(params, " ");
2594 if (*mode == 'o' || *mode == 'v')
2597 * blindly stumble through parameter list hoping one of them
2598 * might turn out to be a numeric nick
2599 * NOTE: this should not cause a problem but _may_ end up finding
2600 * something we aren't looking for. findNUser should be able to
2601 * handle any garbage that is thrown at it, but may return a client
2602 * if we happen to get lucky with a mode string or a timestamp
2604 struct Client *acptr;
2605 if ((acptr = findNUser(parv[i])) != NULL) /* Convert nicks here */
2606 strcat(params, acptr->name);
2609 strcat(params, "<");
2610 strcat(params, parv[i]);
2611 strcat(params, ">");
2614 else /* If it isn't a numnick, send it 'as is' */
2615 strcat(params, parv[i]);
2618 sprintf_irc(sendbuf,
2619 ":%s NOTICE * :*** Notice -- %sHACK(%d): %s MODE %s %s%s ["
2620 TIME_T_FMT "]", me.name, (badop == 3) ? "BOUNCE or " : "", badop,
2621 parv[0], parv[1], parv[2], params, chptr->creationtime);
2622 sendbufto_op_mask((badop == 3) ? SNO_HACK3 : (badop == /* XXX DYING */
2623 4) ? SNO_HACK4 : SNO_HACK2);
2625 if ((IsServer(sptr)) && (badop == 2))
2627 sprintf_irc(sendbuf, ":%s DESYNCH :HACK: %s MODE %s %s%s",
2628 me.name, parv[0], parv[1], parv[2], params);
2629 sendbufto_serv_butone(cptr); /* XXX DYING */
2633 case 2: /* No conversion is needed for CREATE; the only numnick is sptr */
2635 sendto_serv_butone(cptr, ":%s DESYNCH :HACK: %s CREATE %s %s", /* XXX DYING */
2636 me.name, sptr->name, chptr->chname, parv[2]);
2637 sendto_op_mask(SNO_HACK2, "HACK(2): %s CREATE %s %s", /* XXX DYING */
2638 sptr->name, chptr->chname, parv[2]);
2641 case 3: /* Convert nick in KICK message */
2643 struct Client *acptr;
2644 if ((acptr = findNUser(parv[2])) != NULL) /* attempt to convert nick */
2645 sprintf_irc(sendbuf,
2646 ":%s NOTICE * :*** Notice -- HACK: %s KICK %s %s :%s",
2647 me.name, sptr->name, parv[1], acptr->name, parv[3]);
2648 else /* if conversion fails, send it 'as is' in <>'s */
2649 sprintf_irc(sendbuf,
2650 ":%s NOTICE * :*** Notice -- HACK: %s KICK %s <%s> :%s",
2651 me.name, sptr->name, parv[1], parv[2], parv[3]);
2652 sendbufto_op_mask(SNO_HACK4); /* XXX DYING */
2659 * This helper function builds an argument string in strptr, consisting
2660 * of the original string, a space, and str1 and str2 concatenated (if,
2661 * of course, str2 is not NULL)
2664 build_string(char *strptr, int *strptr_i, char *str1, char *str2, char c)
2667 strptr[(*strptr_i)++] = c;
2670 strptr[(*strptr_i)++] = *(str1++);
2674 strptr[(*strptr_i)++] = *(str2++);
2676 strptr[(*strptr_i)] = '\0';
2680 * This is the workhorse of our ModeBuf suite; this actually generates the
2681 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
2684 modebuf_flush_int(struct ModeBuf *mbuf, int all)
2686 /* we only need the flags that don't take args right now */
2687 static int flags[] = {
2688 /* MODE_CHANOP, 'o', */
2689 /* MODE_VOICE, 'v', */
2692 MODE_MODERATED, 'm',
2693 MODE_TOPICLIMIT, 't',
2694 MODE_INVITEONLY, 'i',
2695 MODE_NOPRIVMSGS, 'n',
2696 /* MODE_KEY, 'k', */
2697 /* MODE_BAN, 'b', */
2698 /* MODE_LIMIT, 'l', */
2704 struct Client *app_source; /* where the MODE appears to come from */
2706 char addbuf[20]; /* accumulates +psmtin, etc. */
2708 char rembuf[20]; /* accumulates -psmtin, etc. */
2710 char *bufptr; /* we make use of indirection to simplify the code */
2713 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
2715 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
2717 char *strptr; /* more indirection to simplify the code */
2720 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
2723 char limitbuf[20]; /* convert limits to strings */
2725 unsigned int limitdel = MODE_LIMIT;
2729 /* If the ModeBuf is empty, we have nothing to do */
2730 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
2733 /* Ok, if we were given the OPMODE flag, hide the source if its a user */
2734 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
2735 app_source = mbuf->mb_source->user->server;
2737 app_source = mbuf->mb_source;
2740 * Account for user we're bouncing; we have to get it in on the first
2741 * bounced MODE, or we could have problems
2743 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
2744 totalbuflen -= 6; /* numeric nick == 5, plus one space */
2746 /* Calculate the simple flags */
2747 for (flag_p = flags; flag_p[0]; flag_p += 2) {
2748 if (*flag_p & mbuf->mb_add)
2749 addbuf[addbuf_i++] = flag_p[1];
2750 else if (*flag_p & mbuf->mb_rem)
2751 rembuf[rembuf_i++] = flag_p[1];
2754 /* Now go through the modes with arguments... */
2755 for (i = 0; i < mbuf->mb_count; i++) {
2756 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
2758 bufptr_i = &addbuf_i;
2761 bufptr_i = &rembuf_i;
2764 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
2765 tmp = strlen(MB_CLIENT(mbuf, i)->name);
2767 if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
2768 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
2770 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
2771 totalbuflen -= IRCD_MAX(5, tmp) + 1;
2773 } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN)) {
2774 tmp = strlen(MB_STRING(mbuf, i));
2776 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
2777 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
2779 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_KEY ? 'k' : 'b';
2780 totalbuflen -= tmp + 1;
2782 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
2783 /* if it's a limit, we also format the number */
2784 sprintf_irc(limitbuf, "%d", MB_UINT(mbuf, i));
2786 tmp = strlen(limitbuf);
2788 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
2789 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
2791 bufptr[(*bufptr_i)++] = 'l';
2792 totalbuflen -= tmp + 1;
2797 /* terminate the mode strings */
2798 addbuf[addbuf_i] = '\0';
2799 rembuf[rembuf_i] = '\0';
2801 /* If we're building a user visible MODE or HACK... */
2802 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
2803 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
2804 MODEBUF_DEST_LOG)) {
2805 /* Set up the parameter strings */
2811 for (i = 0; i < mbuf->mb_count; i++) {
2812 if (MB_TYPE(mbuf, i) & MODE_SAVE)
2815 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
2817 strptr_i = &addstr_i;
2820 strptr_i = &remstr_i;
2823 /* deal with clients... */
2824 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
2825 build_string(strptr, strptr_i, MB_CLIENT(mbuf, i)->name, 0, ' ');
2827 /* deal with strings... */
2828 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
2829 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
2832 * deal with limit; note we cannot include the limit parameter if we're
2835 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
2836 (MODE_ADD | MODE_LIMIT))
2837 build_string(strptr, strptr_i, limitbuf, 0, ' ');
2840 /* send the messages off to their destination */
2841 if (mbuf->mb_dest & MODEBUF_DEST_HACK2) {
2842 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
2843 "[%Tu]", app_source->name, mbuf->mb_channel->chname,
2844 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
2845 addbuf, remstr, addstr,
2846 mbuf->mb_channel->creationtime);
2847 sendcmdto_serv_butone(&me, CMD_DESYNCH, mbuf->mb_connect,
2848 ":HACK: %s MODE %s %s%s%s%s%s%s [%Tu]",
2849 app_source->name, mbuf->mb_channel->chname,
2850 rembuf_i ? "-" : "", rembuf,
2851 addbuf_i ? "+" : "", addbuf, remstr, addstr,
2852 mbuf->mb_channel->creationtime);
2855 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
2856 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
2857 "%s%s%s%s%s%s [%Tu]", app_source->name,
2858 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
2859 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
2860 mbuf->mb_channel->creationtime);
2862 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
2863 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
2864 "[%Tu]", app_source->name, mbuf->mb_channel->chname,
2865 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
2866 addbuf, remstr, addstr,
2867 mbuf->mb_channel->creationtime);
2870 if (mbuf->mb_dest & MODEBUF_DEST_LOG) {
2871 write_log(OPATH, "%Tu %#C OPMODE %H %s%s%s%s%s%s\n", TStime(),
2872 mbuf->mb_source, mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
2873 addbuf_i ? "+" : "", addbuf, remstr, addstr);
2877 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
2878 sendcmdto_channel_butserv(app_source, CMD_MODE, mbuf->mb_channel,
2879 "%H %s%s%s%s%s%s", mbuf->mb_channel,
2880 rembuf_i ? "-" : "", rembuf,
2881 addbuf_i ? "+" : "", addbuf, remstr, addstr);
2884 /* Now are we supposed to propagate to other servers? */
2885 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
2886 /* set up parameter string */
2893 * limit is supressed if we're removing it; we have to figure out which
2894 * direction is the direction for it to be removed, though...
2896 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
2898 for (i = 0; i < mbuf->mb_count; i++) {
2899 if (MB_TYPE(mbuf, i) & MODE_SAVE)
2902 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
2904 strptr_i = &addstr_i;
2907 strptr_i = &remstr_i;
2910 /* deal with modes that take clients */
2911 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
2912 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
2914 /* deal with modes that take strings */
2915 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
2916 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
2919 * deal with the limit. Logic here is complicated; if HACK2 is set,
2920 * we're bouncing the mode, so sense is reversed, and we have to
2921 * include the original limit if it looks like it's being removed
2923 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
2924 build_string(strptr, strptr_i, limitbuf, 0, ' ');
2927 /* we were told to deop the source */
2928 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
2929 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
2930 addbuf[addbuf_i] = '\0'; /* terminate the string... */
2931 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
2933 /* mark that we've done this, so we don't do it again */
2934 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
2937 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
2938 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
2939 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
2940 "%H %s%s%s%s%s%s", mbuf->mb_channel,
2941 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
2942 addbuf, remstr, addstr);
2943 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
2945 * If HACK2 was set, we're bouncing; we send the MODE back to the
2946 * connection we got it from with the senses reversed and a TS of 0;
2949 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
2950 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
2951 rembuf_i ? "+" : "", rembuf, addstr, remstr,
2952 mbuf->mb_channel->creationtime);
2955 * We're propagating a normal MODE command to the rest of the network;
2956 * we send the actual channel TS unless this is a HACK3 or a HACK4
2958 if (IsServer(mbuf->mb_source))
2959 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
2960 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
2961 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
2962 addbuf, remstr, addstr,
2963 (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
2964 mbuf->mb_channel->creationtime);
2966 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
2967 "%H %s%s%s%s%s%s", mbuf->mb_channel,
2968 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
2969 addbuf, remstr, addstr);
2973 /* We've drained the ModeBuf... */
2978 /* reinitialize the mode-with-arg slots */
2979 for (i = 0; i < MAXMODEPARAMS; i++) {
2980 /* If we saved any, pack them down */
2981 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
2982 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
2983 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
2985 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
2987 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
2988 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
2990 MB_TYPE(mbuf, i) = 0;
2991 MB_UINT(mbuf, i) = 0;
2994 /* If we're supposed to flush it all, do so--all hail tail recursion */
2995 if (all && mbuf->mb_count)
2996 return modebuf_flush_int(mbuf, 1);
3002 * This routine just initializes a ModeBuf structure with the information
3003 * needed and the options given.
3006 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
3007 struct Client *connect, struct Channel *chan, unsigned int dest)
3012 assert(0 != source);
3018 mbuf->mb_source = source;
3019 mbuf->mb_connect = connect;
3020 mbuf->mb_channel = chan;
3021 mbuf->mb_dest = dest;
3024 /* clear each mode-with-parameter slot */
3025 for (i = 0; i < MAXMODEPARAMS; i++) {
3026 MB_TYPE(mbuf, i) = 0;
3027 MB_UINT(mbuf, i) = 0;
3032 * This routine simply adds modes to be added or deleted; do a binary OR
3033 * with either MODE_ADD or MODE_DEL
3036 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
3039 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3041 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
3042 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
3044 if (mode & MODE_ADD) {
3045 mbuf->mb_rem &= ~mode;
3046 mbuf->mb_add |= mode;
3048 mbuf->mb_add &= ~mode;
3049 mbuf->mb_rem |= mode;
3054 * This routine adds a mode to be added or deleted that takes a unsigned
3055 * int parameter; mode may *only* be the relevant mode flag ORed with one
3056 * of MODE_ADD or MODE_DEL
3059 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
3062 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3064 MB_TYPE(mbuf, mbuf->mb_count) = mode;
3065 MB_UINT(mbuf, mbuf->mb_count) = uint;
3067 /* when we've reached the maximal count, flush the buffer */
3068 if (++mbuf->mb_count >=
3069 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
3070 modebuf_flush_int(mbuf, 0);
3074 * This routine adds a mode to be added or deleted that takes a string
3075 * parameter; mode may *only* be the relevant mode flag ORed with one of
3076 * MODE_ADD or MODE_DEL
3079 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
3083 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3085 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
3086 MB_STRING(mbuf, mbuf->mb_count) = string;
3088 /* when we've reached the maximal count, flush the buffer */
3089 if (++mbuf->mb_count >=
3090 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
3091 modebuf_flush_int(mbuf, 0);
3095 * This routine adds a mode to be added or deleted that takes a client
3096 * parameter; mode may *only* be the relevant mode flag ORed with one of
3097 * MODE_ADD or MODE_DEL
3100 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
3101 struct Client *client)
3104 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
3106 MB_TYPE(mbuf, mbuf->mb_count) = mode;
3107 MB_CLIENT(mbuf, mbuf->mb_count) = client;
3109 /* when we've reached the maximal count, flush the buffer */
3110 if (++mbuf->mb_count >=
3111 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
3112 modebuf_flush_int(mbuf, 0);
3116 * This is the exported binding for modebuf_flush()
3119 modebuf_flush(struct ModeBuf *mbuf)
3121 return modebuf_flush_int(mbuf, 1);
3125 * Simple function to invalidate bans
3128 mode_ban_invalidate(struct Channel *chan)
3130 struct Membership *member;
3132 for (member = chan->members; member; member = member->next_member)
3133 ClearBanValid(member);
3137 * Simple function to drop invite structures
3140 mode_invite_clear(struct Channel *chan)
3142 while (chan->invites)
3143 del_invite(chan->invites->value.cptr, chan);
3146 /* What we've done for mode_parse so far... */
3147 #define DONE_LIMIT 0x01 /* We've set the limit */
3148 #define DONE_KEY 0x02 /* We've set the key */
3149 #define DONE_BANLIST 0x04 /* We've sent the ban list */
3150 #define DONE_NOTOPER 0x08 /* We've sent a "Not oper" error */
3151 #define DONE_BANCLEAN 0x10 /* We've cleaned bans... */
3154 struct ModeBuf *mbuf;
3155 struct Client *cptr;
3156 struct Client *sptr;
3157 struct Channel *chptr;
3166 struct SLink banlist[MAXPARA];
3169 struct Client *client;
3170 } cli_change[MAXPARA];
3174 * Here's a helper function to deal with sending along "Not oper" or
3175 * "Not member" messages
3178 send_notoper(struct ParseState *state)
3180 if (state->done & DONE_NOTOPER)
3183 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
3184 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
3186 state->done |= DONE_NOTOPER;
3190 * Helper function to convert limits
3193 mode_parse_limit(struct ParseState *state, int *flag_p)
3195 unsigned int t_limit;
3197 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
3198 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
3201 if (state->parc <= 0) { /* warn if not enough args */
3202 if (MyUser(state->sptr))
3203 need_more_params(state->sptr, "MODE +l");
3207 t_limit = atoi(state->parv[state->args_used++]); /* grab arg */
3211 if (!t_limit) /* if it was zero, ignore it */
3214 t_limit = state->chptr->mode.limit;
3216 /* If they're not an oper, they can't change modes */
3217 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3218 send_notoper(state);
3222 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
3224 state->done |= DONE_LIMIT;
3226 assert(0 != state->mbuf);
3228 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
3230 if (state->flags & MODE_PARSE_SET) { /* set the limit */
3231 if (state->dir & MODE_ADD) {
3232 state->chptr->mode.mode |= flag_p[0];
3233 state->chptr->mode.limit = t_limit;
3235 state->chptr->mode.mode &= flag_p[0];
3236 state->chptr->mode.limit = 0;
3242 * Helper function to convert keys
3245 mode_parse_key(struct ParseState *state, int *flag_p)
3250 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3253 if (state->parc <= 0) { /* warn if not enough args */
3254 if (MyUser(state->sptr))
3255 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
3260 t_str = state->parv[state->args_used++]; /* grab arg */
3264 /* If they're not an oper, they can't change modes */
3265 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3266 send_notoper(state);
3270 if (state->done & DONE_KEY) /* allow key to be set only once */
3272 state->done |= DONE_KEY;
3276 /* clean up the key string */
3278 while (*++s > ' ' && *s != ':' && --t_len)
3282 if (!*t_str) { /* warn if empty */
3283 if (MyUser(state->sptr))
3284 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
3289 /* can't add a key if one is set, nor can one remove the wrong key */
3290 if (!(state->flags & MODE_PARSE_FORCE))
3291 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
3292 (state->dir == MODE_DEL &&
3293 ircd_strcmp(state->chptr->mode.key, t_str))) {
3294 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
3298 assert(0 != state->mbuf);
3300 if (state->flags & MODE_PARSE_BOUNCE) {
3301 if (*state->chptr->mode.key) /* reset old key */
3302 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
3303 state->chptr->mode.key, 0);
3304 else /* remove new bogus key */
3305 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
3306 } else /* send new key */
3307 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
3309 if (state->flags & MODE_PARSE_SET) {
3310 if (state->dir == MODE_ADD) /* set the new key */
3311 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
3312 else /* remove the old key */
3313 *state->chptr->mode.key = '\0';
3318 * Helper function to convert bans
3321 mode_parse_ban(struct ParseState *state, int *flag_p)
3324 struct SLink *ban, *newban = 0;
3326 if (state->parc <= 0) { /* Not enough args, send ban list */
3327 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
3328 send_ban_list(state->sptr, state->chptr);
3329 state->done |= DONE_BANLIST;
3335 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3338 t_str = state->parv[state->args_used++]; /* grab arg */
3342 /* If they're not an oper, they can't change modes */
3343 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3344 send_notoper(state);
3348 if ((s = strchr(t_str, ' ')))
3351 if (!*t_str || *t_str == ':') { /* warn if empty */
3352 if (MyUser(state->sptr))
3353 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
3358 t_str = collapse(pretty_mask(t_str));
3360 /* remember the ban for the moment... */
3361 if (state->dir == MODE_ADD) {
3362 newban = state->banlist + (state->numbans++);
3365 DupString(newban->value.ban.banstr, t_str);
3366 newban->value.ban.who = state->sptr->name;
3367 newban->value.ban.when = TStime();
3369 newban->flags = CHFL_BAN | MODE_ADD;
3371 if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
3372 newban->flags |= CHFL_BAN_IPMASK;
3375 if (!state->chptr->banlist) {
3376 state->chptr->banlist = newban; /* add our ban with its flags */
3377 state->done |= DONE_BANCLEAN;
3381 /* Go through all bans */
3382 for (ban = state->chptr->banlist; ban; ban = ban->next) {
3383 /* first, clean the ban flags up a bit */
3384 if (!(state->done & DONE_BANCLEAN))
3385 /* Note: We're overloading *lots* of bits here; be careful! */
3386 ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
3390 * MODE_ADD - Ban was added; if we're bouncing modes,
3391 * then we'll remove it below; otherwise,
3392 * we'll have to allocate a real ban
3394 * MODE_DEL - Ban was marked for deletion; if we're
3395 * bouncing modes, we'll have to re-add it,
3396 * otherwise, we'll have to remove it
3398 * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
3399 * with a ban already set; if we're
3400 * bouncing modes, we'll have to bounce
3401 * this one; otherwise, we'll just ignore
3402 * it when we process added bans
3405 if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
3406 ban->flags |= MODE_DEL; /* delete one ban */
3408 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
3410 } else if (state->dir == MODE_ADD) {
3411 /* if the ban already exists, don't worry about it */
3412 if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
3413 if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
3416 } else if (!mmatch(ban->value.ban.banstr, t_str)) {
3417 if (!(ban->flags & MODE_DEL))
3418 newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
3419 } else if (!mmatch(t_str, ban->value.ban.banstr))
3420 ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
3423 ban->next = newban; /* add our ban with its flags */
3424 break; /* get out of loop */
3428 state->done |= DONE_BANCLEAN;
3432 * This is the bottom half of the ban processor
3435 mode_process_bans(struct ParseState *state)
3437 struct SLink *ban, *newban, *prevban, *nextban;
3443 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
3445 banlen = strlen(ban->value.ban.banstr);
3447 nextban = ban->next;
3449 if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
3451 prevban->next = 0; /* Break the list; ban isn't a real ban */
3453 state->chptr->banlist = 0;
3458 MyFree(ban->value.ban.banstr);
3461 } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
3462 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
3463 ban->value.ban.banstr,
3464 state->flags & MODE_PARSE_SET);
3466 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
3467 if (prevban) /* clip it out of the list... */
3468 prevban->next = ban->next;
3470 state->chptr->banlist = ban->next;
3475 MyFree(ban->value.ban.who);
3479 continue; /* next ban; keep prevban like it is */
3481 ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
3482 } else if (ban->flags & MODE_ADD) { /* adding a ban? */
3484 prevban->next = 0; /* Break the list; ban isn't a real ban */
3486 state->chptr->banlist = 0;
3488 /* If we're supposed to ignore it, do so. */
3489 if (ban->flags & CHFL_BAN_OVERLAPPED &&
3490 !(state->flags & MODE_PARSE_BOUNCE)) {
3494 MyFree(ban->value.ban.banstr);
3496 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
3497 (len > MAXBANLENGTH || count >= MAXBANS)) {
3498 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
3499 ban->value.ban.banstr);
3503 MyFree(ban->value.ban.banstr);
3505 /* add the ban to the buffer */
3506 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
3507 ban->value.ban.banstr,
3508 !(state->flags & MODE_PARSE_SET));
3510 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
3511 newban = make_link();
3512 newban->value.ban.banstr = ban->value.ban.banstr;
3513 DupString(newban->value.ban.who, ban->value.ban.who);
3514 newban->value.ban.when = ban->value.ban.when;
3515 newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
3517 newban->next = state->chptr->banlist; /* and link it in */
3518 state->chptr->banlist = newban;
3527 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
3529 if (changed) /* if we changed the ban list, we must invalidate the bans */
3530 mode_ban_invalidate(state->chptr);
3534 * Helper function to process client changes
3537 mode_parse_client(struct ParseState *state, int *flag_p)
3540 struct Client *acptr;
3543 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
3546 if (state->parc <= 0) { /* warn if not enough args */
3547 if (MyUser(state->sptr))
3548 need_more_params(state->sptr, state->dir == MODE_ADD ?
3549 (flag_p[0] == MODE_CHANOP ? "MODE +o" : "MODE +v") :
3550 (flag_p[0] == MODE_CHANOP ? "MODE -o" : "MODE -v"));
3554 t_str = state->parv[state->args_used++]; /* grab arg */
3558 /* If they're not an oper, they can't change modes */
3559 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3560 send_notoper(state);
3564 if (MyUser(state->sptr)) /* find client we're manipulating */
3565 acptr = find_chasing(state->sptr, t_str, NULL);
3567 acptr = findNUser(t_str);
3570 return; /* find_chasing() already reported an error to the user */
3572 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
3573 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
3574 state->cli_change[i].flag & flag_p[0]))
3575 break; /* found a slot */
3577 /* Store what we're doing to them */
3578 state->cli_change[i].flag = state->dir | flag_p[0];
3579 state->cli_change[i].client = acptr;
3583 * Helper function to process the changed client list
3586 mode_process_clients(struct ParseState *state)
3589 struct Membership *member;
3591 for (i = 0; state->cli_change[i].flag; i++) {
3592 assert(0 != state->cli_change[i].client);
3594 /* look up member link */
3595 if (!(member = find_member_link(state->chptr,
3596 state->cli_change[i].client)) ||
3597 (MyUser(state->sptr) && IsZombie(member))) {
3598 if (MyUser(state->sptr))
3599 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
3600 state->cli_change[i].client->name, state->chptr->chname);
3604 if ((state->cli_change[i].flag & MODE_ADD &&
3605 (state->cli_change[i].flag & member->status)) ||
3606 (state->cli_change[i].flag & MODE_DEL &&
3607 !(state->cli_change[i].flag & member->status)))
3608 continue; /* no change made, don't do anything */
3610 /* see if the deop is allowed */
3611 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
3612 (MODE_DEL | MODE_CHANOP)) {
3613 /* prevent +k users from being deopped */
3614 if (IsChannelService(state->cli_change[i].client)) {
3615 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
3616 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
3618 (IsServer(state->sptr) ? state->sptr->name :
3619 state->sptr->user->server->name));
3621 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
3622 send_reply(state->sptr, ERR_ISCHANSERVICE,
3623 state->cli_change[i].client->name, state->chptr->chname);
3628 #ifdef NO_OPER_DEOP_LCHAN
3629 /* don't allow local opers to be deopped on local channels */
3630 if (MyUser(state->sptr) && state->cli_change[i].client != state->sptr &&
3631 IsOperOnLocalChannel(state->cli_change[i].client,
3632 state->chptr->chname)) {
3633 send_reply(state->sptr, ERR_ISOPERLCHAN,
3634 state->cli_change[i].client->name, state->chptr->chname);
3640 /* accumulate the change */
3641 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
3642 state->cli_change[i].client);
3644 /* actually effect the change */
3645 if (state->flags & MODE_PARSE_SET) {
3646 if (state->cli_change[i].flag & MODE_ADD) {
3647 member->status |= (state->cli_change[i].flag &
3648 (MODE_CHANOP | MODE_VOICE));
3649 if (state->cli_change[i].flag & MODE_CHANOP)
3650 ClearDeopped(member);
3652 member->status &= ~(state->cli_change[i].flag &
3653 (MODE_CHANOP | MODE_VOICE));
3655 } /* for (i = 0; state->cli_change[i].flags; i++) { */
3659 * Helper function to process the simple modes
3662 mode_parse_mode(struct ParseState *state, int *flag_p)
3664 if ((state->dir == MODE_ADD && (flag_p[0] & state->chptr->mode.mode)) ||
3665 (state->dir == MODE_DEL && !(flag_p[0] & state->chptr->mode.mode)))
3666 return; /* no change */
3668 /* If they're not an oper, they can't change modes */
3669 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3670 send_notoper(state);
3674 assert(0 != state->mbuf);
3676 modebuf_mode(state->mbuf, state->dir | flag_p[0]);
3678 /* make +p and +s mutually exclusive */
3679 if (state->dir == MODE_ADD && flag_p[0] & (MODE_SECRET | MODE_PRIVATE)) {
3680 if (flag_p[0] == MODE_SECRET && (state->chptr->mode.mode & MODE_PRIVATE))
3681 modebuf_mode(state->mbuf, MODE_DEL | MODE_PRIVATE);
3682 else if (flag_p[0] == MODE_PRIVATE &&
3683 (state->chptr->mode.mode & MODE_SECRET))
3684 modebuf_mode(state->mbuf, MODE_DEL | MODE_SECRET);
3687 if (state->flags & MODE_PARSE_SET) { /* set the flags */
3688 if (state->dir == MODE_ADD) { /* add the mode to the channel */
3689 state->chptr->mode.mode |= flag_p[0];
3691 /* make +p and +s mutually exclusive */
3692 if (state->dir == MODE_ADD && flag_p[0] & (MODE_SECRET | MODE_PRIVATE)) {
3693 if (flag_p[0] == MODE_PRIVATE)
3694 state->chptr->mode.mode &= ~MODE_SECRET;
3696 state->chptr->mode.mode &= ~MODE_PRIVATE;
3698 } else /* remove the mode from the channel */
3699 state->chptr->mode.mode &= ~flag_p[0];
3702 /* Clear out invite structures if we're removing invites */
3703 if (state->flags & MODE_PARSE_SET && state->dir == MODE_DEL &&
3704 flag_p[0] == MODE_INVITEONLY)
3705 mode_invite_clear(state->chptr);
3709 * This routine is intended to parse MODE or OPMODE commands and effect the
3710 * changes (or just build the bounce buffer). We pass the starting offset
3714 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
3715 struct Channel *chptr, int parc, char *parv[], unsigned int flags)
3717 static int chan_flags[] = {
3722 MODE_MODERATED, 'm',
3723 MODE_TOPICLIMIT, 't',
3724 MODE_INVITEONLY, 'i',
3725 MODE_NOPRIVMSGS, 'n',
3736 struct ParseState state;
3747 state.chptr = chptr;
3750 state.flags = flags;
3751 state.dir = MODE_ADD;
3753 state.args_used = 0;
3754 state.max_args = MAXMODEPARAMS;
3757 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
3758 state.banlist[i].next = 0;
3759 state.banlist[i].value.ban.banstr = 0;
3760 state.banlist[i].value.ban.who = 0;
3761 state.banlist[i].value.ban.when = 0;
3762 state.banlist[i].flags = 0;
3763 state.cli_change[i].flag = 0;
3764 state.cli_change[i].client = 0;
3767 modestr = state.parv[state.args_used++];
3771 for (; *modestr; modestr++) {
3772 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
3773 if (flag_p[1] == *modestr)
3776 if (!flag_p[0]) { /* didn't find it? complain and continue */
3777 if (MyUser(state.sptr))
3778 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
3783 case '+': /* switch direction to MODE_ADD */
3784 case '-': /* switch direction to MODE_DEL */
3785 state.dir = flag_p[0];
3788 case 'l': /* deal with limits */
3789 mode_parse_limit(&state, flag_p);
3792 case 'k': /* deal with keys */
3793 mode_parse_key(&state, flag_p);
3796 case 'b': /* deal with bans */
3797 mode_parse_ban(&state, flag_p);
3800 case 'o': /* deal with ops/voice */
3802 mode_parse_client(&state, flag_p);
3805 default: /* deal with other modes */
3806 mode_parse_mode(&state, flag_p);
3808 } /* switch (*modestr) { */
3809 } /* for (; *modestr; modestr++) { */
3811 if (state.parc > 0) { /* process next argument in string */
3812 modestr = state.parv[state.args_used++];
3816 if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
3819 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
3820 break; /* we're then going to bounce the mode! */
3822 recv_ts = atoi(modestr);
3824 if (recv_ts && recv_ts < state.chptr->creationtime)
3825 state.chptr->creationtime = recv_ts; /* respect earlier TS */
3827 break; /* break out of while loop */
3828 } else if (state.flags & MODE_PARSE_STRICT ||
3829 (MyUser(state.sptr) && state.max_args <= 0)) {
3830 state.parc++; /* we didn't actually gobble the argument */
3832 break; /* break out of while loop */
3835 } /* while (*modestr) { */
3838 * the rest of the function finishes building resultant MODEs; if the
3839 * origin isn't a member or an oper, skip it.
3841 if (state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3842 return state.args_used; /* tell our parent how many args we gobbled */
3844 assert(0 != state.mbuf);
3846 if (state.done & DONE_BANCLEAN) /* process bans */
3847 mode_process_bans(&state);
3849 /* process client changes */
3850 if (state.cli_change[0].flag)
3851 mode_process_clients(&state);
3853 return state.args_used; /* tell our parent how many args we gobbled */
3857 * Initialize a join buffer
3860 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
3861 struct Client *connect, unsigned int type, char *comment,
3867 assert(0 != source);
3868 assert(0 != connect);
3870 jbuf->jb_source = source; /* just initialize struct JoinBuf */
3871 jbuf->jb_connect = connect;
3872 jbuf->jb_type = type;
3873 jbuf->jb_comment = comment;
3874 jbuf->jb_create = create;
3876 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
3877 type == JOINBUF_TYPE_PART ||
3878 type == JOINBUF_TYPE_PARTALL) ?
3879 STARTJOINLEN : STARTCREATELEN) +
3880 (comment ? strlen(comment) + 2 : 0));
3882 for (i = 0; i < MAXJOINARGS; i++)
3883 jbuf->jb_channels[i] = 0;
3887 * Add a channel to the join buffer
3890 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
3897 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
3898 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
3899 /* Send notification to channel */
3900 if (!(flags & CHFL_ZOMBIE))
3901 sendcmdto_channel_butserv(jbuf->jb_source, CMD_PART, chan,
3902 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3903 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3904 else if (MyUser(jbuf->jb_source))
3905 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
3906 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3907 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3908 /* XXX: Shouldn't we send a PART here anyway? */
3911 /* Add user to channel */
3912 add_user_to_channel(chan, jbuf->jb_source, flags);
3914 /* Send the notification to the channel */
3915 sendcmdto_channel_butserv(jbuf->jb_source, CMD_JOIN, chan, ":%H", chan);
3917 /* send an op, too, if needed */
3918 if (jbuf->jb_type == JOINBUF_TYPE_CREATE &&
3919 !IsModelessChannel(chan->chname))
3920 sendcmdto_channel_butserv(jbuf->jb_source, CMD_MODE, chan, "%H +o %C",
3921 chan, jbuf->jb_source);
3924 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || IsLocalChannel(chan->chname))
3925 return; /* don't send to remote */
3928 /* figure out if channel name will cause buffer to be overflowed */
3929 len = chan ? strlen(chan->chname) + 1 : 2;
3930 if (jbuf->jb_strlen + len > IRC_BUFSIZE)
3931 joinbuf_flush(jbuf);
3933 /* add channel to list of channels to send and update counts */
3934 jbuf->jb_channels[jbuf->jb_count++] = chan;
3935 jbuf->jb_strlen += len;
3937 /* if we've used up all slots, flush */
3938 if (jbuf->jb_count >= MAXJOINARGS)
3939 joinbuf_flush(jbuf);
3943 * Flush the channel list to remote servers
3946 joinbuf_flush(struct JoinBuf *jbuf)
3948 char chanlist[IRC_BUFSIZE];
3952 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL)
3953 return 0; /* no joins to process */
3955 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
3956 build_string(chanlist, &chanlist_i,
3957 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
3958 i == 0 ? '\0' : ',');
3959 if (JOINBUF_TYPE_PART == jbuf->jb_type)
3960 /* Remove user from channel */
3961 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
3963 jbuf->jb_channels[i] = 0; /* mark slot empty */
3966 jbuf->jb_count = 0; /* reset base counters */
3967 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_JOIN ||
3968 jbuf->jb_type == JOINBUF_TYPE_PART ?
3969 STARTJOINLEN : STARTCREATELEN) +
3970 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
3972 /* and send the appropriate command */
3973 switch (jbuf->jb_type) {
3974 case JOINBUF_TYPE_JOIN:
3975 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
3979 case JOINBUF_TYPE_CREATE:
3980 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
3981 "%s %Tu", chanlist, jbuf->jb_create);
3984 case JOINBUF_TYPE_PART:
3985 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
3986 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,