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 "list.h" |
29 |
#include "channel.h" |
30 |
#include "channel_mode.h" |
31 |
#include "client.h" |
32 |
#include "hash.h" |
33 |
#include "conf.h" |
34 |
#include "hostmask.h" |
35 |
#include "irc_string.h" |
36 |
#include "ircd.h" |
37 |
#include "numeric.h" |
38 |
#include "s_serv.h" /* captab */ |
39 |
#include "s_user.h" |
40 |
#include "send.h" |
41 |
#include "event.h" |
42 |
#include "memory.h" |
43 |
#include "mempool.h" |
44 |
#include "s_misc.h" |
45 |
#include "resv.h" |
46 |
|
47 |
struct config_channel_entry ConfigChannel; |
48 |
dlink_list global_channel_list = { NULL, NULL, 0 }; |
49 |
mp_pool_t *ban_pool; /*! \todo ban_pool shouldn't be a global var */ |
50 |
|
51 |
static mp_pool_t *member_pool = NULL; |
52 |
static mp_pool_t *channel_pool = NULL; |
53 |
|
54 |
static char buf[IRCD_BUFSIZE]; |
55 |
static char modebuf[MODEBUFLEN]; |
56 |
static char parabuf[MODEBUFLEN]; |
57 |
|
58 |
|
59 |
/*! \brief Initializes the channel blockheap, adds known channel CAPAB |
60 |
*/ |
61 |
void |
62 |
channel_init(void) |
63 |
{ |
64 |
add_capability("EX", CAP_EX, 1); |
65 |
add_capability("IE", CAP_IE, 1); |
66 |
add_capability("CHW", CAP_CHW, 1); |
67 |
|
68 |
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 |
} |
72 |
|
73 |
/*! \brief adds a user to a channel by adding another link to the |
74 |
* channels member chain. |
75 |
* \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 |
*/ |
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 |
chptr->number_joined++; |
90 |
|
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 |
sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE, |
108 |
"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 |
ms = mp_pool_get(member_pool); |
118 |
memset(ms, 0, sizeof(*ms)); |
119 |
|
120 |
ms->client_p = who; |
121 |
ms->chptr = chptr; |
122 |
ms->flags = flags; |
123 |
|
124 |
dlinkAdd(ms, &ms->channode, &chptr->members); |
125 |
dlinkAdd(ms, &ms->usernode, &who->channel); |
126 |
} |
127 |
|
128 |
/*! \brief deletes an user from a channel by removing a link in the |
129 |
* channels member chain. |
130 |
* \param member pointer to Membership struct |
131 |
*/ |
132 |
void |
133 |
remove_user_from_channel(struct Membership *member) |
134 |
{ |
135 |
struct Client *client_p = member->client_p; |
136 |
struct Channel *chptr = member->chptr; |
137 |
|
138 |
dlinkDelete(&member->channode, &chptr->members); |
139 |
dlinkDelete(&member->usernode, &client_p->channel); |
140 |
|
141 |
mp_pool_release(member); |
142 |
|
143 |
if (chptr->members.head == NULL) |
144 |
destroy_channel(chptr); |
145 |
} |
146 |
|
147 |
/* send_members() |
148 |
* |
149 |
* inputs - |
150 |
* output - NONE |
151 |
* side effects - |
152 |
*/ |
153 |
static void |
154 |
send_members(struct Client *client_p, struct Channel *chptr, |
155 |
char *lmodebuf, char *lparabuf) |
156 |
{ |
157 |
struct Membership *ms; |
158 |
dlink_node *ptr; |
159 |
int tlen; /* length of text to append */ |
160 |
char *t, *start; /* temp char pointer */ |
161 |
|
162 |
start = t = buf + sprintf(buf, ":%s SJOIN %lu %s %s %s:", |
163 |
ID_or_name(&me, client_p), |
164 |
(unsigned long)chptr->channelts, |
165 |
chptr->chname, lmodebuf, lparabuf); |
166 |
|
167 |
DLINK_FOREACH(ptr, chptr->members.head) |
168 |
{ |
169 |
ms = ptr->data; |
170 |
|
171 |
tlen = strlen(IsCapable(client_p, CAP_TS6) ? |
172 |
ID(ms->client_p) : ms->client_p->name) + 1; /* nick + space */ |
173 |
|
174 |
if (ms->flags & CHFL_CHANOP) |
175 |
tlen++; |
176 |
#ifdef HALFOPS |
177 |
else if (ms->flags & CHFL_HALFOP) |
178 |
tlen++; |
179 |
#endif |
180 |
if (ms->flags & CHFL_VOICE) |
181 |
tlen++; |
182 |
|
183 |
/* space will be converted into CR, but we also need space for LF.. |
184 |
* That's why we use '- 1' here |
185 |
* -adx */ |
186 |
if (t + tlen - buf > IRCD_BUFSIZE - 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, struct Channel *chptr, |
222 |
dlink_list *top, char flag) |
223 |
{ |
224 |
int ts5 = !IsCapable(client_p, CAP_TS6); |
225 |
dlink_node *lp; |
226 |
struct Ban *banptr; |
227 |
char pbuf[IRCD_BUFSIZE]; |
228 |
int tlen, mlen, cur_len, count = 0; |
229 |
char *mp = NULL, *pp = pbuf; |
230 |
|
231 |
if (top == NULL || top->length == 0) |
232 |
return; |
233 |
|
234 |
if (ts5) |
235 |
mlen = sprintf(buf, ":%s MODE %s +", me.name, chptr->chname); |
236 |
else |
237 |
mlen = sprintf(buf, ":%s BMASK %lu %s %c :", me.id, |
238 |
(unsigned long)chptr->channelts, chptr->chname, flag); |
239 |
|
240 |
/* MODE needs additional one byte for space between buf and pbuf */ |
241 |
cur_len = mlen + ts5; |
242 |
mp = buf + mlen; |
243 |
|
244 |
DLINK_FOREACH(lp, top->head) |
245 |
{ |
246 |
banptr = lp->data; |
247 |
|
248 |
/* must add another b/e/I letter if we use MODE */ |
249 |
tlen = banptr->len + 3 + ts5; |
250 |
|
251 |
/* |
252 |
* send buffer and start over if we cannot fit another ban, |
253 |
* or if the target is non-ts6 and we have too many modes in |
254 |
* in this line. |
255 |
*/ |
256 |
if (cur_len + (tlen - 1) > IRCD_BUFSIZE - 2 || |
257 |
(!IsCapable(client_p, CAP_TS6) && |
258 |
(count >= MAXMODEPARAMS || pp - pbuf >= MODEBUFLEN))) |
259 |
{ |
260 |
*(pp - 1) = '\0'; /* get rid of trailing space on buffer */ |
261 |
sendto_one(client_p, "%s%s%s", buf, ts5 ? " " : "", pbuf); |
262 |
|
263 |
cur_len = mlen + ts5; |
264 |
mp = buf + mlen; |
265 |
pp = pbuf; |
266 |
count = 0; |
267 |
} |
268 |
|
269 |
count++; |
270 |
if (ts5) |
271 |
{ |
272 |
*mp++ = flag; |
273 |
*mp = '\0'; |
274 |
} |
275 |
|
276 |
pp += sprintf(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 |
*modebuf = *parabuf = '\0'; |
293 |
channel_modes(chptr, client_p, modebuf, parabuf); |
294 |
send_members(client_p, chptr, modebuf, parabuf); |
295 |
|
296 |
send_mode_list(client_p, chptr, &chptr->banlist, 'b'); |
297 |
send_mode_list(client_p, chptr, &chptr->exceptlist, 'e'); |
298 |
send_mode_list(client_p, chptr, &chptr->invexlist, 'I'); |
299 |
} |
300 |
|
301 |
/*! \brief check channel name for invalid characters |
302 |
* \param name pointer to channel name string |
303 |
* \param local indicates whether it's a local or remote creation |
304 |
* \return 0 if invalid, 1 otherwise |
305 |
*/ |
306 |
int |
307 |
check_channel_name(const char *name, int local) |
308 |
{ |
309 |
const char *p = name; |
310 |
const int max_length = local ? LOCAL_CHANNELLEN : CHANNELLEN; |
311 |
assert(name != NULL); |
312 |
|
313 |
if (!IsChanPrefix(*p)) |
314 |
return 0; |
315 |
|
316 |
if (!local || !ConfigChannel.disable_fake_channels) |
317 |
{ |
318 |
while (*++p) |
319 |
if (!IsChanChar(*p)) |
320 |
return 0; |
321 |
} |
322 |
else |
323 |
{ |
324 |
while (*++p) |
325 |
if (!IsVisibleChanChar(*p)) |
326 |
return 0; |
327 |
} |
328 |
|
329 |
return p - name <= max_length; |
330 |
} |
331 |
|
332 |
void |
333 |
remove_ban(struct Ban *bptr, dlink_list *list) |
334 |
{ |
335 |
dlinkDelete(&bptr->node, list); |
336 |
|
337 |
MyFree(bptr->name); |
338 |
MyFree(bptr->username); |
339 |
MyFree(bptr->host); |
340 |
MyFree(bptr->who); |
341 |
|
342 |
mp_pool_release(bptr); |
343 |
} |
344 |
|
345 |
/* free_channel_list() |
346 |
* |
347 |
* inputs - pointer to dlink_list |
348 |
* output - NONE |
349 |
* side effects - |
350 |
*/ |
351 |
void |
352 |
free_channel_list(dlink_list *list) |
353 |
{ |
354 |
dlink_node *ptr = NULL, *next_ptr = NULL; |
355 |
|
356 |
DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
357 |
remove_ban(ptr->data, list); |
358 |
|
359 |
assert(list->tail == NULL && list->head == NULL); |
360 |
} |
361 |
|
362 |
/*! \brief Get Channel block for chname (and allocate a new channel |
363 |
* block, if it didn't exist before) |
364 |
* \param chname channel name |
365 |
* \return channel block |
366 |
*/ |
367 |
struct Channel * |
368 |
make_channel(const char *chname) |
369 |
{ |
370 |
struct Channel *chptr = NULL; |
371 |
|
372 |
assert(!EmptyString(chname)); |
373 |
|
374 |
chptr = mp_pool_get(channel_pool); |
375 |
|
376 |
memset(chptr, 0, sizeof(*chptr)); |
377 |
|
378 |
/* doesn't hurt to set it here */ |
379 |
chptr->channelts = CurrentTime; |
380 |
chptr->last_join_time = CurrentTime; |
381 |
|
382 |
strlcpy(chptr->chname, chname, sizeof(chptr->chname)); |
383 |
dlinkAdd(chptr, &chptr->node, &global_channel_list); |
384 |
|
385 |
hash_add_channel(chptr); |
386 |
|
387 |
return chptr; |
388 |
} |
389 |
|
390 |
/*! \brief walk through this channel, and destroy it. |
391 |
* \param chptr channel pointer |
392 |
*/ |
393 |
void |
394 |
destroy_channel(struct Channel *chptr) |
395 |
{ |
396 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
397 |
|
398 |
DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->invites.head) |
399 |
del_invite(chptr, ptr->data); |
400 |
|
401 |
/* free ban/exception/invex lists */ |
402 |
free_channel_list(&chptr->banlist); |
403 |
free_channel_list(&chptr->exceptlist); |
404 |
free_channel_list(&chptr->invexlist); |
405 |
|
406 |
dlinkDelete(&chptr->node, &global_channel_list); |
407 |
hash_del_channel(chptr); |
408 |
|
409 |
mp_pool_release(chptr); |
410 |
} |
411 |
|
412 |
/*! |
413 |
* \param chptr pointer to channel |
414 |
* \return string pointer "=" if public, "@" if secret else "*" |
415 |
*/ |
416 |
static const char * |
417 |
channel_pub_or_secret(const struct Channel *chptr) |
418 |
{ |
419 |
if (SecretChannel(chptr)) |
420 |
return "@"; |
421 |
if (PrivateChannel(chptr)) |
422 |
return "*"; |
423 |
return "="; |
424 |
} |
425 |
|
426 |
/*! \brief lists all names on given channel |
427 |
* \param source_p pointer to client struct requesting names |
428 |
* \param chptr pointer to channel block |
429 |
* \param show_eon show ENDOFNAMES numeric or not |
430 |
* (don't want it with /names with no params) |
431 |
*/ |
432 |
void |
433 |
channel_member_names(struct Client *source_p, struct Channel *chptr, |
434 |
int show_eon) |
435 |
{ |
436 |
struct Client *target_p = NULL; |
437 |
struct Membership *ms = NULL; |
438 |
dlink_node *ptr = NULL; |
439 |
char lbuf[IRCD_BUFSIZE + 1]; |
440 |
char *t = NULL, *start = NULL; |
441 |
int tlen = 0; |
442 |
int is_member = IsMember(source_p, chptr); |
443 |
int multi_prefix = HasCap(source_p, CAP_MULTI_PREFIX) != 0; |
444 |
|
445 |
if (PubChannel(chptr) || is_member) |
446 |
{ |
447 |
t = lbuf + sprintf(lbuf, RPL_NAMREPLY, |
448 |
me.name, source_p->name, |
449 |
channel_pub_or_secret(chptr), |
450 |
chptr->chname); |
451 |
start = t; |
452 |
|
453 |
DLINK_FOREACH(ptr, chptr->members.head) |
454 |
{ |
455 |
ms = ptr->data; |
456 |
target_p = ms->client_p; |
457 |
|
458 |
if (HasUMode(target_p, UMODE_INVISIBLE) && !is_member) |
459 |
continue; |
460 |
|
461 |
tlen = strlen(target_p->name) + 1; /* nick + space */ |
462 |
|
463 |
if (!multi_prefix) |
464 |
{ |
465 |
if (ms->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE)) |
466 |
++tlen; |
467 |
} |
468 |
else |
469 |
{ |
470 |
if (ms->flags & CHFL_CHANOP) |
471 |
++tlen; |
472 |
if (ms->flags & CHFL_HALFOP) |
473 |
++tlen; |
474 |
if (ms->flags & CHFL_VOICE) |
475 |
++tlen; |
476 |
} |
477 |
|
478 |
if (t + tlen - lbuf > IRCD_BUFSIZE - 2) |
479 |
{ |
480 |
*(t - 1) = '\0'; |
481 |
sendto_one(source_p, "%s", lbuf); |
482 |
t = start; |
483 |
} |
484 |
|
485 |
t += sprintf(t, "%s%s ", get_member_status(ms, multi_prefix), |
486 |
target_p->name); |
487 |
} |
488 |
|
489 |
if (tlen != 0) |
490 |
{ |
491 |
*(t - 1) = '\0'; |
492 |
sendto_one(source_p, "%s", lbuf); |
493 |
} |
494 |
} |
495 |
|
496 |
if (show_eon) |
497 |
sendto_one(source_p, RPL_ENDOFNAMES, |
498 |
me.name, source_p->name, chptr->chname); |
499 |
} |
500 |
|
501 |
/*! \brief adds client to invite list |
502 |
* \param chptr pointer to channel block |
503 |
* \param who pointer to client to add invite to |
504 |
*/ |
505 |
void |
506 |
add_invite(struct Channel *chptr, struct Client *who) |
507 |
{ |
508 |
del_invite(chptr, who); |
509 |
|
510 |
/* |
511 |
* delete last link in chain if the list is max length |
512 |
*/ |
513 |
if (dlink_list_length(&who->localClient->invited) >= |
514 |
ConfigChannel.max_chans_per_user) |
515 |
del_invite(who->localClient->invited.tail->data, who); |
516 |
|
517 |
/* add client to channel invite list */ |
518 |
dlinkAdd(who, make_dlink_node(), &chptr->invites); |
519 |
|
520 |
/* add channel to the end of the client invite list */ |
521 |
dlinkAdd(chptr, make_dlink_node(), &who->localClient->invited); |
522 |
} |
523 |
|
524 |
/*! \brief Delete Invite block from channel invite list |
525 |
* and client invite list |
526 |
* \param chptr pointer to Channel struct |
527 |
* \param who pointer to client to remove invites from |
528 |
*/ |
529 |
void |
530 |
del_invite(struct Channel *chptr, struct Client *who) |
531 |
{ |
532 |
dlink_node *ptr = NULL; |
533 |
|
534 |
if ((ptr = dlinkFindDelete(&who->localClient->invited, chptr))) |
535 |
free_dlink_node(ptr); |
536 |
|
537 |
if ((ptr = dlinkFindDelete(&chptr->invites, who))) |
538 |
free_dlink_node(ptr); |
539 |
} |
540 |
|
541 |
/* get_member_status() |
542 |
* |
543 |
* inputs - pointer to struct Membership |
544 |
* - YES if we can combine different flags |
545 |
* output - string either @, +, % or "" depending on whether |
546 |
* chanop, voiced or user |
547 |
* side effects - |
548 |
* |
549 |
* NOTE: Returned string is usually a static buffer |
550 |
* (like in get_client_name) |
551 |
*/ |
552 |
const char * |
553 |
get_member_status(const struct Membership *ms, int combine) |
554 |
{ |
555 |
static char buffer[4]; |
556 |
char *p = NULL; |
557 |
|
558 |
if (ms == NULL) |
559 |
return ""; |
560 |
p = buffer; |
561 |
|
562 |
if (ms->flags & CHFL_CHANOP) |
563 |
{ |
564 |
if (!combine) |
565 |
return "@"; |
566 |
*p++ = '@'; |
567 |
} |
568 |
|
569 |
#ifdef HALFOPS |
570 |
if (ms->flags & CHFL_HALFOP) |
571 |
{ |
572 |
if (!combine) |
573 |
return "%"; |
574 |
*p++ = '%'; |
575 |
} |
576 |
#endif |
577 |
|
578 |
if (ms->flags & CHFL_VOICE) |
579 |
*p++ = '+'; |
580 |
*p = '\0'; |
581 |
|
582 |
return buffer; |
583 |
} |
584 |
|
585 |
/*! |
586 |
* \param who pointer to Client to check |
587 |
* \param list pointer to ban list to search |
588 |
* \return 1 if ban found for given n!u\@h mask, 0 otherwise |
589 |
* |
590 |
*/ |
591 |
static int |
592 |
find_bmask(const struct Client *who, const dlink_list *const list) |
593 |
{ |
594 |
const dlink_node *ptr = NULL; |
595 |
|
596 |
DLINK_FOREACH(ptr, list->head) |
597 |
{ |
598 |
const struct Ban *bp = ptr->data; |
599 |
|
600 |
if (!match(bp->name, who->name) && !match(bp->username, who->username)) |
601 |
{ |
602 |
switch (bp->type) |
603 |
{ |
604 |
case HM_HOST: |
605 |
if (!match(bp->host, who->host) || !match(bp->host, who->sockhost)) |
606 |
return 1; |
607 |
break; |
608 |
case HM_IPV4: |
609 |
if (who->localClient->aftype == AF_INET) |
610 |
if (match_ipv4(&who->localClient->ip, &bp->addr, bp->bits)) |
611 |
return 1; |
612 |
break; |
613 |
#ifdef IPV6 |
614 |
case HM_IPV6: |
615 |
if (who->localClient->aftype == AF_INET6) |
616 |
if (match_ipv6(&who->localClient->ip, &bp->addr, bp->bits)) |
617 |
return 1; |
618 |
break; |
619 |
#endif |
620 |
default: |
621 |
assert(0); |
622 |
} |
623 |
} |
624 |
} |
625 |
|
626 |
return 0; |
627 |
} |
628 |
|
629 |
/*! |
630 |
* \param chptr pointer to channel block |
631 |
* \param who pointer to client to check access fo |
632 |
* \return 0 if not banned, 1 otherwise |
633 |
*/ |
634 |
int |
635 |
is_banned(const struct Channel *chptr, const struct Client *who) |
636 |
{ |
637 |
if (find_bmask(who, &chptr->banlist)) |
638 |
if (!find_bmask(who, &chptr->exceptlist)) |
639 |
return 1; |
640 |
|
641 |
return 0; |
642 |
} |
643 |
|
644 |
/*! |
645 |
* \param source_p pointer to client attempting to join |
646 |
* \param chptr pointer to channel |
647 |
* \param key key sent by client attempting to join if present |
648 |
* \return ERR_BANNEDFROMCHAN, ERR_INVITEONLYCHAN, ERR_CHANNELISFULL |
649 |
* or 0 if allowed to join. |
650 |
*/ |
651 |
const char * |
652 |
can_join(struct Client *source_p, struct Channel *chptr, const char *key) |
653 |
{ |
654 |
if (is_banned(chptr, source_p)) |
655 |
return ERR_BANNEDFROMCHAN; |
656 |
|
657 |
#ifdef HAVE_LIBCRYPTO |
658 |
if ((chptr->mode.mode & MODE_SSLONLY) && !source_p->localClient->fd.ssl) |
659 |
return ERR_SSLONLYCHAN; |
660 |
#endif |
661 |
|
662 |
if ((chptr->mode.mode & MODE_REGONLY) && !HasUMode(source_p, UMODE_REGISTERED)) |
663 |
return ERR_NEEDREGGEDNICK; |
664 |
|
665 |
if ((chptr->mode.mode & MODE_OPERONLY) && !HasUMode(source_p, UMODE_OPER)) |
666 |
return ERR_OPERONLYCHAN; |
667 |
|
668 |
if (chptr->mode.mode & MODE_INVITEONLY) |
669 |
if (!dlinkFind(&source_p->localClient->invited, chptr)) |
670 |
if (!find_bmask(source_p, &chptr->invexlist)) |
671 |
return ERR_INVITEONLYCHAN; |
672 |
|
673 |
if (chptr->mode.key[0] && (!key || strcmp(chptr->mode.key, key))) |
674 |
return ERR_BADCHANNELKEY; |
675 |
|
676 |
if (chptr->mode.limit && dlink_list_length(&chptr->members) >= |
677 |
chptr->mode.limit) |
678 |
return ERR_CHANNELISFULL; |
679 |
|
680 |
return NULL; |
681 |
} |
682 |
|
683 |
int |
684 |
has_member_flags(struct Membership *ms, unsigned int flags) |
685 |
{ |
686 |
if (ms != NULL) |
687 |
return ms->flags & flags; |
688 |
return 0; |
689 |
} |
690 |
|
691 |
struct Membership * |
692 |
find_channel_link(struct Client *client_p, struct Channel *chptr) |
693 |
{ |
694 |
dlink_node *ptr = NULL; |
695 |
|
696 |
if (!IsClient(client_p)) |
697 |
return NULL; |
698 |
|
699 |
DLINK_FOREACH(ptr, client_p->channel.head) |
700 |
if (((struct Membership *)ptr->data)->chptr == chptr) |
701 |
return ptr->data; |
702 |
|
703 |
return NULL; |
704 |
} |
705 |
|
706 |
/*! |
707 |
* \param chptr pointer to Channel struct |
708 |
* \param source_p pointer to Client struct |
709 |
* \param ms pointer to Membership struct (can be NULL) |
710 |
* \return CAN_SEND_OPV if op or voiced on channel\n |
711 |
* CAN_SEND_NONOP if can send to channel but is not an op\n |
712 |
* ERR_CANNOTSENDTOCHAN or ERR_NEEDREGGEDNICK if they cannot send to channel\n |
713 |
*/ |
714 |
int |
715 |
can_send(struct Channel *chptr, struct Client *source_p, struct Membership *ms) |
716 |
{ |
717 |
if (IsServer(source_p) || HasFlag(source_p, FLAGS_SERVICE)) |
718 |
return CAN_SEND_OPV; |
719 |
|
720 |
if (MyClient(source_p) && !IsExemptResv(source_p)) |
721 |
if (!(HasUMode(source_p, UMODE_OPER) && ConfigFileEntry.oper_pass_resv)) |
722 |
if (!match_find_resv(chptr->chname) == ConfigChannel.restrict_channels) |
723 |
return CAN_SEND_NO; |
724 |
|
725 |
if (ms != NULL || (ms = find_channel_link(source_p, chptr))) |
726 |
{ |
727 |
if (ms->flags & (CHFL_CHANOP|CHFL_HALFOP|CHFL_VOICE)) |
728 |
return CAN_SEND_OPV; |
729 |
|
730 |
/* cache can send if quiet_on_ban and banned */ |
731 |
if (ConfigChannel.quiet_on_ban && MyClient(source_p)) |
732 |
{ |
733 |
if (ms->flags & CHFL_BAN_SILENCED) |
734 |
return CAN_SEND_NO; |
735 |
|
736 |
if (!(ms->flags & CHFL_BAN_CHECKED)) |
737 |
{ |
738 |
if (is_banned(chptr, source_p)) |
739 |
{ |
740 |
ms->flags |= (CHFL_BAN_CHECKED|CHFL_BAN_SILENCED); |
741 |
return CAN_SEND_NO; |
742 |
} |
743 |
|
744 |
ms->flags |= CHFL_BAN_CHECKED; |
745 |
} |
746 |
} |
747 |
} |
748 |
else if (chptr->mode.mode & MODE_NOPRIVMSGS) |
749 |
return CAN_SEND_NO; |
750 |
|
751 |
if (chptr->mode.mode & MODE_MODERATED) |
752 |
return CAN_SEND_NO; |
753 |
|
754 |
return CAN_SEND_NONOP; |
755 |
} |
756 |
|
757 |
/*! \brief Updates the client's oper_warn_count_down, warns the |
758 |
* IRC operators if necessary, and updates |
759 |
* join_leave_countdown as needed. |
760 |
* \param source_p pointer to struct Client to check |
761 |
* \param name channel name or NULL if this is a part. |
762 |
*/ |
763 |
void |
764 |
check_spambot_warning(struct Client *source_p, const char *name) |
765 |
{ |
766 |
int t_delta = 0; |
767 |
int decrement_count = 0; |
768 |
|
769 |
if ((GlobalSetOptions.spam_num && |
770 |
(source_p->localClient->join_leave_count >= |
771 |
GlobalSetOptions.spam_num))) |
772 |
{ |
773 |
if (source_p->localClient->oper_warn_count_down > 0) |
774 |
source_p->localClient->oper_warn_count_down--; |
775 |
else |
776 |
source_p->localClient->oper_warn_count_down = 0; |
777 |
|
778 |
if (source_p->localClient->oper_warn_count_down == 0) |
779 |
{ |
780 |
/* Its already known as a possible spambot */ |
781 |
if (name != NULL) |
782 |
sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE, |
783 |
"User %s (%s@%s) trying to join %s is a possible spambot", |
784 |
source_p->name, source_p->username, |
785 |
source_p->host, name); |
786 |
else |
787 |
sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE, |
788 |
"User %s (%s@%s) is a possible spambot", |
789 |
source_p->name, source_p->username, |
790 |
source_p->host); |
791 |
source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN; |
792 |
} |
793 |
} |
794 |
else |
795 |
{ |
796 |
if ((t_delta = (CurrentTime - source_p->localClient->last_leave_time)) > |
797 |
JOIN_LEAVE_COUNT_EXPIRE_TIME) |
798 |
{ |
799 |
decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME); |
800 |
if (decrement_count > source_p->localClient->join_leave_count) |
801 |
source_p->localClient->join_leave_count = 0; |
802 |
else |
803 |
source_p->localClient->join_leave_count -= decrement_count; |
804 |
} |
805 |
else |
806 |
{ |
807 |
if ((CurrentTime - (source_p->localClient->last_join_time)) < |
808 |
GlobalSetOptions.spam_time) |
809 |
{ |
810 |
/* oh, its a possible spambot */ |
811 |
source_p->localClient->join_leave_count++; |
812 |
} |
813 |
} |
814 |
|
815 |
if (name != NULL) |
816 |
source_p->localClient->last_join_time = CurrentTime; |
817 |
else |
818 |
source_p->localClient->last_leave_time = CurrentTime; |
819 |
} |
820 |
} |
821 |
|
822 |
/*! \brief compares usercount and servercount against their split |
823 |
* values and adjusts splitmode accordingly |
824 |
* \param unused Unused address pointer |
825 |
*/ |
826 |
void |
827 |
check_splitmode(void *unused) |
828 |
{ |
829 |
if (splitchecking && (ConfigChannel.no_join_on_split || |
830 |
ConfigChannel.no_create_on_split)) |
831 |
{ |
832 |
const unsigned int server = dlink_list_length(&global_serv_list); |
833 |
|
834 |
if (!splitmode && ((server < split_servers) || (Count.total < split_users))) |
835 |
{ |
836 |
splitmode = 1; |
837 |
|
838 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
839 |
"Network split, activating splitmode"); |
840 |
eventAddIsh("check_splitmode", check_splitmode, NULL, 10); |
841 |
} |
842 |
else if (splitmode && (server > split_servers) && (Count.total > split_users)) |
843 |
{ |
844 |
splitmode = 0; |
845 |
|
846 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
847 |
"Network rejoined, deactivating splitmode"); |
848 |
eventDelete(check_splitmode, NULL); |
849 |
} |
850 |
} |
851 |
} |
852 |
|
853 |
/*! \brief Sets the channel topic for chptr |
854 |
* \param chptr Pointer to struct Channel |
855 |
* \param topic The topic string |
856 |
* \param topic_info n!u\@h formatted string of the topic setter |
857 |
* \param topicts timestamp on the topic |
858 |
*/ |
859 |
void |
860 |
set_channel_topic(struct Channel *chptr, const char *topic, |
861 |
const char *topic_info, time_t topicts, int local) |
862 |
{ |
863 |
if (local) |
864 |
strlcpy(chptr->topic, topic, IRCD_MIN(sizeof(chptr->topic), ServerInfo.max_topic_length + 1)); |
865 |
else |
866 |
strlcpy(chptr->topic, topic, sizeof(chptr->topic)); |
867 |
|
868 |
strlcpy(chptr->topic_info, topic_info, sizeof(chptr->topic_info)); |
869 |
chptr->topic_time = topicts; |
870 |
} |