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"
44 #include "ipv4_from_ipv6.h"
47 struct config_channel_entry ConfigChannel
;
48 rb_dlink_list global_channel_list
;
49 static rb_bh
*channel_heap
;
50 static rb_bh
*ban_heap
;
51 static rb_bh
*topic_heap
;
52 static rb_bh
*member_heap
;
54 static void free_topic(struct Channel
*chptr
);
56 static int h_can_join
;
57 static int h_can_send
;
58 int h_get_channel_access
;
64 * side effects - initialises the various blockheaps
69 channel_heap
= rb_bh_create(sizeof(struct Channel
), CHANNEL_HEAP_SIZE
, "channel_heap");
70 ban_heap
= rb_bh_create(sizeof(struct Ban
), BAN_HEAP_SIZE
, "ban_heap");
71 topic_heap
= rb_bh_create(TOPICLEN
+ 1 + USERHOST_REPLYLEN
, TOPIC_HEAP_SIZE
, "topic_heap");
72 member_heap
= rb_bh_create(sizeof(struct membership
), MEMBER_HEAP_SIZE
, "member_heap");
74 h_can_join
= register_hook("can_join");
75 h_can_send
= register_hook("can_send");
76 h_get_channel_access
= register_hook("get_channel_access");
80 * allocate_channel - Allocates a channel
83 allocate_channel(const char *chname
)
85 struct Channel
*chptr
;
86 chptr
= rb_bh_alloc(channel_heap
);
87 chptr
->chname
= rb_strdup(chname
);
92 free_channel(struct Channel
*chptr
)
94 rb_free(chptr
->chname
);
95 rb_free(chptr
->mode_lock
);
96 rb_bh_free(channel_heap
, chptr
);
100 allocate_ban(const char *banstr
, const char *who
, const char *forward
)
103 bptr
= rb_bh_alloc(ban_heap
);
104 bptr
->banstr
= rb_strdup(banstr
);
105 bptr
->who
= rb_strdup(who
);
106 bptr
->forward
= forward
? rb_strdup(forward
) : NULL
;
112 free_ban(struct Ban
*bptr
)
114 rb_free(bptr
->banstr
);
116 rb_free(bptr
->forward
);
117 rb_bh_free(ban_heap
, bptr
);
121 * send_channel_join()
123 * input - channel to join, client joining.
125 * side effects - none
128 send_channel_join(struct Channel
*chptr
, struct Client
*client_p
)
130 if (!IsClient(client_p
))
133 sendto_channel_local_with_capability(ALL_MEMBERS
, NOCAPS
, CLICAP_EXTENDED_JOIN
, chptr
, ":%s!%s@%s JOIN %s",
134 client_p
->name
, client_p
->username
, client_p
->host
, chptr
->chname
);
136 sendto_channel_local_with_capability(ALL_MEMBERS
, CLICAP_EXTENDED_JOIN
, NOCAPS
, chptr
, ":%s!%s@%s JOIN %s %s :%s",
137 client_p
->name
, client_p
->username
, client_p
->host
, chptr
->chname
,
138 EmptyString(client_p
->user
->suser
) ? "*" : client_p
->user
->suser
,
141 /* Send away message to away-notify enabled clients. */
142 if (client_p
->user
->away
)
143 sendto_channel_local_with_capability_butone(client_p
, ALL_MEMBERS
, CLICAP_AWAY_NOTIFY
, NOCAPS
, chptr
,
144 ":%s!%s@%s AWAY :%s", client_p
->name
, client_p
->username
,
145 client_p
->host
, client_p
->user
->away
);
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 if (IsCapable(client_p
, CLICAP_USERHOST_IN_NAMES
))
470 /* space, possible "@+" prefix */
471 if (cur_len
+ strlen(target_p
->name
) + strlen(target_p
->username
) + strlen(target_p
->host
) + 5 >= BUFSIZE
- 5)
474 sendto_one(client_p
, "%s", lbuf
);
479 tlen
= rb_sprintf(t
, "%s%s!%s@%s ", find_channel_status(msptr
, stack
),
480 target_p
->name
, target_p
->username
, target_p
->host
);
484 /* space, possible "@+" prefix */
485 if(cur_len
+ strlen(target_p
->name
) + 3 >= BUFSIZE
- 3)
488 sendto_one(client_p
, "%s", lbuf
);
493 tlen
= rb_sprintf(t
, "%s%s ", find_channel_status(msptr
, stack
),
501 /* The old behaviour here was to always output our buffer,
502 * even if there are no clients we can show. This happens
503 * when a client does "NAMES" with no parameters, and all
504 * the clients on a -sp channel are +i. I dont see a good
505 * reason for keeping that behaviour, as it just wastes
511 sendto_one(client_p
, "%s", lbuf
);
516 sendto_one(client_p
, form_str(RPL_ENDOFNAMES
),
517 me
.name
, client_p
->name
, chptr
->chname
);
522 * input - channel to remove invite from, client to remove
524 * side effects - user is removed from invite list, if exists
527 del_invite(struct Channel
*chptr
, struct Client
*who
)
529 rb_dlinkFindDestroy(who
, &chptr
->invites
);
530 rb_dlinkFindDestroy(chptr
, &who
->user
->invited
);
535 * input - channel to check bans for, ban list (banlist or quietlist),
536 * user to check bans against, optional prebuilt buffers,
537 * optional forward channel pointer
538 * output - 1 if banned, else 0
542 is_banned_list(struct Channel
*chptr
, rb_dlink_list
*list
,
543 struct Client
*who
, struct membership
*msptr
,
544 const char *s
, const char *s2
, const char **forward
)
546 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
547 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
548 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
549 char src_ip4host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
552 struct sockaddr_in ip4
;
554 struct Ban
*actualBan
= NULL
;
555 struct Ban
*actualExcept
= NULL
;
560 /* if the buffers havent been built, do it here */
563 rb_sprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
564 rb_sprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
569 if(who
->localClient
->mangledhost
!= NULL
)
571 /* if host mangling mode enabled, also check their real host */
572 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
574 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
577 /* if host mangling mode not enabled and no other spoof,
578 * also check the mangled form of their host */
579 else if (!IsDynSpoof(who
))
581 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
586 if(who
->localClient
->ip
.ss_family
== AF_INET6
&&
587 ipv4_from_ipv6((const struct sockaddr_in6
*)&who
->localClient
->ip
, &ip4
))
589 rb_sprintf(src_ip4host
, "%s!%s@", who
->name
, who
->username
);
590 s4
= src_ip4host
+ strlen(src_ip4host
);
591 rb_inet_ntop_sock((struct sockaddr
*)&ip4
,
592 s4
, src_ip4host
+ sizeof src_ip4host
- s4
);
597 RB_DLINK_FOREACH(ptr
, list
->head
)
599 actualBan
= ptr
->data
;
600 if(match(actualBan
->banstr
, s
) ||
601 match(actualBan
->banstr
, s2
) ||
602 match_cidr(actualBan
->banstr
, s2
) ||
603 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_BAN
) ||
604 (s3
!= NULL
&& match(actualBan
->banstr
, s3
))
607 (s4
!= NULL
&& (match(actualBan
->banstr
, s4
) || match_cidr(actualBan
->banstr
, s4
)))
615 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
617 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
619 actualExcept
= ptr
->data
;
621 /* theyre exempted.. */
622 if(match(actualExcept
->banstr
, s
) ||
623 match(actualExcept
->banstr
, s2
) ||
624 match_cidr(actualExcept
->banstr
, s2
) ||
625 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
626 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
628 /* cache the fact theyre not banned */
631 msptr
->bants
= chptr
->bants
;
632 msptr
->flags
&= ~CHFL_BANNED
;
635 return CHFL_EXCEPTION
;
640 /* cache the banned/not banned status */
643 msptr
->bants
= chptr
->bants
;
645 if(actualBan
!= NULL
)
647 msptr
->flags
|= CHFL_BANNED
;
652 msptr
->flags
&= ~CHFL_BANNED
;
657 if (actualBan
&& actualBan
->forward
&& forward
)
658 *forward
= actualBan
->forward
;
660 return ((actualBan
? CHFL_BAN
: 0));
665 * input - channel to check bans for, user to check bans against
666 * optional prebuilt buffers, optional forward channel pointer
667 * output - 1 if banned, else 0
671 is_banned(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
672 const char *s
, const char *s2
, const char **forward
)
674 return is_banned_list(chptr
, &chptr
->banlist
, who
, msptr
, s
, s2
,
680 * input - channel to check bans for, user to check bans against
681 * optional prebuilt buffers
682 * output - 1 if banned, else 0
686 is_quieted(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
687 const char *s
, const char *s2
)
689 return is_banned_list(chptr
, &chptr
->quietlist
, who
, msptr
, s
, s2
,
695 * input - client to check, channel to check for, key
696 * output - reason for not being able to join, else 0, channel name to forward to
698 * caveats - this function should only be called on a local user.
701 can_join(struct Client
*source_p
, struct Channel
*chptr
, const char *key
, const char **forward
)
703 rb_dlink_node
*invite
= NULL
;
705 struct Ban
*invex
= NULL
;
706 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
707 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
708 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
711 hook_data_channel moduledata
;
713 s_assert(source_p
->localClient
!= NULL
);
715 moduledata
.client
= source_p
;
716 moduledata
.chptr
= chptr
;
717 moduledata
.approved
= 0;
719 rb_sprintf(src_host
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
720 rb_sprintf(src_iphost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->sockhost
);
721 if(source_p
->localClient
->mangledhost
!= NULL
)
723 /* if host mangling mode enabled, also check their real host */
724 if(!strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
726 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->orighost
);
729 /* if host mangling mode not enabled and no other spoof,
730 * also check the mangled form of their host */
731 else if (!IsDynSpoof(source_p
))
733 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->localClient
->mangledhost
);
738 if((is_banned(chptr
, source_p
, NULL
, src_host
, src_iphost
, forward
)) == CHFL_BAN
)
740 moduledata
.approved
= ERR_BANNEDFROMCHAN
;
741 goto finish_join_check
;
744 if(*chptr
->mode
.key
&& (EmptyString(key
) || irccmp(chptr
->mode
.key
, key
)))
746 moduledata
.approved
= ERR_BADCHANNELKEY
;
747 goto finish_join_check
;
750 /* All checks from this point on will forward... */
752 *forward
= chptr
->mode
.forward
;
754 if(chptr
->mode
.mode
& MODE_INVITEONLY
)
756 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
758 if(invite
->data
== chptr
)
763 if(!ConfigChannel
.use_invex
)
764 moduledata
.approved
= ERR_INVITEONLYCHAN
;
765 RB_DLINK_FOREACH(ptr
, chptr
->invexlist
.head
)
768 if(match(invex
->banstr
, src_host
)
769 || match(invex
->banstr
, src_iphost
)
770 || match_cidr(invex
->banstr
, src_iphost
)
771 || match_extban(invex
->banstr
, source_p
, chptr
, CHFL_INVEX
)
772 || (use_althost
&& match(invex
->banstr
, src_althost
)))
776 moduledata
.approved
= ERR_INVITEONLYCHAN
;
780 if(chptr
->mode
.limit
&&
781 rb_dlink_list_length(&chptr
->members
) >= (unsigned long) chptr
->mode
.limit
)
782 i
= ERR_CHANNELISFULL
;
783 if(chptr
->mode
.mode
& MODE_REGONLY
&& EmptyString(source_p
->user
->suser
))
784 i
= ERR_NEEDREGGEDNICK
;
785 /* join throttling stuff --nenolod */
786 else if(chptr
->mode
.join_num
> 0 && chptr
->mode
.join_time
> 0)
788 if ((rb_current_time() - chptr
->join_delta
<=
789 chptr
->mode
.join_time
) && (chptr
->join_count
>=
790 chptr
->mode
.join_num
))
794 /* allow /invite to override +l/+r/+j also -- jilles */
795 if (i
!= 0 && invite
== NULL
)
797 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
799 if(invite
->data
== chptr
)
803 moduledata
.approved
= i
;
807 call_hook(h_can_join
, &moduledata
);
809 return moduledata
.approved
;
814 * input - user to check in channel, membership pointer
815 * output - whether can explicitly send or not, else CAN_SEND_NONOP
819 can_send(struct Channel
*chptr
, struct Client
*source_p
, struct membership
*msptr
)
821 hook_data_channel_approval moduledata
;
823 moduledata
.approved
= CAN_SEND_NONOP
;
825 if(IsServer(source_p
) || IsService(source_p
))
828 if(MyClient(source_p
) && hash_find_resv(chptr
->chname
) &&
829 !IsOper(source_p
) && !IsExemptResv(source_p
))
830 moduledata
.approved
= CAN_SEND_NO
;
834 msptr
= find_channel_membership(chptr
, source_p
);
838 /* if its +m or +n and theyre not in the channel,
839 * they cant send. we dont check bans here because
840 * theres no possibility of caching them --fl
842 if(chptr
->mode
.mode
& MODE_NOPRIVMSGS
|| chptr
->mode
.mode
& MODE_MODERATED
)
843 moduledata
.approved
= CAN_SEND_NO
;
845 moduledata
.approved
= CAN_SEND_NONOP
;
847 return moduledata
.approved
;
851 if(chptr
->mode
.mode
& MODE_MODERATED
)
852 moduledata
.approved
= CAN_SEND_NO
;
854 if(MyClient(source_p
))
856 /* cached can_send */
857 if(msptr
->bants
== chptr
->bants
)
859 if(can_send_banned(msptr
))
860 moduledata
.approved
= CAN_SEND_NO
;
862 else if(is_banned(chptr
, source_p
, msptr
, NULL
, NULL
, NULL
) == CHFL_BAN
863 || is_quieted(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
)
864 moduledata
.approved
= CAN_SEND_NO
;
867 if(is_chanop_voiced(msptr
))
868 moduledata
.approved
= CAN_SEND_OPV
;
870 moduledata
.client
= source_p
;
871 moduledata
.chptr
= msptr
->chptr
;
872 moduledata
.msptr
= msptr
;
873 moduledata
.target
= NULL
;
875 call_hook(h_can_send
, &moduledata
);
877 return moduledata
.approved
;
881 * flood_attack_channel
882 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
883 * says NOTICE must not auto reply
884 * - pointer to source Client
885 * - pointer to target channel
886 * output - 1 if target is under flood attack
887 * side effects - check for flood attack on target chptr
890 flood_attack_channel(int p_or_n
, struct Client
*source_p
, struct Channel
*chptr
, char *chname
)
894 if(GlobalSetOptions
.floodcount
&& MyClient(source_p
))
896 if((chptr
->first_received_message_time
+ 1) < rb_current_time())
898 delta
= rb_current_time() - chptr
->first_received_message_time
;
899 chptr
->received_number_of_privmsgs
-= delta
;
900 chptr
->first_received_message_time
= rb_current_time();
901 if(chptr
->received_number_of_privmsgs
<= 0)
903 chptr
->received_number_of_privmsgs
= 0;
904 chptr
->flood_noticed
= 0;
908 if((chptr
->received_number_of_privmsgs
>= GlobalSetOptions
.floodcount
)
909 || chptr
->flood_noticed
)
911 if(chptr
->flood_noticed
== 0)
913 sendto_realops_snomask(SNO_BOTS
, *chptr
->chname
== '&' ? L_ALL
: L_NETWIDE
,
914 "Possible Flooder %s[%s@%s] on %s target: %s",
915 source_p
->name
, source_p
->username
,
917 source_p
->servptr
->name
, chptr
->chname
);
918 chptr
->flood_noticed
= 1;
920 /* Add a bit of penalty */
921 chptr
->received_number_of_privmsgs
+= 2;
923 if(MyClient(source_p
) && (p_or_n
!= 1))
925 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
926 me
.name
, source_p
->name
, chptr
->chname
);
930 chptr
->received_number_of_privmsgs
++;
936 /* find_bannickchange_channel()
937 * Input: client to check
938 * Output: channel preventing nick change
941 find_bannickchange_channel(struct Client
*client_p
)
943 struct Channel
*chptr
;
944 struct membership
*msptr
;
946 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
947 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
949 if (!MyClient(client_p
))
952 rb_sprintf(src_host
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->host
);
953 rb_sprintf(src_iphost
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->sockhost
);
955 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
958 chptr
= msptr
->chptr
;
959 if (is_chanop_voiced(msptr
))
961 /* cached can_send */
962 if (msptr
->bants
== chptr
->bants
)
964 if (can_send_banned(msptr
))
967 else if (is_banned(chptr
, client_p
, msptr
, src_host
, src_iphost
, NULL
) == CHFL_BAN
968 || is_quieted(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
)
974 /* void check_spambot_warning(struct Client *source_p)
975 * Input: Client to check, channel name or NULL if this is a part.
977 * Side-effects: Updates the client's oper_warn_count_down, warns the
978 * IRC operators if necessary, and updates join_leave_countdown as
982 check_spambot_warning(struct Client
*source_p
, const char *name
)
986 if((GlobalSetOptions
.spam_num
&&
987 (source_p
->localClient
->join_leave_count
>= GlobalSetOptions
.spam_num
)))
989 if(source_p
->localClient
->oper_warn_count_down
> 0)
990 source_p
->localClient
->oper_warn_count_down
--;
992 source_p
->localClient
->oper_warn_count_down
= 0;
993 if(source_p
->localClient
->oper_warn_count_down
== 0 &&
996 /* Its already known as a possible spambot */
997 sendto_realops_snomask(SNO_BOTS
, L_NETWIDE
,
998 "User %s (%s@%s) trying to join %s is a possible spambot",
1000 source_p
->username
, source_p
->orighost
, name
);
1001 source_p
->localClient
->oper_warn_count_down
= OPER_SPAM_COUNTDOWN
;
1007 (rb_current_time() - source_p
->localClient
->last_leave_time
)) >
1008 JOIN_LEAVE_COUNT_EXPIRE_TIME
)
1010 decrement_count
= (t_delta
/ JOIN_LEAVE_COUNT_EXPIRE_TIME
);
1013 else if(decrement_count
> source_p
->localClient
->join_leave_count
)
1014 source_p
->localClient
->join_leave_count
= 0;
1016 source_p
->localClient
->join_leave_count
-= decrement_count
;
1020 if((rb_current_time() -
1021 (source_p
->localClient
->last_join_time
)) < GlobalSetOptions
.spam_time
)
1023 /* oh, its a possible spambot */
1024 source_p
->localClient
->join_leave_count
++;
1028 source_p
->localClient
->last_join_time
= rb_current_time();
1030 source_p
->localClient
->last_leave_time
= rb_current_time();
1034 /* check_splitmode()
1038 * side effects - compares usercount and servercount against their split
1039 * values and adjusts splitmode accordingly
1042 check_splitmode(void *unused
)
1044 if(splitchecking
&& (ConfigChannel
.no_join_on_split
|| ConfigChannel
.no_create_on_split
))
1046 /* not split, we're being asked to check now because someone
1051 if(eob_count
< split_servers
|| Count
.total
< split_users
)
1054 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1055 "Network split, activating splitmode");
1056 check_splitmode_ev
= rb_event_addish("check_splitmode", check_splitmode
, NULL
, 2);
1059 /* in splitmode, check whether its finished */
1060 else if(eob_count
>= split_servers
&& Count
.total
>= split_users
)
1064 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1065 "Network rejoined, deactivating splitmode");
1067 rb_event_delete(check_splitmode_ev
);
1068 check_splitmode_ev
= NULL
;
1076 * input - channel to allocate topic for
1077 * output - 1 on success, else 0
1078 * side effects - channel gets a topic allocated
1081 allocate_topic(struct Channel
*chptr
)
1088 ptr
= rb_bh_alloc(topic_heap
);
1090 /* Basically we allocate one large block for the topic and
1091 * the topic info. We then split it up into two and shove it
1095 chptr
->topic_info
= (char *) ptr
+ TOPICLEN
+ 1;
1096 *chptr
->topic
= '\0';
1097 *chptr
->topic_info
= '\0';
1102 * input - channel which has topic to free
1104 * side effects - channels topic is free'd
1107 free_topic(struct Channel
*chptr
)
1111 if(chptr
== NULL
|| chptr
->topic
== NULL
)
1114 /* This is safe for now - If you change allocate_topic you
1115 * MUST change this as well
1118 rb_bh_free(topic_heap
, ptr
);
1119 chptr
->topic
= NULL
;
1120 chptr
->topic_info
= NULL
;
1123 /* set_channel_topic()
1125 * input - channel, topic to set, topic info and topic ts
1127 * side effects - channels topic, topic info and TS are set.
1130 set_channel_topic(struct Channel
*chptr
, const char *topic
, const char *topic_info
, time_t topicts
)
1132 if(strlen(topic
) > 0)
1134 if(chptr
->topic
== NULL
)
1135 allocate_topic(chptr
);
1136 rb_strlcpy(chptr
->topic
, topic
, TOPICLEN
+ 1);
1137 rb_strlcpy(chptr
->topic_info
, topic_info
, USERHOST_REPLYLEN
);
1138 chptr
->topic_time
= topicts
;
1142 if(chptr
->topic
!= NULL
)
1144 chptr
->topic_time
= 0;
1150 * inputs - pointer to channel
1151 * - pointer to client
1152 * output - string with simple modes
1153 * side effects - result from previous calls overwritten
1155 * Stolen from ShadowIRCd 4 --nenolod
1158 channel_modes(struct Channel
*chptr
, struct Client
*client_p
)
1163 static char final
[BUFSIZE
];
1170 for (i
= 0; i
< 256; i
++)
1171 if(chptr
->mode
.mode
& chmode_flags
[i
])
1174 if(chptr
->mode
.limit
)
1178 if(!IsClient(client_p
) || IsMember(client_p
, chptr
))
1179 pbuf
+= rb_sprintf(pbuf
, " %d", chptr
->mode
.limit
);
1182 if(*chptr
->mode
.key
)
1186 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1187 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.key
);
1190 if(chptr
->mode
.join_num
)
1194 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1195 pbuf
+= rb_sprintf(pbuf
, " %d:%d", chptr
->mode
.join_num
,
1196 chptr
->mode
.join_time
);
1199 if(*chptr
->mode
.forward
&&
1200 (ConfigChannel
.use_forward
|| !IsClient(client_p
)))
1204 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1205 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.forward
);
1210 rb_strlcpy(final
, buf1
, sizeof final
);
1211 rb_strlcat(final
, buf2
, sizeof final
);
1215 /* void send_cap_mode_changes(struct Client *client_p,
1216 * struct Client *source_p,
1217 * struct Channel *chptr, int cap, int nocap)
1218 * Input: The client sending(client_p), the source client(source_p),
1219 * the channel to send mode changes for(chptr)
1221 * Side-effects: Sends the appropriate mode changes to capable servers.
1223 * Reverted back to my original design, except that we now keep a count
1224 * of the number of servers which each combination as an optimisation, so
1225 * the capabs combinations which are not needed are not worked out. -A1kmm
1227 * Removed most code here because we don't need to be compatible with ircd
1228 * 2.8.21+CSr and stuff. --nenolod
1231 send_cap_mode_changes(struct Client
*client_p
, struct Client
*source_p
,
1232 struct Channel
*chptr
, struct ChModeChange mode_changes
[], int mode_count
)
1234 static char modebuf
[BUFSIZE
];
1235 static char parabuf
[BUFSIZE
];
1236 int i
, mbl
, pbl
, nc
, mc
, preflen
, len
;
1242 /* Now send to servers... */
1250 mbl
= preflen
= rb_sprintf(modebuf
, ":%s TMODE %ld %s ",
1251 use_id(source_p
), (long) chptr
->channelts
,
1254 /* loop the list of - modes we have */
1255 for (i
= 0; i
< mode_count
; i
++)
1257 /* if they dont support the cap we need, or they do support a cap they
1258 * cant have, then dont add it to the modebuf.. that way they wont see
1261 if (mode_changes
[i
].letter
== 0)
1264 if (!EmptyString(mode_changes
[i
].id
))
1265 arg
= mode_changes
[i
].id
;
1267 arg
= mode_changes
[i
].arg
;
1271 arglen
= strlen(arg
);
1273 /* dont even think about it! --fl */
1274 if(arglen
> MODEBUFLEN
- 5)
1278 /* if we're creeping past the buf size, we need to send it and make
1279 * another line for the other modes
1280 * XXX - this could give away server topology with uids being
1281 * different lengths, but not much we can do, except possibly break
1282 * them as if they were the longest of the nick or uid at all times,
1283 * which even then won't work as we don't always know the uid -A1kmm.
1285 if(arg
&& ((mc
== MAXMODEPARAMSSERV
) ||
1286 ((mbl
+ pbl
+ arglen
+ 4) > (BUFSIZE
- 3))))
1289 sendto_server(client_p
, chptr
, NOCAPS
, NOCAPS
,
1290 "%s %s", modebuf
, parabuf
);
1301 if(dir
!= mode_changes
[i
].dir
)
1303 modebuf
[mbl
++] = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1304 dir
= mode_changes
[i
].dir
;
1307 modebuf
[mbl
++] = mode_changes
[i
].letter
;
1313 len
= rb_sprintf(pbuf
, "%s ", arg
);
1320 if(pbl
&& parabuf
[pbl
- 1] == ' ')
1321 parabuf
[pbl
- 1] = 0;
1324 sendto_server(client_p
, chptr
, NOCAPS
, NOCAPS
, "%s %s", modebuf
, parabuf
);
1328 resv_chan_forcepart(const char *name
, const char *reason
, int temp_time
)
1331 rb_dlink_node
*next_ptr
;
1332 struct Channel
*chptr
;
1333 struct membership
*msptr
;
1334 struct Client
*target_p
;
1336 if(!ConfigChannel
.resv_forcepart
)
1339 /* for each user on our server in the channel list
1340 * send them a PART, and notify opers.
1342 chptr
= find_channel(name
);
1345 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->locmembers
.head
)
1348 target_p
= msptr
->client_p
;
1350 if(IsExemptResv(target_p
))
1353 sendto_server(target_p
, chptr
, CAP_TS6
, NOCAPS
,
1354 ":%s PART %s", target_p
->id
, chptr
->chname
);
1356 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s PART %s :%s",
1357 target_p
->name
, target_p
->username
,
1358 target_p
->host
, chptr
->chname
, target_p
->name
);
1360 remove_user_from_channel(msptr
);
1362 /* notify opers & user they were removed from the channel */
1363 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1364 "Forced PART for %s!%s@%s from %s (%s)",
1365 target_p
->name
, target_p
->username
,
1366 target_p
->host
, name
, reason
);
1369 sendto_one_notice(target_p
, ":*** Channel %s is temporarily unavailable on this server.",
1372 sendto_one_notice(target_p
, ":*** Channel %s is no longer available on this server.",