]> jfr.im git - irc/rqf/shadowircd.git/blame - src/channel.c
Add is_any_op() in preperation for adding +ah, start using it instead of is_chanop...
[irc/rqf/shadowircd.git] / src / channel.c
CommitLineData
212380e3 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 25 */
26
27#include "stdinc.h"
212380e3 28#include "channel.h"
fba62b01 29#include "chmode.h"
212380e3 30#include "client.h"
31#include "common.h"
32#include "hash.h"
33#include "hook.h"
13ae2f4b 34#include "match.h"
212380e3 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"
d3455e2c 43#include "logger.h"
3472ff3f 44#include "packet.h"
212380e3 45
9813daca 46struct config_channel_entry ConfigChannel;
1a218aaf 47rb_dlink_list global_channel_list;
5475a932
VY
48static rb_bh *channel_heap;
49static rb_bh *ban_heap;
50static rb_bh *topic_heap;
51static rb_bh *member_heap;
52
212380e3 53static int channel_capabs[] = { CAP_EX, CAP_IE,
54 CAP_SERVICE,
55 CAP_TS6
56};
57
58#define NCHCAPS (sizeof(channel_capabs)/sizeof(int))
59#define NCHCAP_COMBOS (1 << NCHCAPS)
60
61static struct ChCapCombo chcap_combos[NCHCAP_COMBOS];
62
63static void free_topic(struct Channel *chptr);
64
65static int h_can_join;
67b90240
JH
66static int h_can_create_channel;
67static int h_channel_join;
212380e3 68
69/* init_channels()
70 *
71 * input -
72 * output -
73 * side effects - initialises the various blockheaps
74 */
75void
76init_channels(void)
77{
dd9be678
WP
78 channel_heap = rb_bh_create(sizeof(struct Channel), CHANNEL_HEAP_SIZE, "channel_heap");
79 ban_heap = rb_bh_create(sizeof(struct Ban), BAN_HEAP_SIZE, "ban_heap");
80 topic_heap = rb_bh_create(TOPICLEN + 1 + USERHOST_REPLYLEN, TOPIC_HEAP_SIZE, "topic_heap");
81 member_heap = rb_bh_create(sizeof(struct membership), MEMBER_HEAP_SIZE, "member_heap");
212380e3 82
83 h_can_join = register_hook("can_join");
67b90240
JH
84 h_channel_join = register_hook("channel_join");
85 h_can_create_channel = register_hook("can_create_channel");
212380e3 86}
87
88/*
89 * allocate_channel - Allocates a channel
90 */
91struct Channel *
92allocate_channel(const char *chname)
93{
94 struct Channel *chptr;
6e9b4415 95 chptr = rb_bh_alloc(channel_heap);
62d28946 96 chptr->chname = rb_strdup(chname);
212380e3 97 return (chptr);
98}
99
100void
101free_channel(struct Channel *chptr)
102{
90a3c35b 103 rb_free(chptr->chname);
6e9b4415 104 rb_bh_free(channel_heap, chptr);
212380e3 105}
106
107struct Ban *
108allocate_ban(const char *banstr, const char *who)
109{
110 struct Ban *bptr;
6e9b4415 111 bptr = rb_bh_alloc(ban_heap);
62d28946
VY
112 bptr->banstr = rb_strdup(banstr);
113 bptr->who = rb_strdup(who);
212380e3 114
115 return (bptr);
116}
117
118void
119free_ban(struct Ban *bptr)
120{
90a3c35b
VY
121 rb_free(bptr->banstr);
122 rb_free(bptr->who);
6e9b4415 123 rb_bh_free(ban_heap, bptr);
212380e3 124}
125
126
127/* find_channel_membership()
128 *
129 * input - channel to find them in, client to find
130 * output - membership of client in channel, else NULL
131 * side effects -
132 */
133struct membership *
134find_channel_membership(struct Channel *chptr, struct Client *client_p)
135{
136 struct membership *msptr;
af81d5a0 137 rb_dlink_node *ptr;
212380e3 138
139 if(!IsClient(client_p))
140 return NULL;
141
142 /* Pick the most efficient list to use to be nice to things like
143 * CHANSERV which could be in a large number of channels
144 */
af81d5a0 145 if(rb_dlink_list_length(&chptr->members) < rb_dlink_list_length(&client_p->user->channel))
212380e3 146 {
8e69bb4e 147 RB_DLINK_FOREACH(ptr, chptr->members.head)
212380e3 148 {
149 msptr = ptr->data;
150
151 if(msptr->client_p == client_p)
152 return msptr;
153 }
154 }
155 else
156 {
8e69bb4e 157 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
212380e3 158 {
159 msptr = ptr->data;
160
161 if(msptr->chptr == chptr)
162 return msptr;
163 }
164 }
165
166 return NULL;
167}
168
169/* find_channel_status()
170 *
171 * input - membership to get status for, whether we can combine flags
172 * output - flags of user on channel
173 * side effects -
174 */
175const char *
176find_channel_status(struct membership *msptr, int combine)
177{
178 static char buffer[3];
179 char *p;
180
181 p = buffer;
182
183 if(is_chanop(msptr))
184 {
185 if(!combine)
186 return "@";
187 *p++ = '@';
188 }
189
190 if(is_voiced(msptr))
191 *p++ = '+';
192
193 *p = '\0';
194 return buffer;
195}
196
d1c7eccf
G
197/* is_any_op()
198 *
199 * input - membership to check for ops
200 * output - 1 if the user is op, halfop, or owner, 0 elsewise
201 * side effects -
202 */
203
204int
205is_any_op(struct membership *msptr)
206{
207 /* Checks for +ah will go here when +ah are implemented */
208 if(is_chanop(msptr))
209 return 1;
210 else
211 return 0;
212}
213
212380e3 214/* add_user_to_channel()
215 *
216 * input - channel to add client to, client to add, channel flags
217 * output -
218 * side effects - user is added to channel
219 */
220void
221add_user_to_channel(struct Channel *chptr, struct Client *client_p, int flags)
222{
223 struct membership *msptr;
224
225 s_assert(client_p->user != NULL);
226 if(client_p->user == NULL)
227 return;
228
6e9b4415 229 msptr = rb_bh_alloc(member_heap);
212380e3 230
231 msptr->chptr = chptr;
232 msptr->client_p = client_p;
233 msptr->flags = flags;
234
af81d5a0
WP
235 rb_dlinkAdd(msptr, &msptr->usernode, &client_p->user->channel);
236 rb_dlinkAdd(msptr, &msptr->channode, &chptr->members);
212380e3 237
238 if(MyClient(client_p))
af81d5a0 239 rb_dlinkAdd(msptr, &msptr->locchannode, &chptr->locmembers);
212380e3 240}
241
242/* remove_user_from_channel()
243 *
244 * input - membership pointer to remove from channel
245 * output -
246 * side effects - membership (thus user) is removed from channel
247 */
248void
249remove_user_from_channel(struct membership *msptr)
250{
251 struct Client *client_p;
252 struct Channel *chptr;
253 s_assert(msptr != NULL);
254 if(msptr == NULL)
255 return;
256
257 client_p = msptr->client_p;
258 chptr = msptr->chptr;
259
af81d5a0
WP
260 rb_dlinkDelete(&msptr->usernode, &client_p->user->channel);
261 rb_dlinkDelete(&msptr->channode, &chptr->members);
212380e3 262
263 if(client_p->servptr == &me)
af81d5a0 264 rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers);
212380e3 265
af81d5a0 266 if(!(chptr->mode.mode & MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0)
212380e3 267 destroy_channel(chptr);
268
6e9b4415 269 rb_bh_free(member_heap, msptr);
212380e3 270
271 return;
272}
273
274/* remove_user_from_channels()
275 *
276 * input - user to remove from all channels
277 * output -
278 * side effects - user is removed from all channels
279 */
280void
281remove_user_from_channels(struct Client *client_p)
282{
283 struct Channel *chptr;
284 struct membership *msptr;
af81d5a0 285 rb_dlink_node *ptr;
90a3c35b 286 rb_dlink_node *next_ptr;
212380e3 287
288 if(client_p == NULL)
289 return;
290
90a3c35b 291 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->user->channel.head)
212380e3 292 {
293 msptr = ptr->data;
294 chptr = msptr->chptr;
295
af81d5a0 296 rb_dlinkDelete(&msptr->channode, &chptr->members);
212380e3 297
298 if(client_p->servptr == &me)
af81d5a0 299 rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers);
212380e3 300
af81d5a0 301 if(!(chptr->mode.mode & MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0)
212380e3 302 destroy_channel(chptr);
303
6e9b4415 304 rb_bh_free(member_heap, msptr);
212380e3 305 }
306
307 client_p->user->channel.head = client_p->user->channel.tail = NULL;
308 client_p->user->channel.length = 0;
309}
310
311/* invalidate_bancache_user()
312 *
313 * input - user to invalidate ban cache for
314 * output -
315 * side effects - ban cache is invalidated for all memberships of that user
316 * to be used after a nick change
317 */
318void
319invalidate_bancache_user(struct Client *client_p)
320{
321 struct membership *msptr;
af81d5a0 322 rb_dlink_node *ptr;
212380e3 323
324 if(client_p == NULL)
325 return;
326
8e69bb4e 327 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
212380e3 328 {
329 msptr = ptr->data;
330 msptr->bants = 0;
331 msptr->flags &= ~CHFL_BANNED;
332 }
333}
334
335/* check_channel_name()
336 *
337 * input - channel name
338 * output - 1 if valid channel name, else 0
339 * side effects -
340 */
341int
342check_channel_name(const char *name)
343{
344 s_assert(name != NULL);
345 if(name == NULL)
346 return 0;
347
348 for (; *name; ++name)
349 {
350 if(!IsChanChar(*name))
351 return 0;
352 }
353
354 return 1;
355}
356
357/* free_channel_list()
358 *
af81d5a0 359 * input - rb_dlink list to free
212380e3 360 * output -
361 * side effects - list of b/e/I modes is cleared
362 */
363void
af81d5a0 364free_channel_list(rb_dlink_list * list)
212380e3 365{
af81d5a0 366 rb_dlink_node *ptr;
90a3c35b 367 rb_dlink_node *next_ptr;
212380e3 368 struct Ban *actualBan;
369
90a3c35b 370 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
212380e3 371 {
372 actualBan = ptr->data;
373 free_ban(actualBan);
374 }
375
376 list->head = list->tail = NULL;
377 list->length = 0;
378}
379
380/* destroy_channel()
381 *
382 * input - channel to destroy
383 * output -
384 * side effects - channel is obliterated
385 */
386void
387destroy_channel(struct Channel *chptr)
388{
90a3c35b 389 rb_dlink_node *ptr, *next_ptr;
212380e3 390
90a3c35b 391 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->invites.head)
212380e3 392 {
393 del_invite(chptr, ptr->data);
394 }
395
396 /* free all bans/exceptions/denies */
397 free_channel_list(&chptr->banlist);
398 free_channel_list(&chptr->exceptlist);
399 free_channel_list(&chptr->invexlist);
fea1ad52 400 free_channel_list(&chptr->quietlist);
212380e3 401
402 /* Free the topic */
403 free_topic(chptr);
404
af81d5a0 405 rb_dlinkDelete(&chptr->node, &global_channel_list);
212380e3 406 del_from_channel_hash(chptr->chname, chptr);
407 free_channel(chptr);
408}
409
410/* channel_pub_or_secret()
411 *
412 * input - channel
413 * output - "=" if public, "@" if secret, else "*"
414 * side effects -
415 */
416static const char *
417channel_pub_or_secret(struct Channel *chptr)
418{
419 if(PubChannel(chptr))
420 return ("=");
421 else if(SecretChannel(chptr))
422 return ("@");
423 return ("*");
424}
425
426/* channel_member_names()
427 *
428 * input - channel to list, client to list to, show endofnames
429 * output -
430 * side effects - client is given list of users on channel
431 */
432void
433channel_member_names(struct Channel *chptr, struct Client *client_p, int show_eon)
434{
435 struct membership *msptr;
436 struct Client *target_p;
af81d5a0 437 rb_dlink_node *ptr;
212380e3 438 char lbuf[BUFSIZE];
439 char *t;
440 int mlen;
441 int tlen;
442 int cur_len;
443 int is_member;
444 int stack = IsCapable(client_p, CLICAP_MULTI_PREFIX);
445
446 if(ShowChannel(client_p, chptr))
447 {
448 is_member = IsMember(client_p, chptr);
449
38e6acdd 450 cur_len = mlen = rb_sprintf(lbuf, form_str(RPL_NAMREPLY),
212380e3 451 me.name, client_p->name,
452 channel_pub_or_secret(chptr), chptr->chname);
453
454 t = lbuf + cur_len;
455
8e69bb4e 456 RB_DLINK_FOREACH(ptr, chptr->members.head)
212380e3 457 {
458 msptr = ptr->data;
459 target_p = msptr->client_p;
460
461 if(IsInvisible(target_p) && !is_member)
462 continue;
463
464 /* space, possible "@+" prefix */
465 if(cur_len + strlen(target_p->name) + 3 >= BUFSIZE - 3)
466 {
467 *(t - 1) = '\0';
468 sendto_one(client_p, "%s", lbuf);
469 cur_len = mlen;
470 t = lbuf + mlen;
471 }
472
38e6acdd 473 tlen = rb_sprintf(t, "%s%s ", find_channel_status(msptr, stack),
212380e3 474 target_p->name);
475
476 cur_len += tlen;
477 t += tlen;
478 }
479
480 /* The old behaviour here was to always output our buffer,
481 * even if there are no clients we can show. This happens
482 * when a client does "NAMES" with no parameters, and all
483 * the clients on a -sp channel are +i. I dont see a good
484 * reason for keeping that behaviour, as it just wastes
485 * bandwidth. --anfl
486 */
487 if(cur_len != mlen)
488 {
489 *(t - 1) = '\0';
490 sendto_one(client_p, "%s", lbuf);
491 }
492 }
493
494 if(show_eon)
495 sendto_one(client_p, form_str(RPL_ENDOFNAMES),
496 me.name, client_p->name, chptr->chname);
497}
498
499/* del_invite()
500 *
501 * input - channel to remove invite from, client to remove
502 * output -
503 * side effects - user is removed from invite list, if exists
504 */
505void
506del_invite(struct Channel *chptr, struct Client *who)
507{
af81d5a0
WP
508 rb_dlinkFindDestroy(who, &chptr->invites);
509 rb_dlinkFindDestroy(chptr, &who->user->invited);
212380e3 510}
511
512/* is_banned()
513 *
514 * input - channel to check bans for, user to check bans against
515 * optional prebuilt buffers
516 * output - 1 if banned, else 0
517 * side effects -
518 */
519int
520is_banned(struct Channel *chptr, struct Client *who, struct membership *msptr,
521 const char *s, const char *s2)
522{
523 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
524 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
525 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
526 char *s3 = NULL;
af81d5a0 527 rb_dlink_node *ptr;
212380e3 528 struct Ban *actualBan = NULL;
529 struct Ban *actualExcept = NULL;
530
531 if(!MyClient(who))
532 return 0;
533
534 /* if the buffers havent been built, do it here */
535 if(s == NULL)
536 {
38e6acdd
WP
537 rb_sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host);
538 rb_sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost);
212380e3 539
540 s = src_host;
541 s2 = src_iphost;
542 }
543 if(who->localClient->mangledhost != NULL)
544 {
545 /* if host mangling mode enabled, also check their real host */
546 if(!strcmp(who->host, who->localClient->mangledhost))
547 {
38e6acdd 548 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost);
212380e3 549 s3 = src_althost;
550 }
551 /* if host mangling mode not enabled and no other spoof,
552 * also check the mangled form of their host */
553 else if (!IsDynSpoof(who))
554 {
38e6acdd 555 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost);
212380e3 556 s3 = src_althost;
557 }
558 }
559
8e69bb4e 560 RB_DLINK_FOREACH(ptr, chptr->banlist.head)
212380e3 561 {
562 actualBan = ptr->data;
563 if(match(actualBan->banstr, s) ||
564 match(actualBan->banstr, s2) ||
565 match_cidr(actualBan->banstr, s2) ||
566 match_extban(actualBan->banstr, who, chptr, CHFL_BAN) ||
567 (s3 != NULL && match(actualBan->banstr, s3)))
568 break;
569 else
570 actualBan = NULL;
571 }
572
573 if((actualBan != NULL) && ConfigChannel.use_except)
574 {
8e69bb4e 575 RB_DLINK_FOREACH(ptr, chptr->exceptlist.head)
212380e3 576 {
577 actualExcept = ptr->data;
578
579 /* theyre exempted.. */
580 if(match(actualExcept->banstr, s) ||
581 match(actualExcept->banstr, s2) ||
582 match_cidr(actualExcept->banstr, s2) ||
583 match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) ||
584 (s3 != NULL && match(actualExcept->banstr, s3)))
585 {
586 /* cache the fact theyre not banned */
587 if(msptr != NULL)
588 {
589 msptr->bants = chptr->bants;
590 msptr->flags &= ~CHFL_BANNED;
591 }
592
593 return CHFL_EXCEPTION;
594 }
595 }
596 }
597
598 /* cache the banned/not banned status */
599 if(msptr != NULL)
600 {
601 msptr->bants = chptr->bants;
602
603 if(actualBan != NULL)
604 {
605 msptr->flags |= CHFL_BANNED;
606 return CHFL_BAN;
607 }
608 else
609 {
610 msptr->flags &= ~CHFL_BANNED;
611 return 0;
612 }
613 }
614
615 return ((actualBan ? CHFL_BAN : 0));
616}
617
618/* is_quieted()
619 *
620 * input - channel to check bans for, user to check bans against
621 * optional prebuilt buffers
622 * output - 1 if banned, else 0
623 * side effects -
624 */
625int
626is_quieted(struct Channel *chptr, struct Client *who, struct membership *msptr,
627 const char *s, const char *s2)
628{
629 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
630 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
631 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
632 char *s3 = NULL;
af81d5a0 633 rb_dlink_node *ptr;
212380e3 634 struct Ban *actualBan = NULL;
635 struct Ban *actualExcept = NULL;
636
637 if(!MyClient(who))
638 return 0;
639
640 /* if the buffers havent been built, do it here */
641 if(s == NULL)
642 {
38e6acdd
WP
643 rb_sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host);
644 rb_sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost);
212380e3 645
646 s = src_host;
647 s2 = src_iphost;
648 }
649 if(who->localClient->mangledhost != NULL)
650 {
651 /* if host mangling mode enabled, also check their real host */
652 if(!strcmp(who->host, who->localClient->mangledhost))
653 {
38e6acdd 654 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost);
212380e3 655 s3 = src_althost;
656 }
657 /* if host mangling mode not enabled and no other spoof,
658 * also check the mangled form of their host */
659 else if (!IsDynSpoof(who))
660 {
38e6acdd 661 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost);
212380e3 662 s3 = src_althost;
663 }
664 }
665
8e69bb4e 666 RB_DLINK_FOREACH(ptr, chptr->quietlist.head)
212380e3 667 {
668 actualBan = ptr->data;
669 if(match(actualBan->banstr, s) ||
670 match(actualBan->banstr, s2) ||
671 match_cidr(actualBan->banstr, s2) ||
672 match_extban(actualBan->banstr, who, chptr, CHFL_QUIET) ||
673 (s3 != NULL && match(actualBan->banstr, s3)))
674 break;
675 else
676 actualBan = NULL;
677 }
678
679 if((actualBan != NULL) && ConfigChannel.use_except)
680 {
8e69bb4e 681 RB_DLINK_FOREACH(ptr, chptr->exceptlist.head)
212380e3 682 {
683 actualExcept = ptr->data;
684
685 /* theyre exempted.. */
686 if(match(actualExcept->banstr, s) ||
687 match(actualExcept->banstr, s2) ||
688 match_cidr(actualExcept->banstr, s2) ||
689 match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) ||
690 (s3 != NULL && match(actualExcept->banstr, s3)))
691 {
692 /* cache the fact theyre not banned */
693 if(msptr != NULL)
694 {
695 msptr->bants = chptr->bants;
696 msptr->flags &= ~CHFL_BANNED;
697 }
698
699 return CHFL_EXCEPTION;
700 }
701 }
702 }
703
704 /* cache the banned/not banned status */
705 if(msptr != NULL)
706 {
707 msptr->bants = chptr->bants;
708
709 if(actualBan != NULL)
710 {
711 msptr->flags |= CHFL_BANNED;
712 return CHFL_BAN;
713 }
714 else
715 {
716 msptr->flags &= ~CHFL_BANNED;
717 return 0;
718 }
719 }
720
721 return ((actualBan ? CHFL_BAN : 0));
722}
723
724/* can_join()
725 *
726 * input - client to check, channel to check for, key
727 * output - reason for not being able to join, else 0
728 * side effects -
729 */
730int
731can_join(struct Client *source_p, struct Channel *chptr, char *key)
732{
af81d5a0
WP
733 rb_dlink_node *invite = NULL;
734 rb_dlink_node *ptr;
212380e3 735 struct Ban *invex = NULL;
736 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
737 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
738 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
739 int use_althost = 0;
1ebf4db4 740 int i = 0;
212380e3 741 hook_data_channel moduledata;
742
743 s_assert(source_p->localClient != NULL);
744
13a467bb
JH
745 if(IsOverride(source_p))
746 return 0;
747
38e6acdd
WP
748 rb_sprintf(src_host, "%s!%s@%s", source_p->name, source_p->username, source_p->host);
749 rb_sprintf(src_iphost, "%s!%s@%s", source_p->name, source_p->username, source_p->sockhost);
212380e3 750 if(source_p->localClient->mangledhost != NULL)
751 {
752 /* if host mangling mode enabled, also check their real host */
753 if(!strcmp(source_p->host, source_p->localClient->mangledhost))
754 {
38e6acdd 755 rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->orighost);
212380e3 756 use_althost = 1;
757 }
758 /* if host mangling mode not enabled and no other spoof,
759 * also check the mangled form of their host */
760 else if (!IsDynSpoof(source_p))
761 {
38e6acdd 762 rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->localClient->mangledhost);
212380e3 763 use_althost = 1;
764 }
765 }
766
767 if((is_banned(chptr, source_p, NULL, src_host, src_iphost)) == CHFL_BAN)
768 return (ERR_BANNEDFROMCHAN);
769
770 if(chptr->mode.mode & MODE_INVITEONLY)
771 {
8e69bb4e 772 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
212380e3 773 {
1ebf4db4 774 if(invite->data == chptr)
212380e3 775 break;
776 }
1ebf4db4 777 if(invite == NULL)
212380e3 778 {
779 if(!ConfigChannel.use_invex)
780 return (ERR_INVITEONLYCHAN);
8e69bb4e 781 RB_DLINK_FOREACH(ptr, chptr->invexlist.head)
212380e3 782 {
783 invex = ptr->data;
784 if(match(invex->banstr, src_host)
785 || match(invex->banstr, src_iphost)
786 || match_cidr(invex->banstr, src_iphost)
787 || match_extban(invex->banstr, source_p, chptr, CHFL_INVEX)
788 || (use_althost && match(invex->banstr, src_althost)))
789 break;
790 }
791 if(ptr == NULL)
792 return (ERR_INVITEONLYCHAN);
793 }
794 }
795
796 if(*chptr->mode.key && (EmptyString(key) || irccmp(chptr->mode.key, key)))
797 return (ERR_BADCHANNELKEY);
798
799 if(chptr->mode.limit &&
af81d5a0 800 rb_dlink_list_length(&chptr->members) >= (unsigned long) chptr->mode.limit)
1ebf4db4 801 i = ERR_CHANNELISFULL;
212380e3 802 if(chptr->mode.mode & MODE_REGONLY && EmptyString(source_p->user->suser))
1ebf4db4 803 i = ERR_NEEDREGGEDNICK;
212380e3 804 /* join throttling stuff --nenolod */
1ebf4db4 805 else if(chptr->mode.join_num > 0 && chptr->mode.join_time > 0)
212380e3 806 {
9f6bbe3c 807 if ((rb_current_time() - chptr->join_delta <=
212380e3 808 chptr->mode.join_time) && (chptr->join_count >=
809 chptr->mode.join_num))
1ebf4db4 810 i = ERR_THROTTLE;
811 }
812
813 /* allow /invite to override +l/+r/+j also -- jilles */
814 if (i != 0 && invite == NULL)
815 {
8e69bb4e 816 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
1ebf4db4 817 {
818 if(invite->data == chptr)
819 break;
820 }
821 if (invite == NULL)
822 return i;
212380e3 823 }
824
825 moduledata.client = source_p;
826 moduledata.chptr = chptr;
827 moduledata.approved = 0;
828
829 call_hook(h_can_join, &moduledata);
830
831 return moduledata.approved;
832}
833
834/* can_send()
835 *
836 * input - user to check in channel, membership pointer
837 * output - whether can explicitly send or not, else CAN_SEND_NONOP
838 * side effects -
839 */
840int
841can_send(struct Channel *chptr, struct Client *source_p, struct membership *msptr)
842{
843 if(IsServer(source_p) || IsService(source_p))
844 return CAN_SEND_OPV;
845
846 if(MyClient(source_p) && hash_find_resv(chptr->chname) &&
847 !IsOper(source_p) && !IsExemptResv(source_p))
848 return CAN_SEND_NO;
849
850 if(msptr == NULL)
851 {
852 msptr = find_channel_membership(chptr, source_p);
853
854 if(msptr == NULL)
855 {
856 /* if its +m or +n and theyre not in the channel,
857 * they cant send. we dont check bans here because
858 * theres no possibility of caching them --fl
859 */
860 if(chptr->mode.mode & MODE_NOPRIVMSGS || chptr->mode.mode & MODE_MODERATED)
861 return CAN_SEND_NO;
862 else
863 return CAN_SEND_NONOP;
864 }
865 }
866
867 if(is_chanop_voiced(msptr))
868 return CAN_SEND_OPV;
869
13a467bb
JH
870 if(IsOverride(source_p))
871 return CAN_SEND_NONOP;
872
212380e3 873 if(chptr->mode.mode & MODE_MODERATED)
874 return CAN_SEND_NO;
875
876 if(MyClient(source_p))
877 {
878 /* cached can_send */
879 if(msptr->bants == chptr->bants)
880 {
881 if(can_send_banned(msptr))
882 return CAN_SEND_NO;
883 }
884 else if(is_banned(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN
885 || is_quieted(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN)
886 return CAN_SEND_NO;
887 }
888
889 return CAN_SEND_NONOP;
890}
891
892/* find_bannickchange_channel()
893 * Input: client to check
894 * Output: channel preventing nick change
895 */
896struct Channel *
897find_bannickchange_channel(struct Client *client_p)
898{
899 struct Channel *chptr;
900 struct membership *msptr;
af81d5a0 901 rb_dlink_node *ptr;
212380e3 902 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
903 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
904
13a467bb 905 if (!MyClient(client_p) || IsOverride(client_p))
212380e3 906 return NULL;
907
38e6acdd
WP
908 rb_sprintf(src_host, "%s!%s@%s", client_p->name, client_p->username, client_p->host);
909 rb_sprintf(src_iphost, "%s!%s@%s", client_p->name, client_p->username, client_p->sockhost);
212380e3 910
8e69bb4e 911 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
212380e3 912 {
913 msptr = ptr->data;
914 chptr = msptr->chptr;
915 if (is_chanop_voiced(msptr))
916 continue;
917 /* cached can_send */
918 if (msptr->bants == chptr->bants)
919 {
920 if (can_send_banned(msptr))
921 return chptr;
922 }
923 else if (is_banned(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN
924 || is_quieted(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN)
925 return chptr;
926 }
927 return NULL;
928}
929
afd4834b
G
930/* find_nonickchange_channel()
931 * Input: client to check
932 * Output: channel preventing nick change
933 */
934struct Channel *
935find_nonickchange_channel(struct Client *client_p)
936{
937 struct Channel *chptr;
938 struct membership *msptr;
939 rb_dlink_node *ptr;
940
941 if (!MyClient(client_p))
942 return NULL;
943
944 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
945 {
946 msptr = ptr->data;
947 chptr = msptr->chptr;
d1c7eccf 948 if (is_any_op(msptr))
afd4834b
G
949 continue;
950 if (chptr->mode.mode & MODE_NONICK)
951 return chptr;
952 }
953 return NULL;
954}
955
212380e3 956/* void check_spambot_warning(struct Client *source_p)
957 * Input: Client to check, channel name or NULL if this is a part.
958 * Output: none
959 * Side-effects: Updates the client's oper_warn_count_down, warns the
960 * IRC operators if necessary, and updates join_leave_countdown as
961 * needed.
962 */
963void
964check_spambot_warning(struct Client *source_p, const char *name)
965{
966 int t_delta;
967 int decrement_count;
968 if((GlobalSetOptions.spam_num &&
969 (source_p->localClient->join_leave_count >= GlobalSetOptions.spam_num)))
970 {
971 if(source_p->localClient->oper_warn_count_down > 0)
972 source_p->localClient->oper_warn_count_down--;
973 else
974 source_p->localClient->oper_warn_count_down = 0;
05d8a68c
JT
975 if(source_p->localClient->oper_warn_count_down == 0 &&
976 name != NULL)
212380e3 977 {
978 /* Its already known as a possible spambot */
05d8a68c
JT
979 sendto_realops_snomask(SNO_BOTS, L_NETWIDE,
980 "User %s (%s@%s) trying to join %s is a possible spambot",
981 source_p->name,
982 source_p->username, source_p->orighost, name);
212380e3 983 source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
984 }
985 }
986 else
987 {
988 if((t_delta =
9f6bbe3c 989 (rb_current_time() - source_p->localClient->last_leave_time)) >
212380e3 990 JOIN_LEAVE_COUNT_EXPIRE_TIME)
991 {
992 decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
b6698246
JT
993 if(name != NULL)
994 ;
995 else if(decrement_count > source_p->localClient->join_leave_count)
212380e3 996 source_p->localClient->join_leave_count = 0;
997 else
998 source_p->localClient->join_leave_count -= decrement_count;
999 }
1000 else
1001 {
9f6bbe3c 1002 if((rb_current_time() -
212380e3 1003 (source_p->localClient->last_join_time)) < GlobalSetOptions.spam_time)
1004 {
1005 /* oh, its a possible spambot */
1006 source_p->localClient->join_leave_count++;
1007 }
1008 }
1009 if(name != NULL)
9f6bbe3c 1010 source_p->localClient->last_join_time = rb_current_time();
212380e3 1011 else
9f6bbe3c 1012 source_p->localClient->last_leave_time = rb_current_time();
212380e3 1013 }
1014}
1015
1016/* check_splitmode()
1017 *
1018 * input -
1019 * output -
1020 * side effects - compares usercount and servercount against their split
1021 * values and adjusts splitmode accordingly
1022 */
1023void
1024check_splitmode(void *unused)
1025{
1026 if(splitchecking && (ConfigChannel.no_join_on_split || ConfigChannel.no_create_on_split))
1027 {
1028 /* not split, we're being asked to check now because someone
1029 * has left
1030 */
1031 if(!splitmode)
1032 {
1033 if(eob_count < split_servers || Count.total < split_users)
1034 {
1035 splitmode = 1;
1036 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1037 "Network split, activating splitmode");
dd9be678 1038 check_splitmode_ev = rb_event_addish("check_splitmode", check_splitmode, NULL, 2);
212380e3 1039 }
1040 }
1041 /* in splitmode, check whether its finished */
1042 else if(eob_count >= split_servers && Count.total >= split_users)
1043 {
1044 splitmode = 0;
1045
1046 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1047 "Network rejoined, deactivating splitmode");
1048
dd9be678 1049 rb_event_delete(check_splitmode_ev);
0ae330b4 1050 check_splitmode_ev = NULL;
212380e3 1051 }
1052 }
1053}
1054
1055
1056/* allocate_topic()
1057 *
1058 * input - channel to allocate topic for
1059 * output - 1 on success, else 0
1060 * side effects - channel gets a topic allocated
1061 */
1062static void
1063allocate_topic(struct Channel *chptr)
1064{
1065 void *ptr;
1066
1067 if(chptr == NULL)
1068 return;
1069
6e9b4415 1070 ptr = rb_bh_alloc(topic_heap);
212380e3 1071
1072 /* Basically we allocate one large block for the topic and
1073 * the topic info. We then split it up into two and shove it
1074 * in the chptr
1075 */
1076 chptr->topic = ptr;
1077 chptr->topic_info = (char *) ptr + TOPICLEN + 1;
1078 *chptr->topic = '\0';
1079 *chptr->topic_info = '\0';
1080}
1081
1082/* free_topic()
1083 *
1084 * input - channel which has topic to free
1085 * output -
1086 * side effects - channels topic is free'd
1087 */
1088static void
1089free_topic(struct Channel *chptr)
1090{
1091 void *ptr;
1092
1093 if(chptr == NULL || chptr->topic == NULL)
1094 return;
1095
1096 /* This is safe for now - If you change allocate_topic you
1097 * MUST change this as well
1098 */
1099 ptr = chptr->topic;
6e9b4415 1100 rb_bh_free(topic_heap, ptr);
212380e3 1101 chptr->topic = NULL;
1102 chptr->topic_info = NULL;
1103}
1104
1105/* set_channel_topic()
1106 *
1107 * input - channel, topic to set, topic info and topic ts
1108 * output -
1109 * side effects - channels topic, topic info and TS are set.
1110 */
1111void
1112set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_info, time_t topicts)
1113{
1114 if(strlen(topic) > 0)
1115 {
1116 if(chptr->topic == NULL)
1117 allocate_topic(chptr);
907468c4
VY
1118 rb_strlcpy(chptr->topic, topic, TOPICLEN + 1);
1119 rb_strlcpy(chptr->topic_info, topic_info, USERHOST_REPLYLEN);
212380e3 1120 chptr->topic_time = topicts;
1121 }
1122 else
1123 {
1124 if(chptr->topic != NULL)
1125 free_topic(chptr);
1126 chptr->topic_time = 0;
1127 }
1128}
1129
c279d43b
G
1130/* has_common_channel()
1131 *
1132 * input - pointer to client
1133 * - pointer to another client
1134 * output - 1 if the two have a channel in common, 0 elsewise
1135 * side effects - none
1136 */
1137int
1138has_common_channel(struct Client *client1, struct Client *client2)
1139{
1140 rb_dlink_node *ptr;
1141
1142 RB_DLINK_FOREACH(ptr, client1->user->channel.head)
1143 {
1144 if(IsMember(client2, ((struct membership *)ptr->data)->chptr))
1145 return 1;
1146 }
99c78094 1147 return 0;
c279d43b
G
1148}
1149
212380e3 1150/* channel_modes()
1151 *
1152 * inputs - pointer to channel
1153 * - pointer to client
f1e35c19 1154 * output - string with simple modes
1155 * side effects - result from previous calls overwritten
212380e3 1156 *
1157 * Stolen from ShadowIRCd 4 --nenolod
1158 */
1159const char *
1160channel_modes(struct Channel *chptr, struct Client *client_p)
1161{
1162 int i;
1163 char buf1[BUFSIZE];
1164 char buf2[BUFSIZE];
1165 static char final[BUFSIZE];
1166 char *mbuf = buf1;
1167 char *pbuf = buf2;
1168
1169 *mbuf++ = '+';
1170 *pbuf = '\0';
1171
75818939
VY
1172 for (i = 0; i < 256; i++)
1173 if(chptr->mode.mode & chmode_flags[i])
1174 *mbuf++ = i;
212380e3 1175
1176 if(chptr->mode.limit)
1177 {
1178 *mbuf++ = 'l';
1179
f1e35c19 1180 if(!IsClient(client_p) || IsMember(client_p, chptr))
38e6acdd 1181 pbuf += rb_sprintf(pbuf, " %d", chptr->mode.limit);
212380e3 1182 }
1183
1184 if(*chptr->mode.key)
1185 {
1186 *mbuf++ = 'k';
1187
f1e35c19 1188 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
38e6acdd 1189 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.key);
212380e3 1190 }
1191
1192 if(chptr->mode.join_num)
1193 {
1194 *mbuf++ = 'j';
1195
f1e35c19 1196 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
38e6acdd 1197 pbuf += rb_sprintf(pbuf, " %d:%d", chptr->mode.join_num,
212380e3 1198 chptr->mode.join_time);
1199 }
1200
f1e35c19 1201 if(*chptr->mode.forward && (ConfigChannel.use_forward || !IsClient(client_p)))
212380e3 1202 {
1203 *mbuf++ = 'f';
1204
f1e35c19 1205 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
38e6acdd 1206 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.forward);
212380e3 1207 }
1208
1209 *mbuf = '\0';
1210
907468c4 1211 rb_strlcpy(final, buf1, sizeof final);
a64c5173 1212 rb_strlcat(final, buf2, sizeof final);
212380e3 1213 return final;
1214}
1215
1216/* Now lets do some stuff to keep track of what combinations of
1217 * servers exist...
1218 * Note that the number of combinations doubles each time you add
1219 * something to this list. Each one is only quick if no servers use that
1220 * combination, but if the numbers get too high here MODE will get too
1221 * slow. I suggest if you get more than 7 here, you consider getting rid
1222 * of some and merging or something. If it wasn't for irc+cs we would
1223 * probably not even need to bother about most of these, but unfortunately
1224 * we do. -A1kmm
1225 */
1226
1227/* void init_chcap_usage_counts(void)
1228 *
1229 * Inputs - none
1230 * Output - none
1231 * Side-effects - Initialises the usage counts to zero. Fills in the
1232 * chcap_yes and chcap_no combination tables.
1233 */
1234void
1235init_chcap_usage_counts(void)
1236{
1237 unsigned long m, c, y, n;
1238
1239 memset(chcap_combos, 0, sizeof(chcap_combos));
1240
1241 /* For every possible combination */
1242 for (m = 0; m < NCHCAP_COMBOS; m++)
1243 {
1244 /* Check each capab */
1245 for (c = y = n = 0; c < NCHCAPS; c++)
1246 {
1247 if((m & (1 << c)) == 0)
1248 n |= channel_capabs[c];
1249 else
1250 y |= channel_capabs[c];
1251 }
1252 chcap_combos[m].cap_yes = y;
1253 chcap_combos[m].cap_no = n;
1254 }
1255}
1256
1257/* void set_chcap_usage_counts(struct Client *serv_p)
1258 * Input: serv_p; The client whose capabs to register.
1259 * Output: none
1260 * Side-effects: Increments the usage counts for the correct capab
1261 * combination.
1262 */
1263void
1264set_chcap_usage_counts(struct Client *serv_p)
1265{
1266 int n;
1267
1268 for (n = 0; n < NCHCAP_COMBOS; n++)
1269 {
1270 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1271 NotCapable(serv_p, chcap_combos[n].cap_no))
1272 {
1273 chcap_combos[n].count++;
1274 return;
1275 }
1276 }
1277
1278 /* This should be impossible -A1kmm. */
1279 s_assert(0);
1280}
1281
1282/* void set_chcap_usage_counts(struct Client *serv_p)
1283 *
1284 * Inputs - serv_p; The client whose capabs to register.
1285 * Output - none
1286 * Side-effects - Decrements the usage counts for the correct capab
1287 * combination.
1288 */
1289void
1290unset_chcap_usage_counts(struct Client *serv_p)
1291{
1292 int n;
1293
1294 for (n = 0; n < NCHCAP_COMBOS; n++)
1295 {
1296 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1297 NotCapable(serv_p, chcap_combos[n].cap_no))
1298 {
1299 /* Hopefully capabs can't change dynamically or anything... */
1300 s_assert(chcap_combos[n].count > 0);
1301
1302 if(chcap_combos[n].count > 0)
1303 chcap_combos[n].count--;
1304 return;
1305 }
1306 }
1307
1308 /* This should be impossible -A1kmm. */
1309 s_assert(0);
1310}
1311
1312/* void send_cap_mode_changes(struct Client *client_p,
1313 * struct Client *source_p,
1314 * struct Channel *chptr, int cap, int nocap)
1315 * Input: The client sending(client_p), the source client(source_p),
1316 * the channel to send mode changes for(chptr)
1317 * Output: None.
1318 * Side-effects: Sends the appropriate mode changes to capable servers.
1319 *
1320 * Reverted back to my original design, except that we now keep a count
1321 * of the number of servers which each combination as an optimisation, so
1322 * the capabs combinations which are not needed are not worked out. -A1kmm
1323 */
1324void
1325send_cap_mode_changes(struct Client *client_p, struct Client *source_p,
1326 struct Channel *chptr, struct ChModeChange mode_changes[], int mode_count)
1327{
1328 static char modebuf[BUFSIZE];
1329 static char parabuf[BUFSIZE];
1330 int i, mbl, pbl, nc, mc, preflen, len;
1331 char *pbuf;
1332 const char *arg;
1333 int dir;
1334 int j;
1335 int cap;
1336 int nocap;
1337 int arglen;
1338
1339 /* Now send to servers... */
1340 for (j = 0; j < NCHCAP_COMBOS; j++)
1341 {
1342 if(chcap_combos[j].count == 0)
1343 continue;
1344
1345 mc = 0;
1346 nc = 0;
1347 pbl = 0;
1348 parabuf[0] = 0;
1349 pbuf = parabuf;
1350 dir = MODE_QUERY;
1351
1352 cap = chcap_combos[j].cap_yes;
1353 nocap = chcap_combos[j].cap_no;
1354
9f9b4d7b
WP
1355 mbl = preflen = rb_sprintf(modebuf, ":%s TMODE %ld %s ",
1356 use_id(source_p), (long) chptr->channelts,
1357 chptr->chname);
212380e3 1358
1359 /* loop the list of - modes we have */
1360 for (i = 0; i < mode_count; i++)
1361 {
1362 /* if they dont support the cap we need, or they do support a cap they
1363 * cant have, then dont add it to the modebuf.. that way they wont see
1364 * the mode
1365 */
1366 if((mode_changes[i].letter == 0) ||
1367 ((cap & mode_changes[i].caps) != mode_changes[i].caps)
1368 || ((nocap & mode_changes[i].nocaps) != mode_changes[i].nocaps))
1369 continue;
1370
7592f950
JT
1371 if(!EmptyString(mode_changes[i].id))
1372 arg = mode_changes[i].id;
1373 else
1374 arg = mode_changes[i].arg;
212380e3 1375
1376 if(arg)
1377 {
1378 arglen = strlen(arg);
1379
1380 /* dont even think about it! --fl */
1381 if(arglen > MODEBUFLEN - 5)
1382 continue;
1383 }
1384
1385 /* if we're creeping past the buf size, we need to send it and make
1386 * another line for the other modes
1387 * XXX - this could give away server topology with uids being
1388 * different lengths, but not much we can do, except possibly break
1389 * them as if they were the longest of the nick or uid at all times,
1390 * which even then won't work as we don't always know the uid -A1kmm.
1391 */
1392 if(arg && ((mc == MAXMODEPARAMSSERV) ||
1393 ((mbl + pbl + arglen + 4) > (BUFSIZE - 3))))
1394 {
1395 if(nc != 0)
1396 sendto_server(client_p, chptr, cap, nocap,
1397 "%s %s", modebuf, parabuf);
1398 nc = 0;
1399 mc = 0;
1400
1401 mbl = preflen;
1402 pbl = 0;
1403 pbuf = parabuf;
1404 parabuf[0] = 0;
1405 dir = MODE_QUERY;
1406 }
1407
1408 if(dir != mode_changes[i].dir)
1409 {
1410 modebuf[mbl++] = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
1411 dir = mode_changes[i].dir;
1412 }
1413
1414 modebuf[mbl++] = mode_changes[i].letter;
1415 modebuf[mbl] = 0;
1416 nc++;
1417
1418 if(arg != NULL)
1419 {
38e6acdd 1420 len = rb_sprintf(pbuf, "%s ", arg);
212380e3 1421 pbuf += len;
1422 pbl += len;
1423 mc++;
1424 }
1425 }
1426
1427 if(pbl && parabuf[pbl - 1] == ' ')
1428 parabuf[pbl - 1] = 0;
1429
1430 if(nc != 0)
1431 sendto_server(client_p, chptr, cap, nocap, "%s %s", modebuf, parabuf);
1432 }
1433}
080bb5cf
JH
1434
1435/* Check what we will forward to, without sending any notices to the user
1436 * -- jilles
1437 */
1438struct Channel *
1439check_forward(struct Client *source_p, struct Channel *chptr,
1440 char *key)
1441{
1442 int depth = 0, i;
1443
1444 /* User is +Q */
1445 if (IsNoForward(source_p))
1446 return NULL;
1447
1448 while (depth < 16)
1449 {
1450 chptr = find_channel(chptr->mode.forward);
1451 /* Can only forward to existing channels */
1452 if (chptr == NULL)
1453 return NULL;
1454 /* Already on there, show original error message */
1455 if (IsMember(source_p, chptr))
1456 return NULL;
1457 /* Juped. Sending a warning notice would be unfair */
1458 if (hash_find_resv(chptr->chname))
1459 return NULL;
1460 /* Don't forward to +Q channel */
1461 if (chptr->mode.mode & MODE_DISFORWARD)
1462 return NULL;
1463 i = can_join(source_p, chptr, key);
1464 if (i == 0)
1465 return chptr;
1466 if (i != ERR_INVITEONLYCHAN && i != ERR_NEEDREGGEDNICK && i != ERR_THROTTLE && i != ERR_CHANNELISFULL)
1467 return NULL;
1468 depth++;
1469 }
1470
1471 return NULL;
1472}
67b90240 1473
9230426e
JH
1474/*
1475 * do_join_0
1476 *
1477 * inputs - pointer to client doing join 0
1478 * output - NONE
1479 * side effects - Use has decided to join 0. This is legacy
1480 * from the days when channels were numbers not names. *sigh*
9230426e
JH
1481 */
1482void
1483do_join_0(struct Client *client_p, struct Client *source_p)
1484{
1485 struct membership *msptr;
1486 struct Channel *chptr = NULL;
1487 rb_dlink_node *ptr;
1488
1489 /* Finish the flood grace period... */
1490 if(MyClient(source_p) && !IsFloodDone(source_p))
1491 flood_endgrace(source_p);
1492
1493 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s JOIN 0", use_id(source_p));
1494
aa35afbb
JH
1495 while((ptr = source_p->user->channel.head))
1496 {
9230426e
JH
1497 if(source_p->user->channel.head && MyConnect(source_p) &&
1498 !IsOper(source_p) && !IsExemptSpambot(source_p))
1499 check_spambot_warning(source_p, NULL);
1500
9230426e
JH
1501 msptr = ptr->data;
1502 chptr = msptr->chptr;
1503 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s",
1504 source_p->name,
1505 source_p->username, source_p->host, chptr->chname);
1506 remove_user_from_channel(msptr);
1507 }
1508}
1509
1510int
1511check_channel_name_loc(struct Client *source_p, const char *name)
1512{
aa35afbb
JH
1513 const char *p;
1514
9230426e
JH
1515 s_assert(name != NULL);
1516 if(EmptyString(name))
1517 return 0;
1518
1519 if(ConfigFileEntry.disable_fake_channels && !IsOper(source_p))
1520 {
aa35afbb 1521 for(p = name; *p; ++p)
9230426e 1522 {
aa35afbb 1523 if(!IsChanChar(*p) || IsFakeChanChar(*p))
9230426e
JH
1524 return 0;
1525 }
1526 }
1527 else
1528 {
aa35afbb 1529 for(p = name; *p; ++p)
9230426e 1530 {
aa35afbb 1531 if(!IsChanChar(*p))
9230426e
JH
1532 return 0;
1533 }
1534 }
1535
aa35afbb
JH
1536 if(ConfigChannel.only_ascii_channels)
1537 {
1538 for(p = name; *p; ++p)
1539 if(*p < 33 || *p > 126)
1540 return 0;
1541 }
1542
1543
9230426e
JH
1544 return 1;
1545}
1546
824455ab 1547void user_join(struct Client * client_p, struct Client * source_p, const char * channels, const char * keys)
67b90240
JH
1548{
1549 static char jbuf[BUFSIZE];
1550 struct Channel *chptr = NULL;
1551 struct ConfItem *aconf;
1552 char *name;
1553 char *key = NULL;
1554 const char *modes;
1555 int i, flags = 0;
1556 char *p = NULL, *p2 = NULL;
1557 char *chanlist;
1558 char *mykey;
67b90240
JH
1559
1560 jbuf[0] = '\0';
1561
1562 if(channels == NULL)
1563 return;
1564
1565 /* rebuild the list of channels theyre supposed to be joining.
1566 * this code has a side effect of losing keys, but..
1567 */
1568 chanlist = LOCAL_COPY(channels);
1569 for(name = rb_strtok_r(chanlist, ",", &p); name; name = rb_strtok_r(NULL, ",", &p))
1570 {
1571 /* check the length and name of channel is ok */
1572 if(!check_channel_name_loc(source_p, name) || (strlen(name) > LOC_CHANNELLEN))
1573 {
1574 sendto_one_numeric(source_p, ERR_BADCHANNAME,
1575 form_str(ERR_BADCHANNAME), (unsigned char *) name);
1576 continue;
1577 }
1578
1579 /* join 0 parts all channels */
1580 if(*name == '0' && (name[1] == ',' || name[1] == '\0') && name == chanlist)
1581 {
1582 (void) strcpy(jbuf, "0");
1583 continue;
1584 }
1585
1586 /* check it begins with # or &, and local chans are disabled */
1587 else if(!IsChannelName(name))
1588 {
1589 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
1590 form_str(ERR_NOSUCHCHANNEL), name);
1591 continue;
1592 }
1593
1594 /* see if its resv'd */
1595 if(!IsExemptResv(source_p) && (aconf = hash_find_resv(name)))
1596 {
1597 sendto_one_numeric(source_p, ERR_BADCHANNAME,
1598 form_str(ERR_BADCHANNAME), name);
1599
1600 /* dont warn for opers */
1601 if(!IsExemptJupe(source_p) && !IsOper(source_p))
1602 sendto_realops_snomask(SNO_SPY, L_NETWIDE,
1603 "User %s (%s@%s) is attempting to join locally juped channel %s (%s)",
1604 source_p->name, source_p->username,
1605 source_p->orighost, name, aconf->passwd);
1606 /* dont update tracking for jupe exempt users, these
1607 * are likely to be spamtrap leaves
1608 */
1609 else if(IsExemptJupe(source_p))
1610 aconf->port--;
1611
1612 continue;
1613 }
1614
1615 if(splitmode && !IsOper(source_p) && (*name != '&') &&
1616 ConfigChannel.no_join_on_split)
1617 {
1618 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1619 me.name, source_p->name, name);
1620 continue;
1621 }
1622
1623 if(*jbuf)
1624 (void) strcat(jbuf, ",");
1625 (void) rb_strlcat(jbuf, name, sizeof(jbuf));
1626 }
1627
1628 if(keys != NULL)
1629 {
1630 mykey = LOCAL_COPY(keys);
1631 key = rb_strtok_r(mykey, ",", &p2);
1632 }
1633
1634 for(name = rb_strtok_r(jbuf, ",", &p); name;
1635 key = (key) ? rb_strtok_r(NULL, ",", &p2) : NULL, name = rb_strtok_r(NULL, ",", &p))
1636 {
1637 hook_data_channel_activity hook_info;
1638
1639 /* JOIN 0 simply parts all channels the user is in */
1640 if(*name == '0' && !atoi(name))
1641 {
1642 if(source_p->user->channel.head == NULL)
1643 continue;
1644
1645 do_join_0(&me, source_p);
1646 continue;
1647 }
1648
1649 /* look for the channel */
1650 if((chptr = find_channel(name)) != NULL)
1651 {
1652 if(IsMember(source_p, chptr))
1653 continue;
1654
1655 flags = 0;
1656 }
1657 else
1658 {
1659 hook_data_client_approval moduledata;
1660
1661 moduledata.client = source_p;
1662 moduledata.approved = 0;
1663
1664 call_hook(h_can_create_channel, &moduledata);
1665
1666 if(moduledata.approved != 0)
1667 {
1668 sendto_one(source_p, form_str(moduledata.approved),
1669 me.name, source_p->name, name);
1670 continue;
1671 }
1672
1673 if(splitmode && !IsOper(source_p) && (*name != '&') &&
1674 ConfigChannel.no_create_on_split)
1675 {
1676 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1677 me.name, source_p->name, name);
1678 continue;
1679 }
1680
1681 flags = CHFL_CHANOP;
1682 }
1683
1684 if((rb_dlink_list_length(&source_p->user->channel) >=
1685 (unsigned long) ConfigChannel.max_chans_per_user) &&
1686 (!IsOper(source_p) ||
1687 (rb_dlink_list_length(&source_p->user->channel) >=
1688 (unsigned long) ConfigChannel.max_chans_per_user * 3)))
1689 {
1690 sendto_one(source_p, form_str(ERR_TOOMANYCHANNELS),
1691 me.name, source_p->name, name);
67b90240
JH
1692 return;
1693 }
1694
67b90240
JH
1695 if(chptr == NULL) /* If I already have a chptr, no point doing this */
1696 {
1697 chptr = get_or_create_channel(source_p, name, NULL);
1698
1699 if(chptr == NULL)
1700 {
1701 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1702 me.name, source_p->name, name);
67b90240
JH
1703 continue;
1704 }
1705 }
1706
67b90240
JH
1707 /* can_join checks for +i key, bans etc */
1708 if((i = can_join(source_p, chptr, key)))
1709 {
1710 if ((i != ERR_NEEDREGGEDNICK && i != ERR_THROTTLE && i != ERR_INVITEONLYCHAN && i != ERR_CHANNELISFULL) ||
1711 (!ConfigChannel.use_forward || (chptr = check_forward(source_p, chptr, key)) == NULL))
1712 {
1713 /* might be wrong, but is there any other better location for such?
1714 * see extensions/chm_operonly.c for other comments on this
1715 * -- dwr
1716 */
1717 if(i != ERR_CUSTOM)
1718 sendto_one(source_p, form_str(i), me.name, source_p->name, name);
1719
67b90240
JH
1720 continue;
1721 }
1722
1723 sendto_one_numeric(source_p, ERR_LINKCHANNEL, form_str(ERR_LINKCHANNEL), name, chptr->chname);
1724 }
aa35afbb
JH
1725
1726 if(flags == 0 &&
1727 !IsOper(source_p) && !IsExemptSpambot(source_p))
1728 check_spambot_warning(source_p, name);
67b90240
JH
1729
1730 /* add the user to the channel */
1731 add_user_to_channel(chptr, source_p, flags);
1732 if (chptr->mode.join_num &&
1733 rb_current_time() - chptr->join_delta >= chptr->mode.join_time)
1734 {
1735 chptr->join_count = 0;
1736 chptr->join_delta = rb_current_time();
1737 }
1738 chptr->join_count++;
1739
1740 /* we send the user their join here, because we could have to
1741 * send a mode out next.
1742 */
1743 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s JOIN :%s",
1744 source_p->name,
1745 source_p->username, source_p->host, chptr->chname);
1746
1747 /* its a new channel, set +nt and burst. */
1748 if(flags & CHFL_CHANOP)
1749 {
1750 chptr->channelts = rb_current_time();
13ec57db
JH
1751
1752 /* autochanmodes stuff */
1753 if(ConfigChannel.autochanmodes)
1754 {
1755 char * ch;
1756 for(ch = ConfigChannel.autochanmodes; *ch; *ch++)
1757 {
1758 chptr->mode.mode |= chmode_table[*ch].mode_type;
1759 }
1760 }
1761 else
1762 {
1763 chptr->mode.mode |= MODE_TOPICLIMIT;
1764 chptr->mode.mode |= MODE_NOPRIVMSGS;
1765 }
1766
67b90240
JH
1767 modes = channel_modes(chptr, &me);
1768
1769 sendto_channel_local(ONLY_CHANOPS, chptr, ":%s MODE %s %s",
1770 me.name, chptr->chname, modes);
1771
1772 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
1773 ":%s SJOIN %ld %s %s :@%s",
1774 me.id, (long) chptr->channelts,
1775 chptr->chname, modes, source_p->id);
1776 }
1777 else
1778 {
1779 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
1780 ":%s JOIN %ld %s +",
1781 use_id(source_p), (long) chptr->channelts,
1782 chptr->chname);
1783 }
1784
1785 del_invite(chptr, source_p);
1786
1787 if(chptr->topic != NULL)
1788 {
1789 sendto_one(source_p, form_str(RPL_TOPIC), me.name,
1790 source_p->name, chptr->chname, chptr->topic);
1791
1792 sendto_one(source_p, form_str(RPL_TOPICWHOTIME),
1793 me.name, source_p->name, chptr->chname,
1794 chptr->topic_info, chptr->topic_time);
1795 }
1796
1797 channel_member_names(chptr, source_p, 1);
1798
67b90240
JH
1799 hook_info.client = source_p;
1800 hook_info.chptr = chptr;
1801 hook_info.key = key;
1802 call_hook(h_channel_join, &hook_info);
1803 }
1804
1805 return;
1806}