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