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