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