]> jfr.im git - solanum.git/blob - src/channel.c
c301953bd9a3509c8a4b6c88eb482cb521aaca14
[solanum.git] / src / channel.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * channel.c: Controls channels.
4 *
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
8 *
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.
13 *
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.
18 *
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
22 * USA
23 *
24 * $Id: channel.c 3580 2007-11-07 23:45:14Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "channel.h"
29 #include "chmode.h"
30 #include "client.h"
31 #include "common.h"
32 #include "hash.h"
33 #include "hook.h"
34 #include "match.h"
35 #include "ircd.h"
36 #include "numeric.h"
37 #include "s_serv.h" /* captab */
38 #include "s_user.h"
39 #include "send.h"
40 #include "whowas.h"
41 #include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
42 #include "s_newconf.h"
43 #include "logger.h"
44
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;
51
52 static int channel_capabs[] = { CAP_EX, CAP_IE,
53 CAP_SERVICE,
54 CAP_TS6
55 };
56
57 #define NCHCAPS (sizeof(channel_capabs)/sizeof(int))
58 #define NCHCAP_COMBOS (1 << NCHCAPS)
59
60 static struct ChCapCombo chcap_combos[NCHCAP_COMBOS];
61
62 static void free_topic(struct Channel *chptr);
63
64 static int h_can_join;
65 static int h_can_send;
66 int h_get_channel_access;
67
68 /* init_channels()
69 *
70 * input -
71 * output -
72 * side effects - initialises the various blockheaps
73 */
74 void
75 init_channels(void)
76 {
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");
81
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");
85 }
86
87 /*
88 * allocate_channel - Allocates a channel
89 */
90 struct Channel *
91 allocate_channel(const char *chname)
92 {
93 struct Channel *chptr;
94 chptr = rb_bh_alloc(channel_heap);
95 chptr->chname = rb_strdup(chname);
96 return (chptr);
97 }
98
99 void
100 free_channel(struct Channel *chptr)
101 {
102 rb_free(chptr->chname);
103 rb_free(chptr->mode_lock);
104 rb_bh_free(channel_heap, chptr);
105 }
106
107 struct Ban *
108 allocate_ban(const char *banstr, const char *who)
109 {
110 struct Ban *bptr;
111 bptr = rb_bh_alloc(ban_heap);
112 bptr->banstr = rb_strdup(banstr);
113 bptr->who = rb_strdup(who);
114
115 return (bptr);
116 }
117
118 void
119 free_ban(struct Ban *bptr)
120 {
121 rb_free(bptr->banstr);
122 rb_free(bptr->who);
123 rb_bh_free(ban_heap, bptr);
124 }
125
126
127 /* find_channel_membership()
128 *
129 * input - channel to find them in, client to find
130 * output - membership of client in channel, else NULL
131 * side effects -
132 */
133 struct membership *
134 find_channel_membership(struct Channel *chptr, struct Client *client_p)
135 {
136 struct membership *msptr;
137 rb_dlink_node *ptr;
138
139 if(!IsClient(client_p))
140 return NULL;
141
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
144 */
145 if(rb_dlink_list_length(&chptr->members) < rb_dlink_list_length(&client_p->user->channel))
146 {
147 RB_DLINK_FOREACH(ptr, chptr->members.head)
148 {
149 msptr = ptr->data;
150
151 if(msptr->client_p == client_p)
152 return msptr;
153 }
154 }
155 else
156 {
157 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
158 {
159 msptr = ptr->data;
160
161 if(msptr->chptr == chptr)
162 return msptr;
163 }
164 }
165
166 return NULL;
167 }
168
169 /* find_channel_status()
170 *
171 * input - membership to get status for, whether we can combine flags
172 * output - flags of user on channel
173 * side effects -
174 */
175 const char *
176 find_channel_status(struct membership *msptr, int combine)
177 {
178 static char buffer[3];
179 char *p;
180
181 p = buffer;
182
183 if(is_chanop(msptr))
184 {
185 if(!combine)
186 return "@";
187 *p++ = '@';
188 }
189
190 if(is_voiced(msptr))
191 *p++ = '+';
192
193 *p = '\0';
194 return buffer;
195 }
196
197 /* add_user_to_channel()
198 *
199 * input - channel to add client to, client to add, channel flags
200 * output -
201 * side effects - user is added to channel
202 */
203 void
204 add_user_to_channel(struct Channel *chptr, struct Client *client_p, int flags)
205 {
206 struct membership *msptr;
207
208 s_assert(client_p->user != NULL);
209 if(client_p->user == NULL)
210 return;
211
212 msptr = rb_bh_alloc(member_heap);
213
214 msptr->chptr = chptr;
215 msptr->client_p = client_p;
216 msptr->flags = flags;
217
218 rb_dlinkAdd(msptr, &msptr->usernode, &client_p->user->channel);
219 rb_dlinkAdd(msptr, &msptr->channode, &chptr->members);
220
221 if(MyClient(client_p))
222 rb_dlinkAdd(msptr, &msptr->locchannode, &chptr->locmembers);
223 }
224
225 /* remove_user_from_channel()
226 *
227 * input - membership pointer to remove from channel
228 * output -
229 * side effects - membership (thus user) is removed from channel
230 */
231 void
232 remove_user_from_channel(struct membership *msptr)
233 {
234 struct Client *client_p;
235 struct Channel *chptr;
236 s_assert(msptr != NULL);
237 if(msptr == NULL)
238 return;
239
240 client_p = msptr->client_p;
241 chptr = msptr->chptr;
242
243 rb_dlinkDelete(&msptr->usernode, &client_p->user->channel);
244 rb_dlinkDelete(&msptr->channode, &chptr->members);
245
246 if(client_p->servptr == &me)
247 rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers);
248
249 if(!(chptr->mode.mode & MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0)
250 destroy_channel(chptr);
251
252 rb_bh_free(member_heap, msptr);
253
254 return;
255 }
256
257 /* remove_user_from_channels()
258 *
259 * input - user to remove from all channels
260 * output -
261 * side effects - user is removed from all channels
262 */
263 void
264 remove_user_from_channels(struct Client *client_p)
265 {
266 struct Channel *chptr;
267 struct membership *msptr;
268 rb_dlink_node *ptr;
269 rb_dlink_node *next_ptr;
270
271 if(client_p == NULL)
272 return;
273
274 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->user->channel.head)
275 {
276 msptr = ptr->data;
277 chptr = msptr->chptr;
278
279 rb_dlinkDelete(&msptr->channode, &chptr->members);
280
281 if(client_p->servptr == &me)
282 rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers);
283
284 if(!(chptr->mode.mode & MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0)
285 destroy_channel(chptr);
286
287 rb_bh_free(member_heap, msptr);
288 }
289
290 client_p->user->channel.head = client_p->user->channel.tail = NULL;
291 client_p->user->channel.length = 0;
292 }
293
294 /* invalidate_bancache_user()
295 *
296 * input - user to invalidate ban cache for
297 * output -
298 * side effects - ban cache is invalidated for all memberships of that user
299 * to be used after a nick change
300 */
301 void
302 invalidate_bancache_user(struct Client *client_p)
303 {
304 struct membership *msptr;
305 rb_dlink_node *ptr;
306
307 if(client_p == NULL)
308 return;
309
310 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
311 {
312 msptr = ptr->data;
313 msptr->bants = 0;
314 msptr->flags &= ~CHFL_BANNED;
315 }
316 }
317
318 /* check_channel_name()
319 *
320 * input - channel name
321 * output - 1 if valid channel name, else 0
322 * side effects -
323 */
324 int
325 check_channel_name(const char *name)
326 {
327 s_assert(name != NULL);
328 if(name == NULL)
329 return 0;
330
331 for (; *name; ++name)
332 {
333 if(!IsChanChar(*name))
334 return 0;
335 }
336
337 return 1;
338 }
339
340 /* free_channel_list()
341 *
342 * input - rb_dlink list to free
343 * output -
344 * side effects - list of b/e/I modes is cleared
345 */
346 void
347 free_channel_list(rb_dlink_list * list)
348 {
349 rb_dlink_node *ptr;
350 rb_dlink_node *next_ptr;
351 struct Ban *actualBan;
352
353 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
354 {
355 actualBan = ptr->data;
356 free_ban(actualBan);
357 }
358
359 list->head = list->tail = NULL;
360 list->length = 0;
361 }
362
363 /* destroy_channel()
364 *
365 * input - channel to destroy
366 * output -
367 * side effects - channel is obliterated
368 */
369 void
370 destroy_channel(struct Channel *chptr)
371 {
372 rb_dlink_node *ptr, *next_ptr;
373
374 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->invites.head)
375 {
376 del_invite(chptr, ptr->data);
377 }
378
379 /* free all bans/exceptions/denies */
380 free_channel_list(&chptr->banlist);
381 free_channel_list(&chptr->exceptlist);
382 free_channel_list(&chptr->invexlist);
383 free_channel_list(&chptr->quietlist);
384
385 /* Free the topic */
386 free_topic(chptr);
387
388 rb_dlinkDelete(&chptr->node, &global_channel_list);
389 del_from_channel_hash(chptr->chname, chptr);
390 free_channel(chptr);
391 }
392
393 /* channel_pub_or_secret()
394 *
395 * input - channel
396 * output - "=" if public, "@" if secret, else "*"
397 * side effects -
398 */
399 static const char *
400 channel_pub_or_secret(struct Channel *chptr)
401 {
402 if(PubChannel(chptr))
403 return ("=");
404 else if(SecretChannel(chptr))
405 return ("@");
406 return ("*");
407 }
408
409 /* channel_member_names()
410 *
411 * input - channel to list, client to list to, show endofnames
412 * output -
413 * side effects - client is given list of users on channel
414 */
415 void
416 channel_member_names(struct Channel *chptr, struct Client *client_p, int show_eon)
417 {
418 struct membership *msptr;
419 struct Client *target_p;
420 rb_dlink_node *ptr;
421 char lbuf[BUFSIZE];
422 char *t;
423 int mlen;
424 int tlen;
425 int cur_len;
426 int is_member;
427 int stack = IsCapable(client_p, CLICAP_MULTI_PREFIX);
428
429 if(ShowChannel(client_p, chptr))
430 {
431 is_member = IsMember(client_p, chptr);
432
433 cur_len = mlen = rb_sprintf(lbuf, form_str(RPL_NAMREPLY),
434 me.name, client_p->name,
435 channel_pub_or_secret(chptr), chptr->chname);
436
437 t = lbuf + cur_len;
438
439 RB_DLINK_FOREACH(ptr, chptr->members.head)
440 {
441 msptr = ptr->data;
442 target_p = msptr->client_p;
443
444 if(IsInvisible(target_p) && !is_member)
445 continue;
446
447 /* space, possible "@+" prefix */
448 if(cur_len + strlen(target_p->name) + 3 >= BUFSIZE - 3)
449 {
450 *(t - 1) = '\0';
451 sendto_one(client_p, "%s", lbuf);
452 cur_len = mlen;
453 t = lbuf + mlen;
454 }
455
456 tlen = rb_sprintf(t, "%s%s ", find_channel_status(msptr, stack),
457 target_p->name);
458
459 cur_len += tlen;
460 t += tlen;
461 }
462
463 /* The old behaviour here was to always output our buffer,
464 * even if there are no clients we can show. This happens
465 * when a client does "NAMES" with no parameters, and all
466 * the clients on a -sp channel are +i. I dont see a good
467 * reason for keeping that behaviour, as it just wastes
468 * bandwidth. --anfl
469 */
470 if(cur_len != mlen)
471 {
472 *(t - 1) = '\0';
473 sendto_one(client_p, "%s", lbuf);
474 }
475 }
476
477 if(show_eon)
478 sendto_one(client_p, form_str(RPL_ENDOFNAMES),
479 me.name, client_p->name, chptr->chname);
480 }
481
482 /* del_invite()
483 *
484 * input - channel to remove invite from, client to remove
485 * output -
486 * side effects - user is removed from invite list, if exists
487 */
488 void
489 del_invite(struct Channel *chptr, struct Client *who)
490 {
491 rb_dlinkFindDestroy(who, &chptr->invites);
492 rb_dlinkFindDestroy(chptr, &who->user->invited);
493 }
494
495 /* is_banned()
496 *
497 * input - channel to check bans for, user to check bans against
498 * optional prebuilt buffers
499 * output - 1 if banned, else 0
500 * side effects -
501 */
502 int
503 is_banned(struct Channel *chptr, struct Client *who, struct membership *msptr,
504 const char *s, const char *s2)
505 {
506 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
507 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
508 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
509 char *s3 = NULL;
510 rb_dlink_node *ptr;
511 struct Ban *actualBan = NULL;
512 struct Ban *actualExcept = NULL;
513
514 if(!MyClient(who))
515 return 0;
516
517 /* if the buffers havent been built, do it here */
518 if(s == NULL)
519 {
520 rb_sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host);
521 rb_sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost);
522
523 s = src_host;
524 s2 = src_iphost;
525 }
526 if(who->localClient->mangledhost != NULL)
527 {
528 /* if host mangling mode enabled, also check their real host */
529 if(!strcmp(who->host, who->localClient->mangledhost))
530 {
531 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost);
532 s3 = src_althost;
533 }
534 /* if host mangling mode not enabled and no other spoof,
535 * also check the mangled form of their host */
536 else if (!IsDynSpoof(who))
537 {
538 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost);
539 s3 = src_althost;
540 }
541 }
542
543 RB_DLINK_FOREACH(ptr, chptr->banlist.head)
544 {
545 actualBan = ptr->data;
546 if(match(actualBan->banstr, s) ||
547 match(actualBan->banstr, s2) ||
548 match_cidr(actualBan->banstr, s2) ||
549 match_extban(actualBan->banstr, who, chptr, CHFL_BAN) ||
550 (s3 != NULL && match(actualBan->banstr, s3)))
551 break;
552 else
553 actualBan = NULL;
554 }
555
556 if((actualBan != NULL) && ConfigChannel.use_except)
557 {
558 RB_DLINK_FOREACH(ptr, chptr->exceptlist.head)
559 {
560 actualExcept = ptr->data;
561
562 /* theyre exempted.. */
563 if(match(actualExcept->banstr, s) ||
564 match(actualExcept->banstr, s2) ||
565 match_cidr(actualExcept->banstr, s2) ||
566 match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) ||
567 (s3 != NULL && match(actualExcept->banstr, s3)))
568 {
569 /* cache the fact theyre not banned */
570 if(msptr != NULL)
571 {
572 msptr->bants = chptr->bants;
573 msptr->flags &= ~CHFL_BANNED;
574 }
575
576 return CHFL_EXCEPTION;
577 }
578 }
579 }
580
581 /* cache the banned/not banned status */
582 if(msptr != NULL)
583 {
584 msptr->bants = chptr->bants;
585
586 if(actualBan != NULL)
587 {
588 msptr->flags |= CHFL_BANNED;
589 return CHFL_BAN;
590 }
591 else
592 {
593 msptr->flags &= ~CHFL_BANNED;
594 return 0;
595 }
596 }
597
598 return ((actualBan ? CHFL_BAN : 0));
599 }
600
601 /* is_quieted()
602 *
603 * input - channel to check bans for, user to check bans against
604 * optional prebuilt buffers
605 * output - 1 if banned, else 0
606 * side effects -
607 */
608 int
609 is_quieted(struct Channel *chptr, struct Client *who, struct membership *msptr,
610 const char *s, const char *s2)
611 {
612 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
613 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
614 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
615 char *s3 = NULL;
616 rb_dlink_node *ptr;
617 struct Ban *actualBan = NULL;
618 struct Ban *actualExcept = NULL;
619
620 if(!MyClient(who))
621 return 0;
622
623 /* if the buffers havent been built, do it here */
624 if(s == NULL)
625 {
626 rb_sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host);
627 rb_sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost);
628
629 s = src_host;
630 s2 = src_iphost;
631 }
632 if(who->localClient->mangledhost != NULL)
633 {
634 /* if host mangling mode enabled, also check their real host */
635 if(!strcmp(who->host, who->localClient->mangledhost))
636 {
637 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost);
638 s3 = src_althost;
639 }
640 /* if host mangling mode not enabled and no other spoof,
641 * also check the mangled form of their host */
642 else if (!IsDynSpoof(who))
643 {
644 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost);
645 s3 = src_althost;
646 }
647 }
648
649 RB_DLINK_FOREACH(ptr, chptr->quietlist.head)
650 {
651 actualBan = ptr->data;
652 if(match(actualBan->banstr, s) ||
653 match(actualBan->banstr, s2) ||
654 match_cidr(actualBan->banstr, s2) ||
655 match_extban(actualBan->banstr, who, chptr, CHFL_QUIET) ||
656 (s3 != NULL && match(actualBan->banstr, s3)))
657 break;
658 else
659 actualBan = NULL;
660 }
661
662 if((actualBan != NULL) && ConfigChannel.use_except)
663 {
664 RB_DLINK_FOREACH(ptr, chptr->exceptlist.head)
665 {
666 actualExcept = ptr->data;
667
668 /* theyre exempted.. */
669 if(match(actualExcept->banstr, s) ||
670 match(actualExcept->banstr, s2) ||
671 match_cidr(actualExcept->banstr, s2) ||
672 match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) ||
673 (s3 != NULL && match(actualExcept->banstr, s3)))
674 {
675 /* cache the fact theyre not banned */
676 if(msptr != NULL)
677 {
678 msptr->bants = chptr->bants;
679 msptr->flags &= ~CHFL_BANNED;
680 }
681
682 return CHFL_EXCEPTION;
683 }
684 }
685 }
686
687 /* cache the banned/not banned status */
688 if(msptr != NULL)
689 {
690 msptr->bants = chptr->bants;
691
692 if(actualBan != NULL)
693 {
694 msptr->flags |= CHFL_BANNED;
695 return CHFL_BAN;
696 }
697 else
698 {
699 msptr->flags &= ~CHFL_BANNED;
700 return 0;
701 }
702 }
703
704 return ((actualBan ? CHFL_BAN : 0));
705 }
706
707 /* can_join()
708 *
709 * input - client to check, channel to check for, key
710 * output - reason for not being able to join, else 0
711 * side effects -
712 * caveats - this function should only be called on a local user.
713 */
714 int
715 can_join(struct Client *source_p, struct Channel *chptr, char *key)
716 {
717 rb_dlink_node *invite = NULL;
718 rb_dlink_node *ptr;
719 struct Ban *invex = NULL;
720 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
721 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
722 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
723 int use_althost = 0;
724 int i = 0;
725 hook_data_channel moduledata;
726
727 s_assert(source_p->localClient != NULL);
728
729 moduledata.client = source_p;
730 moduledata.chptr = chptr;
731 moduledata.approved = 0;
732
733 rb_sprintf(src_host, "%s!%s@%s", source_p->name, source_p->username, source_p->host);
734 rb_sprintf(src_iphost, "%s!%s@%s", source_p->name, source_p->username, source_p->sockhost);
735 if(source_p->localClient->mangledhost != NULL)
736 {
737 /* if host mangling mode enabled, also check their real host */
738 if(!strcmp(source_p->host, source_p->localClient->mangledhost))
739 {
740 rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->orighost);
741 use_althost = 1;
742 }
743 /* if host mangling mode not enabled and no other spoof,
744 * also check the mangled form of their host */
745 else if (!IsDynSpoof(source_p))
746 {
747 rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->localClient->mangledhost);
748 use_althost = 1;
749 }
750 }
751
752 if((is_banned(chptr, source_p, NULL, src_host, src_iphost)) == CHFL_BAN)
753 {
754 moduledata.approved = ERR_BANNEDFROMCHAN;
755 goto finish_join_check;
756 }
757
758 if(chptr->mode.mode & MODE_INVITEONLY)
759 {
760 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
761 {
762 if(invite->data == chptr)
763 break;
764 }
765 if(invite == NULL)
766 {
767 if(!ConfigChannel.use_invex)
768 moduledata.approved = ERR_INVITEONLYCHAN;
769 RB_DLINK_FOREACH(ptr, chptr->invexlist.head)
770 {
771 invex = ptr->data;
772 if(match(invex->banstr, src_host)
773 || match(invex->banstr, src_iphost)
774 || match_cidr(invex->banstr, src_iphost)
775 || match_extban(invex->banstr, source_p, chptr, CHFL_INVEX)
776 || (use_althost && match(invex->banstr, src_althost)))
777 break;
778 }
779 if(ptr == NULL)
780 moduledata.approved = ERR_INVITEONLYCHAN;
781 }
782 }
783
784 if(*chptr->mode.key && (EmptyString(key) || irccmp(chptr->mode.key, key)))
785 moduledata.approved = ERR_BADCHANNELKEY;
786
787 if(chptr->mode.limit &&
788 rb_dlink_list_length(&chptr->members) >= (unsigned long) chptr->mode.limit)
789 i = ERR_CHANNELISFULL;
790 if(chptr->mode.mode & MODE_REGONLY && EmptyString(source_p->user->suser))
791 i = ERR_NEEDREGGEDNICK;
792 /* join throttling stuff --nenolod */
793 else if(chptr->mode.join_num > 0 && chptr->mode.join_time > 0)
794 {
795 if ((rb_current_time() - chptr->join_delta <=
796 chptr->mode.join_time) && (chptr->join_count >=
797 chptr->mode.join_num))
798 i = ERR_THROTTLE;
799 }
800
801 /* allow /invite to override +l/+r/+j also -- jilles */
802 if (i != 0 && invite == NULL)
803 {
804 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
805 {
806 if(invite->data == chptr)
807 break;
808 }
809 if (invite == NULL)
810 moduledata.approved = i;
811 }
812
813 finish_join_check:
814 call_hook(h_can_join, &moduledata);
815
816 return moduledata.approved;
817 }
818
819 /* can_send()
820 *
821 * input - user to check in channel, membership pointer
822 * output - whether can explicitly send or not, else CAN_SEND_NONOP
823 * side effects -
824 */
825 int
826 can_send(struct Channel *chptr, struct Client *source_p, struct membership *msptr)
827 {
828 hook_data_channel_approval moduledata;
829
830 moduledata.approved = CAN_SEND_NONOP;
831
832 if(IsServer(source_p) || IsService(source_p))
833 return CAN_SEND_OPV;
834
835 if(MyClient(source_p) && hash_find_resv(chptr->chname) &&
836 !IsOper(source_p) && !IsExemptResv(source_p))
837 moduledata.approved = CAN_SEND_NO;
838
839 if(msptr == NULL)
840 {
841 msptr = find_channel_membership(chptr, source_p);
842
843 if(msptr == NULL)
844 {
845 /* if its +m or +n and theyre not in the channel,
846 * they cant send. we dont check bans here because
847 * theres no possibility of caching them --fl
848 */
849 if(chptr->mode.mode & MODE_NOPRIVMSGS || chptr->mode.mode & MODE_MODERATED)
850 moduledata.approved = CAN_SEND_NO;
851 else
852 moduledata.approved = CAN_SEND_NONOP;
853 }
854 }
855
856 if(is_chanop_voiced(msptr))
857 moduledata.approved = CAN_SEND_OPV;
858
859 if(chptr->mode.mode & MODE_MODERATED)
860 moduledata.approved = CAN_SEND_NO;
861
862 if(MyClient(source_p))
863 {
864 /* cached can_send */
865 if(msptr->bants == chptr->bants)
866 {
867 if(can_send_banned(msptr))
868 moduledata.approved = CAN_SEND_NO;
869 }
870 else if(is_banned(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN
871 || is_quieted(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN)
872 moduledata.approved = CAN_SEND_NO;
873 }
874
875 call_hook(h_can_send, &moduledata);
876
877 return moduledata.approved;
878 }
879
880 /*
881 * flood_attack_channel
882 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
883 * says NOTICE must not auto reply
884 * - pointer to source Client
885 * - pointer to target channel
886 * output - 1 if target is under flood attack
887 * side effects - check for flood attack on target chptr
888 */
889 int
890 flood_attack_channel(int p_or_n, struct Client *source_p, struct Channel *chptr, char *chname)
891 {
892 int delta;
893
894 if(GlobalSetOptions.floodcount && MyClient(source_p))
895 {
896 if((chptr->first_received_message_time + 1) < rb_current_time())
897 {
898 delta = rb_current_time() - chptr->first_received_message_time;
899 chptr->received_number_of_privmsgs -= delta;
900 chptr->first_received_message_time = rb_current_time();
901 if(chptr->received_number_of_privmsgs <= 0)
902 {
903 chptr->received_number_of_privmsgs = 0;
904 chptr->flood_noticed = 0;
905 }
906 }
907
908 if((chptr->received_number_of_privmsgs >= GlobalSetOptions.floodcount)
909 || chptr->flood_noticed)
910 {
911 if(chptr->flood_noticed == 0)
912 {
913 sendto_realops_snomask(SNO_BOTS, *chptr->chname == '&' ? L_ALL : L_NETWIDE,
914 "Possible Flooder %s[%s@%s] on %s target: %s",
915 source_p->name, source_p->username,
916 source_p->orighost,
917 source_p->servptr->name, chptr->chname);
918 chptr->flood_noticed = 1;
919
920 /* Add a bit of penalty */
921 chptr->received_number_of_privmsgs += 2;
922 }
923 if(MyClient(source_p) && (p_or_n != 1))
924 sendto_one(source_p,
925 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
926 me.name, source_p->name, chptr->chname);
927 return 1;
928 }
929 else
930 chptr->received_number_of_privmsgs++;
931 }
932
933 return 0;
934 }
935
936 /* find_bannickchange_channel()
937 * Input: client to check
938 * Output: channel preventing nick change
939 */
940 struct Channel *
941 find_bannickchange_channel(struct Client *client_p)
942 {
943 struct Channel *chptr;
944 struct membership *msptr;
945 rb_dlink_node *ptr;
946 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
947 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
948
949 if (!MyClient(client_p))
950 return NULL;
951
952 rb_sprintf(src_host, "%s!%s@%s", client_p->name, client_p->username, client_p->host);
953 rb_sprintf(src_iphost, "%s!%s@%s", client_p->name, client_p->username, client_p->sockhost);
954
955 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
956 {
957 msptr = ptr->data;
958 chptr = msptr->chptr;
959 if (is_chanop_voiced(msptr))
960 continue;
961 /* cached can_send */
962 if (msptr->bants == chptr->bants)
963 {
964 if (can_send_banned(msptr))
965 return chptr;
966 }
967 else if (is_banned(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN
968 || is_quieted(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN)
969 return chptr;
970 }
971 return NULL;
972 }
973
974 /* void check_spambot_warning(struct Client *source_p)
975 * Input: Client to check, channel name or NULL if this is a part.
976 * Output: none
977 * Side-effects: Updates the client's oper_warn_count_down, warns the
978 * IRC operators if necessary, and updates join_leave_countdown as
979 * needed.
980 */
981 void
982 check_spambot_warning(struct Client *source_p, const char *name)
983 {
984 int t_delta;
985 int decrement_count;
986 if((GlobalSetOptions.spam_num &&
987 (source_p->localClient->join_leave_count >= GlobalSetOptions.spam_num)))
988 {
989 if(source_p->localClient->oper_warn_count_down > 0)
990 source_p->localClient->oper_warn_count_down--;
991 else
992 source_p->localClient->oper_warn_count_down = 0;
993 if(source_p->localClient->oper_warn_count_down == 0 &&
994 name != NULL)
995 {
996 /* Its already known as a possible spambot */
997 sendto_realops_snomask(SNO_BOTS, L_NETWIDE,
998 "User %s (%s@%s) trying to join %s is a possible spambot",
999 source_p->name,
1000 source_p->username, source_p->orighost, name);
1001 source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
1002 }
1003 }
1004 else
1005 {
1006 if((t_delta =
1007 (rb_current_time() - source_p->localClient->last_leave_time)) >
1008 JOIN_LEAVE_COUNT_EXPIRE_TIME)
1009 {
1010 decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
1011 if(name != NULL)
1012 ;
1013 else if(decrement_count > source_p->localClient->join_leave_count)
1014 source_p->localClient->join_leave_count = 0;
1015 else
1016 source_p->localClient->join_leave_count -= decrement_count;
1017 }
1018 else
1019 {
1020 if((rb_current_time() -
1021 (source_p->localClient->last_join_time)) < GlobalSetOptions.spam_time)
1022 {
1023 /* oh, its a possible spambot */
1024 source_p->localClient->join_leave_count++;
1025 }
1026 }
1027 if(name != NULL)
1028 source_p->localClient->last_join_time = rb_current_time();
1029 else
1030 source_p->localClient->last_leave_time = rb_current_time();
1031 }
1032 }
1033
1034 /* check_splitmode()
1035 *
1036 * input -
1037 * output -
1038 * side effects - compares usercount and servercount against their split
1039 * values and adjusts splitmode accordingly
1040 */
1041 void
1042 check_splitmode(void *unused)
1043 {
1044 if(splitchecking && (ConfigChannel.no_join_on_split || ConfigChannel.no_create_on_split))
1045 {
1046 /* not split, we're being asked to check now because someone
1047 * has left
1048 */
1049 if(!splitmode)
1050 {
1051 if(eob_count < split_servers || Count.total < split_users)
1052 {
1053 splitmode = 1;
1054 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1055 "Network split, activating splitmode");
1056 check_splitmode_ev = rb_event_addish("check_splitmode", check_splitmode, NULL, 2);
1057 }
1058 }
1059 /* in splitmode, check whether its finished */
1060 else if(eob_count >= split_servers && Count.total >= split_users)
1061 {
1062 splitmode = 0;
1063
1064 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1065 "Network rejoined, deactivating splitmode");
1066
1067 rb_event_delete(check_splitmode_ev);
1068 check_splitmode_ev = NULL;
1069 }
1070 }
1071 }
1072
1073
1074 /* allocate_topic()
1075 *
1076 * input - channel to allocate topic for
1077 * output - 1 on success, else 0
1078 * side effects - channel gets a topic allocated
1079 */
1080 static void
1081 allocate_topic(struct Channel *chptr)
1082 {
1083 void *ptr;
1084
1085 if(chptr == NULL)
1086 return;
1087
1088 ptr = rb_bh_alloc(topic_heap);
1089
1090 /* Basically we allocate one large block for the topic and
1091 * the topic info. We then split it up into two and shove it
1092 * in the chptr
1093 */
1094 chptr->topic = ptr;
1095 chptr->topic_info = (char *) ptr + TOPICLEN + 1;
1096 *chptr->topic = '\0';
1097 *chptr->topic_info = '\0';
1098 }
1099
1100 /* free_topic()
1101 *
1102 * input - channel which has topic to free
1103 * output -
1104 * side effects - channels topic is free'd
1105 */
1106 static void
1107 free_topic(struct Channel *chptr)
1108 {
1109 void *ptr;
1110
1111 if(chptr == NULL || chptr->topic == NULL)
1112 return;
1113
1114 /* This is safe for now - If you change allocate_topic you
1115 * MUST change this as well
1116 */
1117 ptr = chptr->topic;
1118 rb_bh_free(topic_heap, ptr);
1119 chptr->topic = NULL;
1120 chptr->topic_info = NULL;
1121 }
1122
1123 /* set_channel_topic()
1124 *
1125 * input - channel, topic to set, topic info and topic ts
1126 * output -
1127 * side effects - channels topic, topic info and TS are set.
1128 */
1129 void
1130 set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_info, time_t topicts)
1131 {
1132 if(strlen(topic) > 0)
1133 {
1134 if(chptr->topic == NULL)
1135 allocate_topic(chptr);
1136 rb_strlcpy(chptr->topic, topic, TOPICLEN + 1);
1137 rb_strlcpy(chptr->topic_info, topic_info, USERHOST_REPLYLEN);
1138 chptr->topic_time = topicts;
1139 }
1140 else
1141 {
1142 if(chptr->topic != NULL)
1143 free_topic(chptr);
1144 chptr->topic_time = 0;
1145 }
1146 }
1147
1148 /* channel_modes()
1149 *
1150 * inputs - pointer to channel
1151 * - pointer to client
1152 * output - string with simple modes
1153 * side effects - result from previous calls overwritten
1154 *
1155 * Stolen from ShadowIRCd 4 --nenolod
1156 */
1157 const char *
1158 channel_modes(struct Channel *chptr, struct Client *client_p)
1159 {
1160 int i;
1161 char buf1[BUFSIZE];
1162 char buf2[BUFSIZE];
1163 static char final[BUFSIZE];
1164 char *mbuf = buf1;
1165 char *pbuf = buf2;
1166
1167 *mbuf++ = '+';
1168 *pbuf = '\0';
1169
1170 for (i = 0; i < 256; i++)
1171 if(chptr->mode.mode & chmode_flags[i])
1172 *mbuf++ = i;
1173
1174 if(chptr->mode.limit)
1175 {
1176 *mbuf++ = 'l';
1177
1178 if(!IsClient(client_p) || IsMember(client_p, chptr))
1179 pbuf += rb_sprintf(pbuf, " %d", chptr->mode.limit);
1180 }
1181
1182 if(*chptr->mode.key)
1183 {
1184 *mbuf++ = 'k';
1185
1186 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1187 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.key);
1188 }
1189
1190 if(chptr->mode.join_num)
1191 {
1192 *mbuf++ = 'j';
1193
1194 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1195 pbuf += rb_sprintf(pbuf, " %d:%d", chptr->mode.join_num,
1196 chptr->mode.join_time);
1197 }
1198
1199 if(*chptr->mode.forward && (ConfigChannel.use_forward || !IsClient(client_p)))
1200 {
1201 *mbuf++ = 'f';
1202
1203 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1204 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.forward);
1205 }
1206
1207 *mbuf = '\0';
1208
1209 rb_strlcpy(final, buf1, sizeof final);
1210 rb_strlcat(final, buf2, sizeof final);
1211 return final;
1212 }
1213
1214 /* Now lets do some stuff to keep track of what combinations of
1215 * servers exist...
1216 * Note that the number of combinations doubles each time you add
1217 * something to this list. Each one is only quick if no servers use that
1218 * combination, but if the numbers get too high here MODE will get too
1219 * slow. I suggest if you get more than 7 here, you consider getting rid
1220 * of some and merging or something. If it wasn't for irc+cs we would
1221 * probably not even need to bother about most of these, but unfortunately
1222 * we do. -A1kmm
1223 */
1224
1225 /* void init_chcap_usage_counts(void)
1226 *
1227 * Inputs - none
1228 * Output - none
1229 * Side-effects - Initialises the usage counts to zero. Fills in the
1230 * chcap_yes and chcap_no combination tables.
1231 */
1232 void
1233 init_chcap_usage_counts(void)
1234 {
1235 unsigned long m, c, y, n;
1236
1237 memset(chcap_combos, 0, sizeof(chcap_combos));
1238
1239 /* For every possible combination */
1240 for (m = 0; m < NCHCAP_COMBOS; m++)
1241 {
1242 /* Check each capab */
1243 for (c = y = n = 0; c < NCHCAPS; c++)
1244 {
1245 if((m & (1 << c)) == 0)
1246 n |= channel_capabs[c];
1247 else
1248 y |= channel_capabs[c];
1249 }
1250 chcap_combos[m].cap_yes = y;
1251 chcap_combos[m].cap_no = n;
1252 }
1253 }
1254
1255 /* void set_chcap_usage_counts(struct Client *serv_p)
1256 * Input: serv_p; The client whose capabs to register.
1257 * Output: none
1258 * Side-effects: Increments the usage counts for the correct capab
1259 * combination.
1260 */
1261 void
1262 set_chcap_usage_counts(struct Client *serv_p)
1263 {
1264 int n;
1265
1266 for (n = 0; n < NCHCAP_COMBOS; n++)
1267 {
1268 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1269 NotCapable(serv_p, chcap_combos[n].cap_no))
1270 {
1271 chcap_combos[n].count++;
1272 return;
1273 }
1274 }
1275
1276 /* This should be impossible -A1kmm. */
1277 s_assert(0);
1278 }
1279
1280 /* void set_chcap_usage_counts(struct Client *serv_p)
1281 *
1282 * Inputs - serv_p; The client whose capabs to register.
1283 * Output - none
1284 * Side-effects - Decrements the usage counts for the correct capab
1285 * combination.
1286 */
1287 void
1288 unset_chcap_usage_counts(struct Client *serv_p)
1289 {
1290 int n;
1291
1292 for (n = 0; n < NCHCAP_COMBOS; n++)
1293 {
1294 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1295 NotCapable(serv_p, chcap_combos[n].cap_no))
1296 {
1297 /* Hopefully capabs can't change dynamically or anything... */
1298 s_assert(chcap_combos[n].count > 0);
1299
1300 if(chcap_combos[n].count > 0)
1301 chcap_combos[n].count--;
1302 return;
1303 }
1304 }
1305
1306 /* This should be impossible -A1kmm. */
1307 s_assert(0);
1308 }
1309
1310 /* void send_cap_mode_changes(struct Client *client_p,
1311 * struct Client *source_p,
1312 * struct Channel *chptr, int cap, int nocap)
1313 * Input: The client sending(client_p), the source client(source_p),
1314 * the channel to send mode changes for(chptr)
1315 * Output: None.
1316 * Side-effects: Sends the appropriate mode changes to capable servers.
1317 *
1318 * Reverted back to my original design, except that we now keep a count
1319 * of the number of servers which each combination as an optimisation, so
1320 * the capabs combinations which are not needed are not worked out. -A1kmm
1321 */
1322 void
1323 send_cap_mode_changes(struct Client *client_p, struct Client *source_p,
1324 struct Channel *chptr, struct ChModeChange mode_changes[], int mode_count)
1325 {
1326 static char modebuf[BUFSIZE];
1327 static char parabuf[BUFSIZE];
1328 int i, mbl, pbl, nc, mc, preflen, len;
1329 char *pbuf;
1330 const char *arg;
1331 int dir;
1332 int j;
1333 int cap;
1334 int nocap;
1335 int arglen;
1336
1337 /* Now send to servers... */
1338 for (j = 0; j < NCHCAP_COMBOS; j++)
1339 {
1340 if(chcap_combos[j].count == 0)
1341 continue;
1342
1343 mc = 0;
1344 nc = 0;
1345 pbl = 0;
1346 parabuf[0] = 0;
1347 pbuf = parabuf;
1348 dir = MODE_QUERY;
1349
1350 cap = chcap_combos[j].cap_yes;
1351 nocap = chcap_combos[j].cap_no;
1352
1353 mbl = preflen = rb_sprintf(modebuf, ":%s TMODE %ld %s ",
1354 use_id(source_p), (long) chptr->channelts,
1355 chptr->chname);
1356
1357 /* loop the list of - modes we have */
1358 for (i = 0; i < mode_count; i++)
1359 {
1360 /* if they dont support the cap we need, or they do support a cap they
1361 * cant have, then dont add it to the modebuf.. that way they wont see
1362 * the mode
1363 */
1364 if((mode_changes[i].letter == 0) ||
1365 ((cap & mode_changes[i].caps) != mode_changes[i].caps)
1366 || ((nocap & mode_changes[i].nocaps) != mode_changes[i].nocaps))
1367 continue;
1368
1369 if(!EmptyString(mode_changes[i].id))
1370 arg = mode_changes[i].id;
1371 else
1372 arg = mode_changes[i].arg;
1373
1374 if(arg)
1375 {
1376 arglen = strlen(arg);
1377
1378 /* dont even think about it! --fl */
1379 if(arglen > MODEBUFLEN - 5)
1380 continue;
1381 }
1382
1383 /* if we're creeping past the buf size, we need to send it and make
1384 * another line for the other modes
1385 * XXX - this could give away server topology with uids being
1386 * different lengths, but not much we can do, except possibly break
1387 * them as if they were the longest of the nick or uid at all times,
1388 * which even then won't work as we don't always know the uid -A1kmm.
1389 */
1390 if(arg && ((mc == MAXMODEPARAMSSERV) ||
1391 ((mbl + pbl + arglen + 4) > (BUFSIZE - 3))))
1392 {
1393 if(nc != 0)
1394 sendto_server(client_p, chptr, cap, nocap,
1395 "%s %s", modebuf, parabuf);
1396 nc = 0;
1397 mc = 0;
1398
1399 mbl = preflen;
1400 pbl = 0;
1401 pbuf = parabuf;
1402 parabuf[0] = 0;
1403 dir = MODE_QUERY;
1404 }
1405
1406 if(dir != mode_changes[i].dir)
1407 {
1408 modebuf[mbl++] = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
1409 dir = mode_changes[i].dir;
1410 }
1411
1412 modebuf[mbl++] = mode_changes[i].letter;
1413 modebuf[mbl] = 0;
1414 nc++;
1415
1416 if(arg != NULL)
1417 {
1418 len = rb_sprintf(pbuf, "%s ", arg);
1419 pbuf += len;
1420 pbl += len;
1421 mc++;
1422 }
1423 }
1424
1425 if(pbl && parabuf[pbl - 1] == ' ')
1426 parabuf[pbl - 1] = 0;
1427
1428 if(nc != 0)
1429 sendto_server(client_p, chptr, cap, nocap, "%s %s", modebuf, parabuf);
1430 }
1431 }
1432
1433 void
1434 resv_chan_forcepart(const char *name, const char *reason, int temp_time)
1435 {
1436 rb_dlink_node *ptr;
1437 rb_dlink_node *next_ptr;
1438 struct Channel *chptr;
1439 struct membership *msptr;
1440 struct Client *target_p;
1441
1442 if(!ConfigChannel.resv_forcepart)
1443 return;
1444
1445 /* for each user on our server in the channel list
1446 * send them a PART, and notify opers.
1447 */
1448 chptr = find_channel(name);
1449 if(chptr != NULL)
1450 {
1451 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
1452 {
1453 msptr = ptr->data;
1454 target_p = msptr->client_p;
1455
1456 if(IsExemptResv(target_p))
1457 continue;
1458
1459 sendto_server(target_p, chptr, CAP_TS6, NOCAPS,
1460 ":%s PART %s", target_p->id, chptr->chname);
1461
1462 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s :%s",
1463 target_p->name, target_p->username,
1464 target_p->host, chptr->chname, target_p->name);
1465
1466 remove_user_from_channel(msptr);
1467
1468 /* notify opers & user they were removed from the channel */
1469 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1470 "Forced PART for %s!%s@%s from %s (%s)",
1471 target_p->name, target_p->username,
1472 target_p->host, name, reason);
1473
1474 if(temp_time > 0)
1475 sendto_one_notice(target_p, ":*** Channel %s is temporarily unavailable on this server.",
1476 name);
1477 else
1478 sendto_one_notice(target_p, ":*** Channel %s is no longer available on this server.",
1479 name);
1480 }
1481 }
1482 }