ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
Revision: 3112
Committed: Thu Mar 6 22:06:02 2014 UTC (11 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 28780 byte(s)
Log Message:
- send.c: removed useless to->from checks

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    
45 michael 1078 static unsigned int current_serial = 0;
46 adx 30
47    
48     /* send_format()
49     *
50     * inputs - buffer to format into
51     * - size of the buffer
52     * - 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     dead_link_on_write(to, 0);
103     return;
104     }
105 michael 2638
106 michael 3107 dbuf_add(&to->localClient->buf_sendq, buf);
107 adx 30
108     /*
109     ** Update statistics. The following is slightly incorrect
110     ** because it counts messages even if queued, but bytes
111     ** only really sent. Queued bytes get updated in SendQueued.
112     */
113     ++to->localClient->send.messages;
114     ++me.localClient->send.messages;
115    
116 michael 2881 send_queued_write(to);
117 adx 30 }
118    
119     /* send_message_remote()
120     *
121     * inputs - pointer to client from message is being sent
122     * - pointer to client to send to
123     * - pointer to preformatted buffer
124     * - length of input buffer
125     * 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 1474 sendto_server(NULL, CAP_TS6, NOCAPS,
163 adx 30 ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
164     me.id, to->name, me.name, to->name,
165     to->username, to->host, to->from->name);
166 michael 1474 sendto_server(NULL, NOCAPS, CAP_TS6,
167 adx 30 ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
168     me.name, to->name, me.name, to->name,
169     to->username, to->host, to->from->name);
170    
171 michael 1219 AddFlag(to, FLAGS_KILLED);
172 adx 30
173     if (IsClient(from))
174 michael 3109 sendto_one_numeric(from, &me, ERR_GHOSTEDCLIENT, to->name,
175     to->username, to->host, to->from);
176 adx 30
177     exit_client(to, &me, "Ghosted client");
178     return;
179 michael 2638 }
180 adx 30
181 michael 3107 send_message(to, buf);
182 adx 30 }
183    
184     /*
185     ** sendq_unblocked
186     ** Called when a socket is ready for writing.
187     */
188     void
189     sendq_unblocked(fde_t *fd, struct Client *client_p)
190     {
191 michael 2881 assert(fd == &client_p->localClient->fd);
192     send_queued_write(client_p);
193 adx 30 }
194    
195     /*
196     ** send_queued_write
197     ** This is called when there is a chance that some output would
198     ** be possible. This attempts to empty the send queue as far as
199     ** possible, and then if any data is left, a write is rescheduled.
200     */
201     void
202     send_queued_write(struct Client *to)
203     {
204 michael 2638 int retlen = 0;
205 michael 3107 struct dbuf_queue *sendq = &to->localClient->buf_sendq;
206 adx 30
207     /*
208     ** Once socket is marked dead, we cannot start writing to it,
209     ** even if the error is removed...
210     */
211 michael 2881 if (IsDead(to))
212 adx 30 return; /* no use calling send() now */
213    
214     /* Next, lets try to write some data */
215 michael 3107 if (dbuf_length(sendq))
216 adx 30 {
217 michael 2638 do
218     {
219 michael 3107 struct dbuf_block *first = sendq->blocks.head->data;
220 adx 30
221     #ifdef HAVE_LIBCRYPTO
222     if (to->localClient->fd.ssl)
223     {
224 michael 3107 retlen = SSL_write(to->localClient->fd.ssl, first->data + sendq->pos, first->size - sendq->pos);
225 adx 30
226     /* translate openssl error codes, sigh */
227 michael 2638 if (retlen < 0)
228     {
229     switch (SSL_get_error(to->localClient->fd.ssl, retlen))
230     {
231 adx 30 case SSL_ERROR_WANT_READ:
232 michael 2638 return; /* retry later, don't register for write events */
233     case SSL_ERROR_WANT_WRITE:
234     errno = EWOULDBLOCK;
235 adx 30 case SSL_ERROR_SYSCALL:
236 michael 2638 break;
237 michael 430 case SSL_ERROR_SSL:
238     if (errno == EAGAIN)
239     break;
240 adx 30 default:
241 michael 2638 retlen = errno = 0; /* either an SSL-specific error or EOF */
242     }
243     }
244 adx 30 }
245     else
246     #endif
247 michael 3107 retlen = send(to->localClient->fd.fd, first->data + sendq->pos, first->size - sendq->pos, 0);
248 adx 30
249     if (retlen <= 0)
250     break;
251    
252     dbuf_delete(&to->localClient->buf_sendq, retlen);
253    
254     /* We have some data written .. update counters */
255     to->localClient->send.bytes += retlen;
256     me.localClient->send.bytes += retlen;
257     } while (dbuf_length(&to->localClient->buf_sendq));
258    
259     if ((retlen < 0) && (ignoreErrno(errno)))
260     {
261     /* we have a non-fatal error, reschedule a write */
262 michael 3107 comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE, (PF *)sendq_unblocked, to, 0);
263 adx 30 }
264     else if (retlen <= 0)
265     {
266     dead_link_on_write(to, errno);
267     return;
268     }
269     }
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     DLINK_FOREACH(ptr, serv_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 3107 struct dbuf_block *buffer;
315 adx 30
316 michael 3112 to = to->from;
317    
318 adx 30 if (IsDead(to))
319 michael 3107 return; /* This socket has already been marked as dead */
320 adx 30
321 michael 3107 buffer = dbuf_alloc();
322    
323 adx 30 va_start(args, pattern);
324 michael 3107 send_format(buffer, pattern, args);
325 adx 30 va_end(args);
326    
327 michael 3107 send_message(to, buffer);
328    
329     dbuf_ref_free(buffer);
330 adx 30 }
331    
332 michael 3109 void
333     sendto_one_numeric(struct Client *to, struct Client *from, enum irc_numerics numeric, ...)
334     {
335     struct dbuf_block *buffer;
336     va_list args;
337     const char *dest;
338    
339 michael 3112 to = to->from;
340    
341 michael 3109 if (IsDead(to))
342     return;
343    
344     dest = ID_or_name(to, to);
345     if (EmptyString(dest))
346     dest = "*";
347    
348     buffer = dbuf_alloc();
349    
350     dbuf_put(buffer, ":%s %03d %s ", ID_or_name(from, to), numeric, dest);
351    
352     va_start(args, numeric);
353     send_format(buffer, numeric_form(numeric), args);
354     va_end(args);
355    
356     send_message(to, buffer);
357    
358     dbuf_ref_free(buffer);
359     }
360    
361 michael 3110 void
362     sendto_one_notice(struct Client *to, struct Client *from, const char *pattern, ...)
363     {
364     struct dbuf_block *buffer;
365     va_list args;
366     const char *dest;
367    
368 michael 3112 to = to->from;
369    
370 michael 3110 if (IsDead(to))
371     return;
372    
373     dest = ID_or_name(to, to);
374     if (EmptyString(dest))
375     dest = "*";
376    
377     buffer = dbuf_alloc();
378    
379     dbuf_put(buffer, ":%s NOTICE %s ", ID_or_name(from, to), dest);
380    
381     va_start(args, pattern);
382     send_format(buffer, pattern, args);
383     va_end(args);
384    
385     send_message(to, buffer);
386    
387     dbuf_ref_free(buffer);
388     }
389    
390 adx 30 /* sendto_channel_butone()
391     *
392     * inputs - pointer to client(server) to NOT send message to
393     * - pointer to client that is sending this message
394     * - pointer to channel being sent to
395     * - vargs message
396     * output - NONE
397     * side effects - message as given is sent to given channel members.
398     *
399     * WARNING - +D clients are ignored
400     */
401     void
402     sendto_channel_butone(struct Client *one, struct Client *from,
403 michael 1479 struct Channel *chptr, unsigned int type,
404 adx 30 const char *pattern, ...)
405     {
406 adx 285 va_list alocal, aremote, auid;
407 michael 3107 struct dbuf_block *local_buf, *remote_buf, *uid_buf;
408 michael 1078 dlink_node *ptr = NULL, *ptr_next = NULL;
409 adx 30
410 michael 3107 local_buf = dbuf_alloc(), remote_buf = dbuf_alloc(), uid_buf = dbuf_alloc();
411    
412 adx 30 if (IsServer(from))
413 michael 3107 dbuf_put(local_buf, ":%s ", from->name);
414 adx 30 else
415 michael 3107 dbuf_put(local_buf, ":%s!%s@%s ", from->name, from->username, from->host);
416 adx 30
417 michael 3107 dbuf_put(remote_buf, ":%s ", from->name);
418     dbuf_put(uid_buf, ":%s ", ID(from));
419    
420 adx 285 va_start(alocal, pattern);
421     va_start(aremote, pattern);
422     va_start(auid, pattern);
423 michael 3107 send_format(local_buf, pattern, alocal);
424     send_format(remote_buf, pattern, aremote);
425     send_format(uid_buf, pattern, auid);
426 adx 285 va_end(auid);
427     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 1219 if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF) || target_p->from == one)
440 adx 30 continue;
441    
442 michael 1479 if (type != 0 && (ms->flags & type) == 0)
443     continue;
444    
445 michael 1078 if (MyConnect(target_p))
446 michael 3103 send_message(target_p, local_buf);
447     else if (target_p->from->localClient->serial != current_serial)
448     send_message_remote(target_p->from, from, remote_buf);
449     target_p->from->localClient->serial = current_serial;
450 adx 30 }
451 michael 3107
452     dbuf_ref_free(local_buf);
453     dbuf_ref_free(remote_buf);
454     dbuf_ref_free(uid_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     DLINK_FOREACH(ptr, serv_list.head)
492     {
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     if (one != NULL && (client_p == one->from))
500     continue;
501     /* check we have required capabs */
502     if ((client_p->localClient->caps & caps) != caps)
503     continue;
504     /* check we don't have any forbidden capabs */
505     if ((client_p->localClient->caps & nocaps) != 0)
506     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 assert(chptr != NULL);
547    
548 michael 572 DLINK_FOREACH(uptr, chptr->members.head)
549 adx 30 {
550     ms = uptr->data;
551     target_p = ms->client_p;
552     assert(target_p != NULL);
553    
554 michael 572 if (!MyConnect(target_p) || target_p == user || IsDefunct(target_p) ||
555 michael 1078 target_p->localClient->serial == current_serial)
556 adx 30 continue;
557    
558 michael 1734 if (HasCap(target_p, cap) != cap)
559     continue;
560    
561 michael 1078 target_p->localClient->serial = current_serial;
562 michael 3107 send_message(target_p, buffer);
563 adx 30 }
564     }
565    
566     if (touser && MyConnect(user) && !IsDead(user) &&
567 michael 1078 user->localClient->serial != current_serial)
568 michael 1844 if (HasCap(user, cap) == cap)
569 michael 3107 send_message(user, buffer);
570    
571     dbuf_ref_free(buffer);
572 adx 30 }
573    
574     /* sendto_channel_local()
575     *
576     * inputs - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
577     * - whether to ignore +D clients (YES/NO)
578     * - pointer to channel to send to
579     * - var args pattern
580     * output - NONE
581     * side effects - Send a message to all members of a channel that are
582     * locally connected to this server.
583     */
584     void
585 michael 2543 sendto_channel_local(unsigned int type, int nodeaf, struct Channel *chptr,
586 adx 30 const char *pattern, ...)
587     {
588     va_list args;
589 michael 2638 dlink_node *ptr = NULL;
590 michael 3107 struct dbuf_block *buffer;
591 adx 30
592 michael 3107 buffer = dbuf_alloc();
593    
594 adx 30 va_start(args, pattern);
595 michael 3107 send_format(buffer, pattern, args);
596 adx 30 va_end(args);
597    
598 michael 572 DLINK_FOREACH(ptr, chptr->members.head)
599 adx 30 {
600 michael 2638 struct Membership *ms = ptr->data;
601     struct Client *target_p = ms->client_p;
602 adx 30
603     if (type != 0 && (ms->flags & type) == 0)
604     continue;
605    
606 michael 572 if (!MyConnect(target_p) || IsDefunct(target_p) ||
607 michael 1219 (nodeaf && HasUMode(target_p, UMODE_DEAF)))
608 adx 30 continue;
609    
610 michael 3107 send_message(target_p, buffer);
611 adx 30 }
612 michael 3107
613     dbuf_ref_free(buffer);
614 adx 30 }
615    
616     /* sendto_channel_local_butone()
617     *
618     * inputs - pointer to client to NOT send message to
619     * - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
620     * - pointer to channel to send to
621     * - var args pattern
622     * output - NONE
623     * side effects - Send a message to all members of a channel that are
624     * locally connected to this server except one.
625     *
626     * WARNING - +D clients are omitted
627     */
628 michael 2638 void
629 michael 2543 sendto_channel_local_butone(struct Client *one, unsigned int type, unsigned int cap,
630 michael 2638 struct Channel *chptr, const char *pattern, ...)
631 adx 30 {
632     va_list args;
633 michael 2638 dlink_node *ptr = NULL;
634 michael 3107 struct dbuf_block *buffer;
635 adx 30
636 michael 3107 buffer = dbuf_alloc();
637    
638 michael 2638 va_start(args, pattern);
639 michael 3107 send_format(buffer, pattern, args);
640 adx 30 va_end(args);
641    
642 michael 2638 DLINK_FOREACH(ptr, chptr->members.head)
643     {
644     struct Membership *ms = ptr->data;
645     struct Client *target_p = ms->client_p;
646 adx 30
647     if (type != 0 && (ms->flags & type) == 0)
648     continue;
649    
650 michael 572 if (!MyConnect(target_p) || target_p == one ||
651 michael 1219 IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF))
652 adx 30 continue;
653 michael 1734
654     if (HasCap(target_p, cap) != cap)
655 michael 2638 continue;
656    
657 michael 3107 send_message(target_p, buffer);
658 adx 30 }
659 michael 3107
660     dbuf_ref_free(buffer);
661 adx 30 }
662    
663     /*
664     ** match_it() and sendto_match_butone() ARE only used
665     ** to send a msg to all ppl on servers/hosts that match a specified mask
666     ** (used for enhanced PRIVMSGs) for opers
667     **
668     ** addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
669     **
670     */
671    
672     /* match_it()
673     *
674     * inputs - client pointer to match on
675     * - actual mask to match
676     * - what to match on, HOST or SERVER
677     * output - 1 or 0 if match or not
678     * side effects - NONE
679     */
680     static int
681 michael 2867 match_it(const struct Client *one, const char *mask, unsigned int what)
682 adx 30 {
683     if (what == MATCH_HOST)
684 michael 1652 return !match(mask, one->host);
685 adx 30
686 michael 1652 return !match(mask, one->servptr->name);
687 adx 30 }
688    
689     /* sendto_match_butone()
690     *
691     * Send to all clients which match the mask in a way defined on 'what';
692     * either by user hostname or user servername.
693     *
694     * ugh. ONLY used by m_message.c to send an "oper magic" message. ugh.
695     */
696     void
697     sendto_match_butone(struct Client *one, struct Client *from, char *mask,
698     int what, const char *pattern, ...)
699     {
700 adx 285 va_list alocal, aremote;
701 michael 3107 dlink_node *ptr = NULL, *ptr_next = NULL;
702     struct dbuf_block *local_buf, *remote_buf;
703 adx 30
704 michael 3107 local_buf = dbuf_alloc(), remote_buf = dbuf_alloc();
705    
706     dbuf_put(local_buf, ":%s!%s@%s ", from->name, from->username, from->host);
707     dbuf_put(remote_buf, ":%s ", from->name);
708    
709 adx 285 va_start(alocal, pattern);
710     va_start(aremote, pattern);
711 michael 3107 send_format(local_buf, pattern, alocal);
712     send_format(remote_buf, pattern, aremote);
713 adx 285 va_end(aremote);
714     va_end(alocal);
715 adx 30
716     /* scan the local clients */
717     DLINK_FOREACH(ptr, local_client_list.head)
718     {
719 michael 3107 struct Client *client_p = ptr->data;
720 adx 30
721     if (client_p != one && !IsDefunct(client_p) &&
722     match_it(client_p, mask, what))
723 michael 3107 send_message(client_p, local_buf);
724 adx 30 }
725    
726     /* Now scan servers */
727     DLINK_FOREACH_SAFE(ptr, ptr_next, serv_list.head)
728     {
729 michael 3107 struct Client *client_p = ptr->data;
730 adx 30
731     /*
732     * The old code looped through every client on the
733     * network for each server to check if the
734     * server (client_p) has at least 1 client matching
735     * the mask, using something like:
736     *
737     * for (target_p = GlobalClientList; target_p; target_p = target_p->next)
738     * if (IsRegisteredUser(target_p) &&
739     * match_it(target_p, mask, what) &&
740     * (target_p->from == client_p))
741     * vsendto_prefix_one(client_p, from, pattern, args);
742     *
743     * That way, we wouldn't send the message to
744     * a server who didn't have a matching client.
745     * However, on a network such as EFNet, that
746     * code would have looped through about 50
747     * servers, and in each loop, loop through
748     * about 50k clients as well, calling match()
749     * in each nested loop. That is a very bad
750     * thing cpu wise - just send the message
751     * to every connected server and let that
752     * server deal with it.
753     * -wnder
754     */
755     if (client_p != one && !IsDefunct(client_p))
756 michael 3107 send_message_remote(client_p, from, remote_buf);
757 adx 30 }
758 michael 3107
759     dbuf_ref_free(local_buf);
760     dbuf_ref_free(remote_buf);
761 adx 30 }
762    
763     /* sendto_match_servs()
764     *
765     * inputs - source client
766     * - mask to send to
767     * - capab needed
768     * - data
769     * outputs - none
770     * side effects - data sent to servers matching with capab
771     */
772     void
773 michael 2543 sendto_match_servs(struct Client *source_p, const char *mask, unsigned int cap,
774 adx 30 const char *pattern, ...)
775     {
776     va_list args;
777 michael 2638 dlink_node *ptr = NULL;
778 michael 3107 struct dbuf_block *buff_suid, *buff_name;
779 adx 30
780 michael 3107 buff_suid = dbuf_alloc(), buff_name = dbuf_alloc();
781    
782 adx 30 va_start(args, pattern);
783 michael 3107 dbuf_put(buff_suid, ":%s ", ID(source_p));
784     dbuf_put_args(buff_suid, pattern, args);
785 adx 30 va_end(args);
786    
787 michael 2793 va_start(args, pattern);
788 michael 3107 dbuf_put(buff_name, ":%s ", source_p->name);
789     dbuf_put_args(buff_name, pattern, args);
790 michael 2793 va_end(args);
791    
792 michael 948 ++current_serial;
793 adx 30
794     DLINK_FOREACH(ptr, global_serv_list.head)
795     {
796 michael 2638 struct Client *target_p = ptr->data;
797 adx 30
798     /* Do not attempt to send to ourselves, or the source */
799     if (IsMe(target_p) || target_p->from == source_p->from)
800     continue;
801    
802 michael 1078 if (target_p->from->localClient->serial == current_serial)
803 adx 30 continue;
804    
805 michael 1652 if (!match(mask, target_p->name))
806 adx 30 {
807     /*
808     * if we set the serial here, then we'll never do a
809     * match() again, if !IsCapable()
810     */
811 michael 1078 target_p->from->localClient->serial = current_serial;
812 adx 30
813     if (!IsCapable(target_p->from, cap))
814     continue;
815    
816 michael 2793 if (HasID(target_p->from))
817 michael 3107 send_message_remote(target_p->from, source_p, buff_suid);
818 michael 2793 else
819 michael 3107 send_message_remote(target_p->from, source_p, buff_name);
820 adx 30 }
821     }
822 michael 3107
823     dbuf_ref_free(buff_suid);
824     dbuf_ref_free(buff_name);
825 adx 30 }
826    
827     /* sendto_anywhere()
828     *
829     * inputs - pointer to dest client
830     * - pointer to from client
831     * - varags
832     * output - NONE
833     * side effects - less efficient than sendto_remote and sendto_one
834     * but useful when one does not know where target "lives"
835     */
836     void
837     sendto_anywhere(struct Client *to, struct Client *from,
838 michael 2793 const char *command,
839 adx 30 const char *pattern, ...)
840     {
841     va_list args;
842 michael 3107 struct dbuf_block *buffer;
843 adx 30
844 michael 2793 if (IsDead(to->from))
845 adx 30 return;
846    
847 michael 3107 buffer = dbuf_alloc();
848    
849 adx 30 if (MyClient(to))
850     {
851     if (IsServer(from))
852 michael 3107 dbuf_put(buffer, ":%s %s %s ", from->name, command, to->name);
853 adx 30 else
854 michael 3107 dbuf_put(buffer, ":%s!%s@%s %s %s ", from->name, from->username, from->host, command, to->name);
855 adx 30 }
856 michael 2634 else
857 michael 3107 dbuf_put(buffer, ":%s %s %s ", ID_or_name(from, to), command, ID_or_name(to, to));
858 adx 30
859     va_start(args, pattern);
860 michael 3107 send_format(buffer, pattern, args);
861 adx 30 va_end(args);
862    
863 michael 2518 if (MyClient(to))
864 michael 3107 send_message(to, buffer);
865 adx 30 else
866 michael 3107 send_message_remote(to, from, buffer);
867    
868     dbuf_ref_free(buffer);
869 adx 30 }
870    
871     /* sendto_realops_flags()
872     *
873     * inputs - flag types of messages to show to real opers
874     * - flag indicating opers/admins
875     * - var args input message
876     * output - NONE
877     * side effects - Send to *local* ops only but NOT +s nonopers.
878     */
879     void
880 michael 1618 sendto_realops_flags(unsigned int flags, int level, int type, const char *pattern, ...)
881 adx 30 {
882 michael 1618 const char *ntype = NULL;
883 michael 1078 dlink_node *ptr = NULL;
884 adx 30 char nbuf[IRCD_BUFSIZE];
885     va_list args;
886    
887     va_start(args, pattern);
888 michael 1618 vsnprintf(nbuf, sizeof(nbuf), pattern, args);
889 adx 30 va_end(args);
890    
891 michael 1618 switch (type)
892 adx 30 {
893 michael 1618 case SEND_NOTICE:
894     ntype = "Notice";
895     break;
896     case SEND_GLOBAL:
897     ntype = "Global";
898     break;
899     case SEND_LOCOPS:
900     ntype = "LocOps";
901     break;
902     default:
903     assert(0);
904 adx 30 }
905    
906 michael 1206 DLINK_FOREACH(ptr, oper_list.head)
907     {
908     struct Client *client_p = ptr->data;
909 michael 1618 assert(HasUMode(client_p, UMODE_OPER));
910 michael 1206
911 michael 1618 /*
912     * If we're sending it to opers and they're an admin, skip.
913     * If we're sending it to admins, and they're not, skip.
914 michael 1206 */
915 michael 1219 if (((level == L_ADMIN) && !HasUMode(client_p, UMODE_ADMIN)) ||
916 michael 2638 ((level == L_OPER) && HasUMode(client_p, UMODE_ADMIN)))
917 michael 1206 continue;
918    
919 michael 1219 if (HasUMode(client_p, flags))
920 michael 1618 sendto_one(client_p, ":%s NOTICE %s :*** %s -- %s",
921     me.name, client_p->name, ntype, nbuf);
922 michael 1206 }
923     }
924    
925 adx 30 /* sendto_wallops_flags()
926     *
927     * inputs - flag types of messages to show to real opers
928     * - client sending request
929     * - var args input message
930     * output - NONE
931     * side effects - Send a wallops to local opers
932     */
933     void
934     sendto_wallops_flags(unsigned int flags, struct Client *source_p,
935     const char *pattern, ...)
936     {
937 michael 1078 dlink_node *ptr = NULL;
938 adx 30 va_list args;
939 michael 3107 struct dbuf_block *buffer;
940 adx 30
941 michael 3107 buffer = dbuf_alloc();
942    
943 adx 30 if (IsClient(source_p))
944 michael 3107 dbuf_put(buffer, ":%s!%s@%s WALLOPS :", source_p->name, source_p->username, source_p->host);
945 adx 30 else
946 michael 3107 dbuf_put(buffer, ":%s WALLOPS :", source_p->name);
947 adx 30
948     va_start(args, pattern);
949 michael 3107 send_format(buffer, pattern, args);
950 adx 30 va_end(args);
951    
952     DLINK_FOREACH(ptr, oper_list.head)
953     {
954 michael 1078 struct Client *client_p = ptr->data;
955 adx 30 assert(client_p->umodes & UMODE_OPER);
956    
957 michael 1219 if (HasUMode(client_p, flags) && !IsDefunct(client_p))
958 michael 3107 send_message(client_p, buffer);
959 adx 30 }
960 michael 3107
961     dbuf_ref_free(buffer);
962 adx 30 }
963    
964     /* ts_warn()
965     *
966     * inputs - var args message
967     * output - NONE
968     * side effects - Call sendto_realops_flags, with some flood checking
969     * (at most 5 warnings every 5 seconds)
970     */
971     void
972 michael 3078 sendto_realops_flags_ratelimited(const char *pattern, ...)
973 adx 30 {
974     va_list args;
975 michael 1078 char buffer[IRCD_BUFSIZE];
976 adx 30 static time_t last = 0;
977     static int warnings = 0;
978    
979     /*
980     ** if we're running with TS_WARNINGS enabled and someone does
981     ** something silly like (remotely) connecting a nonTS server,
982     ** we'll get a ton of warnings, so we make sure we don't send
983     ** more than 5 every 5 seconds. -orabidoo
984     */
985    
986     if (CurrentTime - last < 5)
987     {
988     if (++warnings > 5)
989     return;
990     }
991     else
992     {
993     last = CurrentTime;
994     warnings = 0;
995     }
996    
997     va_start(args, pattern);
998 michael 1086 vsnprintf(buffer, sizeof(buffer), pattern, args);
999 adx 30 va_end(args);
1000    
1001 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "%s", buffer);
1002 michael 1247 ilog(LOG_TYPE_IRCD, "%s", buffer);
1003 adx 30 }
1004    
1005     /* kill_client()
1006     *
1007     * inputs - client to send kill towards
1008     * - pointer to client to kill
1009     * - reason for kill
1010     * output - NONE
1011     * side effects - NONE
1012     */
1013     void
1014     kill_client(struct Client *client_p, struct Client *diedie,
1015     const char *pattern, ...)
1016     {
1017     va_list args;
1018 michael 3107 struct dbuf_block *buffer;
1019 adx 30
1020     if (client_p->from != NULL)
1021     client_p = client_p->from;
1022     if (IsDead(client_p))
1023     return;
1024    
1025 michael 3107 buffer = dbuf_alloc();
1026 adx 30
1027 michael 3107 dbuf_put(buffer, ":%s KILL %s :",
1028     ID_or_name(&me, client_p),
1029     ID_or_name(diedie, client_p));
1030    
1031 adx 30 va_start(args, pattern);
1032 michael 3107 send_format(buffer, pattern, args);
1033 adx 30 va_end(args);
1034    
1035 michael 3107 send_message(client_p, buffer);
1036    
1037     dbuf_ref_free(buffer);
1038 adx 30 }
1039    
1040     /* kill_client_ll_serv_butone()
1041     *
1042     * inputs - pointer to client to not send to
1043     * - pointer to client to kill
1044     * output - NONE
1045     * side effects - Send a KILL for the given client
1046     * message to all connected servers
1047     * except the client 'one'. Also deal with
1048     * client being unknown to leaf, as in lazylink...
1049     */
1050     void
1051 michael 2565 kill_client_serv_butone(struct Client *one, struct Client *source_p,
1052     const char *pattern, ...)
1053 adx 30 {
1054     va_list args;
1055     int have_uid = 0;
1056 michael 885 dlink_node *ptr = NULL;
1057 michael 3107 struct dbuf_block *uid_buffer, *nick_buffer;
1058 adx 30
1059 michael 3107 uid_buffer = dbuf_alloc(), nick_buffer = dbuf_alloc();
1060    
1061 michael 1115 if (HasID(source_p))
1062 adx 30 {
1063     have_uid = 1;
1064 adx 285 va_start(args, pattern);
1065 michael 3107 dbuf_put(uid_buffer, ":%s KILL %s :", me.id, ID(source_p));
1066     send_format(uid_buffer, pattern, args);
1067 adx 285 va_end(args);
1068 adx 30 }
1069 adx 285
1070     va_start(args, pattern);
1071 michael 3107 dbuf_put(nick_buffer, ":%s KILL %s :", me.name, source_p->name);
1072     send_format(nick_buffer, pattern, args);
1073 adx 30 va_end(args);
1074    
1075     DLINK_FOREACH(ptr, serv_list.head)
1076     {
1077 michael 885 struct Client *client_p = ptr->data;
1078 adx 30
1079     if (one != NULL && (client_p == one->from))
1080     continue;
1081     if (IsDefunct(client_p))
1082     continue;
1083    
1084 michael 885 if (have_uid && IsCapable(client_p, CAP_TS6))
1085 michael 3107 send_message(client_p, uid_buffer);
1086 michael 885 else
1087 michael 3107 send_message(client_p, nick_buffer);
1088 adx 30 }
1089 michael 3107
1090     dbuf_ref_free(uid_buffer);
1091     dbuf_ref_free(nick_buffer);
1092 michael 2638 }

Properties

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