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