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

# 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     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * 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     #include "s_serv.h"
39 michael 1309 #include "conf.h"
40     #include "log.h"
41 adx 30 #include "memory.h"
42     #include "packet.h"
43    
44 michael 3241 #ifndef IOV_MAX
45     #define IOV_MAX 64
46     #endif
47 adx 30
48 michael 1078 static unsigned int current_serial = 0;
49 adx 30
50    
51     /* send_format()
52     *
53 michael 3120 * inputs
54     * - buffer
55 adx 30 * - format pattern to use
56     * - var args
57     * output - number of bytes formatted output
58     * side effects - modifies sendbuf
59     */
60 michael 3107 static void
61     send_format(struct dbuf_block *buffer, const char *pattern, va_list args)
62 adx 30 {
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 michael 2638 * exceed 512 characters in length, counting all characters
69 adx 30 * 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 michael 3107 dbuf_put_args(buffer, pattern, args);
76 adx 30
77 michael 3107 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 adx 30 }
83    
84     /*
85     ** send_message
86     ** Internal utility which appends given buffer to the sockets
87     ** sendq.
88     */
89     static void
90 michael 3107 send_message(struct Client *to, struct dbuf_block *buf)
91 adx 30 {
92 michael 439 assert(!IsMe(to));
93 michael 438 assert(to != &me);
94 adx 30
95 michael 3107 if (dbuf_length(&to->localClient->buf_sendq) + buf->size > get_sendq(&to->localClient->confs))
96 adx 30 {
97     if (IsServer(to))
98 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
99 michael 1761 "Max SendQ limit exceeded for %s: %lu > %u",
100 adx 30 get_client_name(to, HIDE_IP),
101 michael 3107 (unsigned long)(dbuf_length(&to->localClient->buf_sendq) + buf->size),
102 michael 1632 get_sendq(&to->localClient->confs));
103 adx 30 if (IsClient(to))
104     SetSendQExceeded(to);
105 michael 3215
106 adx 30 dead_link_on_write(to, 0);
107     return;
108     }
109 michael 2638
110 michael 3107 dbuf_add(&to->localClient->buf_sendq, buf);
111 adx 30
112     /*
113 michael 3215 * 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 adx 30 */
117     ++to->localClient->send.messages;
118     ++me.localClient->send.messages;
119    
120 michael 2881 send_queued_write(to);
121 adx 30 }
122    
123     /* send_message_remote()
124     *
125     * inputs - pointer to client from message is being sent
126     * - pointer to client to send to
127 michael 3120 * - pointer to buffer
128 adx 30 * output - none
129     * side effects - Despite the function name, this only sends to directly
130     * connected clients.
131 michael 2916 *
132 adx 30 */
133     static void
134 michael 3107 send_message_remote(struct Client *to, struct Client *from, struct dbuf_block *buf)
135 adx 30 {
136 michael 3112 to = to->from;
137 michael 2793
138 adx 30 if (!MyConnect(to))
139     {
140 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
141 michael 2638 "Server send message to %s [%s] dropped from %s(Not local server)",
142     to->name, to->from->name, from->name);
143 adx 30 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 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
154 adx 30 "Send message to %s [%s] dropped from %s(Fake Dir)",
155     to->name, to->from->name, from->name);
156     return;
157     }
158    
159 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
160 adx 30 "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 michael 3135 sendto_server(NULL, NOCAPS, NOCAPS,
166 adx 30 ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
167 michael 3174 me.id, to->id, me.name, to->name,
168 adx 30 to->username, to->host, to->from->name);
169    
170 michael 1219 AddFlag(to, FLAGS_KILLED);
171 adx 30
172     if (IsClient(from))
173 michael 3109 sendto_one_numeric(from, &me, ERR_GHOSTEDCLIENT, to->name,
174     to->username, to->host, to->from);
175 adx 30
176 michael 3171 exit_client(to, "Ghosted client");
177 adx 30 return;
178 michael 2638 }
179 adx 30
180 michael 3107 send_message(to, buf);
181 adx 30 }
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 michael 2881 assert(fd == &client_p->localClient->fd);
191     send_queued_write(client_p);
192 adx 30 }
193    
194 michael 3241 #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 adx 30 /*
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 michael 3241 int retlen = 0, i = 0;
243     dlink_node *ptr = NULL;
244     struct iovec vec[IOV_MAX];
245 adx 30
246     /*
247 michael 3241 * Once socket is marked dead, we cannot start writing to it,
248     * even if the error is removed...
249 adx 30 */
250 michael 2881 if (IsDead(to))
251 michael 3241 return; /* No use calling send() now */
252 adx 30
253 michael 3241 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 adx 30 {
263 michael 3241 struct dbuf_block *block = ptr->data;
264     struct iovec *iov;
265     int offset;
266 adx 30
267 michael 3241 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 adx 30 #ifdef HAVE_LIBCRYPTO
281 michael 3241 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 adx 30 #endif
288 michael 3241 retlen = writev(to->localClient->fd.fd, vec, i);
289 adx 30
290 michael 3241 if (retlen > 0)
291     {
292     dbuf_delete(&to->localClient->buf_sendq, retlen);
293 adx 30
294 michael 3241 /* We have some data written .. update counters */
295     to->localClient->send.bytes += retlen;
296     me.localClient->send.bytes += retlen;
297 adx 30
298 michael 3241 if (dbuf_length(&to->localClient->buf_sendq))
299 michael 3113 comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE,
300     (PF *)sendq_unblocked, to, 0);
301 adx 30 }
302 michael 3241 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 adx 30 }
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 michael 2638 send_queued_write(ptr->data);
326 adx 30
327     DLINK_FOREACH(ptr, unknown_list.head)
328 michael 2638 send_queued_write(ptr->data);
329 adx 30
330     DLINK_FOREACH(ptr, local_client_list.head)
331 michael 2638 send_queued_write(ptr->data);
332 adx 30
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 michael 3189 struct dbuf_block *buffer = NULL;
353 adx 30
354 michael 3189 if (IsDead(to->from))
355 michael 3107 return; /* This socket has already been marked as dead */
356 adx 30
357 michael 3107 buffer = dbuf_alloc();
358    
359 adx 30 va_start(args, pattern);
360 michael 3107 send_format(buffer, pattern, args);
361 adx 30 va_end(args);
362    
363 michael 3189 send_message(to->from, buffer);
364 michael 3107
365     dbuf_ref_free(buffer);
366 adx 30 }
367    
368 michael 3109 void
369     sendto_one_numeric(struct Client *to, struct Client *from, enum irc_numerics numeric, ...)
370     {
371 michael 3189 struct dbuf_block *buffer = NULL;
372     const char *dest = NULL;
373 michael 3109 va_list args;
374    
375 michael 3189 if (IsDead(to->from))
376 michael 3109 return;
377    
378     dest = ID_or_name(to, to);
379     if (EmptyString(dest))
380     dest = "*";
381    
382     buffer = dbuf_alloc();
383    
384 michael 3113 dbuf_put_fmt(buffer, ":%s %03d %s ", ID_or_name(from, to), numeric, dest);
385 michael 3109
386     va_start(args, numeric);
387     send_format(buffer, numeric_form(numeric), args);
388     va_end(args);
389    
390 michael 3189 send_message(to->from, buffer);
391 michael 3109
392     dbuf_ref_free(buffer);
393     }
394    
395 michael 3110 void
396     sendto_one_notice(struct Client *to, struct Client *from, const char *pattern, ...)
397     {
398 michael 3189 struct dbuf_block *buffer = NULL;
399     const char *dest = NULL;
400 michael 3110 va_list args;
401    
402 michael 3189 if (IsDead(to->from))
403 michael 3110 return;
404    
405     dest = ID_or_name(to, to);
406     if (EmptyString(dest))
407     dest = "*";
408    
409     buffer = dbuf_alloc();
410    
411 michael 3113 dbuf_put_fmt(buffer, ":%s NOTICE %s ", ID_or_name(from, to), dest);
412 michael 3110
413     va_start(args, pattern);
414     send_format(buffer, pattern, args);
415     va_end(args);
416    
417 michael 3189 send_message(to->from, buffer);
418 michael 3110
419     dbuf_ref_free(buffer);
420     }
421    
422 adx 30 /* 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 michael 1479 struct Channel *chptr, unsigned int type,
436 adx 30 const char *pattern, ...)
437     {
438 michael 3136 va_list alocal, aremote;
439     struct dbuf_block *local_buf, *remote_buf;
440 michael 1078 dlink_node *ptr = NULL, *ptr_next = NULL;
441 adx 30
442 michael 3136 local_buf = dbuf_alloc(), remote_buf = dbuf_alloc();
443 michael 3107
444 adx 30 if (IsServer(from))
445 michael 3113 dbuf_put_fmt(local_buf, ":%s ", from->name);
446 adx 30 else
447 michael 3113 dbuf_put_fmt(local_buf, ":%s!%s@%s ", from->name, from->username, from->host);
448 adx 30
449 michael 3186 dbuf_put_fmt(remote_buf, ":%s ", from->id);
450 michael 3107
451 adx 285 va_start(alocal, pattern);
452     va_start(aremote, pattern);
453 michael 3107 send_format(local_buf, pattern, alocal);
454     send_format(remote_buf, pattern, aremote);
455 michael 3136
456 adx 285 va_end(aremote);
457     va_end(alocal);
458 adx 30
459     ++current_serial;
460    
461     DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->members.head)
462     {
463 michael 1479 struct Membership *ms = ptr->data;
464     struct Client *target_p = ms->client_p;
465 adx 30
466 michael 1078 assert(IsClient(target_p));
467    
468 michael 3164 if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF) ||
469     (one && target_p->from == one->from))
470 adx 30 continue;
471    
472 michael 3243 if (type && (ms->flags & type) == 0)
473 michael 1479 continue;
474    
475 michael 1078 if (MyConnect(target_p))
476 michael 3103 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 adx 30 }
481 michael 3107
482     dbuf_ref_free(local_buf);
483     dbuf_ref_free(remote_buf);
484 adx 30 }
485    
486     /* sendto_server()
487 michael 2916 *
488 adx 30 * inputs - pointer to client to NOT send to
489 db 939 * - pointer to channel
490 adx 30 * - 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 michael 2916 *
499 adx 30 * 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 michael 2638 void
505 michael 1474 sendto_server(struct Client *one,
506 michael 1015 const unsigned int caps,
507     const unsigned int nocaps,
508 adx 30 const char *format, ...)
509     {
510     va_list args;
511 michael 885 dlink_node *ptr = NULL;
512 michael 3107 struct dbuf_block *buffer;
513 adx 30
514 michael 3107 buffer = dbuf_alloc();
515    
516 adx 30 va_start(args, format);
517 michael 3107 send_format(buffer, format, args);
518 adx 30 va_end(args);
519    
520     DLINK_FOREACH(ptr, serv_list.head)
521     {
522 michael 885 struct Client *client_p = ptr->data;
523 adx 30
524     /* If dead already skip */
525     if (IsDead(client_p))
526     continue;
527     /* check against 'one' */
528 michael 3243 if (one && (client_p == one->from))
529 adx 30 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 michael 3243 if ((client_p->localClient->caps & nocaps))
535 adx 30 continue;
536    
537 michael 3107 send_message(client_p, buffer);
538 adx 30 }
539 michael 3107
540     dbuf_ref_free(buffer);
541 adx 30 }
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 michael 2916 * in same channel with user.
550 adx 30 * used by m_nick.c and exit_one_client.
551     */
552     void
553 michael 1734 sendto_common_channels_local(struct Client *user, int touser, unsigned int cap,
554 adx 30 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 michael 3107 struct dbuf_block *buffer;
563 adx 30
564 michael 3107 buffer = dbuf_alloc();
565    
566 adx 30 va_start(args, pattern);
567 michael 3107 send_format(buffer, pattern, args);
568 adx 30 va_end(args);
569    
570     ++current_serial;
571    
572     DLINK_FOREACH(cptr, user->channel.head)
573     {
574 michael 2638 chptr = ((struct Membership *)cptr->data)->chptr;
575 adx 30
576 michael 572 DLINK_FOREACH(uptr, chptr->members.head)
577 adx 30 {
578     ms = uptr->data;
579     target_p = ms->client_p;
580    
581 michael 572 if (!MyConnect(target_p) || target_p == user || IsDefunct(target_p) ||
582 michael 1078 target_p->localClient->serial == current_serial)
583 adx 30 continue;
584    
585 michael 1734 if (HasCap(target_p, cap) != cap)
586     continue;
587    
588 michael 1078 target_p->localClient->serial = current_serial;
589 michael 3107 send_message(target_p, buffer);
590 adx 30 }
591     }
592    
593     if (touser && MyConnect(user) && !IsDead(user) &&
594 michael 1078 user->localClient->serial != current_serial)
595 michael 1844 if (HasCap(user, cap) == cap)
596 michael 3107 send_message(user, buffer);
597    
598     dbuf_ref_free(buffer);
599 adx 30 }
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 michael 2543 sendto_channel_local(unsigned int type, int nodeaf, struct Channel *chptr,
613 adx 30 const char *pattern, ...)
614     {
615     va_list args;
616 michael 2638 dlink_node *ptr = NULL;
617 michael 3107 struct dbuf_block *buffer;
618 adx 30
619 michael 3107 buffer = dbuf_alloc();
620    
621 adx 30 va_start(args, pattern);
622 michael 3107 send_format(buffer, pattern, args);
623 adx 30 va_end(args);
624    
625 michael 572 DLINK_FOREACH(ptr, chptr->members.head)
626 adx 30 {
627 michael 2638 struct Membership *ms = ptr->data;
628     struct Client *target_p = ms->client_p;
629 adx 30
630 michael 3243 if (type && (ms->flags & type) == 0)
631 adx 30 continue;
632    
633 michael 572 if (!MyConnect(target_p) || IsDefunct(target_p) ||
634 michael 1219 (nodeaf && HasUMode(target_p, UMODE_DEAF)))
635 adx 30 continue;
636    
637 michael 3107 send_message(target_p, buffer);
638 adx 30 }
639 michael 3107
640     dbuf_ref_free(buffer);
641 adx 30 }
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 michael 2638 void
656 michael 2543 sendto_channel_local_butone(struct Client *one, unsigned int type, unsigned int cap,
657 michael 2638 struct Channel *chptr, const char *pattern, ...)
658 adx 30 {
659     va_list args;
660 michael 2638 dlink_node *ptr = NULL;
661 michael 3107 struct dbuf_block *buffer;
662 adx 30
663 michael 3107 buffer = dbuf_alloc();
664    
665 michael 2638 va_start(args, pattern);
666 michael 3107 send_format(buffer, pattern, args);
667 adx 30 va_end(args);
668    
669 michael 2638 DLINK_FOREACH(ptr, chptr->members.head)
670     {
671     struct Membership *ms = ptr->data;
672     struct Client *target_p = ms->client_p;
673 adx 30
674 michael 3243 if (type && (ms->flags & type) == 0)
675 adx 30 continue;
676    
677 michael 3170 if (!MyConnect(target_p) || (one && target_p == one->from))
678 adx 30 continue;
679 michael 1734
680 michael 3170 if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF))
681     continue;
682    
683 michael 1734 if (HasCap(target_p, cap) != cap)
684 michael 2638 continue;
685    
686 michael 3107 send_message(target_p, buffer);
687 adx 30 }
688 michael 3107
689     dbuf_ref_free(buffer);
690 adx 30 }
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 michael 2867 match_it(const struct Client *one, const char *mask, unsigned int what)
711 adx 30 {
712     if (what == MATCH_HOST)
713 michael 1652 return !match(mask, one->host);
714 adx 30
715 michael 1652 return !match(mask, one->servptr->name);
716 adx 30 }
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 michael 3170 sendto_match_butone(struct Client *one, struct Client *from, const char *mask,
727 adx 30 int what, const char *pattern, ...)
728     {
729 adx 285 va_list alocal, aremote;
730 michael 3107 dlink_node *ptr = NULL, *ptr_next = NULL;
731     struct dbuf_block *local_buf, *remote_buf;
732 adx 30
733 michael 3107 local_buf = dbuf_alloc(), remote_buf = dbuf_alloc();
734    
735 michael 3113 dbuf_put_fmt(local_buf, ":%s!%s@%s ", from->name, from->username, from->host);
736 michael 3186 dbuf_put_fmt(remote_buf, ":%s ", from->id);
737 michael 3107
738 adx 285 va_start(alocal, pattern);
739     va_start(aremote, pattern);
740 michael 3107 send_format(local_buf, pattern, alocal);
741     send_format(remote_buf, pattern, aremote);
742 adx 285 va_end(aremote);
743     va_end(alocal);
744 adx 30
745     /* scan the local clients */
746     DLINK_FOREACH(ptr, local_client_list.head)
747     {
748 michael 3107 struct Client *client_p = ptr->data;
749 adx 30
750 michael 3174 if ((!one || client_p != one->from) && !IsDefunct(client_p) &&
751 adx 30 match_it(client_p, mask, what))
752 michael 3107 send_message(client_p, local_buf);
753 adx 30 }
754    
755     /* Now scan servers */
756     DLINK_FOREACH_SAFE(ptr, ptr_next, serv_list.head)
757     {
758 michael 3107 struct Client *client_p = ptr->data;
759 adx 30
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 michael 3170 if ((!one || client_p != one->from) && !IsDefunct(client_p))
785 michael 3107 send_message_remote(client_p, from, remote_buf);
786 adx 30 }
787 michael 3107
788     dbuf_ref_free(local_buf);
789     dbuf_ref_free(remote_buf);
790 adx 30 }
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 michael 2543 sendto_match_servs(struct Client *source_p, const char *mask, unsigned int cap,
803 adx 30 const char *pattern, ...)
804     {
805     va_list args;
806 michael 2638 dlink_node *ptr = NULL;
807 michael 3135 struct dbuf_block *buff_suid;
808 adx 30
809 michael 3135 buff_suid = dbuf_alloc();
810 michael 3107
811 adx 30 va_start(args, pattern);
812 michael 3186 dbuf_put_fmt(buff_suid, ":%s ", source_p->id);
813 michael 3107 dbuf_put_args(buff_suid, pattern, args);
814 adx 30 va_end(args);
815    
816 michael 948 ++current_serial;
817 adx 30
818     DLINK_FOREACH(ptr, global_serv_list.head)
819     {
820 michael 2638 struct Client *target_p = ptr->data;
821 adx 30
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 michael 1078 if (target_p->from->localClient->serial == current_serial)
827 adx 30 continue;
828    
829 michael 1652 if (!match(mask, target_p->name))
830 adx 30 {
831     /*
832     * if we set the serial here, then we'll never do a
833     * match() again, if !IsCapable()
834     */
835 michael 1078 target_p->from->localClient->serial = current_serial;
836 adx 30
837     if (!IsCapable(target_p->from, cap))
838     continue;
839    
840 michael 3135 send_message_remote(target_p->from, source_p, buff_suid);
841 adx 30 }
842     }
843 michael 3107
844     dbuf_ref_free(buff_suid);
845 adx 30 }
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 michael 2793 const char *command,
859 adx 30 const char *pattern, ...)
860     {
861     va_list args;
862 michael 3189 struct dbuf_block *buffer = NULL;
863 adx 30
864 michael 2793 if (IsDead(to->from))
865 adx 30 return;
866    
867 michael 3107 buffer = dbuf_alloc();
868    
869 michael 3189 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 michael 2634 else
873 michael 3189 dbuf_put_fmt(buffer, ":%s %s %s ", ID_or_name(from, to),
874     command, ID_or_name(to, to));
875 adx 30
876     va_start(args, pattern);
877 michael 3107 send_format(buffer, pattern, args);
878 adx 30 va_end(args);
879    
880 michael 2518 if (MyClient(to))
881 michael 3107 send_message(to, buffer);
882 adx 30 else
883 michael 3107 send_message_remote(to, from, buffer);
884    
885     dbuf_ref_free(buffer);
886 adx 30 }
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 michael 1618 sendto_realops_flags(unsigned int flags, int level, int type, const char *pattern, ...)
898 adx 30 {
899 michael 1618 const char *ntype = NULL;
900 michael 1078 dlink_node *ptr = NULL;
901 michael 3215 char nbuf[IRCD_BUFSIZE] = "";
902 adx 30 va_list args;
903    
904     va_start(args, pattern);
905 michael 1618 vsnprintf(nbuf, sizeof(nbuf), pattern, args);
906 adx 30 va_end(args);
907    
908 michael 1618 switch (type)
909 adx 30 {
910 michael 1618 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 adx 30 }
922    
923 michael 1206 DLINK_FOREACH(ptr, oper_list.head)
924     {
925     struct Client *client_p = ptr->data;
926 michael 1618 assert(HasUMode(client_p, UMODE_OPER));
927 michael 1206
928 michael 1618 /*
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 michael 1206 */
932 michael 1219 if (((level == L_ADMIN) && !HasUMode(client_p, UMODE_ADMIN)) ||
933 michael 2638 ((level == L_OPER) && HasUMode(client_p, UMODE_ADMIN)))
934 michael 1206 continue;
935    
936 michael 1219 if (HasUMode(client_p, flags))
937 michael 1618 sendto_one(client_p, ":%s NOTICE %s :*** %s -- %s",
938     me.name, client_p->name, ntype, nbuf);
939 michael 1206 }
940     }
941    
942 michael 3215 /* 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 adx 30 /* 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 michael 1078 dlink_node *ptr = NULL;
996 adx 30 va_list args;
997 michael 3107 struct dbuf_block *buffer;
998 adx 30
999 michael 3107 buffer = dbuf_alloc();
1000    
1001 adx 30 if (IsClient(source_p))
1002 michael 3113 dbuf_put_fmt(buffer, ":%s!%s@%s WALLOPS :", source_p->name, source_p->username, source_p->host);
1003 adx 30 else
1004 michael 3113 dbuf_put_fmt(buffer, ":%s WALLOPS :", source_p->name);
1005 adx 30
1006     va_start(args, pattern);
1007 michael 3107 send_format(buffer, pattern, args);
1008 adx 30 va_end(args);
1009    
1010     DLINK_FOREACH(ptr, oper_list.head)
1011     {
1012 michael 1078 struct Client *client_p = ptr->data;
1013 adx 30 assert(client_p->umodes & UMODE_OPER);
1014    
1015 michael 1219 if (HasUMode(client_p, flags) && !IsDefunct(client_p))
1016 michael 3107 send_message(client_p, buffer);
1017 adx 30 }
1018 michael 3107
1019     dbuf_ref_free(buffer);
1020 adx 30 }

Properties

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