]> jfr.im git - irc/rqf/shadowircd.git/blame - src/channel.c
Make channel_metadata_time_add accept a regular value as well as a time value.
[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
b8643345
G
189 if(is_owner(msptr))
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
236/* is_owner()
237 *
238 * input - membership to check for owner
239 * output - 1 if the user is an owner, 0 if the user is not or owner
240 * is disabled.
241 * side effects -
242 *
243 */
244int
245is_owner(struct membership *msptr)
246{
40c6b59b
G
247 if(!ConfigChannel.use_owner)
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
258 * output - 1 if the user is op, halfop, or owner, 0 elsewise
259 * side effects -
260 */
d1c7eccf
G
261int
262is_any_op(struct membership *msptr)
263{
40c6b59b 264 if(is_chanop(msptr) || is_halfop(msptr) || is_owner(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
273 * output - 1 if the user is op, halfop, owner, or voice, 0 elsewise
274 * side effects -
275 */
276int
277is_chanop_voiced(struct membership *msptr)
278{
40c6b59b 279 if(is_chanop(msptr) || is_voiced(msptr) || is_halfop(msptr) || is_owner(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{
40c6b59b 294 if(is_chanop(source) && !is_owner(target))
b3b2ed97 295 return 1;
40c6b59b
G
296 else if(is_halfop(source) && !is_any_op(target))
297 return 1;
298 else if(is_owner(source))
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];
829 int use_althost = 0;
1ebf4db4 830 int i = 0;
212380e3 831 hook_data_channel moduledata;
0b370fcc 832 struct Metadata *md;
104becbf
G
833 struct DictionaryIter iter;
834 char *text = rb_strdup("");
212380e3 835
836 s_assert(source_p->localClient != NULL);
837
13a467bb
JH
838 if(IsOverride(source_p))
839 return 0;
840
38e6acdd
WP
841 rb_sprintf(src_host, "%s!%s@%s", source_p->name, source_p->username, source_p->host);
842 rb_sprintf(src_iphost, "%s!%s@%s", source_p->name, source_p->username, source_p->sockhost);
212380e3 843 if(source_p->localClient->mangledhost != NULL)
844 {
845 /* if host mangling mode enabled, also check their real host */
846 if(!strcmp(source_p->host, source_p->localClient->mangledhost))
847 {
38e6acdd 848 rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->orighost);
212380e3 849 use_althost = 1;
850 }
851 /* if host mangling mode not enabled and no other spoof,
852 * also check the mangled form of their host */
853 else if (!IsDynSpoof(source_p))
854 {
38e6acdd 855 rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->localClient->mangledhost);
212380e3 856 use_althost = 1;
857 }
858 }
859
860 if((is_banned(chptr, source_p, NULL, src_host, src_iphost)) == CHFL_BAN)
861 return (ERR_BANNEDFROMCHAN);
862
104becbf
G
863 rb_sprintf(text, "K%s",source_p->name);
864
865 if(md = channel_metadata_find(chptr, text))
866 {
867 if(md->timevalue + ConfigChannel.kick_no_rejoin_time > rb_current_time())
868 {
869 return ERR_KICKNOREJOIN;
870 }
871 /* cleanup the channel's kicknorejoin metadata. */
0b370fcc 872 DICTIONARY_FOREACH(md, &iter, chptr->metadata)
104becbf
G
873 {
874 text = rb_strdup(md->name);
875 if((text[0] == 'K') && (md->timevalue + ConfigChannel.kick_no_rejoin_time > rb_current_time()))
876 channel_metadata_delete(chptr, md->name, 1);
877 }
878
879 }
880
212380e3 881 if(chptr->mode.mode & MODE_INVITEONLY)
882 {
8e69bb4e 883 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
212380e3 884 {
1ebf4db4 885 if(invite->data == chptr)
212380e3 886 break;
887 }
1ebf4db4 888 if(invite == NULL)
212380e3 889 {
890 if(!ConfigChannel.use_invex)
891 return (ERR_INVITEONLYCHAN);
8e69bb4e 892 RB_DLINK_FOREACH(ptr, chptr->invexlist.head)
212380e3 893 {
894 invex = ptr->data;
895 if(match(invex->banstr, src_host)
896 || match(invex->banstr, src_iphost)
897 || match_cidr(invex->banstr, src_iphost)
898 || match_extban(invex->banstr, source_p, chptr, CHFL_INVEX)
899 || (use_althost && match(invex->banstr, src_althost)))
900 break;
901 }
902 if(ptr == NULL)
903 return (ERR_INVITEONLYCHAN);
904 }
905 }
906
907 if(*chptr->mode.key && (EmptyString(key) || irccmp(chptr->mode.key, key)))
908 return (ERR_BADCHANNELKEY);
909
910 if(chptr->mode.limit &&
af81d5a0 911 rb_dlink_list_length(&chptr->members) >= (unsigned long) chptr->mode.limit)
1ebf4db4 912 i = ERR_CHANNELISFULL;
212380e3 913 if(chptr->mode.mode & MODE_REGONLY && EmptyString(source_p->user->suser))
1ebf4db4 914 i = ERR_NEEDREGGEDNICK;
212380e3 915 /* join throttling stuff --nenolod */
1ebf4db4 916 else if(chptr->mode.join_num > 0 && chptr->mode.join_time > 0)
212380e3 917 {
9f6bbe3c 918 if ((rb_current_time() - chptr->join_delta <=
212380e3 919 chptr->mode.join_time) && (chptr->join_count >=
920 chptr->mode.join_num))
1ebf4db4 921 i = ERR_THROTTLE;
922 }
923
924 /* allow /invite to override +l/+r/+j also -- jilles */
925 if (i != 0 && invite == NULL)
926 {
8e69bb4e 927 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
1ebf4db4 928 {
929 if(invite->data == chptr)
930 break;
931 }
932 if (invite == NULL)
933 return i;
212380e3 934 }
935
936 moduledata.client = source_p;
937 moduledata.chptr = chptr;
938 moduledata.approved = 0;
939
940 call_hook(h_can_join, &moduledata);
941
942 return moduledata.approved;
943}
944
945/* can_send()
946 *
947 * input - user to check in channel, membership pointer
948 * output - whether can explicitly send or not, else CAN_SEND_NONOP
949 * side effects -
950 */
951int
952can_send(struct Channel *chptr, struct Client *source_p, struct membership *msptr)
953{
954 if(IsServer(source_p) || IsService(source_p))
955 return CAN_SEND_OPV;
956
957 if(MyClient(source_p) && hash_find_resv(chptr->chname) &&
958 !IsOper(source_p) && !IsExemptResv(source_p))
959 return CAN_SEND_NO;
960
961 if(msptr == NULL)
962 {
963 msptr = find_channel_membership(chptr, source_p);
964
965 if(msptr == NULL)
966 {
967 /* if its +m or +n and theyre not in the channel,
968 * they cant send. we dont check bans here because
969 * theres no possibility of caching them --fl
970 */
971 if(chptr->mode.mode & MODE_NOPRIVMSGS || chptr->mode.mode & MODE_MODERATED)
972 return CAN_SEND_NO;
973 else
974 return CAN_SEND_NONOP;
975 }
976 }
977
978 if(is_chanop_voiced(msptr))
979 return CAN_SEND_OPV;
980
13a467bb
JH
981 if(IsOverride(source_p))
982 return CAN_SEND_NONOP;
983
212380e3 984 if(chptr->mode.mode & MODE_MODERATED)
985 return CAN_SEND_NO;
986
987 if(MyClient(source_p))
988 {
989 /* cached can_send */
990 if(msptr->bants == chptr->bants)
991 {
992 if(can_send_banned(msptr))
993 return CAN_SEND_NO;
994 }
995 else if(is_banned(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN
996 || is_quieted(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN)
997 return CAN_SEND_NO;
998 }
999
1000 return CAN_SEND_NONOP;
1001}
1002
1003/* find_bannickchange_channel()
1004 * Input: client to check
1005 * Output: channel preventing nick change
1006 */
1007struct Channel *
1008find_bannickchange_channel(struct Client *client_p)
1009{
1010 struct Channel *chptr;
1011 struct membership *msptr;
af81d5a0 1012 rb_dlink_node *ptr;
212380e3 1013 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
1014 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
1015
13a467bb 1016 if (!MyClient(client_p) || IsOverride(client_p))
212380e3 1017 return NULL;
1018
38e6acdd
WP
1019 rb_sprintf(src_host, "%s!%s@%s", client_p->name, client_p->username, client_p->host);
1020 rb_sprintf(src_iphost, "%s!%s@%s", client_p->name, client_p->username, client_p->sockhost);
212380e3 1021
8e69bb4e 1022 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
212380e3 1023 {
1024 msptr = ptr->data;
1025 chptr = msptr->chptr;
1026 if (is_chanop_voiced(msptr))
1027 continue;
1028 /* cached can_send */
1029 if (msptr->bants == chptr->bants)
1030 {
1031 if (can_send_banned(msptr))
1032 return chptr;
1033 }
1034 else if (is_banned(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN
1035 || is_quieted(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN)
1036 return chptr;
1037 }
1038 return NULL;
1039}
1040
afd4834b
G
1041/* find_nonickchange_channel()
1042 * Input: client to check
1043 * Output: channel preventing nick change
1044 */
1045struct Channel *
1046find_nonickchange_channel(struct Client *client_p)
1047{
1048 struct Channel *chptr;
1049 struct membership *msptr;
1050 rb_dlink_node *ptr;
1051
1052 if (!MyClient(client_p))
1053 return NULL;
1054
1055 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
1056 {
1057 msptr = ptr->data;
1058 chptr = msptr->chptr;
5ad94b50 1059 if (chptr->mode.mode & MODE_NONICK && (!ConfigChannel.exempt_cmode_N || !is_any_op(msptr)))
afd4834b
G
1060 return chptr;
1061 }
1062 return NULL;
1063}
1064
212380e3 1065/* void check_spambot_warning(struct Client *source_p)
1066 * Input: Client to check, channel name or NULL if this is a part.
1067 * Output: none
1068 * Side-effects: Updates the client's oper_warn_count_down, warns the
1069 * IRC operators if necessary, and updates join_leave_countdown as
1070 * needed.
1071 */
1072void
1073check_spambot_warning(struct Client *source_p, const char *name)
1074{
1075 int t_delta;
1076 int decrement_count;
1077 if((GlobalSetOptions.spam_num &&
1078 (source_p->localClient->join_leave_count >= GlobalSetOptions.spam_num)))
1079 {
1080 if(source_p->localClient->oper_warn_count_down > 0)
1081 source_p->localClient->oper_warn_count_down--;
1082 else
1083 source_p->localClient->oper_warn_count_down = 0;
05d8a68c
JT
1084 if(source_p->localClient->oper_warn_count_down == 0 &&
1085 name != NULL)
212380e3 1086 {
1087 /* Its already known as a possible spambot */
05d8a68c
JT
1088 sendto_realops_snomask(SNO_BOTS, L_NETWIDE,
1089 "User %s (%s@%s) trying to join %s is a possible spambot",
1090 source_p->name,
1091 source_p->username, source_p->orighost, name);
212380e3 1092 source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
1093 }
1094 }
1095 else
1096 {
1097 if((t_delta =
9f6bbe3c 1098 (rb_current_time() - source_p->localClient->last_leave_time)) >
212380e3 1099 JOIN_LEAVE_COUNT_EXPIRE_TIME)
1100 {
1101 decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
b6698246
JT
1102 if(name != NULL)
1103 ;
1104 else if(decrement_count > source_p->localClient->join_leave_count)
212380e3 1105 source_p->localClient->join_leave_count = 0;
1106 else
1107 source_p->localClient->join_leave_count -= decrement_count;
1108 }
1109 else
1110 {
9f6bbe3c 1111 if((rb_current_time() -
212380e3 1112 (source_p->localClient->last_join_time)) < GlobalSetOptions.spam_time)
1113 {
1114 /* oh, its a possible spambot */
1115 source_p->localClient->join_leave_count++;
1116 }
1117 }
1118 if(name != NULL)
9f6bbe3c 1119 source_p->localClient->last_join_time = rb_current_time();
212380e3 1120 else
9f6bbe3c 1121 source_p->localClient->last_leave_time = rb_current_time();
212380e3 1122 }
1123}
1124
1125/* check_splitmode()
1126 *
1127 * input -
1128 * output -
1129 * side effects - compares usercount and servercount against their split
1130 * values and adjusts splitmode accordingly
1131 */
1132void
1133check_splitmode(void *unused)
1134{
1135 if(splitchecking && (ConfigChannel.no_join_on_split || ConfigChannel.no_create_on_split))
1136 {
1137 /* not split, we're being asked to check now because someone
1138 * has left
1139 */
1140 if(!splitmode)
1141 {
1142 if(eob_count < split_servers || Count.total < split_users)
1143 {
1144 splitmode = 1;
1145 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1146 "Network split, activating splitmode");
dd9be678 1147 check_splitmode_ev = rb_event_addish("check_splitmode", check_splitmode, NULL, 2);
212380e3 1148 }
1149 }
1150 /* in splitmode, check whether its finished */
1151 else if(eob_count >= split_servers && Count.total >= split_users)
1152 {
1153 splitmode = 0;
1154
1155 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1156 "Network rejoined, deactivating splitmode");
1157
dd9be678 1158 rb_event_delete(check_splitmode_ev);
0ae330b4 1159 check_splitmode_ev = NULL;
212380e3 1160 }
1161 }
1162}
1163
1164
1165/* allocate_topic()
1166 *
1167 * input - channel to allocate topic for
1168 * output - 1 on success, else 0
1169 * side effects - channel gets a topic allocated
1170 */
1171static void
1172allocate_topic(struct Channel *chptr)
1173{
1174 void *ptr;
1175
1176 if(chptr == NULL)
1177 return;
1178
6e9b4415 1179 ptr = rb_bh_alloc(topic_heap);
212380e3 1180
1181 /* Basically we allocate one large block for the topic and
1182 * the topic info. We then split it up into two and shove it
1183 * in the chptr
1184 */
1185 chptr->topic = ptr;
1186 chptr->topic_info = (char *) ptr + TOPICLEN + 1;
1187 *chptr->topic = '\0';
1188 *chptr->topic_info = '\0';
1189}
1190
1191/* free_topic()
1192 *
1193 * input - channel which has topic to free
1194 * output -
1195 * side effects - channels topic is free'd
1196 */
1197static void
1198free_topic(struct Channel *chptr)
1199{
1200 void *ptr;
1201
1202 if(chptr == NULL || chptr->topic == NULL)
1203 return;
1204
1205 /* This is safe for now - If you change allocate_topic you
1206 * MUST change this as well
1207 */
1208 ptr = chptr->topic;
6e9b4415 1209 rb_bh_free(topic_heap, ptr);
212380e3 1210 chptr->topic = NULL;
1211 chptr->topic_info = NULL;
1212}
1213
1214/* set_channel_topic()
1215 *
1216 * input - channel, topic to set, topic info and topic ts
1217 * output -
1218 * side effects - channels topic, topic info and TS are set.
1219 */
1220void
1221set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_info, time_t topicts)
1222{
1223 if(strlen(topic) > 0)
1224 {
1225 if(chptr->topic == NULL)
1226 allocate_topic(chptr);
907468c4
VY
1227 rb_strlcpy(chptr->topic, topic, TOPICLEN + 1);
1228 rb_strlcpy(chptr->topic_info, topic_info, USERHOST_REPLYLEN);
212380e3 1229 chptr->topic_time = topicts;
1230 }
1231 else
1232 {
1233 if(chptr->topic != NULL)
1234 free_topic(chptr);
1235 chptr->topic_time = 0;
1236 }
1237}
1238
c279d43b
G
1239/* has_common_channel()
1240 *
1241 * input - pointer to client
1242 * - pointer to another client
1243 * output - 1 if the two have a channel in common, 0 elsewise
1244 * side effects - none
1245 */
1246int
1247has_common_channel(struct Client *client1, struct Client *client2)
1248{
1249 rb_dlink_node *ptr;
1250
1251 RB_DLINK_FOREACH(ptr, client1->user->channel.head)
1252 {
1253 if(IsMember(client2, ((struct membership *)ptr->data)->chptr))
1254 return 1;
1255 }
99c78094 1256 return 0;
c279d43b
G
1257}
1258
212380e3 1259/* channel_modes()
1260 *
1261 * inputs - pointer to channel
1262 * - pointer to client
f1e35c19 1263 * output - string with simple modes
1264 * side effects - result from previous calls overwritten
212380e3 1265 *
1266 * Stolen from ShadowIRCd 4 --nenolod
1267 */
1268const char *
1269channel_modes(struct Channel *chptr, struct Client *client_p)
1270{
1271 int i;
1272 char buf1[BUFSIZE];
1273 char buf2[BUFSIZE];
1274 static char final[BUFSIZE];
1275 char *mbuf = buf1;
1276 char *pbuf = buf2;
1277
1278 *mbuf++ = '+';
1279 *pbuf = '\0';
1280
75818939 1281 for (i = 0; i < 256; i++)
1cdd8fdf 1282 {
565f4362 1283 if(chmode_table[i].set_func == chm_hidden && !IsOper(client_p) && IsClient(client_p))
1cdd8fdf 1284 continue;
75818939
VY
1285 if(chptr->mode.mode & chmode_flags[i])
1286 *mbuf++ = i;
1cdd8fdf 1287 }
212380e3 1288
1289 if(chptr->mode.limit)
1290 {
1291 *mbuf++ = 'l';
1292
f1e35c19 1293 if(!IsClient(client_p) || IsMember(client_p, chptr))
38e6acdd 1294 pbuf += rb_sprintf(pbuf, " %d", chptr->mode.limit);
212380e3 1295 }
1296
1297 if(*chptr->mode.key)
1298 {
1299 *mbuf++ = 'k';
1300
f1e35c19 1301 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
38e6acdd 1302 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.key);
212380e3 1303 }
1304
1305 if(chptr->mode.join_num)
1306 {
1307 *mbuf++ = 'j';
1308
f1e35c19 1309 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
38e6acdd 1310 pbuf += rb_sprintf(pbuf, " %d:%d", chptr->mode.join_num,
212380e3 1311 chptr->mode.join_time);
1312 }
1313
f1e35c19 1314 if(*chptr->mode.forward && (ConfigChannel.use_forward || !IsClient(client_p)))
212380e3 1315 {
1316 *mbuf++ = 'f';
1317
f1e35c19 1318 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
38e6acdd 1319 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.forward);
212380e3 1320 }
1321
1322 *mbuf = '\0';
1323
907468c4 1324 rb_strlcpy(final, buf1, sizeof final);
a64c5173 1325 rb_strlcat(final, buf2, sizeof final);
212380e3 1326 return final;
1327}
1328
1329/* Now lets do some stuff to keep track of what combinations of
1330 * servers exist...
1331 * Note that the number of combinations doubles each time you add
1332 * something to this list. Each one is only quick if no servers use that
1333 * combination, but if the numbers get too high here MODE will get too
1334 * slow. I suggest if you get more than 7 here, you consider getting rid
1335 * of some and merging or something. If it wasn't for irc+cs we would
1336 * probably not even need to bother about most of these, but unfortunately
1337 * we do. -A1kmm
1338 */
1339
1340/* void init_chcap_usage_counts(void)
1341 *
1342 * Inputs - none
1343 * Output - none
1344 * Side-effects - Initialises the usage counts to zero. Fills in the
1345 * chcap_yes and chcap_no combination tables.
1346 */
1347void
1348init_chcap_usage_counts(void)
1349{
1350 unsigned long m, c, y, n;
1351
1352 memset(chcap_combos, 0, sizeof(chcap_combos));
1353
1354 /* For every possible combination */
1355 for (m = 0; m < NCHCAP_COMBOS; m++)
1356 {
1357 /* Check each capab */
1358 for (c = y = n = 0; c < NCHCAPS; c++)
1359 {
1360 if((m & (1 << c)) == 0)
1361 n |= channel_capabs[c];
1362 else
1363 y |= channel_capabs[c];
1364 }
1365 chcap_combos[m].cap_yes = y;
1366 chcap_combos[m].cap_no = n;
1367 }
1368}
1369
1370/* void set_chcap_usage_counts(struct Client *serv_p)
1371 * Input: serv_p; The client whose capabs to register.
1372 * Output: none
1373 * Side-effects: Increments the usage counts for the correct capab
1374 * combination.
1375 */
1376void
1377set_chcap_usage_counts(struct Client *serv_p)
1378{
1379 int n;
1380
1381 for (n = 0; n < NCHCAP_COMBOS; n++)
1382 {
1383 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1384 NotCapable(serv_p, chcap_combos[n].cap_no))
1385 {
1386 chcap_combos[n].count++;
1387 return;
1388 }
1389 }
1390
1391 /* This should be impossible -A1kmm. */
1392 s_assert(0);
1393}
1394
1395/* void set_chcap_usage_counts(struct Client *serv_p)
1396 *
1397 * Inputs - serv_p; The client whose capabs to register.
1398 * Output - none
1399 * Side-effects - Decrements the usage counts for the correct capab
1400 * combination.
1401 */
1402void
1403unset_chcap_usage_counts(struct Client *serv_p)
1404{
1405 int n;
1406
1407 for (n = 0; n < NCHCAP_COMBOS; n++)
1408 {
1409 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1410 NotCapable(serv_p, chcap_combos[n].cap_no))
1411 {
1412 /* Hopefully capabs can't change dynamically or anything... */
1413 s_assert(chcap_combos[n].count > 0);
1414
1415 if(chcap_combos[n].count > 0)
1416 chcap_combos[n].count--;
1417 return;
1418 }
1419 }
1420
1421 /* This should be impossible -A1kmm. */
1422 s_assert(0);
1423}
1424
1425/* void send_cap_mode_changes(struct Client *client_p,
1426 * struct Client *source_p,
1427 * struct Channel *chptr, int cap, int nocap)
1428 * Input: The client sending(client_p), the source client(source_p),
1429 * the channel to send mode changes for(chptr)
1430 * Output: None.
1431 * Side-effects: Sends the appropriate mode changes to capable servers.
1432 *
1433 * Reverted back to my original design, except that we now keep a count
1434 * of the number of servers which each combination as an optimisation, so
1435 * the capabs combinations which are not needed are not worked out. -A1kmm
1436 */
1437void
1438send_cap_mode_changes(struct Client *client_p, struct Client *source_p,
1439 struct Channel *chptr, struct ChModeChange mode_changes[], int mode_count)
1440{
1441 static char modebuf[BUFSIZE];
1442 static char parabuf[BUFSIZE];
1443 int i, mbl, pbl, nc, mc, preflen, len;
1444 char *pbuf;
1445 const char *arg;
1446 int dir;
1447 int j;
1448 int cap;
1449 int nocap;
1450 int arglen;
1451
1452 /* Now send to servers... */
1453 for (j = 0; j < NCHCAP_COMBOS; j++)
1454 {
1455 if(chcap_combos[j].count == 0)
1456 continue;
1457
1458 mc = 0;
1459 nc = 0;
1460 pbl = 0;
1461 parabuf[0] = 0;
1462 pbuf = parabuf;
1463 dir = MODE_QUERY;
1464
1465 cap = chcap_combos[j].cap_yes;
1466 nocap = chcap_combos[j].cap_no;
1467
9f9b4d7b
WP
1468 mbl = preflen = rb_sprintf(modebuf, ":%s TMODE %ld %s ",
1469 use_id(source_p), (long) chptr->channelts,
1470 chptr->chname);
212380e3 1471
1472 /* loop the list of - modes we have */
1473 for (i = 0; i < mode_count; i++)
1474 {
1475 /* if they dont support the cap we need, or they do support a cap they
1476 * cant have, then dont add it to the modebuf.. that way they wont see
1477 * the mode
1478 */
1479 if((mode_changes[i].letter == 0) ||
1480 ((cap & mode_changes[i].caps) != mode_changes[i].caps)
1481 || ((nocap & mode_changes[i].nocaps) != mode_changes[i].nocaps))
1482 continue;
1483
7592f950
JT
1484 if(!EmptyString(mode_changes[i].id))
1485 arg = mode_changes[i].id;
1486 else
1487 arg = mode_changes[i].arg;
212380e3 1488
1489 if(arg)
1490 {
1491 arglen = strlen(arg);
1492
1493 /* dont even think about it! --fl */
1494 if(arglen > MODEBUFLEN - 5)
1495 continue;
1496 }
1497
1498 /* if we're creeping past the buf size, we need to send it and make
1499 * another line for the other modes
1500 * XXX - this could give away server topology with uids being
1501 * different lengths, but not much we can do, except possibly break
1502 * them as if they were the longest of the nick or uid at all times,
1503 * which even then won't work as we don't always know the uid -A1kmm.
1504 */
1505 if(arg && ((mc == MAXMODEPARAMSSERV) ||
1506 ((mbl + pbl + arglen + 4) > (BUFSIZE - 3))))
1507 {
1508 if(nc != 0)
1509 sendto_server(client_p, chptr, cap, nocap,
1510 "%s %s", modebuf, parabuf);
1511 nc = 0;
1512 mc = 0;
1513
1514 mbl = preflen;
1515 pbl = 0;
1516 pbuf = parabuf;
1517 parabuf[0] = 0;
1518 dir = MODE_QUERY;
1519 }
1520
1521 if(dir != mode_changes[i].dir)
1522 {
1523 modebuf[mbl++] = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
1524 dir = mode_changes[i].dir;
1525 }
1526
1527 modebuf[mbl++] = mode_changes[i].letter;
1528 modebuf[mbl] = 0;
1529 nc++;
1530
1531 if(arg != NULL)
1532 {
38e6acdd 1533 len = rb_sprintf(pbuf, "%s ", arg);
212380e3 1534 pbuf += len;
1535 pbl += len;
1536 mc++;
1537 }
1538 }
1539
1540 if(pbl && parabuf[pbl - 1] == ' ')
1541 parabuf[pbl - 1] = 0;
1542
1543 if(nc != 0)
1544 sendto_server(client_p, chptr, cap, nocap, "%s %s", modebuf, parabuf);
1545 }
1546}
080bb5cf
JH
1547
1548/* Check what we will forward to, without sending any notices to the user
1549 * -- jilles
1550 */
1551struct Channel *
1552check_forward(struct Client *source_p, struct Channel *chptr,
1553 char *key)
1554{
1555 int depth = 0, i;
1556
1557 /* User is +Q */
1558 if (IsNoForward(source_p))
1559 return NULL;
1560
1561 while (depth < 16)
1562 {
1563 chptr = find_channel(chptr->mode.forward);
1564 /* Can only forward to existing channels */
1565 if (chptr == NULL)
1566 return NULL;
1567 /* Already on there, show original error message */
1568 if (IsMember(source_p, chptr))
1569 return NULL;
1570 /* Juped. Sending a warning notice would be unfair */
1571 if (hash_find_resv(chptr->chname))
1572 return NULL;
1573 /* Don't forward to +Q channel */
1574 if (chptr->mode.mode & MODE_DISFORWARD)
1575 return NULL;
1576 i = can_join(source_p, chptr, key);
1577 if (i == 0)
1578 return chptr;
1579 if (i != ERR_INVITEONLYCHAN && i != ERR_NEEDREGGEDNICK && i != ERR_THROTTLE && i != ERR_CHANNELISFULL)
1580 return NULL;
1581 depth++;
1582 }
1583
1584 return NULL;
1585}
67b90240 1586
9230426e
JH
1587/*
1588 * do_join_0
1589 *
1590 * inputs - pointer to client doing join 0
1591 * output - NONE
1592 * side effects - Use has decided to join 0. This is legacy
1593 * from the days when channels were numbers not names. *sigh*
9230426e
JH
1594 */
1595void
1596do_join_0(struct Client *client_p, struct Client *source_p)
1597{
1598 struct membership *msptr;
1599 struct Channel *chptr = NULL;
1600 rb_dlink_node *ptr;
1601
1602 /* Finish the flood grace period... */
1603 if(MyClient(source_p) && !IsFloodDone(source_p))
1604 flood_endgrace(source_p);
1605
1606 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s JOIN 0", use_id(source_p));
1607
aa35afbb
JH
1608 while((ptr = source_p->user->channel.head))
1609 {
9230426e
JH
1610 if(source_p->user->channel.head && MyConnect(source_p) &&
1611 !IsOper(source_p) && !IsExemptSpambot(source_p))
1612 check_spambot_warning(source_p, NULL);
1613
9230426e
JH
1614 msptr = ptr->data;
1615 chptr = msptr->chptr;
1616 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s",
1617 source_p->name,
1618 source_p->username, source_p->host, chptr->chname);
1619 remove_user_from_channel(msptr);
1620 }
1621}
1622
1623int
1624check_channel_name_loc(struct Client *source_p, const char *name)
1625{
aa35afbb
JH
1626 const char *p;
1627
9230426e
JH
1628 s_assert(name != NULL);
1629 if(EmptyString(name))
1630 return 0;
1631
1632 if(ConfigFileEntry.disable_fake_channels && !IsOper(source_p))
1633 {
aa35afbb 1634 for(p = name; *p; ++p)
9230426e 1635 {
aa35afbb 1636 if(!IsChanChar(*p) || IsFakeChanChar(*p))
9230426e
JH
1637 return 0;
1638 }
1639 }
1640 else
1641 {
aa35afbb 1642 for(p = name; *p; ++p)
9230426e 1643 {
aa35afbb 1644 if(!IsChanChar(*p))
9230426e
JH
1645 return 0;
1646 }
1647 }
1648
aa35afbb
JH
1649 if(ConfigChannel.only_ascii_channels)
1650 {
1651 for(p = name; *p; ++p)
1652 if(*p < 33 || *p > 126)
1653 return 0;
1654 }
1655
1656
9230426e
JH
1657 return 1;
1658}
1659
824455ab 1660void user_join(struct Client * client_p, struct Client * source_p, const char * channels, const char * keys)
67b90240
JH
1661{
1662 static char jbuf[BUFSIZE];
1663 struct Channel *chptr = NULL;
1664 struct ConfItem *aconf;
1665 char *name;
1666 char *key = NULL;
1667 const char *modes;
1668 int i, flags = 0;
1669 char *p = NULL, *p2 = NULL;
1670 char *chanlist;
1671 char *mykey;
67b90240
JH
1672
1673 jbuf[0] = '\0';
1674
1675 if(channels == NULL)
1676 return;
1677
1678 /* rebuild the list of channels theyre supposed to be joining.
1679 * this code has a side effect of losing keys, but..
1680 */
1681 chanlist = LOCAL_COPY(channels);
1682 for(name = rb_strtok_r(chanlist, ",", &p); name; name = rb_strtok_r(NULL, ",", &p))
1683 {
1684 /* check the length and name of channel is ok */
1685 if(!check_channel_name_loc(source_p, name) || (strlen(name) > LOC_CHANNELLEN))
1686 {
1687 sendto_one_numeric(source_p, ERR_BADCHANNAME,
1688 form_str(ERR_BADCHANNAME), (unsigned char *) name);
1689 continue;
1690 }
1691
1692 /* join 0 parts all channels */
1693 if(*name == '0' && (name[1] == ',' || name[1] == '\0') && name == chanlist)
1694 {
1695 (void) strcpy(jbuf, "0");
1696 continue;
1697 }
1698
1699 /* check it begins with # or &, and local chans are disabled */
0eceaff1
G
1700 else if(!IsChannelName(name) ||
1701 ( !ConfigChannel.use_local_channels && name[0] == '&'))
67b90240
JH
1702 {
1703 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
1704 form_str(ERR_NOSUCHCHANNEL), name);
1705 continue;
1706 }
1707
1708 /* see if its resv'd */
1709 if(!IsExemptResv(source_p) && (aconf = hash_find_resv(name)))
1710 {
1711 sendto_one_numeric(source_p, ERR_BADCHANNAME,
1712 form_str(ERR_BADCHANNAME), name);
1713
1714 /* dont warn for opers */
1715 if(!IsExemptJupe(source_p) && !IsOper(source_p))
1716 sendto_realops_snomask(SNO_SPY, L_NETWIDE,
1717 "User %s (%s@%s) is attempting to join locally juped channel %s (%s)",
1718 source_p->name, source_p->username,
1719 source_p->orighost, name, aconf->passwd);
1720 /* dont update tracking for jupe exempt users, these
1721 * are likely to be spamtrap leaves
1722 */
1723 else if(IsExemptJupe(source_p))
1724 aconf->port--;
1725
1726 continue;
1727 }
1728
1729 if(splitmode && !IsOper(source_p) && (*name != '&') &&
1730 ConfigChannel.no_join_on_split)
1731 {
1732 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1733 me.name, source_p->name, name);
1734 continue;
1735 }
1736
1737 if(*jbuf)
1738 (void) strcat(jbuf, ",");
1739 (void) rb_strlcat(jbuf, name, sizeof(jbuf));
1740 }
1741
1742 if(keys != NULL)
1743 {
1744 mykey = LOCAL_COPY(keys);
1745 key = rb_strtok_r(mykey, ",", &p2);
1746 }
1747
1748 for(name = rb_strtok_r(jbuf, ",", &p); name;
1749 key = (key) ? rb_strtok_r(NULL, ",", &p2) : NULL, name = rb_strtok_r(NULL, ",", &p))
1750 {
1751 hook_data_channel_activity hook_info;
1752
1753 /* JOIN 0 simply parts all channels the user is in */
1754 if(*name == '0' && !atoi(name))
1755 {
1756 if(source_p->user->channel.head == NULL)
1757 continue;
1758
1759 do_join_0(&me, source_p);
1760 continue;
1761 }
1762
1763 /* look for the channel */
1764 if((chptr = find_channel(name)) != NULL)
1765 {
1766 if(IsMember(source_p, chptr))
1767 continue;
1768
1769 flags = 0;
1770 }
1771 else
1772 {
1773 hook_data_client_approval moduledata;
1774
1775 moduledata.client = source_p;
1776 moduledata.approved = 0;
1777
1778 call_hook(h_can_create_channel, &moduledata);
1779
1780 if(moduledata.approved != 0)
1781 {
1782 sendto_one(source_p, form_str(moduledata.approved),
1783 me.name, source_p->name, name);
1784 continue;
1785 }
1786
1787 if(splitmode && !IsOper(source_p) && (*name != '&') &&
1788 ConfigChannel.no_create_on_split)
1789 {
1790 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1791 me.name, source_p->name, name);
1792 continue;
1793 }
1794
1795 flags = CHFL_CHANOP;
1796 }
1797
1798 if((rb_dlink_list_length(&source_p->user->channel) >=
1799 (unsigned long) ConfigChannel.max_chans_per_user) &&
1800 (!IsOper(source_p) ||
1801 (rb_dlink_list_length(&source_p->user->channel) >=
1802 (unsigned long) ConfigChannel.max_chans_per_user * 3)))
1803 {
1804 sendto_one(source_p, form_str(ERR_TOOMANYCHANNELS),
1805 me.name, source_p->name, name);
67b90240
JH
1806 return;
1807 }
1808
67b90240
JH
1809 if(chptr == NULL) /* If I already have a chptr, no point doing this */
1810 {
1811 chptr = get_or_create_channel(source_p, name, NULL);
1812
1813 if(chptr == NULL)
1814 {
1815 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1816 me.name, source_p->name, name);
67b90240
JH
1817 continue;
1818 }
1819 }
1820
67b90240
JH
1821 /* can_join checks for +i key, bans etc */
1822 if((i = can_join(source_p, chptr, key)))
1823 {
1824 if ((i != ERR_NEEDREGGEDNICK && i != ERR_THROTTLE && i != ERR_INVITEONLYCHAN && i != ERR_CHANNELISFULL) ||
1825 (!ConfigChannel.use_forward || (chptr = check_forward(source_p, chptr, key)) == NULL))
1826 {
1827 /* might be wrong, but is there any other better location for such?
1828 * see extensions/chm_operonly.c for other comments on this
1829 * -- dwr
1830 */
1831 if(i != ERR_CUSTOM)
1832 sendto_one(source_p, form_str(i), me.name, source_p->name, name);
1833
67b90240
JH
1834 continue;
1835 }
1836
1837 sendto_one_numeric(source_p, ERR_LINKCHANNEL, form_str(ERR_LINKCHANNEL), name, chptr->chname);
1838 }
aa35afbb
JH
1839
1840 if(flags == 0 &&
1841 !IsOper(source_p) && !IsExemptSpambot(source_p))
1842 check_spambot_warning(source_p, name);
67b90240
JH
1843
1844 /* add the user to the channel */
1845 add_user_to_channel(chptr, source_p, flags);
1846 if (chptr->mode.join_num &&
1847 rb_current_time() - chptr->join_delta >= chptr->mode.join_time)
1848 {
1849 chptr->join_count = 0;
1850 chptr->join_delta = rb_current_time();
1851 }
1852 chptr->join_count++;
1853
1854 /* we send the user their join here, because we could have to
1855 * send a mode out next.
1856 */
1857 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s JOIN :%s",
1858 source_p->name,
1859 source_p->username, source_p->host, chptr->chname);
1860
1861 /* its a new channel, set +nt and burst. */
1862 if(flags & CHFL_CHANOP)
1863 {
1864 chptr->channelts = rb_current_time();
13ec57db
JH
1865
1866 /* autochanmodes stuff */
1867 if(ConfigChannel.autochanmodes)
1868 {
1869 char * ch;
1870 for(ch = ConfigChannel.autochanmodes; *ch; *ch++)
1871 {
1872 chptr->mode.mode |= chmode_table[*ch].mode_type;
1873 }
1874 }
1875 else
1876 {
1877 chptr->mode.mode |= MODE_TOPICLIMIT;
1878 chptr->mode.mode |= MODE_NOPRIVMSGS;
1879 }
1880
67b90240
JH
1881 modes = channel_modes(chptr, &me);
1882
1883 sendto_channel_local(ONLY_CHANOPS, chptr, ":%s MODE %s %s",
1884 me.name, chptr->chname, modes);
1885
1886 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
1887 ":%s SJOIN %ld %s %s :@%s",
1888 me.id, (long) chptr->channelts,
1889 chptr->chname, modes, source_p->id);
1890 }
1891 else
1892 {
1893 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
1894 ":%s JOIN %ld %s +",
1895 use_id(source_p), (long) chptr->channelts,
1896 chptr->chname);
1897 }
1898
1899 del_invite(chptr, source_p);
1900
1901 if(chptr->topic != NULL)
1902 {
1903 sendto_one(source_p, form_str(RPL_TOPIC), me.name,
1904 source_p->name, chptr->chname, chptr->topic);
1905
1906 sendto_one(source_p, form_str(RPL_TOPICWHOTIME),
1907 me.name, source_p->name, chptr->chname,
1908 chptr->topic_info, chptr->topic_time);
1909 }
1910
1911 channel_member_names(chptr, source_p, 1);
1912
67b90240
JH
1913 hook_info.client = source_p;
1914 hook_info.chptr = chptr;
1915 hook_info.key = key;
1916 call_hook(h_channel_join, &hook_info);
1917 }
1918
1919 return;
1920}
6f659342
G
1921
1922/*
1923 * channel_metadata_add
1924 *
1925 * inputs - pointer to channel struct
1926 * - name of metadata item you wish to add
1927 * - value of metadata item
1928 * - 1 if metadata should be propegated, 0 if not
1929 * output - none
1930 * side effects - metadata is added to the channel in question
1931 * - metadata is propegated if propegate is set.
1932 */
1933struct Metadata *
1934channel_metadata_add(struct Channel *target, const char *name, const char *value, int propegate)
1935{
0b370fcc 1936 struct Metadata *md;
6f659342 1937
6f659342
G
1938 md = rb_malloc(sizeof(struct Metadata));
1939 md->name = rb_strdup(name);
1940 md->value = rb_strdup(value);
1941
0b370fcc 1942 irc_dictionary_add(target->metadata, md->name, md);
6f659342
G
1943
1944 if(propegate)
1945 sendto_match_servs(&me, "*", CAP_ENCAP, NOCAPS, "ENCAP * METADATA ADD %s %s :%s",
1946 target->chname, name, value);
1947
1948 return md;
1949}
1950
104becbf
G
1951/*
1952 * channel_metadata_time_add
1953 *
1954 * inputs - pointer to channel struct
1955 * - name of metadata item you wish to add
1956 * - time_t you wish to add
6a97cac6 1957 * - value you wish to add
104becbf
G
1958 * output - none
1959 * side effects - metadata is added to the channel in question
1960 */
1961struct Metadata *
6a97cac6 1962channel_metadata_time_add(struct Channel *target, const char *name, time_t timevalue, const char *value)
104becbf 1963{
0b370fcc 1964 struct Metadata *md;
104becbf 1965
0b370fcc 1966 md = rb_malloc(sizeof(struct Metadata));
104becbf 1967 md->name = rb_strdup(name);
6a97cac6
G
1968 md->value = rb_strdup(value);
1969 md->timevalue = timevalue;
104becbf 1970
0b370fcc 1971 irc_dictionary_add(target->metadata, md->name, md);
104becbf
G
1972
1973 return md;
1974}
1975
6f659342
G
1976/*
1977 * channel_metadata_delete
1978 *
1979 * inputs - pointer to channel struct
1980 * - name of metadata item you wish to delete
1981 * output - none
1982 * side effects - metadata is deleted from the channel in question
1983 * - deletion is propegated if propegate is set
1984 */
1985void
1986channel_metadata_delete(struct Channel *target, const char *name, int propegate)
1987{
0b370fcc 1988 struct Metadata *md = channel_metadata_find(target, name);
6f659342
G
1989
1990 if(!md)
1991 return;
1992
0b370fcc 1993 irc_dictionary_delete(target->metadata, md->name);
6f659342
G
1994
1995 rb_free(md);
1996
1997 if(propegate)
1998 sendto_match_servs(&me, "*", CAP_ENCAP, NOCAPS, "ENCAP * METADATA DELETE %s %s",
1999 target->chname, name);
2000}
2001
2002/*
2003 * channel_metadata_find
2004 *
2005 * inputs - pointer to channel struct
2006 * - name of metadata item you wish to read
2007 * output - the requested metadata, if it exists, elsewise null.
2008 * side effects -
2009 */
2010struct Metadata *
2011channel_metadata_find(struct Channel *target, const char *name)
2012{
2013 if(!target)
2014 return NULL;
2015
0b370fcc 2016 if(!target->metadata)
6f659342
G
2017 return NULL;
2018
0b370fcc 2019 return irc_dictionary_retrieve(target->metadata, name);
6f659342 2020}
8bced6dc
G
2021
2022/*
2023 * channel_metadata_clear
2024 *
2025 * inputs - pointer to channel struct
2026 * output - none
2027 * side effects - metadata is cleared from the channel in question
2028 */
2029void
2030channel_metadata_clear(struct Channel *chptr)
2031{
2032 struct Metadata *md;
2033 struct DictionaryIter iter;
2034
0b370fcc 2035 DICTIONARY_FOREACH(md, &iter, chptr->metadata)
8bced6dc
G
2036 {
2037 channel_metadata_delete(chptr, md->name, 0);
2038 }
2039}