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 * send_channel_join()
129 * input - channel to join, client joining.
131 * side effects - none
134 send_channel_join(struct Channel
*chptr
, struct Client
*client_p
)
136 if (!IsClient(client_p
))
139 sendto_channel_local_with_capability(ALL_MEMBERS
, NOCAPS
, CLICAP_EXTENDED_JOIN
, chptr
, ":%s!%s@%s JOIN %s",
140 client_p
->name
, client_p
->username
, client_p
->host
, chptr
->chname
);
142 sendto_channel_local_with_capability(ALL_MEMBERS
, CLICAP_EXTENDED_JOIN
, NOCAPS
, chptr
, ":%s!%s@%s JOIN %s %s %ld :%s",
143 client_p
->name
, client_p
->username
, client_p
->host
, chptr
->chname
,
144 EmptyString(client_p
->user
->suser
) ? "*" : client_p
->user
->suser
,
145 client_p
->tsinfo
, client_p
->info
);
148 /* find_channel_membership()
150 * input - channel to find them in, client to find
151 * output - membership of client in channel, else NULL
155 find_channel_membership(struct Channel
*chptr
, struct Client
*client_p
)
157 struct membership
*msptr
;
160 if(!IsClient(client_p
))
163 /* Pick the most efficient list to use to be nice to things like
164 * CHANSERV which could be in a large number of channels
166 if(rb_dlink_list_length(&chptr
->members
) < rb_dlink_list_length(&client_p
->user
->channel
))
168 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
172 if(msptr
->client_p
== client_p
)
178 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
182 if(msptr
->chptr
== chptr
)
190 /* find_channel_status()
192 * input - membership to get status for, whether we can combine flags
193 * output - flags of user on channel
197 find_channel_status(struct membership
*msptr
, int combine
)
199 static char buffer
[3];
218 /* add_user_to_channel()
220 * input - channel to add client to, client to add, channel flags
222 * side effects - user is added to channel
225 add_user_to_channel(struct Channel
*chptr
, struct Client
*client_p
, int flags
)
227 struct membership
*msptr
;
229 s_assert(client_p
->user
!= NULL
);
230 if(client_p
->user
== NULL
)
233 msptr
= rb_bh_alloc(member_heap
);
235 msptr
->chptr
= chptr
;
236 msptr
->client_p
= client_p
;
237 msptr
->flags
= flags
;
239 rb_dlinkAdd(msptr
, &msptr
->usernode
, &client_p
->user
->channel
);
240 rb_dlinkAdd(msptr
, &msptr
->channode
, &chptr
->members
);
242 if(MyClient(client_p
))
243 rb_dlinkAdd(msptr
, &msptr
->locchannode
, &chptr
->locmembers
);
246 /* remove_user_from_channel()
248 * input - membership pointer to remove from channel
250 * side effects - membership (thus user) is removed from channel
253 remove_user_from_channel(struct membership
*msptr
)
255 struct Client
*client_p
;
256 struct Channel
*chptr
;
257 s_assert(msptr
!= NULL
);
261 client_p
= msptr
->client_p
;
262 chptr
= msptr
->chptr
;
264 rb_dlinkDelete(&msptr
->usernode
, &client_p
->user
->channel
);
265 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
267 if(client_p
->servptr
== &me
)
268 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
270 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
271 destroy_channel(chptr
);
273 rb_bh_free(member_heap
, msptr
);
278 /* remove_user_from_channels()
280 * input - user to remove from all channels
282 * side effects - user is removed from all channels
285 remove_user_from_channels(struct Client
*client_p
)
287 struct Channel
*chptr
;
288 struct membership
*msptr
;
290 rb_dlink_node
*next_ptr
;
295 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->user
->channel
.head
)
298 chptr
= msptr
->chptr
;
300 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
302 if(client_p
->servptr
== &me
)
303 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
305 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
306 destroy_channel(chptr
);
308 rb_bh_free(member_heap
, msptr
);
311 client_p
->user
->channel
.head
= client_p
->user
->channel
.tail
= NULL
;
312 client_p
->user
->channel
.length
= 0;
315 /* invalidate_bancache_user()
317 * input - user to invalidate ban cache for
319 * side effects - ban cache is invalidated for all memberships of that user
320 * to be used after a nick change
323 invalidate_bancache_user(struct Client
*client_p
)
325 struct membership
*msptr
;
331 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
335 msptr
->flags
&= ~CHFL_BANNED
;
339 /* check_channel_name()
341 * input - channel name
342 * output - 1 if valid channel name, else 0
346 check_channel_name(const char *name
)
348 s_assert(name
!= NULL
);
352 for (; *name
; ++name
)
354 if(!IsChanChar(*name
))
361 /* free_channel_list()
363 * input - rb_dlink list to free
365 * side effects - list of b/e/I modes is cleared
368 free_channel_list(rb_dlink_list
* list
)
371 rb_dlink_node
*next_ptr
;
372 struct Ban
*actualBan
;
374 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
376 actualBan
= ptr
->data
;
380 list
->head
= list
->tail
= NULL
;
386 * input - channel to destroy
388 * side effects - channel is obliterated
391 destroy_channel(struct Channel
*chptr
)
393 rb_dlink_node
*ptr
, *next_ptr
;
395 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
397 del_invite(chptr
, ptr
->data
);
400 /* free all bans/exceptions/denies */
401 free_channel_list(&chptr
->banlist
);
402 free_channel_list(&chptr
->exceptlist
);
403 free_channel_list(&chptr
->invexlist
);
404 free_channel_list(&chptr
->quietlist
);
409 rb_dlinkDelete(&chptr
->node
, &global_channel_list
);
410 del_from_channel_hash(chptr
->chname
, chptr
);
414 /* channel_pub_or_secret()
417 * output - "=" if public, "@" if secret, else "*"
421 channel_pub_or_secret(struct Channel
*chptr
)
423 if(PubChannel(chptr
))
425 else if(SecretChannel(chptr
))
430 /* channel_member_names()
432 * input - channel to list, client to list to, show endofnames
434 * side effects - client is given list of users on channel
437 channel_member_names(struct Channel
*chptr
, struct Client
*client_p
, int show_eon
)
439 struct membership
*msptr
;
440 struct Client
*target_p
;
448 int stack
= IsCapable(client_p
, CLICAP_MULTI_PREFIX
);
450 if(ShowChannel(client_p
, chptr
))
452 is_member
= IsMember(client_p
, chptr
);
454 cur_len
= mlen
= rb_sprintf(lbuf
, form_str(RPL_NAMREPLY
),
455 me
.name
, client_p
->name
,
456 channel_pub_or_secret(chptr
), chptr
->chname
);
460 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
463 target_p
= msptr
->client_p
;
465 if(IsInvisible(target_p
) && !is_member
)
468 /* space, possible "@+" prefix */
469 if(cur_len
+ strlen(target_p
->name
) + 3 >= BUFSIZE
- 3)
472 sendto_one(client_p
, "%s", lbuf
);
477 tlen
= rb_sprintf(t
, "%s%s ", find_channel_status(msptr
, stack
),
484 /* The old behaviour here was to always output our buffer,
485 * even if there are no clients we can show. This happens
486 * when a client does "NAMES" with no parameters, and all
487 * the clients on a -sp channel are +i. I dont see a good
488 * reason for keeping that behaviour, as it just wastes
494 sendto_one(client_p
, "%s", lbuf
);
499 sendto_one(client_p
, form_str(RPL_ENDOFNAMES
),
500 me
.name
, client_p
->name
, chptr
->chname
);
505 * input - channel to remove invite from, client to remove
507 * side effects - user is removed from invite list, if exists
510 del_invite(struct Channel
*chptr
, struct Client
*who
)
512 rb_dlinkFindDestroy(who
, &chptr
->invites
);
513 rb_dlinkFindDestroy(chptr
, &who
->user
->invited
);
518 * input - channel to check bans for, user to check bans against
519 * optional prebuilt buffers
520 * output - 1 if banned, else 0
524 is_banned(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
525 const char *s
, const char *s2
)
527 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
528 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
529 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
532 struct Ban
*actualBan
= NULL
;
533 struct Ban
*actualExcept
= NULL
;
538 /* if the buffers havent been built, do it here */
541 rb_sprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
542 rb_sprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
547 if(who
->localClient
->mangledhost
!= NULL
)
549 /* if host mangling mode enabled, also check their real host */
550 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
552 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
555 /* if host mangling mode not enabled and no other spoof,
556 * also check the mangled form of their host */
557 else if (!IsDynSpoof(who
))
559 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
564 RB_DLINK_FOREACH(ptr
, chptr
->banlist
.head
)
566 actualBan
= ptr
->data
;
567 if(match(actualBan
->banstr
, s
) ||
568 match(actualBan
->banstr
, s2
) ||
569 match_cidr(actualBan
->banstr
, s2
) ||
570 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_BAN
) ||
571 (s3
!= NULL
&& match(actualBan
->banstr
, s3
)))
577 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
579 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
581 actualExcept
= ptr
->data
;
583 /* theyre exempted.. */
584 if(match(actualExcept
->banstr
, s
) ||
585 match(actualExcept
->banstr
, s2
) ||
586 match_cidr(actualExcept
->banstr
, s2
) ||
587 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
588 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
590 /* cache the fact theyre not banned */
593 msptr
->bants
= chptr
->bants
;
594 msptr
->flags
&= ~CHFL_BANNED
;
597 return CHFL_EXCEPTION
;
602 /* cache the banned/not banned status */
605 msptr
->bants
= chptr
->bants
;
607 if(actualBan
!= NULL
)
609 msptr
->flags
|= CHFL_BANNED
;
614 msptr
->flags
&= ~CHFL_BANNED
;
619 return ((actualBan
? CHFL_BAN
: 0));
624 * input - channel to check bans for, user to check bans against
625 * optional prebuilt buffers
626 * output - 1 if banned, else 0
630 is_quieted(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
631 const char *s
, const char *s2
)
633 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
634 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
635 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
638 struct Ban
*actualBan
= NULL
;
639 struct Ban
*actualExcept
= NULL
;
644 /* if the buffers havent been built, do it here */
647 rb_sprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
648 rb_sprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
653 if(who
->localClient
->mangledhost
!= NULL
)
655 /* if host mangling mode enabled, also check their real host */
656 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
658 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
661 /* if host mangling mode not enabled and no other spoof,
662 * also check the mangled form of their host */
663 else if (!IsDynSpoof(who
))
665 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
670 RB_DLINK_FOREACH(ptr
, chptr
->quietlist
.head
)
672 actualBan
= ptr
->data
;
673 if(match(actualBan
->banstr
, s
) ||
674 match(actualBan
->banstr
, s2
) ||
675 match_cidr(actualBan
->banstr
, s2
) ||
676 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_QUIET
) ||
677 (s3
!= NULL
&& match(actualBan
->banstr
, s3
)))
683 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
685 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
687 actualExcept
= ptr
->data
;
689 /* theyre exempted.. */
690 if(match(actualExcept
->banstr
, s
) ||
691 match(actualExcept
->banstr
, s2
) ||
692 match_cidr(actualExcept
->banstr
, s2
) ||
693 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
694 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
696 /* cache the fact theyre not banned */
699 msptr
->bants
= chptr
->bants
;
700 msptr
->flags
&= ~CHFL_BANNED
;
703 return CHFL_EXCEPTION
;
708 /* cache the banned/not banned status */
711 msptr
->bants
= chptr
->bants
;
713 if(actualBan
!= NULL
)
715 msptr
->flags
|= CHFL_BANNED
;
720 msptr
->flags
&= ~CHFL_BANNED
;
725 return ((actualBan
? CHFL_BAN
: 0));
730 * input - client to check, channel to check for, key
731 * output - reason for not being able to join, else 0
733 * caveats - this function should only be called on a local user.
736 can_join(struct Client
*source_p
, struct Channel
*chptr
, char *key
)
738 rb_dlink_node
*invite
= NULL
;
740 struct Ban
*invex
= NULL
;
741 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
742 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
743 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
746 hook_data_channel moduledata
;
748 s_assert(source_p
->localClient
!= NULL
);
750 moduledata
.client
= source_p
;
751 moduledata
.chptr
= chptr
;
752 moduledata
.approved
= 0;
754 rb_sprintf(src_host
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
755 rb_sprintf(src_iphost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->sockhost
);
756 if(source_p
->localClient
->mangledhost
!= NULL
)
758 /* if host mangling mode enabled, also check their real host */
759 if(!strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
761 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->orighost
);
764 /* if host mangling mode not enabled and no other spoof,
765 * also check the mangled form of their host */
766 else if (!IsDynSpoof(source_p
))
768 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->localClient
->mangledhost
);
773 if((is_banned(chptr
, source_p
, NULL
, src_host
, src_iphost
)) == CHFL_BAN
)
775 moduledata
.approved
= ERR_BANNEDFROMCHAN
;
776 goto finish_join_check
;
779 if(chptr
->mode
.mode
& MODE_INVITEONLY
)
781 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
783 if(invite
->data
== chptr
)
788 if(!ConfigChannel
.use_invex
)
789 moduledata
.approved
= ERR_INVITEONLYCHAN
;
790 RB_DLINK_FOREACH(ptr
, chptr
->invexlist
.head
)
793 if(match(invex
->banstr
, src_host
)
794 || match(invex
->banstr
, src_iphost
)
795 || match_cidr(invex
->banstr
, src_iphost
)
796 || match_extban(invex
->banstr
, source_p
, chptr
, CHFL_INVEX
)
797 || (use_althost
&& match(invex
->banstr
, src_althost
)))
801 moduledata
.approved
= ERR_INVITEONLYCHAN
;
805 if(*chptr
->mode
.key
&& (EmptyString(key
) || irccmp(chptr
->mode
.key
, key
)))
806 moduledata
.approved
= ERR_BADCHANNELKEY
;
808 if(chptr
->mode
.limit
&&
809 rb_dlink_list_length(&chptr
->members
) >= (unsigned long) chptr
->mode
.limit
)
810 i
= ERR_CHANNELISFULL
;
811 if(chptr
->mode
.mode
& MODE_REGONLY
&& EmptyString(source_p
->user
->suser
))
812 i
= ERR_NEEDREGGEDNICK
;
813 /* join throttling stuff --nenolod */
814 else if(chptr
->mode
.join_num
> 0 && chptr
->mode
.join_time
> 0)
816 if ((rb_current_time() - chptr
->join_delta
<=
817 chptr
->mode
.join_time
) && (chptr
->join_count
>=
818 chptr
->mode
.join_num
))
822 /* allow /invite to override +l/+r/+j also -- jilles */
823 if (i
!= 0 && invite
== NULL
)
825 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
827 if(invite
->data
== chptr
)
831 moduledata
.approved
= i
;
835 call_hook(h_can_join
, &moduledata
);
837 return moduledata
.approved
;
842 * input - user to check in channel, membership pointer
843 * output - whether can explicitly send or not, else CAN_SEND_NONOP
847 can_send(struct Channel
*chptr
, struct Client
*source_p
, struct membership
*msptr
)
849 hook_data_channel_approval moduledata
;
851 moduledata
.approved
= CAN_SEND_NONOP
;
853 if(IsServer(source_p
) || IsService(source_p
))
856 if(MyClient(source_p
) && hash_find_resv(chptr
->chname
) &&
857 !IsOper(source_p
) && !IsExemptResv(source_p
))
858 moduledata
.approved
= CAN_SEND_NO
;
862 msptr
= find_channel_membership(chptr
, source_p
);
866 /* if its +m or +n and theyre not in the channel,
867 * they cant send. we dont check bans here because
868 * theres no possibility of caching them --fl
870 if(chptr
->mode
.mode
& MODE_NOPRIVMSGS
|| chptr
->mode
.mode
& MODE_MODERATED
)
871 moduledata
.approved
= CAN_SEND_NO
;
873 moduledata
.approved
= CAN_SEND_NONOP
;
875 return moduledata
.approved
;
879 if(chptr
->mode
.mode
& MODE_MODERATED
)
880 moduledata
.approved
= CAN_SEND_NO
;
882 if(MyClient(source_p
))
884 /* cached can_send */
885 if(msptr
->bants
== chptr
->bants
)
887 if(can_send_banned(msptr
))
888 moduledata
.approved
= CAN_SEND_NO
;
890 else if(is_banned(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
891 || is_quieted(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
)
892 moduledata
.approved
= CAN_SEND_NO
;
895 if(is_chanop_voiced(msptr
))
896 moduledata
.approved
= CAN_SEND_OPV
;
898 call_hook(h_can_send
, &moduledata
);
900 return moduledata
.approved
;
904 * flood_attack_channel
905 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
906 * says NOTICE must not auto reply
907 * - pointer to source Client
908 * - pointer to target channel
909 * output - 1 if target is under flood attack
910 * side effects - check for flood attack on target chptr
913 flood_attack_channel(int p_or_n
, struct Client
*source_p
, struct Channel
*chptr
, char *chname
)
917 if(GlobalSetOptions
.floodcount
&& MyClient(source_p
))
919 if((chptr
->first_received_message_time
+ 1) < rb_current_time())
921 delta
= rb_current_time() - chptr
->first_received_message_time
;
922 chptr
->received_number_of_privmsgs
-= delta
;
923 chptr
->first_received_message_time
= rb_current_time();
924 if(chptr
->received_number_of_privmsgs
<= 0)
926 chptr
->received_number_of_privmsgs
= 0;
927 chptr
->flood_noticed
= 0;
931 if((chptr
->received_number_of_privmsgs
>= GlobalSetOptions
.floodcount
)
932 || chptr
->flood_noticed
)
934 if(chptr
->flood_noticed
== 0)
936 sendto_realops_snomask(SNO_BOTS
, *chptr
->chname
== '&' ? L_ALL
: L_NETWIDE
,
937 "Possible Flooder %s[%s@%s] on %s target: %s",
938 source_p
->name
, source_p
->username
,
940 source_p
->servptr
->name
, chptr
->chname
);
941 chptr
->flood_noticed
= 1;
943 /* Add a bit of penalty */
944 chptr
->received_number_of_privmsgs
+= 2;
946 if(MyClient(source_p
) && (p_or_n
!= 1))
948 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
949 me
.name
, source_p
->name
, chptr
->chname
);
953 chptr
->received_number_of_privmsgs
++;
959 /* find_bannickchange_channel()
960 * Input: client to check
961 * Output: channel preventing nick change
964 find_bannickchange_channel(struct Client
*client_p
)
966 struct Channel
*chptr
;
967 struct membership
*msptr
;
969 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
970 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
972 if (!MyClient(client_p
))
975 rb_sprintf(src_host
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->host
);
976 rb_sprintf(src_iphost
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->sockhost
);
978 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
981 chptr
= msptr
->chptr
;
982 if (is_chanop_voiced(msptr
))
984 /* cached can_send */
985 if (msptr
->bants
== chptr
->bants
)
987 if (can_send_banned(msptr
))
990 else if (is_banned(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
991 || is_quieted(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
)
997 /* void check_spambot_warning(struct Client *source_p)
998 * Input: Client to check, channel name or NULL if this is a part.
1000 * Side-effects: Updates the client's oper_warn_count_down, warns the
1001 * IRC operators if necessary, and updates join_leave_countdown as
1005 check_spambot_warning(struct Client
*source_p
, const char *name
)
1008 int decrement_count
;
1009 if((GlobalSetOptions
.spam_num
&&
1010 (source_p
->localClient
->join_leave_count
>= GlobalSetOptions
.spam_num
)))
1012 if(source_p
->localClient
->oper_warn_count_down
> 0)
1013 source_p
->localClient
->oper_warn_count_down
--;
1015 source_p
->localClient
->oper_warn_count_down
= 0;
1016 if(source_p
->localClient
->oper_warn_count_down
== 0 &&
1019 /* Its already known as a possible spambot */
1020 sendto_realops_snomask(SNO_BOTS
, L_NETWIDE
,
1021 "User %s (%s@%s) trying to join %s is a possible spambot",
1023 source_p
->username
, source_p
->orighost
, name
);
1024 source_p
->localClient
->oper_warn_count_down
= OPER_SPAM_COUNTDOWN
;
1030 (rb_current_time() - source_p
->localClient
->last_leave_time
)) >
1031 JOIN_LEAVE_COUNT_EXPIRE_TIME
)
1033 decrement_count
= (t_delta
/ JOIN_LEAVE_COUNT_EXPIRE_TIME
);
1036 else if(decrement_count
> source_p
->localClient
->join_leave_count
)
1037 source_p
->localClient
->join_leave_count
= 0;
1039 source_p
->localClient
->join_leave_count
-= decrement_count
;
1043 if((rb_current_time() -
1044 (source_p
->localClient
->last_join_time
)) < GlobalSetOptions
.spam_time
)
1046 /* oh, its a possible spambot */
1047 source_p
->localClient
->join_leave_count
++;
1051 source_p
->localClient
->last_join_time
= rb_current_time();
1053 source_p
->localClient
->last_leave_time
= rb_current_time();
1057 /* check_splitmode()
1061 * side effects - compares usercount and servercount against their split
1062 * values and adjusts splitmode accordingly
1065 check_splitmode(void *unused
)
1067 if(splitchecking
&& (ConfigChannel
.no_join_on_split
|| ConfigChannel
.no_create_on_split
))
1069 /* not split, we're being asked to check now because someone
1074 if(eob_count
< split_servers
|| Count
.total
< split_users
)
1077 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1078 "Network split, activating splitmode");
1079 check_splitmode_ev
= rb_event_addish("check_splitmode", check_splitmode
, NULL
, 2);
1082 /* in splitmode, check whether its finished */
1083 else if(eob_count
>= split_servers
&& Count
.total
>= split_users
)
1087 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1088 "Network rejoined, deactivating splitmode");
1090 rb_event_delete(check_splitmode_ev
);
1091 check_splitmode_ev
= NULL
;
1099 * input - channel to allocate topic for
1100 * output - 1 on success, else 0
1101 * side effects - channel gets a topic allocated
1104 allocate_topic(struct Channel
*chptr
)
1111 ptr
= rb_bh_alloc(topic_heap
);
1113 /* Basically we allocate one large block for the topic and
1114 * the topic info. We then split it up into two and shove it
1118 chptr
->topic_info
= (char *) ptr
+ TOPICLEN
+ 1;
1119 *chptr
->topic
= '\0';
1120 *chptr
->topic_info
= '\0';
1125 * input - channel which has topic to free
1127 * side effects - channels topic is free'd
1130 free_topic(struct Channel
*chptr
)
1134 if(chptr
== NULL
|| chptr
->topic
== NULL
)
1137 /* This is safe for now - If you change allocate_topic you
1138 * MUST change this as well
1141 rb_bh_free(topic_heap
, ptr
);
1142 chptr
->topic
= NULL
;
1143 chptr
->topic_info
= NULL
;
1146 /* set_channel_topic()
1148 * input - channel, topic to set, topic info and topic ts
1150 * side effects - channels topic, topic info and TS are set.
1153 set_channel_topic(struct Channel
*chptr
, const char *topic
, const char *topic_info
, time_t topicts
)
1155 if(strlen(topic
) > 0)
1157 if(chptr
->topic
== NULL
)
1158 allocate_topic(chptr
);
1159 rb_strlcpy(chptr
->topic
, topic
, TOPICLEN
+ 1);
1160 rb_strlcpy(chptr
->topic_info
, topic_info
, USERHOST_REPLYLEN
);
1161 chptr
->topic_time
= topicts
;
1165 if(chptr
->topic
!= NULL
)
1167 chptr
->topic_time
= 0;
1173 * inputs - pointer to channel
1174 * - pointer to client
1175 * output - string with simple modes
1176 * side effects - result from previous calls overwritten
1178 * Stolen from ShadowIRCd 4 --nenolod
1181 channel_modes(struct Channel
*chptr
, struct Client
*client_p
)
1186 static char final
[BUFSIZE
];
1193 for (i
= 0; i
< 256; i
++)
1194 if(chptr
->mode
.mode
& chmode_flags
[i
])
1197 if(chptr
->mode
.limit
)
1201 if(!IsClient(client_p
) || IsMember(client_p
, chptr
))
1202 pbuf
+= rb_sprintf(pbuf
, " %d", chptr
->mode
.limit
);
1205 if(*chptr
->mode
.key
)
1209 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1210 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.key
);
1213 if(chptr
->mode
.join_num
)
1217 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1218 pbuf
+= rb_sprintf(pbuf
, " %d:%d", chptr
->mode
.join_num
,
1219 chptr
->mode
.join_time
);
1222 if(*chptr
->mode
.forward
&& (ConfigChannel
.use_forward
|| !IsClient(client_p
)))
1226 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1227 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.forward
);
1232 rb_strlcpy(final
, buf1
, sizeof final
);
1233 rb_strlcat(final
, buf2
, sizeof final
);
1237 /* Now lets do some stuff to keep track of what combinations of
1239 * Note that the number of combinations doubles each time you add
1240 * something to this list. Each one is only quick if no servers use that
1241 * combination, but if the numbers get too high here MODE will get too
1242 * slow. I suggest if you get more than 7 here, you consider getting rid
1243 * of some and merging or something. If it wasn't for irc+cs we would
1244 * probably not even need to bother about most of these, but unfortunately
1248 /* void init_chcap_usage_counts(void)
1252 * Side-effects - Initialises the usage counts to zero. Fills in the
1253 * chcap_yes and chcap_no combination tables.
1256 init_chcap_usage_counts(void)
1258 unsigned long m
, c
, y
, n
;
1260 memset(chcap_combos
, 0, sizeof(chcap_combos
));
1262 /* For every possible combination */
1263 for (m
= 0; m
< NCHCAP_COMBOS
; m
++)
1265 /* Check each capab */
1266 for (c
= y
= n
= 0; c
< NCHCAPS
; c
++)
1268 if((m
& (1 << c
)) == 0)
1269 n
|= channel_capabs
[c
];
1271 y
|= channel_capabs
[c
];
1273 chcap_combos
[m
].cap_yes
= y
;
1274 chcap_combos
[m
].cap_no
= n
;
1278 /* void set_chcap_usage_counts(struct Client *serv_p)
1279 * Input: serv_p; The client whose capabs to register.
1281 * Side-effects: Increments the usage counts for the correct capab
1285 set_chcap_usage_counts(struct Client
*serv_p
)
1289 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1291 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1292 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1294 chcap_combos
[n
].count
++;
1299 /* This should be impossible -A1kmm. */
1303 /* void set_chcap_usage_counts(struct Client *serv_p)
1305 * Inputs - serv_p; The client whose capabs to register.
1307 * Side-effects - Decrements the usage counts for the correct capab
1311 unset_chcap_usage_counts(struct Client
*serv_p
)
1315 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1317 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1318 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1320 /* Hopefully capabs can't change dynamically or anything... */
1321 s_assert(chcap_combos
[n
].count
> 0);
1323 if(chcap_combos
[n
].count
> 0)
1324 chcap_combos
[n
].count
--;
1329 /* This should be impossible -A1kmm. */
1333 /* void send_cap_mode_changes(struct Client *client_p,
1334 * struct Client *source_p,
1335 * struct Channel *chptr, int cap, int nocap)
1336 * Input: The client sending(client_p), the source client(source_p),
1337 * the channel to send mode changes for(chptr)
1339 * Side-effects: Sends the appropriate mode changes to capable servers.
1341 * Reverted back to my original design, except that we now keep a count
1342 * of the number of servers which each combination as an optimisation, so
1343 * the capabs combinations which are not needed are not worked out. -A1kmm
1346 send_cap_mode_changes(struct Client
*client_p
, struct Client
*source_p
,
1347 struct Channel
*chptr
, struct ChModeChange mode_changes
[], int mode_count
)
1349 static char modebuf
[BUFSIZE
];
1350 static char parabuf
[BUFSIZE
];
1351 int i
, mbl
, pbl
, nc
, mc
, preflen
, len
;
1360 /* Now send to servers... */
1361 for (j
= 0; j
< NCHCAP_COMBOS
; j
++)
1363 if(chcap_combos
[j
].count
== 0)
1373 cap
= chcap_combos
[j
].cap_yes
;
1374 nocap
= chcap_combos
[j
].cap_no
;
1376 mbl
= preflen
= rb_sprintf(modebuf
, ":%s TMODE %ld %s ",
1377 use_id(source_p
), (long) chptr
->channelts
,
1380 /* loop the list of - modes we have */
1381 for (i
= 0; i
< mode_count
; i
++)
1383 /* if they dont support the cap we need, or they do support a cap they
1384 * cant have, then dont add it to the modebuf.. that way they wont see
1387 if((mode_changes
[i
].letter
== 0) ||
1388 ((cap
& mode_changes
[i
].caps
) != mode_changes
[i
].caps
)
1389 || ((nocap
& mode_changes
[i
].nocaps
) != mode_changes
[i
].nocaps
))
1392 if(!EmptyString(mode_changes
[i
].id
))
1393 arg
= mode_changes
[i
].id
;
1395 arg
= mode_changes
[i
].arg
;
1399 arglen
= strlen(arg
);
1401 /* dont even think about it! --fl */
1402 if(arglen
> MODEBUFLEN
- 5)
1406 /* if we're creeping past the buf size, we need to send it and make
1407 * another line for the other modes
1408 * XXX - this could give away server topology with uids being
1409 * different lengths, but not much we can do, except possibly break
1410 * them as if they were the longest of the nick or uid at all times,
1411 * which even then won't work as we don't always know the uid -A1kmm.
1413 if(arg
&& ((mc
== MAXMODEPARAMSSERV
) ||
1414 ((mbl
+ pbl
+ arglen
+ 4) > (BUFSIZE
- 3))))
1417 sendto_server(client_p
, chptr
, cap
, nocap
,
1418 "%s %s", modebuf
, parabuf
);
1429 if(dir
!= mode_changes
[i
].dir
)
1431 modebuf
[mbl
++] = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1432 dir
= mode_changes
[i
].dir
;
1435 modebuf
[mbl
++] = mode_changes
[i
].letter
;
1441 len
= rb_sprintf(pbuf
, "%s ", arg
);
1448 if(pbl
&& parabuf
[pbl
- 1] == ' ')
1449 parabuf
[pbl
- 1] = 0;
1452 sendto_server(client_p
, chptr
, cap
, nocap
, "%s %s", modebuf
, parabuf
);
1457 resv_chan_forcepart(const char *name
, const char *reason
, int temp_time
)
1460 rb_dlink_node
*next_ptr
;
1461 struct Channel
*chptr
;
1462 struct membership
*msptr
;
1463 struct Client
*target_p
;
1465 if(!ConfigChannel
.resv_forcepart
)
1468 /* for each user on our server in the channel list
1469 * send them a PART, and notify opers.
1471 chptr
= find_channel(name
);
1474 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->locmembers
.head
)
1477 target_p
= msptr
->client_p
;
1479 if(IsExemptResv(target_p
))
1482 sendto_server(target_p
, chptr
, CAP_TS6
, NOCAPS
,
1483 ":%s PART %s", target_p
->id
, chptr
->chname
);
1485 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s PART %s :%s",
1486 target_p
->name
, target_p
->username
,
1487 target_p
->host
, chptr
->chname
, target_p
->name
);
1489 remove_user_from_channel(msptr
);
1491 /* notify opers & user they were removed from the channel */
1492 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1493 "Forced PART for %s!%s@%s from %s (%s)",
1494 target_p
->name
, target_p
->username
,
1495 target_p
->host
, name
, reason
);
1498 sendto_one_notice(target_p
, ":*** Channel %s is temporarily unavailable on this server.",
1501 sendto_one_notice(target_p
, ":*** Channel %s is no longer available on this server.",