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

Properties

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