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