ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 7797
Committed: Tue Oct 18 17:27:10 2016 UTC (8 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 33809 byte(s)
Log Message:
- Minor style corrections and constifications

File Contents

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

Properties

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