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 3259 2007-03-15 18:09:08Z jilles $
34 #include "irc_string.h"
35 #include "sprintf_irc.h"
38 #include "s_serv.h" /* captab */
42 #include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
43 #include "s_newconf.h"
49 extern dlink_list global_channel_list
;
51 extern struct config_channel_entry ConfigChannel
;
52 extern BlockHeap
*channel_heap
;
53 extern BlockHeap
*ban_heap
;
54 extern BlockHeap
*topic_heap
;
55 extern BlockHeap
*member_heap
;
57 static int channel_capabs
[] = { CAP_EX
, CAP_IE
,
62 #define NCHCAPS (sizeof(channel_capabs)/sizeof(int))
63 #define NCHCAP_COMBOS (1 << NCHCAPS)
65 static struct ChCapCombo chcap_combos
[NCHCAP_COMBOS
];
67 static void free_topic(struct Channel
*chptr
);
69 static int h_can_join
;
75 * side effects - initialises the various blockheaps
80 channel_heap
= BlockHeapCreate(sizeof(struct Channel
), CHANNEL_HEAP_SIZE
);
81 ban_heap
= BlockHeapCreate(sizeof(struct Ban
), BAN_HEAP_SIZE
);
82 topic_heap
= BlockHeapCreate(TOPICLEN
+ 1 + USERHOST_REPLYLEN
, TOPIC_HEAP_SIZE
);
83 member_heap
= BlockHeapCreate(sizeof(struct membership
), MEMBER_HEAP_SIZE
);
85 h_can_join
= register_hook("can_join");
89 * allocate_channel - Allocates a channel
92 allocate_channel(const char *chname
)
94 struct Channel
*chptr
;
95 chptr
= BlockHeapAlloc(channel_heap
);
96 DupNString(chptr
->chname
, chname
, CHANNELLEN
);
101 free_channel(struct Channel
*chptr
)
103 MyFree(chptr
->chname
);
104 BlockHeapFree(channel_heap
, chptr
);
108 allocate_ban(const char *banstr
, const char *who
)
111 bptr
= BlockHeapAlloc(ban_heap
);
112 DupNString(bptr
->banstr
, banstr
, BANLEN
);
113 DupNString(bptr
->who
, who
, BANLEN
);
119 free_ban(struct Ban
*bptr
)
121 MyFree(bptr
->banstr
);
123 BlockHeapFree(ban_heap
, bptr
);
127 /* find_channel_membership()
129 * input - channel to find them in, client to find
130 * output - membership of client in channel, else NULL
134 find_channel_membership(struct Channel
*chptr
, struct Client
*client_p
)
136 struct membership
*msptr
;
139 if(!IsClient(client_p
))
142 /* Pick the most efficient list to use to be nice to things like
143 * CHANSERV which could be in a large number of channels
145 if(dlink_list_length(&chptr
->members
) < dlink_list_length(&client_p
->user
->channel
))
147 DLINK_FOREACH(ptr
, chptr
->members
.head
)
151 if(msptr
->client_p
== client_p
)
157 DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
161 if(msptr
->chptr
== chptr
)
169 /* find_channel_status()
171 * input - membership to get status for, whether we can combine flags
172 * output - flags of user on channel
176 find_channel_status(struct membership
*msptr
, int combine
)
178 static char buffer
[3];
197 /* add_user_to_channel()
199 * input - channel to add client to, client to add, channel flags
201 * side effects - user is added to channel
204 add_user_to_channel(struct Channel
*chptr
, struct Client
*client_p
, int flags
)
206 struct membership
*msptr
;
208 s_assert(client_p
->user
!= NULL
);
209 if(client_p
->user
== NULL
)
212 msptr
= BlockHeapAlloc(member_heap
);
214 msptr
->chptr
= chptr
;
215 msptr
->client_p
= client_p
;
216 msptr
->flags
= flags
;
218 dlinkAdd(msptr
, &msptr
->usernode
, &client_p
->user
->channel
);
219 dlinkAdd(msptr
, &msptr
->channode
, &chptr
->members
);
221 if(MyClient(client_p
))
222 dlinkAdd(msptr
, &msptr
->locchannode
, &chptr
->locmembers
);
225 /* remove_user_from_channel()
227 * input - membership pointer to remove from channel
229 * side effects - membership (thus user) is removed from channel
232 remove_user_from_channel(struct membership
*msptr
)
234 struct Client
*client_p
;
235 struct Channel
*chptr
;
236 s_assert(msptr
!= NULL
);
240 client_p
= msptr
->client_p
;
241 chptr
= msptr
->chptr
;
243 dlinkDelete(&msptr
->usernode
, &client_p
->user
->channel
);
244 dlinkDelete(&msptr
->channode
, &chptr
->members
);
246 if(client_p
->servptr
== &me
)
247 dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
249 chptr
->users_last
= CurrentTime
;
251 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && dlink_list_length(&chptr
->members
) <= 0)
252 destroy_channel(chptr
);
254 BlockHeapFree(member_heap
, msptr
);
259 /* remove_user_from_channels()
261 * input - user to remove from all channels
263 * side effects - user is removed from all channels
266 remove_user_from_channels(struct Client
*client_p
)
268 struct Channel
*chptr
;
269 struct membership
*msptr
;
271 dlink_node
*next_ptr
;
276 DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->user
->channel
.head
)
279 chptr
= msptr
->chptr
;
281 dlinkDelete(&msptr
->channode
, &chptr
->members
);
283 if(client_p
->servptr
== &me
)
284 dlinkDelete(&msptr
->locchannode
, &chptr
->locmembers
);
286 chptr
->users_last
= CurrentTime
;
288 if(!(chptr
->mode
.mode
& MODE_PERMANENT
) && dlink_list_length(&chptr
->members
) <= 0)
289 destroy_channel(chptr
);
291 BlockHeapFree(member_heap
, msptr
);
294 client_p
->user
->channel
.head
= client_p
->user
->channel
.tail
= NULL
;
295 client_p
->user
->channel
.length
= 0;
298 /* invalidate_bancache_user()
300 * input - user to invalidate ban cache for
302 * side effects - ban cache is invalidated for all memberships of that user
303 * to be used after a nick change
306 invalidate_bancache_user(struct Client
*client_p
)
308 struct membership
*msptr
;
314 DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
318 msptr
->flags
&= ~CHFL_BANNED
;
322 /* check_channel_name()
324 * input - channel name
325 * output - 1 if valid channel name, else 0
329 check_channel_name(const char *name
)
331 s_assert(name
!= NULL
);
335 for (; *name
; ++name
)
337 if(!IsChanChar(*name
))
344 /* free_channel_list()
346 * input - dlink list to free
348 * side effects - list of b/e/I modes is cleared
351 free_channel_list(dlink_list
* list
)
354 dlink_node
*next_ptr
;
355 struct Ban
*actualBan
;
357 DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
359 actualBan
= ptr
->data
;
363 list
->head
= list
->tail
= NULL
;
369 * input - channel to destroy
371 * side effects - channel is obliterated
374 destroy_channel(struct Channel
*chptr
)
376 dlink_node
*ptr
, *next_ptr
;
378 DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
380 del_invite(chptr
, ptr
->data
);
383 /* free all bans/exceptions/denies */
384 free_channel_list(&chptr
->banlist
);
385 free_channel_list(&chptr
->exceptlist
);
386 free_channel_list(&chptr
->invexlist
);
391 dlinkDelete(&chptr
->node
, &global_channel_list
);
392 del_from_channel_hash(chptr
->chname
, chptr
);
396 /* channel_pub_or_secret()
399 * output - "=" if public, "@" if secret, else "*"
403 channel_pub_or_secret(struct Channel
*chptr
)
405 if(PubChannel(chptr
))
407 else if(SecretChannel(chptr
))
412 /* channel_member_names()
414 * input - channel to list, client to list to, show endofnames
416 * side effects - client is given list of users on channel
419 channel_member_names(struct Channel
*chptr
, struct Client
*client_p
, int show_eon
)
421 struct membership
*msptr
;
422 struct Client
*target_p
;
430 int stack
= IsCapable(client_p
, CLICAP_MULTI_PREFIX
);
432 if(ShowChannel(client_p
, chptr
))
434 is_member
= IsMember(client_p
, chptr
);
436 cur_len
= mlen
= ircsprintf(lbuf
, form_str(RPL_NAMREPLY
),
437 me
.name
, client_p
->name
,
438 channel_pub_or_secret(chptr
), chptr
->chname
);
442 DLINK_FOREACH(ptr
, chptr
->members
.head
)
445 target_p
= msptr
->client_p
;
447 if(IsInvisible(target_p
) && !is_member
)
450 /* space, possible "@+" prefix */
451 if(cur_len
+ strlen(target_p
->name
) + 3 >= BUFSIZE
- 3)
454 sendto_one(client_p
, "%s", lbuf
);
459 tlen
= ircsprintf(t
, "%s%s ", find_channel_status(msptr
, stack
),
466 /* The old behaviour here was to always output our buffer,
467 * even if there are no clients we can show. This happens
468 * when a client does "NAMES" with no parameters, and all
469 * the clients on a -sp channel are +i. I dont see a good
470 * reason for keeping that behaviour, as it just wastes
476 sendto_one(client_p
, "%s", lbuf
);
481 sendto_one(client_p
, form_str(RPL_ENDOFNAMES
),
482 me
.name
, client_p
->name
, chptr
->chname
);
487 * input - channel to remove invite from, client to remove
489 * side effects - user is removed from invite list, if exists
492 del_invite(struct Channel
*chptr
, struct Client
*who
)
494 dlinkFindDestroy(who
, &chptr
->invites
);
495 dlinkFindDestroy(chptr
, &who
->user
->invited
);
500 * input - channel to check bans for, user to check bans against
501 * optional prebuilt buffers
502 * output - 1 if banned, else 0
506 is_banned(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
507 const char *s
, const char *s2
)
509 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
510 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
511 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
514 struct Ban
*actualBan
= NULL
;
515 struct Ban
*actualExcept
= NULL
;
520 /* if the buffers havent been built, do it here */
523 ircsprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
524 ircsprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
529 if(who
->localClient
->mangledhost
!= NULL
)
531 /* if host mangling mode enabled, also check their real host */
532 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
534 ircsprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
537 /* if host mangling mode not enabled and no other spoof,
538 * also check the mangled form of their host */
539 else if (!IsDynSpoof(who
))
541 ircsprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
546 DLINK_FOREACH(ptr
, chptr
->banlist
.head
)
548 actualBan
= ptr
->data
;
549 if(match(actualBan
->banstr
, s
) ||
550 match(actualBan
->banstr
, s2
) ||
551 match_cidr(actualBan
->banstr
, s2
) ||
552 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_BAN
) ||
553 (s3
!= NULL
&& match(actualBan
->banstr
, s3
)))
559 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
561 DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
563 actualExcept
= ptr
->data
;
565 /* theyre exempted.. */
566 if(match(actualExcept
->banstr
, s
) ||
567 match(actualExcept
->banstr
, s2
) ||
568 match_cidr(actualExcept
->banstr
, s2
) ||
569 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
570 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
572 /* cache the fact theyre not banned */
575 msptr
->bants
= chptr
->bants
;
576 msptr
->flags
&= ~CHFL_BANNED
;
579 return CHFL_EXCEPTION
;
584 /* cache the banned/not banned status */
587 msptr
->bants
= chptr
->bants
;
589 if(actualBan
!= NULL
)
591 msptr
->flags
|= CHFL_BANNED
;
596 msptr
->flags
&= ~CHFL_BANNED
;
601 return ((actualBan
? CHFL_BAN
: 0));
606 * input - channel to check bans for, user to check bans against
607 * optional prebuilt buffers
608 * output - 1 if banned, else 0
612 is_quieted(struct Channel
*chptr
, struct Client
*who
, struct membership
*msptr
,
613 const char *s
, const char *s2
)
615 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
616 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
617 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
620 struct Ban
*actualBan
= NULL
;
621 struct Ban
*actualExcept
= NULL
;
626 /* if the buffers havent been built, do it here */
629 ircsprintf(src_host
, "%s!%s@%s", who
->name
, who
->username
, who
->host
);
630 ircsprintf(src_iphost
, "%s!%s@%s", who
->name
, who
->username
, who
->sockhost
);
635 if(who
->localClient
->mangledhost
!= NULL
)
637 /* if host mangling mode enabled, also check their real host */
638 if(!strcmp(who
->host
, who
->localClient
->mangledhost
))
640 ircsprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->orighost
);
643 /* if host mangling mode not enabled and no other spoof,
644 * also check the mangled form of their host */
645 else if (!IsDynSpoof(who
))
647 ircsprintf(src_althost
, "%s!%s@%s", who
->name
, who
->username
, who
->localClient
->mangledhost
);
652 DLINK_FOREACH(ptr
, chptr
->quietlist
.head
)
654 actualBan
= ptr
->data
;
655 if(match(actualBan
->banstr
, s
) ||
656 match(actualBan
->banstr
, s2
) ||
657 match_cidr(actualBan
->banstr
, s2
) ||
658 match_extban(actualBan
->banstr
, who
, chptr
, CHFL_QUIET
) ||
659 (s3
!= NULL
&& match(actualBan
->banstr
, s3
)))
665 if((actualBan
!= NULL
) && ConfigChannel
.use_except
)
667 DLINK_FOREACH(ptr
, chptr
->exceptlist
.head
)
669 actualExcept
= ptr
->data
;
671 /* theyre exempted.. */
672 if(match(actualExcept
->banstr
, s
) ||
673 match(actualExcept
->banstr
, s2
) ||
674 match_cidr(actualExcept
->banstr
, s2
) ||
675 match_extban(actualExcept
->banstr
, who
, chptr
, CHFL_EXCEPTION
) ||
676 (s3
!= NULL
&& match(actualExcept
->banstr
, s3
)))
678 /* cache the fact theyre not banned */
681 msptr
->bants
= chptr
->bants
;
682 msptr
->flags
&= ~CHFL_BANNED
;
685 return CHFL_EXCEPTION
;
690 /* cache the banned/not banned status */
693 msptr
->bants
= chptr
->bants
;
695 if(actualBan
!= NULL
)
697 msptr
->flags
|= CHFL_BANNED
;
702 msptr
->flags
&= ~CHFL_BANNED
;
707 return ((actualBan
? CHFL_BAN
: 0));
712 * input - client to check, channel to check for, key
713 * output - reason for not being able to join, else 0
717 can_join(struct Client
*source_p
, struct Channel
*chptr
, char *key
)
719 dlink_node
*invite
= NULL
;
721 struct Ban
*invex
= NULL
;
722 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
723 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
724 char src_althost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
727 hook_data_channel moduledata
;
729 s_assert(source_p
->localClient
!= NULL
);
731 ircsprintf(src_host
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
732 ircsprintf(src_iphost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->sockhost
);
733 if(source_p
->localClient
->mangledhost
!= NULL
)
735 /* if host mangling mode enabled, also check their real host */
736 if(!strcmp(source_p
->host
, source_p
->localClient
->mangledhost
))
738 ircsprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->orighost
);
741 /* if host mangling mode not enabled and no other spoof,
742 * also check the mangled form of their host */
743 else if (!IsDynSpoof(source_p
))
745 ircsprintf(src_althost
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->localClient
->mangledhost
);
750 if((is_banned(chptr
, source_p
, NULL
, src_host
, src_iphost
)) == CHFL_BAN
)
751 return (ERR_BANNEDFROMCHAN
);
753 if(chptr
->mode
.mode
& MODE_INVITEONLY
)
755 DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
757 if(invite
->data
== chptr
)
762 if(!ConfigChannel
.use_invex
)
763 return (ERR_INVITEONLYCHAN
);
764 DLINK_FOREACH(ptr
, chptr
->invexlist
.head
)
767 if(match(invex
->banstr
, src_host
)
768 || match(invex
->banstr
, src_iphost
)
769 || match_cidr(invex
->banstr
, src_iphost
)
770 || match_extban(invex
->banstr
, source_p
, chptr
, CHFL_INVEX
)
771 || (use_althost
&& match(invex
->banstr
, src_althost
)))
775 return (ERR_INVITEONLYCHAN
);
779 if(*chptr
->mode
.key
&& (EmptyString(key
) || irccmp(chptr
->mode
.key
, key
)))
780 return (ERR_BADCHANNELKEY
);
782 if(chptr
->mode
.limit
&&
783 dlink_list_length(&chptr
->members
) >= (unsigned long) chptr
->mode
.limit
)
784 i
= ERR_CHANNELISFULL
;
785 if(chptr
->mode
.mode
& MODE_REGONLY
&& EmptyString(source_p
->user
->suser
))
786 i
= ERR_NEEDREGGEDNICK
;
787 /* join throttling stuff --nenolod */
788 else if(chptr
->mode
.join_num
> 0 && chptr
->mode
.join_time
> 0)
790 if ((CurrentTime
- chptr
->join_delta
<=
791 chptr
->mode
.join_time
) && (chptr
->join_count
>=
792 chptr
->mode
.join_num
))
796 /* allow /invite to override +l/+r/+j also -- jilles */
797 if (i
!= 0 && invite
== NULL
)
799 DLINK_FOREACH(invite
, source_p
->user
->invited
.head
)
801 if(invite
->data
== chptr
)
808 moduledata
.client
= source_p
;
809 moduledata
.chptr
= chptr
;
810 moduledata
.approved
= 0;
812 call_hook(h_can_join
, &moduledata
);
814 return moduledata
.approved
;
819 * input - user to check in channel, membership pointer
820 * output - whether can explicitly send or not, else CAN_SEND_NONOP
824 can_send(struct Channel
*chptr
, struct Client
*source_p
, struct membership
*msptr
)
826 if(IsServer(source_p
) || IsService(source_p
))
829 if(MyClient(source_p
) && hash_find_resv(chptr
->chname
) &&
830 !IsOper(source_p
) && !IsExemptResv(source_p
))
835 msptr
= find_channel_membership(chptr
, source_p
);
839 /* if its +m or +n and theyre not in the channel,
840 * they cant send. we dont check bans here because
841 * theres no possibility of caching them --fl
843 if(chptr
->mode
.mode
& MODE_NOPRIVMSGS
|| chptr
->mode
.mode
& MODE_MODERATED
)
846 return CAN_SEND_NONOP
;
850 if(is_chanop_voiced(msptr
))
853 if(chptr
->mode
.mode
& MODE_MODERATED
)
856 if(MyClient(source_p
))
858 /* cached can_send */
859 if(msptr
->bants
== chptr
->bants
)
861 if(can_send_banned(msptr
))
864 else if(is_banned(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
865 || is_quieted(chptr
, source_p
, msptr
, NULL
, NULL
) == CHFL_BAN
)
869 return CAN_SEND_NONOP
;
872 /* find_bannickchange_channel()
873 * Input: client to check
874 * Output: channel preventing nick change
877 find_bannickchange_channel(struct Client
*client_p
)
879 struct Channel
*chptr
;
880 struct membership
*msptr
;
882 char src_host
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
883 char src_iphost
[NICKLEN
+ USERLEN
+ HOSTLEN
+ 6];
885 if (!MyClient(client_p
))
888 ircsprintf(src_host
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->host
);
889 ircsprintf(src_iphost
, "%s!%s@%s", client_p
->name
, client_p
->username
, client_p
->sockhost
);
891 DLINK_FOREACH(ptr
, client_p
->user
->channel
.head
)
894 chptr
= msptr
->chptr
;
895 if (is_chanop_voiced(msptr
))
897 /* cached can_send */
898 if (msptr
->bants
== chptr
->bants
)
900 if (can_send_banned(msptr
))
903 else if (is_banned(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
904 || is_quieted(chptr
, client_p
, msptr
, src_host
, src_iphost
) == CHFL_BAN
)
910 /* void check_spambot_warning(struct Client *source_p)
911 * Input: Client to check, channel name or NULL if this is a part.
913 * Side-effects: Updates the client's oper_warn_count_down, warns the
914 * IRC operators if necessary, and updates join_leave_countdown as
918 check_spambot_warning(struct Client
*source_p
, const char *name
)
922 if((GlobalSetOptions
.spam_num
&&
923 (source_p
->localClient
->join_leave_count
>= GlobalSetOptions
.spam_num
)))
925 if(source_p
->localClient
->oper_warn_count_down
> 0)
926 source_p
->localClient
->oper_warn_count_down
--;
928 source_p
->localClient
->oper_warn_count_down
= 0;
929 if(source_p
->localClient
->oper_warn_count_down
== 0)
931 /* Its already known as a possible spambot */
933 sendto_realops_snomask(SNO_BOTS
, L_ALL
,
934 "User %s (%s@%s) trying to join %s is a possible spambot",
936 source_p
->username
, source_p
->orighost
, name
);
938 sendto_realops_snomask(SNO_BOTS
, L_ALL
,
939 "User %s (%s@%s) is a possible spambot",
941 source_p
->username
, source_p
->orighost
);
942 source_p
->localClient
->oper_warn_count_down
= OPER_SPAM_COUNTDOWN
;
948 (CurrentTime
- source_p
->localClient
->last_leave_time
)) >
949 JOIN_LEAVE_COUNT_EXPIRE_TIME
)
951 decrement_count
= (t_delta
/ JOIN_LEAVE_COUNT_EXPIRE_TIME
);
952 if(decrement_count
> source_p
->localClient
->join_leave_count
)
953 source_p
->localClient
->join_leave_count
= 0;
955 source_p
->localClient
->join_leave_count
-= decrement_count
;
960 (source_p
->localClient
->last_join_time
)) < GlobalSetOptions
.spam_time
)
962 /* oh, its a possible spambot */
963 source_p
->localClient
->join_leave_count
++;
967 source_p
->localClient
->last_join_time
= CurrentTime
;
969 source_p
->localClient
->last_leave_time
= CurrentTime
;
977 * side effects - compares usercount and servercount against their split
978 * values and adjusts splitmode accordingly
981 check_splitmode(void *unused
)
983 if(splitchecking
&& (ConfigChannel
.no_join_on_split
|| ConfigChannel
.no_create_on_split
))
985 /* not split, we're being asked to check now because someone
990 if(eob_count
< split_servers
|| Count
.total
< split_users
)
993 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
994 "Network split, activating splitmode");
995 eventAddIsh("check_splitmode", check_splitmode
, NULL
, 2);
998 /* in splitmode, check whether its finished */
999 else if(eob_count
>= split_servers
&& Count
.total
>= split_users
)
1003 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1004 "Network rejoined, deactivating splitmode");
1006 eventDelete(check_splitmode
, NULL
);
1014 * input - channel to allocate topic for
1015 * output - 1 on success, else 0
1016 * side effects - channel gets a topic allocated
1019 allocate_topic(struct Channel
*chptr
)
1026 ptr
= BlockHeapAlloc(topic_heap
);
1028 /* Basically we allocate one large block for the topic and
1029 * the topic info. We then split it up into two and shove it
1033 chptr
->topic_info
= (char *) ptr
+ TOPICLEN
+ 1;
1034 *chptr
->topic
= '\0';
1035 *chptr
->topic_info
= '\0';
1040 * input - channel which has topic to free
1042 * side effects - channels topic is free'd
1045 free_topic(struct Channel
*chptr
)
1049 if(chptr
== NULL
|| chptr
->topic
== NULL
)
1052 /* This is safe for now - If you change allocate_topic you
1053 * MUST change this as well
1056 BlockHeapFree(topic_heap
, ptr
);
1057 chptr
->topic
= NULL
;
1058 chptr
->topic_info
= NULL
;
1061 /* set_channel_topic()
1063 * input - channel, topic to set, topic info and topic ts
1065 * side effects - channels topic, topic info and TS are set.
1068 set_channel_topic(struct Channel
*chptr
, const char *topic
, const char *topic_info
, time_t topicts
)
1070 if(strlen(topic
) > 0)
1072 if(chptr
->topic
== NULL
)
1073 allocate_topic(chptr
);
1074 strlcpy(chptr
->topic
, topic
, TOPICLEN
+ 1);
1075 strlcpy(chptr
->topic_info
, topic_info
, USERHOST_REPLYLEN
);
1076 chptr
->topic_time
= topicts
;
1080 if(chptr
->topic
!= NULL
)
1082 chptr
->topic_time
= 0;
1086 static const struct mode_letter
1088 const unsigned int mode
;
1089 const unsigned char letter
;
1092 {MODE_INVITEONLY
, 'i'},
1093 {MODE_MODERATED
, 'm'},
1094 {MODE_NOPRIVMSGS
, 'n'},
1095 {MODE_PRIVATE
, 'p'},
1097 {MODE_TOPICLIMIT
, 't'},
1098 {MODE_NOCOLOR
, 'c'},
1099 {MODE_FREEINVITE
, 'g'},
1100 {MODE_OPMODERATE
, 'z'},
1101 {MODE_EXLIMIT
, 'L'},
1102 {MODE_PERMANENT
, 'P'},
1103 {MODE_FREETARGET
, 'F'},
1104 {MODE_DISFORWARD
, 'Q'},
1105 {MODE_REGONLY
, 'r'},
1111 * inputs - pointer to channel
1112 * - pointer to client
1114 * side effects - write the "simple" list of channel modes for channel
1115 * chptr onto buffer mbuf with the parameters in pbuf.
1117 * Stolen from ShadowIRCd 4 --nenolod
1120 channel_modes(struct Channel
*chptr
, struct Client
*client_p
)
1125 static char final
[BUFSIZE
];
1132 for (i
= 0; flags
[i
].mode
; ++i
)
1133 if(chptr
->mode
.mode
& flags
[i
].mode
)
1134 *mbuf
++ = flags
[i
].letter
;
1136 if(chptr
->mode
.limit
)
1140 if(IsMember(client_p
, chptr
) || IsServer(client_p
) || IsMe(client_p
))
1141 pbuf
+= ircsprintf(pbuf
, "%d ", chptr
->mode
.limit
);
1144 if(*chptr
->mode
.key
)
1148 if(*pbuf
|| IsMember(client_p
, chptr
) || IsServer(client_p
) || IsMe(client_p
))
1149 pbuf
+= ircsprintf(pbuf
, "%s ", chptr
->mode
.key
);
1152 if(chptr
->mode
.join_num
)
1156 if(*pbuf
|| IsMember(client_p
, chptr
) || IsServer(client_p
) || IsMe(client_p
))
1157 pbuf
+= ircsprintf(pbuf
, "%d:%d ", chptr
->mode
.join_num
,
1158 chptr
->mode
.join_time
);
1161 if(*chptr
->mode
.forward
&& (ConfigChannel
.use_forward
|| IsServer(client_p
) || IsMe(client_p
)))
1165 if(*pbuf
|| IsMember(client_p
, chptr
) || IsServer(client_p
) || IsMe(client_p
))
1166 pbuf
+= ircsprintf(pbuf
, "%s ", chptr
->mode
.forward
);
1171 ircsprintf(final
, "%s %s", buf1
, buf2
);
1175 /* Now lets do some stuff to keep track of what combinations of
1177 * Note that the number of combinations doubles each time you add
1178 * something to this list. Each one is only quick if no servers use that
1179 * combination, but if the numbers get too high here MODE will get too
1180 * slow. I suggest if you get more than 7 here, you consider getting rid
1181 * of some and merging or something. If it wasn't for irc+cs we would
1182 * probably not even need to bother about most of these, but unfortunately
1186 /* void init_chcap_usage_counts(void)
1190 * Side-effects - Initialises the usage counts to zero. Fills in the
1191 * chcap_yes and chcap_no combination tables.
1194 init_chcap_usage_counts(void)
1196 unsigned long m
, c
, y
, n
;
1198 memset(chcap_combos
, 0, sizeof(chcap_combos
));
1200 /* For every possible combination */
1201 for (m
= 0; m
< NCHCAP_COMBOS
; m
++)
1203 /* Check each capab */
1204 for (c
= y
= n
= 0; c
< NCHCAPS
; c
++)
1206 if((m
& (1 << c
)) == 0)
1207 n
|= channel_capabs
[c
];
1209 y
|= channel_capabs
[c
];
1211 chcap_combos
[m
].cap_yes
= y
;
1212 chcap_combos
[m
].cap_no
= n
;
1216 /* void set_chcap_usage_counts(struct Client *serv_p)
1217 * Input: serv_p; The client whose capabs to register.
1219 * Side-effects: Increments the usage counts for the correct capab
1223 set_chcap_usage_counts(struct Client
*serv_p
)
1227 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1229 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1230 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1232 chcap_combos
[n
].count
++;
1237 /* This should be impossible -A1kmm. */
1241 /* void set_chcap_usage_counts(struct Client *serv_p)
1243 * Inputs - serv_p; The client whose capabs to register.
1245 * Side-effects - Decrements the usage counts for the correct capab
1249 unset_chcap_usage_counts(struct Client
*serv_p
)
1253 for (n
= 0; n
< NCHCAP_COMBOS
; n
++)
1255 if(IsCapable(serv_p
, chcap_combos
[n
].cap_yes
) &&
1256 NotCapable(serv_p
, chcap_combos
[n
].cap_no
))
1258 /* Hopefully capabs can't change dynamically or anything... */
1259 s_assert(chcap_combos
[n
].count
> 0);
1261 if(chcap_combos
[n
].count
> 0)
1262 chcap_combos
[n
].count
--;
1267 /* This should be impossible -A1kmm. */
1271 /* void send_cap_mode_changes(struct Client *client_p,
1272 * struct Client *source_p,
1273 * struct Channel *chptr, int cap, int nocap)
1274 * Input: The client sending(client_p), the source client(source_p),
1275 * the channel to send mode changes for(chptr)
1277 * Side-effects: Sends the appropriate mode changes to capable servers.
1279 * Reverted back to my original design, except that we now keep a count
1280 * of the number of servers which each combination as an optimisation, so
1281 * the capabs combinations which are not needed are not worked out. -A1kmm
1284 send_cap_mode_changes(struct Client
*client_p
, struct Client
*source_p
,
1285 struct Channel
*chptr
, struct ChModeChange mode_changes
[], int mode_count
)
1287 static char modebuf
[BUFSIZE
];
1288 static char parabuf
[BUFSIZE
];
1289 int i
, mbl
, pbl
, nc
, mc
, preflen
, len
;
1298 /* Now send to servers... */
1299 for (j
= 0; j
< NCHCAP_COMBOS
; j
++)
1301 if(chcap_combos
[j
].count
== 0)
1311 cap
= chcap_combos
[j
].cap_yes
;
1312 nocap
= chcap_combos
[j
].cap_no
;
1315 mbl
= preflen
= ircsprintf(modebuf
, ":%s TMODE %ld %s ",
1316 use_id(source_p
), (long) chptr
->channelts
,
1319 mbl
= preflen
= ircsprintf(modebuf
, ":%s MODE %s ",
1320 source_p
->name
, chptr
->chname
);
1322 /* loop the list of - modes we have */
1323 for (i
= 0; i
< mode_count
; i
++)
1325 /* if they dont support the cap we need, or they do support a cap they
1326 * cant have, then dont add it to the modebuf.. that way they wont see
1329 if((mode_changes
[i
].letter
== 0) ||
1330 ((cap
& mode_changes
[i
].caps
) != mode_changes
[i
].caps
)
1331 || ((nocap
& mode_changes
[i
].nocaps
) != mode_changes
[i
].nocaps
))
1334 if((cap
& CAP_TS6
) && !EmptyString(mode_changes
[i
].id
))
1335 arg
= mode_changes
[i
].id
;
1337 arg
= mode_changes
[i
].arg
;
1341 arglen
= strlen(arg
);
1343 /* dont even think about it! --fl */
1344 if(arglen
> MODEBUFLEN
- 5)
1348 /* if we're creeping past the buf size, we need to send it and make
1349 * another line for the other modes
1350 * XXX - this could give away server topology with uids being
1351 * different lengths, but not much we can do, except possibly break
1352 * them as if they were the longest of the nick or uid at all times,
1353 * which even then won't work as we don't always know the uid -A1kmm.
1355 if(arg
&& ((mc
== MAXMODEPARAMSSERV
) ||
1356 ((mbl
+ pbl
+ arglen
+ 4) > (BUFSIZE
- 3))))
1359 sendto_server(client_p
, chptr
, cap
, nocap
,
1360 "%s %s", modebuf
, parabuf
);
1371 if(dir
!= mode_changes
[i
].dir
)
1373 modebuf
[mbl
++] = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1374 dir
= mode_changes
[i
].dir
;
1377 modebuf
[mbl
++] = mode_changes
[i
].letter
;
1383 len
= ircsprintf(pbuf
, "%s ", arg
);
1390 if(pbl
&& parabuf
[pbl
- 1] == ' ')
1391 parabuf
[pbl
- 1] = 0;
1394 sendto_server(client_p
, chptr
, cap
, nocap
, "%s %s", modebuf
, parabuf
);