ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 4086
Committed: Sat Jun 28 16:44:20 2014 UTC (12 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 32933 byte(s)
Error occurred while calculating annotation data.
Log Message:
- Let mp_pool_get() clear memory

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

Properties

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