ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
Revision: 4890
Committed: Wed Nov 19 17:10:46 2014 UTC (10 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 25501 byte(s)
Log Message:
- Style corrections

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

Properties

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