ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
Revision: 4995
Committed: Tue Dec 9 13:26:01 2014 UTC (10 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 24537 byte(s)
Log Message:
- send.c:sendq_unblocked(): const correctness

File Contents

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

Properties

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