ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
Revision: 3243
Committed: Sun Mar 30 16:53:43 2014 UTC (11 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 26579 byte(s)
Log Message:
- send.c: mostly style cleanups. Removed some useless assert() statements

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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 "numeric.h"
36 #include "fdlist.h"
37 #include "s_bsd.h"
38 #include "s_serv.h"
39 #include "conf.h"
40 #include "log.h"
41 #include "memory.h"
42 #include "packet.h"
43
44 #ifndef IOV_MAX
45 #define IOV_MAX 64
46 #endif
47
48 static unsigned int current_serial = 0;
49
50
51 /* send_format()
52 *
53 * inputs
54 * - buffer
55 * - format pattern to use
56 * - var args
57 * output - number of bytes formatted output
58 * side effects - modifies sendbuf
59 */
60 static void
61 send_format(struct dbuf_block *buffer, const char *pattern, va_list args)
62 {
63 /*
64 * from rfc1459
65 *
66 * IRC messages are always lines of characters terminated with a CR-LF
67 * (Carriage Return - Line Feed) pair, and these messages shall not
68 * exceed 512 characters in length, counting all characters
69 * including the trailing CR-LF.
70 * Thus, there are 510 characters maximum allowed
71 * for the command and its parameters. There is no provision for
72 * continuation message lines. See section 7 for more details about
73 * current implementations.
74 */
75 dbuf_put_args(buffer, pattern, args);
76
77 if (buffer->size > sizeof(buffer->data) - 2)
78 buffer->size = sizeof(buffer->data) - 2;
79
80 buffer->data[buffer->size++] = '\r';
81 buffer->data[buffer->size++] = '\n';
82 }
83
84 /*
85 ** send_message
86 ** Internal utility which appends given buffer to the sockets
87 ** sendq.
88 */
89 static void
90 send_message(struct Client *to, struct dbuf_block *buf)
91 {
92 assert(!IsMe(to));
93 assert(to != &me);
94
95 if (dbuf_length(&to->localClient->buf_sendq) + buf->size > get_sendq(&to->localClient->confs))
96 {
97 if (IsServer(to))
98 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
99 "Max SendQ limit exceeded for %s: %lu > %u",
100 get_client_name(to, HIDE_IP),
101 (unsigned long)(dbuf_length(&to->localClient->buf_sendq) + buf->size),
102 get_sendq(&to->localClient->confs));
103 if (IsClient(to))
104 SetSendQExceeded(to);
105
106 dead_link_on_write(to, 0);
107 return;
108 }
109
110 dbuf_add(&to->localClient->buf_sendq, buf);
111
112 /*
113 * Update statistics. The following is slightly incorrect because
114 * it counts messages even if queued, but bytes only really sent.
115 * Queued bytes get updated in send_queued_write().
116 */
117 ++to->localClient->send.messages;
118 ++me.localClient->send.messages;
119
120 send_queued_write(to);
121 }
122
123 /* send_message_remote()
124 *
125 * inputs - pointer to client from message is being sent
126 * - pointer to client to send to
127 * - pointer to buffer
128 * output - none
129 * side effects - Despite the function name, this only sends to directly
130 * connected clients.
131 *
132 */
133 static void
134 send_message_remote(struct Client *to, struct Client *from, struct dbuf_block *buf)
135 {
136 to = to->from;
137
138 if (!MyConnect(to))
139 {
140 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
141 "Server send message to %s [%s] dropped from %s(Not local server)",
142 to->name, to->from->name, from->name);
143 return;
144 }
145
146 /* Optimize by checking if (from && to) before everything */
147 /* we set to->from up there.. */
148
149 if (!MyClient(from) && IsClient(to) && (to == from->from))
150 {
151 if (IsServer(from))
152 {
153 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
154 "Send message to %s [%s] dropped from %s(Fake Dir)",
155 to->name, to->from->name, from->name);
156 return;
157 }
158
159 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
160 "Ghosted: %s[%s@%s] from %s[%s@%s] (%s)",
161 to->name, to->username, to->host,
162 from->name, from->username, from->host,
163 to->from->name);
164
165 sendto_server(NULL, NOCAPS, NOCAPS,
166 ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
167 me.id, to->id, me.name, to->name,
168 to->username, to->host, to->from->name);
169
170 AddFlag(to, FLAGS_KILLED);
171
172 if (IsClient(from))
173 sendto_one_numeric(from, &me, ERR_GHOSTEDCLIENT, to->name,
174 to->username, to->host, to->from);
175
176 exit_client(to, "Ghosted client");
177 return;
178 }
179
180 send_message(to, buf);
181 }
182
183 /*
184 ** sendq_unblocked
185 ** Called when a socket is ready for writing.
186 */
187 void
188 sendq_unblocked(fde_t *fd, struct Client *client_p)
189 {
190 assert(fd == &client_p->localClient->fd);
191 send_queued_write(client_p);
192 }
193
194 #ifdef HAVE_LIBCRYPTO
195 static int
196 send_SSL_writev(SSL *ssl, const struct iovec *vec, int iovcnt)
197 {
198 int total_written = 0;
199
200 for (int i = 0; i < iovcnt; ++i)
201 {
202 const struct iovec *iov = &vec[i];
203 int ret = 0;
204
205 /* Translate openssl error codes, sigh */
206 if ((ret = SSL_write(ssl, iov->iov_base, iov->iov_len)) <= 0)
207 {
208 switch (SSL_get_error(ssl, i))
209 {
210 case SSL_ERROR_WANT_READ:
211 return -1;
212 case SSL_ERROR_WANT_WRITE:
213 errno = EWOULDBLOCK;
214 case SSL_ERROR_SYSCALL:
215 break;
216 case SSL_ERROR_SSL:
217 if (errno == EAGAIN)
218 break;
219 default:
220 errno = 0; /* Either an SSL-specific error or EOF */
221 }
222
223 return 0;
224 }
225
226 total_written += ret;
227 }
228
229 return total_written;
230 }
231 #endif
232
233 /*
234 ** send_queued_write
235 ** This is called when there is a chance that some output would
236 ** be possible. This attempts to empty the send queue as far as
237 ** possible, and then if any data is left, a write is rescheduled.
238 */
239 void
240 send_queued_write(struct Client *to)
241 {
242 int retlen = 0, i = 0;
243 dlink_node *ptr = NULL;
244 struct iovec vec[IOV_MAX];
245
246 /*
247 * Once socket is marked dead, we cannot start writing to it,
248 * even if the error is removed...
249 */
250 if (IsDead(to))
251 return; /* No use calling send() now */
252
253 if (HasFlag(to, FLAGS_CORK))
254 return;
255
256 /* Nothing to do? */
257 if (!dbuf_length(&to->localClient->buf_sendq))
258 return;
259
260 /* Build iovec */
261 DLINK_FOREACH(ptr, to->localClient->buf_sendq.blocks.head)
262 {
263 struct dbuf_block *block = ptr->data;
264 struct iovec *iov;
265 int offset;
266
267 if (i >= sizeof(vec) / sizeof(struct iovec))
268 break;
269
270 iov = &vec[i];
271 offset = !i ? to->localClient->buf_sendq.pos : 0;
272
273 iov->iov_base = block->data + offset;
274 iov->iov_len = block->size - offset;
275
276 ++i;
277 }
278
279 /* Next, lets try to write some data */
280 #ifdef HAVE_LIBCRYPTO
281 if (to->localClient->fd.ssl)
282 {
283 if ((retlen = send_SSL_writev(to->localClient->fd.ssl, vec, i)) == -1)
284 return;
285 }
286 else
287 #endif
288 retlen = writev(to->localClient->fd.fd, vec, i);
289
290 if (retlen > 0)
291 {
292 dbuf_delete(&to->localClient->buf_sendq, retlen);
293
294 /* We have some data written .. update counters */
295 to->localClient->send.bytes += retlen;
296 me.localClient->send.bytes += retlen;
297
298 if (dbuf_length(&to->localClient->buf_sendq))
299 comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE,
300 (PF *)sendq_unblocked, to, 0);
301 }
302 else if (retlen < 0 && ignoreErrno(errno))
303 /* we have a non-fatal error, reschedule a write */
304 comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE,
305 (PF *)sendq_unblocked, to, 0);
306 else if (retlen <= 0)
307 dead_link_on_write(to, errno);
308 }
309
310 /* send_queued_all()
311 *
312 * input - NONE
313 * output - NONE
314 * side effects - try to flush sendq of each client
315 */
316 void
317 send_queued_all(void)
318 {
319 dlink_node *ptr;
320
321 /* Servers are processed first, mainly because this can generate
322 * a notice to opers, which is to be delivered by this function.
323 */
324 DLINK_FOREACH(ptr, serv_list.head)
325 send_queued_write(ptr->data);
326
327 DLINK_FOREACH(ptr, unknown_list.head)
328 send_queued_write(ptr->data);
329
330 DLINK_FOREACH(ptr, local_client_list.head)
331 send_queued_write(ptr->data);
332
333 /* NOTE: This can still put clients on aborted_list; unfortunately,
334 * exit_aborted_clients takes precedence over send_queued_all,
335 * because exiting clients can generate server/oper traffic.
336 * The easiest approach is dealing with aborted clients in the next I/O lap.
337 * -adx
338 */
339 }
340
341 /* sendto_one()
342 *
343 * inputs - pointer to destination client
344 * - var args message
345 * output - NONE
346 * side effects - send message to single client
347 */
348 void
349 sendto_one(struct Client *to, const char *pattern, ...)
350 {
351 va_list args;
352 struct dbuf_block *buffer = NULL;
353
354 if (IsDead(to->from))
355 return; /* This socket has already been marked as dead */
356
357 buffer = dbuf_alloc();
358
359 va_start(args, pattern);
360 send_format(buffer, pattern, args);
361 va_end(args);
362
363 send_message(to->from, buffer);
364
365 dbuf_ref_free(buffer);
366 }
367
368 void
369 sendto_one_numeric(struct Client *to, struct Client *from, enum irc_numerics numeric, ...)
370 {
371 struct dbuf_block *buffer = NULL;
372 const char *dest = NULL;
373 va_list args;
374
375 if (IsDead(to->from))
376 return;
377
378 dest = ID_or_name(to, to);
379 if (EmptyString(dest))
380 dest = "*";
381
382 buffer = dbuf_alloc();
383
384 dbuf_put_fmt(buffer, ":%s %03d %s ", ID_or_name(from, to), numeric, dest);
385
386 va_start(args, numeric);
387 send_format(buffer, numeric_form(numeric), args);
388 va_end(args);
389
390 send_message(to->from, buffer);
391
392 dbuf_ref_free(buffer);
393 }
394
395 void
396 sendto_one_notice(struct Client *to, struct Client *from, const char *pattern, ...)
397 {
398 struct dbuf_block *buffer = NULL;
399 const char *dest = NULL;
400 va_list args;
401
402 if (IsDead(to->from))
403 return;
404
405 dest = ID_or_name(to, to);
406 if (EmptyString(dest))
407 dest = "*";
408
409 buffer = dbuf_alloc();
410
411 dbuf_put_fmt(buffer, ":%s NOTICE %s ", ID_or_name(from, to), dest);
412
413 va_start(args, pattern);
414 send_format(buffer, pattern, args);
415 va_end(args);
416
417 send_message(to->from, buffer);
418
419 dbuf_ref_free(buffer);
420 }
421
422 /* sendto_channel_butone()
423 *
424 * inputs - pointer to client(server) to NOT send message to
425 * - pointer to client that is sending this message
426 * - pointer to channel being sent to
427 * - vargs message
428 * output - NONE
429 * side effects - message as given is sent to given channel members.
430 *
431 * WARNING - +D clients are ignored
432 */
433 void
434 sendto_channel_butone(struct Client *one, struct Client *from,
435 struct Channel *chptr, unsigned int type,
436 const char *pattern, ...)
437 {
438 va_list alocal, aremote;
439 struct dbuf_block *local_buf, *remote_buf;
440 dlink_node *ptr = NULL, *ptr_next = NULL;
441
442 local_buf = dbuf_alloc(), remote_buf = dbuf_alloc();
443
444 if (IsServer(from))
445 dbuf_put_fmt(local_buf, ":%s ", from->name);
446 else
447 dbuf_put_fmt(local_buf, ":%s!%s@%s ", from->name, from->username, from->host);
448
449 dbuf_put_fmt(remote_buf, ":%s ", from->id);
450
451 va_start(alocal, pattern);
452 va_start(aremote, pattern);
453 send_format(local_buf, pattern, alocal);
454 send_format(remote_buf, pattern, aremote);
455
456 va_end(aremote);
457 va_end(alocal);
458
459 ++current_serial;
460
461 DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->members.head)
462 {
463 struct Membership *ms = ptr->data;
464 struct Client *target_p = ms->client_p;
465
466 assert(IsClient(target_p));
467
468 if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF) ||
469 (one && target_p->from == one->from))
470 continue;
471
472 if (type && (ms->flags & type) == 0)
473 continue;
474
475 if (MyConnect(target_p))
476 send_message(target_p, local_buf);
477 else if (target_p->from->localClient->serial != current_serial)
478 send_message_remote(target_p->from, from, remote_buf);
479 target_p->from->localClient->serial = current_serial;
480 }
481
482 dbuf_ref_free(local_buf);
483 dbuf_ref_free(remote_buf);
484 }
485
486 /* sendto_server()
487 *
488 * inputs - pointer to client to NOT send to
489 * - pointer to channel
490 * - caps or'd together which must ALL be present
491 * - caps or'd together which must ALL NOT be present
492 * - printf style format string
493 * - args to format string
494 * output - NONE
495 * side effects - Send a message to all connected servers, except the
496 * client 'one' (if non-NULL), as long as the servers
497 * support ALL capabs in 'caps', and NO capabs in 'nocaps'.
498 *
499 * This function was written in an attempt to merge together the other
500 * billion sendto_*serv*() functions, which sprung up with capabs,
501 * lazylinks, uids, etc.
502 * -davidt
503 */
504 void
505 sendto_server(struct Client *one,
506 const unsigned int caps,
507 const unsigned int nocaps,
508 const char *format, ...)
509 {
510 va_list args;
511 dlink_node *ptr = NULL;
512 struct dbuf_block *buffer;
513
514 buffer = dbuf_alloc();
515
516 va_start(args, format);
517 send_format(buffer, format, args);
518 va_end(args);
519
520 DLINK_FOREACH(ptr, serv_list.head)
521 {
522 struct Client *client_p = ptr->data;
523
524 /* If dead already skip */
525 if (IsDead(client_p))
526 continue;
527 /* check against 'one' */
528 if (one && (client_p == one->from))
529 continue;
530 /* check we have required capabs */
531 if ((client_p->localClient->caps & caps) != caps)
532 continue;
533 /* check we don't have any forbidden capabs */
534 if ((client_p->localClient->caps & nocaps))
535 continue;
536
537 send_message(client_p, buffer);
538 }
539
540 dbuf_ref_free(buffer);
541 }
542
543 /* sendto_common_channels_local()
544 *
545 * inputs - pointer to client
546 * - pattern to send
547 * output - NONE
548 * side effects - Sends a message to all people on local server who are
549 * in same channel with user.
550 * used by m_nick.c and exit_one_client.
551 */
552 void
553 sendto_common_channels_local(struct Client *user, int touser, unsigned int cap,
554 const char *pattern, ...)
555 {
556 va_list args;
557 dlink_node *uptr;
558 dlink_node *cptr;
559 struct Channel *chptr;
560 struct Membership *ms;
561 struct Client *target_p;
562 struct dbuf_block *buffer;
563
564 buffer = dbuf_alloc();
565
566 va_start(args, pattern);
567 send_format(buffer, pattern, args);
568 va_end(args);
569
570 ++current_serial;
571
572 DLINK_FOREACH(cptr, user->channel.head)
573 {
574 chptr = ((struct Membership *)cptr->data)->chptr;
575
576 DLINK_FOREACH(uptr, chptr->members.head)
577 {
578 ms = uptr->data;
579 target_p = ms->client_p;
580
581 if (!MyConnect(target_p) || target_p == user || IsDefunct(target_p) ||
582 target_p->localClient->serial == current_serial)
583 continue;
584
585 if (HasCap(target_p, cap) != cap)
586 continue;
587
588 target_p->localClient->serial = current_serial;
589 send_message(target_p, buffer);
590 }
591 }
592
593 if (touser && MyConnect(user) && !IsDead(user) &&
594 user->localClient->serial != current_serial)
595 if (HasCap(user, cap) == cap)
596 send_message(user, buffer);
597
598 dbuf_ref_free(buffer);
599 }
600
601 /* sendto_channel_local()
602 *
603 * inputs - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
604 * - whether to ignore +D clients (YES/NO)
605 * - pointer to channel to send to
606 * - var args pattern
607 * output - NONE
608 * side effects - Send a message to all members of a channel that are
609 * locally connected to this server.
610 */
611 void
612 sendto_channel_local(unsigned int type, int nodeaf, struct Channel *chptr,
613 const char *pattern, ...)
614 {
615 va_list args;
616 dlink_node *ptr = NULL;
617 struct dbuf_block *buffer;
618
619 buffer = dbuf_alloc();
620
621 va_start(args, pattern);
622 send_format(buffer, pattern, args);
623 va_end(args);
624
625 DLINK_FOREACH(ptr, chptr->members.head)
626 {
627 struct Membership *ms = ptr->data;
628 struct Client *target_p = ms->client_p;
629
630 if (type && (ms->flags & type) == 0)
631 continue;
632
633 if (!MyConnect(target_p) || IsDefunct(target_p) ||
634 (nodeaf && HasUMode(target_p, UMODE_DEAF)))
635 continue;
636
637 send_message(target_p, buffer);
638 }
639
640 dbuf_ref_free(buffer);
641 }
642
643 /* sendto_channel_local_butone()
644 *
645 * inputs - pointer to client to NOT send message to
646 * - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
647 * - pointer to channel to send to
648 * - var args pattern
649 * output - NONE
650 * side effects - Send a message to all members of a channel that are
651 * locally connected to this server except one.
652 *
653 * WARNING - +D clients are omitted
654 */
655 void
656 sendto_channel_local_butone(struct Client *one, unsigned int type, unsigned int cap,
657 struct Channel *chptr, const char *pattern, ...)
658 {
659 va_list args;
660 dlink_node *ptr = NULL;
661 struct dbuf_block *buffer;
662
663 buffer = dbuf_alloc();
664
665 va_start(args, pattern);
666 send_format(buffer, pattern, args);
667 va_end(args);
668
669 DLINK_FOREACH(ptr, chptr->members.head)
670 {
671 struct Membership *ms = ptr->data;
672 struct Client *target_p = ms->client_p;
673
674 if (type && (ms->flags & type) == 0)
675 continue;
676
677 if (!MyConnect(target_p) || (one && target_p == one->from))
678 continue;
679
680 if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF))
681 continue;
682
683 if (HasCap(target_p, cap) != cap)
684 continue;
685
686 send_message(target_p, buffer);
687 }
688
689 dbuf_ref_free(buffer);
690 }
691
692 /*
693 ** match_it() and sendto_match_butone() ARE only used
694 ** to send a msg to all ppl on servers/hosts that match a specified mask
695 ** (used for enhanced PRIVMSGs) for opers
696 **
697 ** addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
698 **
699 */
700
701 /* match_it()
702 *
703 * inputs - client pointer to match on
704 * - actual mask to match
705 * - what to match on, HOST or SERVER
706 * output - 1 or 0 if match or not
707 * side effects - NONE
708 */
709 static int
710 match_it(const struct Client *one, const char *mask, unsigned int what)
711 {
712 if (what == MATCH_HOST)
713 return !match(mask, one->host);
714
715 return !match(mask, one->servptr->name);
716 }
717
718 /* sendto_match_butone()
719 *
720 * Send to all clients which match the mask in a way defined on 'what';
721 * either by user hostname or user servername.
722 *
723 * ugh. ONLY used by m_message.c to send an "oper magic" message. ugh.
724 */
725 void
726 sendto_match_butone(struct Client *one, struct Client *from, const char *mask,
727 int what, const char *pattern, ...)
728 {
729 va_list alocal, aremote;
730 dlink_node *ptr = NULL, *ptr_next = NULL;
731 struct dbuf_block *local_buf, *remote_buf;
732
733 local_buf = dbuf_alloc(), remote_buf = dbuf_alloc();
734
735 dbuf_put_fmt(local_buf, ":%s!%s@%s ", from->name, from->username, from->host);
736 dbuf_put_fmt(remote_buf, ":%s ", from->id);
737
738 va_start(alocal, pattern);
739 va_start(aremote, pattern);
740 send_format(local_buf, pattern, alocal);
741 send_format(remote_buf, pattern, aremote);
742 va_end(aremote);
743 va_end(alocal);
744
745 /* scan the local clients */
746 DLINK_FOREACH(ptr, local_client_list.head)
747 {
748 struct Client *client_p = ptr->data;
749
750 if ((!one || client_p != one->from) && !IsDefunct(client_p) &&
751 match_it(client_p, mask, what))
752 send_message(client_p, local_buf);
753 }
754
755 /* Now scan servers */
756 DLINK_FOREACH_SAFE(ptr, ptr_next, serv_list.head)
757 {
758 struct Client *client_p = ptr->data;
759
760 /*
761 * The old code looped through every client on the
762 * network for each server to check if the
763 * server (client_p) has at least 1 client matching
764 * the mask, using something like:
765 *
766 * for (target_p = GlobalClientList; target_p; target_p = target_p->next)
767 * if (IsRegisteredUser(target_p) &&
768 * match_it(target_p, mask, what) &&
769 * (target_p->from == client_p))
770 * vsendto_prefix_one(client_p, from, pattern, args);
771 *
772 * That way, we wouldn't send the message to
773 * a server who didn't have a matching client.
774 * However, on a network such as EFNet, that
775 * code would have looped through about 50
776 * servers, and in each loop, loop through
777 * about 50k clients as well, calling match()
778 * in each nested loop. That is a very bad
779 * thing cpu wise - just send the message
780 * to every connected server and let that
781 * server deal with it.
782 * -wnder
783 */
784 if ((!one || client_p != one->from) && !IsDefunct(client_p))
785 send_message_remote(client_p, from, remote_buf);
786 }
787
788 dbuf_ref_free(local_buf);
789 dbuf_ref_free(remote_buf);
790 }
791
792 /* sendto_match_servs()
793 *
794 * inputs - source client
795 * - mask to send to
796 * - capab needed
797 * - data
798 * outputs - none
799 * side effects - data sent to servers matching with capab
800 */
801 void
802 sendto_match_servs(struct Client *source_p, const char *mask, unsigned int cap,
803 const char *pattern, ...)
804 {
805 va_list args;
806 dlink_node *ptr = NULL;
807 struct dbuf_block *buff_suid;
808
809 buff_suid = dbuf_alloc();
810
811 va_start(args, pattern);
812 dbuf_put_fmt(buff_suid, ":%s ", source_p->id);
813 dbuf_put_args(buff_suid, pattern, args);
814 va_end(args);
815
816 ++current_serial;
817
818 DLINK_FOREACH(ptr, global_serv_list.head)
819 {
820 struct Client *target_p = ptr->data;
821
822 /* Do not attempt to send to ourselves, or the source */
823 if (IsMe(target_p) || target_p->from == source_p->from)
824 continue;
825
826 if (target_p->from->localClient->serial == current_serial)
827 continue;
828
829 if (!match(mask, target_p->name))
830 {
831 /*
832 * if we set the serial here, then we'll never do a
833 * match() again, if !IsCapable()
834 */
835 target_p->from->localClient->serial = current_serial;
836
837 if (!IsCapable(target_p->from, cap))
838 continue;
839
840 send_message_remote(target_p->from, source_p, buff_suid);
841 }
842 }
843
844 dbuf_ref_free(buff_suid);
845 }
846
847 /* sendto_anywhere()
848 *
849 * inputs - pointer to dest client
850 * - pointer to from client
851 * - varags
852 * output - NONE
853 * side effects - less efficient than sendto_remote and sendto_one
854 * but useful when one does not know where target "lives"
855 */
856 void
857 sendto_anywhere(struct Client *to, struct Client *from,
858 const char *command,
859 const char *pattern, ...)
860 {
861 va_list args;
862 struct dbuf_block *buffer = NULL;
863
864 if (IsDead(to->from))
865 return;
866
867 buffer = dbuf_alloc();
868
869 if (MyClient(to) && IsClient(from))
870 dbuf_put_fmt(buffer, ":%s!%s@%s %s %s ", from->name, from->username,
871 from->host, command, to->name);
872 else
873 dbuf_put_fmt(buffer, ":%s %s %s ", ID_or_name(from, to),
874 command, ID_or_name(to, to));
875
876 va_start(args, pattern);
877 send_format(buffer, pattern, args);
878 va_end(args);
879
880 if (MyClient(to))
881 send_message(to, buffer);
882 else
883 send_message_remote(to, from, buffer);
884
885 dbuf_ref_free(buffer);
886 }
887
888 /* sendto_realops_flags()
889 *
890 * inputs - flag types of messages to show to real opers
891 * - flag indicating opers/admins
892 * - var args input message
893 * output - NONE
894 * side effects - Send to *local* ops only but NOT +s nonopers.
895 */
896 void
897 sendto_realops_flags(unsigned int flags, int level, int type, const char *pattern, ...)
898 {
899 const char *ntype = NULL;
900 dlink_node *ptr = NULL;
901 char nbuf[IRCD_BUFSIZE] = "";
902 va_list args;
903
904 va_start(args, pattern);
905 vsnprintf(nbuf, sizeof(nbuf), pattern, args);
906 va_end(args);
907
908 switch (type)
909 {
910 case SEND_NOTICE:
911 ntype = "Notice";
912 break;
913 case SEND_GLOBAL:
914 ntype = "Global";
915 break;
916 case SEND_LOCOPS:
917 ntype = "LocOps";
918 break;
919 default:
920 assert(0);
921 }
922
923 DLINK_FOREACH(ptr, oper_list.head)
924 {
925 struct Client *client_p = ptr->data;
926 assert(HasUMode(client_p, UMODE_OPER));
927
928 /*
929 * If we're sending it to opers and they're an admin, skip.
930 * If we're sending it to admins, and they're not, skip.
931 */
932 if (((level == L_ADMIN) && !HasUMode(client_p, UMODE_ADMIN)) ||
933 ((level == L_OPER) && HasUMode(client_p, UMODE_ADMIN)))
934 continue;
935
936 if (HasUMode(client_p, flags))
937 sendto_one(client_p, ":%s NOTICE %s :*** %s -- %s",
938 me.name, client_p->name, ntype, nbuf);
939 }
940 }
941
942 /* ts_warn()
943 *
944 * inputs - var args message
945 * output - NONE
946 * side effects - Call sendto_realops_flags, with some flood checking
947 * (at most 5 warnings every 5 seconds)
948 */
949 void
950 sendto_realops_flags_ratelimited(const char *pattern, ...)
951 {
952 va_list args;
953 char buffer[IRCD_BUFSIZE] = "";
954 static time_t last = 0;
955 static int warnings = 0;
956
957 /*
958 ** if we're running with TS_WARNINGS enabled and someone does
959 ** something silly like (remotely) connecting a nonTS server,
960 ** we'll get a ton of warnings, so we make sure we don't send
961 ** more than 5 every 5 seconds. -orabidoo
962 */
963
964 if (CurrentTime - last < 5)
965 {
966 if (++warnings > 5)
967 return;
968 }
969 else
970 {
971 last = CurrentTime;
972 warnings = 0;
973 }
974
975 va_start(args, pattern);
976 vsnprintf(buffer, sizeof(buffer), pattern, args);
977 va_end(args);
978
979 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "%s", buffer);
980 ilog(LOG_TYPE_IRCD, "%s", buffer);
981 }
982
983 /* sendto_wallops_flags()
984 *
985 * inputs - flag types of messages to show to real opers
986 * - client sending request
987 * - var args input message
988 * output - NONE
989 * side effects - Send a wallops to local opers
990 */
991 void
992 sendto_wallops_flags(unsigned int flags, struct Client *source_p,
993 const char *pattern, ...)
994 {
995 dlink_node *ptr = NULL;
996 va_list args;
997 struct dbuf_block *buffer;
998
999 buffer = dbuf_alloc();
1000
1001 if (IsClient(source_p))
1002 dbuf_put_fmt(buffer, ":%s!%s@%s WALLOPS :", source_p->name, source_p->username, source_p->host);
1003 else
1004 dbuf_put_fmt(buffer, ":%s WALLOPS :", source_p->name);
1005
1006 va_start(args, pattern);
1007 send_format(buffer, pattern, args);
1008 va_end(args);
1009
1010 DLINK_FOREACH(ptr, oper_list.head)
1011 {
1012 struct Client *client_p = ptr->data;
1013 assert(client_p->umodes & UMODE_OPER);
1014
1015 if (HasUMode(client_p, flags) && !IsDefunct(client_p))
1016 send_message(client_p, buffer);
1017 }
1018
1019 dbuf_ref_free(buffer);
1020 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision