ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 4811
Committed: Sat Nov 1 11:56:53 2014 UTC (10 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 34179 byte(s)
Log Message:
- channel.c, channel.h: added clear_invites() and make use of it
- ms_sjoin.c:ms_sjoin(): clear invites if the introduced channel has lower TS

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

Properties

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