ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 8044
Committed: Sat Mar 18 16:52:10 2017 UTC (8 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 33735 byte(s)
Log Message:
- Add chptr->mode.mode manipulation macros

File Contents

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

Properties

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