]> jfr.im git - solanum.git/blob - src/channel.c
308bd32a79356987d2cab15a8eaaa352bb2d2477
[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 return moduledata.approved;
855 }
856 }
857
858 if(chptr->mode.mode & MODE_MODERATED)
859 moduledata.approved = CAN_SEND_NO;
860
861 if(MyClient(source_p))
862 {
863 /* cached can_send */
864 if(msptr->bants == chptr->bants)
865 {
866 if(can_send_banned(msptr))
867 moduledata.approved = CAN_SEND_NO;
868 }
869 else if(is_banned(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN
870 || is_quieted(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN)
871 moduledata.approved = CAN_SEND_NO;
872 }
873
874 if(is_chanop_voiced(msptr))
875 moduledata.approved = CAN_SEND_OPV;
876
877 call_hook(h_can_send, &moduledata);
878
879 return moduledata.approved;
880 }
881
882 /*
883 * flood_attack_channel
884 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
885 * says NOTICE must not auto reply
886 * - pointer to source Client
887 * - pointer to target channel
888 * output - 1 if target is under flood attack
889 * side effects - check for flood attack on target chptr
890 */
891 int
892 flood_attack_channel(int p_or_n, struct Client *source_p, struct Channel *chptr, char *chname)
893 {
894 int delta;
895
896 if(GlobalSetOptions.floodcount && MyClient(source_p))
897 {
898 if((chptr->first_received_message_time + 1) < rb_current_time())
899 {
900 delta = rb_current_time() - chptr->first_received_message_time;
901 chptr->received_number_of_privmsgs -= delta;
902 chptr->first_received_message_time = rb_current_time();
903 if(chptr->received_number_of_privmsgs <= 0)
904 {
905 chptr->received_number_of_privmsgs = 0;
906 chptr->flood_noticed = 0;
907 }
908 }
909
910 if((chptr->received_number_of_privmsgs >= GlobalSetOptions.floodcount)
911 || chptr->flood_noticed)
912 {
913 if(chptr->flood_noticed == 0)
914 {
915 sendto_realops_snomask(SNO_BOTS, *chptr->chname == '&' ? L_ALL : L_NETWIDE,
916 "Possible Flooder %s[%s@%s] on %s target: %s",
917 source_p->name, source_p->username,
918 source_p->orighost,
919 source_p->servptr->name, chptr->chname);
920 chptr->flood_noticed = 1;
921
922 /* Add a bit of penalty */
923 chptr->received_number_of_privmsgs += 2;
924 }
925 if(MyClient(source_p) && (p_or_n != 1))
926 sendto_one(source_p,
927 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
928 me.name, source_p->name, chptr->chname);
929 return 1;
930 }
931 else
932 chptr->received_number_of_privmsgs++;
933 }
934
935 return 0;
936 }
937
938 /* find_bannickchange_channel()
939 * Input: client to check
940 * Output: channel preventing nick change
941 */
942 struct Channel *
943 find_bannickchange_channel(struct Client *client_p)
944 {
945 struct Channel *chptr;
946 struct membership *msptr;
947 rb_dlink_node *ptr;
948 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
949 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
950
951 if (!MyClient(client_p))
952 return NULL;
953
954 rb_sprintf(src_host, "%s!%s@%s", client_p->name, client_p->username, client_p->host);
955 rb_sprintf(src_iphost, "%s!%s@%s", client_p->name, client_p->username, client_p->sockhost);
956
957 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
958 {
959 msptr = ptr->data;
960 chptr = msptr->chptr;
961 if (is_chanop_voiced(msptr))
962 continue;
963 /* cached can_send */
964 if (msptr->bants == chptr->bants)
965 {
966 if (can_send_banned(msptr))
967 return chptr;
968 }
969 else if (is_banned(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN
970 || is_quieted(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN)
971 return chptr;
972 }
973 return NULL;
974 }
975
976 /* void check_spambot_warning(struct Client *source_p)
977 * Input: Client to check, channel name or NULL if this is a part.
978 * Output: none
979 * Side-effects: Updates the client's oper_warn_count_down, warns the
980 * IRC operators if necessary, and updates join_leave_countdown as
981 * needed.
982 */
983 void
984 check_spambot_warning(struct Client *source_p, const char *name)
985 {
986 int t_delta;
987 int decrement_count;
988 if((GlobalSetOptions.spam_num &&
989 (source_p->localClient->join_leave_count >= GlobalSetOptions.spam_num)))
990 {
991 if(source_p->localClient->oper_warn_count_down > 0)
992 source_p->localClient->oper_warn_count_down--;
993 else
994 source_p->localClient->oper_warn_count_down = 0;
995 if(source_p->localClient->oper_warn_count_down == 0 &&
996 name != NULL)
997 {
998 /* Its already known as a possible spambot */
999 sendto_realops_snomask(SNO_BOTS, L_NETWIDE,
1000 "User %s (%s@%s) trying to join %s is a possible spambot",
1001 source_p->name,
1002 source_p->username, source_p->orighost, name);
1003 source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
1004 }
1005 }
1006 else
1007 {
1008 if((t_delta =
1009 (rb_current_time() - source_p->localClient->last_leave_time)) >
1010 JOIN_LEAVE_COUNT_EXPIRE_TIME)
1011 {
1012 decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
1013 if(name != NULL)
1014 ;
1015 else if(decrement_count > source_p->localClient->join_leave_count)
1016 source_p->localClient->join_leave_count = 0;
1017 else
1018 source_p->localClient->join_leave_count -= decrement_count;
1019 }
1020 else
1021 {
1022 if((rb_current_time() -
1023 (source_p->localClient->last_join_time)) < GlobalSetOptions.spam_time)
1024 {
1025 /* oh, its a possible spambot */
1026 source_p->localClient->join_leave_count++;
1027 }
1028 }
1029 if(name != NULL)
1030 source_p->localClient->last_join_time = rb_current_time();
1031 else
1032 source_p->localClient->last_leave_time = rb_current_time();
1033 }
1034 }
1035
1036 /* check_splitmode()
1037 *
1038 * input -
1039 * output -
1040 * side effects - compares usercount and servercount against their split
1041 * values and adjusts splitmode accordingly
1042 */
1043 void
1044 check_splitmode(void *unused)
1045 {
1046 if(splitchecking && (ConfigChannel.no_join_on_split || ConfigChannel.no_create_on_split))
1047 {
1048 /* not split, we're being asked to check now because someone
1049 * has left
1050 */
1051 if(!splitmode)
1052 {
1053 if(eob_count < split_servers || Count.total < split_users)
1054 {
1055 splitmode = 1;
1056 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1057 "Network split, activating splitmode");
1058 check_splitmode_ev = rb_event_addish("check_splitmode", check_splitmode, NULL, 2);
1059 }
1060 }
1061 /* in splitmode, check whether its finished */
1062 else if(eob_count >= split_servers && Count.total >= split_users)
1063 {
1064 splitmode = 0;
1065
1066 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1067 "Network rejoined, deactivating splitmode");
1068
1069 rb_event_delete(check_splitmode_ev);
1070 check_splitmode_ev = NULL;
1071 }
1072 }
1073 }
1074
1075
1076 /* allocate_topic()
1077 *
1078 * input - channel to allocate topic for
1079 * output - 1 on success, else 0
1080 * side effects - channel gets a topic allocated
1081 */
1082 static void
1083 allocate_topic(struct Channel *chptr)
1084 {
1085 void *ptr;
1086
1087 if(chptr == NULL)
1088 return;
1089
1090 ptr = rb_bh_alloc(topic_heap);
1091
1092 /* Basically we allocate one large block for the topic and
1093 * the topic info. We then split it up into two and shove it
1094 * in the chptr
1095 */
1096 chptr->topic = ptr;
1097 chptr->topic_info = (char *) ptr + TOPICLEN + 1;
1098 *chptr->topic = '\0';
1099 *chptr->topic_info = '\0';
1100 }
1101
1102 /* free_topic()
1103 *
1104 * input - channel which has topic to free
1105 * output -
1106 * side effects - channels topic is free'd
1107 */
1108 static void
1109 free_topic(struct Channel *chptr)
1110 {
1111 void *ptr;
1112
1113 if(chptr == NULL || chptr->topic == NULL)
1114 return;
1115
1116 /* This is safe for now - If you change allocate_topic you
1117 * MUST change this as well
1118 */
1119 ptr = chptr->topic;
1120 rb_bh_free(topic_heap, ptr);
1121 chptr->topic = NULL;
1122 chptr->topic_info = NULL;
1123 }
1124
1125 /* set_channel_topic()
1126 *
1127 * input - channel, topic to set, topic info and topic ts
1128 * output -
1129 * side effects - channels topic, topic info and TS are set.
1130 */
1131 void
1132 set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_info, time_t topicts)
1133 {
1134 if(strlen(topic) > 0)
1135 {
1136 if(chptr->topic == NULL)
1137 allocate_topic(chptr);
1138 rb_strlcpy(chptr->topic, topic, TOPICLEN + 1);
1139 rb_strlcpy(chptr->topic_info, topic_info, USERHOST_REPLYLEN);
1140 chptr->topic_time = topicts;
1141 }
1142 else
1143 {
1144 if(chptr->topic != NULL)
1145 free_topic(chptr);
1146 chptr->topic_time = 0;
1147 }
1148 }
1149
1150 /* channel_modes()
1151 *
1152 * inputs - pointer to channel
1153 * - pointer to client
1154 * output - string with simple modes
1155 * side effects - result from previous calls overwritten
1156 *
1157 * Stolen from ShadowIRCd 4 --nenolod
1158 */
1159 const char *
1160 channel_modes(struct Channel *chptr, struct Client *client_p)
1161 {
1162 int i;
1163 char buf1[BUFSIZE];
1164 char buf2[BUFSIZE];
1165 static char final[BUFSIZE];
1166 char *mbuf = buf1;
1167 char *pbuf = buf2;
1168
1169 *mbuf++ = '+';
1170 *pbuf = '\0';
1171
1172 for (i = 0; i < 256; i++)
1173 if(chptr->mode.mode & chmode_flags[i])
1174 *mbuf++ = i;
1175
1176 if(chptr->mode.limit)
1177 {
1178 *mbuf++ = 'l';
1179
1180 if(!IsClient(client_p) || IsMember(client_p, chptr))
1181 pbuf += rb_sprintf(pbuf, " %d", chptr->mode.limit);
1182 }
1183
1184 if(*chptr->mode.key)
1185 {
1186 *mbuf++ = 'k';
1187
1188 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1189 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.key);
1190 }
1191
1192 if(chptr->mode.join_num)
1193 {
1194 *mbuf++ = 'j';
1195
1196 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1197 pbuf += rb_sprintf(pbuf, " %d:%d", chptr->mode.join_num,
1198 chptr->mode.join_time);
1199 }
1200
1201 if(*chptr->mode.forward && (ConfigChannel.use_forward || !IsClient(client_p)))
1202 {
1203 *mbuf++ = 'f';
1204
1205 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1206 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.forward);
1207 }
1208
1209 *mbuf = '\0';
1210
1211 rb_strlcpy(final, buf1, sizeof final);
1212 rb_strlcat(final, buf2, sizeof final);
1213 return final;
1214 }
1215
1216 /* Now lets do some stuff to keep track of what combinations of
1217 * servers exist...
1218 * Note that the number of combinations doubles each time you add
1219 * something to this list. Each one is only quick if no servers use that
1220 * combination, but if the numbers get too high here MODE will get too
1221 * slow. I suggest if you get more than 7 here, you consider getting rid
1222 * of some and merging or something. If it wasn't for irc+cs we would
1223 * probably not even need to bother about most of these, but unfortunately
1224 * we do. -A1kmm
1225 */
1226
1227 /* void init_chcap_usage_counts(void)
1228 *
1229 * Inputs - none
1230 * Output - none
1231 * Side-effects - Initialises the usage counts to zero. Fills in the
1232 * chcap_yes and chcap_no combination tables.
1233 */
1234 void
1235 init_chcap_usage_counts(void)
1236 {
1237 unsigned long m, c, y, n;
1238
1239 memset(chcap_combos, 0, sizeof(chcap_combos));
1240
1241 /* For every possible combination */
1242 for (m = 0; m < NCHCAP_COMBOS; m++)
1243 {
1244 /* Check each capab */
1245 for (c = y = n = 0; c < NCHCAPS; c++)
1246 {
1247 if((m & (1 << c)) == 0)
1248 n |= channel_capabs[c];
1249 else
1250 y |= channel_capabs[c];
1251 }
1252 chcap_combos[m].cap_yes = y;
1253 chcap_combos[m].cap_no = n;
1254 }
1255 }
1256
1257 /* void set_chcap_usage_counts(struct Client *serv_p)
1258 * Input: serv_p; The client whose capabs to register.
1259 * Output: none
1260 * Side-effects: Increments the usage counts for the correct capab
1261 * combination.
1262 */
1263 void
1264 set_chcap_usage_counts(struct Client *serv_p)
1265 {
1266 int n;
1267
1268 for (n = 0; n < NCHCAP_COMBOS; n++)
1269 {
1270 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1271 NotCapable(serv_p, chcap_combos[n].cap_no))
1272 {
1273 chcap_combos[n].count++;
1274 return;
1275 }
1276 }
1277
1278 /* This should be impossible -A1kmm. */
1279 s_assert(0);
1280 }
1281
1282 /* void set_chcap_usage_counts(struct Client *serv_p)
1283 *
1284 * Inputs - serv_p; The client whose capabs to register.
1285 * Output - none
1286 * Side-effects - Decrements the usage counts for the correct capab
1287 * combination.
1288 */
1289 void
1290 unset_chcap_usage_counts(struct Client *serv_p)
1291 {
1292 int n;
1293
1294 for (n = 0; n < NCHCAP_COMBOS; n++)
1295 {
1296 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1297 NotCapable(serv_p, chcap_combos[n].cap_no))
1298 {
1299 /* Hopefully capabs can't change dynamically or anything... */
1300 s_assert(chcap_combos[n].count > 0);
1301
1302 if(chcap_combos[n].count > 0)
1303 chcap_combos[n].count--;
1304 return;
1305 }
1306 }
1307
1308 /* This should be impossible -A1kmm. */
1309 s_assert(0);
1310 }
1311
1312 /* void send_cap_mode_changes(struct Client *client_p,
1313 * struct Client *source_p,
1314 * struct Channel *chptr, int cap, int nocap)
1315 * Input: The client sending(client_p), the source client(source_p),
1316 * the channel to send mode changes for(chptr)
1317 * Output: None.
1318 * Side-effects: Sends the appropriate mode changes to capable servers.
1319 *
1320 * Reverted back to my original design, except that we now keep a count
1321 * of the number of servers which each combination as an optimisation, so
1322 * the capabs combinations which are not needed are not worked out. -A1kmm
1323 */
1324 void
1325 send_cap_mode_changes(struct Client *client_p, struct Client *source_p,
1326 struct Channel *chptr, struct ChModeChange mode_changes[], int mode_count)
1327 {
1328 static char modebuf[BUFSIZE];
1329 static char parabuf[BUFSIZE];
1330 int i, mbl, pbl, nc, mc, preflen, len;
1331 char *pbuf;
1332 const char *arg;
1333 int dir;
1334 int j;
1335 int cap;
1336 int nocap;
1337 int arglen;
1338
1339 /* Now send to servers... */
1340 for (j = 0; j < NCHCAP_COMBOS; j++)
1341 {
1342 if(chcap_combos[j].count == 0)
1343 continue;
1344
1345 mc = 0;
1346 nc = 0;
1347 pbl = 0;
1348 parabuf[0] = 0;
1349 pbuf = parabuf;
1350 dir = MODE_QUERY;
1351
1352 cap = chcap_combos[j].cap_yes;
1353 nocap = chcap_combos[j].cap_no;
1354
1355 mbl = preflen = rb_sprintf(modebuf, ":%s TMODE %ld %s ",
1356 use_id(source_p), (long) chptr->channelts,
1357 chptr->chname);
1358
1359 /* loop the list of - modes we have */
1360 for (i = 0; i < mode_count; i++)
1361 {
1362 /* if they dont support the cap we need, or they do support a cap they
1363 * cant have, then dont add it to the modebuf.. that way they wont see
1364 * the mode
1365 */
1366 if((mode_changes[i].letter == 0) ||
1367 ((cap & mode_changes[i].caps) != mode_changes[i].caps)
1368 || ((nocap & mode_changes[i].nocaps) != mode_changes[i].nocaps))
1369 continue;
1370
1371 if(!EmptyString(mode_changes[i].id))
1372 arg = mode_changes[i].id;
1373 else
1374 arg = mode_changes[i].arg;
1375
1376 if(arg)
1377 {
1378 arglen = strlen(arg);
1379
1380 /* dont even think about it! --fl */
1381 if(arglen > MODEBUFLEN - 5)
1382 continue;
1383 }
1384
1385 /* if we're creeping past the buf size, we need to send it and make
1386 * another line for the other modes
1387 * XXX - this could give away server topology with uids being
1388 * different lengths, but not much we can do, except possibly break
1389 * them as if they were the longest of the nick or uid at all times,
1390 * which even then won't work as we don't always know the uid -A1kmm.
1391 */
1392 if(arg && ((mc == MAXMODEPARAMSSERV) ||
1393 ((mbl + pbl + arglen + 4) > (BUFSIZE - 3))))
1394 {
1395 if(nc != 0)
1396 sendto_server(client_p, chptr, cap, nocap,
1397 "%s %s", modebuf, parabuf);
1398 nc = 0;
1399 mc = 0;
1400
1401 mbl = preflen;
1402 pbl = 0;
1403 pbuf = parabuf;
1404 parabuf[0] = 0;
1405 dir = MODE_QUERY;
1406 }
1407
1408 if(dir != mode_changes[i].dir)
1409 {
1410 modebuf[mbl++] = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
1411 dir = mode_changes[i].dir;
1412 }
1413
1414 modebuf[mbl++] = mode_changes[i].letter;
1415 modebuf[mbl] = 0;
1416 nc++;
1417
1418 if(arg != NULL)
1419 {
1420 len = rb_sprintf(pbuf, "%s ", arg);
1421 pbuf += len;
1422 pbl += len;
1423 mc++;
1424 }
1425 }
1426
1427 if(pbl && parabuf[pbl - 1] == ' ')
1428 parabuf[pbl - 1] = 0;
1429
1430 if(nc != 0)
1431 sendto_server(client_p, chptr, cap, nocap, "%s %s", modebuf, parabuf);
1432 }
1433 }
1434
1435 void
1436 resv_chan_forcepart(const char *name, const char *reason, int temp_time)
1437 {
1438 rb_dlink_node *ptr;
1439 rb_dlink_node *next_ptr;
1440 struct Channel *chptr;
1441 struct membership *msptr;
1442 struct Client *target_p;
1443
1444 if(!ConfigChannel.resv_forcepart)
1445 return;
1446
1447 /* for each user on our server in the channel list
1448 * send them a PART, and notify opers.
1449 */
1450 chptr = find_channel(name);
1451 if(chptr != NULL)
1452 {
1453 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
1454 {
1455 msptr = ptr->data;
1456 target_p = msptr->client_p;
1457
1458 if(IsExemptResv(target_p))
1459 continue;
1460
1461 sendto_server(target_p, chptr, CAP_TS6, NOCAPS,
1462 ":%s PART %s", target_p->id, chptr->chname);
1463
1464 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s :%s",
1465 target_p->name, target_p->username,
1466 target_p->host, chptr->chname, target_p->name);
1467
1468 remove_user_from_channel(msptr);
1469
1470 /* notify opers & user they were removed from the channel */
1471 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1472 "Forced PART for %s!%s@%s from %s (%s)",
1473 target_p->name, target_p->username,
1474 target_p->host, name, reason);
1475
1476 if(temp_time > 0)
1477 sendto_one_notice(target_p, ":*** Channel %s is temporarily unavailable on this server.",
1478 name);
1479 else
1480 sendto_one_notice(target_p, ":*** Channel %s is no longer available on this server.",
1481 name);
1482 }
1483 }
1484 }