]> jfr.im git - irc/rqf/shadowircd.git/blob - src/channel.c
44c8d9fbc3c8b93edfe90e63a7cd80809e4f8c92
[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 * $Id: channel.c 3580 2007-11-07 23:45:14Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "channel.h"
29 #include "chmode.h"
30 #include "client.h"
31 #include "common.h"
32 #include "hash.h"
33 #include "hook.h"
34 #include "match.h"
35 #include "ircd.h"
36 #include "numeric.h"
37 #include "s_serv.h" /* captab */
38 #include "s_user.h"
39 #include "send.h"
40 #include "whowas.h"
41 #include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
42 #include "s_newconf.h"
43 #include "logger.h"
44 #include "packet.h"
45 #include "irc_dictionary.h"
46
47 struct config_channel_entry ConfigChannel;
48 rb_dlink_list global_channel_list;
49 static rb_bh *channel_heap;
50 static rb_bh *ban_heap;
51 static rb_bh *topic_heap;
52 static rb_bh *member_heap;
53
54 static int channel_capabs[] = { CAP_EX, CAP_IE,
55 CAP_SERVICE,
56 CAP_TS6
57 };
58
59 #define NCHCAPS (sizeof(channel_capabs)/sizeof(int))
60 #define NCHCAP_COMBOS (1 << NCHCAPS)
61
62 static struct ChCapCombo chcap_combos[NCHCAP_COMBOS];
63
64 static void free_topic(struct Channel *chptr);
65
66 static int h_can_join;
67 static int h_can_create_channel;
68 static int h_channel_join;
69
70 /* init_channels()
71 *
72 * input -
73 * output -
74 * side effects - initialises the various blockheaps
75 */
76 void
77 init_channels(void)
78 {
79 channel_heap = rb_bh_create(sizeof(struct Channel), CHANNEL_HEAP_SIZE, "channel_heap");
80 ban_heap = rb_bh_create(sizeof(struct Ban), BAN_HEAP_SIZE, "ban_heap");
81 topic_heap = rb_bh_create(TOPICLEN + 1 + USERHOST_REPLYLEN, TOPIC_HEAP_SIZE, "topic_heap");
82 member_heap = rb_bh_create(sizeof(struct membership), MEMBER_HEAP_SIZE, "member_heap");
83
84 h_can_join = register_hook("can_join");
85 h_channel_join = register_hook("channel_join");
86 h_can_create_channel = register_hook("can_create_channel");
87 }
88
89 /*
90 * allocate_channel - Allocates a channel
91 */
92 struct Channel *
93 allocate_channel(const char *chname)
94 {
95 struct Channel *chptr;
96 struct Dictionary *c_metadata;
97 chptr = rb_bh_alloc(channel_heap);
98 chptr->chname = rb_strdup(chname);
99
100 c_metadata = irc_dictionary_create(irccmp);
101 chptr->c_metadata = c_metadata;
102 return (chptr);
103 }
104
105 void
106 free_channel(struct Channel *chptr)
107 {
108 /* insert deletion of metadata here! */
109 rb_free(chptr->chname);
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_owner(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_owner()
237 *
238 * input - membership to check for owner
239 * output - 1 if the user is an owner, 0 if the user is not or owner
240 * is disabled.
241 * side effects -
242 *
243 */
244 int
245 is_owner(struct membership *msptr)
246 {
247 if(!ConfigChannel.use_owner)
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 owner, 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_owner(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, owner, 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_owner(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_owner(target))
295 return 1;
296 else if(is_halfop(source) && !is_any_op(target))
297 return 1;
298 else if(is_owner(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 int use_althost = 0;
830 int i = 0;
831 hook_data_channel moduledata;
832
833 s_assert(source_p->localClient != NULL);
834
835 if(IsOverride(source_p))
836 return 0;
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 if(chptr->mode.mode & MODE_INVITEONLY)
861 {
862 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
863 {
864 if(invite->data == chptr)
865 break;
866 }
867 if(invite == NULL)
868 {
869 if(!ConfigChannel.use_invex)
870 return (ERR_INVITEONLYCHAN);
871 RB_DLINK_FOREACH(ptr, chptr->invexlist.head)
872 {
873 invex = ptr->data;
874 if(match(invex->banstr, src_host)
875 || match(invex->banstr, src_iphost)
876 || match_cidr(invex->banstr, src_iphost)
877 || match_extban(invex->banstr, source_p, chptr, CHFL_INVEX)
878 || (use_althost && match(invex->banstr, src_althost)))
879 break;
880 }
881 if(ptr == NULL)
882 return (ERR_INVITEONLYCHAN);
883 }
884 }
885
886 if(*chptr->mode.key && (EmptyString(key) || irccmp(chptr->mode.key, key)))
887 return (ERR_BADCHANNELKEY);
888
889 if(chptr->mode.limit &&
890 rb_dlink_list_length(&chptr->members) >= (unsigned long) chptr->mode.limit)
891 i = ERR_CHANNELISFULL;
892 if(chptr->mode.mode & MODE_REGONLY && EmptyString(source_p->user->suser))
893 i = ERR_NEEDREGGEDNICK;
894 /* join throttling stuff --nenolod */
895 else if(chptr->mode.join_num > 0 && chptr->mode.join_time > 0)
896 {
897 if ((rb_current_time() - chptr->join_delta <=
898 chptr->mode.join_time) && (chptr->join_count >=
899 chptr->mode.join_num))
900 i = ERR_THROTTLE;
901 }
902
903 /* allow /invite to override +l/+r/+j also -- jilles */
904 if (i != 0 && invite == NULL)
905 {
906 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
907 {
908 if(invite->data == chptr)
909 break;
910 }
911 if (invite == NULL)
912 return i;
913 }
914
915 moduledata.client = source_p;
916 moduledata.chptr = chptr;
917 moduledata.approved = 0;
918
919 call_hook(h_can_join, &moduledata);
920
921 return moduledata.approved;
922 }
923
924 /* can_send()
925 *
926 * input - user to check in channel, membership pointer
927 * output - whether can explicitly send or not, else CAN_SEND_NONOP
928 * side effects -
929 */
930 int
931 can_send(struct Channel *chptr, struct Client *source_p, struct membership *msptr)
932 {
933 if(IsServer(source_p) || IsService(source_p))
934 return CAN_SEND_OPV;
935
936 if(MyClient(source_p) && hash_find_resv(chptr->chname) &&
937 !IsOper(source_p) && !IsExemptResv(source_p))
938 return CAN_SEND_NO;
939
940 if(msptr == NULL)
941 {
942 msptr = find_channel_membership(chptr, source_p);
943
944 if(msptr == NULL)
945 {
946 /* if its +m or +n and theyre not in the channel,
947 * they cant send. we dont check bans here because
948 * theres no possibility of caching them --fl
949 */
950 if(chptr->mode.mode & MODE_NOPRIVMSGS || chptr->mode.mode & MODE_MODERATED)
951 return CAN_SEND_NO;
952 else
953 return CAN_SEND_NONOP;
954 }
955 }
956
957 if(is_chanop_voiced(msptr))
958 return CAN_SEND_OPV;
959
960 if(IsOverride(source_p))
961 return CAN_SEND_NONOP;
962
963 if(chptr->mode.mode & MODE_MODERATED)
964 return CAN_SEND_NO;
965
966 if(MyClient(source_p))
967 {
968 /* cached can_send */
969 if(msptr->bants == chptr->bants)
970 {
971 if(can_send_banned(msptr))
972 return CAN_SEND_NO;
973 }
974 else if(is_banned(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN
975 || is_quieted(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN)
976 return CAN_SEND_NO;
977 }
978
979 return CAN_SEND_NONOP;
980 }
981
982 /* find_bannickchange_channel()
983 * Input: client to check
984 * Output: channel preventing nick change
985 */
986 struct Channel *
987 find_bannickchange_channel(struct Client *client_p)
988 {
989 struct Channel *chptr;
990 struct membership *msptr;
991 rb_dlink_node *ptr;
992 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
993 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
994
995 if (!MyClient(client_p) || IsOverride(client_p))
996 return NULL;
997
998 rb_sprintf(src_host, "%s!%s@%s", client_p->name, client_p->username, client_p->host);
999 rb_sprintf(src_iphost, "%s!%s@%s", client_p->name, client_p->username, client_p->sockhost);
1000
1001 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
1002 {
1003 msptr = ptr->data;
1004 chptr = msptr->chptr;
1005 if (is_chanop_voiced(msptr))
1006 continue;
1007 /* cached can_send */
1008 if (msptr->bants == chptr->bants)
1009 {
1010 if (can_send_banned(msptr))
1011 return chptr;
1012 }
1013 else if (is_banned(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN
1014 || is_quieted(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN)
1015 return chptr;
1016 }
1017 return NULL;
1018 }
1019
1020 /* find_nonickchange_channel()
1021 * Input: client to check
1022 * Output: channel preventing nick change
1023 */
1024 struct Channel *
1025 find_nonickchange_channel(struct Client *client_p)
1026 {
1027 struct Channel *chptr;
1028 struct membership *msptr;
1029 rb_dlink_node *ptr;
1030
1031 if (!MyClient(client_p))
1032 return NULL;
1033
1034 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
1035 {
1036 msptr = ptr->data;
1037 chptr = msptr->chptr;
1038 if (chptr->mode.mode & MODE_NONICK && (!ConfigChannel.exempt_cmode_N || !is_any_op(msptr)))
1039 return chptr;
1040 }
1041 return NULL;
1042 }
1043
1044 /* void check_spambot_warning(struct Client *source_p)
1045 * Input: Client to check, channel name or NULL if this is a part.
1046 * Output: none
1047 * Side-effects: Updates the client's oper_warn_count_down, warns the
1048 * IRC operators if necessary, and updates join_leave_countdown as
1049 * needed.
1050 */
1051 void
1052 check_spambot_warning(struct Client *source_p, const char *name)
1053 {
1054 int t_delta;
1055 int decrement_count;
1056 if((GlobalSetOptions.spam_num &&
1057 (source_p->localClient->join_leave_count >= GlobalSetOptions.spam_num)))
1058 {
1059 if(source_p->localClient->oper_warn_count_down > 0)
1060 source_p->localClient->oper_warn_count_down--;
1061 else
1062 source_p->localClient->oper_warn_count_down = 0;
1063 if(source_p->localClient->oper_warn_count_down == 0 &&
1064 name != NULL)
1065 {
1066 /* Its already known as a possible spambot */
1067 sendto_realops_snomask(SNO_BOTS, L_NETWIDE,
1068 "User %s (%s@%s) trying to join %s is a possible spambot",
1069 source_p->name,
1070 source_p->username, source_p->orighost, name);
1071 source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
1072 }
1073 }
1074 else
1075 {
1076 if((t_delta =
1077 (rb_current_time() - source_p->localClient->last_leave_time)) >
1078 JOIN_LEAVE_COUNT_EXPIRE_TIME)
1079 {
1080 decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
1081 if(name != NULL)
1082 ;
1083 else if(decrement_count > source_p->localClient->join_leave_count)
1084 source_p->localClient->join_leave_count = 0;
1085 else
1086 source_p->localClient->join_leave_count -= decrement_count;
1087 }
1088 else
1089 {
1090 if((rb_current_time() -
1091 (source_p->localClient->last_join_time)) < GlobalSetOptions.spam_time)
1092 {
1093 /* oh, its a possible spambot */
1094 source_p->localClient->join_leave_count++;
1095 }
1096 }
1097 if(name != NULL)
1098 source_p->localClient->last_join_time = rb_current_time();
1099 else
1100 source_p->localClient->last_leave_time = rb_current_time();
1101 }
1102 }
1103
1104 /* check_splitmode()
1105 *
1106 * input -
1107 * output -
1108 * side effects - compares usercount and servercount against their split
1109 * values and adjusts splitmode accordingly
1110 */
1111 void
1112 check_splitmode(void *unused)
1113 {
1114 if(splitchecking && (ConfigChannel.no_join_on_split || ConfigChannel.no_create_on_split))
1115 {
1116 /* not split, we're being asked to check now because someone
1117 * has left
1118 */
1119 if(!splitmode)
1120 {
1121 if(eob_count < split_servers || Count.total < split_users)
1122 {
1123 splitmode = 1;
1124 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1125 "Network split, activating splitmode");
1126 check_splitmode_ev = rb_event_addish("check_splitmode", check_splitmode, NULL, 2);
1127 }
1128 }
1129 /* in splitmode, check whether its finished */
1130 else if(eob_count >= split_servers && Count.total >= split_users)
1131 {
1132 splitmode = 0;
1133
1134 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1135 "Network rejoined, deactivating splitmode");
1136
1137 rb_event_delete(check_splitmode_ev);
1138 check_splitmode_ev = NULL;
1139 }
1140 }
1141 }
1142
1143
1144 /* allocate_topic()
1145 *
1146 * input - channel to allocate topic for
1147 * output - 1 on success, else 0
1148 * side effects - channel gets a topic allocated
1149 */
1150 static void
1151 allocate_topic(struct Channel *chptr)
1152 {
1153 void *ptr;
1154
1155 if(chptr == NULL)
1156 return;
1157
1158 ptr = rb_bh_alloc(topic_heap);
1159
1160 /* Basically we allocate one large block for the topic and
1161 * the topic info. We then split it up into two and shove it
1162 * in the chptr
1163 */
1164 chptr->topic = ptr;
1165 chptr->topic_info = (char *) ptr + TOPICLEN + 1;
1166 *chptr->topic = '\0';
1167 *chptr->topic_info = '\0';
1168 }
1169
1170 /* free_topic()
1171 *
1172 * input - channel which has topic to free
1173 * output -
1174 * side effects - channels topic is free'd
1175 */
1176 static void
1177 free_topic(struct Channel *chptr)
1178 {
1179 void *ptr;
1180
1181 if(chptr == NULL || chptr->topic == NULL)
1182 return;
1183
1184 /* This is safe for now - If you change allocate_topic you
1185 * MUST change this as well
1186 */
1187 ptr = chptr->topic;
1188 rb_bh_free(topic_heap, ptr);
1189 chptr->topic = NULL;
1190 chptr->topic_info = NULL;
1191 }
1192
1193 /* set_channel_topic()
1194 *
1195 * input - channel, topic to set, topic info and topic ts
1196 * output -
1197 * side effects - channels topic, topic info and TS are set.
1198 */
1199 void
1200 set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_info, time_t topicts)
1201 {
1202 if(strlen(topic) > 0)
1203 {
1204 if(chptr->topic == NULL)
1205 allocate_topic(chptr);
1206 rb_strlcpy(chptr->topic, topic, TOPICLEN + 1);
1207 rb_strlcpy(chptr->topic_info, topic_info, USERHOST_REPLYLEN);
1208 chptr->topic_time = topicts;
1209 }
1210 else
1211 {
1212 if(chptr->topic != NULL)
1213 free_topic(chptr);
1214 chptr->topic_time = 0;
1215 }
1216 }
1217
1218 /* has_common_channel()
1219 *
1220 * input - pointer to client
1221 * - pointer to another client
1222 * output - 1 if the two have a channel in common, 0 elsewise
1223 * side effects - none
1224 */
1225 int
1226 has_common_channel(struct Client *client1, struct Client *client2)
1227 {
1228 rb_dlink_node *ptr;
1229
1230 RB_DLINK_FOREACH(ptr, client1->user->channel.head)
1231 {
1232 if(IsMember(client2, ((struct membership *)ptr->data)->chptr))
1233 return 1;
1234 }
1235 return 0;
1236 }
1237
1238 /* channel_modes()
1239 *
1240 * inputs - pointer to channel
1241 * - pointer to client
1242 * output - string with simple modes
1243 * side effects - result from previous calls overwritten
1244 *
1245 * Stolen from ShadowIRCd 4 --nenolod
1246 */
1247 const char *
1248 channel_modes(struct Channel *chptr, struct Client *client_p)
1249 {
1250 int i;
1251 char buf1[BUFSIZE];
1252 char buf2[BUFSIZE];
1253 static char final[BUFSIZE];
1254 char *mbuf = buf1;
1255 char *pbuf = buf2;
1256
1257 *mbuf++ = '+';
1258 *pbuf = '\0';
1259
1260 for (i = 0; i < 256; i++)
1261 {
1262 if(chmode_table[i].set_func == chm_hidden && !IsOper(client_p) && IsClient(client_p))
1263 continue;
1264 if(chptr->mode.mode & chmode_flags[i])
1265 *mbuf++ = i;
1266 }
1267
1268 if(chptr->mode.limit)
1269 {
1270 *mbuf++ = 'l';
1271
1272 if(!IsClient(client_p) || IsMember(client_p, chptr))
1273 pbuf += rb_sprintf(pbuf, " %d", chptr->mode.limit);
1274 }
1275
1276 if(*chptr->mode.key)
1277 {
1278 *mbuf++ = 'k';
1279
1280 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1281 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.key);
1282 }
1283
1284 if(chptr->mode.join_num)
1285 {
1286 *mbuf++ = 'j';
1287
1288 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1289 pbuf += rb_sprintf(pbuf, " %d:%d", chptr->mode.join_num,
1290 chptr->mode.join_time);
1291 }
1292
1293 if(*chptr->mode.forward && (ConfigChannel.use_forward || !IsClient(client_p)))
1294 {
1295 *mbuf++ = 'f';
1296
1297 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1298 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.forward);
1299 }
1300
1301 *mbuf = '\0';
1302
1303 rb_strlcpy(final, buf1, sizeof final);
1304 rb_strlcat(final, buf2, sizeof final);
1305 return final;
1306 }
1307
1308 /* Now lets do some stuff to keep track of what combinations of
1309 * servers exist...
1310 * Note that the number of combinations doubles each time you add
1311 * something to this list. Each one is only quick if no servers use that
1312 * combination, but if the numbers get too high here MODE will get too
1313 * slow. I suggest if you get more than 7 here, you consider getting rid
1314 * of some and merging or something. If it wasn't for irc+cs we would
1315 * probably not even need to bother about most of these, but unfortunately
1316 * we do. -A1kmm
1317 */
1318
1319 /* void init_chcap_usage_counts(void)
1320 *
1321 * Inputs - none
1322 * Output - none
1323 * Side-effects - Initialises the usage counts to zero. Fills in the
1324 * chcap_yes and chcap_no combination tables.
1325 */
1326 void
1327 init_chcap_usage_counts(void)
1328 {
1329 unsigned long m, c, y, n;
1330
1331 memset(chcap_combos, 0, sizeof(chcap_combos));
1332
1333 /* For every possible combination */
1334 for (m = 0; m < NCHCAP_COMBOS; m++)
1335 {
1336 /* Check each capab */
1337 for (c = y = n = 0; c < NCHCAPS; c++)
1338 {
1339 if((m & (1 << c)) == 0)
1340 n |= channel_capabs[c];
1341 else
1342 y |= channel_capabs[c];
1343 }
1344 chcap_combos[m].cap_yes = y;
1345 chcap_combos[m].cap_no = n;
1346 }
1347 }
1348
1349 /* void set_chcap_usage_counts(struct Client *serv_p)
1350 * Input: serv_p; The client whose capabs to register.
1351 * Output: none
1352 * Side-effects: Increments the usage counts for the correct capab
1353 * combination.
1354 */
1355 void
1356 set_chcap_usage_counts(struct Client *serv_p)
1357 {
1358 int n;
1359
1360 for (n = 0; n < NCHCAP_COMBOS; n++)
1361 {
1362 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1363 NotCapable(serv_p, chcap_combos[n].cap_no))
1364 {
1365 chcap_combos[n].count++;
1366 return;
1367 }
1368 }
1369
1370 /* This should be impossible -A1kmm. */
1371 s_assert(0);
1372 }
1373
1374 /* void set_chcap_usage_counts(struct Client *serv_p)
1375 *
1376 * Inputs - serv_p; The client whose capabs to register.
1377 * Output - none
1378 * Side-effects - Decrements the usage counts for the correct capab
1379 * combination.
1380 */
1381 void
1382 unset_chcap_usage_counts(struct Client *serv_p)
1383 {
1384 int n;
1385
1386 for (n = 0; n < NCHCAP_COMBOS; n++)
1387 {
1388 if(IsCapable(serv_p, chcap_combos[n].cap_yes) &&
1389 NotCapable(serv_p, chcap_combos[n].cap_no))
1390 {
1391 /* Hopefully capabs can't change dynamically or anything... */
1392 s_assert(chcap_combos[n].count > 0);
1393
1394 if(chcap_combos[n].count > 0)
1395 chcap_combos[n].count--;
1396 return;
1397 }
1398 }
1399
1400 /* This should be impossible -A1kmm. */
1401 s_assert(0);
1402 }
1403
1404 /* void send_cap_mode_changes(struct Client *client_p,
1405 * struct Client *source_p,
1406 * struct Channel *chptr, int cap, int nocap)
1407 * Input: The client sending(client_p), the source client(source_p),
1408 * the channel to send mode changes for(chptr)
1409 * Output: None.
1410 * Side-effects: Sends the appropriate mode changes to capable servers.
1411 *
1412 * Reverted back to my original design, except that we now keep a count
1413 * of the number of servers which each combination as an optimisation, so
1414 * the capabs combinations which are not needed are not worked out. -A1kmm
1415 */
1416 void
1417 send_cap_mode_changes(struct Client *client_p, struct Client *source_p,
1418 struct Channel *chptr, struct ChModeChange mode_changes[], int mode_count)
1419 {
1420 static char modebuf[BUFSIZE];
1421 static char parabuf[BUFSIZE];
1422 int i, mbl, pbl, nc, mc, preflen, len;
1423 char *pbuf;
1424 const char *arg;
1425 int dir;
1426 int j;
1427 int cap;
1428 int nocap;
1429 int arglen;
1430
1431 /* Now send to servers... */
1432 for (j = 0; j < NCHCAP_COMBOS; j++)
1433 {
1434 if(chcap_combos[j].count == 0)
1435 continue;
1436
1437 mc = 0;
1438 nc = 0;
1439 pbl = 0;
1440 parabuf[0] = 0;
1441 pbuf = parabuf;
1442 dir = MODE_QUERY;
1443
1444 cap = chcap_combos[j].cap_yes;
1445 nocap = chcap_combos[j].cap_no;
1446
1447 mbl = preflen = rb_sprintf(modebuf, ":%s TMODE %ld %s ",
1448 use_id(source_p), (long) chptr->channelts,
1449 chptr->chname);
1450
1451 /* loop the list of - modes we have */
1452 for (i = 0; i < mode_count; i++)
1453 {
1454 /* if they dont support the cap we need, or they do support a cap they
1455 * cant have, then dont add it to the modebuf.. that way they wont see
1456 * the mode
1457 */
1458 if((mode_changes[i].letter == 0) ||
1459 ((cap & mode_changes[i].caps) != mode_changes[i].caps)
1460 || ((nocap & mode_changes[i].nocaps) != mode_changes[i].nocaps))
1461 continue;
1462
1463 if(!EmptyString(mode_changes[i].id))
1464 arg = mode_changes[i].id;
1465 else
1466 arg = mode_changes[i].arg;
1467
1468 if(arg)
1469 {
1470 arglen = strlen(arg);
1471
1472 /* dont even think about it! --fl */
1473 if(arglen > MODEBUFLEN - 5)
1474 continue;
1475 }
1476
1477 /* if we're creeping past the buf size, we need to send it and make
1478 * another line for the other modes
1479 * XXX - this could give away server topology with uids being
1480 * different lengths, but not much we can do, except possibly break
1481 * them as if they were the longest of the nick or uid at all times,
1482 * which even then won't work as we don't always know the uid -A1kmm.
1483 */
1484 if(arg && ((mc == MAXMODEPARAMSSERV) ||
1485 ((mbl + pbl + arglen + 4) > (BUFSIZE - 3))))
1486 {
1487 if(nc != 0)
1488 sendto_server(client_p, chptr, cap, nocap,
1489 "%s %s", modebuf, parabuf);
1490 nc = 0;
1491 mc = 0;
1492
1493 mbl = preflen;
1494 pbl = 0;
1495 pbuf = parabuf;
1496 parabuf[0] = 0;
1497 dir = MODE_QUERY;
1498 }
1499
1500 if(dir != mode_changes[i].dir)
1501 {
1502 modebuf[mbl++] = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
1503 dir = mode_changes[i].dir;
1504 }
1505
1506 modebuf[mbl++] = mode_changes[i].letter;
1507 modebuf[mbl] = 0;
1508 nc++;
1509
1510 if(arg != NULL)
1511 {
1512 len = rb_sprintf(pbuf, "%s ", arg);
1513 pbuf += len;
1514 pbl += len;
1515 mc++;
1516 }
1517 }
1518
1519 if(pbl && parabuf[pbl - 1] == ' ')
1520 parabuf[pbl - 1] = 0;
1521
1522 if(nc != 0)
1523 sendto_server(client_p, chptr, cap, nocap, "%s %s", modebuf, parabuf);
1524 }
1525 }
1526
1527 /* Check what we will forward to, without sending any notices to the user
1528 * -- jilles
1529 */
1530 struct Channel *
1531 check_forward(struct Client *source_p, struct Channel *chptr,
1532 char *key)
1533 {
1534 int depth = 0, i;
1535
1536 /* User is +Q */
1537 if (IsNoForward(source_p))
1538 return NULL;
1539
1540 while (depth < 16)
1541 {
1542 chptr = find_channel(chptr->mode.forward);
1543 /* Can only forward to existing channels */
1544 if (chptr == NULL)
1545 return NULL;
1546 /* Already on there, show original error message */
1547 if (IsMember(source_p, chptr))
1548 return NULL;
1549 /* Juped. Sending a warning notice would be unfair */
1550 if (hash_find_resv(chptr->chname))
1551 return NULL;
1552 /* Don't forward to +Q channel */
1553 if (chptr->mode.mode & MODE_DISFORWARD)
1554 return NULL;
1555 i = can_join(source_p, chptr, key);
1556 if (i == 0)
1557 return chptr;
1558 if (i != ERR_INVITEONLYCHAN && i != ERR_NEEDREGGEDNICK && i != ERR_THROTTLE && i != ERR_CHANNELISFULL)
1559 return NULL;
1560 depth++;
1561 }
1562
1563 return NULL;
1564 }
1565
1566 /*
1567 * do_join_0
1568 *
1569 * inputs - pointer to client doing join 0
1570 * output - NONE
1571 * side effects - Use has decided to join 0. This is legacy
1572 * from the days when channels were numbers not names. *sigh*
1573 */
1574 void
1575 do_join_0(struct Client *client_p, struct Client *source_p)
1576 {
1577 struct membership *msptr;
1578 struct Channel *chptr = NULL;
1579 rb_dlink_node *ptr;
1580
1581 /* Finish the flood grace period... */
1582 if(MyClient(source_p) && !IsFloodDone(source_p))
1583 flood_endgrace(source_p);
1584
1585 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s JOIN 0", use_id(source_p));
1586
1587 while((ptr = source_p->user->channel.head))
1588 {
1589 if(source_p->user->channel.head && MyConnect(source_p) &&
1590 !IsOper(source_p) && !IsExemptSpambot(source_p))
1591 check_spambot_warning(source_p, NULL);
1592
1593 msptr = ptr->data;
1594 chptr = msptr->chptr;
1595 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s",
1596 source_p->name,
1597 source_p->username, source_p->host, chptr->chname);
1598 remove_user_from_channel(msptr);
1599 }
1600 }
1601
1602 int
1603 check_channel_name_loc(struct Client *source_p, const char *name)
1604 {
1605 const char *p;
1606
1607 s_assert(name != NULL);
1608 if(EmptyString(name))
1609 return 0;
1610
1611 if(ConfigFileEntry.disable_fake_channels && !IsOper(source_p))
1612 {
1613 for(p = name; *p; ++p)
1614 {
1615 if(!IsChanChar(*p) || IsFakeChanChar(*p))
1616 return 0;
1617 }
1618 }
1619 else
1620 {
1621 for(p = name; *p; ++p)
1622 {
1623 if(!IsChanChar(*p))
1624 return 0;
1625 }
1626 }
1627
1628 if(ConfigChannel.only_ascii_channels)
1629 {
1630 for(p = name; *p; ++p)
1631 if(*p < 33 || *p > 126)
1632 return 0;
1633 }
1634
1635
1636 return 1;
1637 }
1638
1639 void user_join(struct Client * client_p, struct Client * source_p, const char * channels, const char * keys)
1640 {
1641 static char jbuf[BUFSIZE];
1642 struct Channel *chptr = NULL;
1643 struct ConfItem *aconf;
1644 char *name;
1645 char *key = NULL;
1646 const char *modes;
1647 int i, flags = 0;
1648 char *p = NULL, *p2 = NULL;
1649 char *chanlist;
1650 char *mykey;
1651
1652 jbuf[0] = '\0';
1653
1654 if(channels == NULL)
1655 return;
1656
1657 /* rebuild the list of channels theyre supposed to be joining.
1658 * this code has a side effect of losing keys, but..
1659 */
1660 chanlist = LOCAL_COPY(channels);
1661 for(name = rb_strtok_r(chanlist, ",", &p); name; name = rb_strtok_r(NULL, ",", &p))
1662 {
1663 /* check the length and name of channel is ok */
1664 if(!check_channel_name_loc(source_p, name) || (strlen(name) > LOC_CHANNELLEN))
1665 {
1666 sendto_one_numeric(source_p, ERR_BADCHANNAME,
1667 form_str(ERR_BADCHANNAME), (unsigned char *) name);
1668 continue;
1669 }
1670
1671 /* join 0 parts all channels */
1672 if(*name == '0' && (name[1] == ',' || name[1] == '\0') && name == chanlist)
1673 {
1674 (void) strcpy(jbuf, "0");
1675 continue;
1676 }
1677
1678 /* check it begins with # or &, and local chans are disabled */
1679 else if(!IsChannelName(name) ||
1680 ( !ConfigChannel.use_local_channels && name[0] == '&'))
1681 {
1682 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
1683 form_str(ERR_NOSUCHCHANNEL), name);
1684 continue;
1685 }
1686
1687 /* see if its resv'd */
1688 if(!IsExemptResv(source_p) && (aconf = hash_find_resv(name)))
1689 {
1690 sendto_one_numeric(source_p, ERR_BADCHANNAME,
1691 form_str(ERR_BADCHANNAME), name);
1692
1693 /* dont warn for opers */
1694 if(!IsExemptJupe(source_p) && !IsOper(source_p))
1695 sendto_realops_snomask(SNO_SPY, L_NETWIDE,
1696 "User %s (%s@%s) is attempting to join locally juped channel %s (%s)",
1697 source_p->name, source_p->username,
1698 source_p->orighost, name, aconf->passwd);
1699 /* dont update tracking for jupe exempt users, these
1700 * are likely to be spamtrap leaves
1701 */
1702 else if(IsExemptJupe(source_p))
1703 aconf->port--;
1704
1705 continue;
1706 }
1707
1708 if(splitmode && !IsOper(source_p) && (*name != '&') &&
1709 ConfigChannel.no_join_on_split)
1710 {
1711 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1712 me.name, source_p->name, name);
1713 continue;
1714 }
1715
1716 if(*jbuf)
1717 (void) strcat(jbuf, ",");
1718 (void) rb_strlcat(jbuf, name, sizeof(jbuf));
1719 }
1720
1721 if(keys != NULL)
1722 {
1723 mykey = LOCAL_COPY(keys);
1724 key = rb_strtok_r(mykey, ",", &p2);
1725 }
1726
1727 for(name = rb_strtok_r(jbuf, ",", &p); name;
1728 key = (key) ? rb_strtok_r(NULL, ",", &p2) : NULL, name = rb_strtok_r(NULL, ",", &p))
1729 {
1730 hook_data_channel_activity hook_info;
1731
1732 /* JOIN 0 simply parts all channels the user is in */
1733 if(*name == '0' && !atoi(name))
1734 {
1735 if(source_p->user->channel.head == NULL)
1736 continue;
1737
1738 do_join_0(&me, source_p);
1739 continue;
1740 }
1741
1742 /* look for the channel */
1743 if((chptr = find_channel(name)) != NULL)
1744 {
1745 if(IsMember(source_p, chptr))
1746 continue;
1747
1748 flags = 0;
1749 }
1750 else
1751 {
1752 hook_data_client_approval moduledata;
1753
1754 moduledata.client = source_p;
1755 moduledata.approved = 0;
1756
1757 call_hook(h_can_create_channel, &moduledata);
1758
1759 if(moduledata.approved != 0)
1760 {
1761 sendto_one(source_p, form_str(moduledata.approved),
1762 me.name, source_p->name, name);
1763 continue;
1764 }
1765
1766 if(splitmode && !IsOper(source_p) && (*name != '&') &&
1767 ConfigChannel.no_create_on_split)
1768 {
1769 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1770 me.name, source_p->name, name);
1771 continue;
1772 }
1773
1774 flags = CHFL_CHANOP;
1775 }
1776
1777 if((rb_dlink_list_length(&source_p->user->channel) >=
1778 (unsigned long) ConfigChannel.max_chans_per_user) &&
1779 (!IsOper(source_p) ||
1780 (rb_dlink_list_length(&source_p->user->channel) >=
1781 (unsigned long) ConfigChannel.max_chans_per_user * 3)))
1782 {
1783 sendto_one(source_p, form_str(ERR_TOOMANYCHANNELS),
1784 me.name, source_p->name, name);
1785 return;
1786 }
1787
1788 if(chptr == NULL) /* If I already have a chptr, no point doing this */
1789 {
1790 chptr = get_or_create_channel(source_p, name, NULL);
1791
1792 if(chptr == NULL)
1793 {
1794 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
1795 me.name, source_p->name, name);
1796 continue;
1797 }
1798 }
1799
1800 /* can_join checks for +i key, bans etc */
1801 if((i = can_join(source_p, chptr, key)))
1802 {
1803 if ((i != ERR_NEEDREGGEDNICK && i != ERR_THROTTLE && i != ERR_INVITEONLYCHAN && i != ERR_CHANNELISFULL) ||
1804 (!ConfigChannel.use_forward || (chptr = check_forward(source_p, chptr, key)) == NULL))
1805 {
1806 /* might be wrong, but is there any other better location for such?
1807 * see extensions/chm_operonly.c for other comments on this
1808 * -- dwr
1809 */
1810 if(i != ERR_CUSTOM)
1811 sendto_one(source_p, form_str(i), me.name, source_p->name, name);
1812
1813 continue;
1814 }
1815
1816 sendto_one_numeric(source_p, ERR_LINKCHANNEL, form_str(ERR_LINKCHANNEL), name, chptr->chname);
1817 }
1818
1819 if(flags == 0 &&
1820 !IsOper(source_p) && !IsExemptSpambot(source_p))
1821 check_spambot_warning(source_p, name);
1822
1823 /* add the user to the channel */
1824 add_user_to_channel(chptr, source_p, flags);
1825 if (chptr->mode.join_num &&
1826 rb_current_time() - chptr->join_delta >= chptr->mode.join_time)
1827 {
1828 chptr->join_count = 0;
1829 chptr->join_delta = rb_current_time();
1830 }
1831 chptr->join_count++;
1832
1833 /* we send the user their join here, because we could have to
1834 * send a mode out next.
1835 */
1836 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s JOIN :%s",
1837 source_p->name,
1838 source_p->username, source_p->host, chptr->chname);
1839
1840 /* its a new channel, set +nt and burst. */
1841 if(flags & CHFL_CHANOP)
1842 {
1843 chptr->channelts = rb_current_time();
1844
1845 /* autochanmodes stuff */
1846 if(ConfigChannel.autochanmodes)
1847 {
1848 char * ch;
1849 for(ch = ConfigChannel.autochanmodes; *ch; *ch++)
1850 {
1851 chptr->mode.mode |= chmode_table[*ch].mode_type;
1852 }
1853 }
1854 else
1855 {
1856 chptr->mode.mode |= MODE_TOPICLIMIT;
1857 chptr->mode.mode |= MODE_NOPRIVMSGS;
1858 }
1859
1860 modes = channel_modes(chptr, &me);
1861
1862 sendto_channel_local(ONLY_CHANOPS, chptr, ":%s MODE %s %s",
1863 me.name, chptr->chname, modes);
1864
1865 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
1866 ":%s SJOIN %ld %s %s :@%s",
1867 me.id, (long) chptr->channelts,
1868 chptr->chname, modes, source_p->id);
1869 }
1870 else
1871 {
1872 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
1873 ":%s JOIN %ld %s +",
1874 use_id(source_p), (long) chptr->channelts,
1875 chptr->chname);
1876 }
1877
1878 del_invite(chptr, source_p);
1879
1880 if(chptr->topic != NULL)
1881 {
1882 sendto_one(source_p, form_str(RPL_TOPIC), me.name,
1883 source_p->name, chptr->chname, chptr->topic);
1884
1885 sendto_one(source_p, form_str(RPL_TOPICWHOTIME),
1886 me.name, source_p->name, chptr->chname,
1887 chptr->topic_info, chptr->topic_time);
1888 }
1889
1890 channel_member_names(chptr, source_p, 1);
1891
1892 hook_info.client = source_p;
1893 hook_info.chptr = chptr;
1894 hook_info.key = key;
1895 call_hook(h_channel_join, &hook_info);
1896 }
1897
1898 return;
1899 }
1900
1901 /*
1902 * channel_metadata_add
1903 *
1904 * inputs - pointer to channel struct
1905 * - name of metadata item you wish to add
1906 * - value of metadata item
1907 * - 1 if metadata should be propegated, 0 if not
1908 * output - none
1909 * side effects - metadata is added to the channel in question
1910 * - metadata is propegated if propegate is set.
1911 */
1912 struct Metadata *
1913 channel_metadata_add(struct Channel *target, const char *name, const char *value, int propegate)
1914 {
1915 struct Metadata *md;
1916
1917 if(irc_dictionary_find(target->c_metadata, name) != NULL)
1918 return NULL;
1919
1920 md = rb_malloc(sizeof(struct Metadata));
1921 md->name = rb_strdup(name);
1922 md->value = rb_strdup(value);
1923
1924 irc_dictionary_add(target->c_metadata, md->name, md);
1925
1926 if(propegate)
1927 sendto_match_servs(&me, "*", CAP_ENCAP, NOCAPS, "ENCAP * METADATA ADD %s %s :%s",
1928 target->chname, name, value);
1929
1930 return md;
1931 }
1932
1933 /*
1934 * channel_metadata_delete
1935 *
1936 * inputs - pointer to channel struct
1937 * - name of metadata item you wish to delete
1938 * output - none
1939 * side effects - metadata is deleted from the channel in question
1940 * - deletion is propegated if propegate is set
1941 */
1942 void
1943 channel_metadata_delete(struct Channel *target, const char *name, int propegate)
1944 {
1945 struct Metadata *md = channel_metadata_find(target, name);
1946
1947 if(!md)
1948 return;
1949
1950 irc_dictionary_delete(target->c_metadata, md->name);
1951
1952 rb_free(md);
1953
1954 if(propegate)
1955 sendto_match_servs(&me, "*", CAP_ENCAP, NOCAPS, "ENCAP * METADATA DELETE %s %s",
1956 target->chname, name);
1957 }
1958
1959 /*
1960 * channel_metadata_find
1961 *
1962 * inputs - pointer to channel struct
1963 * - name of metadata item you wish to read
1964 * output - the requested metadata, if it exists, elsewise null.
1965 * side effects -
1966 */
1967 struct Metadata *
1968 channel_metadata_find(struct Channel *target, const char *name)
1969 {
1970 if(!target)
1971 return NULL;
1972
1973 if(!target->c_metadata)
1974 return NULL;
1975
1976 return irc_dictionary_retrieve(target->c_metadata, name);
1977 }