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
34 #include "s_serv.h" /* captab */
38 #include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
39 #include "s_newconf.h"
43 struct config_channel_entry ConfigChannel
;
44 rb_dlink_list global_channel_list
;
45 static rb_bh
*channel_heap
;
46 static rb_bh
*ban_heap
;
47 static rb_bh
*topic_heap
;
48 static rb_bh
*member_heap
;
50 static void free_topic(struct Channel
*chptr
);
52 static int h_can_join
;
53 static int h_can_send
;
54 int h_get_channel_access
;
60 * side effects - initialises the various blockheaps
65 channel_heap
= rb_bh_create(sizeof(struct Channel
), CHANNEL_HEAP_SIZE
, "channel_heap");
66 ban_heap
= rb_bh_create(sizeof(struct Ban
), BAN_HEAP_SIZE
, "ban_heap");
67 topic_heap
= rb_bh_create(TOPICLEN
+ 1 + USERHOST_REPLYLEN
, TOPIC_HEAP_SIZE
, "topic_heap");
68 member_heap
= rb_bh_create(sizeof(struct membership
), MEMBER_HEAP_SIZE
, "member_heap");
70 h_can_join
= register_hook("can_join");
71 h_can_send
= register_hook("can_send");
72 h_get_channel_access
= register_hook("get_channel_access");
76 * allocate_channel - Allocates a channel
79 allocate_channel(const char *chname
)
81 struct Channel
*chptr
;
82 chptr
= rb_bh_alloc(channel_heap
);
83 chptr
->chname
= rb_strdup(chname
);
88 free_channel(struct Channel
*chptr
)
90 rb_free(chptr
->chname
);
91 rb_free(chptr
->mode_lock
);
92 rb_bh_free(channel_heap
, chptr
);
96 allocate_ban(const char *banstr
, const char *who
, const char *forward
)
99 bptr
= rb_bh_alloc(ban_heap
);
100 bptr
->banstr
= rb_strdup(banstr
);
101 bptr
->who
= rb_strdup(who
);
102 bptr
->forward
= forward
? rb_strdup(forward
) : NULL
;
108 free_ban(struct Ban
*bptr
)
110 rb_free(bptr
->banstr
);
112 rb_free(bptr
->forward
);
113 rb_bh_free(ban_heap
, bptr
);
117 * send_channel_join()
119 * input - channel to join, client joining.
121 * side effects - none
124 send_channel_join(struct Channel
*chptr
, struct Client
*client_p
)
126 if (!IsClient(client_p
))
129 sendto_channel_local_with_capability(client_p
, ALL_MEMBERS
, NOCAPS
, CLICAP_EXTENDED_JOIN
, chptr
, ":%s!%s@%s JOIN %s",
130 client_p
->name
, client_p
->username
, client_p
->host
, chptr
->chname
);
132 sendto_channel_local_with_capability(client_p
, ALL_MEMBERS
, CLICAP_EXTENDED_JOIN
, NOCAPS
, chptr
, ":%s!%s@%s JOIN %s %s :%s",
133 client_p
->name
, client_p
->username
, client_p
->host
, chptr
->chname
,
134 EmptyString(client_p
->user
->suser
) ? "*" : client_p
->user
->suser
,
137 /* Send away message to away-notify enabled clients. */
138 if (client_p
->user
->away
)
139 sendto_channel_local_with_capability_butone(client_p
, ALL_MEMBERS
, CLICAP_AWAY_NOTIFY
, NOCAPS
, chptr
,
140 ":%s!%s@%s AWAY :%s", client_p
->name
, client_p
->username
,
141 client_p
->host
, client_p
->user
->away
);
144 /* find_channel_membership()
146 * input - channel to find them in, client to find
147 * output - membership of client in channel, else NULL
151 find_channel_membership(struct Channel
*chptr
, struct Client
*client_p
)
153 struct membership
*msptr
;
156 if(!IsClient(client_p
))
159 /* Pick the most efficient list to use to be nice to things like
160 * CHANSERV which could be in a large number of channels
162 if(rb_dlink_list_length(&chptr
->members
) < rb_dlink_list_length(&client_p
->user
->channel
))
164 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
168 if(msptr
->client_p
== client_p
)
174 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
178 if(msptr
->chptr
== chptr
)
186 /* find_channel_status()
188 * input - membership to get status for, whether we can combine flags
189 * output - flags of user on channel
193 find_channel_status(struct membership
*msptr
, int combine
)
195 static char buffer
[3];
214 /* add_user_to_channel()
216 * input - channel to add client to, client to add, channel flags
218 * side effects - user is added to channel
221 add_user_to_channel(struct Channel
*chptr
, struct Client
*client_p
, int flags
)
223 struct membership
*msptr
;
225 s_assert(client_p
->user
!= NULL
);
226 if(client_p
->user
== NULL
)
229 msptr
= rb_bh_alloc(member_heap
);
231 msptr
->chptr
= chptr
;
232 msptr
->client_p
= client_p
;
233 msptr
->flags
= flags
;
235 rb_dlinkAdd(msptr
, &msptr
->usernode
, &client_p
->user
->channel
);
236 rb_dlinkAdd(msptr
, &msptr
->channode
, &chptr
->members
);
238 if(MyClient(client_p
))
239 rb_dlinkAdd(msptr
, &msptr
->locchannode
, &chptr
->locmembers
);
242 /* remove_user_from_channel()
244 * input - membership pointer to remove from channel
246 * side effects - membership (thus user) is removed from channel
249 remove_user_from_channel(struct membership
*msptr
)
251 struct Client
*client_p
;
252 struct Channel
*chptr
;
253 s_assert(msptr
!= NULL
);
257 client_p
= msptr
->client_p
;
258 chptr
= msptr
->chptr
;
260 rb_dlinkDelete(&msptr
->usernode
, &client_p
->user
->channel
);
261 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
263 if(client_p
->servptr
== &me
)
264 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
266 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
267 destroy_channel(chptr
);
269 rb_bh_free(member_heap
, msptr
);
274 /* remove_user_from_channels()
276 * input - user to remove from all channels
278 * side effects - user is removed from all channels
281 remove_user_from_channels(struct Client
*client_p
)
283 struct Channel
*chptr
;
284 struct membership
*msptr
;
286 rb_dlink_node
*next_ptr
;
291 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->user
->channel
.head
)
294 chptr
= msptr
->chptr
;
296 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
298 if(client_p
->servptr
== &me
)
299 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
301 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
302 destroy_channel(chptr
);
304 rb_bh_free(member_heap
, msptr
);
307 client_p
->user
->channel
.head
= client_p
->user
->channel
.tail
= NULL
;
308 client_p
->user
->channel
.length
= 0;
311 /* invalidate_bancache_user()
313 * input - user to invalidate ban cache for
315 * side effects - ban cache is invalidated for all memberships of that user
316 * to be used after a nick change
319 invalidate_bancache_user(struct Client
*client_p
)
321 struct membership
*msptr
;
327 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
331 msptr
->flags
&= ~CHFL_BANNED
;
335 /* check_channel_name()
337 * input - channel name
338 * output - true if valid channel name, else false
342 check_channel_name(const char *name
)
344 s_assert(name
!= NULL
);
348 for (; *name
; ++name
)
350 if(!IsChanChar(*name
))
357 /* free_channel_list()
359 * input - rb_dlink list to free
361 * side effects - list of b/e/I modes is cleared
364 free_channel_list(rb_dlink_list
* list
)
367 rb_dlink_node
*next_ptr
;
368 struct Ban
*actualBan
;
370 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
372 actualBan
= ptr
->data
;
376 list
->head
= list
->tail
= NULL
;
382 * input - channel to destroy
384 * side effects - channel is obliterated
387 destroy_channel(struct Channel
*chptr
)
389 rb_dlink_node
*ptr
, *next_ptr
;
391 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
393 del_invite(chptr
, ptr
->data
);
396 /* free all bans/exceptions/denies */
397 free_channel_list(&chptr
->banlist
);
398 free_channel_list(&chptr
->exceptlist
);
399 free_channel_list(&chptr
->invexlist
);
400 free_channel_list(&chptr
->quietlist
);
405 rb_dlinkDelete(&chptr
->node
, &global_channel_list
);
406 del_from_channel_hash(chptr
->chname
, chptr
);
410 /* channel_pub_or_secret()
413 * output - "=" if public, "@" if secret, else "*"
417 channel_pub_or_secret(struct Channel
*chptr
)
419 if(PubChannel(chptr
))
421 else if(SecretChannel(chptr
))
426 /* channel_member_names()
428 * input - channel to list, client to list to, show endofnames
430 * side effects - client is given list of users on channel
433 channel_member_names(struct Channel
*chptr
, struct Client
*client_p
, int show_eon
)
435 struct membership
*msptr
;
436 struct Client
*target_p
;
444 int stack
= IsCapable(client_p
, CLICAP_MULTI_PREFIX
);
446 if(ShowChannel(client_p
, chptr
))
448 is_member
= IsMember(client_p
, chptr
);
450 cur_len
= mlen
= sprintf(lbuf
, form_str(RPL_NAMREPLY
),
451 me
.name
, client_p
->name
,
452 channel_pub_or_secret(chptr
), chptr
->chname
);
456 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
459 target_p
= msptr
->client_p
;
461 if(IsInvisible(target_p
) && !is_member
)
464 if (IsCapable(client_p
, CLICAP_USERHOST_IN_NAMES
))
466 /* space, possible "@+" prefix */
467 if (cur_len
+ strlen(target_p
->name
) + strlen(target_p
->username
) + strlen(target_p
->host
) + 5 >= BUFSIZE
- 5)
470 sendto_one(client_p
, "%s", lbuf
);
475 tlen
= sprintf(t
, "%s%s!%s@%s ", find_channel_status(msptr
, stack
),
476 target_p
->name
, target_p
->username
, target_p
->host
);
480 /* space, possible "@+" prefix */
481 if(cur_len
+ strlen(target_p
->name
) + 3 >= BUFSIZE
- 3)
484 sendto_one(client_p
, "%s", lbuf
);
489 tlen
= sprintf(t
, "%s%s ", find_channel_status(msptr
, stack
),
497 /* The old behaviour here was to always output our buffer,
498 * even if there are no clients we can show. This happens
499 * when a client does "NAMES" with no parameters, and all
500 * the clients on a -sp channel are +i. I dont see a good
501 * reason for keeping that behaviour, as it just wastes
507 sendto_one(client_p
, "%s", lbuf
);
512 sendto_one(client_p
, form_str(RPL_ENDOFNAMES
),
513 me
.name
, client_p
->name
, chptr
->chname
);
518 * input - channel to remove invite from, client to remove
520 * side effects - user is removed from invite list, if exists
523 del_invite(struct Channel
*chptr
, struct Client
*who
)
525 rb_dlinkFindDestroy(who
, &chptr
->invites
);
526 rb_dlinkFindDestroy(chptr
, &who
->user
->invited
);
531 * input - channel to check bans for, ban list (banlist or quietlist),
532 * user to check bans against, optional prebuilt buffers,
533 * optional forward channel pointer
534 * output - 1 if banned, else 0
538 is_banned_list(struct Channel
*chptr
, rb_dlink_list
*list
,
539 struct Client
*who
, struct membership
*msptr
,
540 const char *s
, const char *s2
, const char **forward
)
542 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
543 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
544 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
545 char src_ip4host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
548 struct sockaddr_in ip4
;
550 struct Ban
*actualBan
= NULL
;
551 struct Ban
*actualExcept
= NULL
;
556 /* if the buffers havent been built, do it here */
559 sprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
560 sprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
565 if(who
->localClient
->mangledhost
!= NULL
)
567 /* if host mangling mode enabled, also check their real host */
568 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
570 sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
573 /* if host mangling mode not enabled and no other spoof,
574 * also check the mangled form of their host */
575 else if (!IsDynSpoof(who
))
577 sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
581 if(GET_SS_FAMILY(&who
->localClient
->ip
) == AF_INET6
&&
582 rb_ipv4_from_ipv6((const struct sockaddr_in6
*)&who
->localClient
->ip
, &ip4
))
584 sprintf(src_ip4host
, "%s!%s@", who
->name
, who
->username
);
585 s4
= src_ip4host
+ strlen(src_ip4host
);
586 rb_inet_ntop_sock((struct sockaddr
*)&ip4
,
587 s4
, src_ip4host
+ sizeof src_ip4host
- s4
);
591 RB_DLINK_FOREACH(ptr
, list
->head
)
593 actualBan
= ptr
->data
;
594 if(match(actualBan
->banstr
, s
) ||
595 match(actualBan
->banstr
, s2
) ||
596 match_cidr(actualBan
->banstr
, s2
) ||
597 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_BAN
) ||
598 (s3
!= NULL
&& match(actualBan
->banstr
, s3
)) ||
599 (s4
!= NULL
&& (match(actualBan
->banstr
, s4
) || match_cidr(actualBan
->banstr
, s4
)))
606 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
608 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
610 actualExcept
= ptr
->data
;
612 /* theyre exempted.. */
613 if(match(actualExcept
->banstr
, s
) ||
614 match(actualExcept
->banstr
, s2
) ||
615 match_cidr(actualExcept
->banstr
, s2
) ||
616 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
617 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
619 /* cache the fact theyre not banned */
622 msptr
->bants
= chptr
->bants
;
623 msptr
->flags
&= ~CHFL_BANNED
;
626 return CHFL_EXCEPTION
;
631 /* cache the banned/not banned status */
634 msptr
->bants
= chptr
->bants
;
636 if(actualBan
!= NULL
)
638 msptr
->flags
|= CHFL_BANNED
;
643 msptr
->flags
&= ~CHFL_BANNED
;
648 if (actualBan
&& actualBan
->forward
&& forward
)
649 *forward
= actualBan
->forward
;
651 return ((actualBan
? CHFL_BAN
: 0));
656 * input - channel to check bans for, user to check bans against
657 * optional prebuilt buffers, optional forward channel pointer
658 * output - 1 if banned, else 0
662 is_banned(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
663 const char *s
, const char *s2
, const char **forward
)
666 if (chptr
->last_checked_client
!= NULL
&&
667 who
== chptr
->last_checked_client
&&
668 chptr
->last_checked_type
== CHFL_BAN
&&
669 chptr
->last_checked_ts
> chptr
->bants
)
670 return chptr
->last_checked_result
;
672 chptr
->last_checked_client
= who
;
673 chptr
->last_checked_type
= CHFL_BAN
;
674 chptr
->last_checked_result
= is_banned_list(chptr
, &chptr
->banlist
, who
, msptr
, s
, s2
, forward
);
675 chptr
->last_checked_ts
= rb_current_time();
677 return chptr
->last_checked_result
;
679 return is_banned_list(chptr
, &chptr
->banlist
, who
, msptr
, s
, s2
, forward
);
685 * input - channel to check bans for, user to check bans against
686 * optional prebuilt buffers
687 * output - 1 if banned, else 0
691 is_quieted(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
692 const char *s
, const char *s2
)
695 if (chptr
->last_checked_client
!= NULL
&&
696 who
== chptr
->last_checked_client
&&
697 chptr
->last_checked_type
== CHFL_QUIET
&&
698 chptr
->last_checked_ts
> chptr
->bants
)
699 return chptr
->last_checked_result
;
701 chptr
->last_checked_client
= who
;
702 chptr
->last_checked_type
= CHFL_QUIET
;
703 chptr
->last_checked_result
= is_banned_list(chptr
, &chptr
->quietlist
, who
, msptr
, s
, s2
, NULL
);
704 chptr
->last_checked_ts
= rb_current_time();
706 return chptr
->last_checked_result
;
708 return is_banned_list(chptr
, &chptr
->quietlist
, who
, msptr
, s
, s2
, NULL
);
714 * input - client to check, channel to check for, key
715 * output - reason for not being able to join, else 0, channel name to forward to
717 * caveats - this function should only be called on a local user.
720 can_join(struct Client
*source_p
, struct Channel
*chptr
, const char *key
, const char **forward
)
722 rb_dlink_node
*invite
= NULL
;
724 struct Ban
*invex
= NULL
;
725 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
726 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
727 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
730 hook_data_channel moduledata
;
732 s_assert(source_p
->localClient
!= NULL
);
734 moduledata
.client
= source_p
;
735 moduledata
.chptr
= chptr
;
736 moduledata
.approved
= 0;
738 sprintf(src_host
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
739 sprintf(src_iphost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->sockhost
);
740 if(source_p
->localClient
->mangledhost
!= NULL
)
742 /* if host mangling mode enabled, also check their real host */
743 if(!strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
745 sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->orighost
);
748 /* if host mangling mode not enabled and no other spoof,
749 * also check the mangled form of their host */
750 else if (!IsDynSpoof(source_p
))
752 sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->localClient
->mangledhost
);
757 if((is_banned(chptr
, source_p
, NULL
, src_host
, src_iphost
, forward
)) == CHFL_BAN
)
759 moduledata
.approved
= ERR_BANNEDFROMCHAN
;
760 goto finish_join_check
;
763 if(*chptr
->mode
.key
&& (EmptyString(key
) || irccmp(chptr
->mode
.key
, key
)))
765 moduledata
.approved
= ERR_BADCHANNELKEY
;
766 goto finish_join_check
;
769 /* All checks from this point on will forward... */
771 *forward
= chptr
->mode
.forward
;
773 if(chptr
->mode
.mode
& MODE_INVITEONLY
)
775 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
777 if(invite
->data
== chptr
)
782 if(!ConfigChannel
.use_invex
)
783 moduledata
.approved
= ERR_INVITEONLYCHAN
;
784 RB_DLINK_FOREACH(ptr
, chptr
->invexlist
.head
)
787 if(match(invex
->banstr
, src_host
)
788 || match(invex
->banstr
, src_iphost
)
789 || match_cidr(invex
->banstr
, src_iphost
)
790 || match_extban(invex
->banstr
, source_p
, chptr
, CHFL_INVEX
)
791 || (use_althost
&& match(invex
->banstr
, src_althost
)))
795 moduledata
.approved
= ERR_INVITEONLYCHAN
;
799 if(chptr
->mode
.limit
&&
800 rb_dlink_list_length(&chptr
->members
) >= (unsigned long) chptr
->mode
.limit
)
801 i
= ERR_CHANNELISFULL
;
802 if(chptr
->mode
.mode
& MODE_REGONLY
&& EmptyString(source_p
->user
->suser
))
803 i
= ERR_NEEDREGGEDNICK
;
804 /* join throttling stuff --nenolod */
805 else if(chptr
->mode
.join_num
> 0 && chptr
->mode
.join_time
> 0)
807 if ((rb_current_time() - chptr
->join_delta
<=
808 chptr
->mode
.join_time
) && (chptr
->join_count
>=
809 chptr
->mode
.join_num
))
813 /* allow /invite to override +l/+r/+j also -- jilles */
814 if (i
!= 0 && invite
== NULL
)
816 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
818 if(invite
->data
== chptr
)
822 moduledata
.approved
= i
;
826 call_hook(h_can_join
, &moduledata
);
828 return moduledata
.approved
;
833 * input - user to check in channel, membership pointer
834 * output - whether can explicitly send or not, else CAN_SEND_NONOP
838 can_send(struct Channel
*chptr
, struct Client
*source_p
, struct membership
*msptr
)
840 hook_data_channel_approval moduledata
;
842 moduledata
.approved
= CAN_SEND_NONOP
;
843 moduledata
.dir
= MODE_QUERY
;
845 if(IsServer(source_p
) || IsService(source_p
))
848 if(MyClient(source_p
) && hash_find_resv(chptr
->chname
) &&
849 !IsOper(source_p
) && !IsExemptResv(source_p
))
850 moduledata
.approved
= CAN_SEND_NO
;
854 msptr
= find_channel_membership(chptr
, source_p
);
858 /* if its +m or +n and theyre not in the channel,
859 * they cant send. we dont check bans here because
860 * theres no possibility of caching them --fl
862 if(chptr
->mode
.mode
& MODE_NOPRIVMSGS
|| chptr
->mode
.mode
& MODE_MODERATED
)
863 moduledata
.approved
= CAN_SEND_NO
;
865 moduledata
.approved
= CAN_SEND_NONOP
;
867 return moduledata
.approved
;
871 if(chptr
->mode
.mode
& MODE_MODERATED
)
872 moduledata
.approved
= CAN_SEND_NO
;
874 if(MyClient(source_p
))
876 /* cached can_send */
877 if(msptr
->bants
== chptr
->bants
)
879 if(can_send_banned(msptr
))
880 moduledata
.approved
= CAN_SEND_NO
;
882 else if(is_banned(chptr
, source_p
, msptr
, NULL
, NULL
, NULL
) == CHFL_BAN
883 || is_quieted(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
)
884 moduledata
.approved
= CAN_SEND_NO
;
887 if(is_chanop_voiced(msptr
))
888 moduledata
.approved
= CAN_SEND_OPV
;
890 moduledata
.client
= source_p
;
891 moduledata
.chptr
= msptr
->chptr
;
892 moduledata
.msptr
= msptr
;
893 moduledata
.target
= NULL
;
894 moduledata
.dir
= (moduledata
.approved
== CAN_SEND_NO
) ? MODE_ADD
: MODE_QUERY
;
896 call_hook(h_can_send
, &moduledata
);
898 return moduledata
.approved
;
902 * flood_attack_channel
903 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
904 * says NOTICE must not auto reply
905 * - pointer to source Client
906 * - pointer to target channel
907 * output - true if target is under flood attack
908 * side effects - check for flood attack on target chptr
911 flood_attack_channel(int p_or_n
, struct Client
*source_p
, struct Channel
*chptr
, char *chname
)
915 if(GlobalSetOptions
.floodcount
&& MyClient(source_p
))
917 if((chptr
->first_received_message_time
+ 1) < rb_current_time())
919 delta
= rb_current_time() - chptr
->first_received_message_time
;
920 chptr
->received_number_of_privmsgs
-= delta
;
921 chptr
->first_received_message_time
= rb_current_time();
922 if(chptr
->received_number_of_privmsgs
<= 0)
924 chptr
->received_number_of_privmsgs
= 0;
925 chptr
->flood_noticed
= 0;
929 if((chptr
->received_number_of_privmsgs
>= GlobalSetOptions
.floodcount
)
930 || chptr
->flood_noticed
)
932 if(chptr
->flood_noticed
== 0)
934 sendto_realops_snomask(SNO_BOTS
, *chptr
->chname
== '&' ? L_ALL
: L_NETWIDE
,
935 "Possible Flooder %s[%s@%s] on %s target: %s",
936 source_p
->name
, source_p
->username
,
938 source_p
->servptr
->name
, chptr
->chname
);
939 chptr
->flood_noticed
= 1;
941 /* Add a bit of penalty */
942 chptr
->received_number_of_privmsgs
+= 2;
944 if(MyClient(source_p
) && (p_or_n
!= 1))
946 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
947 me
.name
, source_p
->name
, chptr
->chname
);
951 chptr
->received_number_of_privmsgs
++;
957 /* find_bannickchange_channel()
958 * Input: client to check
959 * Output: channel preventing nick change
962 find_bannickchange_channel(struct Client
*client_p
)
964 struct Channel
*chptr
;
965 struct membership
*msptr
;
967 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
968 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
970 if (!MyClient(client_p
))
973 sprintf(src_host
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->host
);
974 sprintf(src_iphost
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->sockhost
);
976 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
979 chptr
= msptr
->chptr
;
980 if (is_chanop_voiced(msptr
))
982 /* cached can_send */
983 if (msptr
->bants
== chptr
->bants
)
985 if (can_send_banned(msptr
))
988 else if (is_banned(chptr
, client_p
, msptr
, src_host
, src_iphost
, NULL
) == CHFL_BAN
989 || is_quieted(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
)
995 /* void check_spambot_warning(struct Client *source_p)
996 * Input: Client to check, channel name or NULL if this is a part.
998 * Side-effects: Updates the client's oper_warn_count_down, warns the
999 * IRC operators if necessary, and updates join_leave_countdown as
1003 check_spambot_warning(struct Client
*source_p
, const char *name
)
1006 int decrement_count
;
1007 if((GlobalSetOptions
.spam_num
&&
1008 (source_p
->localClient
->join_leave_count
>= GlobalSetOptions
.spam_num
)))
1010 if(source_p
->localClient
->oper_warn_count_down
> 0)
1011 source_p
->localClient
->oper_warn_count_down
--;
1013 source_p
->localClient
->oper_warn_count_down
= 0;
1014 if(source_p
->localClient
->oper_warn_count_down
== 0 &&
1017 /* Its already known as a possible spambot */
1018 sendto_realops_snomask(SNO_BOTS
, L_NETWIDE
,
1019 "User %s (%s@%s) trying to join %s is a possible spambot",
1021 source_p
->username
, source_p
->orighost
, name
);
1022 source_p
->localClient
->oper_warn_count_down
= OPER_SPAM_COUNTDOWN
;
1028 (rb_current_time() - source_p
->localClient
->last_leave_time
)) >
1029 JOIN_LEAVE_COUNT_EXPIRE_TIME
)
1031 decrement_count
= (t_delta
/ JOIN_LEAVE_COUNT_EXPIRE_TIME
);
1034 else if(decrement_count
> source_p
->localClient
->join_leave_count
)
1035 source_p
->localClient
->join_leave_count
= 0;
1037 source_p
->localClient
->join_leave_count
-= decrement_count
;
1041 if((rb_current_time() -
1042 (source_p
->localClient
->last_join_time
)) < GlobalSetOptions
.spam_time
)
1044 /* oh, its a possible spambot */
1045 source_p
->localClient
->join_leave_count
++;
1049 source_p
->localClient
->last_join_time
= rb_current_time();
1051 source_p
->localClient
->last_leave_time
= rb_current_time();
1055 /* check_splitmode()
1059 * side effects - compares usercount and servercount against their split
1060 * values and adjusts splitmode accordingly
1063 check_splitmode(void *unused
)
1065 if(splitchecking
&& (ConfigChannel
.no_join_on_split
|| ConfigChannel
.no_create_on_split
))
1067 /* not split, we're being asked to check now because someone
1072 if(eob_count
< split_servers
|| Count
.total
< split_users
)
1075 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1076 "Network split, activating splitmode");
1077 check_splitmode_ev
= rb_event_addish("check_splitmode", check_splitmode
, NULL
, 2);
1080 /* in splitmode, check whether its finished */
1081 else if(eob_count
>= split_servers
&& Count
.total
>= split_users
)
1085 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1086 "Network rejoined, deactivating splitmode");
1088 rb_event_delete(check_splitmode_ev
);
1089 check_splitmode_ev
= NULL
;
1097 * input - channel to allocate topic for
1098 * output - 1 on success, else 0
1099 * side effects - channel gets a topic allocated
1102 allocate_topic(struct Channel
*chptr
)
1109 ptr
= rb_bh_alloc(topic_heap
);
1111 /* Basically we allocate one large block for the topic and
1112 * the topic info. We then split it up into two and shove it
1116 chptr
->topic_info
= (char *) ptr
+ TOPICLEN
+ 1;
1117 *chptr
->topic
= '\0';
1118 *chptr
->topic_info
= '\0';
1123 * input - channel which has topic to free
1125 * side effects - channels topic is free'd
1128 free_topic(struct Channel
*chptr
)
1132 if(chptr
== NULL
|| chptr
->topic
== NULL
)
1135 /* This is safe for now - If you change allocate_topic you
1136 * MUST change this as well
1139 rb_bh_free(topic_heap
, ptr
);
1140 chptr
->topic
= NULL
;
1141 chptr
->topic_info
= NULL
;
1144 /* set_channel_topic()
1146 * input - channel, topic to set, topic info and topic ts
1148 * side effects - channels topic, topic info and TS are set.
1151 set_channel_topic(struct Channel
*chptr
, const char *topic
, const char *topic_info
, time_t topicts
)
1153 if(strlen(topic
) > 0)
1155 if(chptr
->topic
== NULL
)
1156 allocate_topic(chptr
);
1157 rb_strlcpy(chptr
->topic
, topic
, TOPICLEN
+ 1);
1158 rb_strlcpy(chptr
->topic_info
, topic_info
, USERHOST_REPLYLEN
);
1159 chptr
->topic_time
= topicts
;
1163 if(chptr
->topic
!= NULL
)
1165 chptr
->topic_time
= 0;
1171 * inputs - pointer to channel
1172 * - pointer to client
1173 * output - string with simple modes
1174 * side effects - result from previous calls overwritten
1176 * Stolen from ShadowIRCd 4 --nenolod
1179 channel_modes(struct Channel
*chptr
, struct Client
*client_p
)
1184 static char final
[BUFSIZE
];
1191 for (i
= 0; i
< 256; i
++)
1193 if(chmode_table
[i
].set_func
== chm_hidden
&& (!IsOper(client_p
) || !IsClient(client_p
)))
1195 if(chptr
->mode
.mode
& chmode_flags
[i
])
1199 if(chptr
->mode
.limit
)
1203 if(!IsClient(client_p
) || IsMember(client_p
, chptr
))
1204 pbuf
+= sprintf(pbuf
, " %d", chptr
->mode
.limit
);
1207 if(*chptr
->mode
.key
)
1211 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1212 pbuf
+= sprintf(pbuf
, " %s", chptr
->mode
.key
);
1215 if(chptr
->mode
.join_num
)
1219 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1220 pbuf
+= sprintf(pbuf
, " %d:%d", chptr
->mode
.join_num
,
1221 chptr
->mode
.join_time
);
1224 if(*chptr
->mode
.forward
&&
1225 (ConfigChannel
.use_forward
|| !IsClient(client_p
)))
1229 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1230 pbuf
+= sprintf(pbuf
, " %s", chptr
->mode
.forward
);
1235 rb_strlcpy(final
, buf1
, sizeof final
);
1236 rb_strlcat(final
, buf2
, sizeof final
);
1240 /* void send_cap_mode_changes(struct Client *client_p,
1241 * struct Client *source_p,
1242 * struct Channel *chptr, int cap, int nocap)
1243 * Input: The client sending(client_p), the source client(source_p),
1244 * the channel to send mode changes for(chptr)
1246 * Side-effects: Sends the appropriate mode changes to capable servers.
1248 * Reverted back to my original design, except that we now keep a count
1249 * of the number of servers which each combination as an optimisation, so
1250 * the capabs combinations which are not needed are not worked out. -A1kmm
1252 * Removed most code here because we don't need to be compatible with ircd
1253 * 2.8.21+CSr and stuff. --nenolod
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
;
1267 /* Now send to servers... */
1275 mbl
= preflen
= sprintf(modebuf
, ":%s TMODE %ld %s ",
1276 use_id(source_p
), (long) chptr
->channelts
,
1279 /* loop the list of - modes we have */
1280 for (i
= 0; i
< mode_count
; i
++)
1282 /* if they dont support the cap we need, or they do support a cap they
1283 * cant have, then dont add it to the modebuf.. that way they wont see
1286 if (mode_changes
[i
].letter
== 0)
1289 if (!EmptyString(mode_changes
[i
].id
))
1290 arg
= mode_changes
[i
].id
;
1292 arg
= mode_changes
[i
].arg
;
1296 arglen
= strlen(arg
);
1298 /* dont even think about it! --fl */
1299 if(arglen
> MODEBUFLEN
- 5)
1303 /* if we're creeping past the buf size, we need to send it and make
1304 * another line for the other modes
1305 * XXX - this could give away server topology with uids being
1306 * different lengths, but not much we can do, except possibly break
1307 * them as if they were the longest of the nick or uid at all times,
1308 * which even then won't work as we don't always know the uid -A1kmm.
1310 if(arg
&& ((mc
== MAXMODEPARAMSSERV
) ||
1311 ((mbl
+ pbl
+ arglen
+ 4) > (BUFSIZE
- 3))))
1314 sendto_server(client_p
, chptr
, NOCAPS
, NOCAPS
,
1315 "%s %s", modebuf
, parabuf
);
1326 if(dir
!= mode_changes
[i
].dir
)
1328 modebuf
[mbl
++] = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1329 dir
= mode_changes
[i
].dir
;
1332 modebuf
[mbl
++] = mode_changes
[i
].letter
;
1338 len
= sprintf(pbuf
, "%s ", arg
);
1345 if(pbl
&& parabuf
[pbl
- 1] == ' ')
1346 parabuf
[pbl
- 1] = 0;
1349 sendto_server(client_p
, chptr
, NOCAPS
, NOCAPS
, "%s %s", modebuf
, parabuf
);
1353 resv_chan_forcepart(const char *name
, const char *reason
, int temp_time
)
1356 rb_dlink_node
*next_ptr
;
1357 struct Channel
*chptr
;
1358 struct membership
*msptr
;
1359 struct Client
*target_p
;
1361 if(!ConfigChannel
.resv_forcepart
)
1364 /* for each user on our server in the channel list
1365 * send them a PART, and notify opers.
1367 chptr
= find_channel(name
);
1370 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->locmembers
.head
)
1373 target_p
= msptr
->client_p
;
1375 if(IsExemptResv(target_p
))
1378 sendto_server(target_p
, chptr
, CAP_TS6
, NOCAPS
,
1379 ":%s PART %s", target_p
->id
, chptr
->chname
);
1381 sendto_channel_local(target_p
, ALL_MEMBERS
, chptr
, ":%s!%s@%s PART %s :%s",
1382 target_p
->name
, target_p
->username
,
1383 target_p
->host
, chptr
->chname
, target_p
->name
);
1385 remove_user_from_channel(msptr
);
1387 /* notify opers & user they were removed from the channel */
1388 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1389 "Forced PART for %s!%s@%s from %s (%s)",
1390 target_p
->name
, target_p
->username
,
1391 target_p
->host
, name
, reason
);
1394 sendto_one_notice(target_p
, ":*** Channel %s is temporarily unavailable on this server.",
1397 sendto_one_notice(target_p
, ":*** Channel %s is no longer available on this server.",