ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/channel.c
Revision: 3934
Committed: Mon Jun 9 17:59:22 2014 UTC (9 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 33033 byte(s)
Log Message:
- Added max_channels to class{} blocks

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

Properties

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