2 * ircd-ratbox: A slightly useful ircd.
3 * channel.c: Controls channels.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 * $Id: channel.c 3580 2007-11-07 23:45:14Z jilles $
37 #include "s_serv.h" /* captab */
41 #include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
42 #include "s_newconf.h"
45 struct config_channel_entry ConfigChannel
;
46 rb_dlink_list global_channel_list
;
47 static rb_bh
*channel_heap
;
48 static rb_bh
*ban_heap
;
49 static rb_bh
*topic_heap
;
50 static rb_bh
*member_heap
;
52 static int channel_capabs
[] = { CAP_EX
, CAP_IE
,
57 #define NCHCAPS (sizeof(channel_capabs)/sizeof(int))
58 #define NCHCAP_COMBOS (1 << NCHCAPS)
60 static struct ChCapCombo chcap_combos
[NCHCAP_COMBOS
];
62 static void free_topic(struct Channel
*chptr
);
64 static int h_can_join
;
70 * side effects - initialises the various blockheaps
75 channel_heap
= rb_bh_create(sizeof(struct Channel
), CHANNEL_HEAP_SIZE
, "channel_heap");
76 ban_heap
= rb_bh_create(sizeof(struct Ban
), BAN_HEAP_SIZE
, "ban_heap");
77 topic_heap
= rb_bh_create(TOPICLEN
+ 1 + USERHOST_REPLYLEN
, TOPIC_HEAP_SIZE
, "topic_heap");
78 member_heap
= rb_bh_create(sizeof(struct membership
), MEMBER_HEAP_SIZE
, "member_heap");
80 h_can_join
= register_hook("can_join");
84 * allocate_channel - Allocates a channel
87 allocate_channel(const char *chname
)
89 struct Channel
*chptr
;
90 chptr
= rb_bh_alloc(channel_heap
);
91 chptr
->chname
= rb_strdup(chname
);
96 free_channel(struct Channel
*chptr
)
98 rb_free(chptr
->chname
);
99 rb_bh_free(channel_heap
, chptr
);
103 allocate_ban(const char *banstr
, const char *who
)
106 bptr
= rb_bh_alloc(ban_heap
);
107 bptr
->banstr
= rb_strdup(banstr
);
108 bptr
->who
= rb_strdup(who
);
114 free_ban(struct Ban
*bptr
)
116 rb_free(bptr
->banstr
);
118 rb_bh_free(ban_heap
, bptr
);
122 /* find_channel_membership()
124 * input - channel to find them in, client to find
125 * output - membership of client in channel, else NULL
129 find_channel_membership(struct Channel
*chptr
, struct Client
*client_p
)
131 struct membership
*msptr
;
134 if(!IsClient(client_p
))
137 /* Pick the most efficient list to use to be nice to things like
138 * CHANSERV which could be in a large number of channels
140 if(rb_dlink_list_length(&chptr
->members
) < rb_dlink_list_length(&client_p
->user
->channel
))
142 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
146 if(msptr
->client_p
== client_p
)
152 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
156 if(msptr
->chptr
== chptr
)
164 /* find_channel_status()
166 * input - membership to get status for, whether we can combine flags
167 * output - flags of user on channel
171 find_channel_status(struct membership
*msptr
, int combine
)
173 static char buffer
[3];
192 /* add_user_to_channel()
194 * input - channel to add client to, client to add, channel flags
196 * side effects - user is added to channel
199 add_user_to_channel(struct Channel
*chptr
, struct Client
*client_p
, int flags
)
201 struct membership
*msptr
;
203 s_assert(client_p
->user
!= NULL
);
204 if(client_p
->user
== NULL
)
207 msptr
= rb_bh_alloc(member_heap
);
209 msptr
->chptr
= chptr
;
210 msptr
->client_p
= client_p
;
211 msptr
->flags
= flags
;
213 rb_dlinkAdd(msptr
, &msptr
->usernode
, &client_p
->user
->channel
);
214 rb_dlinkAdd(msptr
, &msptr
->channode
, &chptr
->members
);
216 if(MyClient(client_p
))
217 rb_dlinkAdd(msptr
, &msptr
->locchannode
, &chptr
->locmembers
);
220 /* remove_user_from_channel()
222 * input - membership pointer to remove from channel
224 * side effects - membership (thus user) is removed from channel
227 remove_user_from_channel(struct membership
*msptr
)
229 struct Client
*client_p
;
230 struct Channel
*chptr
;
231 s_assert(msptr
!= NULL
);
235 client_p
= msptr
->client_p
;
236 chptr
= msptr
->chptr
;
238 rb_dlinkDelete(&msptr
->usernode
, &client_p
->user
->channel
);
239 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
241 if(client_p
->servptr
== &me
)
242 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
244 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
245 destroy_channel(chptr
);
247 rb_bh_free(member_heap
, msptr
);
252 /* remove_user_from_channels()
254 * input - user to remove from all channels
256 * side effects - user is removed from all channels
259 remove_user_from_channels(struct Client
*client_p
)
261 struct Channel
*chptr
;
262 struct membership
*msptr
;
264 rb_dlink_node
*next_ptr
;
269 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->user
->channel
.head
)
272 chptr
= msptr
->chptr
;
274 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
276 if(client_p
->servptr
== &me
)
277 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
279 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
280 destroy_channel(chptr
);
282 rb_bh_free(member_heap
, msptr
);
285 client_p
->user
->channel
.head
= client_p
->user
->channel
.tail
= NULL
;
286 client_p
->user
->channel
.length
= 0;
289 /* invalidate_bancache_user()
291 * input - user to invalidate ban cache for
293 * side effects - ban cache is invalidated for all memberships of that user
294 * to be used after a nick change
297 invalidate_bancache_user(struct Client
*client_p
)
299 struct membership
*msptr
;
305 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
309 msptr
->flags
&= ~CHFL_BANNED
;
313 /* check_channel_name()
315 * input - channel name
316 * output - 1 if valid channel name, else 0
320 check_channel_name(const char *name
)
322 s_assert(name
!= NULL
);
326 for (; *name
; ++name
)
328 if(!IsChanChar(*name
))
335 /* free_channel_list()
337 * input - rb_dlink list to free
339 * side effects - list of b/e/I modes is cleared
342 free_channel_list(rb_dlink_list
* list
)
345 rb_dlink_node
*next_ptr
;
346 struct Ban
*actualBan
;
348 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
350 actualBan
= ptr
->data
;
354 list
->head
= list
->tail
= NULL
;
360 * input - channel to destroy
362 * side effects - channel is obliterated
365 destroy_channel(struct Channel
*chptr
)
367 rb_dlink_node
*ptr
, *next_ptr
;
369 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
371 del_invite(chptr
, ptr
->data
);
374 /* free all bans/exceptions/denies */
375 free_channel_list(&chptr
->banlist
);
376 free_channel_list(&chptr
->exceptlist
);
377 free_channel_list(&chptr
->invexlist
);
378 free_channel_list(&chptr
->quietlist
);
383 rb_dlinkDelete(&chptr
->node
, &global_channel_list
);
384 del_from_channel_hash(chptr
->chname
, chptr
);
388 /* channel_pub_or_secret()
391 * output - "=" if public, "@" if secret, else "*"
395 channel_pub_or_secret(struct Channel
*chptr
)
397 if(PubChannel(chptr
))
399 else if(SecretChannel(chptr
))
404 /* channel_member_names()
406 * input - channel to list, client to list to, show endofnames
408 * side effects - client is given list of users on channel
411 channel_member_names(struct Channel
*chptr
, struct Client
*client_p
, int show_eon
)
413 struct membership
*msptr
;
414 struct Client
*target_p
;
422 int stack
= IsCapable(client_p
, CLICAP_MULTI_PREFIX
);
424 if(ShowChannel(client_p
, chptr
))
426 is_member
= IsMember(client_p
, chptr
);
428 cur_len
= mlen
= rb_sprintf(lbuf
, form_str(RPL_NAMREPLY
),
429 me
.name
, client_p
->name
,
430 channel_pub_or_secret(chptr
), chptr
->chname
);
434 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
437 target_p
= msptr
->client_p
;
439 if(IsInvisible(target_p
) && !is_member
)
442 /* space, possible "@+" prefix */
443 if(cur_len
+ strlen(target_p
->name
) + 3 >= BUFSIZE
- 3)
446 sendto_one(client_p
, "%s", lbuf
);
451 tlen
= rb_sprintf(t
, "%s%s ", find_channel_status(msptr
, stack
),
458 /* The old behaviour here was to always output our buffer,
459 * even if there are no clients we can show. This happens
460 * when a client does "NAMES" with no parameters, and all
461 * the clients on a -sp channel are +i. I dont see a good
462 * reason for keeping that behaviour, as it just wastes
468 sendto_one(client_p
, "%s", lbuf
);
473 sendto_one(client_p
, form_str(RPL_ENDOFNAMES
),
474 me
.name
, client_p
->name
, chptr
->chname
);
479 * input - channel to remove invite from, client to remove
481 * side effects - user is removed from invite list, if exists
484 del_invite(struct Channel
*chptr
, struct Client
*who
)
486 rb_dlinkFindDestroy(who
, &chptr
->invites
);
487 rb_dlinkFindDestroy(chptr
, &who
->user
->invited
);
492 * input - channel to check bans for, user to check bans against
493 * optional prebuilt buffers
494 * output - 1 if banned, else 0
498 is_banned(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
499 const char *s
, const char *s2
)
501 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
502 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
503 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
506 struct Ban
*actualBan
= NULL
;
507 struct Ban
*actualExcept
= NULL
;
512 /* if the buffers havent been built, do it here */
515 rb_sprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
516 rb_sprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
521 if(who
->localClient
->mangledhost
!= NULL
)
523 /* if host mangling mode enabled, also check their real host */
524 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
526 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
529 /* if host mangling mode not enabled and no other spoof,
530 * also check the mangled form of their host */
531 else if (!IsDynSpoof(who
))
533 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
538 RB_DLINK_FOREACH(ptr
, chptr
->banlist
.head
)
540 actualBan
= ptr
->data
;
541 if(match(actualBan
->banstr
, s
) ||
542 match(actualBan
->banstr
, s2
) ||
543 match_cidr(actualBan
->banstr
, s2
) ||
544 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_BAN
) ||
545 (s3
!= NULL
&& match(actualBan
->banstr
, s3
)))
551 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
553 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
555 actualExcept
= ptr
->data
;
557 /* theyre exempted.. */
558 if(match(actualExcept
->banstr
, s
) ||
559 match(actualExcept
->banstr
, s2
) ||
560 match_cidr(actualExcept
->banstr
, s2
) ||
561 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
562 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
564 /* cache the fact theyre not banned */
567 msptr
->bants
= chptr
->bants
;
568 msptr
->flags
&= ~CHFL_BANNED
;
571 return CHFL_EXCEPTION
;
576 /* cache the banned/not banned status */
579 msptr
->bants
= chptr
->bants
;
581 if(actualBan
!= NULL
)
583 msptr
->flags
|= CHFL_BANNED
;
588 msptr
->flags
&= ~CHFL_BANNED
;
593 return ((actualBan
? CHFL_BAN
: 0));
598 * input - channel to check bans for, user to check bans against
599 * optional prebuilt buffers
600 * output - 1 if banned, else 0
604 is_quieted(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
605 const char *s
, const char *s2
)
607 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
608 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
609 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
612 struct Ban
*actualBan
= NULL
;
613 struct Ban
*actualExcept
= NULL
;
618 /* if the buffers havent been built, do it here */
621 rb_sprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
622 rb_sprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
627 if(who
->localClient
->mangledhost
!= NULL
)
629 /* if host mangling mode enabled, also check their real host */
630 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
632 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
635 /* if host mangling mode not enabled and no other spoof,
636 * also check the mangled form of their host */
637 else if (!IsDynSpoof(who
))
639 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
644 RB_DLINK_FOREACH(ptr
, chptr
->quietlist
.head
)
646 actualBan
= ptr
->data
;
647 if(match(actualBan
->banstr
, s
) ||
648 match(actualBan
->banstr
, s2
) ||
649 match_cidr(actualBan
->banstr
, s2
) ||
650 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_QUIET
) ||
651 (s3
!= NULL
&& match(actualBan
->banstr
, s3
)))
657 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
659 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
661 actualExcept
= ptr
->data
;
663 /* theyre exempted.. */
664 if(match(actualExcept
->banstr
, s
) ||
665 match(actualExcept
->banstr
, s2
) ||
666 match_cidr(actualExcept
->banstr
, s2
) ||
667 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
668 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
670 /* cache the fact theyre not banned */
673 msptr
->bants
= chptr
->bants
;
674 msptr
->flags
&= ~CHFL_BANNED
;
677 return CHFL_EXCEPTION
;
682 /* cache the banned/not banned status */
685 msptr
->bants
= chptr
->bants
;
687 if(actualBan
!= NULL
)
689 msptr
->flags
|= CHFL_BANNED
;
694 msptr
->flags
&= ~CHFL_BANNED
;
699 return ((actualBan
? CHFL_BAN
: 0));
704 * input - client to check, channel to check for, key
705 * output - reason for not being able to join, else 0
709 can_join(struct Client
*source_p
, struct Channel
*chptr
, char *key
)
711 rb_dlink_node
*invite
= NULL
;
713 struct Ban
*invex
= NULL
;
714 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
715 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
716 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
719 hook_data_channel moduledata
;
721 s_assert(source_p
->localClient
!= NULL
);
723 rb_sprintf(src_host
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
724 rb_sprintf(src_iphost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->sockhost
);
725 if(source_p
->localClient
->mangledhost
!= NULL
)
727 /* if host mangling mode enabled, also check their real host */
728 if(!strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
730 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->orighost
);
733 /* if host mangling mode not enabled and no other spoof,
734 * also check the mangled form of their host */
735 else if (!IsDynSpoof(source_p
))
737 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->localClient
->mangledhost
);
742 if((is_banned(chptr
, source_p
, NULL
, src_host
, src_iphost
)) == CHFL_BAN
)
743 return (ERR_BANNEDFROMCHAN
);
745 if(chptr
->mode
.mode
& MODE_INVITEONLY
)
747 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
749 if(invite
->data
== chptr
)
754 if(!ConfigChannel
.use_invex
)
755 return (ERR_INVITEONLYCHAN
);
756 RB_DLINK_FOREACH(ptr
, chptr
->invexlist
.head
)
759 if(match(invex
->banstr
, src_host
)
760 || match(invex
->banstr
, src_iphost
)
761 || match_cidr(invex
->banstr
, src_iphost
)
762 || match_extban(invex
->banstr
, source_p
, chptr
, CHFL_INVEX
)
763 || (use_althost
&& match(invex
->banstr
, src_althost
)))
767 return (ERR_INVITEONLYCHAN
);
771 if(*chptr
->mode
.key
&& (EmptyString(key
) || irccmp(chptr
->mode
.key
, key
)))
772 return (ERR_BADCHANNELKEY
);
774 if(chptr
->mode
.limit
&&
775 rb_dlink_list_length(&chptr
->members
) >= (unsigned long) chptr
->mode
.limit
)
776 i
= ERR_CHANNELISFULL
;
777 if(chptr
->mode
.mode
& MODE_REGONLY
&& EmptyString(source_p
->user
->suser
))
778 i
= ERR_NEEDREGGEDNICK
;
779 /* join throttling stuff --nenolod */
780 else if(chptr
->mode
.join_num
> 0 && chptr
->mode
.join_time
> 0)
782 if ((rb_current_time() - chptr
->join_delta
<=
783 chptr
->mode
.join_time
) && (chptr
->join_count
>=
784 chptr
->mode
.join_num
))
788 /* allow /invite to override +l/+r/+j also -- jilles */
789 if (i
!= 0 && invite
== NULL
)
791 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
793 if(invite
->data
== chptr
)
800 moduledata
.client
= source_p
;
801 moduledata
.chptr
= chptr
;
802 moduledata
.approved
= 0;
804 call_hook(h_can_join
, &moduledata
);
806 return moduledata
.approved
;
811 * input - user to check in channel, membership pointer
812 * output - whether can explicitly send or not, else CAN_SEND_NONOP
816 can_send(struct Channel
*chptr
, struct Client
*source_p
, struct membership
*msptr
)
818 if(IsServer(source_p
) || IsService(source_p
))
821 if(MyClient(source_p
) && hash_find_resv(chptr
->chname
) &&
822 !IsOper(source_p
) && !IsExemptResv(source_p
))
827 msptr
= find_channel_membership(chptr
, source_p
);
831 /* if its +m or +n and theyre not in the channel,
832 * they cant send. we dont check bans here because
833 * theres no possibility of caching them --fl
835 if(chptr
->mode
.mode
& MODE_NOPRIVMSGS
|| chptr
->mode
.mode
& MODE_MODERATED
)
838 return CAN_SEND_NONOP
;
842 if(is_chanop_voiced(msptr
))
845 if(chptr
->mode
.mode
& MODE_MODERATED
)
848 if(MyClient(source_p
))
850 /* cached can_send */
851 if(msptr
->bants
== chptr
->bants
)
853 if(can_send_banned(msptr
))
856 else if(is_banned(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
857 || is_quieted(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
)
861 return CAN_SEND_NONOP
;
864 /* find_bannickchange_channel()
865 * Input: client to check
866 * Output: channel preventing nick change
869 find_bannickchange_channel(struct Client
*client_p
)
871 struct Channel
*chptr
;
872 struct membership
*msptr
;
874 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
875 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
877 if (!MyClient(client_p
))
880 rb_sprintf(src_host
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->host
);
881 rb_sprintf(src_iphost
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->sockhost
);
883 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
886 chptr
= msptr
->chptr
;
887 if (is_chanop_voiced(msptr
))
889 /* cached can_send */
890 if (msptr
->bants
== chptr
->bants
)
892 if (can_send_banned(msptr
))
895 else if (is_banned(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
896 || is_quieted(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
)
902 /* void check_spambot_warning(struct Client *source_p)
903 * Input: Client to check, channel name or NULL if this is a part.
905 * Side-effects: Updates the client's oper_warn_count_down, warns the
906 * IRC operators if necessary, and updates join_leave_countdown as
910 check_spambot_warning(struct Client
*source_p
, const char *name
)
914 if((GlobalSetOptions
.spam_num
&&
915 (source_p
->localClient
->join_leave_count
>= GlobalSetOptions
.spam_num
)))
917 if(source_p
->localClient
->oper_warn_count_down
> 0)
918 source_p
->localClient
->oper_warn_count_down
--;
920 source_p
->localClient
->oper_warn_count_down
= 0;
921 if(source_p
->localClient
->oper_warn_count_down
== 0 &&
924 /* Its already known as a possible spambot */
925 sendto_realops_snomask(SNO_BOTS
, L_NETWIDE
,
926 "User %s (%s@%s) trying to join %s is a possible spambot",
928 source_p
->username
, source_p
->orighost
, name
);
929 source_p
->localClient
->oper_warn_count_down
= OPER_SPAM_COUNTDOWN
;
935 (rb_current_time() - source_p
->localClient
->last_leave_time
)) >
936 JOIN_LEAVE_COUNT_EXPIRE_TIME
)
938 decrement_count
= (t_delta
/ JOIN_LEAVE_COUNT_EXPIRE_TIME
);
939 if(decrement_count
> source_p
->localClient
->join_leave_count
)
940 source_p
->localClient
->join_leave_count
= 0;
942 source_p
->localClient
->join_leave_count
-= decrement_count
;
946 if((rb_current_time() -
947 (source_p
->localClient
->last_join_time
)) < GlobalSetOptions
.spam_time
)
949 /* oh, its a possible spambot */
950 source_p
->localClient
->join_leave_count
++;
954 source_p
->localClient
->last_join_time
= rb_current_time();
956 source_p
->localClient
->last_leave_time
= rb_current_time();
964 * side effects - compares usercount and servercount against their split
965 * values and adjusts splitmode accordingly
968 check_splitmode(void *unused
)
970 if(splitchecking
&& (ConfigChannel
.no_join_on_split
|| ConfigChannel
.no_create_on_split
))
972 /* not split, we're being asked to check now because someone
977 if(eob_count
< split_servers
|| Count
.total
< split_users
)
980 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
981 "Network split, activating splitmode");
982 check_splitmode_ev
= rb_event_addish("check_splitmode", check_splitmode
, NULL
, 2);
985 /* in splitmode, check whether its finished */
986 else if(eob_count
>= split_servers
&& Count
.total
>= split_users
)
990 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
991 "Network rejoined, deactivating splitmode");
993 rb_event_delete(check_splitmode_ev
);
994 check_splitmode_ev
= NULL
;
1002 * input - channel to allocate topic for
1003 * output - 1 on success, else 0
1004 * side effects - channel gets a topic allocated
1007 allocate_topic(struct Channel
*chptr
)
1014 ptr
= rb_bh_alloc(topic_heap
);
1016 /* Basically we allocate one large block for the topic and
1017 * the topic info. We then split it up into two and shove it
1021 chptr
->topic_info
= (char *) ptr
+ TOPICLEN
+ 1;
1022 *chptr
->topic
= '\0';
1023 *chptr
->topic_info
= '\0';
1028 * input - channel which has topic to free
1030 * side effects - channels topic is free'd
1033 free_topic(struct Channel
*chptr
)
1037 if(chptr
== NULL
|| chptr
->topic
== NULL
)
1040 /* This is safe for now - If you change allocate_topic you
1041 * MUST change this as well
1044 rb_bh_free(topic_heap
, ptr
);
1045 chptr
->topic
= NULL
;
1046 chptr
->topic_info
= NULL
;
1049 /* set_channel_topic()
1051 * input - channel, topic to set, topic info and topic ts
1053 * side effects - channels topic, topic info and TS are set.
1056 set_channel_topic(struct Channel
*chptr
, const char *topic
, const char *topic_info
, time_t topicts
)
1058 if(strlen(topic
) > 0)
1060 if(chptr
->topic
== NULL
)
1061 allocate_topic(chptr
);
1062 rb_strlcpy(chptr
->topic
, topic
, TOPICLEN
+ 1);
1063 rb_strlcpy(chptr
->topic_info
, topic_info
, USERHOST_REPLYLEN
);
1064 chptr
->topic_time
= topicts
;
1068 if(chptr
->topic
!= NULL
)
1070 chptr
->topic_time
= 0;
1076 * inputs - pointer to channel
1077 * - pointer to client
1078 * output - string with simple modes
1079 * side effects - result from previous calls overwritten
1081 * Stolen from ShadowIRCd 4 --nenolod
1084 channel_modes(struct Channel
*chptr
, struct Client
*client_p
)
1089 static char final
[BUFSIZE
];
1096 for (i
= 0; i
< 256; i
++)
1097 if(chptr
->mode
.mode
& chmode_flags
[i
])
1100 if(chptr
->mode
.limit
)
1104 if(!IsClient(client_p
) || IsMember(client_p
, chptr
))
1105 pbuf
+= rb_sprintf(pbuf
, " %d", chptr
->mode
.limit
);
1108 if(*chptr
->mode
.key
)
1112 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1113 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.key
);
1116 if(chptr
->mode
.join_num
)
1120 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1121 pbuf
+= rb_sprintf(pbuf
, " %d:%d", chptr
->mode
.join_num
,
1122 chptr
->mode
.join_time
);
1125 if(*chptr
->mode
.forward
&& (ConfigChannel
.use_forward
|| !IsClient(client_p
)))
1129 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1130 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.forward
);
1135 rb_strlcpy(final
, buf1
, sizeof final
);
1136 rb_strlcat(final
, buf2
, sizeof final
);
1140 /* Now lets do some stuff to keep track of what combinations of
1142 * Note that the number of combinations doubles each time you add
1143 * something to this list. Each one is only quick if no servers use that
1144 * combination, but if the numbers get too high here MODE will get too
1145 * slow. I suggest if you get more than 7 here, you consider getting rid
1146 * of some and merging or something. If it wasn't for irc+cs we would
1147 * probably not even need to bother about most of these, but unfortunately
1151 /* void init_chcap_usage_counts(void)
1155 * Side-effects - Initialises the usage counts to zero. Fills in the
1156 * chcap_yes and chcap_no combination tables.
1159 init_chcap_usage_counts(void)
1161 unsigned long m
, c
, y
, n
;
1163 memset(chcap_combos
, 0, sizeof(chcap_combos
));
1165 /* For every possible combination */
1166 for (m
= 0; m
< NCHCAP_COMBOS
; m
++)
1168 /* Check each capab */
1169 for (c
= y
= n
= 0; c
< NCHCAPS
; c
++)
1171 if((m
& (1 << c
)) == 0)
1172 n
|= channel_capabs
[c
];
1174 y
|= channel_capabs
[c
];
1176 chcap_combos
[m
].cap_yes
= y
;
1177 chcap_combos
[m
].cap_no
= n
;
1181 /* void set_chcap_usage_counts(struct Client *serv_p)
1182 * Input: serv_p; The client whose capabs to register.
1184 * Side-effects: Increments the usage counts for the correct capab
1188 set_chcap_usage_counts(struct Client
*serv_p
)
1192 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1194 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1195 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1197 chcap_combos
[n
].count
++;
1202 /* This should be impossible -A1kmm. */
1206 /* void set_chcap_usage_counts(struct Client *serv_p)
1208 * Inputs - serv_p; The client whose capabs to register.
1210 * Side-effects - Decrements the usage counts for the correct capab
1214 unset_chcap_usage_counts(struct Client
*serv_p
)
1218 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1220 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1221 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1223 /* Hopefully capabs can't change dynamically or anything... */
1224 s_assert(chcap_combos
[n
].count
> 0);
1226 if(chcap_combos
[n
].count
> 0)
1227 chcap_combos
[n
].count
--;
1232 /* This should be impossible -A1kmm. */
1236 /* void send_cap_mode_changes(struct Client *client_p,
1237 * struct Client *source_p,
1238 * struct Channel *chptr, int cap, int nocap)
1239 * Input: The client sending(client_p), the source client(source_p),
1240 * the channel to send mode changes for(chptr)
1242 * Side-effects: Sends the appropriate mode changes to capable servers.
1244 * Reverted back to my original design, except that we now keep a count
1245 * of the number of servers which each combination as an optimisation, so
1246 * the capabs combinations which are not needed are not worked out. -A1kmm
1249 send_cap_mode_changes(struct Client
*client_p
, struct Client
*source_p
,
1250 struct Channel
*chptr
, struct ChModeChange mode_changes
[], int mode_count
)
1252 static char modebuf
[BUFSIZE
];
1253 static char parabuf
[BUFSIZE
];
1254 int i
, mbl
, pbl
, nc
, mc
, preflen
, len
;
1263 /* Now send to servers... */
1264 for (j
= 0; j
< NCHCAP_COMBOS
; j
++)
1266 if(chcap_combos
[j
].count
== 0)
1276 cap
= chcap_combos
[j
].cap_yes
;
1277 nocap
= chcap_combos
[j
].cap_no
;
1279 mbl
= preflen
= rb_sprintf(modebuf
, ":%s TMODE %ld %s ",
1280 use_id(source_p
), (long) chptr
->channelts
,
1283 /* loop the list of - modes we have */
1284 for (i
= 0; i
< mode_count
; i
++)
1286 /* if they dont support the cap we need, or they do support a cap they
1287 * cant have, then dont add it to the modebuf.. that way they wont see
1290 if((mode_changes
[i
].letter
== 0) ||
1291 ((cap
& mode_changes
[i
].caps
) != mode_changes
[i
].caps
)
1292 || ((nocap
& mode_changes
[i
].nocaps
) != mode_changes
[i
].nocaps
))
1295 if(!EmptyString(mode_changes
[i
].id
))
1296 arg
= mode_changes
[i
].id
;
1298 arg
= mode_changes
[i
].arg
;
1302 arglen
= strlen(arg
);
1304 /* dont even think about it! --fl */
1305 if(arglen
> MODEBUFLEN
- 5)
1309 /* if we're creeping past the buf size, we need to send it and make
1310 * another line for the other modes
1311 * XXX - this could give away server topology with uids being
1312 * different lengths, but not much we can do, except possibly break
1313 * them as if they were the longest of the nick or uid at all times,
1314 * which even then won't work as we don't always know the uid -A1kmm.
1316 if(arg
&& ((mc
== MAXMODEPARAMSSERV
) ||
1317 ((mbl
+ pbl
+ arglen
+ 4) > (BUFSIZE
- 3))))
1320 sendto_server(client_p
, chptr
, cap
, nocap
,
1321 "%s %s", modebuf
, parabuf
);
1332 if(dir
!= mode_changes
[i
].dir
)
1334 modebuf
[mbl
++] = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1335 dir
= mode_changes
[i
].dir
;
1338 modebuf
[mbl
++] = mode_changes
[i
].letter
;
1344 len
= rb_sprintf(pbuf
, "%s ", arg
);
1351 if(pbl
&& parabuf
[pbl
- 1] == ' ')
1352 parabuf
[pbl
- 1] = 0;
1355 sendto_server(client_p
, chptr
, cap
, nocap
, "%s %s", modebuf
, parabuf
);