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