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 $
36 #include "s_serv.h" /* captab */
40 #include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
41 #include "s_newconf.h"
44 struct config_channel_entry ConfigChannel
;
45 rb_dlink_list global_channel_list
;
46 static rb_bh
*channel_heap
;
47 static rb_bh
*ban_heap
;
48 static rb_bh
*topic_heap
;
49 static rb_bh
*member_heap
;
51 static int channel_capabs
[] = { CAP_EX
, CAP_IE
,
56 #define NCHCAPS (sizeof(channel_capabs)/sizeof(int))
57 #define NCHCAP_COMBOS (1 << NCHCAPS)
59 static struct ChCapCombo chcap_combos
[NCHCAP_COMBOS
];
61 static void free_topic(struct Channel
*chptr
);
63 static int h_can_join
;
69 * side effects - initialises the various blockheaps
74 channel_heap
= rb_bh_create(sizeof(struct Channel
), CHANNEL_HEAP_SIZE
, "channel_heap");
75 ban_heap
= rb_bh_create(sizeof(struct Ban
), BAN_HEAP_SIZE
, "ban_heap");
76 topic_heap
= rb_bh_create(TOPICLEN
+ 1 + USERHOST_REPLYLEN
, TOPIC_HEAP_SIZE
, "topic_heap");
77 member_heap
= rb_bh_create(sizeof(struct membership
), MEMBER_HEAP_SIZE
, "member_heap");
79 h_can_join
= register_hook("can_join");
83 * allocate_channel - Allocates a channel
86 allocate_channel(const char *chname
)
88 struct Channel
*chptr
;
89 chptr
= rb_bh_alloc(channel_heap
);
90 chptr
->chname
= rb_strdup(chname
);
95 free_channel(struct Channel
*chptr
)
97 rb_free(chptr
->chname
);
98 rb_bh_free(channel_heap
, chptr
);
102 allocate_ban(const char *banstr
, const char *who
)
105 bptr
= rb_bh_alloc(ban_heap
);
106 bptr
->banstr
= rb_strdup(banstr
);
107 bptr
->who
= rb_strdup(who
);
113 free_ban(struct Ban
*bptr
)
115 rb_free(bptr
->banstr
);
117 rb_bh_free(ban_heap
, bptr
);
121 /* find_channel_membership()
123 * input - channel to find them in, client to find
124 * output - membership of client in channel, else NULL
128 find_channel_membership(struct Channel
*chptr
, struct Client
*client_p
)
130 struct membership
*msptr
;
133 if(!IsClient(client_p
))
136 /* Pick the most efficient list to use to be nice to things like
137 * CHANSERV which could be in a large number of channels
139 if(rb_dlink_list_length(&chptr
->members
) < rb_dlink_list_length(&client_p
->user
->channel
))
141 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
145 if(msptr
->client_p
== client_p
)
151 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
155 if(msptr
->chptr
== chptr
)
163 /* find_channel_status()
165 * input - membership to get status for, whether we can combine flags
166 * output - flags of user on channel
170 find_channel_status(struct membership
*msptr
, int combine
)
172 static char buffer
[3];
191 /* add_user_to_channel()
193 * input - channel to add client to, client to add, channel flags
195 * side effects - user is added to channel
198 add_user_to_channel(struct Channel
*chptr
, struct Client
*client_p
, int flags
)
200 struct membership
*msptr
;
202 s_assert(client_p
->user
!= NULL
);
203 if(client_p
->user
== NULL
)
206 msptr
= rb_bh_alloc(member_heap
);
208 msptr
->chptr
= chptr
;
209 msptr
->client_p
= client_p
;
210 msptr
->flags
= flags
;
212 rb_dlinkAdd(msptr
, &msptr
->usernode
, &client_p
->user
->channel
);
213 rb_dlinkAdd(msptr
, &msptr
->channode
, &chptr
->members
);
215 if(MyClient(client_p
))
216 rb_dlinkAdd(msptr
, &msptr
->locchannode
, &chptr
->locmembers
);
219 /* remove_user_from_channel()
221 * input - membership pointer to remove from channel
223 * side effects - membership (thus user) is removed from channel
226 remove_user_from_channel(struct membership
*msptr
)
228 struct Client
*client_p
;
229 struct Channel
*chptr
;
230 s_assert(msptr
!= NULL
);
234 client_p
= msptr
->client_p
;
235 chptr
= msptr
->chptr
;
237 rb_dlinkDelete(&msptr
->usernode
, &client_p
->user
->channel
);
238 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
240 if(client_p
->servptr
== &me
)
241 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
243 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
244 destroy_channel(chptr
);
246 rb_bh_free(member_heap
, msptr
);
251 /* remove_user_from_channels()
253 * input - user to remove from all channels
255 * side effects - user is removed from all channels
258 remove_user_from_channels(struct Client
*client_p
)
260 struct Channel
*chptr
;
261 struct membership
*msptr
;
263 rb_dlink_node
*next_ptr
;
268 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->user
->channel
.head
)
271 chptr
= msptr
->chptr
;
273 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
275 if(client_p
->servptr
== &me
)
276 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
278 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
279 destroy_channel(chptr
);
281 rb_bh_free(member_heap
, msptr
);
284 client_p
->user
->channel
.head
= client_p
->user
->channel
.tail
= NULL
;
285 client_p
->user
->channel
.length
= 0;
288 /* invalidate_bancache_user()
290 * input - user to invalidate ban cache for
292 * side effects - ban cache is invalidated for all memberships of that user
293 * to be used after a nick change
296 invalidate_bancache_user(struct Client
*client_p
)
298 struct membership
*msptr
;
304 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
308 msptr
->flags
&= ~CHFL_BANNED
;
312 /* check_channel_name()
314 * input - channel name
315 * output - 1 if valid channel name, else 0
319 check_channel_name(const char *name
)
321 s_assert(name
!= NULL
);
325 for (; *name
; ++name
)
327 if(!IsChanChar(*name
))
334 /* free_channel_list()
336 * input - rb_dlink list to free
338 * side effects - list of b/e/I modes is cleared
341 free_channel_list(rb_dlink_list
* list
)
344 rb_dlink_node
*next_ptr
;
345 struct Ban
*actualBan
;
347 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
349 actualBan
= ptr
->data
;
353 list
->head
= list
->tail
= NULL
;
359 * input - channel to destroy
361 * side effects - channel is obliterated
364 destroy_channel(struct Channel
*chptr
)
366 rb_dlink_node
*ptr
, *next_ptr
;
368 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
370 del_invite(chptr
, ptr
->data
);
373 /* free all bans/exceptions/denies */
374 free_channel_list(&chptr
->banlist
);
375 free_channel_list(&chptr
->exceptlist
);
376 free_channel_list(&chptr
->invexlist
);
377 free_channel_list(&chptr
->quietlist
);
382 rb_dlinkDelete(&chptr
->node
, &global_channel_list
);
383 del_from_channel_hash(chptr
->chname
, chptr
);
387 /* channel_pub_or_secret()
390 * output - "=" if public, "@" if secret, else "*"
394 channel_pub_or_secret(struct Channel
*chptr
)
396 if(PubChannel(chptr
))
398 else if(SecretChannel(chptr
))
403 /* channel_member_names()
405 * input - channel to list, client to list to, show endofnames
407 * side effects - client is given list of users on channel
410 channel_member_names(struct Channel
*chptr
, struct Client
*client_p
, int show_eon
)
412 struct membership
*msptr
;
413 struct Client
*target_p
;
421 int stack
= IsCapable(client_p
, CLICAP_MULTI_PREFIX
);
423 if(ShowChannel(client_p
, chptr
))
425 is_member
= IsMember(client_p
, chptr
);
427 cur_len
= mlen
= rb_sprintf(lbuf
, form_str(RPL_NAMREPLY
),
428 me
.name
, client_p
->name
,
429 channel_pub_or_secret(chptr
), chptr
->chname
);
433 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
436 target_p
= msptr
->client_p
;
438 if(IsInvisible(target_p
) && !is_member
)
441 /* space, possible "@+" prefix */
442 if(cur_len
+ strlen(target_p
->name
) + 3 >= BUFSIZE
- 3)
445 sendto_one(client_p
, "%s", lbuf
);
450 tlen
= rb_sprintf(t
, "%s%s ", find_channel_status(msptr
, stack
),
457 /* The old behaviour here was to always output our buffer,
458 * even if there are no clients we can show. This happens
459 * when a client does "NAMES" with no parameters, and all
460 * the clients on a -sp channel are +i. I dont see a good
461 * reason for keeping that behaviour, as it just wastes
467 sendto_one(client_p
, "%s", lbuf
);
472 sendto_one(client_p
, form_str(RPL_ENDOFNAMES
),
473 me
.name
, client_p
->name
, chptr
->chname
);
478 * input - channel to remove invite from, client to remove
480 * side effects - user is removed from invite list, if exists
483 del_invite(struct Channel
*chptr
, struct Client
*who
)
485 rb_dlinkFindDestroy(who
, &chptr
->invites
);
486 rb_dlinkFindDestroy(chptr
, &who
->user
->invited
);
491 * input - channel to check bans for, user to check bans against
492 * optional prebuilt buffers
493 * output - 1 if banned, else 0
497 is_banned(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
498 const char *s
, const char *s2
)
500 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
501 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
502 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
505 struct Ban
*actualBan
= NULL
;
506 struct Ban
*actualExcept
= NULL
;
511 /* if the buffers havent been built, do it here */
514 rb_sprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
515 rb_sprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
520 if(who
->localClient
->mangledhost
!= NULL
)
522 /* if host mangling mode enabled, also check their real host */
523 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
525 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
528 /* if host mangling mode not enabled and no other spoof,
529 * also check the mangled form of their host */
530 else if (!IsDynSpoof(who
))
532 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
537 RB_DLINK_FOREACH(ptr
, chptr
->banlist
.head
)
539 actualBan
= ptr
->data
;
540 if(match(actualBan
->banstr
, s
) ||
541 match(actualBan
->banstr
, s2
) ||
542 match_cidr(actualBan
->banstr
, s2
) ||
543 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_BAN
) ||
544 (s3
!= NULL
&& match(actualBan
->banstr
, s3
)))
550 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
552 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
554 actualExcept
= ptr
->data
;
556 /* theyre exempted.. */
557 if(match(actualExcept
->banstr
, s
) ||
558 match(actualExcept
->banstr
, s2
) ||
559 match_cidr(actualExcept
->banstr
, s2
) ||
560 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
561 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
563 /* cache the fact theyre not banned */
566 msptr
->bants
= chptr
->bants
;
567 msptr
->flags
&= ~CHFL_BANNED
;
570 return CHFL_EXCEPTION
;
575 /* cache the banned/not banned status */
578 msptr
->bants
= chptr
->bants
;
580 if(actualBan
!= NULL
)
582 msptr
->flags
|= CHFL_BANNED
;
587 msptr
->flags
&= ~CHFL_BANNED
;
592 return ((actualBan
? CHFL_BAN
: 0));
597 * input - channel to check bans for, user to check bans against
598 * optional prebuilt buffers
599 * output - 1 if banned, else 0
603 is_quieted(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
604 const char *s
, const char *s2
)
606 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
607 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
608 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
611 struct Ban
*actualBan
= NULL
;
612 struct Ban
*actualExcept
= NULL
;
617 /* if the buffers havent been built, do it here */
620 rb_sprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
621 rb_sprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
626 if(who
->localClient
->mangledhost
!= NULL
)
628 /* if host mangling mode enabled, also check their real host */
629 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
631 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
634 /* if host mangling mode not enabled and no other spoof,
635 * also check the mangled form of their host */
636 else if (!IsDynSpoof(who
))
638 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
643 RB_DLINK_FOREACH(ptr
, chptr
->quietlist
.head
)
645 actualBan
= ptr
->data
;
646 if(match(actualBan
->banstr
, s
) ||
647 match(actualBan
->banstr
, s2
) ||
648 match_cidr(actualBan
->banstr
, s2
) ||
649 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_QUIET
) ||
650 (s3
!= NULL
&& match(actualBan
->banstr
, s3
)))
656 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
658 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
660 actualExcept
= ptr
->data
;
662 /* theyre exempted.. */
663 if(match(actualExcept
->banstr
, s
) ||
664 match(actualExcept
->banstr
, s2
) ||
665 match_cidr(actualExcept
->banstr
, s2
) ||
666 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
667 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
669 /* cache the fact theyre not banned */
672 msptr
->bants
= chptr
->bants
;
673 msptr
->flags
&= ~CHFL_BANNED
;
676 return CHFL_EXCEPTION
;
681 /* cache the banned/not banned status */
684 msptr
->bants
= chptr
->bants
;
686 if(actualBan
!= NULL
)
688 msptr
->flags
|= CHFL_BANNED
;
693 msptr
->flags
&= ~CHFL_BANNED
;
698 return ((actualBan
? CHFL_BAN
: 0));
703 * input - client to check, channel to check for, key
704 * output - reason for not being able to join, else 0
708 can_join(struct Client
*source_p
, struct Channel
*chptr
, char *key
)
710 rb_dlink_node
*invite
= NULL
;
712 struct Ban
*invex
= NULL
;
713 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
714 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
715 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
718 hook_data_channel moduledata
;
720 s_assert(source_p
->localClient
!= NULL
);
722 rb_sprintf(src_host
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
723 rb_sprintf(src_iphost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->sockhost
);
724 if(source_p
->localClient
->mangledhost
!= NULL
)
726 /* if host mangling mode enabled, also check their real host */
727 if(!strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
729 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->orighost
);
732 /* if host mangling mode not enabled and no other spoof,
733 * also check the mangled form of their host */
734 else if (!IsDynSpoof(source_p
))
736 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->localClient
->mangledhost
);
741 if((is_banned(chptr
, source_p
, NULL
, src_host
, src_iphost
)) == CHFL_BAN
)
742 return (ERR_BANNEDFROMCHAN
);
744 if(chptr
->mode
.mode
& MODE_INVITEONLY
)
746 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
748 if(invite
->data
== chptr
)
753 if(!ConfigChannel
.use_invex
)
754 return (ERR_INVITEONLYCHAN
);
755 RB_DLINK_FOREACH(ptr
, chptr
->invexlist
.head
)
758 if(match(invex
->banstr
, src_host
)
759 || match(invex
->banstr
, src_iphost
)
760 || match_cidr(invex
->banstr
, src_iphost
)
761 || match_extban(invex
->banstr
, source_p
, chptr
, CHFL_INVEX
)
762 || (use_althost
&& match(invex
->banstr
, src_althost
)))
766 return (ERR_INVITEONLYCHAN
);
770 if(*chptr
->mode
.key
&& (EmptyString(key
) || irccmp(chptr
->mode
.key
, key
)))
771 return (ERR_BADCHANNELKEY
);
773 if(chptr
->mode
.limit
&&
774 rb_dlink_list_length(&chptr
->members
) >= (unsigned long) chptr
->mode
.limit
)
775 i
= ERR_CHANNELISFULL
;
776 if(chptr
->mode
.mode
& MODE_REGONLY
&& EmptyString(source_p
->user
->suser
))
777 i
= ERR_NEEDREGGEDNICK
;
778 /* join throttling stuff --nenolod */
779 else if(chptr
->mode
.join_num
> 0 && chptr
->mode
.join_time
> 0)
781 if ((rb_current_time() - chptr
->join_delta
<=
782 chptr
->mode
.join_time
) && (chptr
->join_count
>=
783 chptr
->mode
.join_num
))
787 /* allow /invite to override +l/+r/+j also -- jilles */
788 if (i
!= 0 && invite
== NULL
)
790 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
792 if(invite
->data
== chptr
)
799 moduledata
.client
= source_p
;
800 moduledata
.chptr
= chptr
;
801 moduledata
.approved
= 0;
803 call_hook(h_can_join
, &moduledata
);
805 return moduledata
.approved
;
810 * input - user to check in channel, membership pointer
811 * output - whether can explicitly send or not, else CAN_SEND_NONOP
815 can_send(struct Channel
*chptr
, struct Client
*source_p
, struct membership
*msptr
)
817 if(IsServer(source_p
) || IsService(source_p
))
820 if(MyClient(source_p
) && hash_find_resv(chptr
->chname
) &&
821 !IsOper(source_p
) && !IsExemptResv(source_p
))
826 msptr
= find_channel_membership(chptr
, source_p
);
830 /* if its +m or +n and theyre not in the channel,
831 * they cant send. we dont check bans here because
832 * theres no possibility of caching them --fl
834 if(chptr
->mode
.mode
& MODE_NOPRIVMSGS
|| chptr
->mode
.mode
& MODE_MODERATED
)
837 return CAN_SEND_NONOP
;
841 if(is_chanop_voiced(msptr
))
844 if(chptr
->mode
.mode
& MODE_MODERATED
)
847 if(MyClient(source_p
))
849 /* cached can_send */
850 if(msptr
->bants
== chptr
->bants
)
852 if(can_send_banned(msptr
))
855 else if(is_banned(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
856 || is_quieted(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
)
860 return CAN_SEND_NONOP
;
863 /* find_bannickchange_channel()
864 * Input: client to check
865 * Output: channel preventing nick change
868 find_bannickchange_channel(struct Client
*client_p
)
870 struct Channel
*chptr
;
871 struct membership
*msptr
;
873 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
874 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
876 if (!MyClient(client_p
))
879 rb_sprintf(src_host
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->host
);
880 rb_sprintf(src_iphost
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->sockhost
);
882 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
885 chptr
= msptr
->chptr
;
886 if (is_chanop_voiced(msptr
))
888 /* cached can_send */
889 if (msptr
->bants
== chptr
->bants
)
891 if (can_send_banned(msptr
))
894 else if (is_banned(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
895 || is_quieted(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
)
901 /* void check_spambot_warning(struct Client *source_p)
902 * Input: Client to check, channel name or NULL if this is a part.
904 * Side-effects: Updates the client's oper_warn_count_down, warns the
905 * IRC operators if necessary, and updates join_leave_countdown as
909 check_spambot_warning(struct Client
*source_p
, const char *name
)
913 if((GlobalSetOptions
.spam_num
&&
914 (source_p
->localClient
->join_leave_count
>= GlobalSetOptions
.spam_num
)))
916 if(source_p
->localClient
->oper_warn_count_down
> 0)
917 source_p
->localClient
->oper_warn_count_down
--;
919 source_p
->localClient
->oper_warn_count_down
= 0;
920 if(source_p
->localClient
->oper_warn_count_down
== 0)
922 /* Its already known as a possible spambot */
924 sendto_realops_snomask(SNO_BOTS
, L_NETWIDE
,
925 "User %s (%s@%s) trying to join %s is a possible spambot",
927 source_p
->username
, source_p
->orighost
, name
);
929 sendto_realops_snomask(SNO_BOTS
, L_NETWIDE
,
930 "User %s (%s@%s) is a possible spambot",
932 source_p
->username
, source_p
->orighost
);
933 source_p
->localClient
->oper_warn_count_down
= OPER_SPAM_COUNTDOWN
;
939 (rb_current_time() - source_p
->localClient
->last_leave_time
)) >
940 JOIN_LEAVE_COUNT_EXPIRE_TIME
)
942 decrement_count
= (t_delta
/ JOIN_LEAVE_COUNT_EXPIRE_TIME
);
943 if(decrement_count
> source_p
->localClient
->join_leave_count
)
944 source_p
->localClient
->join_leave_count
= 0;
946 source_p
->localClient
->join_leave_count
-= decrement_count
;
950 if((rb_current_time() -
951 (source_p
->localClient
->last_join_time
)) < GlobalSetOptions
.spam_time
)
953 /* oh, its a possible spambot */
954 source_p
->localClient
->join_leave_count
++;
958 source_p
->localClient
->last_join_time
= rb_current_time();
960 source_p
->localClient
->last_leave_time
= rb_current_time();
968 * side effects - compares usercount and servercount against their split
969 * values and adjusts splitmode accordingly
972 check_splitmode(void *unused
)
974 if(splitchecking
&& (ConfigChannel
.no_join_on_split
|| ConfigChannel
.no_create_on_split
))
976 /* not split, we're being asked to check now because someone
981 if(eob_count
< split_servers
|| Count
.total
< split_users
)
984 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
985 "Network split, activating splitmode");
986 check_splitmode_ev
= rb_event_addish("check_splitmode", check_splitmode
, NULL
, 2);
989 /* in splitmode, check whether its finished */
990 else if(eob_count
>= split_servers
&& Count
.total
>= split_users
)
994 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
995 "Network rejoined, deactivating splitmode");
997 rb_event_delete(check_splitmode_ev
);
1005 * input - channel to allocate topic for
1006 * output - 1 on success, else 0
1007 * side effects - channel gets a topic allocated
1010 allocate_topic(struct Channel
*chptr
)
1017 ptr
= rb_bh_alloc(topic_heap
);
1019 /* Basically we allocate one large block for the topic and
1020 * the topic info. We then split it up into two and shove it
1024 chptr
->topic_info
= (char *) ptr
+ TOPICLEN
+ 1;
1025 *chptr
->topic
= '\0';
1026 *chptr
->topic_info
= '\0';
1031 * input - channel which has topic to free
1033 * side effects - channels topic is free'd
1036 free_topic(struct Channel
*chptr
)
1040 if(chptr
== NULL
|| chptr
->topic
== NULL
)
1043 /* This is safe for now - If you change allocate_topic you
1044 * MUST change this as well
1047 rb_bh_free(topic_heap
, ptr
);
1048 chptr
->topic
= NULL
;
1049 chptr
->topic_info
= NULL
;
1052 /* set_channel_topic()
1054 * input - channel, topic to set, topic info and topic ts
1056 * side effects - channels topic, topic info and TS are set.
1059 set_channel_topic(struct Channel
*chptr
, const char *topic
, const char *topic_info
, time_t topicts
)
1061 if(strlen(topic
) > 0)
1063 if(chptr
->topic
== NULL
)
1064 allocate_topic(chptr
);
1065 rb_strlcpy(chptr
->topic
, topic
, TOPICLEN
+ 1);
1066 rb_strlcpy(chptr
->topic_info
, topic_info
, USERHOST_REPLYLEN
);
1067 chptr
->topic_time
= topicts
;
1071 if(chptr
->topic
!= NULL
)
1073 chptr
->topic_time
= 0;
1079 * inputs - pointer to channel
1080 * - pointer to client
1081 * output - string with simple modes
1082 * side effects - result from previous calls overwritten
1084 * Stolen from ShadowIRCd 4 --nenolod
1087 channel_modes(struct Channel
*chptr
, struct Client
*client_p
)
1092 static char final
[BUFSIZE
];
1099 for (i
= 0; i
< 256; i
++)
1100 if(chptr
->mode
.mode
& chmode_flags
[i
])
1103 if(chptr
->mode
.limit
)
1107 if(!IsClient(client_p
) || IsMember(client_p
, chptr
))
1108 pbuf
+= rb_sprintf(pbuf
, " %d", chptr
->mode
.limit
);
1111 if(*chptr
->mode
.key
)
1115 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1116 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.key
);
1119 if(chptr
->mode
.join_num
)
1123 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1124 pbuf
+= rb_sprintf(pbuf
, " %d:%d", chptr
->mode
.join_num
,
1125 chptr
->mode
.join_time
);
1128 if(*chptr
->mode
.forward
&& (ConfigChannel
.use_forward
|| !IsClient(client_p
)))
1132 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1133 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.forward
);
1138 rb_strlcpy(final
, buf1
, sizeof final
);
1139 rb_strlcat(final
, buf2
, sizeof final
);
1143 /* Now lets do some stuff to keep track of what combinations of
1145 * Note that the number of combinations doubles each time you add
1146 * something to this list. Each one is only quick if no servers use that
1147 * combination, but if the numbers get too high here MODE will get too
1148 * slow. I suggest if you get more than 7 here, you consider getting rid
1149 * of some and merging or something. If it wasn't for irc+cs we would
1150 * probably not even need to bother about most of these, but unfortunately
1154 /* void init_chcap_usage_counts(void)
1158 * Side-effects - Initialises the usage counts to zero. Fills in the
1159 * chcap_yes and chcap_no combination tables.
1162 init_chcap_usage_counts(void)
1164 unsigned long m
, c
, y
, n
;
1166 memset(chcap_combos
, 0, sizeof(chcap_combos
));
1168 /* For every possible combination */
1169 for (m
= 0; m
< NCHCAP_COMBOS
; m
++)
1171 /* Check each capab */
1172 for (c
= y
= n
= 0; c
< NCHCAPS
; c
++)
1174 if((m
& (1 << c
)) == 0)
1175 n
|= channel_capabs
[c
];
1177 y
|= channel_capabs
[c
];
1179 chcap_combos
[m
].cap_yes
= y
;
1180 chcap_combos
[m
].cap_no
= n
;
1184 /* void set_chcap_usage_counts(struct Client *serv_p)
1185 * Input: serv_p; The client whose capabs to register.
1187 * Side-effects: Increments the usage counts for the correct capab
1191 set_chcap_usage_counts(struct Client
*serv_p
)
1195 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1197 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1198 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1200 chcap_combos
[n
].count
++;
1205 /* This should be impossible -A1kmm. */
1209 /* void set_chcap_usage_counts(struct Client *serv_p)
1211 * Inputs - serv_p; The client whose capabs to register.
1213 * Side-effects - Decrements the usage counts for the correct capab
1217 unset_chcap_usage_counts(struct Client
*serv_p
)
1221 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1223 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1224 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1226 /* Hopefully capabs can't change dynamically or anything... */
1227 s_assert(chcap_combos
[n
].count
> 0);
1229 if(chcap_combos
[n
].count
> 0)
1230 chcap_combos
[n
].count
--;
1235 /* This should be impossible -A1kmm. */
1239 /* void send_cap_mode_changes(struct Client *client_p,
1240 * struct Client *source_p,
1241 * struct Channel *chptr, int cap, int nocap)
1242 * Input: The client sending(client_p), the source client(source_p),
1243 * the channel to send mode changes for(chptr)
1245 * Side-effects: Sends the appropriate mode changes to capable servers.
1247 * Reverted back to my original design, except that we now keep a count
1248 * of the number of servers which each combination as an optimisation, so
1249 * the capabs combinations which are not needed are not worked out. -A1kmm
1252 send_cap_mode_changes(struct Client
*client_p
, struct Client
*source_p
,
1253 struct Channel
*chptr
, struct ChModeChange mode_changes
[], int mode_count
)
1255 static char modebuf
[BUFSIZE
];
1256 static char parabuf
[BUFSIZE
];
1257 int i
, mbl
, pbl
, nc
, mc
, preflen
, len
;
1266 /* Now send to servers... */
1267 for (j
= 0; j
< NCHCAP_COMBOS
; j
++)
1269 if(chcap_combos
[j
].count
== 0)
1279 cap
= chcap_combos
[j
].cap_yes
;
1280 nocap
= chcap_combos
[j
].cap_no
;
1282 mbl
= preflen
= rb_sprintf(modebuf
, ":%s TMODE %ld %s ",
1283 use_id(source_p
), (long) chptr
->channelts
,
1286 /* loop the list of - modes we have */
1287 for (i
= 0; i
< mode_count
; i
++)
1289 /* if they dont support the cap we need, or they do support a cap they
1290 * cant have, then dont add it to the modebuf.. that way they wont see
1293 if((mode_changes
[i
].letter
== 0) ||
1294 ((cap
& mode_changes
[i
].caps
) != mode_changes
[i
].caps
)
1295 || ((nocap
& mode_changes
[i
].nocaps
) != mode_changes
[i
].nocaps
))
1298 if(!EmptyString(mode_changes
[i
].id
))
1299 arg
= mode_changes
[i
].id
;
1301 arg
= mode_changes
[i
].arg
;
1305 arglen
= strlen(arg
);
1307 /* dont even think about it! --fl */
1308 if(arglen
> MODEBUFLEN
- 5)
1312 /* if we're creeping past the buf size, we need to send it and make
1313 * another line for the other modes
1314 * XXX - this could give away server topology with uids being
1315 * different lengths, but not much we can do, except possibly break
1316 * them as if they were the longest of the nick or uid at all times,
1317 * which even then won't work as we don't always know the uid -A1kmm.
1319 if(arg
&& ((mc
== MAXMODEPARAMSSERV
) ||
1320 ((mbl
+ pbl
+ arglen
+ 4) > (BUFSIZE
- 3))))
1323 sendto_server(client_p
, chptr
, cap
, nocap
,
1324 "%s %s", modebuf
, parabuf
);
1335 if(dir
!= mode_changes
[i
].dir
)
1337 modebuf
[mbl
++] = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1338 dir
= mode_changes
[i
].dir
;
1341 modebuf
[mbl
++] = mode_changes
[i
].letter
;
1347 len
= rb_sprintf(pbuf
, "%s ", arg
);
1354 if(pbl
&& parabuf
[pbl
- 1] == ' ')
1355 parabuf
[pbl
- 1] = 0;
1358 sendto_server(client_p
, chptr
, cap
, nocap
, "%s %s", modebuf
, parabuf
);