ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 9454
Committed: Tue Jun 30 16:56:21 2020 UTC (6 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 33621 byte(s)
Error occurred while calculating annotation data.
Log Message:
- Add member_get_prefix_len() and make use of it in some places;  rename get_member_status() to member_get_prefix()

File Contents

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

Properties

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