ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/send.c
Revision: 4212
Committed: Mon Jul 14 09:24:10 2014 UTC (9 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 25970 byte(s)
Log Message:
- Renamed serv_list to local_server_list

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

Properties

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