ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 5347
Committed: Sun Jan 11 12:42:20 2015 UTC (10 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 34379 byte(s)
Log Message:
- Update copyright years

File Contents

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

Properties

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