ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 8926
Committed: Mon Apr 22 11:08:36 2019 UTC (6 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 32813 byte(s)
Log Message:
- Killed CurrentTime

File Contents

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

Properties

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