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