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"
46 struct config_channel_entry ConfigChannel
;
47 rb_dlink_list global_channel_list
;
48 static rb_bh
*channel_heap
;
49 static rb_bh
*ban_heap
;
50 static rb_bh
*topic_heap
;
51 static rb_bh
*member_heap
;
53 static int channel_capabs
[] = { CAP_EX
, CAP_IE
,
58 #define NCHCAPS (sizeof(channel_capabs)/sizeof(int))
59 #define NCHCAP_COMBOS (1 << NCHCAPS)
61 static struct ChCapCombo chcap_combos
[NCHCAP_COMBOS
];
63 static void free_topic(struct Channel
*chptr
);
65 static int h_can_join
;
66 static int h_can_create_channel
;
67 static int h_channel_join
;
73 * side effects - initialises the various blockheaps
78 channel_heap
= rb_bh_create(sizeof(struct Channel
), CHANNEL_HEAP_SIZE
, "channel_heap");
79 ban_heap
= rb_bh_create(sizeof(struct Ban
), BAN_HEAP_SIZE
, "ban_heap");
80 topic_heap
= rb_bh_create(TOPICLEN
+ 1 + USERHOST_REPLYLEN
, TOPIC_HEAP_SIZE
, "topic_heap");
81 member_heap
= rb_bh_create(sizeof(struct membership
), MEMBER_HEAP_SIZE
, "member_heap");
83 h_can_join
= register_hook("can_join");
84 h_channel_join
= register_hook("channel_join");
85 h_can_create_channel
= register_hook("can_create_channel");
89 * allocate_channel - Allocates a channel
92 allocate_channel(const char *chname
)
94 struct Channel
*chptr
;
95 chptr
= rb_bh_alloc(channel_heap
);
96 chptr
->chname
= rb_strdup(chname
);
101 free_channel(struct Channel
*chptr
)
103 rb_free(chptr
->chname
);
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
714 can_join(struct Client
*source_p
, struct Channel
*chptr
, char *key
)
716 rb_dlink_node
*invite
= NULL
;
718 struct Ban
*invex
= NULL
;
719 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
720 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
721 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
724 hook_data_channel moduledata
;
726 s_assert(source_p
->localClient
!= NULL
);
728 rb_sprintf(src_host
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
729 rb_sprintf(src_iphost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->sockhost
);
730 if(source_p
->localClient
->mangledhost
!= NULL
)
732 /* if host mangling mode enabled, also check their real host */
733 if(!strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
735 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->orighost
);
738 /* if host mangling mode not enabled and no other spoof,
739 * also check the mangled form of their host */
740 else if (!IsDynSpoof(source_p
))
742 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->localClient
->mangledhost
);
747 if((is_banned(chptr
, source_p
, NULL
, src_host
, src_iphost
)) == CHFL_BAN
)
748 return (ERR_BANNEDFROMCHAN
);
750 if(chptr
->mode
.mode
& MODE_INVITEONLY
)
752 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
754 if(invite
->data
== chptr
)
759 if(!ConfigChannel
.use_invex
)
760 return (ERR_INVITEONLYCHAN
);
761 RB_DLINK_FOREACH(ptr
, chptr
->invexlist
.head
)
764 if(match(invex
->banstr
, src_host
)
765 || match(invex
->banstr
, src_iphost
)
766 || match_cidr(invex
->banstr
, src_iphost
)
767 || match_extban(invex
->banstr
, source_p
, chptr
, CHFL_INVEX
)
768 || (use_althost
&& match(invex
->banstr
, src_althost
)))
772 return (ERR_INVITEONLYCHAN
);
776 if(*chptr
->mode
.key
&& (EmptyString(key
) || irccmp(chptr
->mode
.key
, key
)))
777 return (ERR_BADCHANNELKEY
);
779 if(chptr
->mode
.limit
&&
780 rb_dlink_list_length(&chptr
->members
) >= (unsigned long) chptr
->mode
.limit
)
781 i
= ERR_CHANNELISFULL
;
782 if(chptr
->mode
.mode
& MODE_REGONLY
&& EmptyString(source_p
->user
->suser
))
783 i
= ERR_NEEDREGGEDNICK
;
784 /* join throttling stuff --nenolod */
785 else if(chptr
->mode
.join_num
> 0 && chptr
->mode
.join_time
> 0)
787 if ((rb_current_time() - chptr
->join_delta
<=
788 chptr
->mode
.join_time
) && (chptr
->join_count
>=
789 chptr
->mode
.join_num
))
793 /* allow /invite to override +l/+r/+j also -- jilles */
794 if (i
!= 0 && invite
== NULL
)
796 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
798 if(invite
->data
== chptr
)
805 moduledata
.client
= source_p
;
806 moduledata
.chptr
= chptr
;
807 moduledata
.approved
= 0;
809 call_hook(h_can_join
, &moduledata
);
811 return moduledata
.approved
;
816 * input - user to check in channel, membership pointer
817 * output - whether can explicitly send or not, else CAN_SEND_NONOP
821 can_send(struct Channel
*chptr
, struct Client
*source_p
, struct membership
*msptr
)
823 if(IsServer(source_p
) || IsService(source_p
))
826 if(MyClient(source_p
) && hash_find_resv(chptr
->chname
) &&
827 !IsOper(source_p
) && !IsExemptResv(source_p
))
832 msptr
= find_channel_membership(chptr
, source_p
);
836 /* if its +m or +n and theyre not in the channel,
837 * they cant send. we dont check bans here because
838 * theres no possibility of caching them --fl
840 if(chptr
->mode
.mode
& MODE_NOPRIVMSGS
|| chptr
->mode
.mode
& MODE_MODERATED
)
843 return CAN_SEND_NONOP
;
847 if(is_chanop_voiced(msptr
))
850 if(chptr
->mode
.mode
& MODE_MODERATED
)
853 if(MyClient(source_p
))
855 /* cached can_send */
856 if(msptr
->bants
== chptr
->bants
)
858 if(can_send_banned(msptr
))
861 else if(is_banned(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
862 || is_quieted(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
)
866 return CAN_SEND_NONOP
;
869 /* find_bannickchange_channel()
870 * Input: client to check
871 * Output: channel preventing nick change
874 find_bannickchange_channel(struct Client
*client_p
)
876 struct Channel
*chptr
;
877 struct membership
*msptr
;
879 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
880 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
882 if (!MyClient(client_p
))
885 rb_sprintf(src_host
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->host
);
886 rb_sprintf(src_iphost
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->sockhost
);
888 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
891 chptr
= msptr
->chptr
;
892 if (is_chanop_voiced(msptr
))
894 /* cached can_send */
895 if (msptr
->bants
== chptr
->bants
)
897 if (can_send_banned(msptr
))
900 else if (is_banned(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
901 || is_quieted(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
)
907 /* void check_spambot_warning(struct Client *source_p)
908 * Input: Client to check, channel name or NULL if this is a part.
910 * Side-effects: Updates the client's oper_warn_count_down, warns the
911 * IRC operators if necessary, and updates join_leave_countdown as
915 check_spambot_warning(struct Client
*source_p
, const char *name
)
919 if((GlobalSetOptions
.spam_num
&&
920 (source_p
->localClient
->join_leave_count
>= GlobalSetOptions
.spam_num
)))
922 if(source_p
->localClient
->oper_warn_count_down
> 0)
923 source_p
->localClient
->oper_warn_count_down
--;
925 source_p
->localClient
->oper_warn_count_down
= 0;
926 if(source_p
->localClient
->oper_warn_count_down
== 0 &&
929 /* Its already known as a possible spambot */
930 sendto_realops_snomask(SNO_BOTS
, L_NETWIDE
,
931 "User %s (%s@%s) trying to join %s is a possible spambot",
933 source_p
->username
, source_p
->orighost
, name
);
934 source_p
->localClient
->oper_warn_count_down
= OPER_SPAM_COUNTDOWN
;
940 (rb_current_time() - source_p
->localClient
->last_leave_time
)) >
941 JOIN_LEAVE_COUNT_EXPIRE_TIME
)
943 decrement_count
= (t_delta
/ JOIN_LEAVE_COUNT_EXPIRE_TIME
);
946 else if(decrement_count
> source_p
->localClient
->join_leave_count
)
947 source_p
->localClient
->join_leave_count
= 0;
949 source_p
->localClient
->join_leave_count
-= decrement_count
;
953 if((rb_current_time() -
954 (source_p
->localClient
->last_join_time
)) < GlobalSetOptions
.spam_time
)
956 /* oh, its a possible spambot */
957 source_p
->localClient
->join_leave_count
++;
961 source_p
->localClient
->last_join_time
= rb_current_time();
963 source_p
->localClient
->last_leave_time
= rb_current_time();
971 * side effects - compares usercount and servercount against their split
972 * values and adjusts splitmode accordingly
975 check_splitmode(void *unused
)
977 if(splitchecking
&& (ConfigChannel
.no_join_on_split
|| ConfigChannel
.no_create_on_split
))
979 /* not split, we're being asked to check now because someone
984 if(eob_count
< split_servers
|| Count
.total
< split_users
)
987 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
988 "Network split, activating splitmode");
989 check_splitmode_ev
= rb_event_addish("check_splitmode", check_splitmode
, NULL
, 2);
992 /* in splitmode, check whether its finished */
993 else if(eob_count
>= split_servers
&& Count
.total
>= split_users
)
997 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
998 "Network rejoined, deactivating splitmode");
1000 rb_event_delete(check_splitmode_ev
);
1001 check_splitmode_ev
= NULL
;
1009 * input - channel to allocate topic for
1010 * output - 1 on success, else 0
1011 * side effects - channel gets a topic allocated
1014 allocate_topic(struct Channel
*chptr
)
1021 ptr
= rb_bh_alloc(topic_heap
);
1023 /* Basically we allocate one large block for the topic and
1024 * the topic info. We then split it up into two and shove it
1028 chptr
->topic_info
= (char *) ptr
+ TOPICLEN
+ 1;
1029 *chptr
->topic
= '\0';
1030 *chptr
->topic_info
= '\0';
1035 * input - channel which has topic to free
1037 * side effects - channels topic is free'd
1040 free_topic(struct Channel
*chptr
)
1044 if(chptr
== NULL
|| chptr
->topic
== NULL
)
1047 /* This is safe for now - If you change allocate_topic you
1048 * MUST change this as well
1051 rb_bh_free(topic_heap
, ptr
);
1052 chptr
->topic
= NULL
;
1053 chptr
->topic_info
= NULL
;
1056 /* set_channel_topic()
1058 * input - channel, topic to set, topic info and topic ts
1060 * side effects - channels topic, topic info and TS are set.
1063 set_channel_topic(struct Channel
*chptr
, const char *topic
, const char *topic_info
, time_t topicts
)
1065 if(strlen(topic
) > 0)
1067 if(chptr
->topic
== NULL
)
1068 allocate_topic(chptr
);
1069 rb_strlcpy(chptr
->topic
, topic
, TOPICLEN
+ 1);
1070 rb_strlcpy(chptr
->topic_info
, topic_info
, USERHOST_REPLYLEN
);
1071 chptr
->topic_time
= topicts
;
1075 if(chptr
->topic
!= NULL
)
1077 chptr
->topic_time
= 0;
1083 * inputs - pointer to channel
1084 * - pointer to client
1085 * output - string with simple modes
1086 * side effects - result from previous calls overwritten
1088 * Stolen from ShadowIRCd 4 --nenolod
1091 channel_modes(struct Channel
*chptr
, struct Client
*client_p
)
1096 static char final
[BUFSIZE
];
1103 for (i
= 0; i
< 256; i
++)
1104 if(chptr
->mode
.mode
& chmode_flags
[i
])
1107 if(chptr
->mode
.limit
)
1111 if(!IsClient(client_p
) || IsMember(client_p
, chptr
))
1112 pbuf
+= rb_sprintf(pbuf
, " %d", chptr
->mode
.limit
);
1115 if(*chptr
->mode
.key
)
1119 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1120 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.key
);
1123 if(chptr
->mode
.join_num
)
1127 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1128 pbuf
+= rb_sprintf(pbuf
, " %d:%d", chptr
->mode
.join_num
,
1129 chptr
->mode
.join_time
);
1132 if(*chptr
->mode
.forward
&& (ConfigChannel
.use_forward
|| !IsClient(client_p
)))
1136 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1137 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.forward
);
1142 rb_strlcpy(final
, buf1
, sizeof final
);
1143 rb_strlcat(final
, buf2
, sizeof final
);
1147 /* Now lets do some stuff to keep track of what combinations of
1149 * Note that the number of combinations doubles each time you add
1150 * something to this list. Each one is only quick if no servers use that
1151 * combination, but if the numbers get too high here MODE will get too
1152 * slow. I suggest if you get more than 7 here, you consider getting rid
1153 * of some and merging or something. If it wasn't for irc+cs we would
1154 * probably not even need to bother about most of these, but unfortunately
1158 /* void init_chcap_usage_counts(void)
1162 * Side-effects - Initialises the usage counts to zero. Fills in the
1163 * chcap_yes and chcap_no combination tables.
1166 init_chcap_usage_counts(void)
1168 unsigned long m
, c
, y
, n
;
1170 memset(chcap_combos
, 0, sizeof(chcap_combos
));
1172 /* For every possible combination */
1173 for (m
= 0; m
< NCHCAP_COMBOS
; m
++)
1175 /* Check each capab */
1176 for (c
= y
= n
= 0; c
< NCHCAPS
; c
++)
1178 if((m
& (1 << c
)) == 0)
1179 n
|= channel_capabs
[c
];
1181 y
|= channel_capabs
[c
];
1183 chcap_combos
[m
].cap_yes
= y
;
1184 chcap_combos
[m
].cap_no
= n
;
1188 /* void set_chcap_usage_counts(struct Client *serv_p)
1189 * Input: serv_p; The client whose capabs to register.
1191 * Side-effects: Increments the usage counts for the correct capab
1195 set_chcap_usage_counts(struct Client
*serv_p
)
1199 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1201 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1202 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1204 chcap_combos
[n
].count
++;
1209 /* This should be impossible -A1kmm. */
1213 /* void set_chcap_usage_counts(struct Client *serv_p)
1215 * Inputs - serv_p; The client whose capabs to register.
1217 * Side-effects - Decrements the usage counts for the correct capab
1221 unset_chcap_usage_counts(struct Client
*serv_p
)
1225 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1227 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1228 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1230 /* Hopefully capabs can't change dynamically or anything... */
1231 s_assert(chcap_combos
[n
].count
> 0);
1233 if(chcap_combos
[n
].count
> 0)
1234 chcap_combos
[n
].count
--;
1239 /* This should be impossible -A1kmm. */
1243 /* void send_cap_mode_changes(struct Client *client_p,
1244 * struct Client *source_p,
1245 * struct Channel *chptr, int cap, int nocap)
1246 * Input: The client sending(client_p), the source client(source_p),
1247 * the channel to send mode changes for(chptr)
1249 * Side-effects: Sends the appropriate mode changes to capable servers.
1251 * Reverted back to my original design, except that we now keep a count
1252 * of the number of servers which each combination as an optimisation, so
1253 * the capabs combinations which are not needed are not worked out. -A1kmm
1256 send_cap_mode_changes(struct Client
*client_p
, struct Client
*source_p
,
1257 struct Channel
*chptr
, struct ChModeChange mode_changes
[], int mode_count
)
1259 static char modebuf
[BUFSIZE
];
1260 static char parabuf
[BUFSIZE
];
1261 int i
, mbl
, pbl
, nc
, mc
, preflen
, len
;
1270 /* Now send to servers... */
1271 for (j
= 0; j
< NCHCAP_COMBOS
; j
++)
1273 if(chcap_combos
[j
].count
== 0)
1283 cap
= chcap_combos
[j
].cap_yes
;
1284 nocap
= chcap_combos
[j
].cap_no
;
1286 mbl
= preflen
= rb_sprintf(modebuf
, ":%s TMODE %ld %s ",
1287 use_id(source_p
), (long) chptr
->channelts
,
1290 /* loop the list of - modes we have */
1291 for (i
= 0; i
< mode_count
; i
++)
1293 /* if they dont support the cap we need, or they do support a cap they
1294 * cant have, then dont add it to the modebuf.. that way they wont see
1297 if((mode_changes
[i
].letter
== 0) ||
1298 ((cap
& mode_changes
[i
].caps
) != mode_changes
[i
].caps
)
1299 || ((nocap
& mode_changes
[i
].nocaps
) != mode_changes
[i
].nocaps
))
1302 if(!EmptyString(mode_changes
[i
].id
))
1303 arg
= mode_changes
[i
].id
;
1305 arg
= mode_changes
[i
].arg
;
1309 arglen
= strlen(arg
);
1311 /* dont even think about it! --fl */
1312 if(arglen
> MODEBUFLEN
- 5)
1316 /* if we're creeping past the buf size, we need to send it and make
1317 * another line for the other modes
1318 * XXX - this could give away server topology with uids being
1319 * different lengths, but not much we can do, except possibly break
1320 * them as if they were the longest of the nick or uid at all times,
1321 * which even then won't work as we don't always know the uid -A1kmm.
1323 if(arg
&& ((mc
== MAXMODEPARAMSSERV
) ||
1324 ((mbl
+ pbl
+ arglen
+ 4) > (BUFSIZE
- 3))))
1327 sendto_server(client_p
, chptr
, cap
, nocap
,
1328 "%s %s", modebuf
, parabuf
);
1339 if(dir
!= mode_changes
[i
].dir
)
1341 modebuf
[mbl
++] = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1342 dir
= mode_changes
[i
].dir
;
1345 modebuf
[mbl
++] = mode_changes
[i
].letter
;
1351 len
= rb_sprintf(pbuf
, "%s ", arg
);
1358 if(pbl
&& parabuf
[pbl
- 1] == ' ')
1359 parabuf
[pbl
- 1] = 0;
1362 sendto_server(client_p
, chptr
, cap
, nocap
, "%s %s", modebuf
, parabuf
);
1366 /* Check what we will forward to, without sending any notices to the user
1370 check_forward(struct Client
*source_p
, struct Channel
*chptr
,
1376 if (IsNoForward(source_p
))
1381 chptr
= find_channel(chptr
->mode
.forward
);
1382 /* Can only forward to existing channels */
1385 /* Already on there, show original error message */
1386 if (IsMember(source_p
, chptr
))
1388 /* Juped. Sending a warning notice would be unfair */
1389 if (hash_find_resv(chptr
->chname
))
1391 /* Don't forward to +Q channel */
1392 if (chptr
->mode
.mode
& MODE_DISFORWARD
)
1394 i
= can_join(source_p
, chptr
, key
);
1397 if (i
!= ERR_INVITEONLYCHAN
&& i
!= ERR_NEEDREGGEDNICK
&& i
!= ERR_THROTTLE
&& i
!= ERR_CHANNELISFULL
)
1408 * inputs - pointer to client doing join 0
1410 * side effects - Use has decided to join 0. This is legacy
1411 * from the days when channels were numbers not names. *sigh*
1414 do_join_0(struct Client
*client_p
, struct Client
*source_p
)
1416 struct membership
*msptr
;
1417 struct Channel
*chptr
= NULL
;
1420 /* Finish the flood grace period... */
1421 if(MyClient(source_p
) && !IsFloodDone(source_p
))
1422 flood_endgrace(source_p
);
1424 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s JOIN 0", use_id(source_p
));
1426 while((ptr
= source_p
->user
->channel
.head
))
1428 if(source_p
->user
->channel
.head
&& MyConnect(source_p
) &&
1429 !IsOper(source_p
) && !IsExemptSpambot(source_p
))
1430 check_spambot_warning(source_p
, NULL
);
1433 chptr
= msptr
->chptr
;
1434 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s PART %s",
1436 source_p
->username
, source_p
->host
, chptr
->chname
);
1437 remove_user_from_channel(msptr
);
1442 check_channel_name_loc(struct Client
*source_p
, const char *name
)
1446 s_assert(name
!= NULL
);
1447 if(EmptyString(name
))
1450 if(ConfigFileEntry
.disable_fake_channels
&& !IsOper(source_p
))
1452 for(p
= name
; *p
; ++p
)
1454 if(!IsChanChar(*p
) || IsFakeChanChar(*p
))
1460 for(p
= name
; *p
; ++p
)
1467 if(ConfigChannel
.only_ascii_channels
)
1469 for(p
= name
; *p
; ++p
)
1470 if(*p
< 33 || *p
> 126)
1478 void user_join(struct Client
* client_p
, struct Client
* source_p
, const char * channels
, const char * keys
)
1480 static char jbuf
[BUFSIZE
];
1481 struct Channel
*chptr
= NULL
;
1482 struct ConfItem
*aconf
;
1487 char *p
= NULL
, *p2
= NULL
;
1493 if(channels
== NULL
)
1496 /* rebuild the list of channels theyre supposed to be joining.
1497 * this code has a side effect of losing keys, but..
1499 chanlist
= LOCAL_COPY(channels
);
1500 for(name
= rb_strtok_r(chanlist
, ",", &p
); name
; name
= rb_strtok_r(NULL
, ",", &p
))
1502 /* check the length and name of channel is ok */
1503 if(!check_channel_name_loc(source_p
, name
) || (strlen(name
) > LOC_CHANNELLEN
))
1505 sendto_one_numeric(source_p
, ERR_BADCHANNAME
,
1506 form_str(ERR_BADCHANNAME
), (unsigned char *) name
);
1510 /* join 0 parts all channels */
1511 if(*name
== '0' && (name
[1] == ',' || name
[1] == '\0') && name
== chanlist
)
1513 (void) strcpy(jbuf
, "0");
1517 /* check it begins with # or &, and local chans are disabled */
1518 else if(!IsChannelName(name
))
1520 sendto_one_numeric(source_p
, ERR_NOSUCHCHANNEL
,
1521 form_str(ERR_NOSUCHCHANNEL
), name
);
1525 /* see if its resv'd */
1526 if(!IsExemptResv(source_p
) && (aconf
= hash_find_resv(name
)))
1528 sendto_one_numeric(source_p
, ERR_BADCHANNAME
,
1529 form_str(ERR_BADCHANNAME
), name
);
1531 /* dont warn for opers */
1532 if(!IsExemptJupe(source_p
) && !IsOper(source_p
))
1533 sendto_realops_snomask(SNO_SPY
, L_NETWIDE
,
1534 "User %s (%s@%s) is attempting to join locally juped channel %s (%s)",
1535 source_p
->name
, source_p
->username
,
1536 source_p
->orighost
, name
, aconf
->passwd
);
1537 /* dont update tracking for jupe exempt users, these
1538 * are likely to be spamtrap leaves
1540 else if(IsExemptJupe(source_p
))
1546 if(splitmode
&& !IsOper(source_p
) && (*name
!= '&') &&
1547 ConfigChannel
.no_join_on_split
)
1549 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
1550 me
.name
, source_p
->name
, name
);
1555 (void) strcat(jbuf
, ",");
1556 (void) rb_strlcat(jbuf
, name
, sizeof(jbuf
));
1561 mykey
= LOCAL_COPY(keys
);
1562 key
= rb_strtok_r(mykey
, ",", &p2
);
1565 for(name
= rb_strtok_r(jbuf
, ",", &p
); name
;
1566 key
= (key
) ? rb_strtok_r(NULL
, ",", &p2
) : NULL
, name
= rb_strtok_r(NULL
, ",", &p
))
1568 hook_data_channel_activity hook_info
;
1570 /* JOIN 0 simply parts all channels the user is in */
1571 if(*name
== '0' && !atoi(name
))
1573 if(source_p
->user
->channel
.head
== NULL
)
1576 do_join_0(&me
, source_p
);
1580 /* look for the channel */
1581 if((chptr
= find_channel(name
)) != NULL
)
1583 if(IsMember(source_p
, chptr
))
1590 hook_data_client_approval moduledata
;
1592 moduledata
.client
= source_p
;
1593 moduledata
.approved
= 0;
1595 call_hook(h_can_create_channel
, &moduledata
);
1597 if(moduledata
.approved
!= 0)
1599 sendto_one(source_p
, form_str(moduledata
.approved
),
1600 me
.name
, source_p
->name
, name
);
1604 if(splitmode
&& !IsOper(source_p
) && (*name
!= '&') &&
1605 ConfigChannel
.no_create_on_split
)
1607 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
1608 me
.name
, source_p
->name
, name
);
1612 flags
= CHFL_CHANOP
;
1615 if((rb_dlink_list_length(&source_p
->user
->channel
) >=
1616 (unsigned long) ConfigChannel
.max_chans_per_user
) &&
1617 (!IsOper(source_p
) ||
1618 (rb_dlink_list_length(&source_p
->user
->channel
) >=
1619 (unsigned long) ConfigChannel
.max_chans_per_user
* 3)))
1621 sendto_one(source_p
, form_str(ERR_TOOMANYCHANNELS
),
1622 me
.name
, source_p
->name
, name
);
1626 if(chptr
== NULL
) /* If I already have a chptr, no point doing this */
1628 chptr
= get_or_create_channel(source_p
, name
, NULL
);
1632 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
1633 me
.name
, source_p
->name
, name
);
1638 /* can_join checks for +i key, bans etc */
1639 if((i
= can_join(source_p
, chptr
, key
)))
1641 if ((i
!= ERR_NEEDREGGEDNICK
&& i
!= ERR_THROTTLE
&& i
!= ERR_INVITEONLYCHAN
&& i
!= ERR_CHANNELISFULL
) ||
1642 (!ConfigChannel
.use_forward
|| (chptr
= check_forward(source_p
, chptr
, key
)) == NULL
))
1644 /* might be wrong, but is there any other better location for such?
1645 * see extensions/chm_operonly.c for other comments on this
1649 sendto_one(source_p
, form_str(i
), me
.name
, source_p
->name
, name
);
1654 sendto_one_numeric(source_p
, ERR_LINKCHANNEL
, form_str(ERR_LINKCHANNEL
), name
, chptr
->chname
);
1658 !IsOper(source_p
) && !IsExemptSpambot(source_p
))
1659 check_spambot_warning(source_p
, name
);
1661 /* add the user to the channel */
1662 add_user_to_channel(chptr
, source_p
, flags
);
1663 if (chptr
->mode
.join_num
&&
1664 rb_current_time() - chptr
->join_delta
>= chptr
->mode
.join_time
)
1666 chptr
->join_count
= 0;
1667 chptr
->join_delta
= rb_current_time();
1669 chptr
->join_count
++;
1671 /* we send the user their join here, because we could have to
1672 * send a mode out next.
1674 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s JOIN :%s",
1676 source_p
->username
, source_p
->host
, chptr
->chname
);
1678 /* its a new channel, set +nt and burst. */
1679 if(flags
& CHFL_CHANOP
)
1681 chptr
->channelts
= rb_current_time();
1682 chptr
->mode
.mode
|= MODE_TOPICLIMIT
;
1683 chptr
->mode
.mode
|= MODE_NOPRIVMSGS
;
1684 modes
= channel_modes(chptr
, &me
);
1686 sendto_channel_local(ONLY_CHANOPS
, chptr
, ":%s MODE %s %s",
1687 me
.name
, chptr
->chname
, modes
);
1689 sendto_server(client_p
, chptr
, CAP_TS6
, NOCAPS
,
1690 ":%s SJOIN %ld %s %s :@%s",
1691 me
.id
, (long) chptr
->channelts
,
1692 chptr
->chname
, modes
, source_p
->id
);
1696 sendto_server(client_p
, chptr
, CAP_TS6
, NOCAPS
,
1697 ":%s JOIN %ld %s +",
1698 use_id(source_p
), (long) chptr
->channelts
,
1702 del_invite(chptr
, source_p
);
1704 if(chptr
->topic
!= NULL
)
1706 sendto_one(source_p
, form_str(RPL_TOPIC
), me
.name
,
1707 source_p
->name
, chptr
->chname
, chptr
->topic
);
1709 sendto_one(source_p
, form_str(RPL_TOPICWHOTIME
),
1710 me
.name
, source_p
->name
, chptr
->chname
,
1711 chptr
->topic_info
, chptr
->topic_time
);
1714 channel_member_names(chptr
, source_p
, 1);
1716 hook_info
.client
= source_p
;
1717 hook_info
.chptr
= chptr
;
1718 hook_info
.key
= key
;
1719 call_hook(h_channel_join
, &hook_info
);