ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/channel.c
Revision: 4152
Committed: Wed Jul 2 17:45:06 2014 UTC (9 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 33014 byte(s)
Log Message:
- channel.c: from p4: move resv channel oper notice to umode reject

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

Properties

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