ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/send.c
Revision: 2518
Committed: Sun Oct 27 19:41:30 2013 UTC (10 years, 5 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/send.c
File size: 30412 byte(s)
Log Message:
- src/send.c: replaced all sprintf() with snprintf()

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * send.c: Functions for sending messages.
4     *
5     * Copyright (C) 2002 by the past and present ircd coders, and others.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26 michael 1011 #include "list.h"
27 adx 30 #include "send.h"
28     #include "channel.h"
29     #include "client.h"
30     #include "dbuf.h"
31     #include "irc_string.h"
32     #include "ircd.h"
33     #include "numeric.h"
34     #include "fdlist.h"
35     #include "s_bsd.h"
36     #include "s_serv.h"
37 michael 1309 #include "conf.h"
38     #include "log.h"
39 adx 30 #include "memory.h"
40     #include "packet.h"
41    
42    
43 michael 1078 static unsigned int current_serial = 0;
44 adx 30
45    
46     /* send_format()
47     *
48     * inputs - buffer to format into
49     * - size of the buffer
50     * - format pattern to use
51     * - var args
52     * output - number of bytes formatted output
53     * side effects - modifies sendbuf
54     */
55     static inline int
56     send_format(char *lsendbuf, int bufsize, const char *pattern, va_list args)
57     {
58     int len;
59    
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     * exceed 512 characters in length, counting all characters
66     * 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     len = vsnprintf(lsendbuf, bufsize - 1, pattern, args);
73     if (len > bufsize - 2)
74     len = bufsize - 2; /* required by some versions of vsnprintf */
75    
76     lsendbuf[len++] = '\r';
77     lsendbuf[len++] = '\n';
78 michael 1078 return len;
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     send_message(struct Client *to, char *buf, int len)
88     {
89 michael 439 assert(!IsMe(to));
90 michael 438 assert(to != &me);
91 adx 30
92 michael 1632 if (dbuf_length(&to->localClient->buf_sendq) + len > 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     (unsigned long)(dbuf_length(&to->localClient->buf_sendq) + len),
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 1798
106     dbuf_put(&to->localClient->buf_sendq, buf, len);
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     if (dbuf_length(&to->localClient->buf_sendq) >
117     (IsServer(to) ? (unsigned int) 1024 : (unsigned int) 4096))
118     send_queued_write(to);
119     }
120    
121     /* send_message_remote()
122     *
123     * inputs - pointer to client from message is being sent
124     * - pointer to client to send to
125     * - pointer to preformatted buffer
126     * - length of input buffer
127     * output - none
128     * side effects - Despite the function name, this only sends to directly
129     * connected clients.
130     *
131     */
132     static void
133     send_message_remote(struct Client *to, struct Client *from,
134     char *buf, int len)
135     {
136     if (!MyConnect(to))
137     {
138 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
139 adx 30 "server send message to %s [%s] dropped from %s(Not local server)",
140     to->name, to->from->name, from->name);
141     return;
142     }
143    
144     /* Optimize by checking if (from && to) before everything */
145     /* we set to->from up there.. */
146    
147     if (!MyClient(from) && IsClient(to) && (to == from->from))
148     {
149     if (IsServer(from))
150     {
151 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
152 adx 30 "Send message to %s [%s] dropped from %s(Fake Dir)",
153     to->name, to->from->name, from->name);
154     return;
155     }
156    
157 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
158 adx 30 "Ghosted: %s[%s@%s] from %s[%s@%s] (%s)",
159     to->name, to->username, to->host,
160     from->name, from->username, from->host,
161     to->from->name);
162    
163 michael 1474 sendto_server(NULL, CAP_TS6, NOCAPS,
164 adx 30 ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
165     me.id, to->name, me.name, to->name,
166     to->username, to->host, to->from->name);
167 michael 1474 sendto_server(NULL, NOCAPS, CAP_TS6,
168 adx 30 ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
169     me.name, to->name, me.name, to->name,
170     to->username, to->host, to->from->name);
171    
172 michael 1219 AddFlag(to, FLAGS_KILLED);
173 adx 30
174     if (IsClient(from))
175 michael 1834 sendto_one(from, form_str(ERR_GHOSTEDCLIENT),
176 adx 30 me.name, from->name, to->name, to->username,
177     to->host, to->from);
178    
179     exit_client(to, &me, "Ghosted client");
180    
181     return;
182     }
183    
184     send_message(to, buf, len);
185     }
186    
187     /*
188     ** sendq_unblocked
189     ** Called when a socket is ready for writing.
190     */
191     void
192     sendq_unblocked(fde_t *fd, struct Client *client_p)
193     {
194     ClearSendqBlocked(client_p);
195     /* let send_queued_write be executed by send_queued_all */
196    
197     #ifdef HAVE_LIBCRYPTO
198     if (fd->flags.pending_read)
199     {
200     fd->flags.pending_read = 0;
201     read_packet(fd, client_p);
202     }
203     #endif
204     }
205    
206     /*
207     ** send_queued_write
208     ** This is called when there is a chance that some output would
209     ** be possible. This attempts to empty the send queue as far as
210     ** possible, and then if any data is left, a write is rescheduled.
211     */
212     void
213     send_queued_write(struct Client *to)
214     {
215     int retlen;
216     struct dbuf_block *first;
217    
218     /*
219     ** Once socket is marked dead, we cannot start writing to it,
220     ** even if the error is removed...
221     */
222     if (IsDead(to) || IsSendqBlocked(to))
223     return; /* no use calling send() now */
224    
225     /* Next, lets try to write some data */
226    
227     if (dbuf_length(&to->localClient->buf_sendq))
228     {
229     do {
230     first = to->localClient->buf_sendq.blocks.head->data;
231    
232     #ifdef HAVE_LIBCRYPTO
233     if (to->localClient->fd.ssl)
234     {
235     retlen = SSL_write(to->localClient->fd.ssl, first->data, first->size);
236    
237     /* translate openssl error codes, sigh */
238     if (retlen < 0)
239     switch (SSL_get_error(to->localClient->fd.ssl, retlen))
240     {
241     case SSL_ERROR_WANT_READ:
242     return; /* retry later, don't register for write events */
243    
244     case SSL_ERROR_WANT_WRITE:
245     errno = EWOULDBLOCK;
246     case SSL_ERROR_SYSCALL:
247     break;
248 michael 430 case SSL_ERROR_SSL:
249     if (errno == EAGAIN)
250     break;
251 adx 30 default:
252     retlen = errno = 0; /* either an SSL-specific error or EOF */
253     }
254     }
255     else
256     #endif
257     retlen = send(to->localClient->fd.fd, first->data, first->size, 0);
258    
259     if (retlen <= 0)
260     break;
261    
262     dbuf_delete(&to->localClient->buf_sendq, retlen);
263    
264     /* We have some data written .. update counters */
265     to->localClient->send.bytes += retlen;
266     me.localClient->send.bytes += retlen;
267     } while (dbuf_length(&to->localClient->buf_sendq));
268    
269     if ((retlen < 0) && (ignoreErrno(errno)))
270     {
271     /* we have a non-fatal error, reschedule a write */
272     SetSendqBlocked(to);
273     comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE,
274     (PF *)sendq_unblocked, (void *)to, 0);
275     }
276     else if (retlen <= 0)
277     {
278     dead_link_on_write(to, errno);
279     return;
280     }
281     }
282     }
283    
284     /* send_queued_all()
285     *
286     * input - NONE
287     * output - NONE
288     * side effects - try to flush sendq of each client
289     */
290     void
291     send_queued_all(void)
292     {
293     dlink_node *ptr;
294    
295     /* Servers are processed first, mainly because this can generate
296     * a notice to opers, which is to be delivered by this function.
297     */
298     DLINK_FOREACH(ptr, serv_list.head)
299     send_queued_write((struct Client *) ptr->data);
300    
301     DLINK_FOREACH(ptr, unknown_list.head)
302     send_queued_write((struct Client *) ptr->data);
303    
304     DLINK_FOREACH(ptr, local_client_list.head)
305     send_queued_write((struct Client *) ptr->data);
306    
307     /* NOTE: This can still put clients on aborted_list; unfortunately,
308     * exit_aborted_clients takes precedence over send_queued_all,
309     * because exiting clients can generate server/oper traffic.
310     * The easiest approach is dealing with aborted clients in the next I/O lap.
311     * -adx
312     */
313     }
314    
315     /* sendto_one()
316     *
317     * inputs - pointer to destination client
318     * - var args message
319     * output - NONE
320     * side effects - send message to single client
321     */
322     void
323     sendto_one(struct Client *to, const char *pattern, ...)
324     {
325     va_list args;
326     char buffer[IRCD_BUFSIZE];
327     int len;
328    
329     if (to->from != NULL)
330     to = to->from;
331     if (IsDead(to))
332     return; /* This socket has already been marked as dead */
333    
334     va_start(args, pattern);
335     len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
336     va_end(args);
337    
338     send_message(to, buffer, len);
339     }
340    
341     /* sendto_channel_butone()
342     *
343     * inputs - pointer to client(server) to NOT send message to
344     * - pointer to client that is sending this message
345     * - pointer to channel being sent to
346     * - vargs message
347     * output - NONE
348     * side effects - message as given is sent to given channel members.
349     *
350     * WARNING - +D clients are ignored
351     */
352     void
353     sendto_channel_butone(struct Client *one, struct Client *from,
354 michael 1479 struct Channel *chptr, unsigned int type,
355 adx 30 const char *pattern, ...)
356     {
357 adx 285 va_list alocal, aremote, auid;
358 adx 30 char local_buf[IRCD_BUFSIZE];
359     char remote_buf[IRCD_BUFSIZE];
360     char uid_buf[IRCD_BUFSIZE];
361     int local_len, remote_len, uid_len;
362 michael 1078 dlink_node *ptr = NULL, *ptr_next = NULL;
363 adx 30
364     if (IsServer(from))
365 michael 2518 local_len = snprintf(local_buf, sizeof(local_buf), ":%s ",
366 michael 1793 from->name);
367 adx 30 else
368 michael 2518 local_len = snprintf(local_buf, sizeof(local_buf), ":%s!%s@%s ",
369     from->name, from->username, from->host);
370     remote_len = snprintf(remote_buf, sizeof(remote_buf), ":%s ",
371     from->name);
372     uid_len = snprintf(uid_buf, sizeof(uid_buf), ":%s ", ID(from));
373 adx 30
374 adx 285 va_start(alocal, pattern);
375     va_start(aremote, pattern);
376     va_start(auid, pattern);
377 adx 30 local_len += send_format(&local_buf[local_len], IRCD_BUFSIZE - local_len,
378 adx 285 pattern, alocal);
379 adx 30 remote_len += send_format(&remote_buf[remote_len], IRCD_BUFSIZE - remote_len,
380 adx 285 pattern, aremote);
381 adx 30 uid_len += send_format(&uid_buf[uid_len], IRCD_BUFSIZE - uid_len, pattern,
382 adx 285 auid);
383     va_end(auid);
384     va_end(aremote);
385     va_end(alocal);
386 adx 30
387     ++current_serial;
388    
389     DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->members.head)
390     {
391 michael 1479 struct Membership *ms = ptr->data;
392     struct Client *target_p = ms->client_p;
393 adx 30
394 michael 1078 assert(IsClient(target_p));
395    
396 michael 1219 if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF) || target_p->from == one)
397 adx 30 continue;
398    
399 michael 1479 if (type != 0 && (ms->flags & type) == 0)
400     continue;
401    
402 michael 1078 if (MyConnect(target_p))
403 adx 30 {
404 michael 1078 if (target_p->localClient->serial != current_serial)
405 adx 30 {
406     send_message(target_p, local_buf, local_len);
407 michael 1078 target_p->localClient->serial = current_serial;
408 adx 30 }
409     }
410     else
411     {
412     /* Now check whether a message has been sent to this
413     * remote link already
414     */
415 michael 1078 if (target_p->from->localClient->serial != current_serial)
416 adx 30 {
417     if (IsCapable(target_p->from, CAP_TS6))
418     send_message_remote(target_p->from, from, uid_buf, uid_len);
419     else
420     send_message_remote(target_p->from, from, remote_buf, remote_len);
421 michael 1078 target_p->from->localClient->serial = current_serial;
422 adx 30 }
423     }
424     }
425     }
426    
427     /* sendto_server()
428     *
429     * inputs - pointer to client to NOT send to
430 db 939 * - pointer to channel
431 adx 30 * - caps or'd together which must ALL be present
432     * - caps or'd together which must ALL NOT be present
433     * - printf style format string
434     * - args to format string
435     * output - NONE
436     * side effects - Send a message to all connected servers, except the
437     * client 'one' (if non-NULL), as long as the servers
438     * support ALL capabs in 'caps', and NO capabs in 'nocaps'.
439     *
440     * This function was written in an attempt to merge together the other
441     * billion sendto_*serv*() functions, which sprung up with capabs,
442     * lazylinks, uids, etc.
443     * -davidt
444     */
445     void
446 michael 1474 sendto_server(struct Client *one,
447 michael 1015 const unsigned int caps,
448     const unsigned int nocaps,
449 adx 30 const char *format, ...)
450     {
451     va_list args;
452 michael 885 dlink_node *ptr = NULL;
453 adx 30 char buffer[IRCD_BUFSIZE];
454 michael 885 int len = 0;
455 adx 30
456     va_start(args, format);
457     len = send_format(buffer, IRCD_BUFSIZE, format, args);
458     va_end(args);
459    
460     DLINK_FOREACH(ptr, serv_list.head)
461     {
462 michael 885 struct Client *client_p = ptr->data;
463 adx 30
464     /* If dead already skip */
465     if (IsDead(client_p))
466     continue;
467     /* check against 'one' */
468     if (one != NULL && (client_p == one->from))
469     continue;
470     /* check we have required capabs */
471     if ((client_p->localClient->caps & caps) != caps)
472     continue;
473     /* check we don't have any forbidden capabs */
474     if ((client_p->localClient->caps & nocaps) != 0)
475     continue;
476    
477     send_message(client_p, buffer, len);
478     }
479     }
480    
481     /* sendto_common_channels_local()
482     *
483     * inputs - pointer to client
484     * - pattern to send
485     * output - NONE
486     * side effects - Sends a message to all people on local server who are
487     * in same channel with user.
488     * used by m_nick.c and exit_one_client.
489     */
490     void
491 michael 1734 sendto_common_channels_local(struct Client *user, int touser, unsigned int cap,
492 adx 30 const char *pattern, ...)
493     {
494     va_list args;
495     dlink_node *uptr;
496     dlink_node *cptr;
497     struct Channel *chptr;
498     struct Membership *ms;
499     struct Client *target_p;
500     char buffer[IRCD_BUFSIZE];
501     int len;
502    
503     va_start(args, pattern);
504     len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
505     va_end(args);
506    
507     ++current_serial;
508    
509     DLINK_FOREACH(cptr, user->channel.head)
510     {
511     chptr = ((struct Membership *) cptr->data)->chptr;
512     assert(chptr != NULL);
513    
514 michael 572 DLINK_FOREACH(uptr, chptr->members.head)
515 adx 30 {
516     ms = uptr->data;
517     target_p = ms->client_p;
518     assert(target_p != NULL);
519    
520 michael 572 if (!MyConnect(target_p) || target_p == user || IsDefunct(target_p) ||
521 michael 1078 target_p->localClient->serial == current_serial)
522 adx 30 continue;
523    
524 michael 1734 if (HasCap(target_p, cap) != cap)
525     continue;
526    
527 michael 1078 target_p->localClient->serial = current_serial;
528 adx 30 send_message(target_p, buffer, len);
529     }
530     }
531    
532     if (touser && MyConnect(user) && !IsDead(user) &&
533 michael 1078 user->localClient->serial != current_serial)
534 michael 1844 if (HasCap(user, cap) == cap)
535 michael 1734 send_message(user, buffer, len);
536 adx 30 }
537    
538     /* sendto_channel_local()
539     *
540     * inputs - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
541     * - whether to ignore +D clients (YES/NO)
542     * - pointer to channel to send to
543     * - var args pattern
544     * output - NONE
545     * side effects - Send a message to all members of a channel that are
546     * locally connected to this server.
547     */
548     void
549     sendto_channel_local(int type, int nodeaf, struct Channel *chptr,
550     const char *pattern, ...)
551     {
552     va_list args;
553     char buffer[IRCD_BUFSIZE];
554     int len;
555     dlink_node *ptr;
556     struct Membership *ms;
557     struct Client *target_p;
558    
559     va_start(args, pattern);
560     len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
561     va_end(args);
562    
563 michael 572 DLINK_FOREACH(ptr, chptr->members.head)
564 adx 30 {
565     ms = ptr->data;
566     target_p = ms->client_p;
567    
568     if (type != 0 && (ms->flags & type) == 0)
569     continue;
570    
571 michael 572 if (!MyConnect(target_p) || IsDefunct(target_p) ||
572 michael 1219 (nodeaf && HasUMode(target_p, UMODE_DEAF)))
573 adx 30 continue;
574    
575     send_message(target_p, buffer, len);
576     }
577     }
578    
579     /* sendto_channel_local_butone()
580     *
581     * inputs - pointer to client to NOT send message to
582     * - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
583     * - pointer to channel to send to
584     * - var args pattern
585     * output - NONE
586     * side effects - Send a message to all members of a channel that are
587     * locally connected to this server except one.
588     *
589     * WARNING - +D clients are omitted
590     */
591     void
592 michael 1734 sendto_channel_local_butone(struct Client *one, int type, unsigned int cap,
593 adx 30 struct Channel *chptr, const char *pattern, ...)
594     {
595     va_list args;
596     char buffer[IRCD_BUFSIZE];
597     int len;
598     struct Client *target_p;
599     struct Membership *ms;
600     dlink_node *ptr;
601    
602     va_start(args, pattern);
603     len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
604     va_end(args);
605    
606 michael 572 DLINK_FOREACH(ptr, chptr->members.head)
607 adx 30 {
608     ms = ptr->data;
609     target_p = ms->client_p;
610    
611     if (type != 0 && (ms->flags & type) == 0)
612     continue;
613    
614 michael 572 if (!MyConnect(target_p) || target_p == one ||
615 michael 1219 IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF))
616 adx 30 continue;
617 michael 1734
618     if (HasCap(target_p, cap) != cap)
619     continue;
620 adx 30 send_message(target_p, buffer, len);
621     }
622     }
623    
624    
625     /* sendto_channel_remote()
626     *
627     * inputs - Client not to send towards
628     * - Client from whom message is from
629     * - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
630     * - pointer to channel to send to
631     * - var args pattern
632     * output - NONE
633     * side effects - Send a message to all members of a channel that are
634     * remote to this server.
635     */
636     void
637 michael 1015 sendto_channel_remote(struct Client *one, struct Client *from, int type,
638     const unsigned int caps, const unsigned int nocaps,
639     struct Channel *chptr, const char *pattern, ...)
640 adx 30 {
641     va_list args;
642     char buffer[IRCD_BUFSIZE];
643     int len;
644     dlink_node *ptr;
645     struct Client *target_p;
646     struct Membership *ms;
647    
648     va_start(args, pattern);
649     len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
650     va_end(args);
651    
652 db 189 ++current_serial;
653    
654 adx 30 DLINK_FOREACH(ptr, chptr->members.head)
655     {
656     ms = ptr->data;
657     target_p = ms->client_p;
658    
659     if (type != 0 && (ms->flags & type) == 0)
660     continue;
661    
662     if (MyConnect(target_p))
663     continue;
664     target_p = target_p->from;
665    
666     if (target_p == one->from ||
667     ((target_p->from->localClient->caps & caps) != caps) ||
668     ((target_p->from->localClient->caps & nocaps) != 0))
669     continue;
670 michael 1078 if (target_p->from->localClient->serial != current_serial)
671 db 189 {
672     send_message(target_p, buffer, len);
673 michael 1078 target_p->from->localClient->serial = current_serial;
674 db 189 }
675 adx 30 }
676     }
677    
678     /*
679     ** match_it() and sendto_match_butone() ARE only used
680     ** to send a msg to all ppl on servers/hosts that match a specified mask
681     ** (used for enhanced PRIVMSGs) for opers
682     **
683     ** addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
684     **
685     */
686    
687     /* match_it()
688     *
689     * inputs - client pointer to match on
690     * - actual mask to match
691     * - what to match on, HOST or SERVER
692     * output - 1 or 0 if match or not
693     * side effects - NONE
694     */
695     static int
696     match_it(const struct Client *one, const char *mask, int what)
697     {
698     if (what == MATCH_HOST)
699 michael 1652 return !match(mask, one->host);
700 adx 30
701 michael 1652 return !match(mask, one->servptr->name);
702 adx 30 }
703    
704     /* sendto_match_butone()
705     *
706     * Send to all clients which match the mask in a way defined on 'what';
707     * either by user hostname or user servername.
708     *
709     * ugh. ONLY used by m_message.c to send an "oper magic" message. ugh.
710     */
711     void
712     sendto_match_butone(struct Client *one, struct Client *from, char *mask,
713     int what, const char *pattern, ...)
714     {
715 adx 285 va_list alocal, aremote;
716 adx 30 struct Client *client_p;
717     dlink_node *ptr, *ptr_next;
718     char local_buf[IRCD_BUFSIZE], remote_buf[IRCD_BUFSIZE];
719 michael 2518 int local_len = snprintf(local_buf, sizeof(local_buf), ":%s!%s@%s ",
720     from->name, from->username, from->host);
721     int remote_len = snprintf(remote_buf, sizeof(remote_buf), ":%s ",
722     from->name);
723 adx 30
724 adx 285 va_start(alocal, pattern);
725     va_start(aremote, pattern);
726 adx 30 local_len += send_format(&local_buf[local_len], IRCD_BUFSIZE - local_len,
727 adx 285 pattern, alocal);
728 adx 30 remote_len += send_format(&remote_buf[remote_len], IRCD_BUFSIZE - remote_len,
729 adx 285 pattern, aremote);
730     va_end(aremote);
731     va_end(alocal);
732 adx 30
733     /* scan the local clients */
734     DLINK_FOREACH(ptr, local_client_list.head)
735     {
736     client_p = ptr->data;
737    
738     if (client_p != one && !IsDefunct(client_p) &&
739     match_it(client_p, mask, what))
740     send_message(client_p, local_buf, local_len);
741     }
742    
743     /* Now scan servers */
744     DLINK_FOREACH_SAFE(ptr, ptr_next, serv_list.head)
745     {
746     client_p = ptr->data;
747    
748     /*
749     * The old code looped through every client on the
750     * network for each server to check if the
751     * server (client_p) has at least 1 client matching
752     * the mask, using something like:
753     *
754     * for (target_p = GlobalClientList; target_p; target_p = target_p->next)
755     * if (IsRegisteredUser(target_p) &&
756     * match_it(target_p, mask, what) &&
757     * (target_p->from == client_p))
758     * vsendto_prefix_one(client_p, from, pattern, args);
759     *
760     * That way, we wouldn't send the message to
761     * a server who didn't have a matching client.
762     * However, on a network such as EFNet, that
763     * code would have looped through about 50
764     * servers, and in each loop, loop through
765     * about 50k clients as well, calling match()
766     * in each nested loop. That is a very bad
767     * thing cpu wise - just send the message
768     * to every connected server and let that
769     * server deal with it.
770     * -wnder
771     */
772     if (client_p != one && !IsDefunct(client_p))
773     send_message_remote(client_p, from, remote_buf, remote_len);
774     }
775     }
776    
777     /* sendto_match_servs()
778     *
779     * inputs - source client
780     * - mask to send to
781     * - capab needed
782     * - data
783     * outputs - none
784     * side effects - data sent to servers matching with capab
785     */
786     void
787     sendto_match_servs(struct Client *source_p, const char *mask, int cap,
788     const char *pattern, ...)
789     {
790     va_list args;
791     struct Client *target_p;
792     dlink_node *ptr;
793     char buffer[IRCD_BUFSIZE];
794     int found = 0;
795    
796     va_start(args, pattern);
797     vsnprintf(buffer, sizeof(buffer), pattern, args);
798     va_end(args);
799    
800 michael 948 ++current_serial;
801 adx 30
802     DLINK_FOREACH(ptr, global_serv_list.head)
803     {
804     target_p = ptr->data;
805    
806     /* Do not attempt to send to ourselves, or the source */
807     if (IsMe(target_p) || target_p->from == source_p->from)
808     continue;
809    
810 michael 1078 if (target_p->from->localClient->serial == current_serial)
811 adx 30 continue;
812    
813 michael 1652 if (!match(mask, target_p->name))
814 adx 30 {
815     /*
816     * if we set the serial here, then we'll never do a
817     * match() again, if !IsCapable()
818     */
819 michael 1078 target_p->from->localClient->serial = current_serial;
820 adx 30 found++;
821    
822     if (!IsCapable(target_p->from, cap))
823     continue;
824    
825     sendto_anywhere(target_p, source_p, "%s", buffer);
826     }
827     }
828     }
829    
830     /* sendto_anywhere()
831     *
832     * inputs - pointer to dest client
833     * - pointer to from client
834     * - varags
835     * output - NONE
836     * side effects - less efficient than sendto_remote and sendto_one
837     * but useful when one does not know where target "lives"
838     */
839     void
840     sendto_anywhere(struct Client *to, struct Client *from,
841     const char *pattern, ...)
842     {
843     va_list args;
844     char buffer[IRCD_BUFSIZE];
845     int len;
846     struct Client *send_to = (to->from != NULL ? to->from : to);
847    
848     if (IsDead(send_to))
849     return;
850    
851     if (MyClient(to))
852     {
853     if (IsServer(from))
854     {
855     if (IsCapable(to, CAP_TS6) && HasID(from))
856 michael 2518 len = snprintf(buffer, sizeof(buffer), ":%s ", from->id);
857 adx 30 else
858 michael 2518 len = snprintf(buffer, sizeof(buffer), ":%s ", from->name);
859 adx 30 }
860     else
861 michael 2518 len = snprintf(buffer, sizeof(buffer), ":%s!%s@%s ",
862     from->name, from->username, from->host);
863 adx 30 }
864 michael 2518 else len = snprintf(buffer, sizeof(buffer), ":%s ", ID_or_name(from, send_to));
865 adx 30
866     va_start(args, pattern);
867     len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
868     va_end(args);
869    
870 michael 2518 if (MyClient(to))
871 adx 30 send_message(send_to, buffer, len);
872     else
873     send_message_remote(send_to, from, buffer, len);
874     }
875    
876     /* sendto_realops_flags()
877     *
878     * inputs - flag types of messages to show to real opers
879     * - flag indicating opers/admins
880     * - var args input message
881     * output - NONE
882     * side effects - Send to *local* ops only but NOT +s nonopers.
883     */
884     void
885 michael 1618 sendto_realops_flags(unsigned int flags, int level, int type, const char *pattern, ...)
886 adx 30 {
887 michael 1618 const char *ntype = NULL;
888 michael 1078 dlink_node *ptr = NULL;
889 adx 30 char nbuf[IRCD_BUFSIZE];
890     va_list args;
891    
892     va_start(args, pattern);
893 michael 1618 vsnprintf(nbuf, sizeof(nbuf), pattern, args);
894 adx 30 va_end(args);
895    
896 michael 1618 switch (type)
897 adx 30 {
898 michael 1618 case SEND_NOTICE:
899     ntype = "Notice";
900     break;
901     case SEND_GLOBAL:
902     ntype = "Global";
903     break;
904     case SEND_LOCOPS:
905     ntype = "LocOps";
906     break;
907     default:
908     assert(0);
909 adx 30 }
910    
911 michael 1206 DLINK_FOREACH(ptr, oper_list.head)
912     {
913     struct Client *client_p = ptr->data;
914 michael 1618 assert(HasUMode(client_p, UMODE_OPER));
915 michael 1206
916 michael 1618 /*
917     * If we're sending it to opers and they're an admin, skip.
918     * If we're sending it to admins, and they're not, skip.
919 michael 1206 */
920 michael 1219 if (((level == L_ADMIN) && !HasUMode(client_p, UMODE_ADMIN)) ||
921 michael 1618 ((level == L_OPER) && HasUMode(client_p, UMODE_ADMIN)))
922 michael 1206 continue;
923    
924 michael 1219 if (HasUMode(client_p, flags))
925 michael 1618 sendto_one(client_p, ":%s NOTICE %s :*** %s -- %s",
926     me.name, client_p->name, ntype, nbuf);
927 michael 1206 }
928     }
929    
930 adx 30 /* sendto_wallops_flags()
931     *
932     * inputs - flag types of messages to show to real opers
933     * - client sending request
934     * - var args input message
935     * output - NONE
936     * side effects - Send a wallops to local opers
937     */
938     void
939     sendto_wallops_flags(unsigned int flags, struct Client *source_p,
940     const char *pattern, ...)
941     {
942 michael 1078 dlink_node *ptr = NULL;
943 adx 30 va_list args;
944     char buffer[IRCD_BUFSIZE];
945     int len;
946    
947     if (IsClient(source_p))
948 michael 2518 len = snprintf(buffer, sizeof(buffer), ":%s!%s@%s WALLOPS :",
949     source_p->name, source_p->username,
950     source_p->host);
951 adx 30 else
952 michael 2518 len = snprintf(buffer, sizeof(buffer), ":%s WALLOPS :",
953     source_p->name);
954 adx 30
955     va_start(args, pattern);
956     len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
957     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 adx 30 send_message(client_p, buffer, len);
966     }
967     }
968    
969     /* ts_warn()
970     *
971     * inputs - var args message
972     * output - NONE
973     * side effects - Call sendto_realops_flags, with some flood checking
974     * (at most 5 warnings every 5 seconds)
975     */
976     void
977     ts_warn(const char *pattern, ...)
978     {
979     va_list args;
980 michael 1078 char buffer[IRCD_BUFSIZE];
981 adx 30 static time_t last = 0;
982     static int warnings = 0;
983    
984     /*
985     ** if we're running with TS_WARNINGS enabled and someone does
986     ** something silly like (remotely) connecting a nonTS server,
987     ** we'll get a ton of warnings, so we make sure we don't send
988     ** more than 5 every 5 seconds. -orabidoo
989     */
990    
991     if (CurrentTime - last < 5)
992     {
993     if (++warnings > 5)
994     return;
995     }
996     else
997     {
998     last = CurrentTime;
999     warnings = 0;
1000     }
1001    
1002     va_start(args, pattern);
1003 michael 1086 vsnprintf(buffer, sizeof(buffer), pattern, args);
1004 adx 30 va_end(args);
1005    
1006 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "%s", buffer);
1007 michael 1247 ilog(LOG_TYPE_IRCD, "%s", buffer);
1008 adx 30 }
1009    
1010     /* kill_client()
1011     *
1012     * inputs - client to send kill towards
1013     * - pointer to client to kill
1014     * - reason for kill
1015     * output - NONE
1016     * side effects - NONE
1017     */
1018     void
1019     kill_client(struct Client *client_p, struct Client *diedie,
1020     const char *pattern, ...)
1021     {
1022     va_list args;
1023     char buffer[IRCD_BUFSIZE];
1024     int len;
1025    
1026     if (client_p->from != NULL)
1027     client_p = client_p->from;
1028     if (IsDead(client_p))
1029     return;
1030    
1031 michael 2518 len = snprintf(buffer, sizeof(buffer), ":%s KILL %s :",
1032     ID_or_name(&me, client_p->from),
1033     ID_or_name(diedie, client_p));
1034 adx 30
1035     va_start(args, pattern);
1036     len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
1037     va_end(args);
1038    
1039     send_message(client_p, buffer, len);
1040     }
1041    
1042     /* kill_client_ll_serv_butone()
1043     *
1044     * inputs - pointer to client to not send to
1045     * - pointer to client to kill
1046     * output - NONE
1047     * side effects - Send a KILL for the given client
1048     * message to all connected servers
1049     * except the client 'one'. Also deal with
1050     * client being unknown to leaf, as in lazylink...
1051     */
1052     void
1053     kill_client_ll_serv_butone(struct Client *one, struct Client *source_p,
1054     const char *pattern, ...)
1055     {
1056     va_list args;
1057     int have_uid = 0;
1058 michael 885 dlink_node *ptr = NULL;
1059 adx 30 char buf_uid[IRCD_BUFSIZE], buf_nick[IRCD_BUFSIZE];
1060 michael 885 int len_uid = 0, len_nick = 0;
1061 adx 30
1062 michael 1115 if (HasID(source_p))
1063 adx 30 {
1064     have_uid = 1;
1065 adx 285 va_start(args, pattern);
1066 michael 2518 len_uid = snprintf(buf_uid, sizeof(buf_uid), ":%s KILL %s :",
1067     me.id, ID(source_p));
1068 adx 30 len_uid += send_format(&buf_uid[len_uid], IRCD_BUFSIZE - len_uid, pattern,
1069     args);
1070 adx 285 va_end(args);
1071 adx 30 }
1072 adx 285
1073     va_start(args, pattern);
1074 michael 2518 len_nick = snprintf(buf_nick, sizeof(buf_nick), ":%s KILL %s :",
1075     me.name, source_p->name);
1076 adx 30 len_nick += send_format(&buf_nick[len_nick], IRCD_BUFSIZE - len_nick, pattern,
1077     args);
1078     va_end(args);
1079    
1080     DLINK_FOREACH(ptr, serv_list.head)
1081     {
1082 michael 885 struct Client *client_p = ptr->data;
1083 adx 30
1084     if (one != NULL && (client_p == one->from))
1085     continue;
1086     if (IsDefunct(client_p))
1087     continue;
1088    
1089 michael 885 if (have_uid && IsCapable(client_p, CAP_TS6))
1090     send_message(client_p, buf_uid, len_uid);
1091     else
1092     send_message(client_p, buf_nick, len_nick);
1093 adx 30 }
1094     }

Properties

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