ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 4086
Committed: Sat Jun 28 16:44:20 2014 UTC (11 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 32933 byte(s)
Log Message:
- Let mp_pool_get() clear memory

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

Properties

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