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