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