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