]> jfr.im git - solanum.git/blame - ircd/channel.c
ircd: use GET_SS_FAMILY() in a bunch of places
[solanum.git] / ircd / channel.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * channel.c: Controls channels.
4 *
55abcbb2
KB
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
212380e3
AC
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
212380e3
AC
23 */
24
25#include "stdinc.h"
212380e3 26#include "channel.h"
392ae75c 27#include "chmode.h"
212380e3
AC
28#include "client.h"
29#include "common.h"
30#include "hash.h"
31#include "hook.h"
4562c604 32#include "match.h"
212380e3
AC
33#include "ircd.h"
34#include "numeric.h"
35#include "s_serv.h" /* captab */
36#include "s_user.h"
37#include "send.h"
38#include "whowas.h"
39#include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
40#include "s_newconf.h"
4016731b 41#include "logger.h"
1c60de97 42#include "ipv4_from_ipv6.h"
77d3d2db 43#include "s_assert.h"
212380e3 44
18e4d421 45struct config_channel_entry ConfigChannel;
c3d10343 46rb_dlink_list global_channel_list;
b617afdc
VY
47static rb_bh *channel_heap;
48static rb_bh *ban_heap;
49static rb_bh *topic_heap;
50static rb_bh *member_heap;
51
212380e3
AC
52static void free_topic(struct Channel *chptr);
53
54static int h_can_join;
0aa36c5f 55static int h_can_send;
749d8c11 56int h_get_channel_access;
212380e3
AC
57
58/* init_channels()
59 *
60 * input -
61 * output -
62 * side effects - initialises the various blockheaps
63 */
64void
65init_channels(void)
66{
c608a061
AC
67 channel_heap = rb_bh_create(sizeof(struct Channel), CHANNEL_HEAP_SIZE, "channel_heap");
68 ban_heap = rb_bh_create(sizeof(struct Ban), BAN_HEAP_SIZE, "ban_heap");
69 topic_heap = rb_bh_create(TOPICLEN + 1 + USERHOST_REPLYLEN, TOPIC_HEAP_SIZE, "topic_heap");
70 member_heap = rb_bh_create(sizeof(struct membership), MEMBER_HEAP_SIZE, "member_heap");
212380e3
AC
71
72 h_can_join = register_hook("can_join");
0aa36c5f 73 h_can_send = register_hook("can_send");
749d8c11 74 h_get_channel_access = register_hook("get_channel_access");
212380e3
AC
75}
76
77/*
78 * allocate_channel - Allocates a channel
79 */
80struct Channel *
81allocate_channel(const char *chname)
82{
83 struct Channel *chptr;
398b6a73 84 chptr = rb_bh_alloc(channel_heap);
47a03750 85 chptr->chname = rb_strdup(chname);
212380e3
AC
86 return (chptr);
87}
88
89void
90free_channel(struct Channel *chptr)
91{
637c4932 92 rb_free(chptr->chname);
78e6b731 93 rb_free(chptr->mode_lock);
398b6a73 94 rb_bh_free(channel_heap, chptr);
212380e3
AC
95}
96
97struct Ban *
765d839d 98allocate_ban(const char *banstr, const char *who, const char *forward)
212380e3
AC
99{
100 struct Ban *bptr;
398b6a73 101 bptr = rb_bh_alloc(ban_heap);
47a03750
VY
102 bptr->banstr = rb_strdup(banstr);
103 bptr->who = rb_strdup(who);
765d839d 104 bptr->forward = forward ? rb_strdup(forward) : NULL;
212380e3
AC
105
106 return (bptr);
107}
108
109void
110free_ban(struct Ban *bptr)
111{
637c4932
VY
112 rb_free(bptr->banstr);
113 rb_free(bptr->who);
765d839d 114 rb_free(bptr->forward);
398b6a73 115 rb_bh_free(ban_heap, bptr);
212380e3
AC
116}
117
27912fd4
AC
118/*
119 * send_channel_join()
120 *
121 * input - channel to join, client joining.
122 * output - none
123 * side effects - none
124 */
125void
126send_channel_join(struct Channel *chptr, struct Client *client_p)
127{
128 if (!IsClient(client_p))
129 return;
130
92052a5c
AC
131 sendto_channel_local_with_capability(ALL_MEMBERS, NOCAPS, CLICAP_EXTENDED_JOIN, chptr, ":%s!%s@%s JOIN %s",
132 client_p->name, client_p->username, client_p->host, chptr->chname);
133
26e9dd93 134 sendto_channel_local_with_capability(ALL_MEMBERS, CLICAP_EXTENDED_JOIN, NOCAPS, chptr, ":%s!%s@%s JOIN %s %s :%s",
92052a5c
AC
135 client_p->name, client_p->username, client_p->host, chptr->chname,
136 EmptyString(client_p->user->suser) ? "*" : client_p->user->suser,
26e9dd93 137 client_p->info);
c5bbc603
KB
138
139 /* Send away message to away-notify enabled clients. */
140 if (client_p->user->away)
141 sendto_channel_local_with_capability_butone(client_p, ALL_MEMBERS, CLICAP_AWAY_NOTIFY, NOCAPS, chptr,
142 ":%s!%s@%s AWAY :%s", client_p->name, client_p->username,
143 client_p->host, client_p->user->away);
27912fd4 144}
212380e3
AC
145
146/* find_channel_membership()
147 *
148 * input - channel to find them in, client to find
149 * output - membership of client in channel, else NULL
150 * side effects -
151 */
152struct membership *
153find_channel_membership(struct Channel *chptr, struct Client *client_p)
154{
155 struct membership *msptr;
330fc5c1 156 rb_dlink_node *ptr;
212380e3
AC
157
158 if(!IsClient(client_p))
159 return NULL;
160
161 /* Pick the most efficient list to use to be nice to things like
162 * CHANSERV which could be in a large number of channels
163 */
330fc5c1 164 if(rb_dlink_list_length(&chptr->members) < rb_dlink_list_length(&client_p->user->channel))
212380e3 165 {
5cefa1d6 166 RB_DLINK_FOREACH(ptr, chptr->members.head)
212380e3
AC
167 {
168 msptr = ptr->data;
169
170 if(msptr->client_p == client_p)
171 return msptr;
172 }
173 }
174 else
175 {
5cefa1d6 176 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
212380e3
AC
177 {
178 msptr = ptr->data;
179
180 if(msptr->chptr == chptr)
181 return msptr;
182 }
183 }
184
185 return NULL;
186}
187
188/* find_channel_status()
189 *
190 * input - membership to get status for, whether we can combine flags
191 * output - flags of user on channel
192 * side effects -
193 */
194const char *
195find_channel_status(struct membership *msptr, int combine)
196{
197 static char buffer[3];
198 char *p;
199
200 p = buffer;
201
202 if(is_chanop(msptr))
203 {
204 if(!combine)
205 return "@";
206 *p++ = '@';
207 }
208
209 if(is_voiced(msptr))
210 *p++ = '+';
211
212 *p = '\0';
213 return buffer;
214}
215
216/* add_user_to_channel()
217 *
218 * input - channel to add client to, client to add, channel flags
55abcbb2 219 * output -
212380e3
AC
220 * side effects - user is added to channel
221 */
222void
223add_user_to_channel(struct Channel *chptr, struct Client *client_p, int flags)
224{
225 struct membership *msptr;
226
227 s_assert(client_p->user != NULL);
228 if(client_p->user == NULL)
229 return;
230
398b6a73 231 msptr = rb_bh_alloc(member_heap);
212380e3
AC
232
233 msptr->chptr = chptr;
234 msptr->client_p = client_p;
235 msptr->flags = flags;
236
330fc5c1
AC
237 rb_dlinkAdd(msptr, &msptr->usernode, &client_p->user->channel);
238 rb_dlinkAdd(msptr, &msptr->channode, &chptr->members);
212380e3
AC
239
240 if(MyClient(client_p))
330fc5c1 241 rb_dlinkAdd(msptr, &msptr->locchannode, &chptr->locmembers);
212380e3
AC
242}
243
244/* remove_user_from_channel()
245 *
246 * input - membership pointer to remove from channel
247 * output -
248 * side effects - membership (thus user) is removed from channel
249 */
250void
251remove_user_from_channel(struct membership *msptr)
252{
253 struct Client *client_p;
254 struct Channel *chptr;
255 s_assert(msptr != NULL);
256 if(msptr == NULL)
257 return;
258
259 client_p = msptr->client_p;
260 chptr = msptr->chptr;
261
330fc5c1
AC
262 rb_dlinkDelete(&msptr->usernode, &client_p->user->channel);
263 rb_dlinkDelete(&msptr->channode, &chptr->members);
212380e3
AC
264
265 if(client_p->servptr == &me)
330fc5c1 266 rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers);
212380e3 267
330fc5c1 268 if(!(chptr->mode.mode & MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0)
212380e3
AC
269 destroy_channel(chptr);
270
398b6a73 271 rb_bh_free(member_heap, msptr);
212380e3
AC
272
273 return;
274}
275
276/* remove_user_from_channels()
277 *
278 * input - user to remove from all channels
279 * output -
280 * side effects - user is removed from all channels
281 */
282void
283remove_user_from_channels(struct Client *client_p)
284{
285 struct Channel *chptr;
286 struct membership *msptr;
330fc5c1 287 rb_dlink_node *ptr;
637c4932 288 rb_dlink_node *next_ptr;
212380e3
AC
289
290 if(client_p == NULL)
291 return;
292
637c4932 293 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->user->channel.head)
212380e3
AC
294 {
295 msptr = ptr->data;
296 chptr = msptr->chptr;
297
330fc5c1 298 rb_dlinkDelete(&msptr->channode, &chptr->members);
212380e3
AC
299
300 if(client_p->servptr == &me)
330fc5c1 301 rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers);
212380e3 302
330fc5c1 303 if(!(chptr->mode.mode & MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0)
212380e3
AC
304 destroy_channel(chptr);
305
398b6a73 306 rb_bh_free(member_heap, msptr);
212380e3
AC
307 }
308
309 client_p->user->channel.head = client_p->user->channel.tail = NULL;
310 client_p->user->channel.length = 0;
311}
312
313/* invalidate_bancache_user()
314 *
315 * input - user to invalidate ban cache for
316 * output -
317 * side effects - ban cache is invalidated for all memberships of that user
318 * to be used after a nick change
319 */
320void
321invalidate_bancache_user(struct Client *client_p)
322{
323 struct membership *msptr;
330fc5c1 324 rb_dlink_node *ptr;
212380e3
AC
325
326 if(client_p == NULL)
327 return;
328
5cefa1d6 329 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
212380e3
AC
330 {
331 msptr = ptr->data;
332 msptr->bants = 0;
333 msptr->flags &= ~CHFL_BANNED;
334 }
335}
336
337/* check_channel_name()
338 *
339 * input - channel name
340 * output - 1 if valid channel name, else 0
341 * side effects -
342 */
343int
344check_channel_name(const char *name)
345{
346 s_assert(name != NULL);
347 if(name == NULL)
348 return 0;
349
350 for (; *name; ++name)
351 {
352 if(!IsChanChar(*name))
353 return 0;
354 }
355
356 return 1;
357}
358
359/* free_channel_list()
360 *
330fc5c1 361 * input - rb_dlink list to free
212380e3
AC
362 * output -
363 * side effects - list of b/e/I modes is cleared
364 */
365void
330fc5c1 366free_channel_list(rb_dlink_list * list)
212380e3 367{
330fc5c1 368 rb_dlink_node *ptr;
637c4932 369 rb_dlink_node *next_ptr;
212380e3
AC
370 struct Ban *actualBan;
371
637c4932 372 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
212380e3
AC
373 {
374 actualBan = ptr->data;
375 free_ban(actualBan);
376 }
377
378 list->head = list->tail = NULL;
379 list->length = 0;
380}
381
382/* destroy_channel()
383 *
384 * input - channel to destroy
385 * output -
386 * side effects - channel is obliterated
387 */
388void
389destroy_channel(struct Channel *chptr)
390{
637c4932 391 rb_dlink_node *ptr, *next_ptr;
212380e3 392
637c4932 393 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->invites.head)
212380e3
AC
394 {
395 del_invite(chptr, ptr->data);
396 }
397
398 /* free all bans/exceptions/denies */
399 free_channel_list(&chptr->banlist);
400 free_channel_list(&chptr->exceptlist);
401 free_channel_list(&chptr->invexlist);
fea1ad52 402 free_channel_list(&chptr->quietlist);
212380e3
AC
403
404 /* Free the topic */
405 free_topic(chptr);
406
330fc5c1 407 rb_dlinkDelete(&chptr->node, &global_channel_list);
212380e3
AC
408 del_from_channel_hash(chptr->chname, chptr);
409 free_channel(chptr);
410}
411
412/* channel_pub_or_secret()
413 *
414 * input - channel
415 * output - "=" if public, "@" if secret, else "*"
416 * side effects -
417 */
418static const char *
419channel_pub_or_secret(struct Channel *chptr)
420{
421 if(PubChannel(chptr))
422 return ("=");
423 else if(SecretChannel(chptr))
424 return ("@");
425 return ("*");
426}
427
428/* channel_member_names()
429 *
430 * input - channel to list, client to list to, show endofnames
431 * output -
432 * side effects - client is given list of users on channel
433 */
434void
435channel_member_names(struct Channel *chptr, struct Client *client_p, int show_eon)
436{
437 struct membership *msptr;
438 struct Client *target_p;
330fc5c1 439 rb_dlink_node *ptr;
212380e3
AC
440 char lbuf[BUFSIZE];
441 char *t;
442 int mlen;
443 int tlen;
444 int cur_len;
445 int is_member;
446 int stack = IsCapable(client_p, CLICAP_MULTI_PREFIX);
447
448 if(ShowChannel(client_p, chptr))
449 {
450 is_member = IsMember(client_p, chptr);
451
5203cba5 452 cur_len = mlen = sprintf(lbuf, form_str(RPL_NAMREPLY),
212380e3
AC
453 me.name, client_p->name,
454 channel_pub_or_secret(chptr), chptr->chname);
455
456 t = lbuf + cur_len;
457
5cefa1d6 458 RB_DLINK_FOREACH(ptr, chptr->members.head)
212380e3
AC
459 {
460 msptr = ptr->data;
461 target_p = msptr->client_p;
462
463 if(IsInvisible(target_p) && !is_member)
464 continue;
465
1b54aa5c 466 if (IsCapable(client_p, CLICAP_USERHOST_IN_NAMES))
212380e3 467 {
1b54aa5c
MT
468 /* space, possible "@+" prefix */
469 if (cur_len + strlen(target_p->name) + strlen(target_p->username) + strlen(target_p->host) + 5 >= BUFSIZE - 5)
470 {
471 *(t - 1) = '\0';
472 sendto_one(client_p, "%s", lbuf);
473 cur_len = mlen;
474 t = lbuf + mlen;
475 }
476
5203cba5 477 tlen = sprintf(t, "%s%s!%s@%s ", find_channel_status(msptr, stack),
1b54aa5c
MT
478 target_p->name, target_p->username, target_p->host);
479 }
480 else
481 {
482 /* space, possible "@+" prefix */
483 if(cur_len + strlen(target_p->name) + 3 >= BUFSIZE - 3)
484 {
485 *(t - 1) = '\0';
486 sendto_one(client_p, "%s", lbuf);
487 cur_len = mlen;
488 t = lbuf + mlen;
489 }
490
5203cba5 491 tlen = sprintf(t, "%s%s ", find_channel_status(msptr, stack),
1b54aa5c 492 target_p->name);
212380e3 493 }
212380e3
AC
494
495 cur_len += tlen;
496 t += tlen;
497 }
498
499 /* The old behaviour here was to always output our buffer,
500 * even if there are no clients we can show. This happens
501 * when a client does "NAMES" with no parameters, and all
502 * the clients on a -sp channel are +i. I dont see a good
503 * reason for keeping that behaviour, as it just wastes
504 * bandwidth. --anfl
505 */
506 if(cur_len != mlen)
507 {
508 *(t - 1) = '\0';
509 sendto_one(client_p, "%s", lbuf);
510 }
511 }
512
513 if(show_eon)
514 sendto_one(client_p, form_str(RPL_ENDOFNAMES),
515 me.name, client_p->name, chptr->chname);
516}
517
518/* del_invite()
519 *
520 * input - channel to remove invite from, client to remove
521 * output -
522 * side effects - user is removed from invite list, if exists
523 */
524void
525del_invite(struct Channel *chptr, struct Client *who)
526{
330fc5c1
AC
527 rb_dlinkFindDestroy(who, &chptr->invites);
528 rb_dlinkFindDestroy(chptr, &who->user->invited);
212380e3
AC
529}
530
a14de124 531/* is_banned_list()
212380e3 532 *
a14de124
JT
533 * input - channel to check bans for, ban list (banlist or quietlist),
534 * user to check bans against, optional prebuilt buffers,
535 * optional forward channel pointer
212380e3
AC
536 * output - 1 if banned, else 0
537 * side effects -
538 */
a14de124
JT
539static int
540is_banned_list(struct Channel *chptr, rb_dlink_list *list,
541 struct Client *who, struct membership *msptr,
542 const char *s, const char *s2, const char **forward)
212380e3
AC
543{
544 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
545 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
546 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
1c60de97 547 char src_ip4host[NICKLEN + USERLEN + HOSTLEN + 6];
212380e3 548 char *s3 = NULL;
1c60de97
JT
549 char *s4 = NULL;
550 struct sockaddr_in ip4;
330fc5c1 551 rb_dlink_node *ptr;
212380e3
AC
552 struct Ban *actualBan = NULL;
553 struct Ban *actualExcept = NULL;
554
555 if(!MyClient(who))
556 return 0;
557
558 /* if the buffers havent been built, do it here */
559 if(s == NULL)
560 {
5203cba5
VI
561 sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host);
562 sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost);
212380e3
AC
563
564 s = src_host;
565 s2 = src_iphost;
566 }
567 if(who->localClient->mangledhost != NULL)
568 {
569 /* if host mangling mode enabled, also check their real host */
570 if(!strcmp(who->host, who->localClient->mangledhost))
571 {
5203cba5 572 sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost);
212380e3
AC
573 s3 = src_althost;
574 }
575 /* if host mangling mode not enabled and no other spoof,
576 * also check the mangled form of their host */
577 else if (!IsDynSpoof(who))
578 {
5203cba5 579 sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost);
212380e3
AC
580 s3 = src_althost;
581 }
582 }
1c60de97 583#ifdef RB_IPV6
e867208d 584 if(GET_SS_FAMILY(&who->localClient->ip) == AF_INET6 &&
1c60de97
JT
585 ipv4_from_ipv6((const struct sockaddr_in6 *)&who->localClient->ip, &ip4))
586 {
5203cba5 587 sprintf(src_ip4host, "%s!%s@", who->name, who->username);
1c60de97
JT
588 s4 = src_ip4host + strlen(src_ip4host);
589 rb_inet_ntop_sock((struct sockaddr *)&ip4,
590 s4, src_ip4host + sizeof src_ip4host - s4);
591 s4 = src_ip4host;
592 }
593#endif
212380e3 594
a14de124 595 RB_DLINK_FOREACH(ptr, list->head)
212380e3
AC
596 {
597 actualBan = ptr->data;
598 if(match(actualBan->banstr, s) ||
599 match(actualBan->banstr, s2) ||
600 match_cidr(actualBan->banstr, s2) ||
601 match_extban(actualBan->banstr, who, chptr, CHFL_BAN) ||
1c60de97
JT
602 (s3 != NULL && match(actualBan->banstr, s3))
603#ifdef RB_IPV6
604 ||
605 (s4 != NULL && (match(actualBan->banstr, s4) || match_cidr(actualBan->banstr, s4)))
606#endif
607 )
212380e3
AC
608 break;
609 else
610 actualBan = NULL;
611 }
612
613 if((actualBan != NULL) && ConfigChannel.use_except)
614 {
5cefa1d6 615 RB_DLINK_FOREACH(ptr, chptr->exceptlist.head)
212380e3
AC
616 {
617 actualExcept = ptr->data;
618
619 /* theyre exempted.. */
620 if(match(actualExcept->banstr, s) ||
621 match(actualExcept->banstr, s2) ||
622 match_cidr(actualExcept->banstr, s2) ||
623 match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) ||
624 (s3 != NULL && match(actualExcept->banstr, s3)))
625 {
626 /* cache the fact theyre not banned */
627 if(msptr != NULL)
628 {
629 msptr->bants = chptr->bants;
630 msptr->flags &= ~CHFL_BANNED;
631 }
632
633 return CHFL_EXCEPTION;
634 }
635 }
636 }
637
638 /* cache the banned/not banned status */
639 if(msptr != NULL)
640 {
641 msptr->bants = chptr->bants;
642
643 if(actualBan != NULL)
644 {
645 msptr->flags |= CHFL_BANNED;
646 return CHFL_BAN;
647 }
648 else
649 {
650 msptr->flags &= ~CHFL_BANNED;
651 return 0;
652 }
653 }
654
765d839d
EM
655 if (actualBan && actualBan->forward && forward)
656 *forward = actualBan->forward;
657
212380e3
AC
658 return ((actualBan ? CHFL_BAN : 0));
659}
660
a14de124
JT
661/* is_banned()
662 *
663 * input - channel to check bans for, user to check bans against
664 * optional prebuilt buffers, optional forward channel pointer
665 * output - 1 if banned, else 0
666 * side effects -
667 */
668int
669is_banned(struct Channel *chptr, struct Client *who, struct membership *msptr,
670 const char *s, const char *s2, const char **forward)
671{
2f9687c4
AC
672 if (chptr->last_checked_client != NULL &&
673 who == chptr->last_checked_client &&
674 chptr->last_checked_type == CHFL_BAN &&
1e8138af 675 chptr->last_checked_ts > chptr->bants)
2f9687c4
AC
676 return chptr->last_checked_result;
677
bcbc6bd9 678 chptr->last_checked_client = who;
2f9687c4
AC
679 chptr->last_checked_type = CHFL_BAN;
680 chptr->last_checked_result = is_banned_list(chptr, &chptr->banlist, who, msptr, s, s2, forward);
681 chptr->last_checked_ts = rb_current_time();
682
683 return chptr->last_checked_result;
a14de124
JT
684}
685
212380e3
AC
686/* is_quieted()
687 *
688 * input - channel to check bans for, user to check bans against
689 * optional prebuilt buffers
690 * output - 1 if banned, else 0
691 * side effects -
692 */
693int
694is_quieted(struct Channel *chptr, struct Client *who, struct membership *msptr,
695 const char *s, const char *s2)
696{
2f9687c4
AC
697 if (chptr->last_checked_client != NULL &&
698 who == chptr->last_checked_client &&
699 chptr->last_checked_type == CHFL_QUIET &&
1e8138af 700 chptr->last_checked_ts > chptr->bants)
2f9687c4
AC
701 return chptr->last_checked_result;
702
bcbc6bd9 703 chptr->last_checked_client = who;
2f9687c4
AC
704 chptr->last_checked_type = CHFL_QUIET;
705 chptr->last_checked_result = is_banned_list(chptr, &chptr->quietlist, who, msptr, s, s2, NULL);
706 chptr->last_checked_ts = rb_current_time();
707
708 return chptr->last_checked_result;
212380e3
AC
709}
710
711/* can_join()
712 *
713 * input - client to check, channel to check for, key
765d839d 714 * output - reason for not being able to join, else 0, channel name to forward to
212380e3 715 * side effects -
a63f7af7 716 * caveats - this function should only be called on a local user.
212380e3
AC
717 */
718int
fe74401b 719can_join(struct Client *source_p, struct Channel *chptr, const char *key, const char **forward)
212380e3 720{
330fc5c1
AC
721 rb_dlink_node *invite = NULL;
722 rb_dlink_node *ptr;
212380e3
AC
723 struct Ban *invex = NULL;
724 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
725 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
726 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
727 int use_althost = 0;
1ebf4db4 728 int i = 0;
212380e3
AC
729 hook_data_channel moduledata;
730
731 s_assert(source_p->localClient != NULL);
732
8bb19bd7
AC
733 moduledata.client = source_p;
734 moduledata.chptr = chptr;
735 moduledata.approved = 0;
736
5203cba5
VI
737 sprintf(src_host, "%s!%s@%s", source_p->name, source_p->username, source_p->host);
738 sprintf(src_iphost, "%s!%s@%s", source_p->name, source_p->username, source_p->sockhost);
212380e3
AC
739 if(source_p->localClient->mangledhost != NULL)
740 {
741 /* if host mangling mode enabled, also check their real host */
742 if(!strcmp(source_p->host, source_p->localClient->mangledhost))
743 {
5203cba5 744 sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->orighost);
212380e3
AC
745 use_althost = 1;
746 }
747 /* if host mangling mode not enabled and no other spoof,
748 * also check the mangled form of their host */
749 else if (!IsDynSpoof(source_p))
750 {
5203cba5 751 sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->localClient->mangledhost);
212380e3
AC
752 use_althost = 1;
753 }
754 }
755
765d839d 756 if((is_banned(chptr, source_p, NULL, src_host, src_iphost, forward)) == CHFL_BAN)
8bb19bd7
AC
757 {
758 moduledata.approved = ERR_BANNEDFROMCHAN;
759 goto finish_join_check;
760 }
212380e3 761
765d839d
EM
762 if(*chptr->mode.key && (EmptyString(key) || irccmp(chptr->mode.key, key)))
763 {
764 moduledata.approved = ERR_BADCHANNELKEY;
765 goto finish_join_check;
766 }
767
768 /* All checks from this point on will forward... */
769 if(forward)
770 *forward = chptr->mode.forward;
771
212380e3
AC
772 if(chptr->mode.mode & MODE_INVITEONLY)
773 {
5cefa1d6 774 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
212380e3 775 {
1ebf4db4 776 if(invite->data == chptr)
212380e3
AC
777 break;
778 }
1ebf4db4 779 if(invite == NULL)
212380e3
AC
780 {
781 if(!ConfigChannel.use_invex)
8bb19bd7 782 moduledata.approved = ERR_INVITEONLYCHAN;
5cefa1d6 783 RB_DLINK_FOREACH(ptr, chptr->invexlist.head)
212380e3
AC
784 {
785 invex = ptr->data;
786 if(match(invex->banstr, src_host)
787 || match(invex->banstr, src_iphost)
788 || match_cidr(invex->banstr, src_iphost)
789 || match_extban(invex->banstr, source_p, chptr, CHFL_INVEX)
790 || (use_althost && match(invex->banstr, src_althost)))
791 break;
792 }
793 if(ptr == NULL)
8bb19bd7 794 moduledata.approved = ERR_INVITEONLYCHAN;
212380e3
AC
795 }
796 }
797
212380e3 798 if(chptr->mode.limit &&
330fc5c1 799 rb_dlink_list_length(&chptr->members) >= (unsigned long) chptr->mode.limit)
1ebf4db4 800 i = ERR_CHANNELISFULL;
212380e3 801 if(chptr->mode.mode & MODE_REGONLY && EmptyString(source_p->user->suser))
1ebf4db4 802 i = ERR_NEEDREGGEDNICK;
212380e3 803 /* join throttling stuff --nenolod */
1ebf4db4 804 else if(chptr->mode.join_num > 0 && chptr->mode.join_time > 0)
212380e3 805 {
55abcbb2 806 if ((rb_current_time() - chptr->join_delta <=
212380e3
AC
807 chptr->mode.join_time) && (chptr->join_count >=
808 chptr->mode.join_num))
1ebf4db4
JT
809 i = ERR_THROTTLE;
810 }
811
812 /* allow /invite to override +l/+r/+j also -- jilles */
813 if (i != 0 && invite == NULL)
814 {
5cefa1d6 815 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
1ebf4db4
JT
816 {
817 if(invite->data == chptr)
818 break;
819 }
820 if (invite == NULL)
8bb19bd7 821 moduledata.approved = i;
212380e3
AC
822 }
823
8bb19bd7 824finish_join_check:
212380e3
AC
825 call_hook(h_can_join, &moduledata);
826
827 return moduledata.approved;
828}
829
830/* can_send()
831 *
832 * input - user to check in channel, membership pointer
833 * output - whether can explicitly send or not, else CAN_SEND_NONOP
834 * side effects -
835 */
836int
837can_send(struct Channel *chptr, struct Client *source_p, struct membership *msptr)
838{
0aa36c5f
AC
839 hook_data_channel_approval moduledata;
840
841 moduledata.approved = CAN_SEND_NONOP;
202d4966 842 moduledata.dir = MODE_QUERY;
0aa36c5f 843
212380e3
AC
844 if(IsServer(source_p) || IsService(source_p))
845 return CAN_SEND_OPV;
846
847 if(MyClient(source_p) && hash_find_resv(chptr->chname) &&
848 !IsOper(source_p) && !IsExemptResv(source_p))
0aa36c5f 849 moduledata.approved = CAN_SEND_NO;
212380e3
AC
850
851 if(msptr == NULL)
852 {
853 msptr = find_channel_membership(chptr, source_p);
854
855 if(msptr == NULL)
856 {
857 /* if its +m or +n and theyre not in the channel,
858 * they cant send. we dont check bans here because
859 * theres no possibility of caching them --fl
860 */
861 if(chptr->mode.mode & MODE_NOPRIVMSGS || chptr->mode.mode & MODE_MODERATED)
0aa36c5f 862 moduledata.approved = CAN_SEND_NO;
212380e3 863 else
0aa36c5f 864 moduledata.approved = CAN_SEND_NONOP;
b697041e
AC
865
866 return moduledata.approved;
212380e3
AC
867 }
868 }
869
212380e3 870 if(chptr->mode.mode & MODE_MODERATED)
0aa36c5f 871 moduledata.approved = CAN_SEND_NO;
212380e3
AC
872
873 if(MyClient(source_p))
874 {
875 /* cached can_send */
876 if(msptr->bants == chptr->bants)
877 {
878 if(can_send_banned(msptr))
0aa36c5f 879 moduledata.approved = CAN_SEND_NO;
212380e3 880 }
765d839d 881 else if(is_banned(chptr, source_p, msptr, NULL, NULL, NULL) == CHFL_BAN
212380e3 882 || is_quieted(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN)
0aa36c5f 883 moduledata.approved = CAN_SEND_NO;
212380e3
AC
884 }
885
e06988c6
AC
886 if(is_chanop_voiced(msptr))
887 moduledata.approved = CAN_SEND_OPV;
888
f4b52a0a
EM
889 moduledata.client = source_p;
890 moduledata.chptr = msptr->chptr;
891 moduledata.msptr = msptr;
892 moduledata.target = NULL;
202d4966 893 moduledata.dir = (moduledata.approved == CAN_SEND_NO) ? MODE_ADD : MODE_QUERY;
f4b52a0a 894
0aa36c5f
AC
895 call_hook(h_can_send, &moduledata);
896
897 return moduledata.approved;
212380e3
AC
898}
899
15484f02
BG
900/*
901 * flood_attack_channel
902 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
903 * says NOTICE must not auto reply
55abcbb2 904 * - pointer to source Client
15484f02
BG
905 * - pointer to target channel
906 * output - 1 if target is under flood attack
907 * side effects - check for flood attack on target chptr
908 */
909int
910flood_attack_channel(int p_or_n, struct Client *source_p, struct Channel *chptr, char *chname)
911{
912 int delta;
913
914 if(GlobalSetOptions.floodcount && MyClient(source_p))
915 {
916 if((chptr->first_received_message_time + 1) < rb_current_time())
917 {
918 delta = rb_current_time() - chptr->first_received_message_time;
919 chptr->received_number_of_privmsgs -= delta;
920 chptr->first_received_message_time = rb_current_time();
921 if(chptr->received_number_of_privmsgs <= 0)
922 {
923 chptr->received_number_of_privmsgs = 0;
924 chptr->flood_noticed = 0;
925 }
926 }
927
928 if((chptr->received_number_of_privmsgs >= GlobalSetOptions.floodcount)
929 || chptr->flood_noticed)
930 {
931 if(chptr->flood_noticed == 0)
932 {
933 sendto_realops_snomask(SNO_BOTS, *chptr->chname == '&' ? L_ALL : L_NETWIDE,
934 "Possible Flooder %s[%s@%s] on %s target: %s",
935 source_p->name, source_p->username,
936 source_p->orighost,
937 source_p->servptr->name, chptr->chname);
938 chptr->flood_noticed = 1;
939
940 /* Add a bit of penalty */
941 chptr->received_number_of_privmsgs += 2;
942 }
943 if(MyClient(source_p) && (p_or_n != 1))
944 sendto_one(source_p,
945 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
946 me.name, source_p->name, chptr->chname);
947 return 1;
948 }
949 else
950 chptr->received_number_of_privmsgs++;
951 }
952
953 return 0;
954}
955
212380e3
AC
956/* find_bannickchange_channel()
957 * Input: client to check
958 * Output: channel preventing nick change
959 */
960struct Channel *
961find_bannickchange_channel(struct Client *client_p)
962{
963 struct Channel *chptr;
964 struct membership *msptr;
330fc5c1 965 rb_dlink_node *ptr;
212380e3
AC
966 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
967 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
968
969 if (!MyClient(client_p))
970 return NULL;
971
5203cba5
VI
972 sprintf(src_host, "%s!%s@%s", client_p->name, client_p->username, client_p->host);
973 sprintf(src_iphost, "%s!%s@%s", client_p->name, client_p->username, client_p->sockhost);
212380e3 974
5cefa1d6 975 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
212380e3
AC
976 {
977 msptr = ptr->data;
978 chptr = msptr->chptr;
979 if (is_chanop_voiced(msptr))
980 continue;
981 /* cached can_send */
982 if (msptr->bants == chptr->bants)
983 {
984 if (can_send_banned(msptr))
985 return chptr;
986 }
765d839d 987 else if (is_banned(chptr, client_p, msptr, src_host, src_iphost, NULL) == CHFL_BAN
212380e3
AC
988 || is_quieted(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN)
989 return chptr;
990 }
991 return NULL;
992}
993
994/* void check_spambot_warning(struct Client *source_p)
995 * Input: Client to check, channel name or NULL if this is a part.
996 * Output: none
997 * Side-effects: Updates the client's oper_warn_count_down, warns the
998 * IRC operators if necessary, and updates join_leave_countdown as
999 * needed.
1000 */
1001void
1002check_spambot_warning(struct Client *source_p, const char *name)
1003{
1004 int t_delta;
1005 int decrement_count;
1006 if((GlobalSetOptions.spam_num &&
1007 (source_p->localClient->join_leave_count >= GlobalSetOptions.spam_num)))
1008 {
1009 if(source_p->localClient->oper_warn_count_down > 0)
1010 source_p->localClient->oper_warn_count_down--;
1011 else
1012 source_p->localClient->oper_warn_count_down = 0;
e0c1f4ec
JT
1013 if(source_p->localClient->oper_warn_count_down == 0 &&
1014 name != NULL)
212380e3
AC
1015 {
1016 /* Its already known as a possible spambot */
e0c1f4ec
JT
1017 sendto_realops_snomask(SNO_BOTS, L_NETWIDE,
1018 "User %s (%s@%s) trying to join %s is a possible spambot",
1019 source_p->name,
1020 source_p->username, source_p->orighost, name);
212380e3
AC
1021 source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
1022 }
1023 }
1024 else
1025 {
1026 if((t_delta =
e3354945 1027 (rb_current_time() - source_p->localClient->last_leave_time)) >
212380e3
AC
1028 JOIN_LEAVE_COUNT_EXPIRE_TIME)
1029 {
1030 decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
981586df
JT
1031 if(name != NULL)
1032 ;
1033 else if(decrement_count > source_p->localClient->join_leave_count)
212380e3
AC
1034 source_p->localClient->join_leave_count = 0;
1035 else
1036 source_p->localClient->join_leave_count -= decrement_count;
1037 }
1038 else
1039 {
e3354945 1040 if((rb_current_time() -
212380e3
AC
1041 (source_p->localClient->last_join_time)) < GlobalSetOptions.spam_time)
1042 {
1043 /* oh, its a possible spambot */
1044 source_p->localClient->join_leave_count++;
1045 }
1046 }
1047 if(name != NULL)
e3354945 1048 source_p->localClient->last_join_time = rb_current_time();
212380e3 1049 else
e3354945 1050 source_p->localClient->last_leave_time = rb_current_time();
212380e3
AC
1051 }
1052}
1053
1054/* check_splitmode()
1055 *
1056 * input -
1057 * output -
1058 * side effects - compares usercount and servercount against their split
1059 * values and adjusts splitmode accordingly
1060 */
1061void
1062check_splitmode(void *unused)
1063{
1064 if(splitchecking && (ConfigChannel.no_join_on_split || ConfigChannel.no_create_on_split))
1065 {
1066 /* not split, we're being asked to check now because someone
1067 * has left
1068 */
1069 if(!splitmode)
1070 {
1071 if(eob_count < split_servers || Count.total < split_users)
1072 {
1073 splitmode = 1;
1074 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1075 "Network split, activating splitmode");
c608a061 1076 check_splitmode_ev = rb_event_addish("check_splitmode", check_splitmode, NULL, 2);
212380e3
AC
1077 }
1078 }
1079 /* in splitmode, check whether its finished */
1080 else if(eob_count >= split_servers && Count.total >= split_users)
1081 {
1082 splitmode = 0;
1083
1084 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1085 "Network rejoined, deactivating splitmode");
1086
c608a061 1087 rb_event_delete(check_splitmode_ev);
6a309903 1088 check_splitmode_ev = NULL;
212380e3
AC
1089 }
1090 }
1091}
1092
1093
1094/* allocate_topic()
1095 *
1096 * input - channel to allocate topic for
1097 * output - 1 on success, else 0
1098 * side effects - channel gets a topic allocated
1099 */
1100static void
1101allocate_topic(struct Channel *chptr)
1102{
1103 void *ptr;
1104
1105 if(chptr == NULL)
1106 return;
1107
398b6a73 1108 ptr = rb_bh_alloc(topic_heap);
212380e3
AC
1109
1110 /* Basically we allocate one large block for the topic and
1111 * the topic info. We then split it up into two and shove it
55abcbb2 1112 * in the chptr
212380e3
AC
1113 */
1114 chptr->topic = ptr;
1115 chptr->topic_info = (char *) ptr + TOPICLEN + 1;
1116 *chptr->topic = '\0';
1117 *chptr->topic_info = '\0';
1118}
1119
1120/* free_topic()
1121 *
1122 * input - channel which has topic to free
1123 * output -
1124 * side effects - channels topic is free'd
1125 */
1126static void
1127free_topic(struct Channel *chptr)
1128{
1129 void *ptr;
1130
1131 if(chptr == NULL || chptr->topic == NULL)
1132 return;
1133
1134 /* This is safe for now - If you change allocate_topic you
1135 * MUST change this as well
1136 */
1137 ptr = chptr->topic;
398b6a73 1138 rb_bh_free(topic_heap, ptr);
212380e3
AC
1139 chptr->topic = NULL;
1140 chptr->topic_info = NULL;
1141}
1142
1143/* set_channel_topic()
1144 *
1145 * input - channel, topic to set, topic info and topic ts
1146 * output -
1147 * side effects - channels topic, topic info and TS are set.
1148 */
1149void
1150set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_info, time_t topicts)
1151{
1152 if(strlen(topic) > 0)
1153 {
1154 if(chptr->topic == NULL)
1155 allocate_topic(chptr);
f427c8b0
VY
1156 rb_strlcpy(chptr->topic, topic, TOPICLEN + 1);
1157 rb_strlcpy(chptr->topic_info, topic_info, USERHOST_REPLYLEN);
212380e3
AC
1158 chptr->topic_time = topicts;
1159 }
1160 else
1161 {
1162 if(chptr->topic != NULL)
1163 free_topic(chptr);
1164 chptr->topic_time = 0;
1165 }
1166}
1167
3b8a6350 1168/* channel_modes()
212380e3
AC
1169 *
1170 * inputs - pointer to channel
1171 * - pointer to client
f1e35c19
JT
1172 * output - string with simple modes
1173 * side effects - result from previous calls overwritten
212380e3
AC
1174 *
1175 * Stolen from ShadowIRCd 4 --nenolod
1176 */
1177const char *
3b8a6350 1178channel_modes(struct Channel *chptr, struct Client *client_p)
212380e3
AC
1179{
1180 int i;
1181 char buf1[BUFSIZE];
1182 char buf2[BUFSIZE];
1183 static char final[BUFSIZE];
1184 char *mbuf = buf1;
1185 char *pbuf = buf2;
1186
1187 *mbuf++ = '+';
1188 *pbuf = '\0';
1189
efccc22c 1190 for (i = 0; i < 256; i++)
be29ec79
AC
1191 {
1192 if(chmode_table[i].set_func == chm_hidden && (!IsOper(client_p) || !IsClient(client_p)))
1193 continue;
3b8a6350 1194 if(chptr->mode.mode & chmode_flags[i])
efccc22c 1195 *mbuf++ = i;
be29ec79 1196 }
212380e3 1197
3b8a6350 1198 if(chptr->mode.limit)
212380e3
AC
1199 {
1200 *mbuf++ = 'l';
1201
f1e35c19 1202 if(!IsClient(client_p) || IsMember(client_p, chptr))
5203cba5 1203 pbuf += sprintf(pbuf, " %d", chptr->mode.limit);
212380e3
AC
1204 }
1205
3b8a6350 1206 if(*chptr->mode.key)
212380e3
AC
1207 {
1208 *mbuf++ = 'k';
1209
f1e35c19 1210 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
5203cba5 1211 pbuf += sprintf(pbuf, " %s", chptr->mode.key);
212380e3
AC
1212 }
1213
3b8a6350 1214 if(chptr->mode.join_num)
212380e3
AC
1215 {
1216 *mbuf++ = 'j';
1217
f1e35c19 1218 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
5203cba5 1219 pbuf += sprintf(pbuf, " %d:%d", chptr->mode.join_num,
3b8a6350 1220 chptr->mode.join_time);
212380e3
AC
1221 }
1222
2da6f6eb
JT
1223 if(*chptr->mode.forward &&
1224 (ConfigChannel.use_forward || !IsClient(client_p)))
212380e3
AC
1225 {
1226 *mbuf++ = 'f';
1227
f1e35c19 1228 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
5203cba5 1229 pbuf += sprintf(pbuf, " %s", chptr->mode.forward);
212380e3
AC
1230 }
1231
1232 *mbuf = '\0';
1233
f427c8b0 1234 rb_strlcpy(final, buf1, sizeof final);
1f9de103 1235 rb_strlcat(final, buf2, sizeof final);
212380e3
AC
1236 return final;
1237}
1238
212380e3
AC
1239/* void send_cap_mode_changes(struct Client *client_p,
1240 * struct Client *source_p,
1241 * struct Channel *chptr, int cap, int nocap)
1242 * Input: The client sending(client_p), the source client(source_p),
1243 * the channel to send mode changes for(chptr)
1244 * Output: None.
1245 * Side-effects: Sends the appropriate mode changes to capable servers.
1246 *
1247 * Reverted back to my original design, except that we now keep a count
1248 * of the number of servers which each combination as an optimisation, so
1249 * the capabs combinations which are not needed are not worked out. -A1kmm
346fba92
AC
1250 *
1251 * Removed most code here because we don't need to be compatible with ircd
1252 * 2.8.21+CSr and stuff. --nenolod
212380e3
AC
1253 */
1254void
1255send_cap_mode_changes(struct Client *client_p, struct Client *source_p,
1256 struct Channel *chptr, struct ChModeChange mode_changes[], int mode_count)
1257{
1258 static char modebuf[BUFSIZE];
1259 static char parabuf[BUFSIZE];
1260 int i, mbl, pbl, nc, mc, preflen, len;
1261 char *pbuf;
1262 const char *arg;
346fba92 1263 int dir;
212380e3
AC
1264 int arglen;
1265
1266 /* Now send to servers... */
346fba92
AC
1267 mc = 0;
1268 nc = 0;
1269 pbl = 0;
1270 parabuf[0] = 0;
1271 pbuf = parabuf;
1272 dir = MODE_QUERY;
1273
5203cba5 1274 mbl = preflen = sprintf(modebuf, ":%s TMODE %ld %s ",
346fba92
AC
1275 use_id(source_p), (long) chptr->channelts,
1276 chptr->chname);
1277
1278 /* loop the list of - modes we have */
1279 for (i = 0; i < mode_count; i++)
212380e3 1280 {
346fba92
AC
1281 /* if they dont support the cap we need, or they do support a cap they
1282 * cant have, then dont add it to the modebuf.. that way they wont see
1283 * the mode
1284 */
1285 if (mode_changes[i].letter == 0)
212380e3
AC
1286 continue;
1287
346fba92
AC
1288 if (!EmptyString(mode_changes[i].id))
1289 arg = mode_changes[i].id;
1290 else
1291 arg = mode_changes[i].arg;
212380e3 1292
346fba92 1293 if(arg)
212380e3 1294 {
346fba92 1295 arglen = strlen(arg);
212380e3 1296
346fba92
AC
1297 /* dont even think about it! --fl */
1298 if(arglen > MODEBUFLEN - 5)
1299 continue;
1300 }
212380e3 1301
346fba92
AC
1302 /* if we're creeping past the buf size, we need to send it and make
1303 * another line for the other modes
1304 * XXX - this could give away server topology with uids being
1305 * different lengths, but not much we can do, except possibly break
1306 * them as if they were the longest of the nick or uid at all times,
1307 * which even then won't work as we don't always know the uid -A1kmm.
1308 */
1309 if(arg && ((mc == MAXMODEPARAMSSERV) ||
1310 ((mbl + pbl + arglen + 4) > (BUFSIZE - 3))))
1311 {
1312 if(nc != 0)
51452a37 1313 sendto_server(client_p, chptr, NOCAPS, NOCAPS,
346fba92
AC
1314 "%s %s", modebuf, parabuf);
1315 nc = 0;
1316 mc = 0;
1317
1318 mbl = preflen;
1319 pbl = 0;
1320 pbuf = parabuf;
1321 parabuf[0] = 0;
1322 dir = MODE_QUERY;
1323 }
212380e3 1324
346fba92
AC
1325 if(dir != mode_changes[i].dir)
1326 {
1327 modebuf[mbl++] = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
1328 dir = mode_changes[i].dir;
1329 }
212380e3 1330
346fba92
AC
1331 modebuf[mbl++] = mode_changes[i].letter;
1332 modebuf[mbl] = 0;
1333 nc++;
212380e3 1334
346fba92
AC
1335 if(arg != NULL)
1336 {
5203cba5 1337 len = sprintf(pbuf, "%s ", arg);
346fba92
AC
1338 pbuf += len;
1339 pbl += len;
1340 mc++;
212380e3 1341 }
346fba92 1342 }
212380e3 1343
346fba92
AC
1344 if(pbl && parabuf[pbl - 1] == ' ')
1345 parabuf[pbl - 1] = 0;
212380e3 1346
346fba92 1347 if(nc != 0)
51452a37 1348 sendto_server(client_p, chptr, NOCAPS, NOCAPS, "%s %s", modebuf, parabuf);
212380e3 1349}
dca9e552 1350
346fba92 1351void
dca9e552
JT
1352resv_chan_forcepart(const char *name, const char *reason, int temp_time)
1353{
1354 rb_dlink_node *ptr;
1355 rb_dlink_node *next_ptr;
1356 struct Channel *chptr;
1357 struct membership *msptr;
1358 struct Client *target_p;
1359
1360 if(!ConfigChannel.resv_forcepart)
1361 return;
1362
1363 /* for each user on our server in the channel list
1364 * send them a PART, and notify opers.
1365 */
1366 chptr = find_channel(name);
1367 if(chptr != NULL)
1368 {
1369 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
1370 {
1371 msptr = ptr->data;
1372 target_p = msptr->client_p;
1373
1374 if(IsExemptResv(target_p))
1375 continue;
1376
1377 sendto_server(target_p, chptr, CAP_TS6, NOCAPS,
1378 ":%s PART %s", target_p->id, chptr->chname);
1379
1380 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s :%s",
1381 target_p->name, target_p->username,
1382 target_p->host, chptr->chname, target_p->name);
1383
1384 remove_user_from_channel(msptr);
1385
1386 /* notify opers & user they were removed from the channel */
1387 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1388 "Forced PART for %s!%s@%s from %s (%s)",
55abcbb2 1389 target_p->name, target_p->username,
dca9e552
JT
1390 target_p->host, name, reason);
1391
1392 if(temp_time > 0)
1393 sendto_one_notice(target_p, ":*** Channel %s is temporarily unavailable on this server.",
1394 name);
1395 else
1396 sendto_one_notice(target_p, ":*** Channel %s is no longer available on this server.",
1397 name);
1398 }
1399 }
1400}