1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* |
4 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
5 |
* |
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 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "conf/conf.h" |
29 |
#include "channel.h" |
30 |
#include "channel_mode.h" |
31 |
#include "client.h" |
32 |
#include "common.h" |
33 |
#include "hash.h" |
34 |
#include "ircd.h" |
35 |
#include "numeric.h" |
36 |
#include "server.h" /* captab */ |
37 |
#include "user.h" |
38 |
#include "send.h" |
39 |
|
40 |
dlink_list global_channel_list = { NULL, NULL, 0 }; |
41 |
BlockHeap *ban_heap; /*! \todo ban_heap shouldn't be a global var */ |
42 |
|
43 |
static BlockHeap *topic_heap = NULL; |
44 |
static BlockHeap *member_heap = NULL; |
45 |
static BlockHeap *channel_heap = NULL; |
46 |
|
47 |
static char buf[IRCD_BUFSIZE]; |
48 |
static char modebuf[MODEBUFLEN]; |
49 |
static char parabuf[MODEBUFLEN]; |
50 |
|
51 |
|
52 |
/*! \brief Initializes the channel blockheap, adds known channel CAPAB |
53 |
*/ |
54 |
void |
55 |
channel_init(void) |
56 |
{ |
57 |
/* |
58 |
* XXX - These should get moved to somwhere else once we have |
59 |
* a modular channelmode system |
60 |
*/ |
61 |
add_capability("EX", CAP_EX, 1); |
62 |
add_capability("IE", CAP_IE, 1); |
63 |
add_capability("CHW", CAP_CHW, 1); |
64 |
|
65 |
channel_heap = BlockHeapCreate("channel", sizeof(struct Channel), CHANNEL_HEAP_SIZE); |
66 |
ban_heap = BlockHeapCreate("ban", sizeof(struct Ban), BAN_HEAP_SIZE); |
67 |
topic_heap = BlockHeapCreate("topic", TOPICLEN+1 + USERHOST_REPLYLEN, TOPIC_HEAP_SIZE); |
68 |
member_heap = BlockHeapCreate("member", sizeof(struct Membership), CHANNEL_HEAP_SIZE*2); |
69 |
} |
70 |
|
71 |
/*! \brief adds a user to a channel by adding another link to the |
72 |
* channels member chain. |
73 |
* \param chptr pointer to channel to add client to |
74 |
* \param who pointer to client (who) to add |
75 |
* \param flags flags for chanops etc |
76 |
* \param flood_ctrl whether to count this join in flood calculations |
77 |
*/ |
78 |
void |
79 |
add_user_to_channel(struct Channel *chptr, struct Client *who, |
80 |
unsigned int flags, int flood_ctrl) |
81 |
{ |
82 |
struct Membership *ms = NULL; |
83 |
|
84 |
if (GlobalSetOptions.joinfloodtime > 0) |
85 |
{ |
86 |
if (flood_ctrl) |
87 |
++chptr->number_joined; |
88 |
|
89 |
chptr->number_joined -= (CurrentTime - chptr->last_join_time) * |
90 |
(((float)GlobalSetOptions.joinfloodcount) / |
91 |
(float)GlobalSetOptions.joinfloodtime); |
92 |
|
93 |
if (chptr->number_joined <= 0) |
94 |
{ |
95 |
chptr->number_joined = 0; |
96 |
ClearJoinFloodNoticed(chptr); |
97 |
} |
98 |
else if (chptr->number_joined >= GlobalSetOptions.joinfloodcount) |
99 |
{ |
100 |
chptr->number_joined = GlobalSetOptions.joinfloodcount; |
101 |
|
102 |
if (!IsSetJoinFloodNoticed(chptr)) |
103 |
{ |
104 |
SetJoinFloodNoticed(chptr); |
105 |
sendto_realops_flags(UMODE_BOTS, L_ALL, |
106 |
"Possible Join Flooder %s on %s target: %s", |
107 |
get_client_name(who, HIDE_IP), |
108 |
who->servptr->name, chptr->chname); |
109 |
} |
110 |
} |
111 |
|
112 |
chptr->last_join_time = CurrentTime; |
113 |
} |
114 |
|
115 |
ms = BlockHeapAlloc(member_heap); |
116 |
ms->client_p = who; |
117 |
ms->chptr = chptr; |
118 |
ms->flags = flags; |
119 |
|
120 |
dlinkAdd(ms, &ms->channode, &chptr->members); |
121 |
dlinkAdd(ms, &ms->usernode, &who->channel); |
122 |
} |
123 |
|
124 |
/*! \brief deletes an user from a channel by removing a link in the |
125 |
* channels member chain. |
126 |
* \param member pointer to Membership struct |
127 |
*/ |
128 |
void |
129 |
remove_user_from_channel(struct Membership *member) |
130 |
{ |
131 |
struct Client *client_p = member->client_p; |
132 |
struct Channel *chptr = member->chptr; |
133 |
|
134 |
dlinkDelete(&member->channode, &chptr->members); |
135 |
dlinkDelete(&member->usernode, &client_p->channel); |
136 |
|
137 |
BlockHeapFree(member_heap, member); |
138 |
|
139 |
if (chptr->members.head == NULL) |
140 |
{ |
141 |
assert(dlink_list_length(&chptr->members) == 0); |
142 |
destroy_channel(chptr); |
143 |
} |
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, const struct Channel *chptr, |
154 |
char *lmodebuf, char *lparabuf) |
155 |
{ |
156 |
const dlink_node *ptr = NULL; |
157 |
int tlen; // length of text to append |
158 |
char *t, *start; // temp char pointer |
159 |
|
160 |
start = t = buf + ircsprintf(buf, ":%s SJOIN %lu %s %s %s:", |
161 |
ID_or_name(&me, client_p), |
162 |
(unsigned long)chptr->channelts, |
163 |
chptr->chname, lmodebuf, lparabuf); |
164 |
|
165 |
DLINK_FOREACH(ptr, chptr->members.head) |
166 |
{ |
167 |
const struct Membership *ms = ptr->data; |
168 |
|
169 |
tlen = strlen(IsCapable(client_p, CAP_TS6) ? |
170 |
ID(ms->client_p) : ms->client_p->name) + 1; // nick + space |
171 |
|
172 |
if (ms->flags & CHFL_CHANOP) |
173 |
++tlen; |
174 |
#ifdef HALFOPS |
175 |
else if (ms->flags & CHFL_HALFOP) |
176 |
++tlen; |
177 |
#endif |
178 |
if (ms->flags & CHFL_VOICE) |
179 |
++tlen; |
180 |
|
181 |
/* |
182 |
* space will be converted into CR, but we also need space for LF.. |
183 |
* That's why we use '- 1' here |
184 |
* -adx |
185 |
*/ |
186 |
if (t + tlen - buf > sizeof(buf) - 1) |
187 |
{ |
188 |
*(t - 1) = '\0'; // kill the space and terminate the string |
189 |
sendto_one(client_p, "%s", buf); |
190 |
t = start; |
191 |
} |
192 |
|
193 |
if ((ms->flags & (CHFL_CHANOP | CHFL_HALFOP))) |
194 |
*t++ = (!(ms->flags & CHFL_CHANOP) && IsCapable(client_p, CAP_HOPS)) ? |
195 |
'%' : '@'; |
196 |
if ((ms->flags & CHFL_VOICE)) |
197 |
*t++ = '+'; |
198 |
|
199 |
if (IsCapable(client_p, CAP_TS6)) |
200 |
strcpy(t, ID(ms->client_p)); |
201 |
else |
202 |
strcpy(t, ms->client_p->name); |
203 |
t += strlen(t); |
204 |
*t++ = ' '; |
205 |
} |
206 |
|
207 |
// should always be non-NULL unless we have a kind of persistent channels |
208 |
if (chptr->members.head != NULL) |
209 |
--t; // take the space out |
210 |
*t = '\0'; |
211 |
sendto_one(client_p, "%s", buf); |
212 |
} |
213 |
|
214 |
/*! \brief sends +b/+e/+I |
215 |
* \param client_p client pointer to server |
216 |
* \param chptr pointer to channel |
217 |
* \param top pointer to top of mode link list to send |
218 |
* \param flag char flag flagging type of mode. Currently this can be 'b', e' or 'I' |
219 |
*/ |
220 |
static void |
221 |
send_mode_list(struct Client *client_p, const struct Channel *chptr, |
222 |
dlink_list *top, char flag) |
223 |
{ |
224 |
int ts5 = !IsCapable(client_p, CAP_TS6); |
225 |
const dlink_node *lp = NULL; |
226 |
char pbuf[IRCD_BUFSIZE]; |
227 |
int tlen, mlen, cur_len, count = 0; |
228 |
char *mp = NULL, *pp = pbuf; |
229 |
|
230 |
if (top->length == 0) |
231 |
return; |
232 |
|
233 |
if (ts5) |
234 |
mlen = ircsprintf(buf, ":%s MODE %s +", me.name, chptr->chname); |
235 |
else |
236 |
mlen = ircsprintf(buf, ":%s BMASK %lu %s %c :", me.id, |
237 |
(unsigned long)chptr->channelts, chptr->chname, flag); |
238 |
|
239 |
// MODE needs additional one byte for space between buf and pbuf |
240 |
cur_len = mlen + ts5; |
241 |
mp = buf + mlen; |
242 |
|
243 |
DLINK_FOREACH(lp, top->head) |
244 |
{ |
245 |
const struct Ban *banptr = lp->data; |
246 |
|
247 |
// must add another b/e/I letter if we use MODE |
248 |
tlen = banptr->len + 3 + ts5; |
249 |
|
250 |
/* |
251 |
* send buffer and start over if we cannot fit another ban, |
252 |
* or if the target is non-ts6 and we have too many modes in |
253 |
* in this line. |
254 |
*/ |
255 |
if (cur_len + (tlen - 1) > IRCD_BUFSIZE - 2 || |
256 |
(!IsCapable(client_p, CAP_TS6) && |
257 |
(count >= MAXMODEPARAMS || pp - pbuf >= MODEBUFLEN))) |
258 |
{ |
259 |
*(pp - 1) = '\0'; // get rid of trailing space on buffer |
260 |
sendto_one(client_p, "%s%s%s", buf, ts5 ? " " : "", pbuf); |
261 |
|
262 |
cur_len = mlen + ts5; |
263 |
mp = buf + mlen; |
264 |
pp = pbuf; |
265 |
count = 0; |
266 |
} |
267 |
|
268 |
++count; |
269 |
|
270 |
if (ts5) |
271 |
{ |
272 |
*mp++ = flag; |
273 |
*mp = '\0'; |
274 |
} |
275 |
|
276 |
pp += ircsprintf(pp, "%s!%s@%s ", banptr->name, banptr->username, |
277 |
banptr->host); |
278 |
cur_len += tlen; |
279 |
} |
280 |
|
281 |
*(pp - 1) = '\0'; // get rid of trailing space on buffer |
282 |
sendto_one(client_p, "%s%s%s", buf, ts5 ? " " : "", pbuf); |
283 |
} |
284 |
|
285 |
/*! \brief send "client_p" a full list of the modes for channel chptr |
286 |
* \param client_p pointer to client client_p |
287 |
* \param chptr pointer to channel pointer |
288 |
*/ |
289 |
void |
290 |
send_channel_modes(struct Client *client_p, struct Channel *chptr) |
291 |
{ |
292 |
if (chptr->chname[0] != '#') |
293 |
return; |
294 |
|
295 |
*modebuf = *parabuf = '\0'; |
296 |
channel_modes(chptr, client_p, modebuf, parabuf); |
297 |
send_members(client_p, chptr, modebuf, parabuf); |
298 |
|
299 |
send_mode_list(client_p, chptr, &chptr->banlist, 'b'); |
300 |
|
301 |
if (IsCapable(client_p, CAP_EX)) |
302 |
send_mode_list(client_p, chptr, &chptr->exceptlist, 'e'); |
303 |
if (IsCapable(client_p, CAP_IE)) |
304 |
send_mode_list(client_p, chptr, &chptr->invexlist, 'I'); |
305 |
} |
306 |
|
307 |
/*! \brief check channel name for invalid characters |
308 |
* \param name pointer to channel name string |
309 |
* \param local indicates whether it's a local or remote creation |
310 |
* \return 0 if invalid, 1 otherwise |
311 |
*/ |
312 |
int |
313 |
check_channel_name(const char *name, int local) |
314 |
{ |
315 |
const char *p = name; |
316 |
int max_length = local ? LOCAL_CHANNELLEN : CHANNELLEN; |
317 |
assert(name != NULL); |
318 |
|
319 |
if (!IsChanPrefix(*p)) |
320 |
return 0; |
321 |
|
322 |
if (!local || !Channel.disable_fake_channels) |
323 |
{ |
324 |
while (*++p) |
325 |
if (!IsChanChar(*p)) |
326 |
return 0; |
327 |
} |
328 |
else |
329 |
{ |
330 |
while (*++p) |
331 |
if (!IsVisibleChanChar(*p)) |
332 |
return 0; |
333 |
} |
334 |
|
335 |
return p - name <= max_length; |
336 |
} |
337 |
|
338 |
void |
339 |
remove_ban(struct Ban *bptr, dlink_list *list) |
340 |
{ |
341 |
dlinkDelete(&bptr->node, list); |
342 |
|
343 |
MyFree(bptr->name); |
344 |
MyFree(bptr->username); |
345 |
MyFree(bptr->host); |
346 |
MyFree(bptr->who); |
347 |
|
348 |
BlockHeapFree(ban_heap, bptr); |
349 |
} |
350 |
|
351 |
/* free_channel_list() |
352 |
* |
353 |
* inputs - pointer to dlink_list |
354 |
* output - NONE |
355 |
* side effects - |
356 |
*/ |
357 |
void |
358 |
free_channel_list(dlink_list *list) |
359 |
{ |
360 |
dlink_node *ptr = NULL, *next_ptr = NULL; |
361 |
|
362 |
DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
363 |
remove_ban(ptr->data, list); |
364 |
|
365 |
assert(list->tail == NULL && list->head == NULL); |
366 |
} |
367 |
|
368 |
/*! \brief Allocates and initializes a new Channel structure |
369 |
* \param chname channel name |
370 |
* \return channel block |
371 |
*/ |
372 |
struct Channel * |
373 |
make_channel(const char *chname) |
374 |
{ |
375 |
struct Channel *chptr = NULL; |
376 |
|
377 |
assert(!EmptyString(chname)); |
378 |
|
379 |
chptr = BlockHeapAlloc(channel_heap); |
380 |
|
381 |
// doesn't hurt to set it here |
382 |
chptr->channelts = CurrentTime; |
383 |
chptr->last_join_time = CurrentTime; |
384 |
|
385 |
strlcpy(chptr->chname, chname, sizeof(chptr->chname)); |
386 |
dlinkAdd(chptr, &chptr->node, &global_channel_list); |
387 |
|
388 |
hash_add_channel(chptr); |
389 |
|
390 |
return chptr; |
391 |
} |
392 |
|
393 |
/*! \brief walk through this channel, and destroy it. |
394 |
* \param chptr channel pointer |
395 |
*/ |
396 |
void |
397 |
destroy_channel(struct Channel *chptr) |
398 |
{ |
399 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
400 |
|
401 |
DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->invites.head) |
402 |
del_invite(chptr, ptr->data); |
403 |
|
404 |
free_channel_list(&chptr->banlist); |
405 |
free_channel_list(&chptr->exceptlist); |
406 |
free_channel_list(&chptr->invexlist); |
407 |
|
408 |
free_topic(chptr); |
409 |
|
410 |
dlinkDelete(&chptr->node, &global_channel_list); |
411 |
hash_del_channel(chptr); |
412 |
|
413 |
BlockHeapFree(channel_heap, chptr); |
414 |
} |
415 |
|
416 |
/*! |
417 |
* \param chptr pointer to channel |
418 |
* \return string pointer "=" if public, "@" if secret else "*" |
419 |
*/ |
420 |
static const char * |
421 |
channel_pub_or_secret(const struct Channel *chptr) |
422 |
{ |
423 |
if (SecretChannel(chptr)) |
424 |
return "@"; |
425 |
if (ParanoidChannel(chptr)) |
426 |
return "*"; |
427 |
return "="; |
428 |
} |
429 |
|
430 |
/*! \brief lists all names on given channel |
431 |
* \param source_p pointer to client struct requesting names |
432 |
* \param chptr pointer to channel block |
433 |
* \param show_eon show ENDOFNAMES numeric or not |
434 |
* (don't want it with /names with no params) |
435 |
*/ |
436 |
void |
437 |
channel_member_names(struct Client *source_p, struct Channel *chptr, |
438 |
int show_eon) |
439 |
{ |
440 |
const dlink_node *ptr = NULL; |
441 |
char lbuf[IRCD_BUFSIZE + 1]; |
442 |
char *t = NULL, *start = NULL; |
443 |
int tlen = 0; |
444 |
int is_member = IsMember(source_p, chptr); |
445 |
int multi_prefix = (source_p->localClient->cap_active & CAP_MULTI_PREFIX) != 0; |
446 |
|
447 |
if (PubChannel(chptr) || is_member) |
448 |
{ |
449 |
start = t = lbuf + ircsprintf(lbuf, form_str(RPL_NAMREPLY), |
450 |
me.name, source_p->name, |
451 |
channel_pub_or_secret(chptr), |
452 |
chptr->chname); |
453 |
|
454 |
DLINK_FOREACH(ptr, chptr->members.head) |
455 |
{ |
456 |
const struct Membership *ms = ptr->data; |
457 |
const struct Client *target_p = ms->client_p; |
458 |
|
459 |
if (IsInvisible(target_p) && !is_member) |
460 |
continue; |
461 |
|
462 |
tlen = strlen(target_p->name) + 1; // nick + space |
463 |
|
464 |
if (!multi_prefix) |
465 |
{ |
466 |
if (ms->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE)) |
467 |
++tlen; |
468 |
} |
469 |
else |
470 |
{ |
471 |
if (ms->flags & CHFL_CHANOP) |
472 |
++tlen; |
473 |
if (ms->flags & CHFL_HALFOP) |
474 |
++tlen; |
475 |
if (ms->flags & CHFL_VOICE) |
476 |
++tlen; |
477 |
} |
478 |
|
479 |
if (t + tlen - lbuf > IRCD_BUFSIZE - 2) |
480 |
{ |
481 |
*(t - 1) = '\0'; |
482 |
sendto_one(source_p, "%s", lbuf); |
483 |
t = start; |
484 |
} |
485 |
|
486 |
t += ircsprintf(t, "%s%s ", get_member_status(ms, multi_prefix), |
487 |
target_p->name); |
488 |
} |
489 |
|
490 |
if (tlen != 0) |
491 |
{ |
492 |
*(t - 1) = '\0'; |
493 |
sendto_one(source_p, "%s", lbuf); |
494 |
} |
495 |
} |
496 |
|
497 |
if (show_eon) |
498 |
sendto_one(source_p, form_str(RPL_ENDOFNAMES), me.name, |
499 |
source_p->name, chptr->chname); |
500 |
} |
501 |
|
502 |
/*! \brief adds client to invite list |
503 |
* \param chptr pointer to channel block |
504 |
* \param who pointer to client to add invite to |
505 |
*/ |
506 |
void |
507 |
add_invite(struct Channel *chptr, struct Client *who) |
508 |
{ |
509 |
del_invite(chptr, who); |
510 |
|
511 |
// delete last link in chain if the list is max length |
512 |
if (dlink_list_length(&who->localClient->invited) >= |
513 |
Channel.max_chans_per_user) |
514 |
del_invite(who->localClient->invited.tail->data, who); |
515 |
|
516 |
// add client to channel invite list |
517 |
dlinkAdd(who, make_dlink_node(), &chptr->invites); |
518 |
|
519 |
// add channel to the end of the client invite list |
520 |
dlinkAdd(chptr, make_dlink_node(), &who->localClient->invited); |
521 |
} |
522 |
|
523 |
/*! \brief Delete Invite block from channel invite list |
524 |
* and client invite list |
525 |
* \param chptr pointer to Channel struct |
526 |
* \param who pointer to client to remove invites from |
527 |
*/ |
528 |
void |
529 |
del_invite(struct Channel *chptr, struct Client *who) |
530 |
{ |
531 |
dlink_node *ptr = NULL; |
532 |
|
533 |
if ((ptr = dlinkFindDelete(&who->localClient->invited, chptr))) |
534 |
free_dlink_node(ptr); |
535 |
|
536 |
if ((ptr = dlinkFindDelete(&chptr->invites, who))) |
537 |
free_dlink_node(ptr); |
538 |
} |
539 |
|
540 |
/* get_member_status() |
541 |
* |
542 |
* inputs - pointer to struct Membership |
543 |
* - YES if we can combine different flags |
544 |
* output - string either @, +, % or "" depending on whether |
545 |
* chanop, voiced or user |
546 |
* side effects - |
547 |
* |
548 |
* NOTE: Returned string is usually a static buffer |
549 |
* (like in get_client_name) |
550 |
*/ |
551 |
const char * |
552 |
get_member_status(const struct Membership *ms, int combine) |
553 |
{ |
554 |
static char buffer[4]; |
555 |
char *p = NULL; |
556 |
|
557 |
if (ms == NULL) |
558 |
return ""; |
559 |
p = buffer; |
560 |
|
561 |
if (ms->flags & CHFL_CHANOP) |
562 |
{ |
563 |
if (!combine) |
564 |
return "@"; |
565 |
*p++ = '@'; |
566 |
} |
567 |
|
568 |
#ifdef HALFOPS |
569 |
if (ms->flags & CHFL_HALFOP) |
570 |
{ |
571 |
if (!combine) |
572 |
return "%"; |
573 |
*p++ = '%'; |
574 |
} |
575 |
#endif |
576 |
|
577 |
if (ms->flags & CHFL_VOICE) |
578 |
*p++ = '+'; |
579 |
*p = '\0'; |
580 |
|
581 |
return buffer; |
582 |
} |
583 |
|
584 |
/*! |
585 |
* \param who pointer to Client to check |
586 |
* \param list pointer to ban list to search |
587 |
* \return 1 if ban found for given n!u\@h mask, 0 otherwise |
588 |
*/ |
589 |
int |
590 |
find_bmask(const struct Client *who, const dlink_list *const list) |
591 |
{ |
592 |
const dlink_node *ptr = NULL; |
593 |
|
594 |
DLINK_FOREACH(ptr, list->head) |
595 |
{ |
596 |
struct Ban *bp = ptr->data; |
597 |
|
598 |
if (match(bp->name, who->name) && match(bp->username, who->username)) |
599 |
{ |
600 |
switch (bp->type) |
601 |
{ |
602 |
case HM_HOST: |
603 |
if (match(bp->host, who->host) || match(bp->host, who->sockhost)) |
604 |
return 1; |
605 |
break; |
606 |
case HM_IPV4: |
607 |
if (who->localClient->aftype == AF_INET) |
608 |
if (match_ipv4(&who->localClient->ip, &bp->addr, bp->bits)) |
609 |
return 1; |
610 |
break; |
611 |
#ifdef IPV6 |
612 |
case HM_IPV6: |
613 |
if (who->localClient->aftype == AF_INET6) |
614 |
if (match_ipv6(&who->localClient->ip, &bp->addr, bp->bits)) |
615 |
return 1; |
616 |
break; |
617 |
#endif |
618 |
default: |
619 |
assert(0); |
620 |
} |
621 |
} |
622 |
} |
623 |
|
624 |
return 0; |
625 |
} |
626 |
|
627 |
/*! |
628 |
* \param chptr pointer to channel block |
629 |
* \param who pointer to client to check access fo |
630 |
* \return 0 if not banned, 1 otherwise |
631 |
*/ |
632 |
int |
633 |
is_banned(const struct Channel *chptr, const struct Client *who) |
634 |
{ |
635 |
assert(IsClient(who)); |
636 |
|
637 |
if (find_bmask(who, &chptr->banlist)) |
638 |
if (!Channel.use_except || !find_bmask(who, &chptr->exceptlist)) |
639 |
return 1; |
640 |
|
641 |
return 0; |
642 |
} |
643 |
|
644 |
int |
645 |
has_member_flags(const struct Membership *ms, unsigned int flags) |
646 |
{ |
647 |
if (ms != NULL) |
648 |
return ms->flags & flags; |
649 |
return 0; |
650 |
} |
651 |
|
652 |
struct Membership * |
653 |
find_channel_link(struct Client *client_p, struct Channel *chptr) |
654 |
{ |
655 |
dlink_node *ptr = NULL; |
656 |
|
657 |
if (!IsClient(client_p)) |
658 |
return NULL; |
659 |
|
660 |
DLINK_FOREACH(ptr, client_p->channel.head) |
661 |
if (((struct Membership *)ptr->data)->chptr == chptr) |
662 |
return (struct Membership *)ptr->data; |
663 |
|
664 |
return NULL; |
665 |
} |
666 |
|
667 |
/*! |
668 |
* \param chptr pointer to Channel struct |
669 |
* \param source_p pointer to Client struct |
670 |
* \param ms pointer to Membership struct (can be NULL) |
671 |
* \return CAN_SEND_OPV if op or voiced on channel\n |
672 |
* CAN_SEND_NONOP if can send to channel but is not an op\n |
673 |
* CAN_SEND_NO if they cannot send to channel\n |
674 |
*/ |
675 |
int |
676 |
can_send(struct Channel *chptr, struct Client *source_p, struct Membership *ms) |
677 |
{ |
678 |
if (IsServer(source_p)) |
679 |
return CAN_SEND_OPV; |
680 |
|
681 |
if (MyClient(source_p) && !IsExemptResv(source_p)) |
682 |
if (!(IsOper(source_p) && General.oper_pass_resv)) |
683 |
if (!find_channel_resv(chptr->chname) == Channel.restrict_channels) |
684 |
return CAN_SEND_NO; |
685 |
|
686 |
if (ms == NULL) |
687 |
ms = find_channel_link(source_p, chptr); |
688 |
|
689 |
if (ms) |
690 |
{ |
691 |
if (ms->flags & (CHFL_CHANOP|CHFL_HALFOP|CHFL_VOICE)) |
692 |
return CAN_SEND_OPV; |
693 |
} |
694 |
else if (chptr->mode.mode & MODE_NOPRIVMSGS) |
695 |
return CAN_SEND_NO; |
696 |
|
697 |
if (chptr->mode.mode & MODE_MODERATED) |
698 |
return CAN_SEND_NO; |
699 |
|
700 |
// cache can send if possible and quiet_on_ban is enabled |
701 |
if (Channel.quiet_on_ban && MyClient(source_p)) |
702 |
{ |
703 |
if (! (ms && (ms->flags & CHFL_BAN_CHECKED))) |
704 |
{ // have to check |
705 |
if (is_banned(chptr, source_p)) |
706 |
{ |
707 |
if (ms) |
708 |
ms->flags |= (CHFL_BAN_CHECKED|CHFL_BAN_SILENCED); |
709 |
return CAN_SEND_NO; |
710 |
} |
711 |
if (ms) |
712 |
ms->flags |= CHFL_BAN_CHECKED; |
713 |
} |
714 |
else if (ms && (ms->flags & CHFL_BAN_SILENCED)) |
715 |
return CAN_SEND_NO; |
716 |
} |
717 |
|
718 |
return CAN_SEND_NONOP; |
719 |
} |
720 |
|
721 |
/*! \brief Updates the client's oper_warn_count_down, warns the |
722 |
* IRC operators if necessary, and updates |
723 |
* join_leave_countdown as needed. |
724 |
* \param source_p pointer to struct Client to check |
725 |
* \param name channel name or NULL if this is a part. |
726 |
*/ |
727 |
void |
728 |
check_spambot_warning(struct Client *source_p, const char *name) |
729 |
{ |
730 |
int t_delta = 0; |
731 |
int decrement_count = 0; |
732 |
|
733 |
if ((GlobalSetOptions.spam_num && |
734 |
(source_p->localClient->join_leave_count >= |
735 |
GlobalSetOptions.spam_num))) |
736 |
{ |
737 |
if (source_p->localClient->oper_warn_count_down > 0) |
738 |
source_p->localClient->oper_warn_count_down--; |
739 |
else |
740 |
source_p->localClient->oper_warn_count_down = 0; |
741 |
|
742 |
if (source_p->localClient->oper_warn_count_down == 0) |
743 |
{ |
744 |
// It's already known as a possible spambot |
745 |
if (name != NULL) |
746 |
sendto_realops_flags(UMODE_BOTS, L_ALL, |
747 |
"User %s (%s@%s) trying to join %s is a possible spambot", |
748 |
source_p->name, source_p->username, |
749 |
source_p->host, name); |
750 |
else |
751 |
sendto_realops_flags(UMODE_BOTS, L_ALL, |
752 |
"User %s (%s@%s) is a possible spambot", |
753 |
source_p->name, source_p->username, |
754 |
source_p->host); |
755 |
source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN; |
756 |
} |
757 |
} |
758 |
else |
759 |
{ |
760 |
if ((t_delta = (CurrentTime - source_p->localClient->last_leave_time)) > |
761 |
JOIN_LEAVE_COUNT_EXPIRE_TIME) |
762 |
{ |
763 |
decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME); |
764 |
if (decrement_count > source_p->localClient->join_leave_count) |
765 |
source_p->localClient->join_leave_count = 0; |
766 |
else |
767 |
source_p->localClient->join_leave_count -= decrement_count; |
768 |
} |
769 |
else |
770 |
{ |
771 |
if ((CurrentTime - (source_p->localClient->last_join_time)) < |
772 |
GlobalSetOptions.spam_time) |
773 |
{ |
774 |
// oh, it's a possible spambot |
775 |
++source_p->localClient->join_leave_count; |
776 |
} |
777 |
} |
778 |
|
779 |
if (name != NULL) |
780 |
source_p->localClient->last_join_time = CurrentTime; |
781 |
else |
782 |
source_p->localClient->last_leave_time = CurrentTime; |
783 |
} |
784 |
} |
785 |
|
786 |
/*! \brief compares usercount and servercount against their split |
787 |
* values and adjusts splitmode accordingly |
788 |
* \param unused Unused address pointer |
789 |
*/ |
790 |
void |
791 |
check_splitmode(void *unused) |
792 |
{ |
793 |
if (splitchecking && (Channel.no_join_on_split || |
794 |
Channel.no_create_on_split)) |
795 |
{ |
796 |
const unsigned int server = dlink_list_length(&global_serv_list); |
797 |
|
798 |
if (!splitmode && ((server < GlobalSetOptions.split_servers) || |
799 |
(Count.total < GlobalSetOptions.split_users))) |
800 |
{ |
801 |
splitmode = 1; |
802 |
|
803 |
sendto_realops_flags(UMODE_ALL,L_ALL, |
804 |
"Network split, activating splitmode"); |
805 |
eventAddIsh("check_splitmode", check_splitmode, NULL, 10); |
806 |
} |
807 |
else if (splitmode && (server > GlobalSetOptions.split_servers) && |
808 |
(Count.total > GlobalSetOptions.split_users)) |
809 |
{ |
810 |
splitmode = 0; |
811 |
|
812 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
813 |
"Network rejoined, deactivating splitmode"); |
814 |
eventDelete(check_splitmode, NULL); |
815 |
} |
816 |
} |
817 |
} |
818 |
|
819 |
/*! \brief Allocates a new topic |
820 |
* \param chptr Channel to allocate a new topic for |
821 |
*/ |
822 |
static void |
823 |
allocate_topic(struct Channel *chptr) |
824 |
{ |
825 |
void *ptr = NULL; |
826 |
|
827 |
ptr = BlockHeapAlloc(topic_heap); |
828 |
|
829 |
/* |
830 |
* Basically we allocate one large block for the topic and |
831 |
* the topic info. We then split it up into two and shove it |
832 |
* in the chptr |
833 |
*/ |
834 |
chptr->topic = ptr; |
835 |
chptr->topic_info = ((char *)ptr) + TOPICLEN + 1; |
836 |
*chptr->topic = '\0'; |
837 |
*chptr->topic_info = '\0'; |
838 |
} |
839 |
|
840 |
void |
841 |
free_topic(struct Channel *chptr) |
842 |
{ |
843 |
void *ptr = NULL; |
844 |
|
845 |
if (chptr->topic == NULL) |
846 |
return; |
847 |
|
848 |
/* |
849 |
* If you change allocate_topic you MUST change this as well |
850 |
*/ |
851 |
ptr = chptr->topic; |
852 |
BlockHeapFree(topic_heap, ptr); |
853 |
chptr->topic = NULL; |
854 |
chptr->topic_info = NULL; |
855 |
} |
856 |
|
857 |
/*! \brief Sets the channel topic for chptr |
858 |
* \param chptr Pointer to struct Channel |
859 |
* \param topic The topic string |
860 |
* \param topic_info n!u\@h formatted string of the topic setter |
861 |
* \param topicts timestamp on the topic |
862 |
*/ |
863 |
void |
864 |
set_channel_topic(struct Channel *chptr, const char *topic, |
865 |
const char *topic_info, time_t topicts) |
866 |
{ |
867 |
if (!EmptyString(topic)) |
868 |
{ |
869 |
if (chptr->topic == NULL) |
870 |
allocate_topic(chptr); |
871 |
|
872 |
strlcpy(chptr->topic, topic, TOPICLEN+1); |
873 |
strlcpy(chptr->topic_info, topic_info, USERHOST_REPLYLEN); |
874 |
chptr->topic_time = topicts; |
875 |
} |
876 |
else |
877 |
{ |
878 |
if (chptr->topic != NULL) |
879 |
free_topic(chptr); |
880 |
} |
881 |
} |
882 |
|
883 |
/* |
884 |
* Safe list code. |
885 |
* |
886 |
* The idea is really quite simple. As the link lists pointed to in |
887 |
* each "bucket" of the channel hash table are traversed atomically |
888 |
* there is no locking needed. Overall, yes, inconsistent reported |
889 |
* state can still happen, but normally this isn't a big deal. |
890 |
* |
891 |
* - Dianora |
892 |
*/ |
893 |
|
894 |
/* exceeding_sendq() |
895 |
* |
896 |
* inputs - pointer to client to check |
897 |
* output - 1 if client is in danger of blowing its sendq |
898 |
* 0 if it is not. |
899 |
* side effects - |
900 |
* |
901 |
* Sendq limit is fairly conservative at 1/2 (In original anyway) |
902 |
*/ |
903 |
static int |
904 |
exceeding_sendq(const struct Client *to) |
905 |
{ |
906 |
return dbuf_length(&to->localClient->buf_sendq) > |
907 |
(to->localClient->class->sendq_size / 2); |
908 |
} |
909 |
|
910 |
/* free_list_task() |
911 |
* |
912 |
* inputs - pointer to ListTask |
913 |
* - pointer to client being listed on |
914 |
* output - none |
915 |
* side effects - |
916 |
* |
917 |
*/ |
918 |
void |
919 |
free_list_task(struct ListTask *lt, struct Client *source_p) |
920 |
{ |
921 |
dlink_node *dl, *dln; |
922 |
|
923 |
if ((dl = dlinkFindDelete(&listing_client_list, source_p)) != NULL) |
924 |
free_dlink_node(dl); |
925 |
|
926 |
DLINK_FOREACH_SAFE(dl, dln, lt->show_mask.head) |
927 |
{ |
928 |
MyFree(dl->data); |
929 |
free_dlink_node(dl); |
930 |
} |
931 |
|
932 |
DLINK_FOREACH_SAFE(dl, dln, lt->hide_mask.head) |
933 |
{ |
934 |
MyFree(dl->data); |
935 |
free_dlink_node(dl); |
936 |
} |
937 |
|
938 |
MyFree(lt); |
939 |
|
940 |
source_p->localClient->list_task = NULL; |
941 |
} |
942 |
|
943 |
/* list_allow_channel() |
944 |
* |
945 |
* inputs - channel name |
946 |
* - pointer to a list task |
947 |
* output - 1 if the channel is to be displayed |
948 |
* 0 otherwise |
949 |
* side effects - |
950 |
*/ |
951 |
static int |
952 |
list_allow_channel(const char *chname, const struct ListTask *lt) |
953 |
{ |
954 |
const dlink_node *dl = NULL; |
955 |
|
956 |
DLINK_FOREACH(dl, lt->show_mask.head) |
957 |
if (!match_chan(dl->data, chname)) |
958 |
return 0; |
959 |
|
960 |
DLINK_FOREACH(dl, lt->hide_mask.head) |
961 |
if (match_chan(dl->data, chname)) |
962 |
return 0; |
963 |
|
964 |
return 1; |
965 |
} |
966 |
|
967 |
/* list_one_channel() |
968 |
* |
969 |
* inputs - client pointer to return result to |
970 |
* - pointer to channel to list |
971 |
* - pointer to ListTask structure |
972 |
* output - none |
973 |
* side effects - |
974 |
*/ |
975 |
static void |
976 |
list_one_channel(struct Client *source_p, struct Channel *chptr, |
977 |
struct ListTask *list_task) |
978 |
{ |
979 |
if (SecretChannel(chptr) && !IsMember(source_p, chptr)) |
980 |
return; |
981 |
|
982 |
if ((unsigned int)dlink_list_length(&chptr->members) < list_task->users_min || |
983 |
(unsigned int)dlink_list_length(&chptr->members) > list_task->users_max || |
984 |
(chptr->channelts != 0 && |
985 |
((unsigned int)chptr->channelts < list_task->created_min || |
986 |
(unsigned int)chptr->channelts > list_task->created_max)) || |
987 |
(unsigned int)chptr->topic_time < list_task->topicts_min || |
988 |
(chptr->topic_time ? (unsigned int)chptr->topic_time : UINT_MAX) > |
989 |
list_task->topicts_max) |
990 |
return; |
991 |
|
992 |
if (!list_allow_channel(chptr->chname, list_task)) |
993 |
return; |
994 |
sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name, |
995 |
chptr->chname, dlink_list_length(&chptr->members), |
996 |
chptr->topic == NULL ? "" : chptr->topic); |
997 |
} |
998 |
|
999 |
/* safe_list_channels() |
1000 |
* |
1001 |
* inputs - pointer to client requesting list |
1002 |
* output - none |
1003 |
* side effects - safely list all channels to source_p |
1004 |
* |
1005 |
* Walk the channel buckets, ensure all pointers in a bucket are |
1006 |
* traversed before blocking on a sendq. This means, no locking is needed. |
1007 |
* |
1008 |
* - Dianora |
1009 |
*/ |
1010 |
void |
1011 |
safe_list_channels(struct Client *source_p, struct ListTask *list_task, |
1012 |
int only_unmasked_channels) |
1013 |
{ |
1014 |
struct Channel *chptr = NULL; |
1015 |
|
1016 |
if (!only_unmasked_channels) |
1017 |
{ |
1018 |
int i; |
1019 |
|
1020 |
for (i = list_task->hash_index; i < HASHSIZE; ++i) |
1021 |
{ |
1022 |
if (exceeding_sendq(source_p->from)) |
1023 |
{ |
1024 |
list_task->hash_index = i; |
1025 |
return; // still more to do |
1026 |
} |
1027 |
|
1028 |
for (chptr = hash_get_bucket(HASH_TYPE_CHANNEL, i); chptr; |
1029 |
chptr = chptr->hnextch) |
1030 |
list_one_channel(source_p, chptr, list_task); |
1031 |
} |
1032 |
} |
1033 |
else |
1034 |
{ |
1035 |
dlink_node *dl = NULL; |
1036 |
|
1037 |
DLINK_FOREACH(dl, list_task->show_mask.head) |
1038 |
if ((chptr = hash_find_channel(dl->data)) != NULL) |
1039 |
list_one_channel(source_p, chptr, list_task); |
1040 |
} |
1041 |
|
1042 |
free_list_task(list_task, source_p); |
1043 |
sendto_one(source_p, form_str(RPL_LISTEND), |
1044 |
me.name, source_p->name); |
1045 |
} |