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