2 * ircd-ratbox: A slightly useful ircd.
3 * channel.c: Controls channels.
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
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.
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.
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
24 * $Id: channel.c 3580 2007-11-07 23:45:14Z jilles $
37 #include "s_serv.h" /* captab */
41 #include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
42 #include "s_newconf.h"
45 struct config_channel_entry ConfigChannel
;
46 rb_dlink_list global_channel_list
;
47 static rb_bh
*channel_heap
;
48 static rb_bh
*ban_heap
;
49 static rb_bh
*topic_heap
;
50 static rb_bh
*member_heap
;
52 static int channel_capabs
[] = { CAP_EX
, CAP_IE
,
57 #define NCHCAPS (sizeof(channel_capabs)/sizeof(int))
58 #define NCHCAP_COMBOS (1 << NCHCAPS)
60 static struct ChCapCombo chcap_combos
[NCHCAP_COMBOS
];
62 static void free_topic(struct Channel
*chptr
);
64 static int h_can_join
;
65 static int h_can_send
;
66 int h_get_channel_access
;
72 * side effects - initialises the various blockheaps
77 channel_heap
= rb_bh_create(sizeof(struct Channel
), CHANNEL_HEAP_SIZE
, "channel_heap");
78 ban_heap
= rb_bh_create(sizeof(struct Ban
), BAN_HEAP_SIZE
, "ban_heap");
79 topic_heap
= rb_bh_create(TOPICLEN
+ 1 + USERHOST_REPLYLEN
, TOPIC_HEAP_SIZE
, "topic_heap");
80 member_heap
= rb_bh_create(sizeof(struct membership
), MEMBER_HEAP_SIZE
, "member_heap");
82 h_can_join
= register_hook("can_join");
83 h_can_send
= register_hook("can_send");
84 h_get_channel_access
= register_hook("get_channel_access");
88 * allocate_channel - Allocates a channel
91 allocate_channel(const char *chname
)
93 struct Channel
*chptr
;
94 chptr
= rb_bh_alloc(channel_heap
);
95 chptr
->chname
= rb_strdup(chname
);
100 free_channel(struct Channel
*chptr
)
102 rb_free(chptr
->chname
);
103 rb_free(chptr
->mode_lock
);
104 rb_bh_free(channel_heap
, chptr
);
108 allocate_ban(const char *banstr
, const char *who
)
111 bptr
= rb_bh_alloc(ban_heap
);
112 bptr
->banstr
= rb_strdup(banstr
);
113 bptr
->who
= rb_strdup(who
);
119 free_ban(struct Ban
*bptr
)
121 rb_free(bptr
->banstr
);
123 rb_bh_free(ban_heap
, bptr
);
127 /* find_channel_membership()
129 * input - channel to find them in, client to find
130 * output - membership of client in channel, else NULL
134 find_channel_membership(struct Channel
*chptr
, struct Client
*client_p
)
136 struct membership
*msptr
;
139 if(!IsClient(client_p
))
142 /* Pick the most efficient list to use to be nice to things like
143 * CHANSERV which could be in a large number of channels
145 if(rb_dlink_list_length(&chptr
->members
) < rb_dlink_list_length(&client_p
->user
->channel
))
147 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
151 if(msptr
->client_p
== client_p
)
157 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
161 if(msptr
->chptr
== chptr
)
169 /* find_channel_status()
171 * input - membership to get status for, whether we can combine flags
172 * output - flags of user on channel
176 find_channel_status(struct membership
*msptr
, int combine
)
178 static char buffer
[3];
197 /* add_user_to_channel()
199 * input - channel to add client to, client to add, channel flags
201 * side effects - user is added to channel
204 add_user_to_channel(struct Channel
*chptr
, struct Client
*client_p
, int flags
)
206 struct membership
*msptr
;
208 s_assert(client_p
->user
!= NULL
);
209 if(client_p
->user
== NULL
)
212 msptr
= rb_bh_alloc(member_heap
);
214 msptr
->chptr
= chptr
;
215 msptr
->client_p
= client_p
;
216 msptr
->flags
= flags
;
218 rb_dlinkAdd(msptr
, &msptr
->usernode
, &client_p
->user
->channel
);
219 rb_dlinkAdd(msptr
, &msptr
->channode
, &chptr
->members
);
221 if(MyClient(client_p
))
222 rb_dlinkAdd(msptr
, &msptr
->locchannode
, &chptr
->locmembers
);
225 /* remove_user_from_channel()
227 * input - membership pointer to remove from channel
229 * side effects - membership (thus user) is removed from channel
232 remove_user_from_channel(struct membership
*msptr
)
234 struct Client
*client_p
;
235 struct Channel
*chptr
;
236 s_assert(msptr
!= NULL
);
240 client_p
= msptr
->client_p
;
241 chptr
= msptr
->chptr
;
243 rb_dlinkDelete(&msptr
->usernode
, &client_p
->user
->channel
);
244 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
246 if(client_p
->servptr
== &me
)
247 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
249 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
250 destroy_channel(chptr
);
252 rb_bh_free(member_heap
, msptr
);
257 /* remove_user_from_channels()
259 * input - user to remove from all channels
261 * side effects - user is removed from all channels
264 remove_user_from_channels(struct Client
*client_p
)
266 struct Channel
*chptr
;
267 struct membership
*msptr
;
269 rb_dlink_node
*next_ptr
;
274 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->user
->channel
.head
)
277 chptr
= msptr
->chptr
;
279 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
281 if(client_p
->servptr
== &me
)
282 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
284 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
285 destroy_channel(chptr
);
287 rb_bh_free(member_heap
, msptr
);
290 client_p
->user
->channel
.head
= client_p
->user
->channel
.tail
= NULL
;
291 client_p
->user
->channel
.length
= 0;
294 /* invalidate_bancache_user()
296 * input - user to invalidate ban cache for
298 * side effects - ban cache is invalidated for all memberships of that user
299 * to be used after a nick change
302 invalidate_bancache_user(struct Client
*client_p
)
304 struct membership
*msptr
;
310 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
314 msptr
->flags
&= ~CHFL_BANNED
;
318 /* check_channel_name()
320 * input - channel name
321 * output - 1 if valid channel name, else 0
325 check_channel_name(const char *name
)
327 s_assert(name
!= NULL
);
331 for (; *name
; ++name
)
333 if(!IsChanChar(*name
))
340 /* free_channel_list()
342 * input - rb_dlink list to free
344 * side effects - list of b/e/I modes is cleared
347 free_channel_list(rb_dlink_list
* list
)
350 rb_dlink_node
*next_ptr
;
351 struct Ban
*actualBan
;
353 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
355 actualBan
= ptr
->data
;
359 list
->head
= list
->tail
= NULL
;
365 * input - channel to destroy
367 * side effects - channel is obliterated
370 destroy_channel(struct Channel
*chptr
)
372 rb_dlink_node
*ptr
, *next_ptr
;
374 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
376 del_invite(chptr
, ptr
->data
);
379 /* free all bans/exceptions/denies */
380 free_channel_list(&chptr
->banlist
);
381 free_channel_list(&chptr
->exceptlist
);
382 free_channel_list(&chptr
->invexlist
);
383 free_channel_list(&chptr
->quietlist
);
388 rb_dlinkDelete(&chptr
->node
, &global_channel_list
);
389 del_from_channel_hash(chptr
->chname
, chptr
);
393 /* channel_pub_or_secret()
396 * output - "=" if public, "@" if secret, else "*"
400 channel_pub_or_secret(struct Channel
*chptr
)
402 if(PubChannel(chptr
))
404 else if(SecretChannel(chptr
))
409 /* channel_member_names()
411 * input - channel to list, client to list to, show endofnames
413 * side effects - client is given list of users on channel
416 channel_member_names(struct Channel
*chptr
, struct Client
*client_p
, int show_eon
)
418 struct membership
*msptr
;
419 struct Client
*target_p
;
427 int stack
= IsCapable(client_p
, CLICAP_MULTI_PREFIX
);
429 if(ShowChannel(client_p
, chptr
))
431 is_member
= IsMember(client_p
, chptr
);
433 cur_len
= mlen
= rb_sprintf(lbuf
, form_str(RPL_NAMREPLY
),
434 me
.name
, client_p
->name
,
435 channel_pub_or_secret(chptr
), chptr
->chname
);
439 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
442 target_p
= msptr
->client_p
;
444 if(IsInvisible(target_p
) && !is_member
)
447 /* space, possible "@+" prefix */
448 if(cur_len
+ strlen(target_p
->name
) + 3 >= BUFSIZE
- 3)
451 sendto_one(client_p
, "%s", lbuf
);
456 tlen
= rb_sprintf(t
, "%s%s ", find_channel_status(msptr
, stack
),
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
473 sendto_one(client_p
, "%s", lbuf
);
478 sendto_one(client_p
, form_str(RPL_ENDOFNAMES
),
479 me
.name
, client_p
->name
, chptr
->chname
);
484 * input - channel to remove invite from, client to remove
486 * side effects - user is removed from invite list, if exists
489 del_invite(struct Channel
*chptr
, struct Client
*who
)
491 rb_dlinkFindDestroy(who
, &chptr
->invites
);
492 rb_dlinkFindDestroy(chptr
, &who
->user
->invited
);
497 * input - channel to check bans for, user to check bans against
498 * optional prebuilt buffers
499 * output - 1 if banned, else 0
503 is_banned(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
504 const char *s
, const char *s2
)
506 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
507 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
508 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
511 struct Ban
*actualBan
= NULL
;
512 struct Ban
*actualExcept
= NULL
;
517 /* if the buffers havent been built, do it here */
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
);
526 if(who
->localClient
->mangledhost
!= NULL
)
528 /* if host mangling mode enabled, also check their real host */
529 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
531 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
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
))
538 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
543 RB_DLINK_FOREACH(ptr
, chptr
->banlist
.head
)
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
)))
556 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
558 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
560 actualExcept
= ptr
->data
;
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
)))
569 /* cache the fact theyre not banned */
572 msptr
->bants
= chptr
->bants
;
573 msptr
->flags
&= ~CHFL_BANNED
;
576 return CHFL_EXCEPTION
;
581 /* cache the banned/not banned status */
584 msptr
->bants
= chptr
->bants
;
586 if(actualBan
!= NULL
)
588 msptr
->flags
|= CHFL_BANNED
;
593 msptr
->flags
&= ~CHFL_BANNED
;
598 return ((actualBan
? CHFL_BAN
: 0));
603 * input - channel to check bans for, user to check bans against
604 * optional prebuilt buffers
605 * output - 1 if banned, else 0
609 is_quieted(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
610 const char *s
, const char *s2
)
612 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
613 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
614 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
617 struct Ban
*actualBan
= NULL
;
618 struct Ban
*actualExcept
= NULL
;
623 /* if the buffers havent been built, do it here */
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
);
632 if(who
->localClient
->mangledhost
!= NULL
)
634 /* if host mangling mode enabled, also check their real host */
635 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
637 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
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
))
644 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
649 RB_DLINK_FOREACH(ptr
, chptr
->quietlist
.head
)
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
)))
662 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
664 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
666 actualExcept
= ptr
->data
;
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
)))
675 /* cache the fact theyre not banned */
678 msptr
->bants
= chptr
->bants
;
679 msptr
->flags
&= ~CHFL_BANNED
;
682 return CHFL_EXCEPTION
;
687 /* cache the banned/not banned status */
690 msptr
->bants
= chptr
->bants
;
692 if(actualBan
!= NULL
)
694 msptr
->flags
|= CHFL_BANNED
;
699 msptr
->flags
&= ~CHFL_BANNED
;
704 return ((actualBan
? CHFL_BAN
: 0));
709 * input - client to check, channel to check for, key
710 * output - reason for not being able to join, else 0
712 * caveats - this function should only be called on a local user.
715 can_join(struct Client
*source_p
, struct Channel
*chptr
, char *key
)
717 rb_dlink_node
*invite
= NULL
;
719 struct Ban
*invex
= NULL
;
720 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
721 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
722 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
725 hook_data_channel moduledata
;
727 s_assert(source_p
->localClient
!= NULL
);
729 moduledata
.client
= source_p
;
730 moduledata
.chptr
= chptr
;
731 moduledata
.approved
= 0;
733 rb_sprintf(src_host
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
734 rb_sprintf(src_iphost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->sockhost
);
735 if(source_p
->localClient
->mangledhost
!= NULL
)
737 /* if host mangling mode enabled, also check their real host */
738 if(!strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
740 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->orighost
);
743 /* if host mangling mode not enabled and no other spoof,
744 * also check the mangled form of their host */
745 else if (!IsDynSpoof(source_p
))
747 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->localClient
->mangledhost
);
752 if((is_banned(chptr
, source_p
, NULL
, src_host
, src_iphost
)) == CHFL_BAN
)
754 moduledata
.approved
= ERR_BANNEDFROMCHAN
;
755 goto finish_join_check
;
758 if(chptr
->mode
.mode
& MODE_INVITEONLY
)
760 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
762 if(invite
->data
== chptr
)
767 if(!ConfigChannel
.use_invex
)
768 moduledata
.approved
= ERR_INVITEONLYCHAN
;
769 RB_DLINK_FOREACH(ptr
, chptr
->invexlist
.head
)
772 if(match(invex
->banstr
, src_host
)
773 || match(invex
->banstr
, src_iphost
)
774 || match_cidr(invex
->banstr
, src_iphost
)
775 || match_extban(invex
->banstr
, source_p
, chptr
, CHFL_INVEX
)
776 || (use_althost
&& match(invex
->banstr
, src_althost
)))
780 moduledata
.approved
= ERR_INVITEONLYCHAN
;
784 if(*chptr
->mode
.key
&& (EmptyString(key
) || irccmp(chptr
->mode
.key
, key
)))
785 moduledata
.approved
= ERR_BADCHANNELKEY
;
787 if(chptr
->mode
.limit
&&
788 rb_dlink_list_length(&chptr
->members
) >= (unsigned long) chptr
->mode
.limit
)
789 i
= ERR_CHANNELISFULL
;
790 if(chptr
->mode
.mode
& MODE_REGONLY
&& EmptyString(source_p
->user
->suser
))
791 i
= ERR_NEEDREGGEDNICK
;
792 /* join throttling stuff --nenolod */
793 else if(chptr
->mode
.join_num
> 0 && chptr
->mode
.join_time
> 0)
795 if ((rb_current_time() - chptr
->join_delta
<=
796 chptr
->mode
.join_time
) && (chptr
->join_count
>=
797 chptr
->mode
.join_num
))
801 /* allow /invite to override +l/+r/+j also -- jilles */
802 if (i
!= 0 && invite
== NULL
)
804 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
806 if(invite
->data
== chptr
)
810 moduledata
.approved
= i
;
814 call_hook(h_can_join
, &moduledata
);
816 return moduledata
.approved
;
821 * input - user to check in channel, membership pointer
822 * output - whether can explicitly send or not, else CAN_SEND_NONOP
826 can_send(struct Channel
*chptr
, struct Client
*source_p
, struct membership
*msptr
)
828 hook_data_channel_approval moduledata
;
830 moduledata
.approved
= CAN_SEND_NONOP
;
832 if(IsServer(source_p
) || IsService(source_p
))
835 if(MyClient(source_p
) && hash_find_resv(chptr
->chname
) &&
836 !IsOper(source_p
) && !IsExemptResv(source_p
))
837 moduledata
.approved
= CAN_SEND_NO
;
841 msptr
= find_channel_membership(chptr
, source_p
);
845 /* if its +m or +n and theyre not in the channel,
846 * they cant send. we dont check bans here because
847 * theres no possibility of caching them --fl
849 if(chptr
->mode
.mode
& MODE_NOPRIVMSGS
|| chptr
->mode
.mode
& MODE_MODERATED
)
850 moduledata
.approved
= CAN_SEND_NO
;
852 moduledata
.approved
= CAN_SEND_NONOP
;
856 if(is_chanop_voiced(msptr
))
857 moduledata
.approved
= CAN_SEND_OPV
;
859 if(chptr
->mode
.mode
& MODE_MODERATED
)
860 moduledata
.approved
= CAN_SEND_NO
;
862 if(MyClient(source_p
))
864 /* cached can_send */
865 if(msptr
->bants
== chptr
->bants
)
867 if(can_send_banned(msptr
))
868 moduledata
.approved
= CAN_SEND_NO
;
870 else if(is_banned(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
871 || is_quieted(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
)
872 moduledata
.approved
= CAN_SEND_NO
;
875 call_hook(h_can_send
, &moduledata
);
877 return moduledata
.approved
;
881 * flood_attack_channel
882 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
883 * says NOTICE must not auto reply
884 * - pointer to source Client
885 * - pointer to target channel
886 * output - 1 if target is under flood attack
887 * side effects - check for flood attack on target chptr
890 flood_attack_channel(int p_or_n
, struct Client
*source_p
, struct Channel
*chptr
, char *chname
)
894 if(GlobalSetOptions
.floodcount
&& MyClient(source_p
))
896 if((chptr
->first_received_message_time
+ 1) < rb_current_time())
898 delta
= rb_current_time() - chptr
->first_received_message_time
;
899 chptr
->received_number_of_privmsgs
-= delta
;
900 chptr
->first_received_message_time
= rb_current_time();
901 if(chptr
->received_number_of_privmsgs
<= 0)
903 chptr
->received_number_of_privmsgs
= 0;
904 chptr
->flood_noticed
= 0;
908 if((chptr
->received_number_of_privmsgs
>= GlobalSetOptions
.floodcount
)
909 || chptr
->flood_noticed
)
911 if(chptr
->flood_noticed
== 0)
913 sendto_realops_snomask(SNO_BOTS
, *chptr
->chname
== '&' ? L_ALL
: L_NETWIDE
,
914 "Possible Flooder %s[%s@%s] on %s target: %s",
915 source_p
->name
, source_p
->username
,
917 source_p
->servptr
->name
, chptr
->chname
);
918 chptr
->flood_noticed
= 1;
920 /* Add a bit of penalty */
921 chptr
->received_number_of_privmsgs
+= 2;
923 if(MyClient(source_p
) && (p_or_n
!= 1))
925 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
926 me
.name
, source_p
->name
, chptr
->chname
);
930 chptr
->received_number_of_privmsgs
++;
936 /* find_bannickchange_channel()
937 * Input: client to check
938 * Output: channel preventing nick change
941 find_bannickchange_channel(struct Client
*client_p
)
943 struct Channel
*chptr
;
944 struct membership
*msptr
;
946 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
947 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
949 if (!MyClient(client_p
))
952 rb_sprintf(src_host
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->host
);
953 rb_sprintf(src_iphost
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->sockhost
);
955 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
958 chptr
= msptr
->chptr
;
959 if (is_chanop_voiced(msptr
))
961 /* cached can_send */
962 if (msptr
->bants
== chptr
->bants
)
964 if (can_send_banned(msptr
))
967 else if (is_banned(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
968 || is_quieted(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
)
974 /* void check_spambot_warning(struct Client *source_p)
975 * Input: Client to check, channel name or NULL if this is a part.
977 * Side-effects: Updates the client's oper_warn_count_down, warns the
978 * IRC operators if necessary, and updates join_leave_countdown as
982 check_spambot_warning(struct Client
*source_p
, const char *name
)
986 if((GlobalSetOptions
.spam_num
&&
987 (source_p
->localClient
->join_leave_count
>= GlobalSetOptions
.spam_num
)))
989 if(source_p
->localClient
->oper_warn_count_down
> 0)
990 source_p
->localClient
->oper_warn_count_down
--;
992 source_p
->localClient
->oper_warn_count_down
= 0;
993 if(source_p
->localClient
->oper_warn_count_down
== 0 &&
996 /* Its already known as a possible spambot */
997 sendto_realops_snomask(SNO_BOTS
, L_NETWIDE
,
998 "User %s (%s@%s) trying to join %s is a possible spambot",
1000 source_p
->username
, source_p
->orighost
, name
);
1001 source_p
->localClient
->oper_warn_count_down
= OPER_SPAM_COUNTDOWN
;
1007 (rb_current_time() - source_p
->localClient
->last_leave_time
)) >
1008 JOIN_LEAVE_COUNT_EXPIRE_TIME
)
1010 decrement_count
= (t_delta
/ JOIN_LEAVE_COUNT_EXPIRE_TIME
);
1013 else if(decrement_count
> source_p
->localClient
->join_leave_count
)
1014 source_p
->localClient
->join_leave_count
= 0;
1016 source_p
->localClient
->join_leave_count
-= decrement_count
;
1020 if((rb_current_time() -
1021 (source_p
->localClient
->last_join_time
)) < GlobalSetOptions
.spam_time
)
1023 /* oh, its a possible spambot */
1024 source_p
->localClient
->join_leave_count
++;
1028 source_p
->localClient
->last_join_time
= rb_current_time();
1030 source_p
->localClient
->last_leave_time
= rb_current_time();
1034 /* check_splitmode()
1038 * side effects - compares usercount and servercount against their split
1039 * values and adjusts splitmode accordingly
1042 check_splitmode(void *unused
)
1044 if(splitchecking
&& (ConfigChannel
.no_join_on_split
|| ConfigChannel
.no_create_on_split
))
1046 /* not split, we're being asked to check now because someone
1051 if(eob_count
< split_servers
|| Count
.total
< split_users
)
1054 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1055 "Network split, activating splitmode");
1056 check_splitmode_ev
= rb_event_addish("check_splitmode", check_splitmode
, NULL
, 2);
1059 /* in splitmode, check whether its finished */
1060 else if(eob_count
>= split_servers
&& Count
.total
>= split_users
)
1064 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1065 "Network rejoined, deactivating splitmode");
1067 rb_event_delete(check_splitmode_ev
);
1068 check_splitmode_ev
= NULL
;
1076 * input - channel to allocate topic for
1077 * output - 1 on success, else 0
1078 * side effects - channel gets a topic allocated
1081 allocate_topic(struct Channel
*chptr
)
1088 ptr
= rb_bh_alloc(topic_heap
);
1090 /* Basically we allocate one large block for the topic and
1091 * the topic info. We then split it up into two and shove it
1095 chptr
->topic_info
= (char *) ptr
+ TOPICLEN
+ 1;
1096 *chptr
->topic
= '\0';
1097 *chptr
->topic_info
= '\0';
1102 * input - channel which has topic to free
1104 * side effects - channels topic is free'd
1107 free_topic(struct Channel
*chptr
)
1111 if(chptr
== NULL
|| chptr
->topic
== NULL
)
1114 /* This is safe for now - If you change allocate_topic you
1115 * MUST change this as well
1118 rb_bh_free(topic_heap
, ptr
);
1119 chptr
->topic
= NULL
;
1120 chptr
->topic_info
= NULL
;
1123 /* set_channel_topic()
1125 * input - channel, topic to set, topic info and topic ts
1127 * side effects - channels topic, topic info and TS are set.
1130 set_channel_topic(struct Channel
*chptr
, const char *topic
, const char *topic_info
, time_t topicts
)
1132 if(strlen(topic
) > 0)
1134 if(chptr
->topic
== NULL
)
1135 allocate_topic(chptr
);
1136 rb_strlcpy(chptr
->topic
, topic
, TOPICLEN
+ 1);
1137 rb_strlcpy(chptr
->topic_info
, topic_info
, USERHOST_REPLYLEN
);
1138 chptr
->topic_time
= topicts
;
1142 if(chptr
->topic
!= NULL
)
1144 chptr
->topic_time
= 0;
1150 * inputs - pointer to channel
1151 * - pointer to client
1152 * output - string with simple modes
1153 * side effects - result from previous calls overwritten
1155 * Stolen from ShadowIRCd 4 --nenolod
1158 channel_modes(struct Channel
*chptr
, struct Client
*client_p
)
1163 static char final
[BUFSIZE
];
1170 for (i
= 0; i
< 256; i
++)
1171 if(chptr
->mode
.mode
& chmode_flags
[i
])
1174 if(chptr
->mode
.limit
)
1178 if(!IsClient(client_p
) || IsMember(client_p
, chptr
))
1179 pbuf
+= rb_sprintf(pbuf
, " %d", chptr
->mode
.limit
);
1182 if(*chptr
->mode
.key
)
1186 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1187 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.key
);
1190 if(chptr
->mode
.join_num
)
1194 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1195 pbuf
+= rb_sprintf(pbuf
, " %d:%d", chptr
->mode
.join_num
,
1196 chptr
->mode
.join_time
);
1199 if(*chptr
->mode
.forward
&& (ConfigChannel
.use_forward
|| !IsClient(client_p
)))
1203 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1204 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.forward
);
1209 rb_strlcpy(final
, buf1
, sizeof final
);
1210 rb_strlcat(final
, buf2
, sizeof final
);
1214 /* Now lets do some stuff to keep track of what combinations of
1216 * Note that the number of combinations doubles each time you add
1217 * something to this list. Each one is only quick if no servers use that
1218 * combination, but if the numbers get too high here MODE will get too
1219 * slow. I suggest if you get more than 7 here, you consider getting rid
1220 * of some and merging or something. If it wasn't for irc+cs we would
1221 * probably not even need to bother about most of these, but unfortunately
1225 /* void init_chcap_usage_counts(void)
1229 * Side-effects - Initialises the usage counts to zero. Fills in the
1230 * chcap_yes and chcap_no combination tables.
1233 init_chcap_usage_counts(void)
1235 unsigned long m
, c
, y
, n
;
1237 memset(chcap_combos
, 0, sizeof(chcap_combos
));
1239 /* For every possible combination */
1240 for (m
= 0; m
< NCHCAP_COMBOS
; m
++)
1242 /* Check each capab */
1243 for (c
= y
= n
= 0; c
< NCHCAPS
; c
++)
1245 if((m
& (1 << c
)) == 0)
1246 n
|= channel_capabs
[c
];
1248 y
|= channel_capabs
[c
];
1250 chcap_combos
[m
].cap_yes
= y
;
1251 chcap_combos
[m
].cap_no
= n
;
1255 /* void set_chcap_usage_counts(struct Client *serv_p)
1256 * Input: serv_p; The client whose capabs to register.
1258 * Side-effects: Increments the usage counts for the correct capab
1262 set_chcap_usage_counts(struct Client
*serv_p
)
1266 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1268 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1269 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1271 chcap_combos
[n
].count
++;
1276 /* This should be impossible -A1kmm. */
1280 /* void set_chcap_usage_counts(struct Client *serv_p)
1282 * Inputs - serv_p; The client whose capabs to register.
1284 * Side-effects - Decrements the usage counts for the correct capab
1288 unset_chcap_usage_counts(struct Client
*serv_p
)
1292 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1294 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1295 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1297 /* Hopefully capabs can't change dynamically or anything... */
1298 s_assert(chcap_combos
[n
].count
> 0);
1300 if(chcap_combos
[n
].count
> 0)
1301 chcap_combos
[n
].count
--;
1306 /* This should be impossible -A1kmm. */
1310 /* void send_cap_mode_changes(struct Client *client_p,
1311 * struct Client *source_p,
1312 * struct Channel *chptr, int cap, int nocap)
1313 * Input: The client sending(client_p), the source client(source_p),
1314 * the channel to send mode changes for(chptr)
1316 * Side-effects: Sends the appropriate mode changes to capable servers.
1318 * Reverted back to my original design, except that we now keep a count
1319 * of the number of servers which each combination as an optimisation, so
1320 * the capabs combinations which are not needed are not worked out. -A1kmm
1323 send_cap_mode_changes(struct Client
*client_p
, struct Client
*source_p
,
1324 struct Channel
*chptr
, struct ChModeChange mode_changes
[], int mode_count
)
1326 static char modebuf
[BUFSIZE
];
1327 static char parabuf
[BUFSIZE
];
1328 int i
, mbl
, pbl
, nc
, mc
, preflen
, len
;
1337 /* Now send to servers... */
1338 for (j
= 0; j
< NCHCAP_COMBOS
; j
++)
1340 if(chcap_combos
[j
].count
== 0)
1350 cap
= chcap_combos
[j
].cap_yes
;
1351 nocap
= chcap_combos
[j
].cap_no
;
1353 mbl
= preflen
= rb_sprintf(modebuf
, ":%s TMODE %ld %s ",
1354 use_id(source_p
), (long) chptr
->channelts
,
1357 /* loop the list of - modes we have */
1358 for (i
= 0; i
< mode_count
; i
++)
1360 /* if they dont support the cap we need, or they do support a cap they
1361 * cant have, then dont add it to the modebuf.. that way they wont see
1364 if((mode_changes
[i
].letter
== 0) ||
1365 ((cap
& mode_changes
[i
].caps
) != mode_changes
[i
].caps
)
1366 || ((nocap
& mode_changes
[i
].nocaps
) != mode_changes
[i
].nocaps
))
1369 if(!EmptyString(mode_changes
[i
].id
))
1370 arg
= mode_changes
[i
].id
;
1372 arg
= mode_changes
[i
].arg
;
1376 arglen
= strlen(arg
);
1378 /* dont even think about it! --fl */
1379 if(arglen
> MODEBUFLEN
- 5)
1383 /* if we're creeping past the buf size, we need to send it and make
1384 * another line for the other modes
1385 * XXX - this could give away server topology with uids being
1386 * different lengths, but not much we can do, except possibly break
1387 * them as if they were the longest of the nick or uid at all times,
1388 * which even then won't work as we don't always know the uid -A1kmm.
1390 if(arg
&& ((mc
== MAXMODEPARAMSSERV
) ||
1391 ((mbl
+ pbl
+ arglen
+ 4) > (BUFSIZE
- 3))))
1394 sendto_server(client_p
, chptr
, cap
, nocap
,
1395 "%s %s", modebuf
, parabuf
);
1406 if(dir
!= mode_changes
[i
].dir
)
1408 modebuf
[mbl
++] = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1409 dir
= mode_changes
[i
].dir
;
1412 modebuf
[mbl
++] = mode_changes
[i
].letter
;
1418 len
= rb_sprintf(pbuf
, "%s ", arg
);
1425 if(pbl
&& parabuf
[pbl
- 1] == ' ')
1426 parabuf
[pbl
- 1] = 0;
1429 sendto_server(client_p
, chptr
, cap
, nocap
, "%s %s", modebuf
, parabuf
);
1434 resv_chan_forcepart(const char *name
, const char *reason
, int temp_time
)
1437 rb_dlink_node
*next_ptr
;
1438 struct Channel
*chptr
;
1439 struct membership
*msptr
;
1440 struct Client
*target_p
;
1442 if(!ConfigChannel
.resv_forcepart
)
1445 /* for each user on our server in the channel list
1446 * send them a PART, and notify opers.
1448 chptr
= find_channel(name
);
1451 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->locmembers
.head
)
1454 target_p
= msptr
->client_p
;
1456 if(IsExemptResv(target_p
))
1459 sendto_server(target_p
, chptr
, CAP_TS6
, NOCAPS
,
1460 ":%s PART %s", target_p
->id
, chptr
->chname
);
1462 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s PART %s :%s",
1463 target_p
->name
, target_p
->username
,
1464 target_p
->host
, chptr
->chname
, target_p
->name
);
1466 remove_user_from_channel(msptr
);
1468 /* notify opers & user they were removed from the channel */
1469 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1470 "Forced PART for %s!%s@%s from %s (%s)",
1471 target_p
->name
, target_p
->username
,
1472 target_p
->host
, name
, reason
);
1475 sendto_one_notice(target_p
, ":*** Channel %s is temporarily unavailable on this server.",
1478 sendto_one_notice(target_p
, ":*** Channel %s is no longer available on this server.",