]> jfr.im git - irc/quakenet/snircd.git/blame - ircd/channel.c
import of 2.10.12.07
[irc/quakenet/snircd.git] / ircd / channel.c
CommitLineData
189935b1 1/*
2 * IRC - Internet Relay Chat, ircd/channel.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Co Center
5 *
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)
9 * any later version.
10 *
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.
15 *
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.
19 */
20/** @file
21 * @brief Channel management and maintenance
9f8856e9 22 * @version $Id: channel.c,v 1.155.2.15 2006/04/04 23:09:47 entrope Exp $
189935b1 23 */
24#include "config.h"
25
26#include "channel.h"
27#include "client.h"
28#include "destruct_event.h"
29#include "hash.h"
30#include "ircd.h"
31#include "ircd_alloc.h"
32#include "ircd_chattr.h"
33#include "ircd_defs.h"
34#include "ircd_features.h"
35#include "ircd_log.h"
36#include "ircd_reply.h"
37#include "ircd_snprintf.h"
38#include "ircd_string.h"
39#include "list.h"
40#include "match.h"
41#include "msg.h"
42#include "msgq.h"
43#include "numeric.h"
44#include "numnicks.h"
45#include "querycmds.h"
46#include "s_bsd.h"
47#include "s_conf.h"
48#include "s_debug.h"
49#include "s_misc.h"
50#include "s_user.h"
51#include "send.h"
52#include "struct.h"
53#include "sys.h"
54#include "whowas.h"
55
56/* #include <assert.h> -- Now using assert in ircd_log.h */
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60
61/** Linked list containing the full list of all channels */
62struct Channel* GlobalChannelList = 0;
63
64/** Number of struct Membership*'s allocated */
65static unsigned int membershipAllocCount;
66/** Freelist for struct Membership*'s */
67static struct Membership* membershipFreeList;
68/** Freelist for struct Ban*'s */
69static struct Ban* free_bans;
70/** Number of ban structures allocated. */
71static size_t bans_alloc;
72/** Number of ban structures in use. */
73static size_t bans_inuse;
74
75#if !defined(NDEBUG)
76/** return the length (>=0) of a chain of links.
77 * @param lp pointer to the start of the linked list
78 * @return the number of items in the list
79 */
80static int list_length(struct SLink *lp)
81{
82 int count = 0;
83
84 for (; lp; lp = lp->next)
85 ++count;
86 return count;
87}
88#endif
89
90/** Set the mask for a ban, checking for IP masks.
91 * @param[in,out] ban Ban structure to modify.
92 * @param[in] banstr Mask to ban.
93 */
94static void
95set_ban_mask(struct Ban *ban, const char *banstr)
96{
97 char *sep;
98 assert(banstr != NULL);
99 ircd_strncpy(ban->banstr, banstr, sizeof(ban->banstr) - 1);
100 sep = strrchr(banstr, '@');
101 if (sep) {
102 ban->nu_len = sep - banstr;
103 if (ipmask_parse(sep + 1, &ban->address, &ban->addrbits))
104 ban->flags |= BAN_IPMASK;
105 }
106}
107
108/** Allocate a new Ban structure.
109 * @param[in] banstr Ban mask to use.
110 * @return Newly allocated ban.
111 */
112struct Ban *
113make_ban(const char *banstr)
114{
115 struct Ban *ban;
116 if (free_bans) {
117 ban = free_bans;
118 free_bans = free_bans->next;
119 }
120 else if (!(ban = MyMalloc(sizeof(*ban))))
121 return NULL;
122 else
123 bans_alloc++;
124 bans_inuse++;
125 memset(ban, 0, sizeof(*ban));
126 set_ban_mask(ban, banstr);
127 return ban;
128}
129
130/** Deallocate a ban structure.
131 * @param[in] ban Ban to deallocate.
132 */
133void
134free_ban(struct Ban *ban)
135{
136 ban->next = free_bans;
137 free_bans = ban;
138 bans_inuse--;
139}
140
141/** Report ban usage to \a cptr.
142 * @param[in] cptr Client requesting information.
143 */
144void bans_send_meminfo(struct Client *cptr)
145{
146 struct Ban *ban;
147 size_t num_free;
148 for (num_free = 0, ban = free_bans; ban; ban = ban->next)
149 num_free++;
150 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Bans: inuse %zu(%zu) free %zu alloc %zu",
151 bans_inuse, bans_inuse * sizeof(*ban), num_free, bans_alloc);
152}
153
154/** return the struct Membership* that represents a client on a channel
155 * This function finds a struct Membership* which holds the state about
156 * a client on a specific channel. The code is smart enough to iterate
157 * over the channels a user is in, or the users in a channel to find the
158 * user depending on which is likely to be more efficient.
159 *
160 * @param chptr pointer to the channel struct
161 * @param cptr pointer to the client struct
162 *
163 * @returns pointer to the struct Membership representing this client on
164 * this channel. Returns NULL if the client is not on the channel.
165 * Returns NULL if the client is actually a server.
166 * @see find_channel_member()
167 */
168struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
169{
170 struct Membership *m;
171 assert(0 != cptr);
172 assert(0 != chptr);
173
174 /* Servers don't have member links */
175 if (IsServer(cptr)||IsMe(cptr))
176 return 0;
177
178 /* +k users are typically on a LOT of channels. So we iterate over who
179 * is in the channel. X/W are +k and are in about 5800 channels each.
180 * however there are typically no more than 1000 people in a channel
181 * at a time.
182 */
183 if (IsChannelService(cptr)) {
184 m = chptr->members;
185 while (m) {
186 assert(m->channel == chptr);
187 if (m->user == cptr)
188 return m;
189 m = m->next_member;
190 }
191 }
192 /* Users on the other hand aren't allowed on more than 15 channels. 50%
193 * of users that are on channels are on 2 or less, 95% are on 7 or less,
194 * and 99% are on 10 or less.
195 */
196 else {
197 m = (cli_user(cptr))->channel;
198 while (m) {
199 assert(m->user == cptr);
200 if (m->channel == chptr)
201 return m;
202 m = m->next_channel;
203 }
204 }
205 return 0;
206}
207
208/** Find the client structure for a nick name (user)
209 * Find the client structure for a nick name (user)
210 * using history mechanism if necessary. If the client is not found, an error
211 * message (NO SUCH NICK) is generated. If the client was found
212 * through the history, chasing will be 1 and otherwise 0.
213 *
214 * This function was used extensively in the P09 days, and since we now have
215 * numeric nicks is no longer quite as important.
216 *
217 * @param sptr Pointer to the client that has requested the search
218 * @param user a string representing the client to be found
219 * @param chasing a variable set to 0 if the user was found directly,
220 * 1 otherwise
221 * @returns a pointer the client, or NULL if the client wasn't found.
222 */
223struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
224{
225 struct Client* who = FindClient(user);
226
227 if (chasing)
228 *chasing = 0;
229 if (who)
230 return who;
231
232 if (!(who = get_history(user, feature_int(FEAT_KILLCHASETIMELIMIT)))) {
233 send_reply(sptr, ERR_NOSUCHNICK, user);
234 return 0;
235 }
236 if (chasing)
237 *chasing = 1;
238 return who;
239}
240
241/** Decrement the count of users, and free if empty.
242 * Subtract one user from channel i (and free channel * block, if channel
243 * became empty).
244 *
245 * @param chptr The channel to subtract one from.
246 *
247 * @returns true (1) if channel still has members.
248 * false (0) if the channel is now empty.
249 */
250int sub1_from_channel(struct Channel* chptr)
251{
252 if (chptr->users > 1) /* Can be 0, called for an empty channel too */
253 {
254 assert(0 != chptr->members);
255 --chptr->users;
256 return 1;
257 }
258
259 chptr->users = 0;
260
052b069e 261 /*
262 * Also channels without Apass set need to be kept alive,
263 * otherwise Bad Guys(tm) would be able to takeover
264 * existing channels too easily, and then set an Apass!
265 * However, if a channel without Apass becomes empty
266 * then we try to be kind to them and remove possible
267 * limiting modes.
268 */
269 chptr->mode.mode &= ~MODE_INVITEONLY;
270 chptr->mode.limit = 0;
271 /*
272 * We do NOT reset a possible key or bans because when
273 * the 'channel owners' can't get in because of a key
274 * or ban then apparently there was a fight/takeover
275 * on the channel and we want them to contact IRC opers
276 * who then will educate them on the use of Apass/Upass.
189935b1 277 */
052b069e 278 if (!chptr->mode.apass[0]) /* If no Apass, reset all modes. */
279 {
280 struct Ban *link, *next;
281 chptr->mode.mode = 0;
282 *chptr->mode.key = '\0';
283 while (chptr->invites)
284 del_invite(chptr->invites->value.cptr, chptr);
285 for (link = chptr->banlist; link; link = next) {
286 next = link->next;
287 free_ban(link);
288 }
289 chptr->banlist = NULL;
290
291 /* Immediately destruct empty -A channels if not using apass. */
292 if (!feature_bool(FEAT_OPLEVELS))
293 {
294 destruct_channel(chptr);
295 return 0;
296 }
297 }
298 if (TStime() - chptr->creationtime < 172800) /* Channel younger than 48 hours? */
189935b1 299 schedule_destruct_event_1m(chptr); /* Get rid of it in approximately 4-5 minutes */
300 else
301 schedule_destruct_event_48h(chptr); /* Get rid of it in approximately 48 hours */
302
303 return 0;
304}
305
306/** Destroy an empty channel
307 * This function destroys an empty channel, removing it from hashtables,
308 * and removing any resources it may have consumed.
309 *
310 * @param chptr The channel to destroy
311 *
312 * @returns 0 (success)
313 *
314 * FIXME: Change to return void, this function never fails.
315 */
316int destruct_channel(struct Channel* chptr)
317{
318 struct Ban *ban, *next;
319
320 assert(0 == chptr->members);
321
322 /*
323 * Now, find all invite links from channel structure
324 */
325 while (chptr->invites)
326 del_invite(chptr->invites->value.cptr, chptr);
327
328 for (ban = chptr->banlist; ban; ban = next)
329 {
330 next = ban->next;
331 free_ban(ban);
332 }
333 if (chptr->prev)
334 chptr->prev->next = chptr->next;
335 else
336 GlobalChannelList = chptr->next;
337 if (chptr->next)
338 chptr->next->prev = chptr->prev;
339 hRemChannel(chptr);
340 --UserStats.channels;
341 /*
342 * make sure that channel actually got removed from hash table
343 */
344 assert(chptr->hnext == chptr);
345 MyFree(chptr);
346 return 0;
347}
348
349/** returns Membership * if a person is joined and not a zombie
350 * @param cptr Client
351 * @param chptr Channel
352 * @returns pointer to the client's struct Membership * on the channel if that
353 * user is a full member of the channel, or NULL otherwise.
354 *
355 * @see find_member_link()
356 */
357struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
358{
359 struct Membership* member;
360 assert(0 != chptr);
361
362 member = find_member_link(chptr, cptr);
363 return (member && !IsZombie(member)) ? member : 0;
364}
365
366/** Searches for a ban from a ban list that matches a user.
367 * @param[in] cptr The client to test.
368 * @param[in] banlist The list of bans to test.
369 * @return Pointer to a matching ban, or NULL if none exit.
370 */
371struct Ban *find_ban(struct Client *cptr, struct Ban *banlist)
372{
373 char nu[NICKLEN + USERLEN + 2];
374 char tmphost[HOSTLEN + 1];
375 char iphost[SOCKIPLEN + 1];
376 char *hostmask;
377 char *sr;
378 struct Ban *found;
379
380 /* Build nick!user and alternate host names. */
381 ircd_snprintf(0, nu, sizeof(nu), "%s!%s",
382 cli_name(cptr), cli_user(cptr)->username);
383 ircd_ntoa_r(iphost, &cli_ip(cptr));
384 if (!IsAccount(cptr))
385 sr = NULL;
386 else if (HasHiddenHost(cptr))
387 sr = cli_user(cptr)->realhost;
388 else
389 {
390 ircd_snprintf(0, tmphost, HOSTLEN, "%s.%s",
391 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
392 sr = tmphost;
393 }
394
395 /* Walk through ban list. */
396 for (found = NULL; banlist; banlist = banlist->next) {
397 int res;
398 /* If we have found a positive ban already, only consider exceptions. */
399 if (found && !(banlist->flags & BAN_EXCEPTION))
400 continue;
401 /* Compare nick!user portion of ban. */
402 banlist->banstr[banlist->nu_len] = '\0';
403 res = match(banlist->banstr, nu);
404 banlist->banstr[banlist->nu_len] = '@';
405 if (res)
406 continue;
407 /* Compare host portion of ban. */
408 hostmask = banlist->banstr + banlist->nu_len + 1;
409 if (!((banlist->flags & BAN_IPMASK)
410 && ipmask_check(&cli_ip(cptr), &banlist->address, banlist->addrbits))
411 && match(hostmask, cli_user(cptr)->host)
412 && !(sr && !match(hostmask, sr)))
413 continue;
414 /* If an exception matches, no ban can match. */
415 if (banlist->flags & BAN_EXCEPTION)
416 return NULL;
417 /* Otherwise, remember this ban but keep searching for an exception. */
418 found = banlist;
419 }
420 return found;
421}
422
423/**
424 * This function returns true if the user is banned on the said channel.
425 * This function will check the ban cache if applicable, otherwise will
426 * do the comparisons and cache the result.
427 *
428 * @param[in] member The Membership to test for banned-ness.
429 * @return Non-zero if the member is banned, zero if not.
430 */
431static int is_banned(struct Membership* member)
432{
433 if (IsBanValid(member))
434 return IsBanned(member);
435
436 SetBanValid(member);
437 if (find_ban(member->user, member->channel->banlist)) {
438 SetBanned(member);
439 return 1;
440 } else {
441 ClearBanned(member);
442 return 0;
443 }
444}
445
446/** add a user to a channel.
447 * adds a user to a channel by adding another link to the channels member
448 * chain.
449 *
450 * @param chptr The channel to add to.
451 * @param who The user to add.
452 * @param flags The flags the user gets initially.
453 * @param oplevel The oplevel the user starts with.
454 */
455void add_user_to_channel(struct Channel* chptr, struct Client* who,
456 unsigned int flags, int oplevel)
457{
458 assert(0 != chptr);
459 assert(0 != who);
460
461 if (cli_user(who)) {
462
463 struct Membership* member = membershipFreeList;
464 if (member)
465 membershipFreeList = member->next_member;
466 else {
467 member = (struct Membership*) MyMalloc(sizeof(struct Membership));
468 ++membershipAllocCount;
469 }
470
471 assert(0 != member);
472 member->user = who;
473 member->channel = chptr;
474 member->status = flags;
475 SetOpLevel(member, oplevel);
476
477 member->next_member = chptr->members;
478 if (member->next_member)
479 member->next_member->prev_member = member;
480 member->prev_member = 0;
481 chptr->members = member;
482
483 member->next_channel = (cli_user(who))->channel;
484 if (member->next_channel)
485 member->next_channel->prev_channel = member;
486 member->prev_channel = 0;
487 (cli_user(who))->channel = member;
488
489 if (chptr->destruct_event)
490 remove_destruct_event(chptr);
491 ++chptr->users;
492 ++((cli_user(who))->joined);
493 }
494}
495
496/** Remove a person from a channel, given their Membership*
497 *
498 * @param member A member of a channel.
499 *
500 * @returns true if there are more people in the channel.
501 */
502static int remove_member_from_channel(struct Membership* member)
503{
504 struct Channel* chptr;
505 assert(0 != member);
506 chptr = member->channel;
507 /*
508 * unlink channel member list
509 */
510 if (member->next_member)
511 member->next_member->prev_member = member->prev_member;
512 if (member->prev_member)
513 member->prev_member->next_member = member->next_member;
514 else
515 member->channel->members = member->next_member;
516
517 /*
518 * If this is the last delayed-join user, may have to clear WASDELJOINS.
519 */
520 if (IsDelayedJoin(member))
521 CheckDelayedJoins(chptr);
522
523 /*
524 * unlink client channel list
525 */
526 if (member->next_channel)
527 member->next_channel->prev_channel = member->prev_channel;
528 if (member->prev_channel)
529 member->prev_channel->next_channel = member->next_channel;
530 else
531 (cli_user(member->user))->channel = member->next_channel;
532
533 --(cli_user(member->user))->joined;
534
535 member->next_member = membershipFreeList;
536 membershipFreeList = member;
537
538 return sub1_from_channel(chptr);
539}
540
541/** Check if all the remaining members on the channel are zombies
542 *
543 * @returns False if the channel has any non zombie members, True otherwise.
544 * @see \ref zombie
545 */
546static int channel_all_zombies(struct Channel* chptr)
547{
548 struct Membership* member;
549
550 for (member = chptr->members; member; member = member->next_member) {
551 if (!IsZombie(member))
552 return 0;
553 }
554 return 1;
555}
556
557
558/** Remove a user from a channel
559 * This is the generic entry point for removing a user from a channel, this
560 * function will remove the client from the channel, and destroy the channel
561 * if there are no more normal users left.
562 *
563 * @param cptr The client
564 * @param chptr The channel
565 */
566void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
567{
568
569 struct Membership* member;
570 assert(0 != chptr);
571
572 if ((member = find_member_link(chptr, cptr))) {
573 if (remove_member_from_channel(member)) {
574 if (channel_all_zombies(chptr)) {
575 /*
576 * XXX - this looks dangerous but isn't if we got the referential
577 * integrity right for channels
578 */
579 while (remove_member_from_channel(chptr->members))
580 ;
581 }
582 }
583 }
584}
585
586/** Remove a user from all channels they are on.
587 *
588 * This function removes a user from all channels they are on.
589 *
590 * @param cptr The client to remove.
591 */
592void remove_user_from_all_channels(struct Client* cptr)
593{
594 struct Membership* chan;
595 assert(0 != cptr);
596 assert(0 != cli_user(cptr));
597
598 while ((chan = (cli_user(cptr))->channel))
599 remove_user_from_channel(cptr, chan->channel);
600}
601
602/** Check if this user is a legitimate chanop
603 *
604 * @param cptr Client to check
605 * @param chptr Channel to check
606 *
607 * @returns True if the user is a chanop (And not a zombie), False otherwise.
608 * @see \ref zombie
609 */
610int is_chan_op(struct Client *cptr, struct Channel *chptr)
611{
612 struct Membership* member;
613 assert(chptr);
614 if ((member = find_member_link(chptr, cptr)))
615 return (!IsZombie(member) && IsChanOp(member));
616
617 return 0;
618}
619
620/** Check if a user is a Zombie on a specific channel.
621 *
622 * @param cptr The client to check.
623 * @param chptr The channel to check.
624 *
625 * @returns True if the client (cptr) is a zombie on the channel (chptr),
626 * False otherwise.
627 *
628 * @see \ref zombie
629 */
630int is_zombie(struct Client *cptr, struct Channel *chptr)
631{
632 struct Membership* member;
633
634 assert(0 != chptr);
635
636 if ((member = find_member_link(chptr, cptr)))
637 return IsZombie(member);
638 return 0;
639}
640
641/** Returns if a user has voice on a channel.
642 *
643 * @param cptr The client
644 * @param chptr The channel
645 *
646 * @returns True if the client (cptr) is voiced on (chptr) and is not a zombie.
647 * @see \ref zombie
648 */
649int has_voice(struct Client* cptr, struct Channel* chptr)
650{
651 struct Membership* member;
652
653 assert(0 != chptr);
654 if ((member = find_member_link(chptr, cptr)))
655 return (!IsZombie(member) && HasVoice(member));
656
657 return 0;
658}
659
660/** Can this member send to a channel
661 *
662 * A user can speak on a channel iff:
663 * <ol>
664 * <li> They didn't use the Apass to gain ops.
665 * <li> They are op'd or voice'd.
666 * <li> You aren't banned.
667 * <li> The channel isn't +m
668 * <li> The channel isn't +n or you are on the channel.
669 * </ol>
670 *
671 * This function will optionally reveal a user on a delayed join channel if
672 * they are allowed to send to the channel.
673 *
674 * @param member The membership of the user
675 * @param reveal If true, the user will be "revealed" on a delayed
052b069e 676 * joined channel.
189935b1 677 *
678 * @returns True if the client can speak on the channel.
679 */
680int member_can_send_to_channel(struct Membership* member, int reveal)
681{
682 assert(0 != member);
683
052b069e 684 /* Do not check for users on other servers: This should be a
685 * temporary desynch, or maybe they are on an older server, but
686 * we do not want to send ERR_CANNOTSENDTOCHAN more than once.
687 */
688 if (!MyUser(member->user))
689 {
690 if (IsDelayedJoin(member) && reveal)
691 RevealDelayedJoin(member);
692 return 1;
693 }
694
695 /* Discourage using the Apass to get op. They should use the Upass. */
189935b1 696 if (IsChannelManager(member) && member->channel->mode.apass[0])
697 return 0;
698
052b069e 699 /* If you have voice or ops, you can speak. */
189935b1 700 if (IsVoicedOrOpped(member))
701 return 1;
702
703 /*
704 * If it's moderated, and you aren't a privileged user, you can't
705 * speak.
706 */
707 if (member->channel->mode.mode & MODE_MODERATED)
708 return 0;
052b069e 709
189935b1 710 /* If only logged in users may join and you're not one, you can't speak. */
711 if (member->channel->mode.mode & MODE_REGONLY && !IsAccount(member->user))
712 return 0;
052b069e 713
714 /* If you're banned then you can't speak either. */
715 if (is_banned(member))
189935b1 716 return 0;
717
718 if (IsDelayedJoin(member) && reveal)
719 RevealDelayedJoin(member);
720
721 return 1;
722}
723
724/** Check if a client can send to a channel.
725 *
726 * Has the added check over member_can_send_to_channel() of servers can
727 * always speak.
728 *
729 * @param cptr The client to check
730 * @param chptr The channel to check
731 * @param reveal If the user should be revealed (see
732 * member_can_send_to_channel())
733 *
734 * @returns true if the client is allowed to speak on the channel, false
735 * otherwise
736 *
737 * @see member_can_send_to_channel()
738 */
739int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr, int reveal)
740{
741 struct Membership *member;
742 assert(0 != cptr);
743 /*
744 * Servers can always speak on channels.
745 */
746 if (IsServer(cptr))
747 return 1;
748
749 member = find_channel_member(cptr, chptr);
750
751 /*
752 * You can't speak if you're off channel, and it is +n (no external messages)
753 * or +m (moderated).
754 */
755 if (!member) {
756 if ((chptr->mode.mode & (MODE_NOPRIVMSGS|MODE_MODERATED)) ||
757 ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(cptr)))
758 return 0;
759 else
760 return !find_ban(cptr, chptr->banlist);
761 }
762 return member_can_send_to_channel(member, reveal);
763}
764
765/** Returns the name of a channel that prevents the user from changing nick.
766 * if a member and not (opped or voiced) and (banned or moderated), return
767 * the name of the first channel banned on.
768 *
769 * @param cptr The client
770 *
771 * @returns the name of the first channel banned on, or NULL if the user
772 * can change nicks.
773 */
774const char* find_no_nickchange_channel(struct Client* cptr)
775{
776 if (MyUser(cptr)) {
777 struct Membership* member;
778 for (member = (cli_user(cptr))->channel; member;
779 member = member->next_channel) {
052b069e 780 if (IsVoicedOrOpped(member))
781 continue;
782 if ((member->channel->mode.mode & MODE_MODERATED)
783 || (member->channel->mode.mode & MODE_REGONLY && !IsAccount(cptr))
784 || is_banned(member))
189935b1 785 return member->channel->chname;
786 }
787 }
788 return 0;
789}
790
791
792/** Fill mbuf/pbuf with modes from chptr
793 * write the "simple" list of channel modes for channel chptr onto buffer mbuf
794 * with the parameters in pbuf as visible by cptr.
795 *
796 * This function will hide keys from non-op'd, non-server clients.
797 *
798 * @param cptr The client to generate the mode for.
799 * @param mbuf The buffer to write the modes into.
800 * @param pbuf The buffer to write the mode parameters into.
801 * @param buflen The length of the buffers.
802 * @param chptr The channel to get the modes from.
803 * @param member The membership of this client on this channel (or NULL
804 * if this client isn't on this channel)
805 *
806 */
807void channel_modes(struct Client *cptr, char *mbuf, char *pbuf, int buflen,
808 struct Channel *chptr, struct Membership *member)
809{
810 int previous_parameter = 0;
811
812 assert(0 != mbuf);
813 assert(0 != pbuf);
814 assert(0 != chptr);
815
816 *mbuf++ = '+';
817 if (chptr->mode.mode & MODE_SECRET)
818 *mbuf++ = 's';
819 else if (chptr->mode.mode & MODE_PRIVATE)
820 *mbuf++ = 'p';
821 if (chptr->mode.mode & MODE_MODERATED)
822 *mbuf++ = 'm';
823 if (chptr->mode.mode & MODE_TOPICLIMIT)
824 *mbuf++ = 't';
825 if (chptr->mode.mode & MODE_INVITEONLY)
826 *mbuf++ = 'i';
827 if (chptr->mode.mode & MODE_NOPRIVMSGS)
828 *mbuf++ = 'n';
829 if (chptr->mode.mode & MODE_REGONLY)
830 *mbuf++ = 'r';
831 if (chptr->mode.mode & MODE_DELJOINS)
832 *mbuf++ = 'D';
833 else if (MyUser(cptr) && (chptr->mode.mode & MODE_WASDELJOINS))
834 *mbuf++ = 'd';
835 if (chptr->mode.limit) {
836 *mbuf++ = 'l';
837 ircd_snprintf(0, pbuf, buflen, "%u", chptr->mode.limit);
838 previous_parameter = 1;
839 }
840
841 if (*chptr->mode.key) {
842 *mbuf++ = 'k';
843 if (previous_parameter)
844 strcat(pbuf, " ");
845 if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
846 strcat(pbuf, chptr->mode.key);
847 } else
848 strcat(pbuf, "*");
849 previous_parameter = 1;
850 }
851 if (*chptr->mode.apass) {
852 *mbuf++ = 'A';
853 if (previous_parameter)
854 strcat(pbuf, " ");
855 if (IsServer(cptr)) {
856 strcat(pbuf, chptr->mode.apass);
857 } else
858 strcat(pbuf, "*");
859 previous_parameter = 1;
860 }
861 if (*chptr->mode.upass) {
862 *mbuf++ = 'U';
863 if (previous_parameter)
864 strcat(pbuf, " ");
865 if (IsServer(cptr) || (member && IsChanOp(member) && OpLevel(member) == 0)) {
866 strcat(pbuf, chptr->mode.upass);
867 } else
868 strcat(pbuf, "*");
869 }
870 *mbuf = '\0';
871}
872
873/** Compare two members oplevel
874 *
875 * @param mp1 Pointer to a pointer to a membership
876 * @param mp2 Pointer to a pointer to a membership
877 *
878 * @returns 0 if equal, -1 if mp1 is lower, +1 otherwise.
879 *
880 * Used for qsort(3).
881 */
882int compare_member_oplevel(const void *mp1, const void *mp2)
883{
884 struct Membership const* member1 = *(struct Membership const**)mp1;
885 struct Membership const* member2 = *(struct Membership const**)mp2;
886 if (member1->oplevel == member2->oplevel)
887 return 0;
888 return (member1->oplevel < member2->oplevel) ? -1 : 1;
889}
890
891/* send "cptr" a full list of the modes for channel chptr.
892 *
893 * Sends a BURST line to cptr, bursting all the modes for the channel.
894 *
895 * @param cptr Client pointer
896 * @param chptr Channel pointer
897 */
898void send_channel_modes(struct Client *cptr, struct Channel *chptr)
899{
900 /* The order in which modes are generated is now mandatory */
901 static unsigned int current_flags[4] =
902 { 0, CHFL_VOICE, CHFL_CHANOP, CHFL_CHANOP | CHFL_VOICE };
903 int first = 1;
904 int full = 1;
905 int flag_cnt = 0;
906 int new_mode = 0;
907 size_t len;
908 struct Membership* member;
909 struct Ban* lp2;
910 char modebuf[MODEBUFLEN];
911 char parabuf[MODEBUFLEN];
912 struct MsgBuf *mb;
913 int number_of_ops = 0;
914 int opped_members_index = 0;
915 struct Membership** opped_members = NULL;
916 int last_oplevel = 0;
917 int feat_oplevels = (chptr->mode.apass[0]) != '\0';
918
919 assert(0 != cptr);
920 assert(0 != chptr);
921
922 if (IsLocalChannel(chptr->chname))
923 return;
924
925 member = chptr->members;
926 lp2 = chptr->banlist;
927
928 *modebuf = *parabuf = '\0';
929 channel_modes(cptr, modebuf, parabuf, sizeof(parabuf), chptr, 0);
930
931 for (first = 1; full; first = 0) /* Loop for multiple messages */
932 {
933 full = 0; /* Assume by default we get it
934 all in one message */
935
936 /* (Continued) prefix: "<Y> B <channel> <TS>" */
937 /* is there any better way we can do this? */
938 mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
939 chptr->creationtime);
940
941 if (first && modebuf[1]) /* Add simple modes (Aiklmnpstu)
942 if first message */
943 {
944 /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
945 msgq_append(&me, mb, " %s", modebuf);
946
947 if (*parabuf)
948 msgq_append(&me, mb, " %s", parabuf);
949 }
950
951 /*
952 * Attach nicks, comma separated " nick[:modes],nick[:modes],..."
953 *
954 * First find all opless members.
955 * Run 2 times over all members, to group the members with
956 * and without voice together.
957 * Then run 2 times over all opped members (which are ordered
958 * by op-level) to also group voice and non-voice together.
959 */
960 for (first = 1; flag_cnt < 4; new_mode = 1, ++flag_cnt)
961 {
962 while (member)
963 {
964 if (flag_cnt < 2 && IsChanOp(member))
965 {
966 /*
967 * The first loop (to find all non-voice/op), we count the ops.
968 * The second loop (to find all voiced non-ops), store the ops
969 * in a dynamic array.
970 */
971 if (flag_cnt == 0)
972 ++number_of_ops;
973 else
974 opped_members[opped_members_index++] = member;
975 }
976 /* Only handle the members with the flags that we are interested in. */
977 if ((member->status & CHFL_VOICED_OR_OPPED) == current_flags[flag_cnt])
978 {
979 if (msgq_bufleft(mb) < NUMNICKLEN + 3 + MAXOPLEVELDIGITS)
980 /* The 3 + MAXOPLEVELDIGITS is a possible ",:v999". */
981 {
982 full = 1; /* Make sure we continue after
983 sending it so far */
984 /* Ensure the new BURST line contains the current
985 * ":mode", except when there is no mode yet. */
986 new_mode = (flag_cnt > 0) ? 1 : 0;
987 break; /* Do not add this member to this message */
988 }
989 msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
990 first = 0; /* From now on, use commas to add new nicks */
991
992 /*
993 * Do we have a nick with a new mode ?
994 * Or are we starting a new BURST line?
995 */
996 if (new_mode)
997 {
998 /*
999 * This means we are at the _first_ member that has only
1000 * voice, or the first member that has only ops, or the
1001 * first member that has voice and ops (so we get here
1002 * at most three times, plus once for every start of
1003 * a continued BURST line where only these modes is current.
1004 * In the two cases where the current mode includes ops,
1005 * we need to add the _absolute_ value of the oplevel to the mode.
1006 */
1007 char tbuf[3 + MAXOPLEVELDIGITS] = ":";
1008 int loc = 1;
1009
1010 if (HasVoice(member)) /* flag_cnt == 1 or 3 */
1011 tbuf[loc++] = 'v';
1012 if (IsChanOp(member)) /* flag_cnt == 2 or 3 */
1013 {
1014 /* append the absolute value of the oplevel */
1015 if (feat_oplevels)
1016 loc += ircd_snprintf(0, tbuf + loc, sizeof(tbuf) - loc, "%u", last_oplevel = member->oplevel);
1017 else
1018 tbuf[loc++] = 'o';
1019 }
1020 tbuf[loc] = '\0';
1021 msgq_append(&me, mb, tbuf);
1022 new_mode = 0;
1023 }
1024 else if (feat_oplevels && flag_cnt > 1 && last_oplevel != member->oplevel)
1025 {
1026 /*
1027 * This can't be the first member of a (continued) BURST
1028 * message because then either flag_cnt == 0 or new_mode == 1
1029 * Now we need to append the incremental value of the oplevel.
1030 */
1031 char tbuf[2 + MAXOPLEVELDIGITS];
1032 ircd_snprintf(0, tbuf, sizeof(tbuf), ":%u", member->oplevel - last_oplevel);
1033 last_oplevel = member->oplevel;
1034 msgq_append(&me, mb, tbuf);
1035 }
1036 }
1037 /* Go to the next `member'. */
1038 if (flag_cnt < 2)
1039 member = member->next_member;
1040 else
1041 member = opped_members[++opped_members_index];
1042 }
1043 if (full)
1044 break;
1045
1046 /* Point `member' at the start of the list again. */
1047 if (flag_cnt == 0)
1048 {
1049 member = chptr->members;
1050 /* Now, after one loop, we know the number of ops and can
1051 * allocate the dynamic array with pointer to the ops. */
1052 opped_members = (struct Membership**)
1053 MyMalloc((number_of_ops + 1) * sizeof(struct Membership*));
1054 opped_members[number_of_ops] = NULL; /* Needed for loop termination */
1055 }
1056 else
1057 {
1058 /* At the end of the second loop, sort the opped members with
1059 * increasing op-level, so that we will output them in the
1060 * correct order (and all op-level increments stay positive) */
1061 if (flag_cnt == 1)
1062 qsort(opped_members, number_of_ops,
1063 sizeof(struct Membership*), compare_member_oplevel);
1064 /* The third and fourth loop run only over the opped members. */
1065 member = opped_members[(opped_members_index = 0)];
1066 }
1067
1068 } /* loop over 0,+v,+o,+ov */
1069
1070 if (!full)
1071 {
1072 /* Attach all bans, space separated " :%ban ban ..." */
1073 for (first = 2; lp2; lp2 = lp2->next)
1074 {
1075 len = strlen(lp2->banstr);
1076 if (msgq_bufleft(mb) < len + 1 + first)
1077 /* The +1 stands for the added ' '.
1078 * The +first stands for the added ":%".
1079 */
1080 {
1081 full = 1;
1082 break;
1083 }
1084 msgq_append(&me, mb, " %s%s", first ? ":%" : "",
1085 lp2->banstr);
1086 first = 0;
1087 }
1088 }
1089
1090 send_buffer(cptr, mb, 0); /* Send this message */
1091 msgq_clean(mb);
1092 } /* Continue when there was something
1093 that didn't fit (full==1) */
1094 if (opped_members)
1095 MyFree(opped_members);
1096 if (feature_bool(FEAT_TOPIC_BURST) && (chptr->topic[0] != '\0'))
1097 sendcmdto_one(&me, CMD_TOPIC, cptr, "%H %Tu %Tu :%s", chptr,
1098 chptr->creationtime, chptr->topic_time, chptr->topic);
1099}
1100
1101/** Canonify a mask.
1102 * pretty_mask
1103 *
1104 * @author Carlo Wood (Run),
1105 * 05 Oct 1998.
1106 *
1107 * When the nick is longer then NICKLEN, it is cut off (its an error of course).
1108 * When the user name or host name are too long (USERLEN and HOSTLEN
1109 * respectively) then they are cut off at the start with a '*'.
1110 *
1111 * The following transformations are made:
1112 *
1113 * 1) xxx -> nick!*@*
1114 * 2) xxx.xxx -> *!*\@host
1115 * 3) xxx\!yyy -> nick!user\@*
1116 * 4) xxx\@yyy -> *!user\@host
1117 * 5) xxx!yyy\@zzz -> nick!user\@host
1118 *
1119 * @param mask The uncanonified mask.
1120 * @returns The updated mask in a static buffer.
1121 */
1122char *pretty_mask(char *mask)
1123{
1124 static char star[2] = { '*', 0 };
1125 static char retmask[NICKLEN + USERLEN + HOSTLEN + 3];
1126 char *last_dot = NULL;
1127 char *ptr;
1128
1129 /* Case 1: default */
1130 char *nick = mask;
1131 char *user = star;
1132 char *host = star;
1133
1134 /* Do a _single_ pass through the characters of the mask: */
1135 for (ptr = mask; *ptr; ++ptr)
1136 {
1137 if (*ptr == '!')
1138 {
1139 /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1140 user = ++ptr;
1141 host = star;
1142 }
1143 else if (*ptr == '@')
1144 {
1145 /* Case 4: Found last '@' (without finding a '!' yet) */
1146 nick = star;
1147 user = mask;
1148 host = ++ptr;
1149 }
1150 else if (*ptr == '.' || *ptr == ':')
1151 {
1152 /* Case 2: Found character specific to IP or hostname (without
1153 * finding a '!' or '@' yet) */
1154 last_dot = ptr;
1155 continue;
1156 }
1157 else
1158 continue;
1159 for (; *ptr; ++ptr)
1160 {
1161 if (*ptr == '@')
1162 {
1163 /* Case 4 or 5: Found last '@' */
1164 host = ptr + 1;
1165 }
1166 }
1167 break;
1168 }
1169 if (user == star && last_dot)
1170 {
1171 /* Case 2: */
1172 nick = star;
1173 user = star;
1174 host = mask;
1175 }
1176 /* Check lengths */
1177 if (nick != star)
1178 {
1179 char *nick_end = (user != star) ? user - 1 : ptr;
1180 if (nick_end - nick > NICKLEN)
1181 nick[NICKLEN] = 0;
1182 *nick_end = 0;
1183 }
1184 if (user != star)
1185 {
1186 char *user_end = (host != star) ? host - 1 : ptr;
1187 if (user_end - user > USERLEN)
1188 {
1189 user = user_end - USERLEN;
1190 *user = '*';
1191 }
1192 *user_end = 0;
1193 }
1194 if (host != star && ptr - host > HOSTLEN)
1195 {
1196 host = ptr - HOSTLEN;
1197 *host = '*';
1198 }
1199 ircd_snprintf(0, retmask, sizeof(retmask), "%s!%s@%s", nick, user, host);
1200 return retmask;
1201}
1202
1203/** send a banlist to a client for a channel
1204 *
1205 * @param cptr Client to send the banlist to.
1206 * @param chptr Channel whose banlist to send.
1207 */
1208static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1209{
1210 struct Ban* lp;
1211
1212 assert(0 != cptr);
1213 assert(0 != chptr);
1214
1215 for (lp = chptr->banlist; lp; lp = lp->next)
1216 send_reply(cptr, RPL_BANLIST, chptr->chname, lp->banstr,
1217 lp->who, lp->when);
1218
1219 send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1220}
1221
1222/** Remove bells and commas from channel name
1223 *
1224 * @param cn Channel name to clean, modified in place.
1225 */
1226void clean_channelname(char *cn)
1227{
1228 int i;
1229
1230 for (i = 0; cn[i]; i++) {
1231 if (i >= IRCD_MIN(CHANNELLEN, feature_int(FEAT_CHANNELLEN))
1232 || !IsChannelChar(cn[i])) {
1233 cn[i] = '\0';
1234 return;
1235 }
1236 if (IsChannelLower(cn[i])) {
1237 cn[i] = ToLower(cn[i]);
1238#ifndef FIXME
1239 /*
1240 * Remove for .08+
1241 * toupper(0xd0)
1242 */
1243 if ((unsigned char)(cn[i]) == 0xd0)
1244 cn[i] = (char) 0xf0;
1245#endif
1246 }
1247 }
1248}
1249
1250/** Get a channel block, creating if necessary.
1251 * Get Channel block for chname (and allocate a new channel
1252 * block, if it didn't exists before).
1253 *
1254 * @param cptr Client joining the channel.
1255 * @param chname The name of the channel to join.
1256 * @param flag set to CGT_CREATE to create the channel if it doesn't
1257 * exist
1258 *
1259 * @returns NULL if the channel is invalid, doesn't exist and CGT_CREATE
1260 * wasn't specified or a pointer to the channel structure
1261 */
1262struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1263{
1264 struct Channel *chptr;
1265 int len;
1266
1267 if (EmptyString(chname))
1268 return NULL;
1269
1270 len = strlen(chname);
1271 if (MyUser(cptr) && len > CHANNELLEN)
1272 {
1273 len = CHANNELLEN;
1274 *(chname + CHANNELLEN) = '\0';
1275 }
1276 if ((chptr = FindChannel(chname)))
1277 return (chptr);
1278 if (flag == CGT_CREATE)
1279 {
1280 chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1281 assert(0 != chptr);
1282 ++UserStats.channels;
1283 memset(chptr, 0, sizeof(struct Channel));
1284 strcpy(chptr->chname, chname);
1285 if (GlobalChannelList)
1286 GlobalChannelList->prev = chptr;
1287 chptr->prev = NULL;
1288 chptr->next = GlobalChannelList;
1289 chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1290 GlobalChannelList = chptr;
1291 hAddChannel(chptr);
1292 }
1293 return chptr;
1294}
1295
1296/** invite a user to a channel.
1297 *
1298 * Adds an invite for a user to a channel. Limits the number of invites
1299 * to FEAT_MAXCHANNELSPERUSER. Does not sent notification to the user.
1300 *
1301 * @param cptr The client to be invited.
1302 * @param chptr The channel to be invited to.
1303 */
1304void add_invite(struct Client *cptr, struct Channel *chptr)
1305{
1306 struct SLink *inv, **tmp;
1307
1308 del_invite(cptr, chptr);
1309 /*
1310 * Delete last link in chain if the list is max length
1311 */
1312 assert(list_length((cli_user(cptr))->invited) == (cli_user(cptr))->invites);
1313 if ((cli_user(cptr))->invites >= feature_int(FEAT_MAXCHANNELSPERUSER))
1314 del_invite(cptr, (cli_user(cptr))->invited->value.chptr);
1315 /*
1316 * Add client to channel invite list
1317 */
1318 inv = make_link();
1319 inv->value.cptr = cptr;
1320 inv->next = chptr->invites;
1321 chptr->invites = inv;
1322 /*
1323 * Add channel to the end of the client invite list
1324 */
1325 for (tmp = &((cli_user(cptr))->invited); *tmp; tmp = &((*tmp)->next));
1326 inv = make_link();
1327 inv->value.chptr = chptr;
1328 inv->next = NULL;
1329 (*tmp) = inv;
1330 (cli_user(cptr))->invites++;
1331}
1332
1333/** Delete an invite
1334 * Delete Invite block from channel invite list and client invite list
1335 *
1336 * @param cptr Client pointer
1337 * @param chptr Channel pointer
1338 */
1339void del_invite(struct Client *cptr, struct Channel *chptr)
1340{
1341 struct SLink **inv, *tmp;
1342
1343 for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1344 if (tmp->value.cptr == cptr)
1345 {
1346 *inv = tmp->next;
1347 free_link(tmp);
1348 tmp = 0;
1349 (cli_user(cptr))->invites--;
1350 break;
1351 }
1352
1353 for (inv = &((cli_user(cptr))->invited); (tmp = *inv); inv = &tmp->next)
1354 if (tmp->value.chptr == chptr)
1355 {
1356 *inv = tmp->next;
1357 free_link(tmp);
1358 tmp = 0;
1359 break;
1360 }
1361}
1362
1363/** @page zombie Explanation of Zombies
1364 *
1365 * Synopsis:
1366 *
1367 * A channel member is turned into a zombie when he is kicked from a
1368 * channel but his server has not acknowledged the kick. Servers that
1369 * see the member as a zombie can accept actions he performed before
1370 * being kicked, without allowing chanop operations from outsiders or
1371 * desyncing the network.
1372 *
1373 * Consider:
1374 * <pre>
1375 * client
1376 * |
1377 * c
1378 * |
1379 * X --a--> A --b--> B --d--> D
1380 * |
1381 * who
1382 * </pre>
1383 *
1384 * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1385 * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1386 *
1387 * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1388 * Remove the user immediately when no users are left on the channel.
1389 * b) On server B : remove the user (who/lp) from the channel, send a
1390 * PART upstream (to A) and pass on the KICK.
1391 * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1392 * channel, and pass on the KICK.
1393 * d) On server D : remove the user (who/lp) from the channel, and pass on
1394 * the KICK.
1395 *
1396 * Note:
1397 * - Setting the ZOMBIE flag never hurts, we either remove the
1398 * client after that or we don't.
1399 * - The KICK message was already passed on, as should be in all cases.
1400 * - `who' is removed in all cases except case a) when users are left.
1401 * - A PART is only sent upstream in case b).
1402 *
1403 * 2 aug 97:
1404 * <pre>
1405 * 6
1406 * |
1407 * 1 --- 2 --- 3 --- 4 --- 5
1408 * | |
1409 * kicker who
1410 * </pre>
1411 *
1412 * We also need to turn 'who' into a zombie on servers 1 and 6,
1413 * because a KICK from 'who' (kicking someone else in that direction)
1414 * can arrive there afterward - which should not be bounced itself.
1415 * Therefore case a) also applies for servers 1 and 6.
1416 *
1417 * --Run
1418 */
1419
1420/** Turn a user on a channel into a zombie
1421 * This function turns a user into a zombie (see \ref zombie)
1422 *
1423 * @param member The structure representing this user on this channel.
1424 * @param who The client that is being kicked.
1425 * @param cptr The connection the kick came from.
1426 * @param sptr The client that is doing the kicking.
1427 * @param chptr The channel the user is being kicked from.
1428 */
1429void make_zombie(struct Membership* member, struct Client* who,
1430 struct Client* cptr, struct Client* sptr, struct Channel* chptr)
1431{
1432 assert(0 != member);
1433 assert(0 != who);
1434 assert(0 != cptr);
1435 assert(0 != chptr);
1436
1437 /* Default for case a): */
1438 SetZombie(member);
1439
1440 /* Case b) or c) ?: */
1441 if (MyUser(who)) /* server 4 */
1442 {
1443 if (IsServer(cptr)) /* Case b) ? */
1444 sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1445 remove_user_from_channel(who, chptr);
1446 return;
1447 }
1448 if (cli_from(who) == cptr) /* True on servers 1, 5 and 6 */
1449 {
1450 struct Client *acptr = IsServer(sptr) ? sptr : (cli_user(sptr))->server;
1451 for (; acptr != &me; acptr = (cli_serv(acptr))->up)
1452 if (acptr == (cli_user(who))->server) /* Case d) (server 5) */
1453 {
1454 remove_user_from_channel(who, chptr);
1455 return;
1456 }
1457 }
1458
1459 /* Case a) (servers 1, 2, 3 and 6) */
1460 if (channel_all_zombies(chptr))
1461 remove_user_from_channel(who, chptr);
1462
1463 /* XXX Can't actually call Debug here; if the channel is all zombies,
1464 * chptr will no longer exist when we get here.
1465 Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1466 */
1467}
1468
1469/** returns the number of zombies on a channel
1470 * @param chptr Channel to count zombies in.
1471 *
1472 * @returns The number of zombies on the channel.
1473 */
1474int number_of_zombies(struct Channel *chptr)
1475{
1476 struct Membership* member;
1477 int count = 0;
1478
1479 assert(0 != chptr);
1480 for (member = chptr->members; member; member = member->next_member) {
1481 if (IsZombie(member))
1482 ++count;
1483 }
1484 return count;
1485}
1486
1487/** Concatenate some strings together.
1488 * This helper function builds an argument string in strptr, consisting
1489 * of the original string, a space, and str1 and str2 concatenated (if,
1490 * of course, str2 is not NULL)
1491 *
1492 * @param strptr The buffer to concatenate into
1493 * @param strptr_i modified offset to the position to modify
1494 * @param str1 The string to concatenate from.
1495 * @param str2 The second string to contatenate from.
1496 * @param c Charactor to separate the string from str1 and str2.
1497 */
1498static void
1499build_string(char *strptr, int *strptr_i, const char *str1,
1500 const char *str2, char c)
1501{
1502 if (c)
1503 strptr[(*strptr_i)++] = c;
1504
1505 while (*str1)
1506 strptr[(*strptr_i)++] = *(str1++);
1507
1508 if (str2)
1509 while (*str2)
1510 strptr[(*strptr_i)++] = *(str2++);
1511
1512 strptr[(*strptr_i)] = '\0';
1513}
1514
1515/** Flush out the modes
1516 * This is the workhorse of our ModeBuf suite; this actually generates the
1517 * output MODE commands, HACK notices, or whatever. It's pretty complicated.
1518 *
1519 * @param mbuf The mode buffer to flush
1520 * @param all If true, flush all modes, otherwise leave partial modes in the
1521 * buffer.
1522 *
1523 * @returns 0
1524 */
1525static int
1526modebuf_flush_int(struct ModeBuf *mbuf, int all)
1527{
1528 /* we only need the flags that don't take args right now */
1529 static int flags[] = {
1530/* MODE_CHANOP, 'o', */
1531/* MODE_VOICE, 'v', */
1532 MODE_PRIVATE, 'p',
1533 MODE_SECRET, 's',
1534 MODE_MODERATED, 'm',
1535 MODE_TOPICLIMIT, 't',
1536 MODE_INVITEONLY, 'i',
1537 MODE_NOPRIVMSGS, 'n',
1538 MODE_REGONLY, 'r',
1539 MODE_DELJOINS, 'D',
189935b1 1540/* MODE_KEY, 'k', */
1541/* MODE_BAN, 'b', */
1542 MODE_LIMIT, 'l',
1543/* MODE_APASS, 'A', */
1544/* MODE_UPASS, 'U', */
1545 0x0, 0x0
1546 };
052b069e 1547 static int local_flags[] = {
1548 MODE_WASDELJOINS, 'd',
1549 0x0, 0x0
1550 };
189935b1 1551 int i;
1552 int *flag_p;
1553
1554 struct Client *app_source; /* where the MODE appears to come from */
1555
052b069e 1556 char addbuf[20], addbuf_local[20]; /* accumulates +psmtin, etc. */
1557 int addbuf_i = 0, addbuf_local_i = 0;
1558 char rembuf[20], rembuf_local[20]; /* accumulates -psmtin, etc. */
1559 int rembuf_i = 0, rembuf_local_i = 0;
189935b1 1560 char *bufptr; /* we make use of indirection to simplify the code */
1561 int *bufptr_i;
1562
1563 char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1564 int addstr_i;
1565 char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1566 int remstr_i;
1567 char *strptr; /* more indirection to simplify the code */
1568 int *strptr_i;
1569
1570 int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1571 int tmp;
1572
1573 char limitbuf[20]; /* convert limits to strings */
1574
1575 unsigned int limitdel = MODE_LIMIT;
1576
1577 assert(0 != mbuf);
1578
1579 /* If the ModeBuf is empty, we have nothing to do */
1580 if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1581 return 0;
1582
1583 /* Ok, if we were given the OPMODE flag, or its a server, hide the source.
1584 */
052b069e 1585 if (feature_bool(FEAT_HIS_MODEWHO) &&
1586 (mbuf->mb_dest & MODEBUF_DEST_OPMODE ||
1587 IsServer(mbuf->mb_source) ||
1588 IsMe(mbuf->mb_source)))
189935b1 1589 app_source = &his;
1590 else
1591 app_source = mbuf->mb_source;
1592
1593 /*
1594 * Account for user we're bouncing; we have to get it in on the first
1595 * bounced MODE, or we could have problems
1596 */
1597 if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1598 totalbuflen -= 6; /* numeric nick == 5, plus one space */
1599
1600 /* Calculate the simple flags */
1601 for (flag_p = flags; flag_p[0]; flag_p += 2) {
1602 if (*flag_p & mbuf->mb_add)
1603 addbuf[addbuf_i++] = flag_p[1];
1604 else if (*flag_p & mbuf->mb_rem)
1605 rembuf[rembuf_i++] = flag_p[1];
1606 }
1607
052b069e 1608 /* Some flags may be for local display only. */
1609 for (flag_p = local_flags; flag_p[0]; flag_p += 2) {
1610 if (*flag_p & mbuf->mb_add)
1611 addbuf_local[addbuf_local_i++] = flag_p[1];
1612 else if (*flag_p & mbuf->mb_rem)
1613 rembuf_local[rembuf_local_i++] = flag_p[1];
1614 }
1615
189935b1 1616 /* Now go through the modes with arguments... */
1617 for (i = 0; i < mbuf->mb_count; i++) {
1618 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1619 bufptr = addbuf;
1620 bufptr_i = &addbuf_i;
1621 } else {
1622 bufptr = rembuf;
1623 bufptr_i = &rembuf_i;
1624 }
1625
1626 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1627 tmp = strlen(cli_name(MB_CLIENT(mbuf, i)));
1628
1629 if ((totalbuflen - IRCD_MAX(9, tmp)) <= 0) /* don't overflow buffer */
1630 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1631 else {
1632 bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1633 totalbuflen -= IRCD_MAX(9, tmp) + 1;
1634 }
1635 } else if (MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS)) {
1636 tmp = strlen(MB_STRING(mbuf, i));
1637
1638 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1639 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1640 else {
1641 char mode_char;
1642 switch(MB_TYPE(mbuf, i) & (MODE_BAN | MODE_APASS | MODE_UPASS))
1643 {
1644 case MODE_APASS:
1645 mode_char = 'A';
1646 break;
1647 case MODE_UPASS:
1648 mode_char = 'U';
1649 break;
1650 default:
1651 mode_char = 'b';
1652 break;
1653 }
1654 bufptr[(*bufptr_i)++] = mode_char;
1655 totalbuflen -= tmp + 1;
1656 }
1657 } else if (MB_TYPE(mbuf, i) & MODE_KEY) {
1658 tmp = (mbuf->mb_dest & MODEBUF_DEST_NOKEY ? 1 :
1659 strlen(MB_STRING(mbuf, i)));
1660
1661 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1662 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1663 else {
1664 bufptr[(*bufptr_i)++] = 'k';
1665 totalbuflen -= tmp + 1;
1666 }
1667 } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1668 /* if it's a limit, we also format the number */
1669 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
1670
1671 tmp = strlen(limitbuf);
1672
1673 if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1674 MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1675 else {
1676 bufptr[(*bufptr_i)++] = 'l';
1677 totalbuflen -= tmp + 1;
1678 }
1679 }
1680 }
1681
1682 /* terminate the mode strings */
1683 addbuf[addbuf_i] = '\0';
1684 rembuf[rembuf_i] = '\0';
052b069e 1685 addbuf_local[addbuf_local_i] = '\0';
1686 rembuf_local[rembuf_local_i] = '\0';
189935b1 1687
1688 /* If we're building a user visible MODE or HACK... */
1689 if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1690 MODEBUF_DEST_HACK3 | MODEBUF_DEST_HACK4 |
1691 MODEBUF_DEST_LOG)) {
1692 /* Set up the parameter strings */
1693 addstr[0] = '\0';
1694 addstr_i = 0;
1695 remstr[0] = '\0';
1696 remstr_i = 0;
1697
1698 for (i = 0; i < mbuf->mb_count; i++) {
1699 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1700 continue;
1701
1702 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1703 strptr = addstr;
1704 strptr_i = &addstr_i;
1705 } else {
1706 strptr = remstr;
1707 strptr_i = &remstr_i;
1708 }
1709
1710 /* deal with clients... */
1711 if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1712 build_string(strptr, strptr_i, cli_name(MB_CLIENT(mbuf, i)), 0, ' ');
1713
1714 /* deal with bans... */
1715 else if (MB_TYPE(mbuf, i) & MODE_BAN)
1716 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1717
1718 /* deal with keys... */
1719 else if (MB_TYPE(mbuf, i) & MODE_KEY)
1720 build_string(strptr, strptr_i, mbuf->mb_dest & MODEBUF_DEST_NOKEY ?
1721 "*" : MB_STRING(mbuf, i), 0, ' ');
1722
1723 /* deal with invisible passwords */
1724 else if (MB_TYPE(mbuf, i) & (MODE_APASS | MODE_UPASS))
1725 build_string(strptr, strptr_i, "*", 0, ' ');
1726
1727 /*
1728 * deal with limit; note we cannot include the limit parameter if we're
1729 * removing it
1730 */
1731 else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1732 (MODE_ADD | MODE_LIMIT))
1733 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1734 }
1735
1736 /* send the messages off to their destination */
1737 if (mbuf->mb_dest & MODEBUF_DEST_HACK2)
1738 sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1739 "[%Tu]",
1740 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1741 mbuf->mb_source : app_source),
1742 mbuf->mb_channel->chname,
1743 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1744 addbuf, remstr, addstr,
1745 mbuf->mb_channel->creationtime);
1746
1747 if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1748 sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1749 "%s%s%s%s%s%s [%Tu]",
1750 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1751 mbuf->mb_source : app_source),
1752 mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1753 rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1754 mbuf->mb_channel->creationtime);
1755
1756 if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1757 sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1758 "[%Tu]",
1759 cli_name(feature_bool(FEAT_HIS_SNOTICES) ?
1760 mbuf->mb_source : app_source),
1761 mbuf->mb_channel->chname,
1762 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1763 addbuf, remstr, addstr,
1764 mbuf->mb_channel->creationtime);
1765
1766 if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1767 log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1768 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1769 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1770 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1771
1772 if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1773 sendcmdto_channel_butserv_butone(app_source, CMD_MODE, mbuf->mb_channel, NULL, 0,
052b069e 1774 "%H %s%s%s%s%s%s%s%s", mbuf->mb_channel,
1775 rembuf_i || rembuf_local_i ? "-" : "",
1776 rembuf, rembuf_local,
1777 addbuf_i || addbuf_local_i ? "+" : "",
1778 addbuf, addbuf_local,
1779 remstr, addstr);
189935b1 1780 }
1781
1782 /* Now are we supposed to propagate to other servers? */
1783 if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1784 /* set up parameter string */
1785 addstr[0] = '\0';
1786 addstr_i = 0;
1787 remstr[0] = '\0';
1788 remstr_i = 0;
1789
1790 /*
1791 * limit is supressed if we're removing it; we have to figure out which
1792 * direction is the direction for it to be removed, though...
1793 */
052b069e 1794 limitdel |= (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) ? MODE_DEL : MODE_ADD;
189935b1 1795
1796 for (i = 0; i < mbuf->mb_count; i++) {
1797 if (MB_TYPE(mbuf, i) & MODE_SAVE)
1798 continue;
1799
1800 if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1801 strptr = addstr;
1802 strptr_i = &addstr_i;
1803 } else {
1804 strptr = remstr;
1805 strptr_i = &remstr_i;
1806 }
1807
9f8856e9 1808 /* if we're changing oplevels and we know the oplevel, pass it on */
189935b1 1809 if (mbuf->mb_channel->mode.apass[0]
1810 && (MB_TYPE(mbuf, i) & MODE_CHANOP)
1811 && MB_OPLEVEL(mbuf, i) < MAXOPLEVEL)
1812 *strptr_i += ircd_snprintf(0, strptr + *strptr_i, BUFSIZE - *strptr_i,
1813 " %s%s:%d",
1814 NumNick(MB_CLIENT(mbuf, i)),
1815 MB_OPLEVEL(mbuf, i));
1816
1817 /* deal with other modes that take clients */
1818 else if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1819 build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1820
1821 /* deal with modes that take strings */
1822 else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN | MODE_APASS | MODE_UPASS))
1823 build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1824
1825 /*
1826 * deal with the limit. Logic here is complicated; if HACK2 is set,
1827 * we're bouncing the mode, so sense is reversed, and we have to
1828 * include the original limit if it looks like it's being removed
1829 */
1830 else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1831 build_string(strptr, strptr_i, limitbuf, 0, ' ');
1832 }
1833
1834 /* we were told to deop the source */
1835 if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1836 addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1837 addbuf[addbuf_i] = '\0'; /* terminate the string... */
1838 build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1839
1840 /* mark that we've done this, so we don't do it again */
1841 mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1842 }
1843
1844 if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1845 /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1846 sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1847 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1848 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1849 addbuf, remstr, addstr);
1850 } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1851 /*
9f8856e9 1852 * If HACK2 was set, we're bouncing; we send the MODE back to
1853 * the connection we got it from with the senses reversed and
1854 * the proper TS; origin is us
189935b1 1855 */
1856 sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1857 mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1858 rembuf_i ? "+" : "", rembuf, addstr, remstr,
1859 mbuf->mb_channel->creationtime);
1860 } else {
1861 /*
9f8856e9 1862 * We're propagating a normal (or HACK3 or HACK4) MODE command
1863 * to the rest of the network. We send the actual channel TS.
189935b1 1864 */
9f8856e9 1865 sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1866 "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1867 rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1868 addbuf, remstr, addstr,
1869 mbuf->mb_channel->creationtime);
189935b1 1870 }
1871 }
1872
1873 /* We've drained the ModeBuf... */
1874 mbuf->mb_add = 0;
1875 mbuf->mb_rem = 0;
1876 mbuf->mb_count = 0;
1877
1878 /* reinitialize the mode-with-arg slots */
1879 for (i = 0; i < MAXMODEPARAMS; i++) {
1880 /* If we saved any, pack them down */
1881 if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1882 mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1883 MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1884
1885 if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1886 continue;
1887 } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1888 MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1889
1890 MB_TYPE(mbuf, i) = 0;
1891 MB_UINT(mbuf, i) = 0;
1892 }
1893
1894 /* If we're supposed to flush it all, do so--all hail tail recursion */
1895 if (all && mbuf->mb_count)
1896 return modebuf_flush_int(mbuf, 1);
1897
1898 return 0;
1899}
1900
1901/** Initialise a modebuf
1902 * This routine just initializes a ModeBuf structure with the information
1903 * needed and the options given.
1904 *
1905 * @param mbuf The mode buffer to initialise.
1906 * @param source The client that is performing the mode.
1907 * @param connect ?
1908 * @param chan The channel that the mode is being performed upon.
1909 * @param dest ?
1910 */
1911void
1912modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1913 struct Client *connect, struct Channel *chan, unsigned int dest)
1914{
1915 int i;
1916
1917 assert(0 != mbuf);
1918 assert(0 != source);
1919 assert(0 != chan);
1920 assert(0 != dest);
1921
1922 if (IsLocalChannel(chan->chname)) dest &= ~MODEBUF_DEST_SERVER;
1923
1924 mbuf->mb_add = 0;
1925 mbuf->mb_rem = 0;
1926 mbuf->mb_source = source;
1927 mbuf->mb_connect = connect;
1928 mbuf->mb_channel = chan;
1929 mbuf->mb_dest = dest;
1930 mbuf->mb_count = 0;
1931
1932 /* clear each mode-with-parameter slot */
1933 for (i = 0; i < MAXMODEPARAMS; i++) {
1934 MB_TYPE(mbuf, i) = 0;
1935 MB_UINT(mbuf, i) = 0;
1936 }
1937}
1938
1939/** Append a new mode to a modebuf
1940 * This routine simply adds modes to be added or deleted; do a binary OR
1941 * with either MODE_ADD or MODE_DEL
1942 *
1943 * @param mbuf Mode buffer
1944 * @param mode MODE_ADD or MODE_DEL OR'd with MODE_PRIVATE etc.
1945 */
1946void
1947modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1948{
1949 assert(0 != mbuf);
1950 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1951
1952 mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1953 MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS | MODE_REGONLY |
1954 MODE_DELJOINS | MODE_WASDELJOINS);
1955
1956 if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1957 return;
1958
1959 if (mode & MODE_ADD) {
1960 mbuf->mb_rem &= ~mode;
1961 mbuf->mb_add |= mode;
1962 } else {
1963 mbuf->mb_add &= ~mode;
1964 mbuf->mb_rem |= mode;
1965 }
1966}
1967
1968/** Append a mode that takes an int argument to the modebuf
1969 *
1970 * This routine adds a mode to be added or deleted that takes a unsigned
1971 * int parameter; mode may *only* be the relevant mode flag ORed with one
1972 * of MODE_ADD or MODE_DEL
1973 *
1974 * @param mbuf The mode buffer to append to.
1975 * @param mode The mode to append.
1976 * @param uint The argument to the mode.
1977 */
1978void
1979modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1980{
1981 assert(0 != mbuf);
1982 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1983
052b069e 1984 if (mode == (MODE_LIMIT | ((mbuf->mb_dest & MODEBUF_DEST_BOUNCE) ? MODE_ADD : MODE_DEL))) {
189935b1 1985 mbuf->mb_rem |= mode;
1986 return;
1987 }
1988 MB_TYPE(mbuf, mbuf->mb_count) = mode;
1989 MB_UINT(mbuf, mbuf->mb_count) = uint;
1990
1991 /* when we've reached the maximal count, flush the buffer */
1992 if (++mbuf->mb_count >=
1993 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1994 modebuf_flush_int(mbuf, 0);
1995}
1996
1997/** append a string mode
1998 * This routine adds a mode to be added or deleted that takes a string
1999 * parameter; mode may *only* be the relevant mode flag ORed with one of
2000 * MODE_ADD or MODE_DEL
2001 *
2002 * @param mbuf The mode buffer to append to.
2003 * @param mode The mode to append.
2004 * @param string The string parameter to append.
2005 * @param free If the string should be free'd later.
2006 */
2007void
2008modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
2009 int free)
2010{
2011 assert(0 != mbuf);
2012 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2013
2014 MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
2015 MB_STRING(mbuf, mbuf->mb_count) = string;
2016
2017 /* when we've reached the maximal count, flush the buffer */
2018 if (++mbuf->mb_count >=
2019 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2020 modebuf_flush_int(mbuf, 0);
2021}
2022
2023/** Append a mode on a client to a modebuf.
2024 * This routine adds a mode to be added or deleted that takes a client
2025 * parameter; mode may *only* be the relevant mode flag ORed with one of
2026 * MODE_ADD or MODE_DEL
2027 *
2028 * @param mbuf The modebuf to append the mode to.
2029 * @param mode The mode to append.
2030 * @param client The client argument to append.
2031 * @param oplevel The oplevel the user had or will have
2032 */
2033void
2034modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
2035 struct Client *client, int oplevel)
2036{
2037 assert(0 != mbuf);
2038 assert(0 != (mode & (MODE_ADD | MODE_DEL)));
2039
2040 MB_TYPE(mbuf, mbuf->mb_count) = mode;
2041 MB_CLIENT(mbuf, mbuf->mb_count) = client;
2042 MB_OPLEVEL(mbuf, mbuf->mb_count) = oplevel;
2043
2044 /* when we've reached the maximal count, flush the buffer */
2045 if (++mbuf->mb_count >=
2046 (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
2047 modebuf_flush_int(mbuf, 0);
2048}
2049
2050/** The exported binding for modebuf_flush()
2051 *
2052 * @param mbuf The mode buffer to flush.
2053 *
2054 * @see modebuf_flush_int()
2055 */
2056int
2057modebuf_flush(struct ModeBuf *mbuf)
2058{
2059 struct Membership *memb;
2060
2061 /* Check if MODE_WASDELJOINS should be set */
2062 if (!(mbuf->mb_channel->mode.mode & (MODE_DELJOINS | MODE_WASDELJOINS))
2063 && (mbuf->mb_rem & MODE_DELJOINS)) {
2064 for (memb = mbuf->mb_channel->members; memb; memb = memb->next_member) {
2065 if (IsDelayedJoin(memb)) {
2066 mbuf->mb_channel->mode.mode |= MODE_WASDELJOINS;
2067 mbuf->mb_add |= MODE_WASDELJOINS;
2068 mbuf->mb_rem &= ~MODE_WASDELJOINS;
2069 break;
2070 }
2071 }
2072 }
2073
2074 return modebuf_flush_int(mbuf, 1);
2075}
2076
2077/* This extracts the simple modes contained in mbuf
2078 *
2079 * @param mbuf The mode buffer to extract the modes from.
2080 * @param buf The string buffer to write the modes into.
2081 */
2082void
2083modebuf_extract(struct ModeBuf *mbuf, char *buf)
2084{
2085 static int flags[] = {
2086/* MODE_CHANOP, 'o', */
2087/* MODE_VOICE, 'v', */
2088 MODE_PRIVATE, 'p',
2089 MODE_SECRET, 's',
2090 MODE_MODERATED, 'm',
2091 MODE_TOPICLIMIT, 't',
2092 MODE_INVITEONLY, 'i',
2093 MODE_NOPRIVMSGS, 'n',
2094 MODE_KEY, 'k',
2095 MODE_APASS, 'A',
2096 MODE_UPASS, 'U',
2097/* MODE_BAN, 'b', */
2098 MODE_LIMIT, 'l',
2099 MODE_REGONLY, 'r',
2100 MODE_DELJOINS, 'D',
2101 0x0, 0x0
2102 };
2103 unsigned int add;
2104 int i, bufpos = 0, len;
2105 int *flag_p;
2106 char *key = 0, limitbuf[20];
2107 char *apass = 0, *upass = 0;
2108
2109 assert(0 != mbuf);
2110 assert(0 != buf);
2111
2112 buf[0] = '\0';
2113
2114 add = mbuf->mb_add;
2115
2116 for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
2117 if (MB_TYPE(mbuf, i) & MODE_ADD) {
2118 add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT | MODE_APASS | MODE_UPASS);
2119
2120 if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
2121 key = MB_STRING(mbuf, i);
2122 else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
2123 ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%u", MB_UINT(mbuf, i));
2124 else if (MB_TYPE(mbuf, i) & MODE_UPASS)
2125 upass = MB_STRING(mbuf, i);
2126 else if (MB_TYPE(mbuf, i) & MODE_APASS)
2127 apass = MB_STRING(mbuf, i);
2128 }
2129 }
2130
2131 if (!add)
2132 return;
2133
2134 buf[bufpos++] = '+'; /* start building buffer */
2135
2136 for (flag_p = flags; flag_p[0]; flag_p += 2)
2137 if (*flag_p & add)
2138 buf[bufpos++] = flag_p[1];
2139
2140 for (i = 0, len = bufpos; i < len; i++) {
2141 if (buf[i] == 'k')
2142 build_string(buf, &bufpos, key, 0, ' ');
2143 else if (buf[i] == 'l')
2144 build_string(buf, &bufpos, limitbuf, 0, ' ');
2145 else if (buf[i] == 'U')
2146 build_string(buf, &bufpos, upass, 0, ' ');
2147 else if (buf[i] == 'A')
2148 build_string(buf, &bufpos, apass, 0, ' ');
2149 }
2150
2151 buf[bufpos] = '\0';
2152
2153 return;
2154}
2155
2156/** Simple function to invalidate bans
2157 *
2158 * This function sets all bans as being valid.
2159 *
2160 * @param chan The channel to operate on.
2161 */
2162void
2163mode_ban_invalidate(struct Channel *chan)
2164{
2165 struct Membership *member;
2166
2167 for (member = chan->members; member; member = member->next_member)
2168 ClearBanValid(member);
2169}
2170
2171/** Simple function to drop invite structures
2172 *
2173 * Remove all the invites on the channel.
2174 *
2175 * @param chan Channel to remove invites from.
2176 *
2177 */
2178void
2179mode_invite_clear(struct Channel *chan)
2180{
2181 while (chan->invites)
2182 del_invite(chan->invites->value.cptr, chan);
2183}
2184
2185/* What we've done for mode_parse so far... */
2186#define DONE_LIMIT 0x01 /**< We've set the limit */
2187#define DONE_KEY 0x02 /**< We've set the key */
2188#define DONE_BANLIST 0x04 /**< We've sent the ban list */
2189#define DONE_NOTOPER 0x08 /**< We've sent a "Not oper" error */
2190#define DONE_BANCLEAN 0x10 /**< We've cleaned bans... */
2191#define DONE_UPASS 0x20 /**< We've set user pass */
2192#define DONE_APASS 0x40 /**< We've set admin pass */
2193
2194struct ParseState {
2195 struct ModeBuf *mbuf;
2196 struct Client *cptr;
2197 struct Client *sptr;
2198 struct Channel *chptr;
2199 struct Membership *member;
2200 int parc;
2201 char **parv;
2202 unsigned int flags;
2203 unsigned int dir;
2204 unsigned int done;
2205 unsigned int add;
2206 unsigned int del;
2207 int args_used;
2208 int max_args;
2209 int numbans;
2210 struct Ban banlist[MAXPARA];
2211 struct {
2212 unsigned int flag;
2213 unsigned short oplevel;
2214 struct Client *client;
2215 } cli_change[MAXPARA];
2216};
2217
2218/** Helper function to send "Not oper" or "Not member" messages
2219 * Here's a helper function to deal with sending along "Not oper" or
2220 * "Not member" messages
2221 *
2222 * @param state Parsing State object
2223 */
2224static void
2225send_notoper(struct ParseState *state)
2226{
2227 if (state->done & DONE_NOTOPER)
2228 return;
2229
2230 send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
2231 ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
2232
2233 state->done |= DONE_NOTOPER;
2234}
2235
2236/** Parse a limit
2237 * Helper function to convert limits
2238 *
2239 * @param state Parsing state object.
2240 * @param flag_p ?
2241 */
2242static void
2243mode_parse_limit(struct ParseState *state, int *flag_p)
2244{
2245 unsigned int t_limit;
2246
2247 if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
2248 if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2249 return;
2250
2251 if (state->parc <= 0) { /* warn if not enough args */
2252 if (MyUser(state->sptr))
2253 need_more_params(state->sptr, "MODE +l");
2254 return;
2255 }
2256
2257 t_limit = strtoul(state->parv[state->args_used++], 0, 10); /* grab arg */
2258 state->parc--;
2259 state->max_args--;
2260
2261 if ((int)t_limit<0) /* don't permit a negative limit */
2262 return;
2263
2264 if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2265 (!t_limit || t_limit == state->chptr->mode.limit))
2266 return;
2267 } else
2268 t_limit = state->chptr->mode.limit;
2269
2270 /* If they're not an oper, they can't change modes */
2271 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2272 send_notoper(state);
2273 return;
2274 }
2275
2276 /* Can't remove a limit that's not there */
2277 if (state->dir == MODE_DEL && !state->chptr->mode.limit)
2278 return;
2279
2280 /* Skip if this is a burst and a lower limit than this is set already */
2281 if ((state->flags & MODE_PARSE_BURST) &&
2282 (state->chptr->mode.mode & flag_p[0]) &&
2283 (state->chptr->mode.limit < t_limit))
2284 return;
2285
2286 if (state->done & DONE_LIMIT) /* allow limit to be set only once */
2287 return;
2288 state->done |= DONE_LIMIT;
2289
2290 if (!state->mbuf)
2291 return;
2292
2293 modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
2294
2295 if (state->flags & MODE_PARSE_SET) { /* set the limit */
2296 if (state->dir & MODE_ADD) {
2297 state->chptr->mode.mode |= flag_p[0];
2298 state->chptr->mode.limit = t_limit;
2299 } else {
2300 state->chptr->mode.mode &= ~flag_p[0];
2301 state->chptr->mode.limit = 0;
2302 }
2303 }
2304}
2305
2306/** Helper function to clean key-like parameters. */
2307static void
2308clean_key(char *s)
2309{
2310 int t_len = KEYLEN;
2311
2312 while (*s > ' ' && *s != ':' && *s != ',' && t_len--)
2313 s++;
2314 *s = '\0';
2315}
2316
2317/*
2318 * Helper function to convert keys
2319 */
2320static void
2321mode_parse_key(struct ParseState *state, int *flag_p)
2322{
2323 char *t_str;
2324
2325 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2326 return;
2327
2328 if (state->parc <= 0) { /* warn if not enough args */
2329 if (MyUser(state->sptr))
2330 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2331 "MODE -k");
2332 return;
2333 }
2334
2335 t_str = state->parv[state->args_used++]; /* grab arg */
2336 state->parc--;
2337 state->max_args--;
2338
2339 /* If they're not an oper, they can't change modes */
2340 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2341 send_notoper(state);
2342 return;
2343 }
2344
2345 if (state->done & DONE_KEY) /* allow key to be set only once */
2346 return;
2347 state->done |= DONE_KEY;
2348
2349 /* clean up the key string */
2350 clean_key(t_str);
2351 if (!*t_str || *t_str == ':') { /* warn if empty */
2352 if (MyUser(state->sptr))
2353 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2354 "MODE -k");
2355 return;
2356 }
2357
2358 if (!state->mbuf)
2359 return;
2360
2361 /* Skip if this is a burst, we have a key already and the new key is
2362 * after the old one alphabetically */
2363 if ((state->flags & MODE_PARSE_BURST) &&
2364 *(state->chptr->mode.key) &&
2365 ircd_strcmp(state->chptr->mode.key, t_str) <= 0)
2366 return;
2367
2368 /* can't add a key if one is set, nor can one remove the wrong key */
2369 if (!(state->flags & MODE_PARSE_FORCE))
2370 if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2371 (state->dir == MODE_DEL &&
2372 ircd_strcmp(state->chptr->mode.key, t_str))) {
2373 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2374 return;
2375 }
2376
2377 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2378 !ircd_strcmp(state->chptr->mode.key, t_str))
2379 return; /* no key change */
2380
2381 if (state->flags & MODE_PARSE_BOUNCE) {
2382 if (*state->chptr->mode.key) /* reset old key */
2383 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2384 state->chptr->mode.key, 0);
2385 else /* remove new bogus key */
2386 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2387 } else /* send new key */
2388 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2389
2390 if (state->flags & MODE_PARSE_SET) {
2391 if (state->dir == MODE_DEL) /* remove the old key */
2392 *state->chptr->mode.key = '\0';
9f8856e9 2393 else
189935b1 2394 ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2395 }
2396}
2397
2398/*
2399 * Helper function to convert user passes
2400 */
2401static void
2402mode_parse_upass(struct ParseState *state, int *flag_p)
2403{
2404 char *t_str;
2405
2406 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2407 return;
2408
2409 if (state->parc <= 0) { /* warn if not enough args */
2410 if (MyUser(state->sptr))
2411 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +U" :
2412 "MODE -U");
2413 return;
2414 }
2415
2416 t_str = state->parv[state->args_used++]; /* grab arg */
2417 state->parc--;
2418 state->max_args--;
2419
2420 /* If they're not an oper, they can't change modes */
2421 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2422 send_notoper(state);
2423 return;
2424 }
2425
2426 /* If a non-service user is trying to force it, refuse. */
2427 if (state->flags & MODE_PARSE_FORCE && MyUser(state->sptr)
2428 && !HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
2429 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2430 state->chptr->chname);
2431 return;
2432 }
2433
2434 /* If they are not the channel manager, they are not allowed to change it */
2435 if (MyUser(state->sptr) && !(state->flags & MODE_PARSE_FORCE || IsChannelManager(state->member))) {
2436 if (*state->chptr->mode.apass) {
2437 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2438 state->chptr->chname);
2439 } else {
052b069e 2440 send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname,
2441 (TStime() - state->chptr->creationtime < 172800) ?
2442 "approximately 4-5 minutes" : "approximately 48 hours");
189935b1 2443 }
2444 return;
2445 }
2446
2447 if (state->done & DONE_UPASS) /* allow upass to be set only once */
2448 return;
2449 state->done |= DONE_UPASS;
2450
2451 /* clean up the upass string */
2452 clean_key(t_str);
2453 if (!*t_str || *t_str == ':') { /* warn if empty */
2454 if (MyUser(state->sptr))
2455 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +U" :
2456 "MODE -U");
2457 return;
2458 }
2459
2460 if (!state->mbuf)
2461 return;
2462
2463 if (!(state->flags & MODE_PARSE_FORCE)) {
2464 /* can't add the upass while apass is not set */
2465 if (state->dir == MODE_ADD && !*state->chptr->mode.apass) {
2466 send_reply(state->sptr, ERR_UPASSNOTSET, state->chptr->chname, state->chptr->chname);
2467 return;
2468 }
2469 /* cannot set a +U password that is the same as +A */
2470 if (state->dir == MODE_ADD && !ircd_strcmp(state->chptr->mode.apass, t_str)) {
2471 send_reply(state->sptr, ERR_UPASS_SAME_APASS, state->chptr->chname);
2472 return;
2473 }
2474 /* can't add a upass if one is set, nor can one remove the wrong upass */
2475 if ((state->dir == MODE_ADD && *state->chptr->mode.upass) ||
2476 (state->dir == MODE_DEL &&
2477 ircd_strcmp(state->chptr->mode.upass, t_str))) {
2478 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2479 return;
2480 }
2481 }
2482
2483 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2484 !ircd_strcmp(state->chptr->mode.upass, t_str))
2485 return; /* no upass change */
2486
9f8856e9 2487 /* Skip if this is a burst, we have a Upass already and the new Upass is
2488 * after the old one alphabetically */
2489 if ((state->flags & MODE_PARSE_BURST) &&
2490 *(state->chptr->mode.upass) &&
2491 ircd_strcmp(state->chptr->mode.upass, t_str) <= 0)
2492 return;
2493
189935b1 2494 if (state->flags & MODE_PARSE_BOUNCE) {
2495 if (*state->chptr->mode.upass) /* reset old upass */
2496 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2497 state->chptr->mode.upass, 0);
2498 else /* remove new bogus upass */
2499 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2500 } else /* send new upass */
2501 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2502
2503 if (state->flags & MODE_PARSE_SET) {
2504 if (state->dir == MODE_DEL) /* remove the old upass */
2505 *state->chptr->mode.upass = '\0';
9f8856e9 2506 else
189935b1 2507 ircd_strncpy(state->chptr->mode.upass, t_str, KEYLEN);
2508 }
2509}
2510
2511/*
2512 * Helper function to convert admin passes
2513 */
2514static void
2515mode_parse_apass(struct ParseState *state, int *flag_p)
2516{
2517 struct Membership *memb;
2518 char *t_str;
2519
2520 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2521 return;
2522
2523 if (state->parc <= 0) { /* warn if not enough args */
2524 if (MyUser(state->sptr))
2525 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2526 "MODE -A");
2527 return;
2528 }
2529
2530 t_str = state->parv[state->args_used++]; /* grab arg */
2531 state->parc--;
2532 state->max_args--;
2533
2534 /* If they're not an oper, they can't change modes */
2535 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2536 send_notoper(state);
2537 return;
2538 }
2539
052b069e 2540 if (MyUser(state->sptr)) {
2541 if (state->flags & MODE_PARSE_FORCE) {
2542 /* If an unprivileged oper is trying to force it, refuse. */
2543 if (!HasPriv(state->sptr, PRIV_APASS_OPMODE)) {
2544 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2545 state->chptr->chname);
2546 return;
2547 }
189935b1 2548 } else {
052b069e 2549 /* If they are not the channel manager, they are not allowed to change it. */
2550 if (!IsChannelManager(state->member)) {
2551 if (*state->chptr->mode.apass) {
2552 send_reply(state->sptr, ERR_NOTMANAGER, state->chptr->chname,
2553 state->chptr->chname);
2554 } else {
2555 send_reply(state->sptr, ERR_NOMANAGER, state->chptr->chname,
2556 (TStime() - state->chptr->creationtime < 172800) ?
2557 "approximately 4-5 minutes" : "approximately 48 hours");
2558 }
2559 return;
2560 }
2561 /* Can't remove the Apass while Upass is still set. */
2562 if (state->dir == MODE_DEL && *state->chptr->mode.upass) {
2563 send_reply(state->sptr, ERR_UPASSSET, state->chptr->chname, state->chptr->chname);
2564 return;
2565 }
2566 /* Can't add an Apass if one is set, nor can one remove the wrong Apass. */
2567 if ((state->dir == MODE_ADD && *state->chptr->mode.apass) ||
2568 (state->dir == MODE_DEL && ircd_strcmp(state->chptr->mode.apass, t_str))) {
2569 send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2570 return;
2571 }
2572 }
2573
2574 /* Forbid removing the Apass if the channel is older than 48 hours
2575 * unless an oper is doing it. */
2576 if (TStime() - state->chptr->creationtime >= 172800
2577 && state->dir == MODE_DEL
2578 && !IsAnOper(state->sptr)) {
2579 send_reply(state->sptr, ERR_CHANSECURED, state->chptr->chname);
2580 return;
189935b1 2581 }
189935b1 2582 }
2583
2584 if (state->done & DONE_APASS) /* allow apass to be set only once */
2585 return;
2586 state->done |= DONE_APASS;
2587
2588 /* clean up the apass string */
2589 clean_key(t_str);
2590 if (!*t_str || *t_str == ':') { /* warn if empty */
2591 if (MyUser(state->sptr))
2592 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +A" :
2593 "MODE -A");
2594 return;
2595 }
2596
2597 if (!state->mbuf)
2598 return;
2599
189935b1 2600 if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2601 !ircd_strcmp(state->chptr->mode.apass, t_str))
2602 return; /* no apass change */
2603
9f8856e9 2604 /* Skip if this is a burst, we have an Apass already and the new Apass is
2605 * after the old one alphabetically */
2606 if ((state->flags & MODE_PARSE_BURST) &&
2607 *(state->chptr->mode.apass) &&
2608 ircd_strcmp(state->chptr->mode.apass, t_str) <= 0)
2609 return;
2610
189935b1 2611 if (state->flags & MODE_PARSE_BOUNCE) {
2612 if (*state->chptr->mode.apass) /* reset old apass */
2613 modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2614 state->chptr->mode.apass, 0);
2615 else /* remove new bogus apass */
2616 modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2617 } else /* send new apass */
2618 modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2619
2620 if (state->flags & MODE_PARSE_SET) {
2621 if (state->dir == MODE_ADD) { /* set the new apass */
9f8856e9 2622 /* Only accept the new apass if there is no current apass or
2623 * this is a BURST. */
2624 if (state->chptr->mode.apass[0] == '\0' ||
2625 (state->flags & MODE_PARSE_BURST))
189935b1 2626 ircd_strncpy(state->chptr->mode.apass, t_str, KEYLEN);
2627 /* Make it VERY clear to the user that this is a one-time password */
2628 if (MyUser(state->sptr)) {
2629 send_reply(state->sptr, RPL_APASSWARN_SET, state->chptr->mode.apass);
2630 send_reply(state->sptr, RPL_APASSWARN_SECRET, state->chptr->chname,
2631 state->chptr->mode.apass);
2632 }
052b069e 2633 /* Give the channel manager level 0 ops.
2634 There should not be tested for IsChannelManager here because
2635 on the local server it is impossible to set the apass if one
2636 isn't a channel manager and remote servers might need to sync
2637 the oplevel here: when someone creates a channel (and becomes
2638 channel manager) during a net.break, and only sets the Apass
2639 after the net rejoined, they will have oplevel MAXOPLEVEL on
2640 all remote servers. */
2641 if (state->member)
189935b1 2642 SetOpLevel(state->member, 0);
2643 } else { /* remove the old apass */
2644 *state->chptr->mode.apass = '\0';
052b069e 2645 /* Clear Upass so that there is never a Upass set when a zannel is burst. */
2646 *state->chptr->mode.upass = '\0';
189935b1 2647 if (MyUser(state->sptr))
2648 send_reply(state->sptr, RPL_APASSWARN_CLEAR);
2649 /* Revert everyone to MAXOPLEVEL. */
2650 for (memb = state->chptr->members; memb; memb = memb->next_member) {
2651 if (memb->status & MODE_CHANOP)
2652 SetOpLevel(memb, MAXOPLEVEL);
2653 }
2654 }
2655 }
2656}
2657
2658/** Compare one ban's extent to another.
2659 * This works very similarly to mmatch() but it knows about CIDR masks
2660 * and ban exceptions. If both bans are CIDR-based, compare their
2661 * address bits; otherwise, use mmatch().
2662 * @param[in] old_ban One ban.
2663 * @param[in] new_ban Another ban.
2664 * @return Zero if \a old_ban is a superset of \a new_ban, non-zero otherwise.
2665 */
2666static int
2667bmatch(struct Ban *old_ban, struct Ban *new_ban)
2668{
2669 int res;
2670 assert(old_ban != NULL);
2671 assert(new_ban != NULL);
2672 /* A ban is never treated as a superset of an exception. */
2673 if (!(old_ban->flags & BAN_EXCEPTION)
2674 && (new_ban->flags & BAN_EXCEPTION))
2675 return 1;
2676 /* If either is not an address mask, match the text masks. */
2677 if ((old_ban->flags & new_ban->flags & BAN_IPMASK) == 0)
2678 return mmatch(old_ban->banstr, new_ban->banstr);
2679 /* If the old ban has a longer prefix than new, it cannot be a superset. */
2680 if (old_ban->addrbits > new_ban->addrbits)
2681 return 1;
2682 /* Compare the masks before the hostname part. */
2683 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '\0';
2684 res = mmatch(old_ban->banstr, new_ban->banstr);
2685 old_ban->banstr[old_ban->nu_len] = new_ban->banstr[new_ban->nu_len] = '@';
2686 if (res)
2687 return res;
9f8856e9 2688 /* If the old ban's mask mismatches, cannot be a superset. */
2689 if (!ipmask_check(&new_ban->address, &old_ban->address, old_ban->addrbits))
2690 return 1;
2691 /* Otherwise it depends on whether the old ban's text is a superset
2692 * of the new. */
2693 return mmatch(old_ban->banstr, new_ban->banstr);
189935b1 2694}
2695
2696/** Add a ban from a ban list and mark bans that should be removed
2697 * because they overlap.
2698 *
2699 * There are three invariants for a ban list. First, no ban may be
2700 * more specific than another ban. Second, no exception may be more
2701 * specific than another exception. Finally, no ban may be more
2702 * specific than any exception.
2703 *
2704 * @param[in,out] banlist Pointer to head of list.
2705 * @param[in] newban Ban (or exception) to add (or remove).
2706 * @param[in] do_free If non-zero, free \a newban on failure.
2707 * @return Zero if \a newban could be applied, non-zero if not.
2708 */
2709int apply_ban(struct Ban **banlist, struct Ban *newban, int do_free)
2710{
2711 struct Ban *ban;
2712 size_t count = 0;
2713
2714 assert(newban->flags & (BAN_ADD|BAN_DEL));
2715 if (newban->flags & BAN_ADD) {
2716 size_t totlen = 0;
2717 /* If a less specific entry is found, fail. */
2718 for (ban = *banlist; ban; ban = ban->next) {
2719 if (!bmatch(ban, newban)) {
2720 if (do_free)
2721 free_ban(newban);
2722 return 1;
2723 }
2724 if (!(ban->flags & (BAN_OVERLAPPED|BAN_DEL))) {
2725 count++;
2726 totlen += strlen(ban->banstr);
2727 }
2728 }
2729 /* Mark more specific entries and add this one to the end of the list. */
2730 while ((ban = *banlist) != NULL) {
2731 if (!bmatch(newban, ban)) {
2732 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
2733 }
2734 banlist = &ban->next;
2735 }
2736 *banlist = newban;
2737 return 0;
2738 } else if (newban->flags & BAN_DEL) {
2739 size_t remove_count = 0;
2740 /* Mark more specific entries. */
2741 for (ban = *banlist; ban; ban = ban->next) {
2742 if (!bmatch(newban, ban)) {
2743 ban->flags |= BAN_OVERLAPPED | BAN_DEL;
2744 remove_count++;
2745 }
2746 }
2747 if (remove_count)
2748 return 0;
2749 /* If no matches were found, fail. */
2750 if (do_free)
2751 free_ban(newban);
2752 return 3;
2753 }
2754 if (do_free)
2755 free_ban(newban);
2756 return 4;
2757}
2758
2759/*
2760 * Helper function to convert bans
2761 */
2762static void
2763mode_parse_ban(struct ParseState *state, int *flag_p)
2764{
2765 char *t_str, *s;
2766 struct Ban *ban, *newban;
2767
2768 if (state->parc <= 0) { /* Not enough args, send ban list */
2769 if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2770 send_ban_list(state->sptr, state->chptr);
2771 state->done |= DONE_BANLIST;
2772 }
2773
2774 return;
2775 }
2776
2777 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2778 return;
2779
2780 t_str = state->parv[state->args_used++]; /* grab arg */
2781 state->parc--;
2782 state->max_args--;
2783
2784 /* If they're not an oper, they can't change modes */
2785 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2786 send_notoper(state);
2787 return;
2788 }
2789
2790 if ((s = strchr(t_str, ' ')))
2791 *s = '\0';
2792
2793 if (!*t_str || *t_str == ':') { /* warn if empty */
2794 if (MyUser(state->sptr))
2795 need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2796 "MODE -b");
2797 return;
2798 }
2799
2800 /* Clear all ADD/DEL/OVERLAPPED flags from ban list. */
2801 if (!(state->done & DONE_BANCLEAN)) {
2802 for (ban = state->chptr->banlist; ban; ban = ban->next)
2803 ban->flags &= ~(BAN_ADD | BAN_DEL | BAN_OVERLAPPED);
2804 state->done |= DONE_BANCLEAN;
2805 }
2806
2807 /* remember the ban for the moment... */
2808 newban = state->banlist + (state->numbans++);
2809 newban->next = 0;
2810 newban->flags = ((state->dir == MODE_ADD) ? BAN_ADD : BAN_DEL)
2811 | (*flag_p == MODE_BAN ? 0 : BAN_EXCEPTION);
2812 set_ban_mask(newban, collapse(pretty_mask(t_str)));
2813 ircd_strncpy(newban->who, IsUser(state->sptr) ? cli_name(state->sptr) : "*", NICKLEN);
2814 newban->when = TStime();
2815 apply_ban(&state->chptr->banlist, newban, 0);
2816}
2817
2818/*
2819 * This is the bottom half of the ban processor
2820 */
2821static void
2822mode_process_bans(struct ParseState *state)
2823{
2824 struct Ban *ban, *newban, *prevban, *nextban;
2825 int count = 0;
2826 int len = 0;
2827 int banlen;
2828 int changed = 0;
2829
2830 for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2831 count++;
2832 banlen = strlen(ban->banstr);
2833 len += banlen;
2834 nextban = ban->next;
2835
2836 if ((ban->flags & (BAN_DEL | BAN_ADD)) == (BAN_DEL | BAN_ADD)) {
2837 if (prevban)
2838 prevban->next = 0; /* Break the list; ban isn't a real ban */
2839 else
2840 state->chptr->banlist = 0;
2841
2842 count--;
2843 len -= banlen;
2844
2845 continue;
2846 } else if (ban->flags & BAN_DEL) { /* Deleted a ban? */
2847 char *bandup;
2848 DupString(bandup, ban->banstr);
2849 modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2850 bandup, 1);
2851
2852 if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2853 if (prevban) /* clip it out of the list... */
2854 prevban->next = ban->next;
2855 else
2856 state->chptr->banlist = ban->next;
2857
2858 count--;
2859 len -= banlen;
2860 free_ban(ban);
2861
2862 changed++;
2863 continue; /* next ban; keep prevban like it is */
2864 } else
2865 ban->flags &= BAN_IPMASK; /* unset other flags */
2866 } else if (ban->flags & BAN_ADD) { /* adding a ban? */
2867 if (prevban)
2868 prevban->next = 0; /* Break the list; ban isn't a real ban */
2869 else
2870 state->chptr->banlist = 0;
2871
2872 /* If we're supposed to ignore it, do so. */
2873 if (ban->flags & BAN_OVERLAPPED &&
2874 !(state->flags & MODE_PARSE_BOUNCE)) {
2875 count--;
2876 len -= banlen;
2877 } else {
2878 if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2879 (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
2880 count > feature_int(FEAT_MAXBANS))) {
2881 send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2882 ban->banstr);
2883 count--;
2884 len -= banlen;
2885 } else {
2886 char *bandup;
2887 /* add the ban to the buffer */
2888 DupString(bandup, ban->banstr);
2889 modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2890 bandup, 1);
2891
2892 if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2893 newban = make_ban(ban->banstr);
2894 strcpy(newban->who, ban->who);
2895 newban->when = ban->when;
2896 newban->flags = ban->flags & BAN_IPMASK;
2897
2898 newban->next = state->chptr->banlist; /* and link it in */
2899 state->chptr->banlist = newban;
2900
2901 changed++;
2902 }
2903 }
2904 }
2905 }
2906
2907 prevban = ban;
2908 } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2909
2910 if (changed) /* if we changed the ban list, we must invalidate the bans */
2911 mode_ban_invalidate(state->chptr);
2912}
2913
2914/*
2915 * Helper function to process client changes
2916 */
2917static void
2918mode_parse_client(struct ParseState *state, int *flag_p)
2919{
2920 char *t_str;
052b069e 2921 char *colon;
189935b1 2922 struct Client *acptr;
2923 struct Membership *member;
2924 int oplevel = MAXOPLEVEL + 1;
052b069e 2925 int req_oplevel;
189935b1 2926 int i;
2927
2928 if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2929 return;
2930
2931 if (state->parc <= 0) /* return if not enough args */
2932 return;
2933
2934 t_str = state->parv[state->args_used++]; /* grab arg */
2935 state->parc--;
2936 state->max_args--;
2937
2938 /* If they're not an oper, they can't change modes */
2939 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2940 send_notoper(state);
2941 return;
2942 }
2943
052b069e 2944 if (MyUser(state->sptr)) {
2945 colon = strchr(t_str, ':');
2946 if (colon != NULL) {
2947 *colon++ = '\0';
2948 req_oplevel = atoi(colon);
2949 if (!(state->flags & MODE_PARSE_FORCE)
2950 && state->member
2951 && (req_oplevel < OpLevel(state->member)
2952 || (req_oplevel == OpLevel(state->member)
2953 && OpLevel(state->member) < MAXOPLEVEL)
2954 || req_oplevel > MAXOPLEVEL))
2955 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
2956 t_str, state->chptr->chname,
2957 OpLevel(state->member), req_oplevel, "op",
2958 OpLevel(state->member) == req_oplevel ? "the same" : "a higher");
2959 else if (req_oplevel <= MAXOPLEVEL)
2960 oplevel = req_oplevel;
2961 }
2962 /* find client we're manipulating */
189935b1 2963 acptr = find_chasing(state->sptr, t_str, NULL);
052b069e 2964 } else {
189935b1 2965 if (t_str[5] == ':') {
2966 t_str[5] = '\0';
2967 oplevel = atoi(t_str + 6);
2968 }
2969 acptr = findNUser(t_str);
2970 }
2971
2972 if (!acptr)
2973 return; /* find_chasing() already reported an error to the user */
2974
2975 for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2976 if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2977 state->cli_change[i].flag & flag_p[0]))
2978 break; /* found a slot */
2979
2980 /* If we are going to bounce this deop, mark the correct oplevel. */
2981 if (state->flags & MODE_PARSE_BOUNCE
2982 && state->dir == MODE_DEL
2983 && flag_p[0] == MODE_CHANOP
2984 && (member = find_member_link(state->chptr, acptr)))
2985 oplevel = OpLevel(member);
2986
2987 /* Store what we're doing to them */
2988 state->cli_change[i].flag = state->dir | flag_p[0];
2989 state->cli_change[i].oplevel = oplevel;
2990 state->cli_change[i].client = acptr;
2991}
2992
2993/*
2994 * Helper function to process the changed client list
2995 */
2996static void
2997mode_process_clients(struct ParseState *state)
2998{
2999 int i;
3000 struct Membership *member;
3001
3002 for (i = 0; state->cli_change[i].flag; i++) {
3003 assert(0 != state->cli_change[i].client);
3004
3005 /* look up member link */
3006 if (!(member = find_member_link(state->chptr,
3007 state->cli_change[i].client)) ||
3008 (MyUser(state->sptr) && IsZombie(member))) {
3009 if (MyUser(state->sptr))
3010 send_reply(state->sptr, ERR_USERNOTINCHANNEL,
3011 cli_name(state->cli_change[i].client),
3012 state->chptr->chname);
3013 continue;
3014 }
3015
3016 if ((state->cli_change[i].flag & MODE_ADD &&
3017 (state->cli_change[i].flag & member->status)) ||
3018 (state->cli_change[i].flag & MODE_DEL &&
3019 !(state->cli_change[i].flag & member->status)))
3020 continue; /* no change made, don't do anything */
3021
3022 /* see if the deop is allowed */
3023 if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
3024 (MODE_DEL | MODE_CHANOP)) {
3025 /* prevent +k users from being deopped */
3026 if (IsChannelService(state->cli_change[i].client)) {
3027 if (state->flags & MODE_PARSE_FORCE) /* it was forced */
3028 sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
3029 state->chptr,
3030 (IsServer(state->sptr) ? cli_name(state->sptr) :
3031 cli_name((cli_user(state->sptr))->server)));
3032
3033 else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
3034 send_reply(state->sptr, ERR_ISCHANSERVICE,
3035 cli_name(state->cli_change[i].client),
3036 state->chptr->chname);
3037 continue;
3038 }
3039 }
3040
3041 /* check deop for local user */
3042 if (MyUser(state->sptr)) {
3043
3044 /* don't allow local opers to be deopped on local channels */
3045 if (state->cli_change[i].client != state->sptr &&
3046 IsLocalChannel(state->chptr->chname) &&
3047 HasPriv(state->cli_change[i].client, PRIV_DEOP_LCHAN)) {
3048 send_reply(state->sptr, ERR_ISOPERLCHAN,
3049 cli_name(state->cli_change[i].client),
3050 state->chptr->chname);
3051 continue;
3052 }
3053
052b069e 3054 /* Forbid deopping other members with an oplevel less than
3055 * one's own level, and other members with an oplevel the same
3056 * as one's own unless both are at MAXOPLEVEL. */
3057 if (state->sptr != state->cli_change[i].client
189935b1 3058 && state->member
052b069e 3059 && ((OpLevel(member) < OpLevel(state->member))
3060 || (OpLevel(member) == OpLevel(state->member)
3061 && OpLevel(member) < MAXOPLEVEL))) {
189935b1 3062 int equal = (OpLevel(member) == OpLevel(state->member));
3063 send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
3064 cli_name(state->cli_change[i].client),
3065 state->chptr->chname,
3066 OpLevel(state->member), OpLevel(member),
3067 "deop", equal ? "the same" : "a higher");
3068 continue;
3069 }
3070 }
3071 }
3072
3073 /* set op-level of member being opped */
3074 if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
3075 (MODE_ADD | MODE_CHANOP)) {
3076 /* If a valid oplevel was specified, use it.
3077 * Otherwise, if being opped by an outsider, get MAXOPLEVEL.
3078 * Otherwise, if not an apass channel, or state->member has
3079 * MAXOPLEVEL, get oplevel MAXOPLEVEL.
3080 * Otherwise, get state->member's oplevel+1.
3081 */
3082 if (state->cli_change[i].oplevel <= MAXOPLEVEL)
3083 SetOpLevel(member, state->cli_change[i].oplevel);
3084 else if (!state->member)
3085 SetOpLevel(member, MAXOPLEVEL);
3086 else if (!state->chptr->mode.apass[0] || OpLevel(state->member) == MAXOPLEVEL)
3087 SetOpLevel(member, MAXOPLEVEL);
3088 else
3089 SetOpLevel(member, OpLevel(state->member) + 1);
3090 }
3091
3092 /* actually effect the change */
3093 if (state->flags & MODE_PARSE_SET) {
3094 if (state->cli_change[i].flag & MODE_ADD) {
052b069e 3095 if (IsDelayedJoin(member) && !IsZombie(member))
189935b1 3096 RevealDelayedJoin(member);
3097 member->status |= (state->cli_change[i].flag &
3098 (MODE_CHANOP | MODE_VOICE));
3099 if (state->cli_change[i].flag & MODE_CHANOP)
3100 ClearDeopped(member);
3101 } else
3102 member->status &= ~(state->cli_change[i].flag &
3103 (MODE_CHANOP | MODE_VOICE));
3104 }
3105
3106 /* accumulate the change */
3107 modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
3108 state->cli_change[i].client,
3109 state->cli_change[i].oplevel);
3110 } /* for (i = 0; state->cli_change[i].flags; i++) */
3111}
3112
3113/*
3114 * Helper function to process the simple modes
3115 */
3116static void
3117mode_parse_mode(struct ParseState *state, int *flag_p)
3118{
3119 /* If they're not an oper, they can't change modes */
3120 if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
3121 send_notoper(state);
3122 return;
3123 }
3124
3125 if (!state->mbuf)
3126 return;
3127
3128 if (state->dir == MODE_ADD) {
3129 state->add |= flag_p[0];
3130 state->del &= ~flag_p[0];
3131
3132 if (flag_p[0] & MODE_SECRET) {
3133 state->add &= ~MODE_PRIVATE;
3134 state->del |= MODE_PRIVATE;
3135 } else if (flag_p[0] & MODE_PRIVATE) {
3136 state->add &= ~MODE_SECRET;
3137 state->del |= MODE_SECRET;
3138 }
3139 if (flag_p[0] & MODE_DELJOINS) {
3140 state->add &= ~MODE_WASDELJOINS;
3141 state->del |= MODE_WASDELJOINS;
3142 }
3143 } else {
3144 state->add &= ~flag_p[0];
3145 state->del |= flag_p[0];
3146 }
3147
3148 assert(0 == (state->add & state->del));
3149 assert((MODE_SECRET | MODE_PRIVATE) !=
3150 (state->add & (MODE_SECRET | MODE_PRIVATE)));
3151}
3152
3153/*
3154 * This routine is intended to parse MODE or OPMODE commands and effect the
3155 * changes (or just build the bounce buffer). We pass the starting offset
3156 * as a
3157 */
3158int
3159mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
3160 struct Channel *chptr, int parc, char *parv[], unsigned int flags,
3161 struct Membership* member)
3162{
3163 static int chan_flags[] = {
3164 MODE_CHANOP, 'o',
3165 MODE_VOICE, 'v',
3166 MODE_PRIVATE, 'p',
3167 MODE_SECRET, 's',
3168 MODE_MODERATED, 'm',
3169 MODE_TOPICLIMIT, 't',
3170 MODE_INVITEONLY, 'i',
3171 MODE_NOPRIVMSGS, 'n',
3172 MODE_KEY, 'k',
3173 MODE_APASS, 'A',
3174 MODE_UPASS, 'U',
3175 MODE_BAN, 'b',
3176 MODE_LIMIT, 'l',
3177 MODE_REGONLY, 'r',
3178 MODE_DELJOINS, 'D',
3179 MODE_ADD, '+',
3180 MODE_DEL, '-',
3181 0x0, 0x0
3182 };
3183 int i;
3184 int *flag_p;
3185 unsigned int t_mode;
3186 char *modestr;
3187 struct ParseState state;
3188
3189 assert(0 != cptr);
3190 assert(0 != sptr);
3191 assert(0 != chptr);
3192 assert(0 != parc);
3193 assert(0 != parv);
3194
3195 state.mbuf = mbuf;
3196 state.cptr = cptr;
3197 state.sptr = sptr;
3198 state.chptr = chptr;
3199 state.member = member;
3200 state.parc = parc;
3201 state.parv = parv;
3202 state.flags = flags;
3203 state.dir = MODE_ADD;
3204 state.done = 0;
3205 state.add = 0;
3206 state.del = 0;
3207 state.args_used = 0;
3208 state.max_args = MAXMODEPARAMS;
3209 state.numbans = 0;
3210
3211 for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
3212 state.banlist[i].next = 0;
3213 state.banlist[i].who[0] = '\0';
3214 state.banlist[i].when = 0;
3215 state.banlist[i].flags = 0;
3216 state.cli_change[i].flag = 0;
3217 state.cli_change[i].client = 0;
3218 }
3219
3220 modestr = state.parv[state.args_used++];
3221 state.parc--;
3222
3223 while (*modestr) {
3224 for (; *modestr; modestr++) {
3225 for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
3226 if (flag_p[1] == *modestr)
3227 break;
3228
3229 if (!flag_p[0]) { /* didn't find it? complain and continue */
3230 if (MyUser(state.sptr))
3231 send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
3232 continue;
3233 }
3234
3235 switch (*modestr) {
3236 case '+': /* switch direction to MODE_ADD */
3237 case '-': /* switch direction to MODE_DEL */
3238 state.dir = flag_p[0];
3239 break;
3240
3241 case 'l': /* deal with limits */
3242 mode_parse_limit(&state, flag_p);
3243 break;
3244
3245 case 'k': /* deal with keys */
3246 mode_parse_key(&state, flag_p);
3247 break;
3248
3249 case 'A': /* deal with Admin passes */
3250 if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS))
3251 mode_parse_apass(&state, flag_p);
3252 break;
3253
3254 case 'U': /* deal with user passes */
3255 if (IsServer(cptr) || feature_bool(FEAT_OPLEVELS))
3256 mode_parse_upass(&state, flag_p);
3257 break;
3258
3259 case 'b': /* deal with bans */
3260 mode_parse_ban(&state, flag_p);
3261 break;
3262
3263 case 'o': /* deal with ops/voice */
3264 case 'v':
3265 mode_parse_client(&state, flag_p);
3266 break;
3267
3268 default: /* deal with other modes */
3269 mode_parse_mode(&state, flag_p);
3270 break;
3271 } /* switch (*modestr) */
3272 } /* for (; *modestr; modestr++) */
3273
3274 if (state.flags & MODE_PARSE_BURST)
3275 break; /* don't interpret any more arguments */
3276
3277 if (state.parc > 0) { /* process next argument in string */
3278 modestr = state.parv[state.args_used++];
3279 state.parc--;
3280
3281 /* is it a TS? */
9f8856e9 3282 if (IsServer(state.cptr) && !state.parc && IsDigit(*modestr)) {
189935b1 3283 time_t recv_ts;
3284
3285 if (!(state.flags & MODE_PARSE_SET)) /* don't set earlier TS if */
3286 break; /* we're then going to bounce the mode! */
3287
3288 recv_ts = atoi(modestr);
3289
3290 if (recv_ts && recv_ts < state.chptr->creationtime)
3291 state.chptr->creationtime = recv_ts; /* respect earlier TS */
9f8856e9 3292 else if (recv_ts > state.chptr->creationtime) {
3293 struct Client *sserv;
3294
3295 /* Check whether the originating server has fully processed
3296 * the burst to it. */
3297 sserv = state.cptr;
3298 if (!IsServer(sserv))
3299 sserv = cli_user(sserv)->server;
3300 if (IsBurstOrBurstAck(sserv)) {
3301 /* This is a legal but unusual case; the source server
3302 * probably just has not processed the BURST for this
3303 * channel. It SHOULD wipe out all its modes soon, so
3304 * silently ignore the mode change rather than send a
3305 * bounce that could desync modes from our side (that
3306 * have already been sent).
3307 */
3308 state.mbuf->mb_add = 0;
3309 state.mbuf->mb_rem = 0;
3310 state.mbuf->mb_count = 0;
3311 return state.args_used;
3312 } else {
3313 /* Server is desynced; bounce the mode and deop the source
3314 * to fix it. */
3315 state.mbuf->mb_dest &= ~MODEBUF_DEST_CHANNEL;
3316 state.mbuf->mb_dest |= MODEBUF_DEST_BOUNCE | MODEBUF_DEST_HACK2;
3317 if (!IsServer(state.cptr))
3318 state.mbuf->mb_dest |= MODEBUF_DEST_DEOP;
3319 }
3320 }
189935b1 3321
3322 break; /* break out of while loop */
3323 } else if (state.flags & MODE_PARSE_STRICT ||
3324 (MyUser(state.sptr) && state.max_args <= 0)) {
3325 state.parc++; /* we didn't actually gobble the argument */
3326 state.args_used--;
3327 break; /* break out of while loop */
3328 }
3329 }
3330 } /* while (*modestr) */
3331
3332 /*
3333 * the rest of the function finishes building resultant MODEs; if the
3334 * origin isn't a member or an oper, skip it.
3335 */
3336 if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
3337 return state.args_used; /* tell our parent how many args we gobbled */
3338
3339 t_mode = state.chptr->mode.mode;
3340
3341 if (state.del & t_mode) { /* delete any modes to be deleted... */
3342 modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
3343
3344 t_mode &= ~state.del;
3345 }
3346 if (state.add & ~t_mode) { /* add any modes to be added... */
3347 modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
3348
3349 t_mode |= state.add;
3350 }
3351
3352 if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
3353 if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
3354 !(t_mode & MODE_INVITEONLY))
3355 mode_invite_clear(state.chptr);
3356
3357 state.chptr->mode.mode = t_mode;
3358 }
3359
3360 if (state.flags & MODE_PARSE_WIPEOUT) {
3361 if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
3362 modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
3363 state.chptr->mode.limit);
3364 if (*state.chptr->mode.key && !(state.done & DONE_KEY))
3365 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
3366 state.chptr->mode.key, 0);
3367 if (*state.chptr->mode.upass && !(state.done & DONE_UPASS))
3368 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_UPASS,
3369 state.chptr->mode.upass, 0);
3370 if (*state.chptr->mode.apass && !(state.done & DONE_APASS))
3371 modebuf_mode_string(state.mbuf, MODE_DEL | MODE_APASS,
3372 state.chptr->mode.apass, 0);
3373 }
3374
3375 if (state.done & DONE_BANCLEAN) /* process bans */
3376 mode_process_bans(&state);
3377
3378 /* process client changes */
3379 if (state.cli_change[0].flag)
3380 mode_process_clients(&state);
3381
3382 return state.args_used; /* tell our parent how many args we gobbled */
3383}
3384
3385/*
3386 * Initialize a join buffer
3387 */
3388void
3389joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
3390 struct Client *connect, unsigned int type, char *comment,
3391 time_t create)
3392{
3393 int i;
3394
3395 assert(0 != jbuf);
3396 assert(0 != source);
3397 assert(0 != connect);
3398
3399 jbuf->jb_source = source; /* just initialize struct JoinBuf */
3400 jbuf->jb_connect = connect;
3401 jbuf->jb_type = type;
3402 jbuf->jb_comment = comment;
3403 jbuf->jb_create = create;
3404 jbuf->jb_count = 0;
3405 jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
3406 type == JOINBUF_TYPE_PART ||
3407 type == JOINBUF_TYPE_PARTALL) ?
3408 STARTJOINLEN : STARTCREATELEN) +
3409 (comment ? strlen(comment) + 2 : 0));
3410
3411 for (i = 0; i < MAXJOINARGS; i++)
3412 jbuf->jb_channels[i] = 0;
3413}
3414
3415/*
3416 * Add a channel to the join buffer
3417 */
3418void
3419joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
3420{
3421 unsigned int len;
3422 int is_local;
3423
3424 assert(0 != jbuf);
3425
3426 if (!chan) {
3427 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
3428 return;
3429 }
3430
3431 is_local = IsLocalChannel(chan->chname);
3432
3433 if (jbuf->jb_type == JOINBUF_TYPE_PART ||
3434 jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
3435 struct Membership *member = find_member_link(chan, jbuf->jb_source);
3436 if (IsUserParting(member))
3437 return;
3438 SetUserParting(member);
3439
3440 /* Send notification to channel */
3441 if (!(flags & (CHFL_ZOMBIE | CHFL_DELAYED)))
3442 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL, 0,
3443 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3444 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3445 else if (MyUser(jbuf->jb_source))
3446 sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
3447 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
3448 ":%H" : "%H :%s", chan, jbuf->jb_comment);
3449 /* XXX: Shouldn't we send a PART here anyway? */
3450 /* to users on the channel? Why? From their POV, the user isn't on
3451 * the channel anymore anyway. We don't send to servers until below,
3452 * when we gang all the channel parts together. Note that this is
3453 * exactly the same logic, albeit somewhat more concise, as was in
3454 * the original m_part.c */
3455
3456 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3457 is_local) /* got to remove user here */
3458 remove_user_from_channel(jbuf->jb_source, chan);
3459 } else {
3460 int oplevel = !chan->mode.apass[0] ? MAXOPLEVEL
3461 : (flags & CHFL_CHANNEL_MANAGER) ? 0
3462 : 1;
3463 /* Add user to channel */
3464 if ((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))
3465 add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, oplevel);
3466 else
3467 add_user_to_channel(chan, jbuf->jb_source, flags, oplevel);
3468
052b069e 3469 /* send JOIN notification to all servers (CREATE is sent later). */
189935b1 3470 if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local)
052b069e 3471 sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
3472 "%H %Tu", chan, chan->creationtime);
189935b1 3473
3474 if (!((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))) {
3475 /* Send the notification to the channel */
3476 sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, 0, "%H", chan);
3477
3478 /* send an op, too, if needed */
3479 if (flags & CHFL_CHANOP && (oplevel < MAXOPLEVEL || !MyUser(jbuf->jb_source)))
3480 sendcmdto_channel_butserv_butone((chan->mode.apass[0] ? &his : jbuf->jb_source),
3481 CMD_MODE, chan, NULL, 0, "%H +o %C",
3482 chan, jbuf->jb_source);
3483 } else if (MyUser(jbuf->jb_source))
3484 sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan);
3485 }
3486
3487 if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3488 jbuf->jb_type == JOINBUF_TYPE_JOIN || is_local)
3489 return; /* don't send to remote */
3490
3491 /* figure out if channel name will cause buffer to be overflowed */
3492 len = chan ? strlen(chan->chname) + 1 : 2;
3493 if (jbuf->jb_strlen + len > BUFSIZE)
3494 joinbuf_flush(jbuf);
3495
3496 /* add channel to list of channels to send and update counts */
3497 jbuf->jb_channels[jbuf->jb_count++] = chan;
3498 jbuf->jb_strlen += len;
3499
3500 /* if we've used up all slots, flush */
3501 if (jbuf->jb_count >= MAXJOINARGS)
3502 joinbuf_flush(jbuf);
3503}
3504
3505/*
3506 * Flush the channel list to remote servers
3507 */
3508int
3509joinbuf_flush(struct JoinBuf *jbuf)
3510{
3511 char chanlist[BUFSIZE];
3512 int chanlist_i = 0;
3513 int i;
3514
3515 if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
3516 jbuf->jb_type == JOINBUF_TYPE_JOIN)
3517 return 0; /* no joins to process */
3518
3519 for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
3520 build_string(chanlist, &chanlist_i,
3521 jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
3522 i == 0 ? '\0' : ',');
3523 if (JOINBUF_TYPE_PART == jbuf->jb_type)
3524 /* Remove user from channel */
3525 remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
3526
3527 jbuf->jb_channels[i] = 0; /* mark slot empty */
3528 }
3529
3530 jbuf->jb_count = 0; /* reset base counters */
3531 jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
3532 STARTJOINLEN : STARTCREATELEN) +
3533 (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
3534
3535 /* and send the appropriate command */
3536 switch (jbuf->jb_type) {
3537 case JOINBUF_TYPE_CREATE:
3538 sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
3539 "%s %Tu", chanlist, jbuf->jb_create);
3540 break;
3541
3542 case JOINBUF_TYPE_PART:
3543 sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
3544 jbuf->jb_comment ? "%s :%s" : "%s", chanlist,
3545 jbuf->jb_comment);
3546 break;
3547 }
3548
3549 return 0;
3550}
3551
3552/* Returns TRUE (1) if client is invited, FALSE (0) if not */
3553int IsInvited(struct Client* cptr, const void* chptr)
3554{
3555 struct SLink *lp;
3556
3557 for (lp = (cli_user(cptr))->invited; lp; lp = lp->next)
3558 if (lp->value.chptr == chptr)
3559 return 1;
3560 return 0;
3561}
3562
3563/* RevealDelayedJoin: sends a join for a hidden user */
3564
3565void RevealDelayedJoin(struct Membership *member)
3566{
3567 ClearDelayedJoin(member);
3568 sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, 0, ":%H",
3569 member->channel);
3570 CheckDelayedJoins(member->channel);
3571}
3572
3573/* CheckDelayedJoins: checks and clear +d if necessary */
3574
3575void CheckDelayedJoins(struct Channel *chan)
3576{
3577 struct Membership *memb2;
3578
3579 if (chan->mode.mode & MODE_WASDELJOINS) {
3580 for (memb2=chan->members;memb2;memb2=memb2->next_member)
3581 if (IsDelayedJoin(memb2))
3582 break;
3583
3584 if (!memb2) {
3585 /* clear +d */
3586 chan->mode.mode &= ~MODE_WASDELJOINS;
3587 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan, NULL, 0,
3588 "%H -d", chan);
3589 }
3590 }
3591}