1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2022 ircd-hybrid development team |
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
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_invite.h" |
31 |
#include "channel_mode.h" |
32 |
#include "client.h" |
33 |
#include "hash.h" |
34 |
#include "conf.h" |
35 |
#include "conf_resv.h" |
36 |
#include "hostmask.h" |
37 |
#include "irc_string.h" |
38 |
#include "ircd.h" |
39 |
#include "numeric.h" |
40 |
#include "server.h" |
41 |
#include "send.h" |
42 |
#include "event.h" |
43 |
#include "memory.h" |
44 |
#include "misc.h" |
45 |
#include "extban.h" |
46 |
|
47 |
|
48 |
/** Doubly linked list containing a list of all channels. */ |
49 |
static dlink_list channel_list; |
50 |
|
51 |
|
52 |
/*! \brief Returns the channel_list as constant |
53 |
* \return channel_list |
54 |
*/ |
55 |
const dlink_list * |
56 |
channel_get_list(void) |
57 |
{ |
58 |
return &channel_list; |
59 |
} |
60 |
|
61 |
/*! \brief Adds a user to a channel by adding another link to the |
62 |
* channels member chain. |
63 |
* \param channel Pointer to channel to add client to |
64 |
* \param client Pointer to client (who) to add |
65 |
* \param flags Flags for chanops etc |
66 |
* \param flood_ctrl Whether to count this join in flood calculations |
67 |
*/ |
68 |
void |
69 |
add_user_to_channel(struct Channel *channel, struct Client *client, |
70 |
unsigned int flags, bool flood_ctrl) |
71 |
{ |
72 |
assert(IsClient(client)); |
73 |
|
74 |
if (GlobalSetOptions.joinfloodtime) |
75 |
{ |
76 |
if (flood_ctrl == true) |
77 |
++channel->number_joined; |
78 |
|
79 |
channel->number_joined -= (event_base->time.sec_monotonic - channel->last_join_time) * |
80 |
(((float)GlobalSetOptions.joinfloodcount) / |
81 |
(float)GlobalSetOptions.joinfloodtime); |
82 |
|
83 |
if (channel->number_joined <= 0) |
84 |
{ |
85 |
channel->number_joined = 0; |
86 |
ClearJoinFloodNoticed(channel); |
87 |
} |
88 |
else if (channel->number_joined >= GlobalSetOptions.joinfloodcount) |
89 |
{ |
90 |
channel->number_joined = GlobalSetOptions.joinfloodcount; |
91 |
|
92 |
if (!IsSetJoinFloodNoticed(channel)) |
93 |
{ |
94 |
SetJoinFloodNoticed(channel); |
95 |
sendto_realops_flags(UMODE_FLOOD, L_ALL, SEND_NOTICE, |
96 |
"Possible Join Flooder %s on %s target: %s", |
97 |
client_get_name(client, HIDE_IP), |
98 |
client->servptr->name, channel->name); |
99 |
} |
100 |
} |
101 |
|
102 |
channel->last_join_time = event_base->time.sec_monotonic; |
103 |
} |
104 |
|
105 |
struct ChannelMember *member = xcalloc(sizeof(*member)); |
106 |
member->client = client; |
107 |
member->channel = channel; |
108 |
member->flags = flags; |
109 |
|
110 |
dlinkAdd(member, &member->channode, &channel->members); |
111 |
|
112 |
if (MyConnect(client)) |
113 |
dlinkAdd(member, &member->locchannode, &channel->members_local); |
114 |
|
115 |
dlinkAdd(member, &member->usernode, &client->channel); |
116 |
} |
117 |
|
118 |
/*! \brief Deletes an user from a channel by removing a link in the |
119 |
* channels member chain. |
120 |
* \param member Pointer to Membership struct |
121 |
*/ |
122 |
void |
123 |
remove_user_from_channel(struct ChannelMember *member) |
124 |
{ |
125 |
struct Client *const client = member->client; |
126 |
struct Channel *const channel = member->channel; |
127 |
|
128 |
dlinkDelete(&member->channode, &channel->members); |
129 |
|
130 |
if (MyConnect(client)) |
131 |
dlinkDelete(&member->locchannode, &channel->members_local); |
132 |
|
133 |
dlinkDelete(&member->usernode, &client->channel); |
134 |
|
135 |
xfree(member); |
136 |
|
137 |
if (channel->members.head == NULL) |
138 |
channel_free(channel); |
139 |
} |
140 |
|
141 |
/* remove_a_mode() |
142 |
* |
143 |
* inputs - |
144 |
* output - NONE |
145 |
* side effects - remove ONE mode from a channel |
146 |
*/ |
147 |
void |
148 |
channel_demote_members(struct Channel *channel, const struct Client *client) |
149 |
{ |
150 |
dlink_node *node; |
151 |
char modebuf[MAXMODEPARAMS + 1]; |
152 |
char parabuf[MAXMODEPARAMS * (NICKLEN + 1) + 1]; |
153 |
char *mbuf = modebuf; |
154 |
char *pbuf = parabuf; |
155 |
unsigned int pargs = 0; |
156 |
struct member_status |
157 |
{ |
158 |
unsigned char prefix; |
159 |
unsigned int flag; |
160 |
} table[] = |
161 |
{ |
162 |
{ 'o', CHFL_CHANOP }, |
163 |
{ 'h', CHFL_HALFOP }, |
164 |
{ 'v', CHFL_VOICE }, |
165 |
{ '\0', 0 } |
166 |
}; |
167 |
|
168 |
DLINK_FOREACH(node, channel->members.head) |
169 |
{ |
170 |
struct ChannelMember *member = node->data; |
171 |
|
172 |
for (const struct member_status *status = table; status->flag; ++status) |
173 |
{ |
174 |
if (member->flags & status->flag) |
175 |
{ |
176 |
member->flags &= ~status->flag; |
177 |
*mbuf++ = status->prefix; |
178 |
pbuf += snprintf(pbuf, sizeof(parabuf) - (pbuf - parabuf), "%s ", member->client->name); |
179 |
|
180 |
if (++pargs >= MAXMODEPARAMS) |
181 |
{ |
182 |
*mbuf = *(pbuf - 1) = '\0'; |
183 |
sendto_channel_local(NULL, channel, 0, 0, 0, ":%s MODE %s -%s %s", |
184 |
client->name, channel->name, modebuf, parabuf); |
185 |
|
186 |
mbuf = modebuf; |
187 |
pbuf = parabuf; |
188 |
pargs = 0; |
189 |
} |
190 |
} |
191 |
} |
192 |
} |
193 |
|
194 |
if (pargs) |
195 |
{ |
196 |
*mbuf = *(pbuf - 1) = '\0'; |
197 |
sendto_channel_local(NULL, channel, 0, 0, 0, ":%s MODE %s -%s %s", |
198 |
client->name, channel->name, modebuf, parabuf); |
199 |
} |
200 |
} |
201 |
|
202 |
/* channel_send_members() |
203 |
* |
204 |
* inputs - |
205 |
* output - NONE |
206 |
* side effects - |
207 |
*/ |
208 |
static void |
209 |
channel_send_members(struct Client *client, const struct Channel *channel, |
210 |
const char *modebuf, const char *parabuf) |
211 |
{ |
212 |
dlink_node *node; |
213 |
char buf[IRCD_BUFSIZE]; |
214 |
int tlen; /* length of text to append */ |
215 |
char *t, *start; /* temp char pointer */ |
216 |
|
217 |
start = t = buf + snprintf(buf, sizeof(buf), ":%s SJOIN %ju %s %s %s:", |
218 |
me.id, channel->creation_time, |
219 |
channel->name, modebuf, parabuf); |
220 |
|
221 |
DLINK_FOREACH(node, channel->members.head) |
222 |
{ |
223 |
const struct ChannelMember *member = node->data; |
224 |
|
225 |
tlen = strlen(member->client->id) + 1; /* +1 for space */ |
226 |
tlen += member_get_prefix_len(member, true); |
227 |
|
228 |
/* |
229 |
* Space will be converted into CR, but we also need space for LF.. |
230 |
* That's why we use '- 1' here -adx |
231 |
*/ |
232 |
if (t + tlen - buf > sizeof(buf) - 1) |
233 |
{ |
234 |
*(t - 1) = '\0'; /* Kill the space and terminate the string */ |
235 |
sendto_one(client, "%s", buf); |
236 |
t = start; |
237 |
} |
238 |
|
239 |
t += snprintf(t, sizeof(buf) - (t - buf), "%s%s ", member_get_prefix(member, true), member->client->id); |
240 |
} |
241 |
|
242 |
/* Should always be non-NULL unless we have a kind of persistent channels */ |
243 |
if (channel->members.head) |
244 |
--t; /* Take the space out */ |
245 |
*t = '\0'; |
246 |
sendto_one(client, "%s", buf); |
247 |
} |
248 |
|
249 |
/*! \brief Sends +b/+e/+I |
250 |
* \param client Client pointer to server |
251 |
* \param channel Pointer to channel |
252 |
* \param list Pointer to list of modes to send |
253 |
* \param flag Char flag flagging type of mode. Currently this can be 'b', e' or 'I' |
254 |
*/ |
255 |
static void |
256 |
channel_send_mask_list(struct Client *client, const struct Channel *channel, |
257 |
const dlink_list *list, const char flag) |
258 |
{ |
259 |
dlink_node *node; |
260 |
char modebuf[IRCD_BUFSIZE]; |
261 |
char parabuf[IRCD_BUFSIZE]; |
262 |
size_t tlen, mlen, cur_len; |
263 |
char *pbuf = parabuf; |
264 |
|
265 |
if (dlink_list_length(list) == 0) |
266 |
return; |
267 |
|
268 |
mlen = snprintf(modebuf, sizeof(modebuf), ":%s BMASK %ju %s %c :", me.id, |
269 |
channel->creation_time, channel->name, flag); |
270 |
cur_len = mlen; |
271 |
|
272 |
DLINK_FOREACH(node, list->head) |
273 |
{ |
274 |
const struct Ban *ban = node->data; |
275 |
|
276 |
tlen = ban->banstr_len + 1; /* +1 for space */ |
277 |
|
278 |
/* |
279 |
* Send buffer and start over if we cannot fit another ban |
280 |
*/ |
281 |
if (cur_len + (tlen - 1) > sizeof(parabuf) - 2) |
282 |
{ |
283 |
*(pbuf - 1) = '\0'; /* Get rid of trailing space on buffer */ |
284 |
sendto_one(client, "%s%s", modebuf, parabuf); |
285 |
|
286 |
cur_len = mlen; |
287 |
pbuf = parabuf; |
288 |
} |
289 |
|
290 |
pbuf += snprintf(pbuf, sizeof(parabuf) - (pbuf - parabuf), "%s ", ban->banstr); |
291 |
cur_len += tlen; |
292 |
} |
293 |
|
294 |
*(pbuf - 1) = '\0'; /* Get rid of trailing space on buffer */ |
295 |
sendto_one(client, "%s%s", modebuf, parabuf); |
296 |
} |
297 |
|
298 |
/*! \brief Send "client" a full list of the modes for channel channel |
299 |
* \param client Pointer to client client |
300 |
* \param channel Pointer to channel pointer |
301 |
*/ |
302 |
void |
303 |
channel_send_modes(struct Client *client, const struct Channel *channel) |
304 |
{ |
305 |
char modebuf[MODEBUFLEN] = ""; |
306 |
char parabuf[MODEBUFLEN] = ""; |
307 |
|
308 |
channel_modes(channel, client, NULL, modebuf, parabuf); |
309 |
channel_send_members(client, channel, modebuf, parabuf); |
310 |
|
311 |
channel_send_mask_list(client, channel, &channel->banlist, 'b'); |
312 |
channel_send_mask_list(client, channel, &channel->exceptlist, 'e'); |
313 |
channel_send_mask_list(client, channel, &channel->invexlist, 'I'); |
314 |
} |
315 |
|
316 |
/*! \brief Check channel name for invalid characters |
317 |
* \param name Pointer to channel name string |
318 |
* \param local Indicates whether it's a local or remote creation |
319 |
* \return false if invalid, true otherwise |
320 |
*/ |
321 |
bool |
322 |
channel_check_name(const char *name, bool local) |
323 |
{ |
324 |
const char *p = name; |
325 |
|
326 |
assert(!EmptyString(p)); |
327 |
|
328 |
if (!IsChanPrefix(*p)) |
329 |
return false; |
330 |
|
331 |
if (local == false || ConfigChannel.disable_fake_channels == 0) |
332 |
{ |
333 |
while (*++p) |
334 |
if (!IsChanChar(*p)) |
335 |
return false; |
336 |
} |
337 |
else |
338 |
{ |
339 |
while (*++p) |
340 |
if (!IsVisibleChanChar(*p)) |
341 |
return false; |
342 |
} |
343 |
|
344 |
return p - name <= CHANNELLEN; |
345 |
} |
346 |
|
347 |
void |
348 |
remove_ban(struct Ban *ban, dlink_list *list) |
349 |
{ |
350 |
dlinkDelete(&ban->node, list); |
351 |
xfree(ban); |
352 |
} |
353 |
|
354 |
/* channel_free_mask_list() |
355 |
* |
356 |
* inputs - pointer to dlink_list |
357 |
* output - NONE |
358 |
* side effects - |
359 |
*/ |
360 |
static void |
361 |
channel_free_mask_list(dlink_list *list) |
362 |
{ |
363 |
while (list->head) |
364 |
{ |
365 |
struct Ban *ban = list->head->data; |
366 |
remove_ban(ban, list); |
367 |
} |
368 |
} |
369 |
|
370 |
/*! \brief Get Channel block for name (and allocate a new channel |
371 |
* block, if it didn't exist before) |
372 |
* \param name Channel name |
373 |
* \return Channel block |
374 |
*/ |
375 |
struct Channel * |
376 |
channel_make(const char *name) |
377 |
{ |
378 |
assert(!EmptyString(name)); |
379 |
|
380 |
struct Channel *channel = xcalloc(sizeof(*channel)); |
381 |
channel->hnextch = channel; |
382 |
/* Doesn't hurt to set it here */ |
383 |
channel->creation_time = event_base->time.sec_real; |
384 |
channel->last_join_time = event_base->time.sec_monotonic; |
385 |
|
386 |
/* Cache channel name length to avoid repetitive strlen() calls. */ |
387 |
channel->name_len = strlcpy(channel->name, name, sizeof(channel->name)); |
388 |
if (channel->name_len >= sizeof(channel->name)) |
389 |
channel->name_len = sizeof(channel->name) - 1; |
390 |
|
391 |
dlinkAdd(channel, &channel->node, &channel_list); |
392 |
hash_add_channel(channel); |
393 |
|
394 |
return channel; |
395 |
} |
396 |
|
397 |
/*! \brief Walk through this channel, and destroy it. |
398 |
* \param channel Channel pointer |
399 |
*/ |
400 |
void |
401 |
channel_free(struct Channel *channel) |
402 |
{ |
403 |
invite_clear_list(&channel->invites); |
404 |
|
405 |
/* Free ban/exception/invex lists */ |
406 |
channel_free_mask_list(&channel->banlist); |
407 |
channel_free_mask_list(&channel->exceptlist); |
408 |
channel_free_mask_list(&channel->invexlist); |
409 |
|
410 |
dlinkDelete(&channel->node, &channel_list); |
411 |
hash_del_channel(channel); |
412 |
|
413 |
assert(channel->hnextch == channel); |
414 |
|
415 |
assert(channel->node.prev == NULL); |
416 |
assert(channel->node.next == NULL); |
417 |
|
418 |
assert(dlink_list_length(&channel->members_local) == 0); |
419 |
assert(channel->members_local.head == NULL); |
420 |
assert(channel->members_local.tail == NULL); |
421 |
|
422 |
assert(dlink_list_length(&channel->members) == 0); |
423 |
assert(channel->members.head == NULL); |
424 |
assert(channel->members.tail == NULL); |
425 |
|
426 |
assert(dlink_list_length(&channel->invites) == 0); |
427 |
assert(channel->invites.head == NULL); |
428 |
assert(channel->invites.tail == NULL); |
429 |
|
430 |
assert(dlink_list_length(&channel->banlist) == 0); |
431 |
assert(channel->banlist.head == NULL); |
432 |
assert(channel->banlist.tail == NULL); |
433 |
|
434 |
assert(dlink_list_length(&channel->exceptlist) == 0); |
435 |
assert(channel->exceptlist.head == NULL); |
436 |
assert(channel->exceptlist.tail == NULL); |
437 |
|
438 |
assert(dlink_list_length(&channel->invexlist) == 0); |
439 |
assert(channel->invexlist.head == NULL); |
440 |
assert(channel->invexlist.tail == NULL); |
441 |
|
442 |
xfree(channel); |
443 |
} |
444 |
|
445 |
/*! |
446 |
* \param channel Pointer to channel |
447 |
* \return String pointer "=" if public, "@" if secret else "*" |
448 |
*/ |
449 |
static const char * |
450 |
channel_pub_or_secret(const struct Channel *channel) |
451 |
{ |
452 |
if (SecretChannel(channel)) |
453 |
return "@"; |
454 |
if (PrivateChannel(channel)) |
455 |
return "*"; |
456 |
return "="; |
457 |
} |
458 |
|
459 |
/*! \brief lists all names on given channel |
460 |
* \param client Pointer to client struct requesting names |
461 |
* \param channel Pointer to channel block |
462 |
*/ |
463 |
void |
464 |
channel_send_namereply(struct Client *client, struct Channel *channel) |
465 |
{ |
466 |
dlink_node *node; |
467 |
char buf[IRCD_BUFSIZE + 1]; |
468 |
char *bufptr = buf; |
469 |
size_t masklen = 0; |
470 |
bool is_member = member_find_link(client, channel) != NULL; |
471 |
bool multi_prefix = HasCap(client, CAP_MULTI_PREFIX) != 0; |
472 |
bool uhnames = HasCap(client, CAP_UHNAMES) != 0; |
473 |
|
474 |
assert(IsClient(client)); |
475 |
|
476 |
if (PubChannel(channel) || is_member == true) |
477 |
{ |
478 |
/* :me.name 353 client->name @ channel->name :+nick1 @nick2 %nick3 ...\r\n */ |
479 |
/* 1 23456 789 01 2 3 */ |
480 |
size_t len = strlen(me.name) + strlen(client->name) + channel->name_len + 13; |
481 |
|
482 |
DLINK_FOREACH(node, channel->members.head) |
483 |
{ |
484 |
const struct ChannelMember *member = node->data; |
485 |
|
486 |
if (HasUMode(member->client, UMODE_INVISIBLE) && is_member == false) |
487 |
continue; |
488 |
|
489 |
if (uhnames == true) |
490 |
masklen = strlen(member->client->name) + strlen(member->client->username) + |
491 |
strlen(member->client->host) + 3; /* +3 for ! + @ + space */ |
492 |
else |
493 |
masklen = strlen(member->client->name) + 1; /* +1 for space */ |
494 |
|
495 |
masklen += member_get_prefix_len(member, multi_prefix); |
496 |
|
497 |
if ((bufptr - buf) + masklen + len > sizeof(buf)) |
498 |
{ |
499 |
*(bufptr - 1) = '\0'; |
500 |
sendto_one_numeric(client, &me, RPL_NAMREPLY, |
501 |
channel_pub_or_secret(channel), channel->name, buf); |
502 |
bufptr = buf; |
503 |
} |
504 |
|
505 |
if (uhnames == true) |
506 |
bufptr += snprintf(bufptr, sizeof(buf) - (bufptr - buf), "%s%s!%s@%s ", |
507 |
member_get_prefix(member, multi_prefix), |
508 |
member->client->name, member->client->username, |
509 |
member->client->host); |
510 |
else |
511 |
bufptr += snprintf(bufptr, sizeof(buf) - (bufptr - buf), "%s%s ", |
512 |
member_get_prefix(member, multi_prefix), |
513 |
member->client->name); |
514 |
} |
515 |
|
516 |
if (bufptr != buf) |
517 |
{ |
518 |
*(bufptr - 1) = '\0'; |
519 |
sendto_one_numeric(client, &me, RPL_NAMREPLY, |
520 |
channel_pub_or_secret(channel), channel->name, buf); |
521 |
} |
522 |
} |
523 |
|
524 |
sendto_one_numeric(client, &me, RPL_ENDOFNAMES, channel->name); |
525 |
} |
526 |
|
527 |
/* member_get_prefix() |
528 |
* |
529 |
* inputs - pointer to struct ChannelMember |
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 client_get_name) |
537 |
*/ |
538 |
const char * |
539 |
member_get_prefix(const struct ChannelMember *member, bool combine) |
540 |
{ |
541 |
static char buf[CMEMBER_STATUS_FLAGS_LEN + 1]; /* +1 for \0 */ |
542 |
char *bufptr = buf; |
543 |
|
544 |
if (member->flags & CHFL_CHANOP) |
545 |
{ |
546 |
if (combine == false) |
547 |
return "@"; |
548 |
*bufptr++ = '@'; |
549 |
} |
550 |
|
551 |
if (member->flags & CHFL_HALFOP) |
552 |
{ |
553 |
if (combine == false) |
554 |
return "%"; |
555 |
*bufptr++ = '%'; |
556 |
} |
557 |
|
558 |
if (member->flags & CHFL_VOICE) |
559 |
*bufptr++ = '+'; |
560 |
*bufptr = '\0'; |
561 |
|
562 |
return buf; |
563 |
} |
564 |
|
565 |
size_t |
566 |
member_get_prefix_len(const struct ChannelMember *member, bool combine) |
567 |
{ |
568 |
size_t len = 0; |
569 |
|
570 |
if (member->flags & CHFL_CHANOP) |
571 |
{ |
572 |
if (combine == false) |
573 |
return 1; |
574 |
++len; |
575 |
} |
576 |
|
577 |
if (member->flags & CHFL_HALFOP) |
578 |
{ |
579 |
if (combine == false) |
580 |
return 1; |
581 |
++len; |
582 |
} |
583 |
|
584 |
if (member->flags & CHFL_VOICE) |
585 |
{ |
586 |
if (combine == false) |
587 |
return 1; |
588 |
++len; |
589 |
} |
590 |
|
591 |
return len; |
592 |
} |
593 |
|
594 |
/*! |
595 |
* \param client Pointer to Client to check |
596 |
* \param list Pointer to ban list to search |
597 |
* \return true if ban found for given n!u\@h mask, false otherwise |
598 |
*/ |
599 |
static bool |
600 |
ban_matches(struct Client *client, struct Channel *channel, struct Ban *ban) |
601 |
{ |
602 |
/* Is a matching extban, call custom match handler */ |
603 |
if (ban->extban & extban_matching_mask()) |
604 |
{ |
605 |
struct Extban *extban = extban_find_flag(ban->extban & extban_matching_mask()); |
606 |
if (extban == NULL) |
607 |
return false; |
608 |
|
609 |
if (extban->matches == NULL || extban->matches(client, channel, ban) == EXTBAN_NO_MATCH) |
610 |
return false; |
611 |
|
612 |
return true; |
613 |
} |
614 |
|
615 |
if (match(ban->name, client->name) == 0 && match(ban->user, client->username) == 0) |
616 |
{ |
617 |
switch (ban->type) |
618 |
{ |
619 |
case HM_HOST: |
620 |
if (match(ban->host, client->realhost) == 0 || |
621 |
match(ban->host, client->sockhost) == 0 || match(ban->host, client->host) == 0) |
622 |
return true; |
623 |
break; |
624 |
case HM_IPV6: |
625 |
case HM_IPV4: |
626 |
if (address_compare(&client->ip, &ban->addr, false, false, ban->bits) == true) |
627 |
return true; |
628 |
break; |
629 |
default: |
630 |
assert(0); |
631 |
} |
632 |
} |
633 |
|
634 |
return false; |
635 |
} |
636 |
|
637 |
bool |
638 |
find_bmask(struct Client *client, struct Channel *channel, const dlink_list *list, struct Extban *extban) |
639 |
{ |
640 |
dlink_node *node; |
641 |
|
642 |
DLINK_FOREACH(node, list->head) |
643 |
{ |
644 |
struct Ban *ban = node->data; |
645 |
|
646 |
/* Looking for a specific type of extban? */ |
647 |
if (extban) |
648 |
{ |
649 |
if (!(ban->extban & extban->flag)) |
650 |
continue; |
651 |
} |
652 |
else |
653 |
{ |
654 |
/* |
655 |
* Acting extbans have their own time they act and are not general purpose bans, |
656 |
* so skip them unless we are hunting them. |
657 |
*/ |
658 |
if (ban->extban & extban_acting_mask()) |
659 |
continue; |
660 |
} |
661 |
|
662 |
bool matches = ban_matches(client, channel, ban); |
663 |
if (matches == false) |
664 |
continue; |
665 |
|
666 |
return true; |
667 |
} |
668 |
|
669 |
return false; |
670 |
} |
671 |
|
672 |
/*! |
673 |
* \param channel Pointer to channel block |
674 |
* \param client Pointer to client to check access fo |
675 |
* \return false if not banned, true otherwise |
676 |
*/ |
677 |
bool |
678 |
is_banned(struct Channel *channel, struct Client *client) |
679 |
{ |
680 |
if (find_bmask(client, channel, &channel->banlist, NULL) == true) |
681 |
return find_bmask(client, channel, &channel->exceptlist, NULL) == false; |
682 |
return false; |
683 |
} |
684 |
|
685 |
/*! Tests if a client can join a certain channel |
686 |
* \param client Pointer to client attempting to join |
687 |
* \param channel Pointer to channel |
688 |
* \param key Key sent by client attempting to join if present |
689 |
* \return ERR_BANNEDFROMCHAN, ERR_INVITEONLYCHAN, ERR_CHANNELISFULL |
690 |
* or 0 if allowed to join. |
691 |
*/ |
692 |
static int |
693 |
can_join(struct Client *client, struct Channel *channel, const char *key) |
694 |
{ |
695 |
if (HasCMode(channel, MODE_SECUREONLY) && !HasUMode(client, UMODE_SECURE)) |
696 |
return ERR_SECUREONLYCHAN; |
697 |
|
698 |
if (HasCMode(channel, MODE_REGONLY) && !HasUMode(client, UMODE_REGISTERED)) |
699 |
return ERR_NEEDREGGEDNICK; |
700 |
|
701 |
if (HasCMode(channel, MODE_OPERONLY) && !HasUMode(client, UMODE_OPER)) |
702 |
return ERR_OPERONLYCHAN; |
703 |
|
704 |
if (HasCMode(channel, MODE_INVITEONLY)) |
705 |
if (invite_find(channel, client) == NULL) |
706 |
if (find_bmask(client, channel, &channel->invexlist, NULL) == false) |
707 |
return ERR_INVITEONLYCHAN; |
708 |
|
709 |
if (channel->mode.key[0] && (key == NULL || strcmp(channel->mode.key, key))) |
710 |
return ERR_BADCHANNELKEY; |
711 |
|
712 |
if (channel->mode.limit && dlink_list_length(&channel->members) >= |
713 |
channel->mode.limit) |
714 |
return ERR_CHANNELISFULL; |
715 |
|
716 |
if (is_banned(channel, client) == true) |
717 |
return ERR_BANNEDFROMCHAN; |
718 |
|
719 |
return extban_join_can_join(channel, client, NULL); |
720 |
} |
721 |
|
722 |
bool |
723 |
member_has_flags(const struct ChannelMember *member, const unsigned int flags) |
724 |
{ |
725 |
if (member && (member->flags & flags)) |
726 |
return true; |
727 |
return false; |
728 |
} |
729 |
|
730 |
struct ChannelMember * |
731 |
member_find_link(const struct Client *client, const struct Channel *channel) |
732 |
{ |
733 |
dlink_node *node; |
734 |
|
735 |
if (!IsClient(client)) |
736 |
return NULL; |
737 |
|
738 |
/* Take the shortest of the two lists */ |
739 |
if (dlink_list_length(&channel->members) < dlink_list_length(&client->channel)) |
740 |
{ |
741 |
DLINK_FOREACH(node, channel->members.head) |
742 |
if (((struct ChannelMember *)node->data)->client == client) |
743 |
return node->data; |
744 |
} |
745 |
else |
746 |
{ |
747 |
DLINK_FOREACH(node, client->channel.head) |
748 |
if (((struct ChannelMember *)node->data)->channel == channel) |
749 |
return node->data; |
750 |
} |
751 |
|
752 |
return NULL; |
753 |
} |
754 |
|
755 |
/*! Checks if a message contains control codes |
756 |
* \param message The actual message string the client wants to send |
757 |
* \return true if the message does contain any control codes, false otherwise |
758 |
*/ |
759 |
static bool |
760 |
msg_has_ctrls(const char *message) |
761 |
{ |
762 |
const unsigned char *p = (const unsigned char *)message; |
763 |
|
764 |
for (; *p; ++p) |
765 |
{ |
766 |
if (*p > 31 || *p == 1) |
767 |
continue; /* No control code or CTCP */ |
768 |
|
769 |
if (*p == 27) /* Escape */ |
770 |
{ |
771 |
/* ISO 2022 charset shift sequence */ |
772 |
if (*(p + 1) == '$' || |
773 |
*(p + 1) == '(') |
774 |
{ |
775 |
++p; |
776 |
continue; |
777 |
} |
778 |
} |
779 |
|
780 |
return true; /* Control code */ |
781 |
} |
782 |
|
783 |
return false; /* No control code found */ |
784 |
} |
785 |
|
786 |
/*! Tests if a client can send to a channel |
787 |
* \param channel Pointer to Channel struct |
788 |
* \param client Pointer to Client struct |
789 |
* \param member Pointer to Membership struct (can be NULL) |
790 |
* \param message The actual message string the client wants to send |
791 |
* \return CAN_SEND_OPV if op or voiced on channel\n |
792 |
* CAN_SEND_NONOP if can send to channel but is not an op\n |
793 |
* ERR_CANNOTSENDTOCHAN or ERR_NEEDREGGEDNICK if they cannot send to channel\n |
794 |
*/ |
795 |
int |
796 |
can_send(struct Channel *channel, struct Client *client, |
797 |
struct ChannelMember *member, const char *message, bool notice) |
798 |
{ |
799 |
const struct ResvItem *resv; |
800 |
|
801 |
if (IsServer(client) || HasFlag(client, FLAGS_SERVICE)) |
802 |
return CAN_SEND_OPV; |
803 |
|
804 |
if (MyConnect(client) && !HasFlag(client, FLAGS_EXEMPTRESV)) |
805 |
if (!(HasUMode(client, UMODE_OPER) && HasOFlag(client, OPER_FLAG_JOIN_RESV))) |
806 |
if ((resv = resv_find(channel->name, match)) && resv_exempt_find(client, resv) == false) |
807 |
return ERR_CANNOTSENDTOCHAN; |
808 |
|
809 |
if (HasCMode(channel, MODE_NOCTRL) && msg_has_ctrls(message) == true) |
810 |
return ERR_NOCTRLSONCHAN; |
811 |
|
812 |
if (HasCMode(channel, MODE_NOCTCP)) |
813 |
if (*message == '\001' && strncmp(message + 1, "ACTION ", 7)) |
814 |
return ERR_NOCTCP; |
815 |
|
816 |
if (member || (member = member_find_link(client, channel))) |
817 |
if (member->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE)) |
818 |
return CAN_SEND_OPV; |
819 |
|
820 |
if (member == NULL && HasCMode(channel, MODE_NOPRIVMSGS)) |
821 |
return ERR_CANNOTSENDTOCHAN; |
822 |
|
823 |
if (HasCMode(channel, MODE_MODERATED)) |
824 |
return ERR_CANNOTSENDTOCHAN; |
825 |
|
826 |
if (HasCMode(channel, MODE_MODREG) && !HasUMode(client, UMODE_REGISTERED)) |
827 |
return ERR_NEEDREGGEDNICK; |
828 |
|
829 |
if (HasCMode(channel, MODE_NONOTICE) && notice == true) |
830 |
return ERR_CANNOTSENDTOCHAN; |
831 |
|
832 |
/* Cache can send if banned */ |
833 |
if (MyConnect(client)) |
834 |
{ |
835 |
if (member) |
836 |
{ |
837 |
if (member->flags & CHFL_BAN_SILENCED) |
838 |
return ERR_CANNOTSENDTOCHAN; |
839 |
|
840 |
if (!(member->flags & CHFL_BAN_CHECKED)) |
841 |
{ |
842 |
if (is_banned(channel, client) == true) |
843 |
{ |
844 |
member->flags |= (CHFL_BAN_CHECKED | CHFL_BAN_SILENCED); |
845 |
return ERR_CANNOTSENDTOCHAN; |
846 |
} |
847 |
|
848 |
member->flags |= CHFL_BAN_CHECKED; |
849 |
} |
850 |
} |
851 |
else if (is_banned(channel, client) == true) |
852 |
return ERR_CANNOTSENDTOCHAN; |
853 |
} |
854 |
|
855 |
return extban_mute_can_send(channel, client, member); |
856 |
} |
857 |
|
858 |
/*! \brief Updates the client's oper_warn_count_down, warns the |
859 |
* IRC operators if necessary, and updates |
860 |
* join_leave_countdown as needed. |
861 |
* \param client Pointer to struct Client to check |
862 |
* \param name Channel name or NULL if this is a part. |
863 |
*/ |
864 |
void |
865 |
check_spambot_warning(struct Client *client, const char *name) |
866 |
{ |
867 |
if (GlobalSetOptions.spam_num && |
868 |
(client->connection->join_leave_count >= GlobalSetOptions.spam_num)) |
869 |
{ |
870 |
if (client->connection->oper_warn_count_down) |
871 |
--client->connection->oper_warn_count_down; |
872 |
|
873 |
if (client->connection->oper_warn_count_down == 0) |
874 |
{ |
875 |
client->connection->oper_warn_count_down = OPER_SPAM_COUNTDOWN; |
876 |
|
877 |
/* It's already known as a possible spambot */ |
878 |
if (name) |
879 |
sendto_realops_flags(UMODE_FLOOD, L_ALL, SEND_NOTICE, |
880 |
"User %s (%s@%s) trying to join %s is a possible spambot", |
881 |
client->name, client->username, |
882 |
client->host, name); |
883 |
else |
884 |
sendto_realops_flags(UMODE_FLOOD, L_ALL, SEND_NOTICE, |
885 |
"User %s (%s@%s) is a possible spambot", |
886 |
client->name, client->username, |
887 |
client->host); |
888 |
} |
889 |
} |
890 |
else |
891 |
{ |
892 |
unsigned int t_delta = event_base->time.sec_monotonic - client->connection->last_leave_time; |
893 |
if (t_delta > JOIN_LEAVE_COUNT_EXPIRE_TIME) |
894 |
{ |
895 |
unsigned int decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME); |
896 |
if (decrement_count > client->connection->join_leave_count) |
897 |
client->connection->join_leave_count = 0; |
898 |
else |
899 |
client->connection->join_leave_count -= decrement_count; |
900 |
} |
901 |
else |
902 |
{ |
903 |
if ((event_base->time.sec_monotonic - client->connection->last_join_time) < GlobalSetOptions.spam_time) |
904 |
++client->connection->join_leave_count; /* It's a possible spambot */ |
905 |
} |
906 |
|
907 |
if (name) |
908 |
client->connection->last_join_time = event_base->time.sec_monotonic; |
909 |
else |
910 |
client->connection->last_leave_time = event_base->time.sec_monotonic; |
911 |
} |
912 |
} |
913 |
|
914 |
/*! \brief Sets the channel topic for a certain channel |
915 |
* \param channel Pointer to struct Channel |
916 |
* \param topic The topic string |
917 |
* \param topic_info n!u\@h formatted string of the topic setter |
918 |
* \param topicts Timestamp on the topic |
919 |
* \param local Whether the topic is set by a local client |
920 |
*/ |
921 |
void |
922 |
channel_set_topic(struct Channel *channel, const char *topic, |
923 |
const char *topic_info, uintmax_t topicts, bool local) |
924 |
{ |
925 |
if (local == true) |
926 |
strlcpy(channel->topic, topic, IRCD_MIN(sizeof(channel->topic), ConfigServerInfo.max_topic_length + 1)); |
927 |
else |
928 |
strlcpy(channel->topic, topic, sizeof(channel->topic)); |
929 |
|
930 |
strlcpy(channel->topic_info, topic_info, sizeof(channel->topic_info)); |
931 |
channel->topic_time = topicts; |
932 |
} |
933 |
|
934 |
void |
935 |
channel_do_join(struct Client *client, char *chan_list, char *key_list) |
936 |
{ |
937 |
char *p = NULL; |
938 |
const struct ResvItem *resv = NULL; |
939 |
const struct ClassItem *const class = class_get_ptr(&client->connection->confs); |
940 |
unsigned int flags = 0; |
941 |
|
942 |
assert(MyClient(client)); |
943 |
|
944 |
for (const char *name = strtok_r(chan_list, ",", &p); name; |
945 |
name = strtok_r(NULL, ",", &p)) |
946 |
{ |
947 |
const char *key = NULL; |
948 |
|
949 |
/* If we have any more keys, take the first for this channel. */ |
950 |
if (!EmptyString(key_list) && (key_list = strchr(key = key_list, ','))) |
951 |
*key_list++ = '\0'; |
952 |
|
953 |
/* Empty keys are the same as no keys. */ |
954 |
if (key && *key == '\0') |
955 |
key = NULL; |
956 |
|
957 |
if (channel_check_name(name, true) == false) |
958 |
{ |
959 |
sendto_one_numeric(client, &me, ERR_BADCHANNAME, name); |
960 |
continue; |
961 |
} |
962 |
|
963 |
if (!HasFlag(client, FLAGS_EXEMPTRESV) && |
964 |
!(HasUMode(client, UMODE_OPER) && HasOFlag(client, OPER_FLAG_JOIN_RESV)) && |
965 |
((resv = resv_find(name, match)) && resv_exempt_find(client, resv) == false)) |
966 |
{ |
967 |
sendto_one_numeric(client, &me, ERR_CHANBANREASON, name, resv->reason); |
968 |
sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE, |
969 |
"Forbidding reserved channel %s from user %s", |
970 |
name, client_get_name(client, HIDE_IP)); |
971 |
continue; |
972 |
} |
973 |
|
974 |
if (dlink_list_length(&client->channel) >= |
975 |
((class->max_channels) ? class->max_channels : ConfigChannel.max_channels)) |
976 |
{ |
977 |
sendto_one_numeric(client, &me, ERR_TOOMANYCHANNELS, name); |
978 |
break; |
979 |
} |
980 |
|
981 |
struct Channel *channel = hash_find_channel(name); |
982 |
if (channel) |
983 |
{ |
984 |
if (member_find_link(client, channel)) |
985 |
continue; |
986 |
|
987 |
/* can_join() checks for +i, +l, key, bans, etc. */ |
988 |
int ret = can_join(client, channel, key); |
989 |
if (ret) |
990 |
{ |
991 |
sendto_one_numeric(client, &me, ret, channel->name); |
992 |
continue; |
993 |
} |
994 |
|
995 |
/* |
996 |
* This should never be the case unless there is some sort of |
997 |
* persistent channels. |
998 |
*/ |
999 |
if (dlink_list_length(&channel->members) == 0) |
1000 |
flags = CHFL_CHANOP; |
1001 |
else |
1002 |
flags = 0; |
1003 |
} |
1004 |
else |
1005 |
{ |
1006 |
flags = CHFL_CHANOP; |
1007 |
channel = channel_make(name); |
1008 |
} |
1009 |
|
1010 |
if (!HasUMode(client, UMODE_OPER)) |
1011 |
check_spambot_warning(client, channel->name); |
1012 |
|
1013 |
add_user_to_channel(channel, client, flags, true); |
1014 |
|
1015 |
/* |
1016 |
* Set timestamp if appropriate, and propagate |
1017 |
*/ |
1018 |
if (flags == CHFL_CHANOP) |
1019 |
{ |
1020 |
channel->creation_time = event_base->time.sec_real; |
1021 |
AddCMode(channel, MODE_TOPICLIMIT); |
1022 |
AddCMode(channel, MODE_NOPRIVMSGS); |
1023 |
|
1024 |
sendto_server(NULL, 0, 0, ":%s SJOIN %ju %s +nt :@%s", |
1025 |
me.id, channel->creation_time, |
1026 |
channel->name, client->id); |
1027 |
|
1028 |
/* |
1029 |
* Notify all other users on the new channel |
1030 |
*/ |
1031 |
sendto_channel_local(NULL, channel, 0, CAP_EXTENDED_JOIN, 0, ":%s!%s@%s JOIN %s %s :%s", |
1032 |
client->name, client->username, |
1033 |
client->host, channel->name, client->account, client->info); |
1034 |
sendto_channel_local(NULL, channel, 0, 0, CAP_EXTENDED_JOIN, ":%s!%s@%s JOIN :%s", |
1035 |
client->name, client->username, |
1036 |
client->host, channel->name); |
1037 |
sendto_channel_local(NULL, channel, 0, 0, 0, ":%s MODE %s +nt", |
1038 |
me.name, channel->name); |
1039 |
} |
1040 |
else |
1041 |
{ |
1042 |
sendto_server(NULL, 0, 0, ":%s JOIN %ju %s +", |
1043 |
client->id, channel->creation_time, |
1044 |
channel->name); |
1045 |
|
1046 |
sendto_channel_local(NULL, channel, 0, CAP_EXTENDED_JOIN, 0, ":%s!%s@%s JOIN %s %s :%s", |
1047 |
client->name, client->username, |
1048 |
client->host, channel->name, client->account, client->info); |
1049 |
sendto_channel_local(NULL, channel, 0, 0, CAP_EXTENDED_JOIN, ":%s!%s@%s JOIN :%s", |
1050 |
client->name, client->username, |
1051 |
client->host, channel->name); |
1052 |
} |
1053 |
|
1054 |
if (client->away[0]) |
1055 |
sendto_channel_local(client, channel, 0, CAP_AWAY_NOTIFY, 0, |
1056 |
":%s!%s@%s AWAY :%s", |
1057 |
client->name, client->username, |
1058 |
client->host, client->away); |
1059 |
|
1060 |
struct Invite *invite = invite_find(channel, client); |
1061 |
if (invite) |
1062 |
invite_del(invite); |
1063 |
|
1064 |
if (channel->topic[0]) |
1065 |
{ |
1066 |
sendto_one_numeric(client, &me, RPL_TOPIC, channel->name, channel->topic); |
1067 |
sendto_one_numeric(client, &me, RPL_TOPICWHOTIME, channel->name, |
1068 |
channel->topic_info, channel->topic_time); |
1069 |
} |
1070 |
|
1071 |
channel_send_namereply(client, channel); |
1072 |
|
1073 |
client->connection->last_join_time = event_base->time.sec_monotonic; |
1074 |
} |
1075 |
} |
1076 |
|
1077 |
/*! \brief Removes a client from a specific channel |
1078 |
* \param client Pointer to client to remove |
1079 |
* \param name Name of channel to remove from |
1080 |
* \param reason Part reason to show |
1081 |
*/ |
1082 |
static void |
1083 |
channel_part_one_client(struct Client *client, const char *name, const char *reason) |
1084 |
{ |
1085 |
struct Channel *channel = hash_find_channel(name); |
1086 |
if (channel == NULL) |
1087 |
{ |
1088 |
sendto_one_numeric(client, &me, ERR_NOSUCHCHANNEL, name); |
1089 |
return; |
1090 |
} |
1091 |
|
1092 |
struct ChannelMember *member = member_find_link(client, channel); |
1093 |
if (member == NULL) |
1094 |
{ |
1095 |
sendto_one_numeric(client, &me, ERR_NOTONCHANNEL, channel->name); |
1096 |
return; |
1097 |
} |
1098 |
|
1099 |
if (MyConnect(client) && !HasUMode(client, UMODE_OPER)) |
1100 |
check_spambot_warning(client, NULL); |
1101 |
|
1102 |
/* |
1103 |
* Remove user from the old channel (if any). Only allow /part reasons in -m chans. |
1104 |
*/ |
1105 |
if (*reason && (!MyConnect(client) || |
1106 |
((client->connection->created_monotonic + |
1107 |
ConfigGeneral.anti_spam_exit_message_time) < event_base->time.sec_monotonic && |
1108 |
can_send(channel, client, member, reason, false) < 0))) |
1109 |
{ |
1110 |
sendto_server(client, 0, 0, ":%s PART %s :%s", |
1111 |
client->id, channel->name, reason); |
1112 |
sendto_channel_local(NULL, channel, 0, 0, 0, ":%s!%s@%s PART %s :%s", |
1113 |
client->name, client->username, |
1114 |
client->host, channel->name, reason); |
1115 |
} |
1116 |
else |
1117 |
{ |
1118 |
sendto_server(client, 0, 0, ":%s PART %s", |
1119 |
client->id, channel->name); |
1120 |
sendto_channel_local(NULL, channel, 0, 0, 0, ":%s!%s@%s PART %s", |
1121 |
client->name, client->username, |
1122 |
client->host, channel->name); |
1123 |
} |
1124 |
|
1125 |
remove_user_from_channel(member); |
1126 |
} |
1127 |
|
1128 |
void |
1129 |
channel_do_part(struct Client *client, char *channel, const char *reason) |
1130 |
{ |
1131 |
char *p = NULL; |
1132 |
char buf[KICKLEN + 1] = ""; /* Essential that buf[0] = '\0' */ |
1133 |
|
1134 |
assert(IsClient(client)); |
1135 |
|
1136 |
if (!EmptyString(reason)) |
1137 |
strlcpy(buf, reason, sizeof(buf)); |
1138 |
|
1139 |
for (const char *name = strtok_r(channel, ",", &p); name; |
1140 |
name = strtok_r(NULL, ",", &p)) |
1141 |
channel_part_one_client(client, name, buf); |
1142 |
} |