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