]> jfr.im git - irc/rqf/shadowircd.git/blob - src/channel.c
Rework ircd-side MLOCK enforcement: instead of trying to track modes locked on or...
[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_free(chptr->mode_lock);
146 rb_bh_free(channel_heap, chptr);
147 }
148
149 struct Ban *
150 allocate_ban(const char *banstr, const char *who)
151 {
152 struct Ban *bptr;
153 bptr = rb_bh_alloc(ban_heap);
154 bptr->banstr = rb_strdup(banstr);
155 bptr->who = rb_strdup(who);
156
157 return (bptr);
158 }
159
160 void
161 free_ban(struct Ban *bptr)
162 {
163 rb_free(bptr->banstr);
164 rb_free(bptr->who);
165 rb_bh_free(ban_heap, bptr);
166 }
167
168
169 /* find_channel_membership()
170 *
171 * input - channel to find them in, client to find
172 * output - membership of client in channel, else NULL
173 * side effects -
174 */
175 struct membership *
176 find_channel_membership(struct Channel *chptr, struct Client *client_p)
177 {
178 struct membership *msptr;
179 rb_dlink_node *ptr;
180
181 if(!IsClient(client_p))
182 return NULL;
183
184 /* Pick the most efficient list to use to be nice to things like
185 * CHANSERV which could be in a large number of channels
186 */
187 if(rb_dlink_list_length(&chptr->members) < rb_dlink_list_length(&client_p->user->channel))
188 {
189 RB_DLINK_FOREACH(ptr, chptr->members.head)
190 {
191 msptr = ptr->data;
192
193 if(msptr->client_p == client_p)
194 return msptr;
195 }
196 }
197 else
198 {
199 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
200 {
201 msptr = ptr->data;
202
203 if(msptr->chptr == chptr)
204 return msptr;
205 }
206 }
207
208 return NULL;
209 }
210
211 /* find_channel_status()
212 *
213 * input - membership to get status for, whether we can combine flags
214 * output - flags of user on channel
215 * side effects -
216 */
217 const char *
218 find_channel_status(struct membership *msptr, int combine)
219 {
220 static char buffer[5];
221 char *p;
222
223 p = buffer;
224
225 if(is_admin(msptr))
226 {
227 if(!combine)
228 return "!";
229 *p++ = '!';
230 }
231
232 if(is_chanop(msptr))
233 {
234 if(!combine)
235 return "@";
236 *p++ = '@';
237 }
238
239 if(is_halfop(msptr))
240 {
241 if(!combine)
242 return "%";
243 *p++ = '%';
244 }
245
246 if(is_voiced(msptr))
247 *p++ = '+';
248
249 *p = '\0';
250 return buffer;
251 }
252
253 /* is_halfop()
254 *
255 * input - membership to check for halfops
256 * output - 1 if the user is halfopped, 0 if the user is not or halfop
257 * is disabled.
258 * side effects -
259 *
260 */
261 int
262 is_halfop(struct membership *msptr)
263 {
264 if(!ConfigChannel.use_halfop)
265 return 0;
266 if(is_chmode_h(msptr))
267 return 1;
268 else
269 return 0;
270 }
271
272 /* is_admin()
273 *
274 * input - membership to check for admin
275 * output - 1 if the user is an admin, 0 if the user is not or admin
276 * is disabled.
277 * side effects -
278 *
279 */
280 int
281 is_admin(struct membership *msptr)
282 {
283 if(!ConfigChannel.use_admin)
284 return 0;
285 if(is_chmode_a(msptr))
286 return 1;
287 else
288 return 0;
289 }
290
291 /* is_any_op()
292 *
293 * input - membership to check for ops
294 * output - 1 if the user is op, halfop, or admin, 0 elsewise
295 * side effects -
296 */
297 int
298 is_any_op(struct membership *msptr)
299 {
300 if(is_chanop(msptr) || is_halfop(msptr) || is_admin(msptr))
301 return 1;
302 else
303 return 0;
304 }
305
306 /* is_chanop_voiced()
307 *
308 * input - memebership to check for status
309 * output - 1 if the user is op, halfop, admin, or voice, 0 elsewise
310 * side effects -
311 */
312 int
313 is_chanop_voiced(struct membership *msptr)
314 {
315 if(is_chanop(msptr) || is_voiced(msptr) || is_halfop(msptr) || is_admin(msptr))
316 return 1;
317 else
318 return 0;
319 }
320
321 /* can_kick_deop()
322 *
323 * input - two memeberships
324 * output - 1 if the first memebership can kick/deop the second, 0 elsewise
325 * side effects -
326 */
327 int
328 can_kick_deop(struct membership *source, struct membership *target)
329 {
330 if(is_chanop(source) && !is_admin(target))
331 return 1;
332 else if(is_halfop(source) && !is_any_op(target))
333 return 1;
334 else if(is_admin(source))
335 return 1;
336
337 return 0;
338 }
339
340 /* add_user_to_channel()
341 *
342 * input - channel to add client to, client to add, channel flags
343 * output -
344 * side effects - user is added to channel
345 */
346 void
347 add_user_to_channel(struct Channel *chptr, struct Client *client_p, int flags)
348 {
349 struct membership *msptr;
350
351 s_assert(client_p->user != NULL);
352 if(client_p->user == NULL)
353 return;
354
355 msptr = rb_bh_alloc(member_heap);
356
357 msptr->chptr = chptr;
358 msptr->client_p = client_p;
359 msptr->flags = flags;
360
361 rb_dlinkAdd(msptr, &msptr->usernode, &client_p->user->channel);
362 rb_dlinkAdd(msptr, &msptr->channode, &chptr->members);
363
364 if(MyClient(client_p))
365 rb_dlinkAdd(msptr, &msptr->locchannode, &chptr->locmembers);
366 }
367
368 /* remove_user_from_channel()
369 *
370 * input - membership pointer to remove from channel
371 * output -
372 * side effects - membership (thus user) is removed from channel
373 */
374 void
375 remove_user_from_channel(struct membership *msptr)
376 {
377 struct Client *client_p;
378 struct Channel *chptr;
379 s_assert(msptr != NULL);
380 if(msptr == NULL)
381 return;
382
383 client_p = msptr->client_p;
384 chptr = msptr->chptr;
385
386 rb_dlinkDelete(&msptr->usernode, &client_p->user->channel);
387 rb_dlinkDelete(&msptr->channode, &chptr->members);
388
389 if(client_p->servptr == &me)
390 rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers);
391
392 if(!(chptr->mode.mode & ModuleModes.MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0)
393 destroy_channel(chptr);
394
395 rb_bh_free(member_heap, msptr);
396
397 return;
398 }
399
400 /* remove_user_from_channels()
401 *
402 * input - user to remove from all channels
403 * output -
404 * side effects - user is removed from all channels
405 */
406 void
407 remove_user_from_channels(struct Client *client_p)
408 {
409 struct Channel *chptr;
410 struct membership *msptr;
411 rb_dlink_node *ptr;
412 rb_dlink_node *next_ptr;
413
414 if(client_p == NULL)
415 return;
416
417 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->user->channel.head)
418 {
419 msptr = ptr->data;
420 chptr = msptr->chptr;
421
422 rb_dlinkDelete(&msptr->channode, &chptr->members);
423
424 if(client_p->servptr == &me)
425 rb_dlinkDelete(&msptr->locchannode, &chptr->locmembers);
426
427 if(!(chptr->mode.mode & ModuleModes.MODE_PERMANENT) && rb_dlink_list_length(&chptr->members) <= 0)
428 destroy_channel(chptr);
429
430 rb_bh_free(member_heap, msptr);
431 }
432
433 client_p->user->channel.head = client_p->user->channel.tail = NULL;
434 client_p->user->channel.length = 0;
435 }
436
437 /* invalidate_bancache_user()
438 *
439 * input - user to invalidate ban cache for
440 * output -
441 * side effects - ban cache is invalidated for all memberships of that user
442 * to be used after a nick change
443 */
444 void
445 invalidate_bancache_user(struct Client *client_p)
446 {
447 struct membership *msptr;
448 rb_dlink_node *ptr;
449
450 if(client_p == NULL)
451 return;
452
453 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
454 {
455 msptr = ptr->data;
456 msptr->bants = 0;
457 msptr->flags &= ~CHFL_BANNED;
458 }
459 }
460
461 /* check_channel_name()
462 *
463 * input - channel name
464 * output - 1 if valid channel name, else 0
465 * side effects -
466 */
467 int
468 check_channel_name(const char *name)
469 {
470 s_assert(name != NULL);
471 if(name == NULL)
472 return 0;
473
474 for (; *name; ++name)
475 {
476 if(!IsChanChar(*name))
477 return 0;
478 }
479
480 return 1;
481 }
482
483 /* free_channel_list()
484 *
485 * input - rb_dlink list to free
486 * output -
487 * side effects - list of b/e/I modes is cleared
488 */
489 void
490 free_channel_list(rb_dlink_list * list)
491 {
492 rb_dlink_node *ptr;
493 rb_dlink_node *next_ptr;
494 struct Ban *actualBan;
495
496 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
497 {
498 actualBan = ptr->data;
499 free_ban(actualBan);
500 }
501
502 list->head = list->tail = NULL;
503 list->length = 0;
504 }
505
506 /* destroy_channel()
507 *
508 * input - channel to destroy
509 * output -
510 * side effects - channel is obliterated
511 */
512 void
513 destroy_channel(struct Channel *chptr)
514 {
515 rb_dlink_node *ptr, *next_ptr;
516
517 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->invites.head)
518 {
519 del_invite(chptr, ptr->data);
520 }
521
522 /* free all bans/exceptions/denies */
523 free_channel_list(&chptr->banlist);
524 free_channel_list(&chptr->exceptlist);
525 free_channel_list(&chptr->invexlist);
526 free_channel_list(&chptr->quietlist);
527
528 /* Free the topic */
529 free_topic(chptr);
530
531 rb_dlinkDelete(&chptr->node, &global_channel_list);
532 del_from_channel_hash(chptr->chname, chptr);
533 free_channel(chptr);
534 }
535
536 /* channel_pub_or_secret()
537 *
538 * input - channel
539 * output - "=" if public, "@" if secret, else "*"
540 * side effects -
541 */
542 static const char *
543 channel_pub_or_secret(struct Channel *chptr)
544 {
545 if(PubChannel(chptr))
546 return ("=");
547 else if(SecretChannel(chptr))
548 return ("@");
549 return ("*");
550 }
551
552 /* channel_member_names()
553 *
554 * input - channel to list, client to list to, show endofnames
555 * output -
556 * side effects - client is given list of users on channel
557 */
558 void
559 channel_member_names(struct Channel *chptr, struct Client *client_p, int show_eon)
560 {
561 struct membership *msptr;
562 struct Client *target_p;
563 rb_dlink_node *ptr;
564 char lbuf[BUFSIZE];
565 char *t;
566 int mlen;
567 int tlen;
568 int cur_len;
569 int is_member;
570 int stack = IsCapable(client_p, CLICAP_MULTI_PREFIX);
571
572 if(ShowChannel(client_p, chptr))
573 {
574 is_member = IsMember(client_p, chptr);
575
576 cur_len = mlen = rb_sprintf(lbuf, form_str(RPL_NAMREPLY),
577 me.name, client_p->name,
578 channel_pub_or_secret(chptr), chptr->chname);
579
580 t = lbuf + cur_len;
581
582 RB_DLINK_FOREACH(ptr, chptr->members.head)
583 {
584 msptr = ptr->data;
585 target_p = msptr->client_p;
586
587 if(IsInvisible(target_p) && !is_member)
588 continue;
589
590 /* space, possible "@+" prefix */
591 if(cur_len + strlen(target_p->name) + 3 >= BUFSIZE - 3)
592 {
593 *(t - 1) = '\0';
594 sendto_one(client_p, "%s", lbuf);
595 cur_len = mlen;
596 t = lbuf + mlen;
597 }
598
599 tlen = rb_sprintf(t, "%s%s ", find_channel_status(msptr, stack),
600 target_p->name);
601
602 cur_len += tlen;
603 t += tlen;
604 }
605
606 /* The old behaviour here was to always output our buffer,
607 * even if there are no clients we can show. This happens
608 * when a client does "NAMES" with no parameters, and all
609 * the clients on a -sp channel are +i. I dont see a good
610 * reason for keeping that behaviour, as it just wastes
611 * bandwidth. --anfl
612 */
613 if(cur_len != mlen)
614 {
615 *(t - 1) = '\0';
616 sendto_one(client_p, "%s", lbuf);
617 }
618 }
619
620 if(show_eon)
621 sendto_one(client_p, form_str(RPL_ENDOFNAMES),
622 me.name, client_p->name, chptr->chname);
623 }
624
625 /* del_invite()
626 *
627 * input - channel to remove invite from, client to remove
628 * output -
629 * side effects - user is removed from invite list, if exists
630 */
631 void
632 del_invite(struct Channel *chptr, struct Client *who)
633 {
634 rb_dlinkFindDestroy(who, &chptr->invites);
635 rb_dlinkFindDestroy(chptr, &who->user->invited);
636 }
637
638 /* is_banned()
639 *
640 * input - channel to check bans for, user to check bans against
641 * optional prebuilt buffers
642 * output - 1 if banned, else 0
643 * side effects -
644 */
645 int
646 is_banned(struct Channel *chptr, struct Client *who, struct membership *msptr,
647 const char *s, const char *s2)
648 {
649 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
650 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
651 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
652 char *s3 = NULL;
653 rb_dlink_node *ptr;
654 struct Ban *actualBan = NULL;
655 struct Ban *actualExcept = NULL;
656
657 if(!MyClient(who))
658 return 0;
659
660 /* if the buffers havent been built, do it here */
661 if(s == NULL)
662 {
663 rb_sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host);
664 rb_sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost);
665
666 s = src_host;
667 s2 = src_iphost;
668 }
669 if(who->localClient->mangledhost != NULL)
670 {
671 /* if host mangling mode enabled, also check their real host */
672 if(!strcmp(who->host, who->localClient->mangledhost))
673 {
674 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost);
675 s3 = src_althost;
676 }
677 /* if host mangling mode not enabled and no other spoof,
678 * also check the mangled form of their host */
679 else if (!IsDynSpoof(who))
680 {
681 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost);
682 s3 = src_althost;
683 }
684 }
685
686 RB_DLINK_FOREACH(ptr, chptr->banlist.head)
687 {
688 actualBan = ptr->data;
689 if(match(actualBan->banstr, s) ||
690 match(actualBan->banstr, s2) ||
691 match_cidr(actualBan->banstr, s2) ||
692 match_extban(actualBan->banstr, who, chptr, CHFL_BAN) ||
693 (s3 != NULL && match(actualBan->banstr, s3)))
694 break;
695 else
696 actualBan = NULL;
697 }
698
699 if((actualBan != NULL) && ConfigChannel.use_except)
700 {
701 RB_DLINK_FOREACH(ptr, chptr->exceptlist.head)
702 {
703 actualExcept = ptr->data;
704
705 /* theyre exempted.. */
706 if(match(actualExcept->banstr, s) ||
707 match(actualExcept->banstr, s2) ||
708 match_cidr(actualExcept->banstr, s2) ||
709 match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) ||
710 (s3 != NULL && match(actualExcept->banstr, s3)))
711 {
712 /* cache the fact theyre not banned */
713 if(msptr != NULL)
714 {
715 msptr->bants = chptr->bants;
716 msptr->flags &= ~CHFL_BANNED;
717 }
718
719 return CHFL_EXCEPTION;
720 }
721 }
722 }
723
724 /* cache the banned/not banned status */
725 if(msptr != NULL)
726 {
727 msptr->bants = chptr->bants;
728
729 if(actualBan != NULL)
730 {
731 msptr->flags |= CHFL_BANNED;
732 return CHFL_BAN;
733 }
734 else
735 {
736 msptr->flags &= ~CHFL_BANNED;
737 return 0;
738 }
739 }
740
741 return ((actualBan ? CHFL_BAN : 0));
742 }
743
744 /* is_quieted()
745 *
746 * input - channel to check bans for, user to check bans against
747 * optional prebuilt buffers
748 * output - 1 if banned, else 0
749 * side effects -
750 */
751 int
752 is_quieted(struct Channel *chptr, struct Client *who, struct membership *msptr,
753 const char *s, const char *s2)
754 {
755 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
756 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
757 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
758 char *s3 = NULL;
759 rb_dlink_node *ptr;
760 struct Ban *actualBan = NULL;
761 struct Ban *actualExcept = NULL;
762
763 /* check to make sure quiets even exist on this server first */
764 if(ModuleModes.CHFL_QUIET == 0)
765 return 0;
766
767 if(!MyClient(who))
768 return 0;
769
770 /* if the buffers havent been built, do it here */
771 if(s == NULL)
772 {
773 rb_sprintf(src_host, "%s!%s@%s", who->name, who->username, who->host);
774 rb_sprintf(src_iphost, "%s!%s@%s", who->name, who->username, who->sockhost);
775
776 s = src_host;
777 s2 = src_iphost;
778 }
779 if(who->localClient->mangledhost != NULL)
780 {
781 /* if host mangling mode enabled, also check their real host */
782 if(!strcmp(who->host, who->localClient->mangledhost))
783 {
784 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->orighost);
785 s3 = src_althost;
786 }
787 /* if host mangling mode not enabled and no other spoof,
788 * also check the mangled form of their host */
789 else if (!IsDynSpoof(who))
790 {
791 rb_sprintf(src_althost, "%s!%s@%s", who->name, who->username, who->localClient->mangledhost);
792 s3 = src_althost;
793 }
794 }
795
796 RB_DLINK_FOREACH(ptr, chptr->quietlist.head)
797 {
798 actualBan = ptr->data;
799 if(match(actualBan->banstr, s) ||
800 match(actualBan->banstr, s2) ||
801 match_cidr(actualBan->banstr, s2) ||
802 match_extban(actualBan->banstr, who, chptr, ModuleModes.CHFL_QUIET) ||
803 (s3 != NULL && match(actualBan->banstr, s3)))
804 break;
805 else
806 actualBan = NULL;
807 }
808
809 if((actualBan != NULL) && ConfigChannel.use_except)
810 {
811 RB_DLINK_FOREACH(ptr, chptr->exceptlist.head)
812 {
813 actualExcept = ptr->data;
814
815 /* theyre exempted.. */
816 if(match(actualExcept->banstr, s) ||
817 match(actualExcept->banstr, s2) ||
818 match_cidr(actualExcept->banstr, s2) ||
819 match_extban(actualExcept->banstr, who, chptr, CHFL_EXCEPTION) ||
820 (s3 != NULL && match(actualExcept->banstr, s3)))
821 {
822 /* cache the fact theyre not banned */
823 if(msptr != NULL)
824 {
825 msptr->bants = chptr->bants;
826 msptr->flags &= ~CHFL_BANNED;
827 }
828
829 return CHFL_EXCEPTION;
830 }
831 }
832 }
833
834 /* cache the banned/not banned status */
835 if(msptr != NULL)
836 {
837 msptr->bants = chptr->bants;
838
839 if(actualBan != NULL)
840 {
841 msptr->flags |= CHFL_BANNED;
842 return CHFL_BAN;
843 }
844 else
845 {
846 msptr->flags &= ~CHFL_BANNED;
847 return 0;
848 }
849 }
850
851 return ((actualBan ? CHFL_BAN : 0));
852 }
853
854 /* can_join()
855 *
856 * input - client to check, channel to check for, key
857 * output - reason for not being able to join, else 0
858 * side effects -
859 */
860 int
861 can_join(struct Client *source_p, struct Channel *chptr, char *key)
862 {
863 rb_dlink_node *invite = NULL;
864 rb_dlink_node *ptr;
865 struct Ban *invex = NULL;
866 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
867 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
868 char src_althost[NICKLEN + USERLEN + HOSTLEN + 6];
869 char text[10];
870 int use_althost = 0;
871 int i = 0;
872 hook_data_channel moduledata;
873 struct Metadata *md;
874 struct DictionaryIter iter;
875
876 s_assert(source_p->localClient != NULL);
877
878 rb_sprintf(src_host, "%s!%s@%s", source_p->name, source_p->username, source_p->host);
879 rb_sprintf(src_iphost, "%s!%s@%s", source_p->name, source_p->username, source_p->sockhost);
880 if(source_p->localClient->mangledhost != NULL)
881 {
882 /* if host mangling mode enabled, also check their real host */
883 if(!strcmp(source_p->host, source_p->localClient->mangledhost))
884 {
885 rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->orighost);
886 use_althost = 1;
887 }
888 /* if host mangling mode not enabled and no other spoof,
889 * also check the mangled form of their host */
890 else if (!IsDynSpoof(source_p))
891 {
892 rb_sprintf(src_althost, "%s!%s@%s", source_p->name, source_p->username, source_p->localClient->mangledhost);
893 use_althost = 1;
894 }
895 }
896
897 if((is_banned(chptr, source_p, NULL, src_host, src_iphost)) == CHFL_BAN)
898 return (ERR_BANNEDFROMCHAN);
899
900 rb_snprintf(text, sizeof(text), "K%s", source_p->id);
901
902 DICTIONARY_FOREACH(md, &iter, chptr->metadata)
903 {
904 if(!strcmp(md->value, "KICKNOREJOIN") && !strcmp(md->name, text) && (md->timevalue + 2 > rb_current_time()))
905 return ERR_KICKNOREJOIN;
906 /* cleanup any stale KICKNOREJOIN metadata we find while we're at it */
907 if(!strcmp(md->value, "KICKNOREJOIN") && !(md->timevalue + 2 > rb_current_time()))
908 channel_metadata_delete(chptr, md->name, 0);
909 }
910
911 if(chptr->mode.mode & MODE_INVITEONLY)
912 {
913 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
914 {
915 if(invite->data == chptr)
916 break;
917 }
918 if(invite == NULL)
919 {
920 if(!ConfigChannel.use_invex)
921 return (ERR_INVITEONLYCHAN);
922 RB_DLINK_FOREACH(ptr, chptr->invexlist.head)
923 {
924 invex = ptr->data;
925 if(match(invex->banstr, src_host)
926 || match(invex->banstr, src_iphost)
927 || match_cidr(invex->banstr, src_iphost)
928 || match_extban(invex->banstr, source_p, chptr, CHFL_INVEX)
929 || (use_althost && match(invex->banstr, src_althost)))
930 break;
931 }
932 if(ptr == NULL)
933 return (ERR_INVITEONLYCHAN);
934 }
935 }
936
937 if(*chptr->mode.key && (EmptyString(key) || irccmp(chptr->mode.key, key)))
938 return (ERR_BADCHANNELKEY);
939
940 if(chptr->mode.limit &&
941 rb_dlink_list_length(&chptr->members) >= (unsigned long) chptr->mode.limit)
942 i = ERR_CHANNELISFULL;
943 if(chptr->mode.mode & ModuleModes.MODE_REGONLY && EmptyString(source_p->user->suser))
944 i = ERR_NEEDREGGEDNICK;
945 /* join throttling stuff --nenolod */
946 /* only check for throttles if they exist on this server --Taros */
947 else if(ModuleModes.MODE_THROTTLE)
948 {
949 if(chptr->mode.join_num > 0 && chptr->mode.join_time > 0)
950 {
951 if ((rb_current_time() - chptr->join_delta <=
952 chptr->mode.join_time) && (chptr->join_count >=
953 chptr->mode.join_num))
954 i = ERR_THROTTLE;
955 }
956 }
957
958 /* allow /invite to override +l/+r/+j also -- jilles */
959 if (i != 0 && invite == NULL)
960 {
961 RB_DLINK_FOREACH(invite, source_p->user->invited.head)
962 {
963 if(invite->data == chptr)
964 break;
965 }
966 if (invite == NULL)
967 return i;
968 }
969
970 moduledata.client = source_p;
971 moduledata.chptr = chptr;
972 moduledata.approved = 0;
973
974 call_hook(h_can_join, &moduledata);
975
976 return moduledata.approved;
977 }
978
979 /* can_send()
980 *
981 * input - user to check in channel, membership pointer
982 * output - whether can explicitly send or not, else CAN_SEND_NONOP
983 * side effects -
984 */
985 int
986 can_send(struct Channel *chptr, struct Client *source_p, struct membership *msptr)
987 {
988 if(IsServer(source_p) || IsService(source_p))
989 return CAN_SEND_OPV;
990
991 if(MyClient(source_p) && hash_find_resv(chptr->chname) &&
992 !IsOper(source_p) && !IsExemptResv(source_p))
993 return CAN_SEND_NO;
994
995 if(msptr == NULL)
996 {
997 msptr = find_channel_membership(chptr, source_p);
998
999 if(msptr == NULL)
1000 {
1001 /* if its +m or +n and theyre not in the channel,
1002 * they cant send. we dont check bans here because
1003 * theres no possibility of caching them --fl
1004 */
1005 if(chptr->mode.mode & MODE_NOPRIVMSGS || chptr->mode.mode & MODE_MODERATED)
1006 return CAN_SEND_NO;
1007 else
1008 return CAN_SEND_NONOP;
1009 }
1010 }
1011
1012 if(is_chanop_voiced(msptr))
1013 return CAN_SEND_OPV;
1014
1015 if(chptr->mode.mode & MODE_MODERATED)
1016 return CAN_SEND_NO;
1017
1018 if(MyClient(source_p))
1019 {
1020 /* cached can_send */
1021 if(msptr->bants == chptr->bants)
1022 {
1023 if(can_send_banned(msptr))
1024 return CAN_SEND_NO;
1025 }
1026 else if(is_banned(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN
1027 || is_quieted(chptr, source_p, msptr, NULL, NULL) == CHFL_BAN)
1028 return CAN_SEND_NO;
1029 }
1030
1031 return CAN_SEND_NONOP;
1032 }
1033
1034 /* find_bannickchange_channel()
1035 * Input: client to check
1036 * Output: channel preventing nick change
1037 */
1038 struct Channel *
1039 find_bannickchange_channel(struct Client *client_p)
1040 {
1041 struct Channel *chptr;
1042 struct membership *msptr;
1043 rb_dlink_node *ptr;
1044 char src_host[NICKLEN + USERLEN + HOSTLEN + 6];
1045 char src_iphost[NICKLEN + USERLEN + HOSTLEN + 6];
1046
1047 if (!MyClient(client_p) || IsOverride(client_p))
1048 return NULL;
1049
1050 rb_sprintf(src_host, "%s!%s@%s", client_p->name, client_p->username, client_p->host);
1051 rb_sprintf(src_iphost, "%s!%s@%s", client_p->name, client_p->username, client_p->sockhost);
1052
1053 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
1054 {
1055 msptr = ptr->data;
1056 chptr = msptr->chptr;
1057 if (is_chanop_voiced(msptr))
1058 continue;
1059 /* cached can_send */
1060 if (msptr->bants == chptr->bants)
1061 {
1062 if (can_send_banned(msptr))
1063 return chptr;
1064 }
1065 else if (is_banned(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN
1066 || is_quieted(chptr, client_p, msptr, src_host, src_iphost) == CHFL_BAN)
1067 return chptr;
1068 }
1069 return NULL;
1070 }
1071
1072 /* find_nonickchange_channel()
1073 * Input: client to check
1074 * Output: channel preventing nick change
1075 */
1076 struct Channel *
1077 find_nonickchange_channel(struct Client *client_p)
1078 {
1079 struct Channel *chptr;
1080 struct membership *msptr;
1081 rb_dlink_node *ptr;
1082
1083 if (!MyClient(client_p))
1084 return NULL;
1085
1086 RB_DLINK_FOREACH(ptr, client_p->user->channel.head)
1087 {
1088 msptr = ptr->data;
1089 chptr = msptr->chptr;
1090 if (chptr->mode.mode & ModuleModes.MODE_NONICK && (!ConfigChannel.exempt_cmode_N || !is_any_op(msptr)))
1091 return chptr;
1092 }
1093 return NULL;
1094 }
1095
1096 /* void check_spambot_warning(struct Client *source_p)
1097 * Input: Client to check, channel name or NULL if this is a part.
1098 * Output: none
1099 * Side-effects: Updates the client's oper_warn_count_down, warns the
1100 * IRC operators if necessary, and updates join_leave_countdown as
1101 * needed.
1102 */
1103 void
1104 check_spambot_warning(struct Client *source_p, const char *name)
1105 {
1106 int t_delta;
1107 int decrement_count;
1108 if((GlobalSetOptions.spam_num &&
1109 (source_p->localClient->join_leave_count >= GlobalSetOptions.spam_num)))
1110 {
1111 if(source_p->localClient->oper_warn_count_down > 0)
1112 source_p->localClient->oper_warn_count_down--;
1113 else
1114 source_p->localClient->oper_warn_count_down = 0;
1115 if(source_p->localClient->oper_warn_count_down == 0 &&
1116 name != NULL)
1117 {
1118 /* Its already known as a possible spambot */
1119 sendto_realops_snomask(SNO_BOTS, L_NETWIDE,
1120 "User %s (%s@%s) trying to join %s is a possible spambot",
1121 source_p->name,
1122 source_p->username, source_p->orighost, name);
1123 source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
1124 }
1125 }
1126 else
1127 {
1128 if((t_delta =
1129 (rb_current_time() - source_p->localClient->last_leave_time)) >
1130 JOIN_LEAVE_COUNT_EXPIRE_TIME)
1131 {
1132 decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
1133 if(name != NULL)
1134 ;
1135 else if(decrement_count > source_p->localClient->join_leave_count)
1136 source_p->localClient->join_leave_count = 0;
1137 else
1138 source_p->localClient->join_leave_count -= decrement_count;
1139 }
1140 else
1141 {
1142 if((rb_current_time() -
1143 (source_p->localClient->last_join_time)) < GlobalSetOptions.spam_time)
1144 {
1145 /* oh, its a possible spambot */
1146 source_p->localClient->join_leave_count++;
1147 }
1148 }
1149 if(name != NULL)
1150 source_p->localClient->last_join_time = rb_current_time();
1151 else
1152 source_p->localClient->last_leave_time = rb_current_time();
1153 }
1154 }
1155
1156 /* check_splitmode()
1157 *
1158 * input -
1159 * output -
1160 * side effects - compares usercount and servercount against their split
1161 * values and adjusts splitmode accordingly
1162 */
1163 void
1164 check_splitmode(void *unused)
1165 {
1166 if(splitchecking && (ConfigChannel.no_join_on_split || ConfigChannel.no_create_on_split))
1167 {
1168 /* not split, we're being asked to check now because someone
1169 * has left
1170 */
1171 if(!splitmode)
1172 {
1173 if(eob_count < split_servers || Count.total < split_users)
1174 {
1175 splitmode = 1;
1176 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1177 "Network split, activating splitmode");
1178 check_splitmode_ev = rb_event_addish("check_splitmode", check_splitmode, NULL, 2);
1179 }
1180 }
1181 /* in splitmode, check whether its finished */
1182 else if(eob_count >= split_servers && Count.total >= split_users)
1183 {
1184 splitmode = 0;
1185
1186 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1187 "Network rejoined, deactivating splitmode");
1188
1189 rb_event_delete(check_splitmode_ev);
1190 check_splitmode_ev = NULL;
1191 }
1192 }
1193 }
1194
1195
1196 /* allocate_topic()
1197 *
1198 * input - channel to allocate topic for
1199 * output - 1 on success, else 0
1200 * side effects - channel gets a topic allocated
1201 */
1202 static void
1203 allocate_topic(struct Channel *chptr)
1204 {
1205 void *ptr;
1206
1207 if(chptr == NULL)
1208 return;
1209
1210 ptr = rb_bh_alloc(topic_heap);
1211
1212 /* Basically we allocate one large block for the topic and
1213 * the topic info. We then split it up into two and shove it
1214 * in the chptr
1215 */
1216 chptr->topic = ptr;
1217 chptr->topic_info = (char *) ptr + TOPICLEN + 1;
1218 *chptr->topic = '\0';
1219 *chptr->topic_info = '\0';
1220 }
1221
1222 /* free_topic()
1223 *
1224 * input - channel which has topic to free
1225 * output -
1226 * side effects - channels topic is free'd
1227 */
1228 static void
1229 free_topic(struct Channel *chptr)
1230 {
1231 void *ptr;
1232
1233 if(chptr == NULL || chptr->topic == NULL)
1234 return;
1235
1236 /* This is safe for now - If you change allocate_topic you
1237 * MUST change this as well
1238 */
1239 ptr = chptr->topic;
1240 rb_bh_free(topic_heap, ptr);
1241 chptr->topic = NULL;
1242 chptr->topic_info = NULL;
1243 }
1244
1245 /* set_channel_topic()
1246 *
1247 * input - channel, topic to set, topic info and topic ts
1248 * output -
1249 * side effects - channels topic, topic info and TS are set.
1250 */
1251 void
1252 set_channel_topic(struct Channel *chptr, const char *topic, const char *topic_info, time_t topicts)
1253 {
1254 if(strlen(topic) > 0)
1255 {
1256 if(chptr->topic == NULL)
1257 allocate_topic(chptr);
1258 rb_strlcpy(chptr->topic, topic, TOPICLEN + 1);
1259 rb_strlcpy(chptr->topic_info, topic_info, USERHOST_REPLYLEN);
1260 chptr->topic_time = topicts;
1261 }
1262 else
1263 {
1264 if(chptr->topic != NULL)
1265 free_topic(chptr);
1266 chptr->topic_time = 0;
1267 }
1268 }
1269
1270 /* has_common_channel()
1271 *
1272 * input - pointer to client
1273 * - pointer to another client
1274 * output - 1 if the two have a channel in common, 0 elsewise
1275 * side effects - none
1276 */
1277 int
1278 has_common_channel(struct Client *client1, struct Client *client2)
1279 {
1280 rb_dlink_node *ptr;
1281
1282 RB_DLINK_FOREACH(ptr, client1->user->channel.head)
1283 {
1284 if(IsMember(client2, ((struct membership *)ptr->data)->chptr))
1285 return 1;
1286 }
1287 return 0;
1288 }
1289
1290 /* channel_modes()
1291 *
1292 * inputs - pointer to channel
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(struct Channel *chptr, 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(chptr->mode.mode & chmode_flags[i])
1317 *mbuf++ = i;
1318 }
1319
1320 if(chptr->mode.limit)
1321 {
1322 *mbuf++ = 'l';
1323
1324 if(!IsClient(client_p) || IsMember(client_p, chptr))
1325 pbuf += rb_sprintf(pbuf, " %d", chptr->mode.limit);
1326 }
1327
1328 if(*chptr->mode.key)
1329 {
1330 *mbuf++ = 'k';
1331
1332 if(pbuf > buf2 || !IsClient(client_p) || IsMember(client_p, chptr))
1333 pbuf += rb_sprintf(pbuf, " %s", chptr->mode.key);
1334 }
1335
1336 if(chptr->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", chptr->mode.join_num,
1342 chptr->mode.join_time);
1343 }
1344
1345 if(*chptr->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", chptr->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 }