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 send.c |
23 |
* \brief Functions for sending messages. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "send.h" |
30 |
#include "channel.h" |
31 |
#include "client.h" |
32 |
#include "dbuf.h" |
33 |
#include "irc_string.h" |
34 |
#include "ircd.h" |
35 |
#include "s_bsd.h" |
36 |
#include "server_capab.h" |
37 |
#include "conf_class.h" |
38 |
#include "log.h" |
39 |
|
40 |
|
41 |
static uintmax_t current_serial; |
42 |
|
43 |
|
44 |
/* send_format() |
45 |
* |
46 |
* inputs |
47 |
* - buffer |
48 |
* - format pattern to use |
49 |
* - var args |
50 |
* output - number of bytes formatted output |
51 |
* side effects - modifies sendbuf |
52 |
*/ |
53 |
static void |
54 |
send_format(struct dbuf_block *buffer, const char *pattern, va_list args) |
55 |
{ |
56 |
/* |
57 |
* from rfc1459 |
58 |
* |
59 |
* IRC messages are always lines of characters terminated with a CR-LF |
60 |
* (Carriage Return - Line Feed) pair, and these messages shall not |
61 |
* exceed 512 characters in length, counting all characters |
62 |
* including the trailing CR-LF. |
63 |
* Thus, there are 510 characters maximum allowed |
64 |
* for the command and its parameters. There is no provision for |
65 |
* continuation message lines. See section 7 for more details about |
66 |
* current implementations. |
67 |
*/ |
68 |
dbuf_put_args(buffer, pattern, args); |
69 |
|
70 |
if (buffer->size > IRCD_BUFSIZE - 2) |
71 |
buffer->size = IRCD_BUFSIZE - 2; |
72 |
|
73 |
buffer->data[buffer->size++] = '\r'; |
74 |
buffer->data[buffer->size++] = '\n'; |
75 |
} |
76 |
|
77 |
/* |
78 |
** send_message |
79 |
** Internal utility which appends given buffer to the sockets |
80 |
** sendq. |
81 |
*/ |
82 |
static void |
83 |
send_message(struct Client *to, struct dbuf_block *buf) |
84 |
{ |
85 |
assert(!IsMe(to)); |
86 |
assert(to != &me); |
87 |
assert(MyConnect(to)); |
88 |
|
89 |
if (dbuf_length(&to->connection->buf_sendq) + buf->size > get_sendq(&to->connection->confs)) |
90 |
{ |
91 |
if (IsServer(to)) |
92 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
93 |
"Max SendQ limit exceeded for %s: %zu > %u", |
94 |
client_get_name(to, HIDE_IP), |
95 |
(dbuf_length(&to->connection->buf_sendq) + buf->size), |
96 |
get_sendq(&to->connection->confs)); |
97 |
|
98 |
if (IsClient(to)) |
99 |
AddFlag(to, FLAGS_SENDQEX); |
100 |
|
101 |
dead_link_on_write(to, 0); |
102 |
return; |
103 |
} |
104 |
|
105 |
dbuf_add(&to->connection->buf_sendq, buf); |
106 |
|
107 |
/* |
108 |
* Update statistics. The following is slightly incorrect because |
109 |
* it counts messages even if queued, but bytes only really sent. |
110 |
* Queued bytes get updated in send_queued_write(). |
111 |
*/ |
112 |
++to->connection->send.messages; |
113 |
++me.connection->send.messages; |
114 |
|
115 |
send_queued_write(to); |
116 |
} |
117 |
|
118 |
/* send_message_remote() |
119 |
* |
120 |
* inputs - pointer to client from message is being sent |
121 |
* - pointer to client to send to |
122 |
* - pointer to buffer |
123 |
* output - none |
124 |
* side effects - Despite the function name, this only sends to directly |
125 |
* connected clients. |
126 |
* |
127 |
*/ |
128 |
static void |
129 |
send_message_remote(struct Client *to, const struct Client *from, struct dbuf_block *buf) |
130 |
{ |
131 |
assert(MyConnect(to)); |
132 |
assert(IsServer(to)); |
133 |
assert(!IsMe(to)); |
134 |
assert(to->from == to); |
135 |
|
136 |
if (to == from->from) |
137 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, "Send message to %s dropped from %s (Fake Dir)", |
138 |
to->name, from->name); |
139 |
else |
140 |
send_message(to, buf); |
141 |
} |
142 |
|
143 |
/* |
144 |
** sendq_unblocked |
145 |
** Called when a socket is ready for writing. |
146 |
*/ |
147 |
void |
148 |
sendq_unblocked(fde_t *F, void *data) |
149 |
{ |
150 |
struct Client *const client_p = data; |
151 |
|
152 |
assert(client_p); |
153 |
assert(client_p->connection); |
154 |
assert(client_p->connection->fd); |
155 |
assert(client_p->connection->fd == F); |
156 |
|
157 |
DelFlag(client_p, FLAGS_BLOCKED); |
158 |
send_queued_write(client_p); |
159 |
} |
160 |
|
161 |
/* |
162 |
** send_queued_write |
163 |
** This is called when there is a chance that some output would |
164 |
** be possible. This attempts to empty the send queue as far as |
165 |
** possible, and then if any data is left, a write is rescheduled. |
166 |
*/ |
167 |
void |
168 |
send_queued_write(struct Client *to) |
169 |
{ |
170 |
ssize_t retlen; |
171 |
|
172 |
/* |
173 |
* Once socket is marked dead, we cannot start writing to it, |
174 |
* even if the error is removed... |
175 |
*/ |
176 |
if (IsDead(to) || HasFlag(to, FLAGS_BLOCKED)) |
177 |
return; /* no use calling send() now */ |
178 |
|
179 |
/* Next, lets try to write some data */ |
180 |
while (dbuf_length(&to->connection->buf_sendq)) |
181 |
{ |
182 |
bool want_read = false; |
183 |
const struct dbuf_block *first = to->connection->buf_sendq.blocks.head->data; |
184 |
|
185 |
if (tls_isusing(&to->connection->fd->tls)) |
186 |
{ |
187 |
retlen = tls_write(&to->connection->fd->tls, first->data + to->connection->buf_sendq.pos, |
188 |
first->size - to->connection->buf_sendq.pos, &want_read); |
189 |
|
190 |
if (want_read == true) |
191 |
return; /* Retry later, don't register for write events */ |
192 |
} |
193 |
else |
194 |
retlen = send(to->connection->fd->fd, first->data + to->connection->buf_sendq.pos, |
195 |
first->size - to->connection->buf_sendq.pos, 0); |
196 |
|
197 |
if (retlen <= 0) |
198 |
{ |
199 |
if (retlen < 0 && comm_ignore_errno(errno) == true) |
200 |
{ |
201 |
AddFlag(to, FLAGS_BLOCKED); |
202 |
/* We have a non-fatal error, reschedule a write */ |
203 |
comm_setselect(to->connection->fd, COMM_SELECT_WRITE, sendq_unblocked, to, 0); |
204 |
} |
205 |
else |
206 |
dead_link_on_write(to, errno); |
207 |
return; |
208 |
} |
209 |
|
210 |
dbuf_delete(&to->connection->buf_sendq, retlen); |
211 |
|
212 |
/* We have some data written .. update counters */ |
213 |
to->connection->send.bytes += retlen; |
214 |
me.connection->send.bytes += retlen; |
215 |
} |
216 |
} |
217 |
|
218 |
/* sendto_one() |
219 |
* |
220 |
* inputs - pointer to destination client |
221 |
* - var args message |
222 |
* output - NONE |
223 |
* side effects - send message to single client |
224 |
*/ |
225 |
void |
226 |
sendto_one(struct Client *to, const char *pattern, ...) |
227 |
{ |
228 |
va_list args; |
229 |
|
230 |
if (IsDead(to->from)) |
231 |
return; /* This socket has already been marked as dead */ |
232 |
|
233 |
va_start(args, pattern); |
234 |
|
235 |
struct dbuf_block *buffer = dbuf_alloc(); |
236 |
send_format(buffer, pattern, args); |
237 |
|
238 |
va_end(args); |
239 |
|
240 |
send_message(to->from, buffer); |
241 |
|
242 |
dbuf_ref_free(buffer); |
243 |
} |
244 |
|
245 |
void |
246 |
sendto_one_numeric(struct Client *to, const struct Client *from, enum irc_numerics numeric, ...) |
247 |
{ |
248 |
va_list args; |
249 |
|
250 |
if (IsDead(to->from)) |
251 |
return; /* This socket has already been marked as dead */ |
252 |
|
253 |
const char *dest = ID_or_name(to, to); |
254 |
if (EmptyString(dest)) |
255 |
dest = "*"; |
256 |
|
257 |
struct dbuf_block *buffer = dbuf_alloc(); |
258 |
dbuf_put_fmt(buffer, ":%s %03d %s ", ID_or_name(from, to), numeric & ~SND_EXPLICIT, dest); |
259 |
|
260 |
va_start(args, numeric); |
261 |
|
262 |
const char *numstr; |
263 |
if (numeric & SND_EXPLICIT) |
264 |
numstr = va_arg(args, const char *); |
265 |
else |
266 |
numstr = numeric_form(numeric); |
267 |
|
268 |
send_format(buffer, numstr, args); |
269 |
va_end(args); |
270 |
|
271 |
send_message(to->from, buffer); |
272 |
|
273 |
dbuf_ref_free(buffer); |
274 |
} |
275 |
|
276 |
void |
277 |
sendto_one_notice(struct Client *to, const struct Client *from, const char *pattern, ...) |
278 |
{ |
279 |
va_list args; |
280 |
|
281 |
if (IsDead(to->from)) |
282 |
return; /* This socket has already been marked as dead */ |
283 |
|
284 |
const char *dest = ID_or_name(to, to); |
285 |
if (EmptyString(dest)) |
286 |
dest = "*"; |
287 |
|
288 |
struct dbuf_block *buffer = dbuf_alloc(); |
289 |
dbuf_put_fmt(buffer, ":%s NOTICE %s ", ID_or_name(from, to), dest); |
290 |
|
291 |
va_start(args, pattern); |
292 |
send_format(buffer, pattern, args); |
293 |
va_end(args); |
294 |
|
295 |
send_message(to->from, buffer); |
296 |
|
297 |
dbuf_ref_free(buffer); |
298 |
} |
299 |
|
300 |
/* sendto_channel_butone() |
301 |
* |
302 |
* inputs - pointer to client(server) to NOT send message to |
303 |
* - pointer to client that is sending this message |
304 |
* - pointer to channel being sent to |
305 |
* - vargs message |
306 |
* output - NONE |
307 |
* side effects - message as given is sent to given channel members. |
308 |
* |
309 |
* WARNING - +D clients are ignored |
310 |
*/ |
311 |
void |
312 |
sendto_channel_butone(struct Client *one, const struct Client *from, |
313 |
struct Channel *channel, unsigned int type, |
314 |
const char *pattern, ...) |
315 |
{ |
316 |
va_list alocal, aremote; |
317 |
struct dbuf_block *local_buf, *remote_buf; |
318 |
dlink_node *node; |
319 |
|
320 |
local_buf = dbuf_alloc(), remote_buf = dbuf_alloc(); |
321 |
|
322 |
if (IsClient(from)) |
323 |
dbuf_put_fmt(local_buf, ":%s!%s@%s ", from->name, from->username, from->host); |
324 |
else |
325 |
dbuf_put_fmt(local_buf, ":%s ", from->name); |
326 |
|
327 |
dbuf_put_fmt(remote_buf, ":%s ", from->id); |
328 |
|
329 |
va_start(alocal, pattern); |
330 |
va_start(aremote, pattern); |
331 |
send_format(local_buf, pattern, alocal); |
332 |
send_format(remote_buf, pattern, aremote); |
333 |
|
334 |
va_end(aremote); |
335 |
va_end(alocal); |
336 |
|
337 |
++current_serial; |
338 |
|
339 |
DLINK_FOREACH(node, channel->members.head) |
340 |
{ |
341 |
struct ChannelMember *member = node->data; |
342 |
struct Client *target_p = member->client; |
343 |
|
344 |
assert(IsClient(target_p)); |
345 |
|
346 |
if (IsDead(target_p->from)) |
347 |
continue; |
348 |
|
349 |
if (one && (target_p->from == one->from)) |
350 |
continue; |
351 |
|
352 |
if (type && (member->flags & type) == 0) |
353 |
continue; |
354 |
|
355 |
if (HasUMode(target_p, UMODE_DEAF)) |
356 |
continue; |
357 |
|
358 |
if (MyConnect(target_p)) |
359 |
send_message(target_p, local_buf); |
360 |
else if (target_p->from->connection->serial != current_serial) |
361 |
send_message_remote(target_p->from, from, remote_buf); |
362 |
|
363 |
target_p->from->connection->serial = current_serial; |
364 |
} |
365 |
|
366 |
dbuf_ref_free(local_buf); |
367 |
dbuf_ref_free(remote_buf); |
368 |
} |
369 |
|
370 |
/* sendto_server() |
371 |
* |
372 |
* inputs - pointer to client to NOT send to |
373 |
* - pointer to channel |
374 |
* - caps or'd together which must ALL be present |
375 |
* - caps or'd together which must ALL NOT be present |
376 |
* - printf style format string |
377 |
* - args to format string |
378 |
* output - NONE |
379 |
* side effects - Send a message to all connected servers, except the |
380 |
* client 'one' (if non-NULL), as long as the servers |
381 |
* support ALL capabs in 'caps', and NO capabs in 'nocaps'. |
382 |
* |
383 |
* This function was written in an attempt to merge together the other |
384 |
* billion sendto_*serv*() functions, which sprung up with capabs, |
385 |
* lazylinks, uids, etc. |
386 |
* -davidt |
387 |
*/ |
388 |
void |
389 |
sendto_server(const struct Client *one, |
390 |
const unsigned int caps, |
391 |
const unsigned int nocaps, |
392 |
const char *format, ...) |
393 |
{ |
394 |
va_list args; |
395 |
dlink_node *node; |
396 |
|
397 |
va_start(args, format); |
398 |
|
399 |
struct dbuf_block *buffer = dbuf_alloc(); |
400 |
send_format(buffer, format, args); |
401 |
|
402 |
va_end(args); |
403 |
|
404 |
DLINK_FOREACH(node, local_server_list.head) |
405 |
{ |
406 |
struct Client *client_p = node->data; |
407 |
|
408 |
/* If dead already skip */ |
409 |
if (IsDead(client_p)) |
410 |
continue; |
411 |
|
412 |
/* check against 'one' */ |
413 |
if (one && (client_p == one->from)) |
414 |
continue; |
415 |
|
416 |
/* check we have required capabs */ |
417 |
if ((client_p->connection->caps & caps) != caps) |
418 |
continue; |
419 |
|
420 |
/* check we don't have any forbidden capabs */ |
421 |
if ((client_p->connection->caps & nocaps)) |
422 |
continue; |
423 |
|
424 |
send_message(client_p, buffer); |
425 |
} |
426 |
|
427 |
dbuf_ref_free(buffer); |
428 |
} |
429 |
|
430 |
/* sendto_common_channels_local() |
431 |
* |
432 |
* inputs - pointer to client |
433 |
* - pattern to send |
434 |
* output - NONE |
435 |
* side effects - Sends a message to all people on local server who are |
436 |
* in same channel with user. |
437 |
* used by m_nick.c and exit_one_client. |
438 |
*/ |
439 |
void |
440 |
sendto_common_channels_local(struct Client *user, bool touser, unsigned int poscap, |
441 |
unsigned int negcap, const char *pattern, ...) |
442 |
{ |
443 |
va_list args; |
444 |
dlink_node *uptr; |
445 |
dlink_node *cptr; |
446 |
struct Channel *channel; |
447 |
struct ChannelMember *member; |
448 |
struct Client *target_p; |
449 |
struct dbuf_block *buffer = dbuf_alloc(); |
450 |
|
451 |
va_start(args, pattern); |
452 |
send_format(buffer, pattern, args); |
453 |
va_end(args); |
454 |
|
455 |
++current_serial; |
456 |
|
457 |
DLINK_FOREACH(cptr, user->channel.head) |
458 |
{ |
459 |
channel = ((struct ChannelMember *)cptr->data)->channel; |
460 |
|
461 |
DLINK_FOREACH(uptr, channel->members_local.head) |
462 |
{ |
463 |
member = uptr->data; |
464 |
target_p = member->client; |
465 |
|
466 |
if (IsDead(target_p)) |
467 |
continue; |
468 |
|
469 |
if (target_p == user) |
470 |
continue; |
471 |
|
472 |
if (target_p->connection->serial == current_serial) |
473 |
continue; |
474 |
|
475 |
if (poscap && HasCap(target_p, poscap) != poscap) |
476 |
continue; |
477 |
|
478 |
if (negcap && HasCap(target_p, negcap)) |
479 |
continue; |
480 |
|
481 |
target_p->connection->serial = current_serial; |
482 |
send_message(target_p, buffer); |
483 |
} |
484 |
} |
485 |
|
486 |
if (touser == true && MyConnect(user) && !IsDead(user)) |
487 |
if (HasCap(user, poscap) == poscap) |
488 |
send_message(user, buffer); |
489 |
|
490 |
dbuf_ref_free(buffer); |
491 |
} |
492 |
|
493 |
/*! \brief Send a message to members of a channel that are locally connected to this server. |
494 |
* \param one Client to skip; can be NULL |
495 |
* \param channel Destination channel |
496 |
* \param status Channel member status flags clients must have |
497 |
* \param poscap Positive client capabilities flags (CAP) |
498 |
* \param negcap Negative client capabilities flags (CAP) |
499 |
* \param pattern Format string for command arguments |
500 |
*/ |
501 |
void |
502 |
sendto_channel_local(const struct Client *one, struct Channel *channel, unsigned int status, |
503 |
unsigned int poscap, unsigned int negcap, const char *pattern, ...) |
504 |
{ |
505 |
va_list args; |
506 |
dlink_node *node; |
507 |
struct dbuf_block *buffer = dbuf_alloc(); |
508 |
|
509 |
va_start(args, pattern); |
510 |
send_format(buffer, pattern, args); |
511 |
va_end(args); |
512 |
|
513 |
DLINK_FOREACH(node, channel->members_local.head) |
514 |
{ |
515 |
struct ChannelMember *member = node->data; |
516 |
struct Client *target_p = member->client; |
517 |
|
518 |
if (IsDead(target_p)) |
519 |
continue; |
520 |
|
521 |
if (one && (target_p == one->from)) |
522 |
continue; |
523 |
|
524 |
if (status && (member->flags & status) == 0) |
525 |
continue; |
526 |
|
527 |
if (poscap && HasCap(target_p, poscap) != poscap) |
528 |
continue; |
529 |
|
530 |
if (negcap && HasCap(target_p, negcap)) |
531 |
continue; |
532 |
|
533 |
send_message(target_p, buffer); |
534 |
} |
535 |
|
536 |
dbuf_ref_free(buffer); |
537 |
} |
538 |
|
539 |
/* |
540 |
** match_it() and sendto_match_butone() ARE only used |
541 |
** to send a msg to all ppl on servers/hosts that match a specified mask |
542 |
** (used for enhanced PRIVMSGs) for opers |
543 |
** |
544 |
** addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de) |
545 |
** |
546 |
*/ |
547 |
|
548 |
/* match_it() |
549 |
* |
550 |
* inputs - client pointer to match on |
551 |
* - actual mask to match |
552 |
* - what to match on, HOST or SERVER |
553 |
* output - 1 or 0 if match or not |
554 |
* side effects - NONE |
555 |
*/ |
556 |
static bool |
557 |
match_it(const struct Client *one, const char *mask, unsigned int what) |
558 |
{ |
559 |
if (what == MATCH_HOST) |
560 |
return match(mask, one->host) == 0; |
561 |
|
562 |
return match(mask, one->servptr->name) == 0; |
563 |
} |
564 |
|
565 |
/* sendto_match_butone() |
566 |
* |
567 |
* Send to all clients which match the mask in a way defined on 'what'; |
568 |
* either by user hostname or user servername. |
569 |
* |
570 |
* ugh. ONLY used by m_message.c to send an "oper magic" message. ugh. |
571 |
*/ |
572 |
void |
573 |
sendto_match_butone(const struct Client *one, const struct Client *from, |
574 |
const char *mask, int what, const char *pattern, ...) |
575 |
{ |
576 |
va_list alocal, aremote; |
577 |
dlink_node *node; |
578 |
struct dbuf_block *local_buf, *remote_buf; |
579 |
|
580 |
local_buf = dbuf_alloc(), remote_buf = dbuf_alloc(); |
581 |
|
582 |
dbuf_put_fmt(local_buf, ":%s!%s@%s ", from->name, from->username, from->host); |
583 |
dbuf_put_fmt(remote_buf, ":%s ", from->id); |
584 |
|
585 |
va_start(alocal, pattern); |
586 |
va_start(aremote, pattern); |
587 |
send_format(local_buf, pattern, alocal); |
588 |
send_format(remote_buf, pattern, aremote); |
589 |
va_end(aremote); |
590 |
va_end(alocal); |
591 |
|
592 |
/* scan the local clients */ |
593 |
DLINK_FOREACH(node, local_client_list.head) |
594 |
{ |
595 |
struct Client *client_p = node->data; |
596 |
|
597 |
if (IsDead(client_p)) |
598 |
continue; |
599 |
|
600 |
if (one && (client_p == one->from)) |
601 |
continue; |
602 |
|
603 |
if (match_it(client_p, mask, what) == false) |
604 |
continue; |
605 |
|
606 |
send_message(client_p, local_buf); |
607 |
} |
608 |
|
609 |
/* Now scan servers */ |
610 |
DLINK_FOREACH(node, local_server_list.head) |
611 |
{ |
612 |
struct Client *client_p = node->data; |
613 |
|
614 |
/* |
615 |
* The old code looped through every client on the |
616 |
* network for each server to check if the |
617 |
* server (client_p) has at least 1 client matching |
618 |
* the mask, using something like: |
619 |
* |
620 |
* for (target_p = GlobalClientList; target_p; target_p = target_p->next) |
621 |
* if (IsRegisteredUser(target_p) && |
622 |
* match_it(target_p, mask, what) && |
623 |
* (target_p->from == client_p)) |
624 |
* vsendto_prefix_one(client_p, from, pattern, args); |
625 |
* |
626 |
* That way, we wouldn't send the message to |
627 |
* a server who didn't have a matching client. |
628 |
* However, on a network such as EFNet, that |
629 |
* code would have looped through about 50 |
630 |
* servers, and in each loop, loop through |
631 |
* about 50k clients as well, calling match() |
632 |
* in each nested loop. That is a very bad |
633 |
* thing cpu wise - just send the message |
634 |
* to every connected server and let that |
635 |
* server deal with it. |
636 |
* -wnder |
637 |
*/ |
638 |
if (IsDead(client_p)) |
639 |
continue; |
640 |
|
641 |
if (one && (client_p == one->from)) |
642 |
continue; |
643 |
|
644 |
send_message_remote(client_p, from, remote_buf); |
645 |
} |
646 |
|
647 |
dbuf_ref_free(local_buf); |
648 |
dbuf_ref_free(remote_buf); |
649 |
} |
650 |
|
651 |
/* sendto_match_servs() |
652 |
* |
653 |
* inputs - source client |
654 |
* - mask to send to |
655 |
* - capab needed |
656 |
* - data |
657 |
* outputs - none |
658 |
* side effects - data sent to servers matching with capab |
659 |
*/ |
660 |
void |
661 |
sendto_match_servs(const struct Client *source_p, const char *mask, unsigned int cap, |
662 |
const char *pattern, ...) |
663 |
{ |
664 |
va_list args; |
665 |
dlink_node *node; |
666 |
struct dbuf_block *buffer = dbuf_alloc(); |
667 |
|
668 |
dbuf_put_fmt(buffer, ":%s ", source_p->id); |
669 |
va_start(args, pattern); |
670 |
send_format(buffer, pattern, args); |
671 |
va_end(args); |
672 |
|
673 |
++current_serial; |
674 |
|
675 |
DLINK_FOREACH(node, global_server_list.head) |
676 |
{ |
677 |
struct Client *target_p = node->data; |
678 |
|
679 |
if (IsDead(target_p->from)) |
680 |
continue; |
681 |
|
682 |
/* Do not attempt to send to ourselves ... */ |
683 |
if (IsMe(target_p)) |
684 |
continue; |
685 |
|
686 |
/* ... or the source */ |
687 |
if (target_p->from == source_p->from) |
688 |
continue; |
689 |
|
690 |
if (target_p->from->connection->serial == current_serial) |
691 |
continue; |
692 |
|
693 |
if (match(mask, target_p->name)) |
694 |
continue; |
695 |
|
696 |
if (cap && IsCapable(target_p->from, cap) != cap) |
697 |
continue; |
698 |
|
699 |
target_p->from->connection->serial = current_serial; |
700 |
send_message_remote(target_p->from, source_p, buffer); |
701 |
} |
702 |
|
703 |
dbuf_ref_free(buffer); |
704 |
} |
705 |
|
706 |
/* sendto_anywhere() |
707 |
* |
708 |
* inputs - pointer to dest client |
709 |
* - pointer to from client |
710 |
* - varags |
711 |
* output - NONE |
712 |
* side effects - less efficient than sendto_remote and sendto_one |
713 |
* but useful when one does not know where target "lives" |
714 |
*/ |
715 |
void |
716 |
sendto_anywhere(struct Client *to, const struct Client *from, |
717 |
const char *command, |
718 |
const char *pattern, ...) |
719 |
{ |
720 |
va_list args; |
721 |
|
722 |
if (IsDead(to->from)) |
723 |
return; |
724 |
|
725 |
struct dbuf_block *buffer = dbuf_alloc(); |
726 |
if (MyClient(to) && IsClient(from)) |
727 |
dbuf_put_fmt(buffer, ":%s!%s@%s %s %s ", from->name, from->username, |
728 |
from->host, command, to->name); |
729 |
else |
730 |
dbuf_put_fmt(buffer, ":%s %s %s ", ID_or_name(from, to), |
731 |
command, ID_or_name(to, to)); |
732 |
|
733 |
va_start(args, pattern); |
734 |
send_format(buffer, pattern, args); |
735 |
va_end(args); |
736 |
|
737 |
if (MyConnect(to)) |
738 |
send_message(to, buffer); |
739 |
else |
740 |
send_message_remote(to->from, from, buffer); |
741 |
|
742 |
dbuf_ref_free(buffer); |
743 |
} |
744 |
|
745 |
/* sendto_realops_flags() |
746 |
* |
747 |
* inputs - flag types of messages to show to real opers |
748 |
* - flag indicating opers/admins |
749 |
* - var args input message |
750 |
* output - NONE |
751 |
* side effects - Send to *local* ops only but NOT +s nonopers. |
752 |
*/ |
753 |
void |
754 |
sendto_realops_flags(unsigned int flags, int level, int type, const char *pattern, ...) |
755 |
{ |
756 |
const char *ntype = "???"; |
757 |
dlink_node *node; |
758 |
va_list args; |
759 |
|
760 |
switch (type) |
761 |
{ |
762 |
case SEND_NOTICE: |
763 |
ntype = "Notice"; |
764 |
break; |
765 |
case SEND_GLOBAL: |
766 |
ntype = "Global"; |
767 |
break; |
768 |
case SEND_LOCOPS: |
769 |
ntype = "LocOps"; |
770 |
break; |
771 |
default: |
772 |
assert(0); |
773 |
} |
774 |
|
775 |
struct dbuf_block *buffer = dbuf_alloc(); |
776 |
dbuf_put_fmt(buffer, ":%s NOTICE * :*** %s -- ", me.name, ntype); |
777 |
|
778 |
va_start(args, pattern); |
779 |
send_format(buffer, pattern, args); |
780 |
va_end(args); |
781 |
|
782 |
DLINK_FOREACH(node, oper_list.head) |
783 |
{ |
784 |
struct Client *client_p = node->data; |
785 |
assert(HasUMode(client_p, UMODE_OPER)); |
786 |
|
787 |
if (IsDead(client_p)) |
788 |
continue; |
789 |
|
790 |
/* |
791 |
* If we're sending it to opers and they're an admin, skip. |
792 |
* If we're sending it to admins, and they're not, skip. |
793 |
*/ |
794 |
if (((level == L_ADMIN) && !HasUMode(client_p, UMODE_ADMIN)) || |
795 |
((level == L_OPER) && HasUMode(client_p, UMODE_ADMIN))) |
796 |
continue; |
797 |
|
798 |
if (!HasUMode(client_p, flags)) |
799 |
continue; |
800 |
|
801 |
send_message(client_p, buffer); |
802 |
} |
803 |
|
804 |
dbuf_ref_free(buffer); |
805 |
} |
806 |
|
807 |
/* ts_warn() |
808 |
* |
809 |
* inputs - var args message |
810 |
* output - NONE |
811 |
* side effects - Call sendto_realops_flags, with some flood checking |
812 |
* (at most 5 warnings every 5 seconds) |
813 |
*/ |
814 |
void |
815 |
sendto_realops_flags_ratelimited(uintmax_t *rate, const char *pattern, ...) |
816 |
{ |
817 |
va_list args; |
818 |
char buffer[IRCD_BUFSIZE] = ""; |
819 |
|
820 |
if ((event_base->time.sec_monotonic - *rate) < 20) |
821 |
return; |
822 |
|
823 |
*rate = event_base->time.sec_monotonic; |
824 |
|
825 |
va_start(args, pattern); |
826 |
vsnprintf(buffer, sizeof(buffer), pattern, args); |
827 |
va_end(args); |
828 |
|
829 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, "%s", buffer); |
830 |
ilog(LOG_TYPE_IRCD, "%s", buffer); |
831 |
} |
832 |
|
833 |
/* sendto_wallops_flags() |
834 |
* |
835 |
* inputs - flag types of messages to show to real opers |
836 |
* - client sending request |
837 |
* - var args input message |
838 |
* output - NONE |
839 |
* side effects - Send a wallops to local opers |
840 |
*/ |
841 |
void |
842 |
sendto_wallops_flags(unsigned int flags, const struct Client *source_p, |
843 |
const char *pattern, ...) |
844 |
{ |
845 |
dlink_node *node; |
846 |
va_list args; |
847 |
struct dbuf_block *buffer = dbuf_alloc(); |
848 |
|
849 |
if (IsClient(source_p)) |
850 |
dbuf_put_fmt(buffer, ":%s!%s@%s WALLOPS :", source_p->name, source_p->username, source_p->host); |
851 |
else |
852 |
dbuf_put_fmt(buffer, ":%s WALLOPS :", source_p->name); |
853 |
|
854 |
va_start(args, pattern); |
855 |
send_format(buffer, pattern, args); |
856 |
va_end(args); |
857 |
|
858 |
DLINK_FOREACH(node, oper_list.head) |
859 |
{ |
860 |
struct Client *client_p = node->data; |
861 |
assert(client_p->umodes & UMODE_OPER); |
862 |
|
863 |
if (IsDead(client_p)) |
864 |
continue; |
865 |
|
866 |
if (!HasUMode(client_p, flags)) |
867 |
continue; |
868 |
|
869 |
send_message(client_p, buffer); |
870 |
} |
871 |
|
872 |
dbuf_ref_free(buffer); |
873 |
} |