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