]> jfr.im git - irc/rqf/shadowircd.git/blob - src/channel.c
Move flood_attack_channel to channel.c so it can be used outside m_message.c
[irc/rqf/shadowircd.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 */
25
26 #include "stdinc.h"
27 #include "channel.h"
28 #include "chmode.h"
29 #include "client.h"
30 #include "common.h"
31 #include "hash.h"
32 #include "hook.h"
33 #include "match.h"
34 #include "ircd.h"
35 #include "numeric.h"
36 #include "s_serv.h" /* captab */
37 #include "s_user.h"
38 #include "send.h"
39 #include "whowas.h"
40 #include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
41 #include "s_newconf.h"
42 #include "logger.h"
43 #include "packet.h"
44 #include "irc_dictionary.h"
45
46 struct config_channel_entry ConfigChannel;
47 rb_dlink_list global_channel_list;
48 static rb_bh *channel_heap;
49 static rb_bh *ban_heap;
50 static rb_bh *topic_heap;
51 static rb_bh *member_heap;
52
53 static int channel_capabs[] = { CAP_EX, CAP_IE,
54 CAP_SERVICE,
55 CAP_TS6
56 };
57
58 #define NCHCAPS (sizeof(channel_capabs)/sizeof(int))
59 #define NCHCAP_COMBOS (1 << NCHCAPS)
60
61 static struct ChCapCombo chcap_combos[NCHCAP_COMBOS];
62
63 static void free_topic(struct Channel *chptr);
64
65 static int h_can_join;
66 static int h_can_create_channel;
67 static int h_channel_join;
68
69 /* init_channels()
70 *
71 * input -
72 * output -
73 * side effects - initialises the various blockheaps
74 */
75 void
76 init_channels(void)
77 {
78 channel_heap = rb_bh_create(sizeof(struct Channel), CHANNEL_HEAP_SIZE, "channel_heap");
79 ban_heap = rb_bh_create(sizeof(struct Ban), BAN_HEAP_SIZE, "ban_heap");
80 topic_heap = rb_bh_create(TOPICLEN + 1 + USERHOST_REPLYLEN, TOPIC_HEAP_SIZE, "topic_heap");
81 member_heap = rb_bh_create(sizeof(struct membership), MEMBER_HEAP_SIZE, "member_heap");
82
83 h_can_join = register_hook("can_join");
84 h_channel_join = register_hook("channel_join");
85 h_can_create_channel = register_hook("can_create_channel");
86 }
87
88 /*
89 * allocate_channel - Allocates a channel
90 */
91 struct Channel *
92 allocate_channel(const char *chname)
93 {
94 struct Channel *chptr;
95 struct Dictionary *metadata;
96 chptr = rb_bh_alloc(channel_heap);
97 chptr->chname = rb_strdup(chname);
98
99 metadata = irc_dictionary_create(irccmp);
100 chptr->metadata = metadata;
101 return (chptr);
102 }
103
104 void
105 free_channel(struct Channel *chptr)
106 {
107 channel_metadata_clear(chptr);
108 rb_free(chptr->chname);
109 rb_free(chptr->mode_lock);
110 rb_bh_free(channel_heap, chptr);
111 }
112
113 struct Ban *
114 allocate_ban(const char *banstr, const char *who)
115 {
116 struct Ban *bptr;
117 bptr = rb_bh_alloc(ban_heap);
118 bptr->banstr = rb_strdup(banstr);
119 bptr->who = rb_strdup(who);
120
121 return (bptr);
122 }
123
124 void
125 free_ban(struct Ban *bptr)
126 {
127 rb_free(bptr->banstr);
128 rb_free(bptr->who);
129 rb_bh_free(ban_heap, bptr);
130 }
131
132
133 /* find_channel_membership()
134 *
135 * input - channel to find them in, client to find
136 * output - membership of client in channel, else NULL
137 * side effects -
138 */
139 struct membership *
140 find_channel_membership(struct Channel *chptr, struct Client *client_p)
141 {
142 struct membership *msptr;
143 rb_dlink_node *ptr;
144
145 if(!IsClient(client_p))
146 return NULL;
147
148 /* Pick the most efficient list to use to be nice to things like
149 * CHANSERV which could be in a large number of channels
150 */
151 if(rb_dlink_list_length(&chptr->members) < rb_dlink_list_length(&client_p->user->channel))
152 {
153 RB_DLINK_FOREACH(ptr, chptr->members.head)
154 {
155 msptr = ptr->data;
156
157 if(msptr->client_p == client_p)
158 return msptr;
159 }
160 }
161 else
162 {
163 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
164 {
165 msptr = ptr->data;
166
167 if(msptr->chptr == chptr)
168 return msptr;
169 }
170 }
171
172 return NULL;
173 }
174
175 /* find_channel_status()
176 *
177 * input - membership to get status for, whether we can combine flags
178 * output - flags of user on channel
179 * side effects -
180 */
181 const char *
182 find_channel_status(struct membership *msptr, int combine)
183 {
184 static char buffer[5];
185 char *p;
186
187 p = buffer;
188
189 if(is_admin(msptr))
190 {
191 if(!combine)
192 return "!";
193 *p++ = '!';
194 }
195
196 if(is_chanop(msptr))
197 {
198 if(!combine)
199 return "@";
200 *p++ = '@';
201 }
202
203 if(is_halfop(msptr))
204 {
205 if(!combine)
206 return "%";
207 *p++ = '%';
208 }
209
210 if(is_voiced(msptr))
211 *p++ = '+';
212
213 *p = '\0';
214 return buffer;
215 }
216
217 /* is_halfop()
218 *
219 * input - membership to check for halfops
220 * output - 1 if the user is halfopped, 0 if the user is not or halfop
221 * is disabled.
222 * side effects -
223 *
224 */
225 int
226 is_halfop(struct membership *msptr)
227 {
228 if(!ConfigChannel.use_halfop)
229 return 0;
230 if(is_chmode_h(msptr))
231 return 1;
232 else
233 return 0;
234 }
235
236 /* is_admin()
237 *
238 * input - membership to check for admin
239 * output - 1 if the user is an admin, 0 if the user is not or admin
240 * is disabled.
241 * side effects -
242 *
243 */
244 int
245 is_admin(struct membership *msptr)
246 {
247 if(!ConfigChannel.use_admin)
248 return 0;
249 if(is_chmode_a(msptr))
250 return 1;
251 else
252 return 0;
253 }
254
255 /* is_any_op()
256 *
257 * input - membership to check for ops
258 * output - 1 if the user is op, halfop, or admin, 0 elsewise
259 * side effects -
260 */
261 int
262 is_any_op(struct membership *msptr)
263 {
264 if(is_chanop(msptr) || is_halfop(msptr) || is_admin(msptr))
265 return 1;
266 else
267 return 0;
268 }
269
270 /* is_chanop_voiced()
271 *
272 * input - memebership to check for status
273 * output - 1 if the user is op, halfop, admin, or voice, 0 elsewise
274 * side effects -
275 */
276 int
277 is_chanop_voiced(struct membership *msptr)
278 {
279 if(is_chanop(msptr) || is_voiced(msptr) || is_halfop(msptr) || is_admin(msptr))
280 return 1;
281 else
282 return 0;
283 }
284
285 /* can_kick_deop()
286 *
287 * input - two memeberships
288 * output - 1 if the first memebership can kick/deop the second, 0 elsewise
289 * side effects -
290 */
291 int
292 can_kick_deop(struct membership *source, struct membership *target)
293 {
294 if(is_chanop(source) && !is_admin(target))
295 return 1;
296 else if(is_halfop(source) && !is_any_op(target))
297 return 1;
298 else if(is_admin(source))
299 return 1;
300
301 return 0;
302 }
303
304 /* add_user_to_channel()
305 *
306 * input - channel to add client to, client to add, channel flags
307 * output -
308 * side effects - user is added to channel
309 */
310 void
311 add_user_to_channel(struct Channel *chptr, struct Client *client_p, int flags)
312 {
313 struct membership *msptr;
314
315 s_assert(client_p->user != NULL);
316 if(client_p->user == NULL)
317 return;
318
319 msptr = rb_bh_alloc(member_heap);
320
321 msptr->chptr = chptr;
322 msptr->client_p = client_p;
323 msptr->flags = flags;
324
325 rb_dlinkAdd(msptr, &msptr->usernode, &client_p->user->channel);
326 rb_dlinkAdd(msptr, &msptr->channode, &chptr->members);
327
328 if(MyClient(client_p))
329 rb_dlinkAdd(msptr, &msptr->locchannode, &chptr->locmembers);
330 }
331
332 /* remove_user_from_channel()
333 *
334 * input - membership pointer to remove from channel
335 * output -
336 * side effects - membership (thus user) is removed from channel
337 */
338 void
339 remove_user_from_channel(struct membership *msptr)
340 {
341 struct Client *client_p;
342 struct Channel *chptr;
343 s_assert(msptr != NULL);
344 if(msptr == NULL)
345 return;
346
347 client_p = msptr->client_p;
348 chptr = msptr->chptr;
349
350 rb_dlinkDelete(&msptr->usernode, &client_p->user->channel);
351 rb_dlinkDelete(&msptr->channode, &chptr->members);
352
353 if(client_p->servptr == &me)
354 rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers);
355
356 if(!(chptr->mode.mode & MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0)
357 destroy_channel(chptr);
358
359 rb_bh_free(member_heap, msptr);
360
361 return;
362 }
363
364 /* remove_user_from_channels()
365 *
366 * input - user to remove from all channels
367 * output -
368 * side effects - user is removed from all channels
369 */
370 void
371 remove_user_from_channels(struct Client *client_p)
372 {
373 struct Channel *chptr;
374 struct membership *msptr;
375 rb_dlink_node *ptr;
376 rb_dlink_node *next_ptr;
377
378 if(client_p == NULL)
379 return;
380
381 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->user->channel.head)
382 {
383 msptr = ptr->data;
384 chptr = msptr->chptr;
385
386 rb_dlinkDelete(&msptr->channode, &chptr->members);
387
388 if(client_p->servptr == &me)
389 rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers);
390
391 if(!(chptr->mode.mode & MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0)
392 destroy_channel(chptr);
393
394 rb_bh_free(member_heap, msptr);
395 }
396
397 client_p->user->channel.head = client_p->user->channel.tail = NULL;
398 client_p->user->channel.length = 0;
399 }
400
401 /* invalidate_bancache_user()
402 *
403 * input - user to invalidate ban cache for
404 * output -
405 * side effects - ban cache is invalidated for all memberships of that user
406 * to be used after a nick change
407 */
408 void
409 invalidate_bancache_user(struct Client *client_p)
410 {
411 struct membership *msptr;
412 rb_dlink_node *ptr;
413
414 if(client_p == NULL)
415 return;
416
417 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
418 {
419 msptr = ptr->data;
420 msptr->bants = 0;
421 msptr->flags &= ~CHFL_BANNED;
422 }
423 }
424
425 /* check_channel_name()
426 *
427 * input - channel name
428 * output - 1 if valid channel name, else 0
429 * side effects -
430 */
431 int
432 check_channel_name(const char *name)
433 {
434 s_assert(name != NULL);
435 if(name == NULL)
436 return 0;
437
438 for (; *name; ++name)
439 {
440 if(!IsChanChar(*name))
441 return 0;
442 }
443
444 return 1;
445 }
446
447 /* free_channel_list()
448 *
449 * input - rb_dlink list to free
450 * output -
451 * side effects - list of b/e/I modes is cleared
452 */
453 void
454 free_channel_list(rb_dlink_list * list)
455 {
456 rb_dlink_node *ptr;
457 rb_dlink_node *next_ptr;
458 struct Ban *actualBan;
459
460 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
461 {
462 actualBan = ptr->data;
463 free_ban(actualBan);
464 }
465
466 list->head = list->tail = NULL;
467 list->length = 0;
468 }
469
470 /* destroy_channel()
471 *
472 * input - channel to destroy
473 * output -
474 * side effects - channel is obliterated
475 */
476 void
477 destroy_channel(struct Channel *chptr)
478 {
479 rb_dlink_node *ptr, *next_ptr;
480
481 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->invites.head)
482 {
483 del_invite(chptr, ptr->data);
484 }
485
486 /* free all bans/exceptions/denies */
487 free_channel_list(&chptr->banlist);
488 free_channel_list(&chptr->exceptlist);
489 free_channel_list(&chptr->invexlist);
490 free_channel_list(&chptr->quietlist);
491
492 /* Free the topic */
493 free_topic(chptr);
494
495 rb_dlinkDelete(&chptr->node, &global_channel_list);
496 del_from_channel_hash(chptr->chname, chptr);
497 free_channel(chptr);
498 }
499
500 /* channel_pub_or_secret()
501 *
502 * input - channel
503 * output - "=" if public, "@" if secret, else "*"
504 * side effects -
505 */
506 static const char *
507 channel_pub_or_secret(struct Channel *chptr)
508 {
509 if(PubChannel(chptr))
510 return ("=");
511 else if(SecretChannel(chptr))
512 return ("@");
513 return ("*");
514 }
515
516 /* channel_member_names()
517 *
518 * input - channel to list, client to list to, show endofnames
519 * output -
520 * side effects - client is given list of users on channel
521 */
522 void
523 channel_member_names(struct Channel *chptr, struct Client *client_p, int show_eon)
524 {
525 struct membership *msptr;
526 struct Client *target_p;
527 rb_dlink_node *ptr;
528 char lbuf[BUFSIZE];
529 char *t;
530 int mlen;
531 int tlen;
532 int cur_len;
533 int is_member;
534 int stack = IsCapable(client_p, CLICAP_MULTI_PREFIX);
535
536 if(ShowChannel(client_p, chptr))
537 {
538 is_member = IsMember(client_p, chptr);
539
540 cur_len = mlen = rb_sprintf(lbuf, form_str(RPL_NAMREPLY),
541 me.name, client_p->name,
542 channel_pub_or_secret(chptr), chptr->chname);
543
544 t = lbuf + cur_len;
545
546 RB_DLINK_FOREACH(ptr, chptr->members.head)
547 {
548 msptr = ptr->data;
549 target_p = msptr->client_p;
550
551 if(IsInvisible(target_p) && !is_member)
552 continue;
553
554 /* space, possible "@+" prefix */
555 if(cur_len + strlen(target_p->name) + 3 >= BUFSIZE - 3)
556 {
557 *(t - 1) = '\0';
558 sendto_one(client_p, "%s", lbuf);
559 cur_len = mlen;
560 t = lbuf + mlen;
561 }
562
563 tlen = rb_sprintf(t, "%s%s ", find_channel_status(msptr, stack),
564 target_p->name);
565
566 cur_len += tlen;
567 t += tlen;
568 }
569
570 /* The old behaviour here was to always output our buffer,
571 * even if there are no clients we can show. This happens
572 * when a client does "NAMES" with no parameters, and all
573 * the clients on a -sp channel are +i. I dont see a good
574 * reason for keeping that behaviour, as it just wastes
575 * bandwidth. --anfl
576 */
577 if(cur_len != mlen)
578 {
579 *(t - 1) = '\0';
580 sendto_one(client_p, "%s", lbuf);
581 }
582 }
583
584 if(show_eon)
585 sendto_one(client_p, form_str(RPL_ENDOFNAMES),
586 me.name, client_p->name, chptr->chname);
587 }
588
589 /* del_invite()
590 *
591 * input - channel to remove invite from, client to remove
592 * output -
593 * side effects - user is removed from invite list, if exists
594 */
595 void
596 del_invite(struct Channel *chptr, struct Client *who)
597 {
598 rb_dlinkFindDestroy(who, &chptr->invites);
599 rb_dlinkFindDestroy(chptr, &who->user->invited);
600 }
601
602 /* is_banned()
603 *
604 * input - channel to check bans for, user to check bans against
605 * optional prebuilt buffers
606 * output - 1 if banned, else 0
607 * side effects -
608 */
609 int
610 is_banned(struct Channel *chptr, struct Client *who, struct membership *msptr,
611 const char *s, const char *s2)
612 {
613 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
614 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
615 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
616 char *s3 = NULL;
617 rb_dlink_node *ptr;
618 struct Ban *actualBan = NULL;
619 struct Ban *actualExcept = NULL;
620
621 if(!MyClient(who))
622 return 0;
623
624 /* if the buffers havent been built, do it here */
625 if(s == NULL)
626 {
627 rb_sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host);
628 rb_sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost);
629
630 s = src_host;
631 s2 = src_iphost;
632 }
633 if(who->localClient->mangledhost != NULL)
634 {
635 /* if host mangling mode enabled, also check their real host */
636 if(!strcmp(who->host, who->localClient->mangledhost))
637 {
638 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost);
639 s3 = src_althost;
640 }
641 /* if host mangling mode not enabled and no other spoof,
642 * also check the mangled form of their host */
643 else if (!IsDynSpoof(who))
644 {
645 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost);
646 s3 = src_althost;
647 }
648 }
649
650 RB_DLINK_FOREACH(ptr, chptr->banlist.head)
651 {
652 actualBan = ptr->data;
653 if(match(actualBan->banstr, s) ||
654 match(actualBan->banstr, s2) ||
655 match_cidr(actualBan->banstr, s2) ||
656 match_extban(actualBan->banstr, who, chptr, CHFL_BAN) ||
657 (s3 != NULL && match(actualBan->banstr, s3)))
658 break;
659 else
660 actualBan = NULL;
661 }
662
663 if((actualBan != NULL) && ConfigChannel.use_except)
664 {
665 RB_DLINK_FOREACH(ptr, chptr->exceptlist.head)
666 {
667 actualExcept = ptr->data;
668
669 /* theyre exempted.. */
670 if(match(actualExcept->banstr, s) ||
671 match(actualExcept->banstr, s2) ||
672 match_cidr(actualExcept->banstr, s2) ||
673 match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) ||
674 (s3 != NULL && match(actualExcept->banstr, s3)))
675 {
676 /* cache the fact theyre not banned */
677 if(msptr != NULL)
678 {
679 msptr->bants = chptr->bants;
680 msptr->flags &= ~CHFL_BANNED;
681 }
682
683 return CHFL_EXCEPTION;
684 }
685 }
686 }
687
688 /* cache the banned/not banned status */
689 if(msptr != NULL)
690 {
691 msptr->bants = chptr->bants;
692
693 if(actualBan != NULL)
694 {
695 msptr->flags |= CHFL_BANNED;
696 return CHFL_BAN;
697 }
698 else
699 {
700 msptr->flags &= ~CHFL_BANNED;
701 return 0;
702 }
703 }
704
705 return ((actualBan ? CHFL_BAN : 0));
706 }
707
708 /* is_quieted()
709 *
710 * input - channel to check bans for, user to check bans against
711 * optional prebuilt buffers
712 * output - 1 if banned, else 0
713 * side effects -
714 */
715 int
716 is_quieted(struct Channel *chptr, struct Client *who, struct membership *msptr,
717 const char *s, const char *s2)
718 {
719 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
720 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
721 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
722 char *s3 = NULL;
723 rb_dlink_node *ptr;
724 struct Ban *actualBan = NULL;
725 struct Ban *actualExcept = NULL;
726
727 if(!MyClient(who))
728 return 0;
729
730 /* if the buffers havent been built, do it here */
731 if(s == NULL)
732 {
733 rb_sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host);
734 rb_sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost);
735
736 s = src_host;
737 s2 = src_iphost;
738 }
739 if(who->localClient->mangledhost != NULL)
740 {
741 /* if host mangling mode enabled, also check their real host */
742 if(!strcmp(who->host, who->localClient->mangledhost))
743 {
744 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost);
745 s3 = src_althost;
746 }
747 /* if host mangling mode not enabled and no other spoof,
748 * also check the mangled form of their host */
749 else if (!IsDynSpoof(who))
750 {
751 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost);
752 s3 = src_althost;
753 }
754 }
755
756 RB_DLINK_FOREACH(ptr, chptr->quietlist.head)
757 {
758 actualBan = ptr->data;
759 if(match(actualBan->banstr, s) ||
760 match(actualBan->banstr, s2) ||
761 match_cidr(actualBan->banstr, s2) ||
762 match_extban(actualBan->banstr, who, chptr, CHFL_QUIET) ||
763 (s3 != NULL && match(actualBan->banstr, s3)))
764 break;
765 else
766 actualBan = NULL;
767 }
768
769 if((actualBan != NULL) && ConfigChannel.use_except)
770 {
771 RB_DLINK_FOREACH(ptr, chptr->exceptlist.head)
772 {
773 actualExcept = ptr->data;
774
775 /* theyre exempted.. */
776 if(match(actualExcept->banstr, s) ||
777 match(actualExcept->banstr, s2) ||
778 match_cidr(actualExcept->banstr, s2) ||
779 match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) ||
780 (s3 != NULL && match(actualExcept->banstr, s3)))
781 {
782 /* cache the fact theyre not banned */
783 if(msptr != NULL)
784 {
785 msptr->bants = chptr->bants;
786 msptr->flags &= ~CHFL_BANNED;
787 }
788
789 return CHFL_EXCEPTION;
790 }
791 }
792 }
793
794 /* cache the banned/not banned status */
795 if(msptr != NULL)
796 {
797 msptr->bants = chptr->bants;
798
799 if(actualBan != NULL)
800 {
801 msptr->flags |= CHFL_BANNED;
802 return CHFL_BAN;
803 }
804 else
805 {
806 msptr->flags &= ~CHFL_BANNED;
807 return 0;
808 }
809 }
810
811 return ((actualBan ? CHFL_BAN : 0));
812 }
813
814 /* can_join()
815 *
816 * input - client to check, channel to check for, key
817 * output - reason for not being able to join, else 0
818 * side effects -
819 */
820 int
821 can_join(struct Client *source_p, struct Channel *chptr, char *key)
822 {
823 rb_dlink_node *invite = NULL;
824 rb_dlink_node *ptr;
825 struct Ban *invex = NULL;
826 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
827 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
828 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
829 char text[10];
830 int use_althost = 0;
831 int i = 0;
832 hook_data_channel moduledata;
833 struct Metadata *md;
834 struct DictionaryIter iter;
835
836 s_assert(source_p->localClient != NULL);
837
838 rb_sprintf(src_host, "%s!%s@%s", source_p->name, source_p->username, source_p->host);
839 rb_sprintf(src_iphost, "%s!%s@%s", source_p->name, source_p->username, source_p->sockhost);
840 if(source_p->localClient->mangledhost != NULL)
841 {
842 /* if host mangling mode enabled, also check their real host */
843 if(!strcmp(source_p->host, source_p->localClient->mangledhost))
844 {
845 rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->orighost);
846 use_althost = 1;
847 }
848 /* if host mangling mode not enabled and no other spoof,
849 * also check the mangled form of their host */
850 else if (!IsDynSpoof(source_p))
851 {
852 rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->localClient->mangledhost);
853 use_althost = 1;
854 }
855 }
856
857 if((is_banned(chptr, source_p, NULL, src_host, src_iphost)) == CHFL_BAN)
858 return (ERR_BANNEDFROMCHAN);
859
860 rb_snprintf(text, sizeof(text), "K%s", source_p->id);
861
862 DICTIONARY_FOREACH(md, &iter, chptr->metadata)
863 {
864 if(!strcmp(md->value, "KICKNOREJOIN") && !strcmp(md->name, text) && (md->timevalue + 2 > rb_current_time()))
865 return ERR_KICKNOREJOIN;
866 /* cleanup any stale KICKNOREJOIN metadata we find while we're at it */
867 if(!strcmp(md->value, "KICKNOREJOIN") && !(md->timevalue + 2 > rb_current_time()))
868 channel_metadata_delete(chptr, md->name, 0);
869 }
870
871 if(chptr->mode.mode & MODE_INVITEONLY)
872 {
873 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
874 {
875 if(invite->data == chptr)
876 break;
877 }
878 if(invite == NULL)
879 {
880 if(!ConfigChannel.use_invex)
881 return (ERR_INVITEONLYCHAN);
882 RB_DLINK_FOREACH(ptr, chptr->invexlist.head)
883 {
884 invex = ptr->data;
885 if(match(invex->banstr, src_host)
886 || match(invex->banstr, src_iphost)
887 || match_cidr(invex->banstr, src_iphost)
888 || match_extban(invex->banstr, source_p, chptr, CHFL_INVEX)
889 || (use_althost && match(invex->banstr, src_althost)))
890 break;
891 }
892 if(ptr == NULL)
893 return (ERR_INVITEONLYCHAN);
894 }
895 }
896
897 if(*chptr->mode.key && (EmptyString(key) || irccmp(chptr->mode.key, key)))
898 return (ERR_BADCHANNELKEY);
899
900 if(chptr->mode.limit &&
901 rb_dlink_list_length(&chptr->members) >= (unsigned long) chptr->mode.limit)
902 i = ERR_CHANNELISFULL;
903 if(chptr->mode.mode & MODE_REGONLY && EmptyString(source_p->user->suser))
904 i = ERR_NEEDREGGEDNICK;
905 /* join throttling stuff --nenolod */
906 else if(chptr->mode.join_num > 0 && chptr->mode.join_time > 0)
907 {
908 if ((rb_current_time() - chptr->join_delta <=
909 chptr->mode.join_time) && (chptr->join_count >=
910 chptr->mode.join_num))
911 i = ERR_THROTTLE;
912 }
913
914 /* allow /invite to override +l/+r/+j also -- jilles */
915 if (i != 0 && invite == NULL)
916 {
917 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
918 {
919 if(invite->data == chptr)
920 break;
921 }
922 if (invite == NULL)
923 return i;
924 }
925
926 moduledata.client = source_p;
927 moduledata.chptr = chptr;
928 moduledata.approved = 0;
929
930 call_hook(h_can_join, &moduledata);
931
932 return moduledata.approved;
933 }
934
935 /* can_send()
936 *
937 * input - user to check in channel, membership pointer
938 * output - whether can explicitly send or not, else CAN_SEND_NONOP
939 * side effects -
940 */
941 int
942 can_send(struct Channel *chptr, struct Client *source_p, struct membership *msptr)
943 {
944 if(IsServer(source_p) || IsService(source_p))
945 return CAN_SEND_OPV;
946
947 if(MyClient(source_p) && hash_find_resv(chptr->chname) &&
948 !IsOper(source_p) && !IsExemptResv(source_p))
949 return CAN_SEND_NO;
950
951 if(msptr == NULL)
952 {
953 msptr = find_channel_membership(chptr, source_p);
954
955 if(msptr == NULL)
956 {
957 /* if its +m or +n and theyre not in the channel,
958 * they cant send. we dont check bans here because
959 * theres no possibility of caching them --fl
960 */
961 if(chptr->mode.mode & MODE_NOPRIVMSGS || chptr->mode.mode & MODE_MODERATED)
962 return CAN_SEND_NO;
963 else
964 return CAN_SEND_NONOP;
965 }
966 }
967
968 if(is_chanop_voiced(msptr))
969 return CAN_SEND_OPV;
970
971 if(chptr->mode.mode & MODE_MODERATED)
972 return CAN_SEND_NO;
973
974 if(MyClient(source_p))
975 {
976 /* cached can_send */
977 if(msptr->bants == chptr->bants)
978 {
979 if(can_send_banned(msptr))
980 return CAN_SEND_NO;
981 }
982 else if(is_banned(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN
983 || is_quieted(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN)
984 return CAN_SEND_NO;
985 }
986
987 return CAN_SEND_NONOP;
988 }
989
990 /*
991 * flood_attack_channel
992 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
993 * says NOTICE must not auto reply
994 * - pointer to source Client
995 * - pointer to target channel
996 * output - 1 if target is under flood attack
997 * side effects - check for flood attack on target chptr
998 */
999 int
1000 flood_attack_channel(int p_or_n, struct Client *source_p, struct Channel *chptr, char *chname)
1001 {
1002 int delta;
1003
1004 if(GlobalSetOptions.floodcount && MyClient(source_p) && (!IsOper(source_p) || !ConfigFileEntry.true_no_oper_flood))
1005 {
1006 if((chptr->first_received_message_time + 1) < rb_current_time())
1007 {
1008 delta = rb_current_time() - chptr->first_received_message_time;
1009 chptr->received_number_of_privmsgs -= delta;
1010 chptr->first_received_message_time = rb_current_time();
1011 if(chptr->received_number_of_privmsgs <= 0)
1012 {
1013 chptr->received_number_of_privmsgs = 0;
1014 chptr->flood_noticed = 0;
1015 }
1016 }
1017
1018 if((chptr->received_number_of_privmsgs >= GlobalSetOptions.floodcount)
1019 || chptr->flood_noticed)
1020 {
1021 if(chptr->flood_noticed == 0)
1022 {
1023 sendto_realops_snomask(SNO_BOTS, *chptr->chname == '&' ? L_ALL : L_NETWIDE,
1024 "Possible Flooder %s[%s@%s] on %s target: %s",
1025 source_p->name, source_p->username,
1026 source_p->orighost,
1027 source_p->servptr->name, chptr->chname);
1028 chptr->flood_noticed = 1;
1029
1030 /* Add a bit of penalty */
1031 chptr->received_number_of_privmsgs += 2;
1032 }
1033 if(MyClient(source_p) && (p_or_n != 1))
1034 sendto_one(source_p,
1035 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
1036 me.name, source_p->name, chptr->chname);
1037 return 1;
1038 }
1039 else
1040 chptr->received_number_of_privmsgs++;
1041 }
1042
1043 return 0;
1044 }
1045
1046 /* find_bannickchange_channel()
1047 * Input: client to check
1048 * Output: channel preventing nick change
1049 */
1050 struct Channel *
1051 find_bannickchange_channel(struct Client *client_p)
1052 {
1053 struct Channel *chptr;
1054 struct membership *msptr;
1055 rb_dlink_node *ptr;
1056 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
1057 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
1058
1059 if (!MyClient(client_p) || IsOverride(client_p))
1060 return NULL;
1061
1062 rb_sprintf(src_host, "%s!%s@%s", client_p->name, client_p->username, client_p->host);
1063 rb_sprintf(src_iphost, "%s!%s@%s", client_p->name, client_p->username, client_p->sockhost);
1064
1065 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
1066 {
1067 msptr = ptr->data;
1068 chptr = msptr->chptr;
1069 if (is_chanop_voiced(msptr))
1070 continue;
1071 /* cached can_send */
1072 if (msptr->bants == chptr->bants)
1073 {
1074 if (can_send_banned(msptr))
1075 return chptr;
1076 }
1077 else if (is_banned(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN
1078 || is_quieted(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN)
1079 return chptr;
1080 }
1081 return NULL;
1082 }
1083
1084 /* find_nonickchange_channel()
1085 * Input: client to check
1086 * Output: channel preventing nick change
1087 */
1088 struct Channel *
1089 find_nonickchange_channel(struct Client *client_p)
1090 {
1091 struct Channel *chptr;
1092 struct membership *msptr;
1093 rb_dlink_node *ptr;
1094
1095 if (!MyClient(client_p))
1096 return NULL;
1097
1098 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
1099 {
1100 msptr = ptr->data;
1101 chptr = msptr->chptr;
1102 if (chptr->mode.mode & MODE_NONICK && (!ConfigChannel.exempt_cmode_N || !is_any_op(msptr)))
1103 return chptr;
1104 }
1105 return NULL;
1106 }
1107
1108 /* void check_spambot_warning(struct Client *source_p)
1109 * Input: Client to check, channel name or NULL if this is a part.
1110 * Output: none
1111 * Side-effects: Updates the client's oper_warn_count_down, warns the
1112 * IRC operators if necessary, and updates join_leave_countdown as
1113 * needed.
1114 */
1115 void
1116 check_spambot_warning(struct Client *source_p, const char *name)
1117 {
1118 int t_delta;
1119 int decrement_count;
1120 if((GlobalSetOptions.spam_num &&
1121 (source_p->localClient->join_leave_count >= GlobalSetOptions.spam_num)))
1122 {
1123 if(source_p->localClient->oper_warn_count_down > 0)
1124 source_p->localClient->oper_warn_count_down--;
1125 else
1126 source_p->localClient->oper_warn_count_down = 0;
1127 if(source_p->localClient->oper_warn_count_down == 0 &&
1128 name != NULL)
1129 {
1130 /* Its already known as a possible spambot */
1131 sendto_realops_snomask(SNO_BOTS, L_NETWIDE,
1132 "User %s (%s@%s) trying to join %s is a possible spambot",
1133 source_p->name,
1134 source_p->username, source_p->orighost, name);
1135 source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
1136 }
1137 }
1138 else
1139 {
1140 if((t_delta =
1141 (rb_current_time() - source_p->localClient->last_leave_time)) >
1142 JOIN_LEAVE_COUNT_EXPIRE_TIME)
1143 {
1144 decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
1145 if(name != NULL)
1146 ;
1147 else if(decrement_count > source_p->localClient->join_leave_count)
1148 source_p->localClient->join_leave_count = 0;
1149 else
1150 source_p->localClient->join_leave_count -= decrement_count;
1151 }
1152 else
1153 {
1154 if((rb_current_time() -
1155 (source_p->localClient->last_join_time)) < GlobalSetOptions.spam_time)
1156 {
1157 /* oh, its a possible spambot */
1158 source_p->localClient->join_leave_count++;
1159 }
1160 }
1161 if(name != NULL)
1162 source_p->localClient->last_join_time = rb_current_time();
1163 else
1164 source_p->localClient->last_leave_time = rb_current_time();
1165 }
1166 }
1167
1168 /* check_splitmode()
1169 *
1170 * input -
1171 * output -
1172 * side effects - compares usercount and servercount against their split
1173 * values and adjusts splitmode accordingly
1174 */
1175 void
1176 check_splitmode(void *unused)
1177 {
1178 if(splitchecking && (ConfigChannel.no_join_on_split || ConfigChannel.no_create_on_split))
1179 {
1180 /* not split, we're being asked to check now because someone
1181 * has left
1182 */
1183 if(!splitmode)
1184 {
1185 if(eob_count < split_servers || Count.total < split_users)
1186 {
1187 splitmode = 1;
1188 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1189 "Network split, activating splitmode");
1190 check_splitmode_ev = rb_event_addish("check_splitmode", check_splitmode, NULL, 2);
1191 }
1192 }
1193 /* in splitmode, check whether its finished */
1194 else if(eob_count >= split_servers && Count.total >= split_users)
1195 {
1196 splitmode = 0;
1197
1198 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1199 "Network rejoined, deactivating splitmode");
1200
1201 rb_event_delete(check_splitmode_ev);
1202 check_splitmode_ev = NULL;
1203 }
1204 }
1205 }
1206
1207
1208 /* allocate_topic()
1209 *
1210 * input - channel to allocate topic for
1211 * output - 1 on success, else 0
1212 * side effects - channel gets a topic allocated
1213 */
1214 static void
1215 allocate_topic(struct Channel *chptr)
1216 {
1217 void *ptr;
1218
1219 if(chptr == NULL)
1220 return;
1221
1222 ptr = rb_bh_alloc(topic_heap);
1223
1224 /* Basically we allocate one large block for the topic and
1225 * the topic info. We then split it up into two and shove it
1226 * in the chptr
1227 */
1228 chptr->topic = ptr;
1229 chptr->topic_info = (char *) ptr + TOPICLEN + 1;
1230 *chptr->topic = '\0';
1231 *chptr->topic_info = '\0';
1232 }
1233
1234 /* free_topic()
1235 *
1236 * input - channel which has topic to free
1237 * output -
1238 * side effects - channels topic is free'd
1239 */
1240 static void
1241 free_topic(struct Channel *chptr)
1242 {
1243 void *ptr;
1244
1245 if(chptr == NULL || chptr->topic == NULL)
1246 return;
1247
1248 /* This is safe for now - If you change allocate_topic you
1249 * MUST change this as well
1250 */
1251 ptr = chptr->topic;
1252 rb_bh_free(topic_heap, ptr);
1253 chptr->topic = NULL;
1254 chptr->topic_info = NULL;
1255 }
1256
1257 /* set_channel_topic()
1258 *
1259 * input - channel, topic to set, topic info and topic ts
1260 * output -
1261 * side effects - channels topic, topic info and TS are set.
1262 */
1263 void
1264 set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_info, time_t topicts)
1265 {
1266 if(strlen(topic) > 0)
1267 {
1268 if(chptr->topic == NULL)
1269 allocate_topic(chptr);
1270 rb_strlcpy(chptr->topic, topic, TOPICLEN + 1);
1271 rb_strlcpy(chptr->topic_info, topic_info, USERHOST_REPLYLEN);
1272 chptr->topic_time = topicts;
1273 }
1274 else
1275 {
1276 if(chptr->topic != NULL)
1277 free_topic(chptr);
1278 chptr->topic_time = 0;
1279 }
1280 }
1281
1282 /* has_common_channel()
1283 *
1284 * input - pointer to client
1285 * - pointer to another client
1286 * output - 1 if the two have a channel in common, 0 elsewise
1287 * side effects - none
1288 */
1289 int
1290 has_common_channel(struct Client *client1, struct Client *client2)
1291 {
1292 rb_dlink_node *ptr;
1293
1294 RB_DLINK_FOREACH(ptr, client1->user->channel.head)
1295 {
1296 if(IsMember(client2, ((struct membership *)ptr->data)->chptr))
1297 return 1;
1298 }
1299 return 0;
1300 }
1301
1302 /* channel_modes()
1303 *
1304 * inputs - pointer to channel
1305 * - pointer to client
1306 * output - string with simple modes
1307 * side effects - result from previous calls overwritten
1308 *
1309 * Stolen from ShadowIRCd 4 --nenolod
1310 */
1311 const char *
1312 channel_modes(struct Channel *chptr, struct Client *client_p)
1313 {
1314 int i;
1315 char buf1[BUFSIZE];
1316 char buf2[BUFSIZE];
1317 static char final[BUFSIZE];
1318 char *mbuf = buf1;
1319 char *pbuf = buf2;
1320
1321 *mbuf++ = '+';
1322 *pbuf = '\0';
1323
1324 for (i = 0; i < 256; i++)
1325 {
1326 if(chmode_table[i].set_func == chm_hidden && !IsOper(client_p) && IsClient(client_p))
1327 continue;
1328 if(chptr->mode.mode & chmode_flags[i])
1329 *mbuf++ = i;
1330 }
1331
1332 if(chptr->mode.limit)
1333 {
1334 *mbuf++ = 'l';
1335
1336 if(!IsClient(client_p) || IsMember(client_p, chptr))
1337 pbuf += rb_sprintf(pbuf, " %d", chptr->mode.limit);
1338 }
1339
1340 if(*chptr->mode.key)
1341 {
1342 *mbuf++ = 'k';
1343
1344 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1345 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.key);
1346 }
1347
1348 if(chptr->mode.join_num)
1349 {
1350 *mbuf++ = 'j';
1351
1352 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1353 pbuf += rb_sprintf(pbuf, " %d:%d", chptr->mode.join_num,
1354 chptr->mode.join_time);
1355 }
1356
1357 if(*chptr->mode.forward && (ConfigChannel.use_forward || !IsClient(client_p)))
1358 {
1359 *mbuf++ = 'f';
1360
1361 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1362 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.forward);
1363 }
1364
1365 *mbuf = '\0';
1366
1367 rb_strlcpy(final, buf1, sizeof final);
1368 rb_strlcat(final, buf2, sizeof final);
1369 return final;
1370 }
1371
1372 /* Now lets do some stuff to keep track of what combinations of
1373 * servers exist...
1374 * Note that the number of combinations doubles each time you add
1375 * something to this list. Each one is only quick if no servers use that
1376 * combination, but if the numbers get too high here MODE will get too
1377 * slow. I suggest if you get more than 7 here, you consider getting rid
1378 * of some and merging or something. If it wasn't for irc+cs we would
1379 * probably not even need to bother about most of these, but unfortunately
1380 * we do. -A1kmm
1381 */
1382
1383 /* void init_chcap_usage_counts(void)
1384 *
1385 * Inputs - none
1386 * Output - none
1387 * Side-effects - Initialises the usage counts to zero. Fills in the
1388 * chcap_yes and chcap_no combination tables.
1389 */
1390 void
1391 init_chcap_usage_counts(void)
1392 {
1393 unsigned long m, c, y, n;
1394
1395 memset(chcap_combos, 0, sizeof(chcap_combos));
1396
1397 /* For every possible combination */
1398 for (m = 0; m < NCHCAP_COMBOS; m++)
1399 {
1400 /* Check each capab */
1401 for (c = y = n = 0; c < NCHCAPS; c++)
1402 {
1403 if((m & (1 << c)) == 0)
1404 n |= channel_capabs[c];
1405 else
1406 y |= channel_capabs[c];
1407 }
1408 chcap_combos[m].cap_yes = y;
1409 chcap_combos[m].cap_no = n;
1410 }
1411 }
1412
1413 /* void set_chcap_usage_counts(struct Client *serv_p)
1414 * Input: serv_p; The client whose capabs to register.
1415 * Output: none
1416 * Side-effects: Increments the usage counts for the correct capab
1417 * combination.
1418 */
1419 void
1420 set_chcap_usage_counts(struct Client *serv_p)
1421 {
1422 int n;
1423
1424 for (n = 0; n < NCHCAP_COMBOS; n++)
1425 {
1426 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1427 NotCapable(serv_p, chcap_combos[n].cap_no))
1428 {
1429 chcap_combos[n].count++;
1430 return;
1431 }
1432 }
1433
1434 /* This should be impossible -A1kmm. */
1435 s_assert(0);
1436 }
1437
1438 /* void set_chcap_usage_counts(struct Client *serv_p)
1439 *
1440 * Inputs - serv_p; The client whose capabs to register.
1441 * Output - none
1442 * Side-effects - Decrements the usage counts for the correct capab
1443 * combination.
1444 */
1445 void
1446 unset_chcap_usage_counts(struct Client *serv_p)
1447 {
1448 int n;
1449
1450 for (n = 0; n < NCHCAP_COMBOS; n++)
1451 {
1452 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1453 NotCapable(serv_p, chcap_combos[n].cap_no))
1454 {
1455 /* Hopefully capabs can't change dynamically or anything... */
1456 s_assert(chcap_combos[n].count > 0);
1457
1458 if(chcap_combos[n].count > 0)
1459 chcap_combos[n].count--;
1460 return;
1461 }
1462 }
1463
1464 /* This should be impossible -A1kmm. */
1465 s_assert(0);
1466 }
1467
1468 /* void send_cap_mode_changes(struct Client *client_p,
1469 * struct Client *source_p,
1470 * struct Channel *chptr, int cap, int nocap)
1471 * Input: The client sending(client_p), the source client(source_p),
1472 * the channel to send mode changes for(chptr)
1473 * Output: None.
1474 * Side-effects: Sends the appropriate mode changes to capable servers.
1475 *
1476 * Reverted back to my original design, except that we now keep a count
1477 * of the number of servers which each combination as an optimisation, so
1478 * the capabs combinations which are not needed are not worked out. -A1kmm
1479 */
1480 void
1481 send_cap_mode_changes(struct Client *client_p, struct Client *source_p,
1482 struct Channel *chptr, struct ChModeChange mode_changes[], int mode_count)
1483 {
1484 static char modebuf[BUFSIZE];
1485 static char parabuf[BUFSIZE];
1486 int i, mbl, pbl, nc, mc, preflen, len;
1487 char *pbuf;
1488 const char *arg;
1489 int dir;
1490 int j;
1491 int cap;
1492 int nocap;
1493 int arglen;
1494
1495 /* Now send to servers... */
1496 for (j = 0; j < NCHCAP_COMBOS; j++)
1497 {
1498 if(chcap_combos[j].count == 0)
1499 continue;
1500
1501 mc = 0;
1502 nc = 0;
1503 pbl = 0;
1504 parabuf[0] = 0;
1505 pbuf = parabuf;
1506 dir = MODE_QUERY;
1507
1508 cap = chcap_combos[j].cap_yes;
1509 nocap = chcap_combos[j].cap_no;
1510
1511 mbl = preflen = rb_sprintf(modebuf, ":%s TMODE %ld %s ",
1512 use_id(source_p), (long) chptr->channelts,
1513 chptr->chname);
1514
1515 /* loop the list of - modes we have */
1516 for (i = 0; i < mode_count; i++)
1517 {
1518 /* if they dont support the cap we need, or they do support a cap they
1519 * cant have, then dont add it to the modebuf.. that way they wont see
1520 * the mode
1521 */
1522 if((mode_changes[i].letter == 0) ||
1523 ((cap & mode_changes[i].caps) != mode_changes[i].caps)
1524 || ((nocap & mode_changes[i].nocaps) != mode_changes[i].nocaps))
1525 continue;
1526
1527 if(!EmptyString(mode_changes[i].id))
1528 arg = mode_changes[i].id;
1529 else
1530 arg = mode_changes[i].arg;
1531
1532 if(arg)
1533 {
1534 arglen = strlen(arg);
1535
1536 /* dont even think about it! --fl */
1537 if(arglen > MODEBUFLEN - 5)
1538 continue;
1539 }
1540
1541 /* if we're creeping past the buf size, we need to send it and make
1542 * another line for the other modes
1543 * XXX - this could give away server topology with uids being
1544 * different lengths, but not much we can do, except possibly break
1545 * them as if they were the longest of the nick or uid at all times,
1546 * which even then won't work as we don't always know the uid -A1kmm.
1547 */
1548 if(arg && ((mc == MAXMODEPARAMSSERV) ||
1549 ((mbl + pbl + arglen + 4) > (BUFSIZE - 3))))
1550 {
1551 if(nc != 0)
1552 sendto_server(client_p, chptr, cap, nocap,
1553 "%s %s", modebuf, parabuf);
1554 nc = 0;
1555 mc = 0;
1556
1557 mbl = preflen;
1558 pbl = 0;
1559 pbuf = parabuf;
1560 parabuf[0] = 0;
1561 dir = MODE_QUERY;
1562 }
1563
1564 if(dir != mode_changes[i].dir)
1565 {
1566 modebuf[mbl++] = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
1567 dir = mode_changes[i].dir;
1568 }
1569
1570 modebuf[mbl++] = mode_changes[i].letter;
1571 modebuf[mbl] = 0;
1572 nc++;
1573
1574 if(arg != NULL)
1575 {
1576 len = rb_sprintf(pbuf, "%s ", arg);
1577 pbuf += len;
1578 pbl += len;
1579 mc++;
1580 }
1581 }
1582
1583 if(pbl && parabuf[pbl - 1] == ' ')
1584 parabuf[pbl - 1] = 0;
1585
1586 if(nc != 0)
1587 sendto_server(client_p, chptr, cap, nocap, "%s %s", modebuf, parabuf);
1588 }
1589 }
1590
1591 void
1592 resv_chan_forcepart(const char *name, const char *reason, int temp_time)
1593 {
1594 rb_dlink_node *ptr;
1595 rb_dlink_node *next_ptr;
1596 struct Channel *chptr;
1597 struct membership *msptr;
1598 struct Client *target_p;
1599
1600 if(!ConfigChannel.resv_forcepart)
1601 return;
1602
1603 /* for each user on our server in the channel list
1604 * send them a PART, and notify opers.
1605 */
1606 chptr = find_channel(name);
1607 if(chptr != NULL)
1608 {
1609 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
1610 {
1611 msptr = ptr->data;
1612 target_p = msptr->client_p;
1613
1614 if(IsExemptResv(target_p))
1615 continue;
1616
1617 sendto_server(target_p, chptr, CAP_TS6, NOCAPS,
1618 ":%s PART %s", target_p->id, chptr->chname);
1619
1620 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s :%s",
1621 target_p->name, target_p->username,
1622 target_p->host, chptr->chname, target_p->name);
1623
1624 remove_user_from_channel(msptr);
1625
1626 /* notify opers & user they were removed from the channel */
1627 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1628 "Forced PART for %s!%s@%s from %s (%s)",
1629 target_p->name, target_p->username,
1630 target_p->host, name, reason);
1631
1632 if(temp_time > 0)
1633 sendto_one_notice(target_p, ":*** Channel %s is temporarily unavailable on this server.",
1634 name);
1635 else
1636 sendto_one_notice(target_p, ":*** Channel %s is no longer available on this server.",
1637 name);
1638 }
1639 }
1640 }
1641
1642 /* Check what we will forward to, without sending any notices to the user
1643 * -- jilles
1644 */
1645 struct Channel *
1646 check_forward(struct Client *source_p, struct Channel *chptr,
1647 char *key)
1648 {
1649 int depth = 0, i;
1650
1651 /* User is +Q */
1652 if (IsNoForward(source_p))
1653 return NULL;
1654
1655 while (depth < 16)
1656 {
1657 chptr = find_channel(chptr->mode.forward);
1658 /* Can only forward to existing channels */
1659 if (chptr == NULL)
1660 return NULL;
1661 /* Already on there, show original error message */
1662 if (IsMember(source_p, chptr))
1663 return NULL;
1664 /* Juped. Sending a warning notice would be unfair */
1665 if (hash_find_resv(chptr->chname))
1666 return NULL;
1667 /* Don't forward to +Q channel */
1668 if (chptr->mode.mode & MODE_DISFORWARD)
1669 return NULL;
1670 i = can_join(source_p, chptr, key);
1671 if (i == 0)
1672 return chptr;
1673 if (i != ERR_INVITEONLYCHAN && i != ERR_NEEDREGGEDNICK && i != ERR_THROTTLE && i != ERR_CHANNELISFULL)
1674 return NULL;
1675 depth++;
1676 }
1677
1678 return NULL;
1679 }
1680
1681 /*
1682 * do_join_0
1683 *
1684 * inputs - pointer to client doing join 0
1685 * output - NONE
1686 * side effects - Use has decided to join 0. This is legacy
1687 * from the days when channels were numbers not names. *sigh*
1688 */
1689 void
1690 do_join_0(struct Client *client_p, struct Client *source_p)
1691 {
1692 struct membership *msptr;
1693 struct Channel *chptr = NULL;
1694 rb_dlink_node *ptr;
1695
1696 /* Finish the flood grace period... */
1697 if(MyClient(source_p) && !IsFloodDone(source_p))
1698 flood_endgrace(source_p);
1699
1700 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s JOIN 0", use_id(source_p));
1701
1702 while((ptr = source_p->user->channel.head))
1703 {
1704 if(source_p->user->channel.head && MyConnect(source_p) &&
1705 !IsOper(source_p) && !IsExemptSpambot(source_p))
1706 check_spambot_warning(source_p, NULL);
1707
1708 msptr = ptr->data;
1709 chptr = msptr->chptr;
1710 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s",
1711 source_p->name,
1712 source_p->username, source_p->host, chptr->chname);
1713 remove_user_from_channel(msptr);
1714 }
1715 }
1716
1717 int
1718 check_channel_name_loc(struct Client *source_p, const char *name)
1719 {
1720 const char *p;
1721
1722 s_assert(name != NULL);
1723 if(EmptyString(name))
1724 return 0;
1725
1726 if(ConfigFileEntry.disable_fake_channels && !IsOper(source_p))
1727 {
1728 for(p = name; *p; ++p)
1729 {
1730 if(!IsChanChar(*p) || IsFakeChanChar(*p))
1731 return 0;
1732 }
1733 }
1734 else
1735 {
1736 for(p = name; *p; ++p)
1737 {
1738 if(!IsChanChar(*p))
1739 return 0;
1740 }
1741 }
1742
1743 if(ConfigChannel.only_ascii_channels)
1744 {
1745 for(p = name; *p; ++p)
1746 if(*p < 33 || *p > 126)
1747 return 0;
1748 }
1749
1750
1751 return 1;
1752 }
1753
1754 void user_join(struct Client * client_p, struct Client * source_p, const char * channels, const char * keys)
1755 {
1756 static char jbuf[BUFSIZE];
1757 struct Channel *chptr = NULL;
1758 struct ConfItem *aconf;
1759 char *name;
1760 char *key = NULL;
1761 const char *modes;
1762 int i, flags = 0;
1763 char *p = NULL, *p2 = NULL;
1764 char *chanlist;
1765 char *mykey;
1766
1767 jbuf[0] = '\0';
1768
1769 if(channels == NULL)
1770 return;
1771
1772 /* rebuild the list of channels theyre supposed to be joining.
1773 * this code has a side effect of losing keys, but..
1774 */
1775 chanlist = LOCAL_COPY(channels);
1776 for(name = rb_strtok_r(chanlist, ",", &p); name; name = rb_strtok_r(NULL, ",", &p))
1777 {
1778 /* check the length and name of channel is ok */
1779 if(!check_channel_name_loc(source_p, name) || (strlen(name) > LOC_CHANNELLEN))
1780 {
1781 sendto_one_numeric(source_p, ERR_BADCHANNAME,
1782 form_str(ERR_BADCHANNAME), (unsigned char *) name);
1783 continue;
1784 }
1785
1786 /* join 0 parts all channels */
1787 if(*name == '0' && (name[1] == ',' || name[1] == '\0') && name == chanlist)
1788 {
1789 (void) strcpy(jbuf, "0");
1790 continue;
1791 }
1792
1793 /* check it begins with # or &, and local chans are disabled */
1794 else if(!IsChannelName(name) ||
1795 ( !ConfigChannel.use_local_channels && name[0] == '&'))
1796 {
1797 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
1798 form_str(ERR_NOSUCHCHANNEL), name);
1799 continue;
1800 }
1801
1802 /* see if its resv'd */
1803 if(!IsExemptResv(source_p) && (aconf = hash_find_resv(name)))
1804 {
1805 sendto_one_numeric(source_p, ERR_BADCHANNAME,
1806 form_str(ERR_BADCHANNAME), name);
1807
1808 /* dont warn for opers */
1809 if(!IsExemptJupe(source_p) && !IsOper(source_p))
1810 sendto_realops_snomask(SNO_SPY, L_NETWIDE,
1811 "User %s (%s@%s) is attempting to join locally juped channel %s (%s)",
1812 source_p->name, source_p->username,
1813 source_p->orighost, name, aconf->passwd);
1814 /* dont update tracking for jupe exempt users, these
1815 * are likely to be spamtrap leaves
1816 */
1817 else if(IsExemptJupe(source_p))
1818 aconf->port--;
1819
1820 continue;
1821 }
1822
1823 if(splitmode && !IsOper(source_p) && (*name != '&') &&
1824 ConfigChannel.no_join_on_split)
1825 {
1826 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1827 me.name, source_p->name, name);
1828 continue;
1829 }
1830
1831 if(*jbuf)
1832 (void) strcat(jbuf, ",");
1833 (void) rb_strlcat(jbuf, name, sizeof(jbuf));
1834 }
1835
1836 if(keys != NULL)
1837 {
1838 mykey = LOCAL_COPY(keys);
1839 key = rb_strtok_r(mykey, ",", &p2);
1840 }
1841
1842 for(name = rb_strtok_r(jbuf, ",", &p); name;
1843 key = (key) ? rb_strtok_r(NULL, ",", &p2) : NULL, name = rb_strtok_r(NULL, ",", &p))
1844 {
1845 hook_data_channel_activity hook_info;
1846
1847 /* JOIN 0 simply parts all channels the user is in */
1848 if(*name == '0' && !atoi(name))
1849 {
1850 if(source_p->user->channel.head == NULL)
1851 continue;
1852
1853 do_join_0(&me, source_p);
1854 continue;
1855 }
1856
1857 /* look for the channel */
1858 if((chptr = find_channel(name)) != NULL)
1859 {
1860 if(IsMember(source_p, chptr))
1861 continue;
1862
1863 flags = 0;
1864 }
1865 else
1866 {
1867 hook_data_client_approval moduledata;
1868
1869 moduledata.client = source_p;
1870 moduledata.approved = 0;
1871
1872 call_hook(h_can_create_channel, &moduledata);
1873
1874 if(moduledata.approved != 0)
1875 {
1876 sendto_one(source_p, form_str(moduledata.approved),
1877 me.name, source_p->name, name);
1878 continue;
1879 }
1880
1881 if(splitmode && !IsOper(source_p) && (*name != '&') &&
1882 ConfigChannel.no_create_on_split)
1883 {
1884 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1885 me.name, source_p->name, name);
1886 continue;
1887 }
1888
1889 if(ConfigChannel.admin_on_channel_create && ConfigChannel.use_admin)
1890 flags = CHFL_ADMIN | CHFL_CHANOP;
1891 else
1892 flags = CHFL_CHANOP;
1893 }
1894
1895 if((rb_dlink_list_length(&source_p->user->channel) >=
1896 (unsigned long) ConfigChannel.max_chans_per_user) &&
1897 (!IsOper(source_p) ||
1898 (rb_dlink_list_length(&source_p->user->channel) >=
1899 (unsigned long) ConfigChannel.max_chans_per_user * 3)))
1900 {
1901 sendto_one(source_p, form_str(ERR_TOOMANYCHANNELS),
1902 me.name, source_p->name, name);
1903 return;
1904 }
1905
1906 if(chptr == NULL) /* If I already have a chptr, no point doing this */
1907 {
1908 chptr = get_or_create_channel(source_p, name, NULL);
1909
1910 if(chptr == NULL)
1911 {
1912 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1913 me.name, source_p->name, name);
1914 continue;
1915 }
1916 }
1917
1918 /* can_join checks for +i key, bans etc */
1919 if((i = can_join(source_p, chptr, key)))
1920 {
1921 if(IsOverride(source_p))
1922 {
1923 sendto_wallops_flags(UMODE_WALLOP, &me,
1924 "%s is overriding JOIN to [%s]",
1925 get_oper_name(source_p), chptr->chname);
1926 sendto_server(NULL, chptr, NOCAPS, NOCAPS,
1927 ":%s WALLOPS :%s is overriding JOIN to [%s]",
1928 me.name, get_oper_name(source_p), chptr->chname);
1929 }
1930 else if ((i != ERR_NEEDREGGEDNICK && i != ERR_THROTTLE && i != ERR_INVITEONLYCHAN && i != ERR_CHANNELISFULL) ||
1931 (!ConfigChannel.use_forward || (chptr = check_forward(source_p, chptr, key)) == NULL))
1932 {
1933 /* might be wrong, but is there any other better location for such?
1934 * see extensions/chm_operonly.c for other comments on this
1935 * -- dwr
1936 */
1937 if(i != ERR_CUSTOM)
1938 sendto_one(source_p, form_str(i), me.name, source_p->name, name);
1939
1940 continue;
1941 }
1942 else
1943 sendto_one_numeric(source_p, ERR_LINKCHANNEL, form_str(ERR_LINKCHANNEL), name, chptr->chname);
1944 }
1945
1946 if(flags == 0 &&
1947 !IsOper(source_p) && !IsExemptSpambot(source_p))
1948 check_spambot_warning(source_p, name);
1949
1950 /* add the user to the channel */
1951 add_user_to_channel(chptr, source_p, flags);
1952 if (chptr->mode.join_num &&
1953 rb_current_time() - chptr->join_delta >= chptr->mode.join_time)
1954 {
1955 chptr->join_count = 0;
1956 chptr->join_delta = rb_current_time();
1957 }
1958 chptr->join_count++;
1959
1960 /* we send the user their join here, because we could have to
1961 * send a mode out next.
1962 */
1963 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s JOIN :%s",
1964 source_p->name,
1965 source_p->username, source_p->host, chptr->chname);
1966
1967 /* its a new channel, set +nt and burst. */
1968 if(flags & CHFL_CHANOP)
1969 {
1970 chptr->channelts = rb_current_time();
1971
1972 /* autochanmodes stuff */
1973 if(ConfigChannel.autochanmodes)
1974 {
1975 char * ch;
1976 for(ch = ConfigChannel.autochanmodes; *ch; *ch++)
1977 {
1978 chptr->mode.mode |= chmode_table[*ch].mode_type;
1979 }
1980 }
1981 else
1982 {
1983 chptr->mode.mode |= MODE_TOPICLIMIT;
1984 chptr->mode.mode |= MODE_NOPRIVMSGS;
1985 }
1986
1987 modes = channel_modes(chptr, &me);
1988
1989 sendto_channel_local(ONLY_CHANOPS, chptr, ":%s MODE %s %s",
1990 me.name, chptr->chname, modes);
1991
1992 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
1993 ":%s SJOIN %ld %s %s :@%s",
1994 me.id, (long) chptr->channelts,
1995 chptr->chname, modes, source_p->id);
1996 }
1997 else
1998 {
1999 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
2000 ":%s JOIN %ld %s +",
2001 use_id(source_p), (long) chptr->channelts,
2002 chptr->chname);
2003 }
2004
2005 del_invite(chptr, source_p);
2006
2007 if(chptr->topic != NULL)
2008 {
2009 sendto_one(source_p, form_str(RPL_TOPIC), me.name,
2010 source_p->name, chptr->chname, chptr->topic);
2011
2012 sendto_one(source_p, form_str(RPL_TOPICWHOTIME),
2013 me.name, source_p->name, chptr->chname,
2014 chptr->topic_info, chptr->topic_time);
2015 }
2016
2017 channel_member_names(chptr, source_p, 1);
2018
2019 hook_info.client = source_p;
2020 hook_info.chptr = chptr;
2021 hook_info.key = key;
2022 call_hook(h_channel_join, &hook_info);
2023 }
2024
2025 return;
2026 }
2027
2028 /*
2029 * channel_metadata_add
2030 *
2031 * inputs - pointer to channel struct
2032 * - name of metadata item you wish to add
2033 * - value of metadata item
2034 * - 1 if metadata should be propegated, 0 if not
2035 * output - none
2036 * side effects - metadata is added to the channel in question
2037 * - metadata is propegated if propegate is set.
2038 */
2039 struct Metadata *
2040 channel_metadata_add(struct Channel *target, const char *name, const char *value, int propegate)
2041 {
2042 struct Metadata *md;
2043
2044 md = rb_malloc(sizeof(struct Metadata));
2045 md->name = rb_strdup(name);
2046 md->value = rb_strdup(value);
2047
2048 irc_dictionary_add(target->metadata, md->name, md);
2049
2050 if(propegate)
2051 sendto_match_servs(&me, "*", CAP_ENCAP, NOCAPS, "ENCAP * METADATA ADD %s %s :%s",
2052 target->chname, name, value);
2053
2054 return md;
2055 }
2056
2057 /*
2058 * channel_metadata_time_add
2059 *
2060 * inputs - pointer to channel struct
2061 * - name of metadata item you wish to add
2062 * - time_t you wish to add
2063 * - value you wish to add
2064 * output - none
2065 * side effects - metadata is added to the channel in question
2066 */
2067 struct Metadata *
2068 channel_metadata_time_add(struct Channel *target, const char *name, time_t timevalue, const char *value)
2069 {
2070 struct Metadata *md;
2071
2072 md = rb_malloc(sizeof(struct Metadata));
2073 md->name = rb_strdup(name);
2074 md->value = rb_strdup(value);
2075 md->timevalue = timevalue;
2076
2077 irc_dictionary_add(target->metadata, md->name, md);
2078
2079 return md;
2080 }
2081
2082 /*
2083 * channel_metadata_delete
2084 *
2085 * inputs - pointer to channel struct
2086 * - name of metadata item you wish to delete
2087 * output - none
2088 * side effects - metadata is deleted from the channel in question
2089 * - deletion is propegated if propegate is set
2090 */
2091 void
2092 channel_metadata_delete(struct Channel *target, const char *name, int propegate)
2093 {
2094 struct Metadata *md = channel_metadata_find(target, name);
2095
2096 if(!md)
2097 return;
2098
2099 irc_dictionary_delete(target->metadata, md->name);
2100
2101 rb_free(md);
2102
2103 if(propegate)
2104 sendto_match_servs(&me, "*", CAP_ENCAP, NOCAPS, "ENCAP * METADATA DELETE %s %s",
2105 target->chname, name);
2106 }
2107
2108 /*
2109 * channel_metadata_find
2110 *
2111 * inputs - pointer to channel struct
2112 * - name of metadata item you wish to read
2113 * output - the requested metadata, if it exists, elsewise null.
2114 * side effects -
2115 */
2116 struct Metadata *
2117 channel_metadata_find(struct Channel *target, const char *name)
2118 {
2119 if(!target)
2120 return NULL;
2121
2122 if(!target->metadata)
2123 return NULL;
2124
2125 return irc_dictionary_retrieve(target->metadata, name);
2126 }
2127
2128 /*
2129 * channel_metadata_clear
2130 *
2131 * inputs - pointer to channel struct
2132 * output - none
2133 * side effects - metadata is cleared from the channel in question
2134 */
2135 void
2136 channel_metadata_clear(struct Channel *chptr)
2137 {
2138 struct Metadata *md;
2139 struct DictionaryIter iter;
2140
2141 DICTIONARY_FOREACH(md, &iter, chptr->metadata)
2142 {
2143 channel_metadata_delete(chptr, md->name, 0);
2144 }
2145 }