ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 4133
Committed: Tue Jul 1 21:43:15 2014 UTC (11 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 33014 byte(s)
Log Message:
- ircd.c: removed 'event_check_splitmode'. Changed 'splitmode_event' to run every 5 seconds
  if server is in splitmode

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
20 */
21
22 /*! \file channel.c
23 * \brief Responsible for managing channels, members, bans and topics
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "channel.h"
30 #include "channel_mode.h"
31 #include "client.h"
32 #include "hash.h"
33 #include "conf.h"
34 #include "hostmask.h"
35 #include "irc_string.h"
36 #include "ircd.h"
37 #include "numeric.h"
38 #include "server.h"
39 #include "send.h"
40 #include "event.h"
41 #include "memory.h"
42 #include "mempool.h"
43 #include "misc.h"
44 #include "resv.h"
45
46
47 dlink_list channel_list;
48 mp_pool_t *ban_pool; /*! \todo ban_pool shouldn't be a global var */
49
50 struct event splitmode_event =
51 {
52 .name = "check_splitmode",
53 .handler = check_splitmode,
54 .when = 5
55 };
56
57 static mp_pool_t *member_pool, *channel_pool;
58 static char buf[IRCD_BUFSIZE];
59
60
61 /*! \brief Initializes the channel blockheap, adds known channel CAPAB
62 */
63 void
64 channel_init(void)
65 {
66 add_capability("EX", CAP_EX, 1);
67 add_capability("IE", CAP_IE, 1);
68
69 channel_pool = mp_pool_new(sizeof(struct Channel), MP_CHUNK_SIZE_CHANNEL);
70 ban_pool = mp_pool_new(sizeof(struct Ban), MP_CHUNK_SIZE_BAN);
71 member_pool = mp_pool_new(sizeof(struct Membership), MP_CHUNK_SIZE_MEMBER);
72 }
73
74 /*! \brief Adds a user to a channel by adding another link to the
75 * channels member chain.
76 * \param chptr Pointer to channel to add client to
77 * \param who Pointer to client (who) to add
78 * \param flags Flags for chanops etc
79 * \param flood_ctrl Whether to count this join in flood calculations
80 */
81 void
82 add_user_to_channel(struct Channel *chptr, struct Client *who,
83 unsigned int flags, int flood_ctrl)
84 {
85 struct Membership *ms = NULL;
86
87 if (GlobalSetOptions.joinfloodtime > 0)
88 {
89 if (flood_ctrl)
90 ++chptr->number_joined;
91
92 chptr->number_joined -= (CurrentTime - chptr->last_join_time) *
93 (((float)GlobalSetOptions.joinfloodcount) /
94 (float)GlobalSetOptions.joinfloodtime);
95
96 if (chptr->number_joined <= 0)
97 {
98 chptr->number_joined = 0;
99 ClearJoinFloodNoticed(chptr);
100 }
101 else if (chptr->number_joined >= GlobalSetOptions.joinfloodcount)
102 {
103 chptr->number_joined = GlobalSetOptions.joinfloodcount;
104
105 if (!IsSetJoinFloodNoticed(chptr))
106 {
107 SetJoinFloodNoticed(chptr);
108 sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE,
109 "Possible Join Flooder %s on %s target: %s",
110 get_client_name(who, HIDE_IP),
111 who->servptr->name, chptr->chname);
112 }
113 }
114
115 chptr->last_join_time = CurrentTime;
116 }
117
118 ms = mp_pool_get(member_pool);
119 ms->client_p = who;
120 ms->chptr = chptr;
121 ms->flags = flags;
122
123 dlinkAdd(ms, &ms->channode, &chptr->members);
124 dlinkAdd(ms, &ms->usernode, &who->channel);
125 }
126
127 /*! \brief Deletes an user from a channel by removing a link in the
128 * channels member chain.
129 * \param member Pointer to Membership struct
130 */
131 void
132 remove_user_from_channel(struct Membership *member)
133 {
134 struct Client *client_p = member->client_p;
135 struct Channel *chptr = member->chptr;
136
137 dlinkDelete(&member->channode, &chptr->members);
138 dlinkDelete(&member->usernode, &client_p->channel);
139
140 mp_pool_release(member);
141
142 if (chptr->members.head == NULL)
143 destroy_channel(chptr);
144 }
145
146 /* send_members()
147 *
148 * inputs -
149 * output - NONE
150 * side effects -
151 */
152 static void
153 send_members(struct Client *client_p, struct Channel *chptr,
154 char *modebuf, char *parabuf)
155 {
156 const dlink_node *ptr = NULL;
157 int tlen; /* length of text to append */
158 char *t, *start; /* temp char pointer */
159
160 start = t = buf + snprintf(buf, sizeof(buf), ":%s SJOIN %lu %s %s %s:",
161 me.id, (unsigned long)chptr->channelts,
162 chptr->chname, modebuf, parabuf);
163
164 DLINK_FOREACH(ptr, chptr->members.head)
165 {
166 const struct Membership *ms = ptr->data;
167
168 tlen = strlen(ms->client_p->id) + 1; /* +1 for space */
169
170 if (ms->flags & CHFL_CHANOP)
171 ++tlen;
172 if (ms->flags & CHFL_HALFOP)
173 ++tlen;
174 if (ms->flags & CHFL_VOICE)
175 ++tlen;
176
177 /*
178 * Space will be converted into CR, but we also need space for LF..
179 * That's why we use '- 1' here -adx
180 */
181 if (t + tlen - buf > IRCD_BUFSIZE - 1)
182 {
183 *(t - 1) = '\0'; /* Kill the space and terminate the string */
184 sendto_one(client_p, "%s", buf);
185 t = start;
186 }
187
188 if (ms->flags & CHFL_CHANOP)
189 *t++ = '@';
190 if (ms->flags & CHFL_HALFOP)
191 *t++ = '%';
192 if (ms->flags & CHFL_VOICE)
193 *t++ = '+';
194
195 strcpy(t, ms->client_p->id);
196
197 t += strlen(t);
198 *t++ = ' ';
199 }
200
201 /* Should always be non-NULL unless we have a kind of persistent channels */
202 if (chptr->members.head)
203 t--; /* Take the space out */
204 *t = '\0';
205 sendto_one(client_p, "%s", buf);
206 }
207
208 /*! \brief Sends +b/+e/+I
209 * \param client_p Client pointer to server
210 * \param chptr Pointer to channel
211 * \param list Pointer to list of modes to send
212 * \param flag Char flag flagging type of mode. Currently this can be 'b', e' or 'I'
213 */
214 static void
215 send_mode_list(struct Client *client_p, struct Channel *chptr,
216 const dlink_list *list, char flag)
217 {
218 const dlink_node *ptr = NULL;
219 char pbuf[IRCD_BUFSIZE] = "";
220 int tlen, mlen, cur_len;
221 char *pp = pbuf;
222
223 if (list->length == 0)
224 return;
225
226 mlen = snprintf(buf, sizeof(buf), ":%s BMASK %lu %s %c :", me.id,
227 (unsigned long)chptr->channelts, chptr->chname, flag);
228 cur_len = mlen;
229
230 DLINK_FOREACH(ptr, list->head)
231 {
232 const struct Ban *banptr = ptr->data;
233
234 tlen = banptr->len + 3; /* +3 for ! + @ + space */
235
236 /*
237 * Send buffer and start over if we cannot fit another ban
238 */
239 if (cur_len + (tlen - 1) > IRCD_BUFSIZE - 2)
240 {
241 *(pp - 1) = '\0'; /* Get rid of trailing space on buffer */
242 sendto_one(client_p, "%s%s", buf, pbuf);
243
244 cur_len = mlen;
245 pp = pbuf;
246 }
247
248 pp += sprintf(pp, "%s!%s@%s ", banptr->name, banptr->user,
249 banptr->host);
250 cur_len += tlen;
251 }
252
253 *(pp - 1) = '\0'; /* Get rid of trailing space on buffer */
254 sendto_one(client_p, "%s%s", buf, pbuf);
255 }
256
257 /*! \brief Send "client_p" a full list of the modes for channel chptr
258 * \param client_p Pointer to client client_p
259 * \param chptr Pointer to channel pointer
260 */
261 void
262 send_channel_modes(struct Client *client_p, struct Channel *chptr)
263 {
264 char modebuf[MODEBUFLEN] = "";
265 char parabuf[MODEBUFLEN] = "";
266
267 channel_modes(chptr, client_p, modebuf, parabuf);
268 send_members(client_p, chptr, modebuf, parabuf);
269
270 send_mode_list(client_p, chptr, &chptr->banlist, 'b');
271 send_mode_list(client_p, chptr, &chptr->exceptlist, 'e');
272 send_mode_list(client_p, chptr, &chptr->invexlist, 'I');
273 }
274
275 /*! \brief Check channel name for invalid characters
276 * \param name Pointer to channel name string
277 * \param local Indicates whether it's a local or remote creation
278 * \return 0 if invalid, 1 otherwise
279 */
280 int
281 check_channel_name(const char *name, const int local)
282 {
283 const char *p = name;
284
285 assert(name != NULL);
286
287 if (!IsChanPrefix(*p))
288 return 0;
289
290 if (!local || !ConfigChannel.disable_fake_channels)
291 {
292 while (*++p)
293 if (!IsChanChar(*p))
294 return 0;
295 }
296 else
297 {
298 while (*++p)
299 if (!IsVisibleChanChar(*p))
300 return 0;
301 }
302
303 return p - name <= CHANNELLEN;
304 }
305
306 void
307 remove_ban(struct Ban *bptr, dlink_list *list)
308 {
309 dlinkDelete(&bptr->node, list);
310
311 MyFree(bptr->name);
312 MyFree(bptr->user);
313 MyFree(bptr->host);
314 MyFree(bptr->who);
315
316 mp_pool_release(bptr);
317 }
318
319 /* free_channel_list()
320 *
321 * inputs - pointer to dlink_list
322 * output - NONE
323 * side effects -
324 */
325 void
326 free_channel_list(dlink_list *list)
327 {
328 dlink_node *ptr = NULL, *ptr_next = NULL;
329
330 DLINK_FOREACH_SAFE(ptr, ptr_next, list->head)
331 remove_ban(ptr->data, list);
332
333 assert(list->tail == NULL && list->head == NULL);
334 }
335
336 /*! \brief Get Channel block for chname (and allocate a new channel
337 * block, if it didn't exist before)
338 * \param chname Channel name
339 * \return Channel block
340 */
341 struct Channel *
342 make_channel(const char *chname)
343 {
344 struct Channel *chptr = NULL;
345
346 assert(!EmptyString(chname));
347
348 chptr = mp_pool_get(channel_pool);
349
350 /* Doesn't hurt to set it here */
351 chptr->channelts = CurrentTime;
352 chptr->last_join_time = CurrentTime;
353
354 strlcpy(chptr->chname, chname, sizeof(chptr->chname));
355 dlinkAdd(chptr, &chptr->node, &channel_list);
356
357 hash_add_channel(chptr);
358
359 return chptr;
360 }
361
362 /*! \brief Walk through this channel, and destroy it.
363 * \param chptr Channel pointer
364 */
365 void
366 destroy_channel(struct Channel *chptr)
367 {
368 dlink_node *ptr = NULL, *ptr_next = NULL;
369
370 DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->invites.head)
371 del_invite(chptr, ptr->data);
372
373 /* Free ban/exception/invex lists */
374 free_channel_list(&chptr->banlist);
375 free_channel_list(&chptr->exceptlist);
376 free_channel_list(&chptr->invexlist);
377
378 dlinkDelete(&chptr->node, &channel_list);
379 hash_del_channel(chptr);
380
381 mp_pool_release(chptr);
382 }
383
384 /*!
385 * \param chptr Pointer to channel
386 * \return String pointer "=" if public, "@" if secret else "*"
387 */
388 static const char *
389 channel_pub_or_secret(const struct Channel *chptr)
390 {
391 if (SecretChannel(chptr))
392 return "@";
393 if (PrivateChannel(chptr))
394 return "*";
395 return "=";
396 }
397
398 /*! \brief lists all names on given channel
399 * \param source_p Pointer to client struct requesting names
400 * \param chptr Pointer to channel block
401 * \param show_eon Show RPL_ENDOFNAMES numeric or not
402 * (don't want it with /names with no params)
403 */
404 void
405 channel_member_names(struct Client *source_p, struct Channel *chptr,
406 int show_eon)
407 {
408 const dlink_node *ptr = NULL;
409 char lbuf[IRCD_BUFSIZE + 1] = "";
410 char *t = NULL, *start = NULL;
411 int tlen = 0;
412 int is_member = IsMember(source_p, chptr);
413 int multi_prefix = HasCap(source_p, CAP_MULTI_PREFIX) != 0;
414 int uhnames = HasCap(source_p, CAP_UHNAMES) != 0;
415
416 if (PubChannel(chptr) || is_member)
417 {
418 t = lbuf + snprintf(lbuf, sizeof(lbuf), numeric_form(RPL_NAMREPLY),
419 me.name, source_p->name,
420 channel_pub_or_secret(chptr), chptr->chname);
421 start = t;
422
423 DLINK_FOREACH(ptr, chptr->members.head)
424 {
425 const struct Membership *ms = ptr->data;
426
427 if (HasUMode(ms->client_p, UMODE_INVISIBLE) && !is_member)
428 continue;
429
430 if (!uhnames)
431 tlen = strlen(ms->client_p->name) + 1; /* +1 for space */
432 else
433 tlen = strlen(ms->client_p->name) + strlen(ms->client_p->username) +
434 strlen(ms->client_p->host) + 3; /* +3 for ! + @ + space */
435
436 if (!multi_prefix)
437 {
438 if (ms->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE))
439 ++tlen;
440 }
441 else
442 {
443 if (ms->flags & CHFL_CHANOP)
444 ++tlen;
445 if (ms->flags & CHFL_HALFOP)
446 ++tlen;
447 if (ms->flags & CHFL_VOICE)
448 ++tlen;
449 }
450
451 if (t + tlen - lbuf > IRCD_BUFSIZE - 2)
452 {
453 *(t - 1) = '\0';
454 sendto_one(source_p, "%s", lbuf);
455 t = start;
456 }
457
458 if (!uhnames)
459 t += sprintf(t, "%s%s ", get_member_status(ms, multi_prefix),
460 ms->client_p->name);
461 else
462 t += sprintf(t, "%s%s!%s@%s ", get_member_status(ms, multi_prefix),
463 ms->client_p->name, ms->client_p->username,
464 ms->client_p->host);
465 }
466
467 if (tlen)
468 {
469 *(t - 1) = '\0';
470 sendto_one(source_p, "%s", lbuf);
471 }
472 }
473
474 if (show_eon)
475 sendto_one_numeric(source_p, &me, RPL_ENDOFNAMES, chptr->chname);
476 }
477
478 /*! \brief Adds client to invite list
479 * \param chptr Pointer to channel block
480 * \param who Pointer to client to add invite to
481 */
482 void
483 add_invite(struct Channel *chptr, struct Client *who)
484 {
485 del_invite(chptr, who);
486
487 /*
488 * Delete last link in chain if the list is max length
489 */
490 if (dlink_list_length(&who->localClient->invited) >=
491 ConfigChannel.max_channels)
492 del_invite(who->localClient->invited.tail->data, who);
493
494 /* Add client to channel invite list */
495 dlinkAdd(who, make_dlink_node(), &chptr->invites);
496
497 /* Add channel to the end of the client invite list */
498 dlinkAdd(chptr, make_dlink_node(), &who->localClient->invited);
499 }
500
501 /*! \brief Delete Invite block from channel invite list
502 * and client invite list
503 * \param chptr Pointer to Channel struct
504 * \param who Pointer to client to remove invites from
505 */
506 void
507 del_invite(struct Channel *chptr, struct Client *who)
508 {
509 dlink_node *ptr = NULL;
510
511 if ((ptr = dlinkFindDelete(&who->localClient->invited, chptr)))
512 free_dlink_node(ptr);
513
514 if ((ptr = dlinkFindDelete(&chptr->invites, who)))
515 free_dlink_node(ptr);
516 }
517
518 /* get_member_status()
519 *
520 * inputs - pointer to struct Membership
521 * - YES if we can combine different flags
522 * output - string either @, +, % or "" depending on whether
523 * chanop, voiced or user
524 * side effects -
525 *
526 * NOTE: Returned string is usually a static buffer
527 * (like in get_client_name)
528 */
529 const char *
530 get_member_status(const struct Membership *ms, const int combine)
531 {
532 static char buffer[4]; /* 4 for @%+\0 */
533 char *p = buffer;
534
535 if (ms->flags & CHFL_CHANOP)
536 {
537 if (!combine)
538 return "@";
539 *p++ = '@';
540 }
541
542 if (ms->flags & CHFL_HALFOP)
543 {
544 if (!combine)
545 return "%";
546 *p++ = '%';
547 }
548
549 if (ms->flags & CHFL_VOICE)
550 *p++ = '+';
551 *p = '\0';
552
553 return buffer;
554 }
555
556 /*!
557 * \param who Pointer to Client to check
558 * \param list Pointer to ban list to search
559 * \return 1 if ban found for given n!u\@h mask, 0 otherwise
560 *
561 */
562 static int
563 find_bmask(const struct Client *who, const dlink_list *const list)
564 {
565 const dlink_node *ptr = NULL;
566
567 DLINK_FOREACH(ptr, list->head)
568 {
569 const struct Ban *bp = ptr->data;
570
571 if (!match(bp->name, who->name) && !match(bp->user, who->username))
572 {
573 switch (bp->type)
574 {
575 case HM_HOST:
576 if (!match(bp->host, who->host) || !match(bp->host, who->sockhost))
577 return 1;
578 break;
579 case HM_IPV4:
580 if (who->localClient->aftype == AF_INET)
581 if (match_ipv4(&who->localClient->ip, &bp->addr, bp->bits))
582 return 1;
583 break;
584 #ifdef IPV6
585 case HM_IPV6:
586 if (who->localClient->aftype == AF_INET6)
587 if (match_ipv6(&who->localClient->ip, &bp->addr, bp->bits))
588 return 1;
589 break;
590 #endif
591 default:
592 assert(0);
593 }
594 }
595 }
596
597 return 0;
598 }
599
600 /*!
601 * \param chptr Pointer to channel block
602 * \param who Pointer to client to check access fo
603 * \return 0 if not banned, 1 otherwise
604 */
605 int
606 is_banned(const struct Channel *chptr, const struct Client *who)
607 {
608 if (find_bmask(who, &chptr->banlist))
609 if (!find_bmask(who, &chptr->exceptlist))
610 return 1;
611
612 return 0;
613 }
614
615 /*! Tests if a client can join a certain channel
616 * \param source_p Pointer to client attempting to join
617 * \param chptr Pointer to channel
618 * \param key Key sent by client attempting to join if present
619 * \return ERR_BANNEDFROMCHAN, ERR_INVITEONLYCHAN, ERR_CHANNELISFULL
620 * or 0 if allowed to join.
621 */
622 int
623 can_join(struct Client *source_p, struct Channel *chptr, const char *key)
624 {
625 if ((chptr->mode.mode & MODE_SSLONLY) && !HasUMode(source_p, UMODE_SSL))
626 return ERR_SSLONLYCHAN;
627
628 if ((chptr->mode.mode & MODE_REGONLY) && !HasUMode(source_p, UMODE_REGISTERED))
629 return ERR_NEEDREGGEDNICK;
630
631 if ((chptr->mode.mode & MODE_OPERONLY) && !HasUMode(source_p, UMODE_OPER))
632 return ERR_OPERONLYCHAN;
633
634 if (chptr->mode.mode & MODE_INVITEONLY)
635 if (!dlinkFind(&source_p->localClient->invited, chptr))
636 if (!find_bmask(source_p, &chptr->invexlist))
637 return ERR_INVITEONLYCHAN;
638
639 if (chptr->mode.key[0] && (!key || strcmp(chptr->mode.key, key)))
640 return ERR_BADCHANNELKEY;
641
642 if (chptr->mode.limit && dlink_list_length(&chptr->members) >=
643 chptr->mode.limit)
644 return ERR_CHANNELISFULL;
645
646 if (is_banned(chptr, source_p))
647 return ERR_BANNEDFROMCHAN;
648
649 return 0;
650 }
651
652 int
653 has_member_flags(const struct Membership *ms, const unsigned int flags)
654 {
655 return ms && (ms->flags & flags);
656 }
657
658 struct Membership *
659 find_channel_link(struct Client *client_p, struct Channel *chptr)
660 {
661 dlink_node *ptr = NULL;
662
663 if (!IsClient(client_p))
664 return NULL;
665
666 if (dlink_list_length(&chptr->members) < dlink_list_length(&client_p->channel))
667 {
668 DLINK_FOREACH(ptr, chptr->members.head)
669 if (((struct Membership *)ptr->data)->client_p == client_p)
670 return ptr->data;
671 }
672 else
673 {
674 DLINK_FOREACH(ptr, client_p->channel.head)
675 if (((struct Membership *)ptr->data)->chptr == chptr)
676 return ptr->data;
677 }
678
679 return NULL;
680 }
681
682 /*! Tests if a client can send to a channel
683 * \param message The actual message string the client wants to send
684 * \return 1 if the message does contain any control codes, 0 otherwise
685 */
686 static int
687 msg_has_ctrls(const char *message)
688 {
689 const unsigned char *p = (const unsigned char *)message;
690
691 for (; *p; ++p)
692 {
693 if (*p > 31 || *p == 1)
694 continue; /* No control code or CTCP */
695
696 if (*p == 27) /* Escape */
697 {
698 /* ISO 2022 charset shift sequence */
699 if (*(p + 1) == '$' ||
700 *(p + 1) == '(')
701 {
702 ++p;
703 continue;
704 }
705 }
706
707 return 1; /* Control code */
708 }
709
710 return 0;
711 }
712
713 /*! Tests if a client can send to a channel
714 * \param chptr Pointer to Channel struct
715 * \param source_p Pointer to Client struct
716 * \param ms Pointer to Membership struct (can be NULL)
717 * \param message The actual message string the client wants to send
718 * \return CAN_SEND_OPV if op or voiced on channel\n
719 * CAN_SEND_NONOP if can send to channel but is not an op\n
720 * ERR_CANNOTSENDTOCHAN or ERR_NEEDREGGEDNICK if they cannot send to channel\n
721 */
722 int
723 can_send(struct Channel *chptr, struct Client *source_p,
724 struct Membership *ms, const char *message)
725 {
726 struct MaskItem *conf = NULL;
727
728 if (IsServer(source_p) || HasFlag(source_p, FLAGS_SERVICE))
729 return CAN_SEND_OPV;
730
731 if (MyClient(source_p) && !IsExemptResv(source_p))
732 if (!(HasUMode(source_p, UMODE_OPER) && ConfigFileEntry.oper_pass_resv))
733 if ((conf = match_find_resv(chptr->chname)) && !resv_find_exempt(source_p, conf))
734 return ERR_CANNOTSENDTOCHAN;
735
736 if ((chptr->mode.mode & MODE_NOCTRL) && msg_has_ctrls(message))
737 return ERR_NOCTRLSONCHAN;
738 if (ms || (ms = find_channel_link(source_p, chptr)))
739 if (ms->flags & (CHFL_CHANOP|CHFL_HALFOP|CHFL_VOICE))
740 return CAN_SEND_OPV;
741 if (!ms && (chptr->mode.mode & MODE_NOPRIVMSGS))
742 return ERR_CANNOTSENDTOCHAN;
743 if (chptr->mode.mode & MODE_MODERATED)
744 return ERR_CANNOTSENDTOCHAN;
745 if ((chptr->mode.mode & MODE_MODREG) && !HasUMode(source_p, UMODE_REGISTERED))
746 return ERR_NEEDREGGEDNICK;
747
748 /* Cache can send if banned */
749 if (MyClient(source_p))
750 {
751 if (ms)
752 {
753 if (ms->flags & CHFL_BAN_SILENCED)
754 return ERR_CANNOTSENDTOCHAN;
755
756 if (!(ms->flags & CHFL_BAN_CHECKED))
757 {
758 if (is_banned(chptr, source_p))
759 {
760 ms->flags |= (CHFL_BAN_CHECKED|CHFL_BAN_SILENCED);
761 return ERR_CANNOTSENDTOCHAN;
762 }
763
764 ms->flags |= CHFL_BAN_CHECKED;
765 }
766 }
767 else if (is_banned(chptr, source_p))
768 return ERR_CANNOTSENDTOCHAN;
769 }
770
771 return CAN_SEND_NONOP;
772 }
773
774 /*! \brief Updates the client's oper_warn_count_down, warns the
775 * IRC operators if necessary, and updates
776 * join_leave_countdown as needed.
777 * \param source_p Pointer to struct Client to check
778 * \param name Channel name or NULL if this is a part.
779 */
780 void
781 check_spambot_warning(struct Client *source_p, const char *name)
782 {
783 int t_delta = 0;
784 int decrement_count = 0;
785
786 if ((GlobalSetOptions.spam_num &&
787 (source_p->localClient->join_leave_count >=
788 GlobalSetOptions.spam_num)))
789 {
790 if (source_p->localClient->oper_warn_count_down > 0)
791 source_p->localClient->oper_warn_count_down--;
792 else
793 source_p->localClient->oper_warn_count_down = 0;
794
795 if (source_p->localClient->oper_warn_count_down == 0)
796 {
797 /* It's already known as a possible spambot */
798 if (name)
799 sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE,
800 "User %s (%s@%s) trying to join %s is a possible spambot",
801 source_p->name, source_p->username,
802 source_p->host, name);
803 else
804 sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE,
805 "User %s (%s@%s) is a possible spambot",
806 source_p->name, source_p->username,
807 source_p->host);
808 source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
809 }
810 }
811 else
812 {
813 if ((t_delta = (CurrentTime - source_p->localClient->last_leave_time)) >
814 JOIN_LEAVE_COUNT_EXPIRE_TIME)
815 {
816 decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
817 if (decrement_count > source_p->localClient->join_leave_count)
818 source_p->localClient->join_leave_count = 0;
819 else
820 source_p->localClient->join_leave_count -= decrement_count;
821 }
822 else
823 {
824 if ((CurrentTime - (source_p->localClient->last_join_time)) <
825 GlobalSetOptions.spam_time)
826 source_p->localClient->join_leave_count++; /* It's a possible spambot */
827 }
828
829 if (name)
830 source_p->localClient->last_join_time = CurrentTime;
831 else
832 source_p->localClient->last_leave_time = CurrentTime;
833 }
834 }
835
836 /*! \brief Compares usercount and servercount against their split
837 * values and adjusts splitmode accordingly
838 * \param unused Unused address pointer
839 */
840 void
841 check_splitmode(void *unused)
842 {
843 if (splitchecking && (ConfigChannel.no_join_on_split ||
844 ConfigChannel.no_create_on_split))
845 {
846 const unsigned int server = dlink_list_length(&global_serv_list);
847
848 if (!splitmode && ((server < split_servers) || (Count.total < split_users)))
849 {
850 splitmode = 1;
851
852 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
853 "Network split, activating splitmode");
854 event_add(&splitmode_event, NULL);
855 }
856 else if (splitmode && (server >= split_servers) && (Count.total >= split_users))
857 {
858 splitmode = 0;
859
860 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
861 "Network rejoined, deactivating splitmode");
862 event_delete(&splitmode_event);
863 }
864 }
865 }
866
867 /*! \brief Sets the channel topic for a certain channel
868 * \param chptr Pointer to struct Channel
869 * \param topic The topic string
870 * \param topic_info n!u\@h formatted string of the topic setter
871 * \param topicts Timestamp on the topic
872 * \param local Whether the topic is set by a local client
873 */
874 void
875 channel_set_topic(struct Channel *chptr, const char *topic,
876 const char *topic_info, time_t topicts, int local)
877 {
878 if (local)
879 strlcpy(chptr->topic, topic, IRCD_MIN(sizeof(chptr->topic), ServerInfo.max_topic_length + 1));
880 else
881 strlcpy(chptr->topic, topic, sizeof(chptr->topic));
882
883 strlcpy(chptr->topic_info, topic_info, sizeof(chptr->topic_info));
884 chptr->topic_time = topicts;
885 }
886
887 /* do_join_0()
888 *
889 * inputs - pointer to client doing join 0
890 * output - NONE
891 * side effects - Use has decided to join 0. This is legacy
892 * from the days when channels were numbers not names. *sigh*
893 * There is a bunch of evilness necessary here due to
894 * anti spambot code.
895 */
896 void
897 channel_do_join_0(struct Client *source_p)
898 {
899 dlink_node *ptr = NULL, *ptr_next = NULL;
900
901 if (source_p->channel.head)
902 if (MyConnect(source_p) && !HasUMode(source_p, UMODE_OPER))
903 check_spambot_warning(source_p, NULL);
904
905 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->channel.head)
906 {
907 struct Channel *chptr = ((struct Membership *)ptr->data)->chptr;
908
909 sendto_server(source_p, NOCAPS, NOCAPS, ":%s PART %s",
910 source_p->id, chptr->chname);
911 sendto_channel_local(ALL_MEMBERS, 0, chptr, ":%s!%s@%s PART %s",
912 source_p->name, source_p->username,
913 source_p->host, chptr->chname);
914
915 remove_user_from_channel(ptr->data);
916 }
917 }
918
919 static char *
920 channel_find_last0(struct Client *source_p, char *chanlist)
921 {
922 int join0 = 0;
923
924 for (char *p = chanlist; *p; ++p) /* Find last "JOIN 0" */
925 {
926 if (*p == '0' && (*(p + 1) == ',' || *(p + 1) == '\0'))
927 {
928 if ((*p + 1) == ',')
929 ++p;
930
931 chanlist = p + 1;
932 join0 = 1;
933 }
934 else
935 {
936 while (*p != ',' && *p != '\0') /* Skip past channel name */
937 ++p;
938
939 if (*p == '\0') /* Hit the end */
940 break;
941 }
942 }
943
944 if (join0)
945 channel_do_join_0(source_p);
946
947 return chanlist;
948 }
949
950 void
951 channel_do_join(struct Client *source_p, char *channel, char *key_list)
952 {
953 char *p = NULL;
954 char *chan = NULL;
955 char *chan_list = NULL;
956 struct Channel *chptr = NULL;
957 struct MaskItem *conf = NULL;
958 const struct ClassItem *class = get_class_ptr(&source_p->localClient->confs);
959 int i = 0;
960 unsigned int flags = 0;
961
962 chan_list = channel_find_last0(source_p, channel);
963
964 for (chan = strtoken(&p, chan_list, ","); chan;
965 chan = strtoken(&p, NULL, ","))
966 {
967 const char *key = NULL;
968
969 /* If we have any more keys, take the first for this channel. */
970 if (!EmptyString(key_list) && (key_list = strchr(key = key_list, ',')))
971 *key_list++ = '\0';
972
973 /* Empty keys are the same as no keys. */
974 if (key && *key == '\0')
975 key = NULL;
976
977 if (!check_channel_name(chan, 1))
978 {
979 sendto_one_numeric(source_p, &me, ERR_BADCHANNAME, chan);
980 continue;
981 }
982
983 if (!IsExemptResv(source_p) &&
984 !(HasUMode(source_p, UMODE_OPER) && ConfigFileEntry.oper_pass_resv) &&
985 ((conf = match_find_resv(chan)) && !resv_find_exempt(source_p, conf)))
986 {
987 ++conf->count;
988 sendto_one_numeric(source_p, &me, ERR_CHANBANREASON,
989 chan, conf->reason ? conf->reason : "Reserved channel");
990 sendto_realops_flags(UMODE_SPY, L_ALL, SEND_NOTICE,
991 "Forbidding reserved channel %s from user %s",
992 chan, get_client_name(source_p, HIDE_IP));
993 continue;
994 }
995
996 if (dlink_list_length(&source_p->channel) >=
997 ((class->max_channels) ? class->max_channels : ConfigChannel.max_channels))
998 {
999 sendto_one_numeric(source_p, &me, ERR_TOOMANYCHANNELS, chan);
1000 break;
1001 }
1002
1003 if ((chptr = hash_find_channel(chan)))
1004 {
1005 if (IsMember(source_p, chptr))
1006 continue;
1007
1008 if (splitmode && !HasUMode(source_p, UMODE_OPER) &&
1009 ConfigChannel.no_join_on_split)
1010 {
1011 sendto_one_numeric(source_p, &me, ERR_UNAVAILRESOURCE, chptr->chname);
1012 continue;
1013 }
1014
1015 /*
1016 * can_join checks for +i key, bans.
1017 */
1018 if ((i = can_join(source_p, chptr, key)))
1019 {
1020 sendto_one_numeric(source_p, &me, i, chptr->chname);
1021 continue;
1022 }
1023
1024 /*
1025 * This should never be the case unless there is some sort of
1026 * persistant channels.
1027 */
1028 if (dlink_list_length(&chptr->members) == 0)
1029 flags = CHFL_CHANOP;
1030 else
1031 flags = 0;
1032 }
1033 else
1034 {
1035 if (splitmode && !HasUMode(source_p, UMODE_OPER) &&
1036 (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split))
1037 {
1038 sendto_one_numeric(source_p, &me, ERR_UNAVAILRESOURCE, chan);
1039 continue;
1040 }
1041
1042 flags = CHFL_CHANOP;
1043 chptr = make_channel(chan);
1044 }
1045
1046 if (!HasUMode(source_p, UMODE_OPER))
1047 check_spambot_warning(source_p, chptr->chname);
1048
1049 add_user_to_channel(chptr, source_p, flags, 1);
1050
1051 /*
1052 * Set timestamp if appropriate, and propagate
1053 */
1054 if (flags == CHFL_CHANOP)
1055 {
1056 chptr->channelts = CurrentTime;
1057 chptr->mode.mode |= MODE_TOPICLIMIT;
1058 chptr->mode.mode |= MODE_NOPRIVMSGS;
1059
1060 sendto_server(source_p, NOCAPS, NOCAPS, ":%s SJOIN %lu %s +nt :@%s",
1061 me.id, (unsigned long)chptr->channelts,
1062 chptr->chname, source_p->id);
1063
1064 /*
1065 * Notify all other users on the new channel
1066 */
1067 sendto_channel_local(ALL_MEMBERS, 0, chptr, ":%s!%s@%s JOIN :%s",
1068 source_p->name, source_p->username,
1069 source_p->host, chptr->chname);
1070 sendto_channel_local(ALL_MEMBERS, 0, chptr, ":%s MODE %s +nt",
1071 me.name, chptr->chname);
1072
1073 if (source_p->away[0])
1074 sendto_channel_local_butone(source_p, 0, CAP_AWAY_NOTIFY, chptr,
1075 ":%s!%s@%s AWAY :%s",
1076 source_p->name, source_p->username,
1077 source_p->host, source_p->away);
1078 }
1079 else
1080 {
1081 sendto_server(source_p, NOCAPS, NOCAPS, ":%s JOIN %lu %s +",
1082 source_p->id, (unsigned long)chptr->channelts,
1083 chptr->chname);
1084 sendto_channel_local(ALL_MEMBERS, 0, chptr, ":%s!%s@%s JOIN :%s",
1085 source_p->name, source_p->username,
1086 source_p->host, chptr->chname);
1087
1088 if (source_p->away[0])
1089 sendto_channel_local_butone(source_p, 0, CAP_AWAY_NOTIFY, chptr,
1090 ":%s!%s@%s AWAY :%s",
1091 source_p->name, source_p->username,
1092 source_p->host, source_p->away);
1093 }
1094
1095 del_invite(chptr, source_p);
1096
1097 if (chptr->topic[0])
1098 {
1099 sendto_one_numeric(source_p, &me, RPL_TOPIC, chptr->chname, chptr->topic);
1100 sendto_one_numeric(source_p, &me, RPL_TOPICWHOTIME, chptr->chname,
1101 chptr->topic_info, chptr->topic_time);
1102 }
1103
1104 channel_member_names(source_p, chptr, 1);
1105
1106 source_p->localClient->last_join_time = CurrentTime;
1107 }
1108 }
1109
1110 /*! \brief Removes a client from a specific channel
1111 * \param source_p Pointer to source client to remove
1112 * \param name Name of channel to remove from
1113 * \param reason Part reason to show
1114 */
1115 static void
1116 channel_part_one_client(struct Client *source_p, const char *name, const char *reason)
1117 {
1118 struct Channel *chptr = NULL;
1119 struct Membership *ms = NULL;
1120
1121 if ((chptr = hash_find_channel(name)) == NULL)
1122 {
1123 sendto_one_numeric(source_p, &me, ERR_NOSUCHCHANNEL, name);
1124 return;
1125 }
1126
1127 if ((ms = find_channel_link(source_p, chptr)) == NULL)
1128 {
1129 sendto_one_numeric(source_p, &me, ERR_NOTONCHANNEL, chptr->chname);
1130 return;
1131 }
1132
1133 if (MyConnect(source_p) && !HasUMode(source_p, UMODE_OPER))
1134 check_spambot_warning(source_p, NULL);
1135
1136 /*
1137 * Remove user from the old channel (if any)
1138 * only allow /part reasons in -m chans
1139 */
1140 if (*reason && (!MyConnect(source_p) ||
1141 ((can_send(chptr, source_p, ms, reason) &&
1142 (source_p->localClient->firsttime + ConfigFileEntry.anti_spam_exit_message_time)
1143 < CurrentTime))))
1144 {
1145 sendto_server(source_p, NOCAPS, NOCAPS, ":%s PART %s :%s",
1146 source_p->id, chptr->chname, reason);
1147 sendto_channel_local(ALL_MEMBERS, 0, chptr, ":%s!%s@%s PART %s :%s",
1148 source_p->name, source_p->username,
1149 source_p->host, chptr->chname, reason);
1150 }
1151 else
1152 {
1153 sendto_server(source_p, NOCAPS, NOCAPS, ":%s PART %s",
1154 source_p->id, chptr->chname);
1155 sendto_channel_local(ALL_MEMBERS, 0, chptr, ":%s!%s@%s PART %s",
1156 source_p->name, source_p->username,
1157 source_p->host, chptr->chname);
1158 }
1159
1160 remove_user_from_channel(ms);
1161 }
1162
1163 void
1164 channel_do_part(struct Client *source_p, char *channel, char *reason)
1165 {
1166 char *p = NULL, *name = NULL;
1167 char reasonbuf[KICKLEN + 1] = "";
1168
1169 if (!EmptyString(reason))
1170 strlcpy(reasonbuf, reason, sizeof(reasonbuf));
1171
1172 for (name = strtoken(&p, channel, ","); name;
1173 name = strtoken(&p, NULL, ","))
1174 channel_part_one_client(source_p, name, reasonbuf);
1175 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision