1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2021 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: channel.c 9858 2021-01-01 04:43:42Z michael $
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 *
channel_get_list(void)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
add_user_to_channel(struct Channel * channel,struct Client * client,unsigned int flags,bool flood_ctrl)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_FLOOD, 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
remove_user_from_channel(struct ChannelMember * member)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
channel_demote_members(struct Channel * channel,const struct Client * client,unsigned int mask,const char flag)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 = parabuf;
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
channel_send_members(struct Client * client,const struct Channel * channel,const char * modebuf,const char * parabuf)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 > sizeof(buf) - 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
channel_send_mask_list(struct Client * client,const struct Channel * channel,const dlink_list * list,const char flag)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 modebuf[IRCD_BUFSIZE];
259 char parabuf[IRCD_BUFSIZE];
260 size_t tlen, mlen, cur_len;
261 char *pbuf = parabuf;
262
263 if (dlink_list_length(list) == 0)
264 return;
265
266 mlen = snprintf(modebuf, sizeof(modebuf), ":%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(parabuf) - 2)
280 {
281 *(pbuf - 1) = '\0'; /* Get rid of trailing space on buffer */
282 sendto_one(client, "%s%s", modebuf, parabuf);
283
284 cur_len = mlen;
285 pbuf = parabuf;
286 }
287
288 pbuf += snprintf(pbuf, sizeof(parabuf) - (pbuf - parabuf), "%s ", ban->banstr);
289 cur_len += tlen;
290 }
291
292 *(pbuf - 1) = '\0'; /* Get rid of trailing space on buffer */
293 sendto_one(client, "%s%s", modebuf, parabuf);
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
channel_send_modes(struct Client * client,const struct Channel * channel)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, NULL, 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
channel_check_name(const char * name,bool local)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
remove_ban(struct Ban * ban,dlink_list * list)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
channel_free_mask_list(dlink_list * list)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 *
channel_make(const char * name)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
channel_free(struct Channel * channel)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 *
channel_pub_or_secret(const struct Channel * channel)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 */
461 void
channel_send_namereply(struct Client * client,struct Channel * channel)462 channel_send_namereply(struct Client *client, struct Channel *channel)
463 {
464 dlink_node *node;
465 char buf[IRCD_BUFSIZE + 1];
466 char *bufptr = buf;
467 size_t masklen = 0;
468 bool is_member = member_find_link(client, channel) != NULL;
469 bool multi_prefix = HasCap(client, CAP_MULTI_PREFIX) != 0;
470 bool uhnames = HasCap(client, CAP_UHNAMES) != 0;
471
472 assert(IsClient(client));
473
474 if (PubChannel(channel) || is_member == true)
475 {
476 /* :me.name 353 client->name @ channel->name :+nick1 @nick2 %nick3 ...\r\n */
477 /* 1 23456 789 01 2 3 */
478 size_t len = strlen(me.name) + strlen(client->name) + channel->name_len + 13;
479
480 DLINK_FOREACH(node, channel->members.head)
481 {
482 const struct ChannelMember *member = node->data;
483
484 if (HasUMode(member->client, UMODE_INVISIBLE) && is_member == false)
485 continue;
486
487 if (uhnames == true)
488 masklen = strlen(member->client->name) + strlen(member->client->username) +
489 strlen(member->client->host) + 3; /* +3 for ! + @ + space */
490 else
491 masklen = strlen(member->client->name) + 1; /* +1 for space */
492
493 masklen += member_get_prefix_len(member, multi_prefix);
494
495 if ((bufptr - buf) + masklen + len > sizeof(buf))
496 {
497 *(bufptr - 1) = '\0';
498 sendto_one_numeric(client, &me, RPL_NAMREPLY,
499 channel_pub_or_secret(channel), channel->name, buf);
500 bufptr = buf;
501 }
502
503 if (uhnames == true)
504 bufptr += snprintf(bufptr, sizeof(buf) - (bufptr - buf), "%s%s!%s@%s ",
505 member_get_prefix(member, multi_prefix),
506 member->client->name, member->client->username,
507 member->client->host);
508 else
509 bufptr += snprintf(bufptr, sizeof(buf) - (bufptr - buf), "%s%s ",
510 member_get_prefix(member, multi_prefix),
511 member->client->name);
512 }
513
514 if (bufptr != buf)
515 {
516 *(bufptr - 1) = '\0';
517 sendto_one_numeric(client, &me, RPL_NAMREPLY,
518 channel_pub_or_secret(channel), channel->name, buf);
519 }
520 }
521
522 sendto_one_numeric(client, &me, RPL_ENDOFNAMES, channel->name);
523 }
524
525 /* member_get_prefix()
526 *
527 * inputs - pointer to struct ChannelMember
528 * - YES if we can combine different flags
529 * output - string either @, +, % or "" depending on whether
530 * chanop, voiced or user
531 * side effects -
532 *
533 * NOTE: Returned string is usually a static buffer
534 * (like in client_get_name)
535 */
536 const char *
member_get_prefix(const struct ChannelMember * member,bool combine)537 member_get_prefix(const struct ChannelMember *member, bool combine)
538 {
539 static char buf[CMEMBER_STATUS_FLAGS_LEN + 1]; /* +1 for \0 */
540 char *bufptr = buf;
541
542 if (member->flags & CHFL_CHANOP)
543 {
544 if (combine == false)
545 return "@";
546 *bufptr++ = '@';
547 }
548
549 if (member->flags & CHFL_HALFOP)
550 {
551 if (combine == false)
552 return "%";
553 *bufptr++ = '%';
554 }
555
556 if (member->flags & CHFL_VOICE)
557 *bufptr++ = '+';
558 *bufptr = '\0';
559
560 return buf;
561 }
562
563 size_t
member_get_prefix_len(const struct ChannelMember * member,bool combine)564 member_get_prefix_len(const struct ChannelMember *member, bool combine)
565 {
566 size_t len = 0;
567
568 if (member->flags & CHFL_CHANOP)
569 {
570 if (combine == false)
571 return 1;
572 ++len;
573 }
574
575 if (member->flags & CHFL_HALFOP)
576 {
577 if (combine == false)
578 return 1;
579 ++len;
580 }
581
582 if (member->flags & CHFL_VOICE)
583 {
584 if (combine == false)
585 return 1;
586 ++len;
587 }
588
589 return len;
590 }
591
592 /*!
593 * \param client Pointer to Client to check
594 * \param list Pointer to ban list to search
595 * \return 1 if ban found for given n!u\@h mask, 0 otherwise
596 */
597 static bool
ban_matches(struct Client * client,struct Channel * channel,struct Ban * ban)598 ban_matches(struct Client *client, struct Channel *channel, struct Ban *ban)
599 {
600 /* Is a matching extban, call custom match handler */
601 if (ban->extban & extban_matching_mask())
602 {
603 struct Extban *extban = extban_find_flag(ban->extban & extban_matching_mask());
604 if (extban == NULL)
605 return false;
606
607 if (extban->matches == NULL || extban->matches(client, channel, ban) == EXTBAN_NO_MATCH)
608 return false;
609
610 return true;
611 }
612
613 if (match(ban->name, client->name) == 0 && match(ban->user, client->username) == 0)
614 {
615 switch (ban->type)
616 {
617 case HM_HOST:
618 if (match(ban->host, client->realhost) == 0 ||
619 match(ban->host, client->sockhost) == 0 || match(ban->host, client->host) == 0)
620 return true;
621 break;
622 case HM_IPV6:
623 case HM_IPV4:
624 if (address_compare(&client->ip, &ban->addr, false, false, ban->bits) == true)
625 return true;
626 break;
627 default:
628 assert(0);
629 }
630 }
631
632 return false;
633 }
634
635 bool
find_bmask(struct Client * client,struct Channel * channel,const dlink_list * list,struct Extban * extban)636 find_bmask(struct Client *client, struct Channel *channel, const dlink_list *list, struct Extban *extban)
637 {
638 dlink_node *node;
639
640 DLINK_FOREACH(node, list->head)
641 {
642 struct Ban *ban = node->data;
643
644 /* Looking for a specific type of extban? */
645 if (extban)
646 {
647 if (!(ban->extban & extban->flag))
648 continue;
649 }
650 else
651 {
652 /*
653 * Acting extbans have their own time they act and are not general purpose bans,
654 * so skip them unless we are hunting them.
655 */
656 if (ban->extban & extban_acting_mask())
657 continue;
658 }
659
660 bool matches = ban_matches(client, channel, ban);
661 if (matches == false)
662 continue;
663
664 return true;
665 }
666
667 return false;
668 }
669
670 /*!
671 * \param channel Pointer to channel block
672 * \param client Pointer to client to check access fo
673 * \return 0 if not banned, 1 otherwise
674 */
675 bool
is_banned(struct Channel * channel,struct Client * client)676 is_banned(struct Channel *channel, struct Client *client)
677 {
678 if (find_bmask(client, channel, &channel->banlist, NULL) == true)
679 return find_bmask(client, channel, &channel->exceptlist, NULL) == false;
680 return false;
681 }
682
683 /*! Tests if a client can join a certain channel
684 * \param client Pointer to client attempting to join
685 * \param channel Pointer to channel
686 * \param key Key sent by client attempting to join if present
687 * \return ERR_BANNEDFROMCHAN, ERR_INVITEONLYCHAN, ERR_CHANNELISFULL
688 * or 0 if allowed to join.
689 */
690 static int
can_join(struct Client * client,struct Channel * channel,const char * key)691 can_join(struct Client *client, struct Channel *channel, const char *key)
692 {
693 if (HasCMode(channel, MODE_SECUREONLY) && !HasUMode(client, UMODE_SECURE))
694 return ERR_SECUREONLYCHAN;
695
696 if (HasCMode(channel, MODE_REGONLY) && !HasUMode(client, UMODE_REGISTERED))
697 return ERR_NEEDREGGEDNICK;
698
699 if (HasCMode(channel, MODE_OPERONLY) && !HasUMode(client, UMODE_OPER))
700 return ERR_OPERONLYCHAN;
701
702 if (HasCMode(channel, MODE_INVITEONLY))
703 if (invite_find(channel, client) == NULL)
704 if (find_bmask(client, channel, &channel->invexlist, NULL) == false)
705 return ERR_INVITEONLYCHAN;
706
707 if (channel->mode.key[0] && (key == NULL || strcmp(channel->mode.key, key)))
708 return ERR_BADCHANNELKEY;
709
710 if (channel->mode.limit && dlink_list_length(&channel->members) >=
711 channel->mode.limit)
712 return ERR_CHANNELISFULL;
713
714 if (is_banned(channel, client) == true)
715 return ERR_BANNEDFROMCHAN;
716
717 return extban_join_can_join(channel, client, NULL);
718 }
719
720 bool
member_has_flags(const struct ChannelMember * member,const unsigned int flags)721 member_has_flags(const struct ChannelMember *member, const unsigned int flags)
722 {
723 if (member && (member->flags & flags))
724 return true;
725 return false;
726 }
727
728 struct ChannelMember *
member_find_link(const struct Client * client,const struct Channel * channel)729 member_find_link(const struct Client *client, const struct Channel *channel)
730 {
731 dlink_node *node;
732
733 if (!IsClient(client))
734 return NULL;
735
736 /* Take the shortest of the two lists */
737 if (dlink_list_length(&channel->members) < dlink_list_length(&client->channel))
738 {
739 DLINK_FOREACH(node, channel->members.head)
740 if (((struct ChannelMember *)node->data)->client == client)
741 return node->data;
742 }
743 else
744 {
745 DLINK_FOREACH(node, client->channel.head)
746 if (((struct ChannelMember *)node->data)->channel == channel)
747 return node->data;
748 }
749
750 return NULL;
751 }
752
753 /*! Checks if a message contains control codes
754 * \param message The actual message string the client wants to send
755 * \return 1 if the message does contain any control codes, 0 otherwise
756 */
757 static bool
msg_has_ctrls(const char * message)758 msg_has_ctrls(const char *message)
759 {
760 const unsigned char *p = (const unsigned char *)message;
761
762 for (; *p; ++p)
763 {
764 if (*p > 31 || *p == 1)
765 continue; /* No control code or CTCP */
766
767 if (*p == 27) /* Escape */
768 {
769 /* ISO 2022 charset shift sequence */
770 if (*(p + 1) == '$' ||
771 *(p + 1) == '(')
772 {
773 ++p;
774 continue;
775 }
776 }
777
778 return true; /* Control code */
779 }
780
781 return false; /* No control code found */
782 }
783
784 /*! Tests if a client can send to a channel
785 * \param channel Pointer to Channel struct
786 * \param client Pointer to Client struct
787 * \param member Pointer to Membership struct (can be NULL)
788 * \param message The actual message string the client wants to send
789 * \return CAN_SEND_OPV if op or voiced on channel\n
790 * CAN_SEND_NONOP if can send to channel but is not an op\n
791 * ERR_CANNOTSENDTOCHAN or ERR_NEEDREGGEDNICK if they cannot send to channel\n
792 */
793 int
can_send(struct Channel * channel,struct Client * client,struct ChannelMember * member,const char * message,bool notice)794 can_send(struct Channel *channel, struct Client *client,
795 struct ChannelMember *member, const char *message, bool notice)
796 {
797 const struct ResvItem *resv;
798
799 if (IsServer(client) || HasFlag(client, FLAGS_SERVICE))
800 return CAN_SEND_OPV;
801
802 if (MyConnect(client) && !HasFlag(client, FLAGS_EXEMPTRESV))
803 if (!(HasUMode(client, UMODE_OPER) && HasOFlag(client, OPER_FLAG_JOIN_RESV)))
804 if ((resv = resv_find(channel->name, match)) && resv_exempt_find(client, resv) == false)
805 return ERR_CANNOTSENDTOCHAN;
806
807 if (HasCMode(channel, MODE_NOCTRL) && msg_has_ctrls(message) == true)
808 return ERR_NOCTRLSONCHAN;
809
810 if (HasCMode(channel, MODE_NOCTCP))
811 if (*message == '\001' && strncmp(message + 1, "ACTION ", 7))
812 return ERR_NOCTCP;
813
814 if (member || (member = member_find_link(client, channel)))
815 if (member->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE))
816 return CAN_SEND_OPV;
817
818 if (member == NULL && HasCMode(channel, MODE_NOPRIVMSGS))
819 return ERR_CANNOTSENDTOCHAN;
820
821 if (HasCMode(channel, MODE_MODERATED))
822 return ERR_CANNOTSENDTOCHAN;
823
824 if (HasCMode(channel, MODE_MODREG) && !HasUMode(client, UMODE_REGISTERED))
825 return ERR_NEEDREGGEDNICK;
826
827 if (HasCMode(channel, MODE_NONOTICE) && notice == true)
828 return ERR_CANNOTSENDTOCHAN;
829
830 /* Cache can send if banned */
831 if (MyConnect(client))
832 {
833 if (member)
834 {
835 if (member->flags & CHFL_BAN_SILENCED)
836 return ERR_CANNOTSENDTOCHAN;
837
838 if (!(member->flags & CHFL_BAN_CHECKED))
839 {
840 if (is_banned(channel, client) == true)
841 {
842 member->flags |= (CHFL_BAN_CHECKED | CHFL_BAN_SILENCED);
843 return ERR_CANNOTSENDTOCHAN;
844 }
845
846 member->flags |= CHFL_BAN_CHECKED;
847 }
848 }
849 else if (is_banned(channel, client) == true)
850 return ERR_CANNOTSENDTOCHAN;
851 }
852
853 return extban_mute_can_send(channel, client, member);
854 }
855
856 /*! \brief Updates the client's oper_warn_count_down, warns the
857 * IRC operators if necessary, and updates
858 * join_leave_countdown as needed.
859 * \param client Pointer to struct Client to check
860 * \param name Channel name or NULL if this is a part.
861 */
862 void
check_spambot_warning(struct Client * client,const char * name)863 check_spambot_warning(struct Client *client, const char *name)
864 {
865 if (GlobalSetOptions.spam_num &&
866 (client->connection->join_leave_count >= GlobalSetOptions.spam_num))
867 {
868 if (client->connection->oper_warn_count_down)
869 --client->connection->oper_warn_count_down;
870
871 if (client->connection->oper_warn_count_down == 0)
872 {
873 client->connection->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
874
875 /* It's already known as a possible spambot */
876 if (name)
877 sendto_realops_flags(UMODE_FLOOD, L_ALL, SEND_NOTICE,
878 "User %s (%s@%s) trying to join %s is a possible spambot",
879 client->name, client->username,
880 client->host, name);
881 else
882 sendto_realops_flags(UMODE_FLOOD, L_ALL, SEND_NOTICE,
883 "User %s (%s@%s) is a possible spambot",
884 client->name, client->username,
885 client->host);
886 }
887 }
888 else
889 {
890 unsigned int t_delta = event_base->time.sec_monotonic - client->connection->last_leave_time;
891 if (t_delta > JOIN_LEAVE_COUNT_EXPIRE_TIME)
892 {
893 unsigned int decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
894 if (decrement_count > client->connection->join_leave_count)
895 client->connection->join_leave_count = 0;
896 else
897 client->connection->join_leave_count -= decrement_count;
898 }
899 else
900 {
901 if ((event_base->time.sec_monotonic - client->connection->last_join_time) < GlobalSetOptions.spam_time)
902 ++client->connection->join_leave_count; /* It's a possible spambot */
903 }
904
905 if (name)
906 client->connection->last_join_time = event_base->time.sec_monotonic;
907 else
908 client->connection->last_leave_time = event_base->time.sec_monotonic;
909 }
910 }
911
912 /*! \brief Sets the channel topic for a certain channel
913 * \param channel Pointer to struct Channel
914 * \param topic The topic string
915 * \param topic_info n!u\@h formatted string of the topic setter
916 * \param topicts Timestamp on the topic
917 * \param local Whether the topic is set by a local client
918 */
919 void
channel_set_topic(struct Channel * channel,const char * topic,const char * topic_info,uintmax_t topicts,bool local)920 channel_set_topic(struct Channel *channel, const char *topic,
921 const char *topic_info, uintmax_t topicts, bool local)
922 {
923 if (local == true)
924 strlcpy(channel->topic, topic, IRCD_MIN(sizeof(channel->topic), ConfigServerInfo.max_topic_length + 1));
925 else
926 strlcpy(channel->topic, topic, sizeof(channel->topic));
927
928 strlcpy(channel->topic_info, topic_info, sizeof(channel->topic_info));
929 channel->topic_time = topicts;
930 }
931
932 void
channel_do_join(struct Client * client,char * chan_list,char * key_list)933 channel_do_join(struct Client *client, char *chan_list, char *key_list)
934 {
935 char *p = NULL;
936 const struct ResvItem *resv = NULL;
937 const struct ClassItem *const class = class_get_ptr(&client->connection->confs);
938 unsigned int flags = 0;
939
940 assert(MyClient(client));
941
942 for (const char *name = strtok_r(chan_list, ",", &p); name;
943 name = strtok_r(NULL, ",", &p))
944 {
945 const char *key = NULL;
946
947 /* If we have any more keys, take the first for this channel. */
948 if (!EmptyString(key_list) && (key_list = strchr(key = key_list, ',')))
949 *key_list++ = '\0';
950
951 /* Empty keys are the same as no keys. */
952 if (key && *key == '\0')
953 key = NULL;
954
955 if (channel_check_name(name, true) == false)
956 {
957 sendto_one_numeric(client, &me, ERR_BADCHANNAME, name);
958 continue;
959 }
960
961 if (!HasFlag(client, FLAGS_EXEMPTRESV) &&
962 !(HasUMode(client, UMODE_OPER) && HasOFlag(client, OPER_FLAG_JOIN_RESV)) &&
963 ((resv = resv_find(name, match)) && resv_exempt_find(client, resv) == false))
964 {
965 sendto_one_numeric(client, &me, ERR_CHANBANREASON, name, resv->reason);
966 sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE,
967 "Forbidding reserved channel %s from user %s",
968 name, client_get_name(client, HIDE_IP));
969 continue;
970 }
971
972 if (dlink_list_length(&client->channel) >=
973 ((class->max_channels) ? class->max_channels : ConfigChannel.max_channels))
974 {
975 sendto_one_numeric(client, &me, ERR_TOOMANYCHANNELS, name);
976 break;
977 }
978
979 struct Channel *channel = hash_find_channel(name);
980 if (channel)
981 {
982 if (member_find_link(client, channel))
983 continue;
984
985 /* can_join() checks for +i, +l, key, bans, etc. */
986 int ret = can_join(client, channel, key);
987 if (ret)
988 {
989 sendto_one_numeric(client, &me, ret, channel->name);
990 continue;
991 }
992
993 /*
994 * This should never be the case unless there is some sort of
995 * persistent channels.
996 */
997 if (dlink_list_length(&channel->members) == 0)
998 flags = CHFL_CHANOP;
999 else
1000 flags = 0;
1001 }
1002 else
1003 {
1004 flags = CHFL_CHANOP;
1005 channel = channel_make(name);
1006 }
1007
1008 if (!HasUMode(client, UMODE_OPER))
1009 check_spambot_warning(client, channel->name);
1010
1011 add_user_to_channel(channel, client, flags, true);
1012
1013 /*
1014 * Set timestamp if appropriate, and propagate
1015 */
1016 if (flags == CHFL_CHANOP)
1017 {
1018 channel->creation_time = event_base->time.sec_real;
1019 AddCMode(channel, MODE_TOPICLIMIT);
1020 AddCMode(channel, MODE_NOPRIVMSGS);
1021
1022 sendto_server(NULL, 0, 0, ":%s SJOIN %ju %s +nt :@%s",
1023 me.id, channel->creation_time,
1024 channel->name, client->id);
1025
1026 /*
1027 * Notify all other users on the new channel
1028 */
1029 sendto_channel_local(NULL, channel, 0, CAP_EXTENDED_JOIN, 0, ":%s!%s@%s JOIN %s %s :%s",
1030 client->name, client->username,
1031 client->host, channel->name, client->account, client->info);
1032 sendto_channel_local(NULL, channel, 0, 0, CAP_EXTENDED_JOIN, ":%s!%s@%s JOIN :%s",
1033 client->name, client->username,
1034 client->host, channel->name);
1035 sendto_channel_local(NULL, channel, 0, 0, 0, ":%s MODE %s +nt",
1036 me.name, channel->name);
1037 }
1038 else
1039 {
1040 sendto_server(NULL, 0, 0, ":%s JOIN %ju %s +",
1041 client->id, channel->creation_time,
1042 channel->name);
1043
1044 sendto_channel_local(NULL, channel, 0, CAP_EXTENDED_JOIN, 0, ":%s!%s@%s JOIN %s %s :%s",
1045 client->name, client->username,
1046 client->host, channel->name, client->account, client->info);
1047 sendto_channel_local(NULL, channel, 0, 0, CAP_EXTENDED_JOIN, ":%s!%s@%s JOIN :%s",
1048 client->name, client->username,
1049 client->host, channel->name);
1050 }
1051
1052 if (client->away[0])
1053 sendto_channel_local(client, channel, 0, CAP_AWAY_NOTIFY, 0,
1054 ":%s!%s@%s AWAY :%s",
1055 client->name, client->username,
1056 client->host, client->away);
1057
1058 struct Invite *invite = invite_find(channel, client);
1059 if (invite)
1060 invite_del(invite);
1061
1062 if (channel->topic[0])
1063 {
1064 sendto_one_numeric(client, &me, RPL_TOPIC, channel->name, channel->topic);
1065 sendto_one_numeric(client, &me, RPL_TOPICWHOTIME, channel->name,
1066 channel->topic_info, channel->topic_time);
1067 }
1068
1069 channel_send_namereply(client, channel);
1070
1071 client->connection->last_join_time = event_base->time.sec_monotonic;
1072 }
1073 }
1074
1075 /*! \brief Removes a client from a specific channel
1076 * \param client Pointer to client to remove
1077 * \param name Name of channel to remove from
1078 * \param reason Part reason to show
1079 */
1080 static void
channel_part_one_client(struct Client * client,const char * name,const char * reason)1081 channel_part_one_client(struct Client *client, const char *name, const char *reason)
1082 {
1083 struct Channel *channel = hash_find_channel(name);
1084 if (channel == NULL)
1085 {
1086 sendto_one_numeric(client, &me, ERR_NOSUCHCHANNEL, name);
1087 return;
1088 }
1089
1090 struct ChannelMember *member = member_find_link(client, channel);
1091 if (member == NULL)
1092 {
1093 sendto_one_numeric(client, &me, ERR_NOTONCHANNEL, channel->name);
1094 return;
1095 }
1096
1097 if (MyConnect(client) && !HasUMode(client, UMODE_OPER))
1098 check_spambot_warning(client, NULL);
1099
1100 /*
1101 * Remove user from the old channel (if any). Only allow /part reasons in -m chans.
1102 */
1103 if (*reason && (!MyConnect(client) ||
1104 ((client->connection->created_monotonic +
1105 ConfigGeneral.anti_spam_exit_message_time) < event_base->time.sec_monotonic &&
1106 can_send(channel, client, member, reason, false) < 0)))
1107 {
1108 sendto_server(client, 0, 0, ":%s PART %s :%s",
1109 client->id, channel->name, reason);
1110 sendto_channel_local(NULL, channel, 0, 0, 0, ":%s!%s@%s PART %s :%s",
1111 client->name, client->username,
1112 client->host, channel->name, reason);
1113 }
1114 else
1115 {
1116 sendto_server(client, 0, 0, ":%s PART %s",
1117 client->id, channel->name);
1118 sendto_channel_local(NULL, channel, 0, 0, 0, ":%s!%s@%s PART %s",
1119 client->name, client->username,
1120 client->host, channel->name);
1121 }
1122
1123 remove_user_from_channel(member);
1124 }
1125
1126 void
channel_do_part(struct Client * client,char * channel,const char * reason)1127 channel_do_part(struct Client *client, char *channel, const char *reason)
1128 {
1129 char *p = NULL;
1130 char buf[KICKLEN + 1] = ""; /* Essential that buf[0] = '\0' */
1131
1132 assert(IsClient(client));
1133
1134 if (!EmptyString(reason))
1135 strlcpy(buf, reason, sizeof(buf));
1136
1137 for (const char *name = strtok_r(channel, ",", &p); name;
1138 name = strtok_r(NULL, ",", &p))
1139 channel_part_one_client(client, name, buf);
1140 }
1141