ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 6990
Committed: Wed Dec 30 19:14:34 2015 UTC (8 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 33422 byte(s)
Log Message:
- Remove trailing whitespaces

File Contents

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

Properties

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