ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
Revision: 4388
Committed: Sun Aug 3 17:45:49 2014 UTC (11 years ago) by michael
Content type: text/x-csrc
File size: 33211 byte(s)
Log Message:
- channel.c: moved 'buf' to send_members() and send_mode_list()

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

Properties

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