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
;
65 static int h_can_send
;
66 int h_get_channel_access
;
72 * side effects - initialises the various blockheaps
77 channel_heap
= rb_bh_create(sizeof(struct Channel
), CHANNEL_HEAP_SIZE
, "channel_heap");
78 ban_heap
= rb_bh_create(sizeof(struct Ban
), BAN_HEAP_SIZE
, "ban_heap");
79 topic_heap
= rb_bh_create(TOPICLEN
+ 1 + USERHOST_REPLYLEN
, TOPIC_HEAP_SIZE
, "topic_heap");
80 member_heap
= rb_bh_create(sizeof(struct membership
), MEMBER_HEAP_SIZE
, "member_heap");
82 h_can_join
= register_hook("can_join");
83 h_can_send
= register_hook("can_send");
84 h_get_channel_access
= register_hook("get_channel_access");
88 * allocate_channel - Allocates a channel
91 allocate_channel(const char *chname
)
93 struct Channel
*chptr
;
94 chptr
= rb_bh_alloc(channel_heap
);
95 chptr
->chname
= rb_strdup(chname
);
100 free_channel(struct Channel
*chptr
)
102 rb_free(chptr
->chname
);
103 rb_free(chptr
->mode_lock
);
104 rb_bh_free(channel_heap
, chptr
);
108 allocate_ban(const char *banstr
, const char *who
, const char *forward
)
111 bptr
= rb_bh_alloc(ban_heap
);
112 bptr
->banstr
= rb_strdup(banstr
);
113 bptr
->who
= rb_strdup(who
);
114 bptr
->forward
= forward
? rb_strdup(forward
) : NULL
;
120 free_ban(struct Ban
*bptr
)
122 rb_free(bptr
->banstr
);
124 rb_free(bptr
->forward
);
125 rb_bh_free(ban_heap
, bptr
);
129 * send_channel_join()
131 * input - channel to join, client joining.
133 * side effects - none
136 send_channel_join(struct Channel
*chptr
, struct Client
*client_p
)
138 if (!IsClient(client_p
))
141 sendto_channel_local_with_capability(ALL_MEMBERS
, NOCAPS
, CLICAP_EXTENDED_JOIN
, chptr
, ":%s!%s@%s JOIN %s",
142 client_p
->name
, client_p
->username
, client_p
->host
, chptr
->chname
);
144 sendto_channel_local_with_capability(ALL_MEMBERS
, CLICAP_EXTENDED_JOIN
, NOCAPS
, chptr
, ":%s!%s@%s JOIN %s %s :%s",
145 client_p
->name
, client_p
->username
, client_p
->host
, chptr
->chname
,
146 EmptyString(client_p
->user
->suser
) ? "*" : client_p
->user
->suser
,
150 /* find_channel_membership()
152 * input - channel to find them in, client to find
153 * output - membership of client in channel, else NULL
157 find_channel_membership(struct Channel
*chptr
, struct Client
*client_p
)
159 struct membership
*msptr
;
162 if(!IsClient(client_p
))
165 /* Pick the most efficient list to use to be nice to things like
166 * CHANSERV which could be in a large number of channels
168 if(rb_dlink_list_length(&chptr
->members
) < rb_dlink_list_length(&client_p
->user
->channel
))
170 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
174 if(msptr
->client_p
== client_p
)
180 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
184 if(msptr
->chptr
== chptr
)
192 /* find_channel_status()
194 * input - membership to get status for, whether we can combine flags
195 * output - flags of user on channel
199 find_channel_status(struct membership
*msptr
, int combine
)
201 static char buffer
[3];
220 /* add_user_to_channel()
222 * input - channel to add client to, client to add, channel flags
224 * side effects - user is added to channel
227 add_user_to_channel(struct Channel
*chptr
, struct Client
*client_p
, int flags
)
229 struct membership
*msptr
;
231 s_assert(client_p
->user
!= NULL
);
232 if(client_p
->user
== NULL
)
235 msptr
= rb_bh_alloc(member_heap
);
237 msptr
->chptr
= chptr
;
238 msptr
->client_p
= client_p
;
239 msptr
->flags
= flags
;
241 rb_dlinkAdd(msptr
, &msptr
->usernode
, &client_p
->user
->channel
);
242 rb_dlinkAdd(msptr
, &msptr
->channode
, &chptr
->members
);
244 if(MyClient(client_p
))
245 rb_dlinkAdd(msptr
, &msptr
->locchannode
, &chptr
->locmembers
);
248 /* remove_user_from_channel()
250 * input - membership pointer to remove from channel
252 * side effects - membership (thus user) is removed from channel
255 remove_user_from_channel(struct membership
*msptr
)
257 struct Client
*client_p
;
258 struct Channel
*chptr
;
259 s_assert(msptr
!= NULL
);
263 client_p
= msptr
->client_p
;
264 chptr
= msptr
->chptr
;
266 rb_dlinkDelete(&msptr
->usernode
, &client_p
->user
->channel
);
267 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
269 if(client_p
->servptr
== &me
)
270 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
272 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
273 destroy_channel(chptr
);
275 rb_bh_free(member_heap
, msptr
);
280 /* remove_user_from_channels()
282 * input - user to remove from all channels
284 * side effects - user is removed from all channels
287 remove_user_from_channels(struct Client
*client_p
)
289 struct Channel
*chptr
;
290 struct membership
*msptr
;
292 rb_dlink_node
*next_ptr
;
297 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->user
->channel
.head
)
300 chptr
= msptr
->chptr
;
302 rb_dlinkDelete(&msptr
->channode
, &chptr
->members
);
304 if(client_p
->servptr
== &me
)
305 rb_dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
307 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && rb_dlink_list_length(&chptr
->members
) <= 0)
308 destroy_channel(chptr
);
310 rb_bh_free(member_heap
, msptr
);
313 client_p
->user
->channel
.head
= client_p
->user
->channel
.tail
= NULL
;
314 client_p
->user
->channel
.length
= 0;
317 /* invalidate_bancache_user()
319 * input - user to invalidate ban cache for
321 * side effects - ban cache is invalidated for all memberships of that user
322 * to be used after a nick change
325 invalidate_bancache_user(struct Client
*client_p
)
327 struct membership
*msptr
;
333 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
337 msptr
->flags
&= ~CHFL_BANNED
;
341 /* check_channel_name()
343 * input - channel name
344 * output - 1 if valid channel name, else 0
348 check_channel_name(const char *name
)
350 s_assert(name
!= NULL
);
354 for (; *name
; ++name
)
356 if(!IsChanChar(*name
))
363 /* free_channel_list()
365 * input - rb_dlink list to free
367 * side effects - list of b/e/I modes is cleared
370 free_channel_list(rb_dlink_list
* list
)
373 rb_dlink_node
*next_ptr
;
374 struct Ban
*actualBan
;
376 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
378 actualBan
= ptr
->data
;
382 list
->head
= list
->tail
= NULL
;
388 * input - channel to destroy
390 * side effects - channel is obliterated
393 destroy_channel(struct Channel
*chptr
)
395 rb_dlink_node
*ptr
, *next_ptr
;
397 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
399 del_invite(chptr
, ptr
->data
);
402 /* free all bans/exceptions/denies */
403 free_channel_list(&chptr
->banlist
);
404 free_channel_list(&chptr
->exceptlist
);
405 free_channel_list(&chptr
->invexlist
);
406 free_channel_list(&chptr
->quietlist
);
411 rb_dlinkDelete(&chptr
->node
, &global_channel_list
);
412 del_from_channel_hash(chptr
->chname
, chptr
);
416 /* channel_pub_or_secret()
419 * output - "=" if public, "@" if secret, else "*"
423 channel_pub_or_secret(struct Channel
*chptr
)
425 if(PubChannel(chptr
))
427 else if(SecretChannel(chptr
))
432 /* channel_member_names()
434 * input - channel to list, client to list to, show endofnames
436 * side effects - client is given list of users on channel
439 channel_member_names(struct Channel
*chptr
, struct Client
*client_p
, int show_eon
)
441 struct membership
*msptr
;
442 struct Client
*target_p
;
450 int stack
= IsCapable(client_p
, CLICAP_MULTI_PREFIX
);
452 if(ShowChannel(client_p
, chptr
))
454 is_member
= IsMember(client_p
, chptr
);
456 cur_len
= mlen
= rb_sprintf(lbuf
, form_str(RPL_NAMREPLY
),
457 me
.name
, client_p
->name
,
458 channel_pub_or_secret(chptr
), chptr
->chname
);
462 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
465 target_p
= msptr
->client_p
;
467 if(IsInvisible(target_p
) && !is_member
)
470 /* space, possible "@+" prefix */
471 if(cur_len
+ strlen(target_p
->name
) + 3 >= BUFSIZE
- 3)
474 sendto_one(client_p
, "%s", lbuf
);
479 tlen
= rb_sprintf(t
, "%s%s ", find_channel_status(msptr
, stack
),
486 /* The old behaviour here was to always output our buffer,
487 * even if there are no clients we can show. This happens
488 * when a client does "NAMES" with no parameters, and all
489 * the clients on a -sp channel are +i. I dont see a good
490 * reason for keeping that behaviour, as it just wastes
496 sendto_one(client_p
, "%s", lbuf
);
501 sendto_one(client_p
, form_str(RPL_ENDOFNAMES
),
502 me
.name
, client_p
->name
, chptr
->chname
);
507 * input - channel to remove invite from, client to remove
509 * side effects - user is removed from invite list, if exists
512 del_invite(struct Channel
*chptr
, struct Client
*who
)
514 rb_dlinkFindDestroy(who
, &chptr
->invites
);
515 rb_dlinkFindDestroy(chptr
, &who
->user
->invited
);
520 * input - channel to check bans for, ban list (banlist or quietlist),
521 * user to check bans against, optional prebuilt buffers,
522 * optional forward channel pointer
523 * output - 1 if banned, else 0
527 is_banned_list(struct Channel
*chptr
, rb_dlink_list
*list
,
528 struct Client
*who
, struct membership
*msptr
,
529 const char *s
, const char *s2
, const char **forward
)
531 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
532 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
533 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
536 struct Ban
*actualBan
= NULL
;
537 struct Ban
*actualExcept
= NULL
;
542 /* if the buffers havent been built, do it here */
545 rb_sprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
546 rb_sprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
551 if(who
->localClient
->mangledhost
!= NULL
)
553 /* if host mangling mode enabled, also check their real host */
554 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
556 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
559 /* if host mangling mode not enabled and no other spoof,
560 * also check the mangled form of their host */
561 else if (!IsDynSpoof(who
))
563 rb_sprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
568 RB_DLINK_FOREACH(ptr
, list
->head
)
570 actualBan
= ptr
->data
;
571 if(match(actualBan
->banstr
, s
) ||
572 match(actualBan
->banstr
, s2
) ||
573 match_cidr(actualBan
->banstr
, s2
) ||
574 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_BAN
) ||
575 (s3
!= NULL
&& match(actualBan
->banstr
, s3
)))
581 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
583 RB_DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
585 actualExcept
= ptr
->data
;
587 /* theyre exempted.. */
588 if(match(actualExcept
->banstr
, s
) ||
589 match(actualExcept
->banstr
, s2
) ||
590 match_cidr(actualExcept
->banstr
, s2
) ||
591 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
592 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
594 /* cache the fact theyre not banned */
597 msptr
->bants
= chptr
->bants
;
598 msptr
->flags
&= ~CHFL_BANNED
;
601 return CHFL_EXCEPTION
;
606 /* cache the banned/not banned status */
609 msptr
->bants
= chptr
->bants
;
611 if(actualBan
!= NULL
)
613 msptr
->flags
|= CHFL_BANNED
;
618 msptr
->flags
&= ~CHFL_BANNED
;
623 if (actualBan
&& actualBan
->forward
&& forward
)
624 *forward
= actualBan
->forward
;
626 return ((actualBan
? CHFL_BAN
: 0));
631 * input - channel to check bans for, user to check bans against
632 * optional prebuilt buffers, optional forward channel pointer
633 * output - 1 if banned, else 0
637 is_banned(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
638 const char *s
, const char *s2
, const char **forward
)
640 return is_banned_list(chptr
, &chptr
->banlist
, who
, msptr
, s
, s2
,
646 * input - channel to check bans for, user to check bans against
647 * optional prebuilt buffers
648 * output - 1 if banned, else 0
652 is_quieted(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
653 const char *s
, const char *s2
)
655 return is_banned_list(chptr
, &chptr
->quietlist
, who
, msptr
, s
, s2
,
661 * input - client to check, channel to check for, key
662 * output - reason for not being able to join, else 0, channel name to forward to
664 * caveats - this function should only be called on a local user.
667 can_join(struct Client
*source_p
, struct Channel
*chptr
, const char *key
, const char **forward
)
669 rb_dlink_node
*invite
= NULL
;
671 struct Ban
*invex
= NULL
;
672 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
673 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
674 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
677 hook_data_channel moduledata
;
679 s_assert(source_p
->localClient
!= NULL
);
681 moduledata
.client
= source_p
;
682 moduledata
.chptr
= chptr
;
683 moduledata
.approved
= 0;
685 rb_sprintf(src_host
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
686 rb_sprintf(src_iphost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->sockhost
);
687 if(source_p
->localClient
->mangledhost
!= NULL
)
689 /* if host mangling mode enabled, also check their real host */
690 if(!strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
692 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->orighost
);
695 /* if host mangling mode not enabled and no other spoof,
696 * also check the mangled form of their host */
697 else if (!IsDynSpoof(source_p
))
699 rb_sprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->localClient
->mangledhost
);
704 if((is_banned(chptr
, source_p
, NULL
, src_host
, src_iphost
, forward
)) == CHFL_BAN
)
706 moduledata
.approved
= ERR_BANNEDFROMCHAN
;
707 goto finish_join_check
;
710 if(*chptr
->mode
.key
&& (EmptyString(key
) || irccmp(chptr
->mode
.key
, key
)))
712 moduledata
.approved
= ERR_BADCHANNELKEY
;
713 goto finish_join_check
;
716 /* All checks from this point on will forward... */
718 *forward
= chptr
->mode
.forward
;
720 if(chptr
->mode
.mode
& MODE_INVITEONLY
)
722 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
724 if(invite
->data
== chptr
)
729 if(!ConfigChannel
.use_invex
)
730 moduledata
.approved
= ERR_INVITEONLYCHAN
;
731 RB_DLINK_FOREACH(ptr
, chptr
->invexlist
.head
)
734 if(match(invex
->banstr
, src_host
)
735 || match(invex
->banstr
, src_iphost
)
736 || match_cidr(invex
->banstr
, src_iphost
)
737 || match_extban(invex
->banstr
, source_p
, chptr
, CHFL_INVEX
)
738 || (use_althost
&& match(invex
->banstr
, src_althost
)))
742 moduledata
.approved
= ERR_INVITEONLYCHAN
;
746 if(chptr
->mode
.limit
&&
747 rb_dlink_list_length(&chptr
->members
) >= (unsigned long) chptr
->mode
.limit
)
748 i
= ERR_CHANNELISFULL
;
749 if(chptr
->mode
.mode
& MODE_REGONLY
&& EmptyString(source_p
->user
->suser
))
750 i
= ERR_NEEDREGGEDNICK
;
751 /* join throttling stuff --nenolod */
752 else if(chptr
->mode
.join_num
> 0 && chptr
->mode
.join_time
> 0)
754 if ((rb_current_time() - chptr
->join_delta
<=
755 chptr
->mode
.join_time
) && (chptr
->join_count
>=
756 chptr
->mode
.join_num
))
760 /* allow /invite to override +l/+r/+j also -- jilles */
761 if (i
!= 0 && invite
== NULL
)
763 RB_DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
765 if(invite
->data
== chptr
)
769 moduledata
.approved
= i
;
773 call_hook(h_can_join
, &moduledata
);
775 return moduledata
.approved
;
780 * input - user to check in channel, membership pointer
781 * output - whether can explicitly send or not, else CAN_SEND_NONOP
785 can_send(struct Channel
*chptr
, struct Client
*source_p
, struct membership
*msptr
)
787 hook_data_channel_approval moduledata
;
789 moduledata
.approved
= CAN_SEND_NONOP
;
791 if(IsServer(source_p
) || IsService(source_p
))
794 if(MyClient(source_p
) && hash_find_resv(chptr
->chname
) &&
795 !IsOper(source_p
) && !IsExemptResv(source_p
))
796 moduledata
.approved
= CAN_SEND_NO
;
800 msptr
= find_channel_membership(chptr
, source_p
);
804 /* if its +m or +n and theyre not in the channel,
805 * they cant send. we dont check bans here because
806 * theres no possibility of caching them --fl
808 if(chptr
->mode
.mode
& MODE_NOPRIVMSGS
|| chptr
->mode
.mode
& MODE_MODERATED
)
809 moduledata
.approved
= CAN_SEND_NO
;
811 moduledata
.approved
= CAN_SEND_NONOP
;
813 return moduledata
.approved
;
817 if(chptr
->mode
.mode
& MODE_MODERATED
)
818 moduledata
.approved
= CAN_SEND_NO
;
820 if(MyClient(source_p
))
822 /* cached can_send */
823 if(msptr
->bants
== chptr
->bants
)
825 if(can_send_banned(msptr
))
826 moduledata
.approved
= CAN_SEND_NO
;
828 else if(is_banned(chptr
, source_p
, msptr
, NULL
, NULL
, NULL
) == CHFL_BAN
829 || is_quieted(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
)
830 moduledata
.approved
= CAN_SEND_NO
;
833 if(is_chanop_voiced(msptr
))
834 moduledata
.approved
= CAN_SEND_OPV
;
836 moduledata
.client
= source_p
;
837 moduledata
.chptr
= msptr
->chptr
;
838 moduledata
.msptr
= msptr
;
839 moduledata
.target
= NULL
;
841 call_hook(h_can_send
, &moduledata
);
843 return moduledata
.approved
;
847 * flood_attack_channel
848 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
849 * says NOTICE must not auto reply
850 * - pointer to source Client
851 * - pointer to target channel
852 * output - 1 if target is under flood attack
853 * side effects - check for flood attack on target chptr
856 flood_attack_channel(int p_or_n
, struct Client
*source_p
, struct Channel
*chptr
, char *chname
)
860 if(GlobalSetOptions
.floodcount
&& MyClient(source_p
))
862 if((chptr
->first_received_message_time
+ 1) < rb_current_time())
864 delta
= rb_current_time() - chptr
->first_received_message_time
;
865 chptr
->received_number_of_privmsgs
-= delta
;
866 chptr
->first_received_message_time
= rb_current_time();
867 if(chptr
->received_number_of_privmsgs
<= 0)
869 chptr
->received_number_of_privmsgs
= 0;
870 chptr
->flood_noticed
= 0;
874 if((chptr
->received_number_of_privmsgs
>= GlobalSetOptions
.floodcount
)
875 || chptr
->flood_noticed
)
877 if(chptr
->flood_noticed
== 0)
879 sendto_realops_snomask(SNO_BOTS
, *chptr
->chname
== '&' ? L_ALL
: L_NETWIDE
,
880 "Possible Flooder %s[%s@%s] on %s target: %s",
881 source_p
->name
, source_p
->username
,
883 source_p
->servptr
->name
, chptr
->chname
);
884 chptr
->flood_noticed
= 1;
886 /* Add a bit of penalty */
887 chptr
->received_number_of_privmsgs
+= 2;
889 if(MyClient(source_p
) && (p_or_n
!= 1))
891 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
892 me
.name
, source_p
->name
, chptr
->chname
);
896 chptr
->received_number_of_privmsgs
++;
902 /* find_bannickchange_channel()
903 * Input: client to check
904 * Output: channel preventing nick change
907 find_bannickchange_channel(struct Client
*client_p
)
909 struct Channel
*chptr
;
910 struct membership
*msptr
;
912 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
913 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
915 if (!MyClient(client_p
))
918 rb_sprintf(src_host
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->host
);
919 rb_sprintf(src_iphost
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->sockhost
);
921 RB_DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
924 chptr
= msptr
->chptr
;
925 if (is_chanop_voiced(msptr
))
927 /* cached can_send */
928 if (msptr
->bants
== chptr
->bants
)
930 if (can_send_banned(msptr
))
933 else if (is_banned(chptr
, client_p
, msptr
, src_host
, src_iphost
, NULL
) == CHFL_BAN
934 || is_quieted(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
)
940 /* void check_spambot_warning(struct Client *source_p)
941 * Input: Client to check, channel name or NULL if this is a part.
943 * Side-effects: Updates the client's oper_warn_count_down, warns the
944 * IRC operators if necessary, and updates join_leave_countdown as
948 check_spambot_warning(struct Client
*source_p
, const char *name
)
952 if((GlobalSetOptions
.spam_num
&&
953 (source_p
->localClient
->join_leave_count
>= GlobalSetOptions
.spam_num
)))
955 if(source_p
->localClient
->oper_warn_count_down
> 0)
956 source_p
->localClient
->oper_warn_count_down
--;
958 source_p
->localClient
->oper_warn_count_down
= 0;
959 if(source_p
->localClient
->oper_warn_count_down
== 0 &&
962 /* Its already known as a possible spambot */
963 sendto_realops_snomask(SNO_BOTS
, L_NETWIDE
,
964 "User %s (%s@%s) trying to join %s is a possible spambot",
966 source_p
->username
, source_p
->orighost
, name
);
967 source_p
->localClient
->oper_warn_count_down
= OPER_SPAM_COUNTDOWN
;
973 (rb_current_time() - source_p
->localClient
->last_leave_time
)) >
974 JOIN_LEAVE_COUNT_EXPIRE_TIME
)
976 decrement_count
= (t_delta
/ JOIN_LEAVE_COUNT_EXPIRE_TIME
);
979 else if(decrement_count
> source_p
->localClient
->join_leave_count
)
980 source_p
->localClient
->join_leave_count
= 0;
982 source_p
->localClient
->join_leave_count
-= decrement_count
;
986 if((rb_current_time() -
987 (source_p
->localClient
->last_join_time
)) < GlobalSetOptions
.spam_time
)
989 /* oh, its a possible spambot */
990 source_p
->localClient
->join_leave_count
++;
994 source_p
->localClient
->last_join_time
= rb_current_time();
996 source_p
->localClient
->last_leave_time
= rb_current_time();
1000 /* check_splitmode()
1004 * side effects - compares usercount and servercount against their split
1005 * values and adjusts splitmode accordingly
1008 check_splitmode(void *unused
)
1010 if(splitchecking
&& (ConfigChannel
.no_join_on_split
|| ConfigChannel
.no_create_on_split
))
1012 /* not split, we're being asked to check now because someone
1017 if(eob_count
< split_servers
|| Count
.total
< split_users
)
1020 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1021 "Network split, activating splitmode");
1022 check_splitmode_ev
= rb_event_addish("check_splitmode", check_splitmode
, NULL
, 2);
1025 /* in splitmode, check whether its finished */
1026 else if(eob_count
>= split_servers
&& Count
.total
>= split_users
)
1030 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1031 "Network rejoined, deactivating splitmode");
1033 rb_event_delete(check_splitmode_ev
);
1034 check_splitmode_ev
= NULL
;
1042 * input - channel to allocate topic for
1043 * output - 1 on success, else 0
1044 * side effects - channel gets a topic allocated
1047 allocate_topic(struct Channel
*chptr
)
1054 ptr
= rb_bh_alloc(topic_heap
);
1056 /* Basically we allocate one large block for the topic and
1057 * the topic info. We then split it up into two and shove it
1061 chptr
->topic_info
= (char *) ptr
+ TOPICLEN
+ 1;
1062 *chptr
->topic
= '\0';
1063 *chptr
->topic_info
= '\0';
1068 * input - channel which has topic to free
1070 * side effects - channels topic is free'd
1073 free_topic(struct Channel
*chptr
)
1077 if(chptr
== NULL
|| chptr
->topic
== NULL
)
1080 /* This is safe for now - If you change allocate_topic you
1081 * MUST change this as well
1084 rb_bh_free(topic_heap
, ptr
);
1085 chptr
->topic
= NULL
;
1086 chptr
->topic_info
= NULL
;
1089 /* set_channel_topic()
1091 * input - channel, topic to set, topic info and topic ts
1093 * side effects - channels topic, topic info and TS are set.
1096 set_channel_topic(struct Channel
*chptr
, const char *topic
, const char *topic_info
, time_t topicts
)
1098 if(strlen(topic
) > 0)
1100 if(chptr
->topic
== NULL
)
1101 allocate_topic(chptr
);
1102 rb_strlcpy(chptr
->topic
, topic
, TOPICLEN
+ 1);
1103 rb_strlcpy(chptr
->topic_info
, topic_info
, USERHOST_REPLYLEN
);
1104 chptr
->topic_time
= topicts
;
1108 if(chptr
->topic
!= NULL
)
1110 chptr
->topic_time
= 0;
1116 * inputs - pointer to channel
1117 * - pointer to client
1118 * output - string with simple modes
1119 * side effects - result from previous calls overwritten
1121 * Stolen from ShadowIRCd 4 --nenolod
1124 channel_modes(struct Channel
*chptr
, struct Client
*client_p
)
1129 static char final
[BUFSIZE
];
1136 for (i
= 0; i
< 256; i
++)
1137 if(chptr
->mode
.mode
& chmode_flags
[i
])
1140 if(chptr
->mode
.limit
)
1144 if(!IsClient(client_p
) || IsMember(client_p
, chptr
))
1145 pbuf
+= rb_sprintf(pbuf
, " %d", chptr
->mode
.limit
);
1148 if(*chptr
->mode
.key
)
1152 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1153 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.key
);
1156 if(chptr
->mode
.join_num
)
1160 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1161 pbuf
+= rb_sprintf(pbuf
, " %d:%d", chptr
->mode
.join_num
,
1162 chptr
->mode
.join_time
);
1165 if(*chptr
->mode
.forward
&&
1166 (ConfigChannel
.use_forward
|| !IsClient(client_p
)))
1170 if(pbuf
> buf2
|| !IsClient(client_p
) || IsMember(client_p
, chptr
))
1171 pbuf
+= rb_sprintf(pbuf
, " %s", chptr
->mode
.forward
);
1176 rb_strlcpy(final
, buf1
, sizeof final
);
1177 rb_strlcat(final
, buf2
, sizeof final
);
1181 /* Now lets do some stuff to keep track of what combinations of
1183 * Note that the number of combinations doubles each time you add
1184 * something to this list. Each one is only quick if no servers use that
1185 * combination, but if the numbers get too high here MODE will get too
1186 * slow. I suggest if you get more than 7 here, you consider getting rid
1187 * of some and merging or something. If it wasn't for irc+cs we would
1188 * probably not even need to bother about most of these, but unfortunately
1192 /* void init_chcap_usage_counts(void)
1196 * Side-effects - Initialises the usage counts to zero. Fills in the
1197 * chcap_yes and chcap_no combination tables.
1200 init_chcap_usage_counts(void)
1202 unsigned long m
, c
, y
, n
;
1204 memset(chcap_combos
, 0, sizeof(chcap_combos
));
1206 /* For every possible combination */
1207 for (m
= 0; m
< NCHCAP_COMBOS
; m
++)
1209 /* Check each capab */
1210 for (c
= y
= n
= 0; c
< NCHCAPS
; c
++)
1212 if((m
& (1 << c
)) == 0)
1213 n
|= channel_capabs
[c
];
1215 y
|= channel_capabs
[c
];
1217 chcap_combos
[m
].cap_yes
= y
;
1218 chcap_combos
[m
].cap_no
= n
;
1222 /* void set_chcap_usage_counts(struct Client *serv_p)
1223 * Input: serv_p; The client whose capabs to register.
1225 * Side-effects: Increments the usage counts for the correct capab
1229 set_chcap_usage_counts(struct Client
*serv_p
)
1233 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1235 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1236 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1238 chcap_combos
[n
].count
++;
1243 /* This should be impossible -A1kmm. */
1247 /* void set_chcap_usage_counts(struct Client *serv_p)
1249 * Inputs - serv_p; The client whose capabs to register.
1251 * Side-effects - Decrements the usage counts for the correct capab
1255 unset_chcap_usage_counts(struct Client
*serv_p
)
1259 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1261 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1262 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1264 /* Hopefully capabs can't change dynamically or anything... */
1265 s_assert(chcap_combos
[n
].count
> 0);
1267 if(chcap_combos
[n
].count
> 0)
1268 chcap_combos
[n
].count
--;
1273 /* This should be impossible -A1kmm. */
1277 /* void send_cap_mode_changes(struct Client *client_p,
1278 * struct Client *source_p,
1279 * struct Channel *chptr, int cap, int nocap)
1280 * Input: The client sending(client_p), the source client(source_p),
1281 * the channel to send mode changes for(chptr)
1283 * Side-effects: Sends the appropriate mode changes to capable servers.
1285 * Reverted back to my original design, except that we now keep a count
1286 * of the number of servers which each combination as an optimisation, so
1287 * the capabs combinations which are not needed are not worked out. -A1kmm
1290 send_cap_mode_changes(struct Client
*client_p
, struct Client
*source_p
,
1291 struct Channel
*chptr
, struct ChModeChange mode_changes
[], int mode_count
)
1293 static char modebuf
[BUFSIZE
];
1294 static char parabuf
[BUFSIZE
];
1295 int i
, mbl
, pbl
, nc
, mc
, preflen
, len
;
1304 /* Now send to servers... */
1305 for (j
= 0; j
< NCHCAP_COMBOS
; j
++)
1307 if(chcap_combos
[j
].count
== 0)
1317 cap
= chcap_combos
[j
].cap_yes
;
1318 nocap
= chcap_combos
[j
].cap_no
;
1320 mbl
= preflen
= rb_sprintf(modebuf
, ":%s TMODE %ld %s ",
1321 use_id(source_p
), (long) chptr
->channelts
,
1324 /* loop the list of - modes we have */
1325 for (i
= 0; i
< mode_count
; i
++)
1327 /* if they dont support the cap we need, or they do support a cap they
1328 * cant have, then dont add it to the modebuf.. that way they wont see
1331 if((mode_changes
[i
].letter
== 0) ||
1332 ((cap
& mode_changes
[i
].caps
) != mode_changes
[i
].caps
)
1333 || ((nocap
& mode_changes
[i
].nocaps
) != mode_changes
[i
].nocaps
))
1336 if(!EmptyString(mode_changes
[i
].id
))
1337 arg
= mode_changes
[i
].id
;
1339 arg
= mode_changes
[i
].arg
;
1343 arglen
= strlen(arg
);
1345 /* dont even think about it! --fl */
1346 if(arglen
> MODEBUFLEN
- 5)
1350 /* if we're creeping past the buf size, we need to send it and make
1351 * another line for the other modes
1352 * XXX - this could give away server topology with uids being
1353 * different lengths, but not much we can do, except possibly break
1354 * them as if they were the longest of the nick or uid at all times,
1355 * which even then won't work as we don't always know the uid -A1kmm.
1357 if(arg
&& ((mc
== MAXMODEPARAMSSERV
) ||
1358 ((mbl
+ pbl
+ arglen
+ 4) > (BUFSIZE
- 3))))
1361 sendto_server(client_p
, chptr
, cap
, nocap
,
1362 "%s %s", modebuf
, parabuf
);
1373 if(dir
!= mode_changes
[i
].dir
)
1375 modebuf
[mbl
++] = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1376 dir
= mode_changes
[i
].dir
;
1379 modebuf
[mbl
++] = mode_changes
[i
].letter
;
1385 len
= rb_sprintf(pbuf
, "%s ", arg
);
1392 if(pbl
&& parabuf
[pbl
- 1] == ' ')
1393 parabuf
[pbl
- 1] = 0;
1396 sendto_server(client_p
, chptr
, cap
, nocap
, "%s %s", modebuf
, parabuf
);
1401 resv_chan_forcepart(const char *name
, const char *reason
, int temp_time
)
1404 rb_dlink_node
*next_ptr
;
1405 struct Channel
*chptr
;
1406 struct membership
*msptr
;
1407 struct Client
*target_p
;
1409 if(!ConfigChannel
.resv_forcepart
)
1412 /* for each user on our server in the channel list
1413 * send them a PART, and notify opers.
1415 chptr
= find_channel(name
);
1418 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->locmembers
.head
)
1421 target_p
= msptr
->client_p
;
1423 if(IsExemptResv(target_p
))
1426 sendto_server(target_p
, chptr
, CAP_TS6
, NOCAPS
,
1427 ":%s PART %s", target_p
->id
, chptr
->chname
);
1429 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s PART %s :%s",
1430 target_p
->name
, target_p
->username
,
1431 target_p
->host
, chptr
->chname
, target_p
->name
);
1433 remove_user_from_channel(msptr
);
1435 /* notify opers & user they were removed from the channel */
1436 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1437 "Forced PART for %s!%s@%s from %s (%s)",
1438 target_p
->name
, target_p
->username
,
1439 target_p
->host
, name
, reason
);
1442 sendto_one_notice(target_p
, ":*** Channel %s is temporarily unavailable on this server.",
1445 sendto_one_notice(target_p
, ":*** Channel %s is no longer available on this server.",