ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/send.c
Revision: 285
Committed: Sat Dec 3 10:57:47 2005 UTC (19 years, 8 months ago) by adx
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/send.c
File size: 34327 byte(s)
Log Message:
+ fixed unportable usage of va_list that make ircd cry on amd64

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     #include "tools.h"
27     #include "send.h"
28     #include "channel.h"
29     #include "client.h"
30     #include "common.h"
31     #include "dbuf.h"
32     #include "irc_string.h"
33     #include "ircd.h"
34     #include "handlers.h"
35     #include "numeric.h"
36     #include "fdlist.h"
37     #include "s_bsd.h"
38     #include "s_serv.h"
39     #include "sprintf_irc.h"
40     #include "s_conf.h"
41     #include "list.h"
42     #include "s_log.h"
43     #include "memory.h"
44     #include "hook.h"
45     #include "irc_getnameinfo.h"
46     #include "packet.h"
47    
48     #define LOG_BUFSIZE 2048
49    
50     struct Callback *iosend_cb = NULL;
51     struct Callback *iosendctrl_cb = NULL;
52    
53     static void send_message(struct Client *, char *, int);
54     static void send_message_remote(struct Client *, struct Client *, char *, int);
55    
56     static unsigned long current_serial = 0L;
57    
58     /* send_format()
59     *
60     * inputs - buffer to format into
61     * - size of the buffer
62     * - format pattern to use
63     * - var args
64     * output - number of bytes formatted output
65     * side effects - modifies sendbuf
66     */
67     static inline int
68     send_format(char *lsendbuf, int bufsize, const char *pattern, va_list args)
69     {
70     int len;
71    
72     /*
73     * from rfc1459
74     *
75     * IRC messages are always lines of characters terminated with a CR-LF
76     * (Carriage Return - Line Feed) pair, and these messages shall not
77     * exceed 512 characters in length, counting all characters
78     * including the trailing CR-LF.
79     * Thus, there are 510 characters maximum allowed
80     * for the command and its parameters. There is no provision for
81     * continuation message lines. See section 7 for more details about
82     * current implementations.
83     */
84     len = vsnprintf(lsendbuf, bufsize - 1, pattern, args);
85     if (len > bufsize - 2)
86     len = bufsize - 2; /* required by some versions of vsnprintf */
87    
88     lsendbuf[len++] = '\r';
89     lsendbuf[len++] = '\n';
90     return (len);
91     }
92    
93     /*
94 adx 163 * iosend_default - append a packet to the client's sendq.
95     */
96     void *
97     iosend_default(va_list args)
98     {
99     struct Client *to = va_arg(args, struct Client *);
100     int length = va_arg(args, int);
101     char *buf = va_arg(args, char *);
102    
103     dbuf_put(&to->localClient->buf_sendq, buf, length);
104     return NULL;
105     }
106    
107     /*
108 adx 30 ** send_message
109     ** Internal utility which appends given buffer to the sockets
110     ** sendq.
111     */
112     static void
113     send_message(struct Client *to, char *buf, int len)
114     {
115     #ifdef INVARIANTS
116     if (IsMe(to))
117     {
118     sendto_realops_flags(UMODE_ALL, L_ALL,
119     "Trying to send message to myself!");
120     return;
121     }
122     #endif
123    
124     if (dbuf_length(&to->localClient->buf_sendq) + len > get_sendq(to))
125     {
126     if (IsServer(to))
127     sendto_realops_flags(UMODE_ALL, L_ALL,
128     "Max SendQ limit exceeded for %s: %lu > %lu",
129     get_client_name(to, HIDE_IP),
130     (unsigned long)(dbuf_length(&to->localClient->buf_sendq) + len),
131     get_sendq(to));
132     if (IsClient(to))
133     SetSendQExceeded(to);
134     dead_link_on_write(to, 0);
135     return;
136     }
137    
138 adx 163 execute_callback(iosend_cb, to, len, buf);
139 adx 30
140     /*
141     ** Update statistics. The following is slightly incorrect
142     ** because it counts messages even if queued, but bytes
143     ** only really sent. Queued bytes get updated in SendQueued.
144     */
145     ++to->localClient->send.messages;
146     ++me.localClient->send.messages;
147    
148     if (dbuf_length(&to->localClient->buf_sendq) >
149     (IsServer(to) ? (unsigned int) 1024 : (unsigned int) 4096))
150     send_queued_write(to);
151     }
152    
153     /* send_message_remote()
154     *
155     * inputs - pointer to client from message is being sent
156     * - pointer to client to send to
157     * - pointer to preformatted buffer
158     * - length of input buffer
159     * output - none
160     * side effects - Despite the function name, this only sends to directly
161     * connected clients.
162     *
163     */
164     static void
165     send_message_remote(struct Client *to, struct Client *from,
166     char *buf, int len)
167     {
168     if (!MyConnect(to))
169     {
170     sendto_realops_flags(UMODE_ALL, L_ALL,
171     "server send message to %s [%s] dropped from %s(Not local server)",
172     to->name, to->from->name, from->name);
173     return;
174     }
175    
176     if (ServerInfo.hub && IsCapable(to, CAP_LL))
177     {
178     if (((from->lazyLinkClientExists &
179     to->localClient->serverMask) == 0))
180     client_burst_if_needed(to, from);
181     }
182    
183     /* Optimize by checking if (from && to) before everything */
184     /* we set to->from up there.. */
185    
186     if (!MyClient(from) && IsClient(to) && (to == from->from))
187     {
188     if (IsServer(from))
189     {
190     sendto_realops_flags(UMODE_ALL, L_ALL,
191     "Send message to %s [%s] dropped from %s(Fake Dir)",
192     to->name, to->from->name, from->name);
193     return;
194     }
195    
196     sendto_realops_flags(UMODE_ALL, L_ALL,
197     "Ghosted: %s[%s@%s] from %s[%s@%s] (%s)",
198     to->name, to->username, to->host,
199     from->name, from->username, from->host,
200     to->from->name);
201    
202     sendto_server(NULL, to, NULL, CAP_TS6, NOCAPS, NOFLAGS,
203     ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
204     me.id, to->name, me.name, to->name,
205     to->username, to->host, to->from->name);
206     sendto_server(NULL, to, NULL, NOCAPS, CAP_TS6, NOFLAGS,
207     ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
208     me.name, to->name, me.name, to->name,
209     to->username, to->host, to->from->name);
210    
211     SetKilled(to);
212    
213     if (IsClient(from))
214     sendto_one(from, form_str(ERR_GHOSTEDCLIENT),
215     me.name, from->name, to->name, to->username,
216     to->host, to->from);
217    
218     exit_client(to, &me, "Ghosted client");
219    
220     return;
221     }
222    
223     send_message(to, buf, len);
224     }
225    
226     /*
227     ** sendq_unblocked
228     ** Called when a socket is ready for writing.
229     */
230     void
231     sendq_unblocked(fde_t *fd, struct Client *client_p)
232     {
233     ClearSendqBlocked(client_p);
234     /* let send_queued_write be executed by send_queued_all */
235    
236     #ifdef HAVE_LIBCRYPTO
237     if (fd->flags.pending_read)
238     {
239     fd->flags.pending_read = 0;
240     read_packet(fd, client_p);
241     }
242     #endif
243     }
244    
245     /*
246     ** slinkq_unblocked
247     ** Called when a server control socket is ready for writing.
248     */
249     static void
250     slinkq_unblocked(fde_t *fd, struct Client *client_p)
251     {
252     ClearSlinkqBlocked(client_p);
253     send_queued_slink_write(client_p);
254     }
255    
256     /*
257     ** send_queued_write
258     ** This is called when there is a chance that some output would
259     ** be possible. This attempts to empty the send queue as far as
260     ** possible, and then if any data is left, a write is rescheduled.
261     */
262     void
263     send_queued_write(struct Client *to)
264     {
265     int retlen;
266     struct dbuf_block *first;
267    
268     /*
269     ** Once socket is marked dead, we cannot start writing to it,
270     ** even if the error is removed...
271     */
272     if (IsDead(to) || IsSendqBlocked(to))
273     return; /* no use calling send() now */
274    
275     /* Next, lets try to write some data */
276    
277     if (dbuf_length(&to->localClient->buf_sendq))
278     {
279     do {
280     first = to->localClient->buf_sendq.blocks.head->data;
281    
282     #ifdef HAVE_LIBCRYPTO
283     if (to->localClient->fd.ssl)
284     {
285     retlen = SSL_write(to->localClient->fd.ssl, first->data, first->size);
286    
287     /* translate openssl error codes, sigh */
288     if (retlen < 0)
289     switch (SSL_get_error(to->localClient->fd.ssl, retlen))
290     {
291     case SSL_ERROR_WANT_READ:
292     return; /* retry later, don't register for write events */
293    
294     case SSL_ERROR_WANT_WRITE:
295     errno = EWOULDBLOCK;
296     case SSL_ERROR_SYSCALL:
297     break;
298    
299     default:
300     retlen = errno = 0; /* either an SSL-specific error or EOF */
301     }
302     }
303     else
304     #endif
305     retlen = send(to->localClient->fd.fd, first->data, first->size, 0);
306    
307     if (retlen <= 0)
308     {
309     #ifdef _WIN32
310     errno = WSAGetLastError();
311     #endif
312     break;
313     }
314    
315     dbuf_delete(&to->localClient->buf_sendq, retlen);
316    
317     /* We have some data written .. update counters */
318     to->localClient->send.bytes += retlen;
319     me.localClient->send.bytes += retlen;
320     } while (dbuf_length(&to->localClient->buf_sendq));
321    
322     if ((retlen < 0) && (ignoreErrno(errno)))
323     {
324     /* we have a non-fatal error, reschedule a write */
325     SetSendqBlocked(to);
326     comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE,
327     (PF *)sendq_unblocked, (void *)to, 0);
328     }
329     else if (retlen <= 0)
330     {
331     dead_link_on_write(to, errno);
332     return;
333     }
334     }
335     }
336    
337     /*
338     ** send_queued_slink_write
339     ** This is called when there is a chance the some output would
340     ** be possible. This attempts to empty the send queue as far as
341     ** possible, and then if any data is left, a write is rescheduled.
342     */
343     void
344     send_queued_slink_write(struct Client *to)
345     {
346     int retlen;
347    
348     /*
349     ** Once socket is marked dead, we cannot start writing to it,
350     ** even if the error is removed...
351     */
352     if (IsDead(to) || IsSlinkqBlocked(to))
353     return; /* no use calling send() now */
354    
355     /* Next, lets try to write some data */
356     if (to->localClient->slinkq != NULL)
357     {
358     retlen = send(to->localClient->ctrlfd.fd,
359     to->localClient->slinkq + to->localClient->slinkq_ofs,
360     to->localClient->slinkq_len, 0);
361     if (retlen < 0)
362     {
363     /* If we have a fatal error */
364     if (!ignoreErrno(errno))
365     {
366     dead_link_on_write(to, errno);
367     return;
368     }
369     }
370     else if (retlen == 0)
371     {
372     /* 0 bytes is an EOF .. */
373     dead_link_on_write(to, 0);
374     return;
375     }
376     else
377     {
378     execute_callback(iosendctrl_cb, to, retlen,
379     to->localClient->slinkq + to->localClient->slinkq_ofs);
380     to->localClient->slinkq_len -= retlen;
381    
382     assert(to->localClient->slinkq_len >= 0);
383     if (to->localClient->slinkq_len)
384     to->localClient->slinkq_ofs += retlen;
385     else
386     {
387     to->localClient->slinkq_ofs = 0;
388     MyFree(to->localClient->slinkq);
389     to->localClient->slinkq = NULL;
390     }
391     }
392    
393     /* Finally, if we have any more data, reschedule a write */
394     if (to->localClient->slinkq_len)
395     {
396     SetSlinkqBlocked(to);
397     comm_setselect(&to->localClient->ctrlfd, COMM_SELECT_WRITE,
398     (PF *)slinkq_unblocked, (void *)to, 0);
399     }
400     }
401     }
402    
403     /* send_queued_all()
404     *
405     * input - NONE
406     * output - NONE
407     * side effects - try to flush sendq of each client
408     */
409     void
410     send_queued_all(void)
411     {
412     dlink_node *ptr;
413    
414     /* Servers are processed first, mainly because this can generate
415     * a notice to opers, which is to be delivered by this function.
416     */
417     DLINK_FOREACH(ptr, serv_list.head)
418     send_queued_write((struct Client *) ptr->data);
419    
420     DLINK_FOREACH(ptr, unknown_list.head)
421     send_queued_write((struct Client *) ptr->data);
422    
423     DLINK_FOREACH(ptr, local_client_list.head)
424     send_queued_write((struct Client *) ptr->data);
425    
426     /* NOTE: This can still put clients on aborted_list; unfortunately,
427     * exit_aborted_clients takes precedence over send_queued_all,
428     * because exiting clients can generate server/oper traffic.
429     * The easiest approach is dealing with aborted clients in the next I/O lap.
430     * -adx
431     */
432     }
433    
434     /* sendto_one()
435     *
436     * inputs - pointer to destination client
437     * - var args message
438     * output - NONE
439     * side effects - send message to single client
440     */
441     void
442     sendto_one(struct Client *to, const char *pattern, ...)
443     {
444     va_list args;
445     char buffer[IRCD_BUFSIZE];
446     int len;
447    
448     if (to->from != NULL)
449     to = to->from;
450     if (IsDead(to))
451     return; /* This socket has already been marked as dead */
452    
453     va_start(args, pattern);
454     len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
455     va_end(args);
456    
457     send_message(to, buffer, len);
458     }
459    
460     /* sendto_channel_butone()
461     *
462     * inputs - pointer to client(server) to NOT send message to
463     * - pointer to client that is sending this message
464     * - pointer to channel being sent to
465     * - vargs message
466     * output - NONE
467     * side effects - message as given is sent to given channel members.
468     *
469     * WARNING - +D clients are ignored
470     */
471     void
472     sendto_channel_butone(struct Client *one, struct Client *from,
473     struct Channel *chptr, const char *command,
474     const char *pattern, ...)
475     {
476 adx 285 va_list alocal, aremote, auid;
477 adx 30 char local_buf[IRCD_BUFSIZE];
478     char remote_buf[IRCD_BUFSIZE];
479     char uid_buf[IRCD_BUFSIZE];
480     int local_len, remote_len, uid_len;
481     dlink_node *ptr;
482     dlink_node *ptr_next;
483     struct Client *target_p;
484    
485     if (IsServer(from))
486     local_len = ircsprintf(local_buf, ":%s %s %s ",
487     from->name, command, chptr->chname);
488     else
489     local_len = ircsprintf(local_buf, ":%s!%s@%s %s %s ",
490     from->name, from->username, from->host,
491     command, chptr->chname);
492     remote_len = ircsprintf(remote_buf, ":%s %s %s ",
493     from->name, command, chptr->chname);
494     uid_len = ircsprintf(uid_buf, ":%s %s %s ",
495     ID(from), command, chptr->chname);
496    
497 adx 285 va_start(alocal, pattern);
498     va_start(aremote, pattern);
499     va_start(auid, pattern);
500 adx 30 local_len += send_format(&local_buf[local_len], IRCD_BUFSIZE - local_len,
501 adx 285 pattern, alocal);
502 adx 30 remote_len += send_format(&remote_buf[remote_len], IRCD_BUFSIZE - remote_len,
503 adx 285 pattern, aremote);
504 adx 30 uid_len += send_format(&uid_buf[uid_len], IRCD_BUFSIZE - uid_len, pattern,
505 adx 285 auid);
506     va_end(auid);
507     va_end(aremote);
508     va_end(alocal);
509 adx 30
510     ++current_serial;
511    
512     DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->members.head)
513     {
514     target_p = ((struct Membership *)ptr->data)->client_p;
515     assert(target_p != NULL);
516    
517     if (IsDefunct(target_p) || IsDeaf(target_p) || target_p->from == one)
518     continue;
519    
520     if (MyClient(target_p))
521     {
522     if (target_p->serial != current_serial)
523     {
524     send_message(target_p, local_buf, local_len);
525     target_p->serial = current_serial;
526     }
527     }
528     else
529     {
530     /* Now check whether a message has been sent to this
531     * remote link already
532     */
533     if (target_p->from->serial != current_serial)
534     {
535     if (IsCapable(target_p->from, CAP_TS6))
536     send_message_remote(target_p->from, from, uid_buf, uid_len);
537     else
538     send_message_remote(target_p->from, from, remote_buf, remote_len);
539     target_p->from->serial = current_serial;
540     }
541     }
542     }
543     }
544    
545     /* sendto_server()
546     *
547     * inputs - pointer to client to NOT send to
548     * - pointer to source client required by LL (if any)
549     * - pointer to channel required by LL (if any)
550     * - caps or'd together which must ALL be present
551     * - caps or'd together which must ALL NOT be present
552     * - LL flags: LL_ICLIENT | LL_ICHAN
553     * - printf style format string
554     * - args to format string
555     * output - NONE
556     * side effects - Send a message to all connected servers, except the
557     * client 'one' (if non-NULL), as long as the servers
558     * support ALL capabs in 'caps', and NO capabs in 'nocaps'.
559     * If the server is a lazylink client, then it must know
560     * about source_p if non-NULL (unless LL_ICLIENT is specified,
561     * when source_p will be introduced where required) and
562     * chptr if non-NULL (unless LL_ICHANNEL is specified, when
563     * chptr will be introduced where required).
564     * Note: nothing will be introduced to a LazyLeaf unless
565     * the message is actually sent.
566     *
567     * This function was written in an attempt to merge together the other
568     * billion sendto_*serv*() functions, which sprung up with capabs,
569     * lazylinks, uids, etc.
570     * -davidt
571     */
572     void
573     sendto_server(struct Client *one, struct Client *source_p,
574     struct Channel *chptr, unsigned long caps,
575     unsigned long nocaps, unsigned long llflags,
576     const char *format, ...)
577     {
578     va_list args;
579     struct Client *client_p;
580     dlink_node *ptr;
581     char buffer[IRCD_BUFSIZE];
582     int len;
583    
584     if (chptr != NULL)
585     {
586     if (chptr->chname[0] != '#')
587     return;
588     }
589    
590     va_start(args, format);
591     len = send_format(buffer, IRCD_BUFSIZE, format, args);
592     va_end(args);
593    
594     DLINK_FOREACH(ptr, serv_list.head)
595     {
596     client_p = ptr->data;
597    
598     /* If dead already skip */
599     if (IsDead(client_p))
600     continue;
601     /* check against 'one' */
602     if (one != NULL && (client_p == one->from))
603     continue;
604     /* check we have required capabs */
605     if ((client_p->localClient->caps & caps) != caps)
606     continue;
607     /* check we don't have any forbidden capabs */
608     if ((client_p->localClient->caps & nocaps) != 0)
609     continue;
610    
611     if (ServerInfo.hub && IsCapable(client_p, CAP_LL))
612     {
613     /* check LL channel */
614     if (chptr != NULL &&
615     ((chptr->lazyLinkChannelExists &
616     client_p->localClient->serverMask) == 0))
617     {
618     /* Only introduce the channel if we really will send this message */
619     if (!(llflags & LL_ICLIENT) && source_p &&
620     ((source_p->lazyLinkClientExists &
621     client_p->localClient->serverMask) == 0))
622     continue; /* we can't introduce the unknown source_p, skip */
623    
624     if (llflags & LL_ICHAN)
625     burst_channel(client_p, chptr);
626     else
627     continue; /* we can't introduce the unknown chptr, skip */
628     }
629     /* check LL client */
630     if (source_p &&
631     ((source_p->lazyLinkClientExists &
632     client_p->localClient->serverMask) == 0))
633     {
634     if (llflags & LL_ICLIENT)
635     client_burst_if_needed(client_p,source_p);
636     else
637     continue; /* we can't introduce the unknown source_p, skip */
638     }
639     }
640     send_message(client_p, buffer, len);
641     }
642     }
643    
644     /* sendto_common_channels_local()
645     *
646     * inputs - pointer to client
647     * - pattern to send
648     * output - NONE
649     * side effects - Sends a message to all people on local server who are
650     * in same channel with user.
651     * used by m_nick.c and exit_one_client.
652     */
653     void
654     sendto_common_channels_local(struct Client *user, int touser,
655     const char *pattern, ...)
656     {
657     va_list args;
658     dlink_node *uptr;
659     dlink_node *cptr;
660     struct Channel *chptr;
661     struct Membership *ms;
662     struct Client *target_p;
663     char buffer[IRCD_BUFSIZE];
664     int len;
665    
666     va_start(args, pattern);
667     len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
668     va_end(args);
669    
670     ++current_serial;
671    
672     DLINK_FOREACH(cptr, user->channel.head)
673     {
674     chptr = ((struct Membership *) cptr->data)->chptr;
675     assert(chptr != NULL);
676    
677     DLINK_FOREACH(uptr, chptr->locmembers.head)
678     {
679     ms = uptr->data;
680     target_p = ms->client_p;
681     assert(target_p != NULL);
682    
683     if (target_p == user || IsDefunct(target_p) ||
684     target_p->serial == current_serial)
685     continue;
686    
687     target_p->serial = current_serial;
688     send_message(target_p, buffer, len);
689     }
690     }
691    
692     if (touser && MyConnect(user) && !IsDead(user) &&
693     user->serial != current_serial)
694     send_message(user, buffer, len);
695     }
696    
697     /* sendto_channel_local()
698     *
699     * inputs - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
700     * - whether to ignore +D clients (YES/NO)
701     * - pointer to channel to send to
702     * - var args pattern
703     * output - NONE
704     * side effects - Send a message to all members of a channel that are
705     * locally connected to this server.
706     */
707     void
708     sendto_channel_local(int type, int nodeaf, struct Channel *chptr,
709     const char *pattern, ...)
710     {
711     va_list args;
712     char buffer[IRCD_BUFSIZE];
713     int len;
714     dlink_node *ptr;
715     struct Membership *ms;
716     struct Client *target_p;
717    
718     va_start(args, pattern);
719     len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
720     va_end(args);
721    
722     DLINK_FOREACH(ptr, chptr->locmembers.head)
723     {
724     ms = ptr->data;
725     target_p = ms->client_p;
726    
727     if (type != 0 && (ms->flags & type) == 0)
728     continue;
729    
730     if (IsDefunct(target_p) || (nodeaf && IsDeaf(target_p)))
731     continue;
732    
733     send_message(target_p, buffer, len);
734     }
735     }
736    
737     /* sendto_channel_local_butone()
738     *
739     * inputs - pointer to client to NOT send message to
740     * - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
741     * - pointer to channel to send to
742     * - var args pattern
743     * output - NONE
744     * side effects - Send a message to all members of a channel that are
745     * locally connected to this server except one.
746     *
747     * WARNING - +D clients are omitted
748     */
749     void
750     sendto_channel_local_butone(struct Client *one, int type,
751     struct Channel *chptr, const char *pattern, ...)
752     {
753     va_list args;
754     char buffer[IRCD_BUFSIZE];
755     int len;
756     struct Client *target_p;
757     struct Membership *ms;
758     dlink_node *ptr;
759    
760     va_start(args, pattern);
761     len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
762     va_end(args);
763    
764     DLINK_FOREACH(ptr, chptr->locmembers.head)
765     {
766     ms = ptr->data;
767     target_p = ms->client_p;
768    
769     if (type != 0 && (ms->flags & type) == 0)
770     continue;
771    
772     if (target_p == one || IsDefunct(target_p) || IsDeaf(target_p))
773     continue;
774     send_message(target_p, buffer, len);
775     }
776     }
777    
778    
779     /* sendto_channel_remote()
780     *
781     * inputs - Client not to send towards
782     * - Client from whom message is from
783     * - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
784     * - pointer to channel to send to
785     * - var args pattern
786     * output - NONE
787     * side effects - Send a message to all members of a channel that are
788     * remote to this server.
789     */
790     void
791     sendto_channel_remote(struct Client *one, struct Client *from, int type, int caps,
792     int nocaps, struct Channel *chptr, const char *pattern, ...)
793     {
794     va_list args;
795     char buffer[IRCD_BUFSIZE];
796     int len;
797     dlink_node *ptr;
798     struct Client *target_p;
799     struct Membership *ms;
800    
801     va_start(args, pattern);
802     len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
803     va_end(args);
804    
805 db 189 ++current_serial;
806    
807 adx 30 DLINK_FOREACH(ptr, chptr->members.head)
808     {
809     ms = ptr->data;
810     target_p = ms->client_p;
811    
812     if (type != 0 && (ms->flags & type) == 0)
813     continue;
814    
815     if (MyConnect(target_p))
816     continue;
817     target_p = target_p->from;
818    
819     if (target_p == one->from ||
820     ((target_p->from->localClient->caps & caps) != caps) ||
821     ((target_p->from->localClient->caps & nocaps) != 0))
822     continue;
823 db 189 if (target_p->from->serial != current_serial)
824     {
825     send_message(target_p, buffer, len);
826     target_p->from->serial = current_serial;
827     }
828 adx 30 }
829     }
830    
831     /*
832     ** match_it() and sendto_match_butone() ARE only used
833     ** to send a msg to all ppl on servers/hosts that match a specified mask
834     ** (used for enhanced PRIVMSGs) for opers
835     **
836     ** addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
837     **
838     */
839    
840     /* match_it()
841     *
842     * inputs - client pointer to match on
843     * - actual mask to match
844     * - what to match on, HOST or SERVER
845     * output - 1 or 0 if match or not
846     * side effects - NONE
847     */
848     static int
849     match_it(const struct Client *one, const char *mask, int what)
850     {
851     if (what == MATCH_HOST)
852     return(match(mask, one->host));
853    
854     return(match(mask, one->servptr->name));
855     }
856    
857     /* sendto_match_butone()
858     *
859     * Send to all clients which match the mask in a way defined on 'what';
860     * either by user hostname or user servername.
861     *
862     * ugh. ONLY used by m_message.c to send an "oper magic" message. ugh.
863     */
864     void
865     sendto_match_butone(struct Client *one, struct Client *from, char *mask,
866     int what, const char *pattern, ...)
867     {
868 adx 285 va_list alocal, aremote;
869 adx 30 struct Client *client_p;
870     dlink_node *ptr, *ptr_next;
871     char local_buf[IRCD_BUFSIZE], remote_buf[IRCD_BUFSIZE];
872     int local_len = ircsprintf(local_buf, ":%s!%s@%s ", from->name,
873     from->username, from->host);
874     int remote_len = ircsprintf(remote_buf, ":%s ", from->name);
875    
876 adx 285 va_start(alocal, pattern);
877     va_start(aremote, pattern);
878 adx 30 local_len += send_format(&local_buf[local_len], IRCD_BUFSIZE - local_len,
879 adx 285 pattern, alocal);
880 adx 30 remote_len += send_format(&remote_buf[remote_len], IRCD_BUFSIZE - remote_len,
881 adx 285 pattern, aremote);
882     va_end(aremote);
883     va_end(alocal);
884 adx 30
885     /* scan the local clients */
886     DLINK_FOREACH(ptr, local_client_list.head)
887     {
888     client_p = ptr->data;
889    
890     if (client_p != one && !IsDefunct(client_p) &&
891     match_it(client_p, mask, what))
892     send_message(client_p, local_buf, local_len);
893     }
894    
895     /* Now scan servers */
896     DLINK_FOREACH_SAFE(ptr, ptr_next, serv_list.head)
897     {
898     client_p = ptr->data;
899    
900     /*
901     * The old code looped through every client on the
902     * network for each server to check if the
903     * server (client_p) has at least 1 client matching
904     * the mask, using something like:
905     *
906     * for (target_p = GlobalClientList; target_p; target_p = target_p->next)
907     * if (IsRegisteredUser(target_p) &&
908     * match_it(target_p, mask, what) &&
909     * (target_p->from == client_p))
910     * vsendto_prefix_one(client_p, from, pattern, args);
911     *
912     * That way, we wouldn't send the message to
913     * a server who didn't have a matching client.
914     * However, on a network such as EFNet, that
915     * code would have looped through about 50
916     * servers, and in each loop, loop through
917     * about 50k clients as well, calling match()
918     * in each nested loop. That is a very bad
919     * thing cpu wise - just send the message
920     * to every connected server and let that
921     * server deal with it.
922     * -wnder
923     */
924     if (client_p != one && !IsDefunct(client_p))
925     send_message_remote(client_p, from, remote_buf, remote_len);
926     }
927     }
928    
929     /* sendto_match_servs()
930     *
931     * inputs - source client
932     * - mask to send to
933     * - capab needed
934     * - data
935     * outputs - none
936     * side effects - data sent to servers matching with capab
937     */
938     void
939     sendto_match_servs(struct Client *source_p, const char *mask, int cap,
940     const char *pattern, ...)
941     {
942     va_list args;
943     struct Client *target_p;
944     dlink_node *ptr;
945     char buffer[IRCD_BUFSIZE];
946     int found = 0;
947    
948     va_start(args, pattern);
949     vsnprintf(buffer, sizeof(buffer), pattern, args);
950     va_end(args);
951    
952     current_serial++;
953    
954     DLINK_FOREACH(ptr, global_serv_list.head)
955     {
956     target_p = ptr->data;
957    
958     /* Do not attempt to send to ourselves, or the source */
959     if (IsMe(target_p) || target_p->from == source_p->from)
960     continue;
961    
962     if (target_p->from->serial == current_serial)
963     continue;
964    
965     if (match(mask, target_p->name))
966     {
967     /*
968     * if we set the serial here, then we'll never do a
969     * match() again, if !IsCapable()
970     */
971     target_p->from->serial = current_serial;
972     found++;
973    
974     if (!IsCapable(target_p->from, cap))
975     continue;
976    
977     sendto_anywhere(target_p, source_p, "%s", buffer);
978     }
979     }
980     }
981    
982     /* sendto_anywhere()
983     *
984     * inputs - pointer to dest client
985     * - pointer to from client
986     * - varags
987     * output - NONE
988     * side effects - less efficient than sendto_remote and sendto_one
989     * but useful when one does not know where target "lives"
990     */
991     void
992     sendto_anywhere(struct Client *to, struct Client *from,
993     const char *pattern, ...)
994     {
995     va_list args;
996     char buffer[IRCD_BUFSIZE];
997     int len;
998     struct Client *send_to = (to->from != NULL ? to->from : to);
999    
1000     if (IsDead(send_to))
1001     return;
1002    
1003     if (MyClient(to))
1004     {
1005     if (IsServer(from))
1006     {
1007     if (IsCapable(to, CAP_TS6) && HasID(from))
1008     len = ircsprintf(buffer, ":%s ", from->id);
1009     else
1010     len = ircsprintf(buffer, ":%s ", from->name);
1011     }
1012     else
1013     len = ircsprintf(buffer, ":%s!%s@%s ",
1014     from->name, from->username, from->host);
1015     }
1016     else len = ircsprintf(buffer, ":%s ", ID_or_name(from, send_to));
1017    
1018     va_start(args, pattern);
1019     len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
1020     va_end(args);
1021    
1022     if(MyClient(to))
1023     send_message(send_to, buffer, len);
1024     else
1025     send_message_remote(send_to, from, buffer, len);
1026     }
1027    
1028     /* sendto_realops_flags()
1029     *
1030     * inputs - flag types of messages to show to real opers
1031     * - flag indicating opers/admins
1032     * - var args input message
1033     * output - NONE
1034     * side effects - Send to *local* ops only but NOT +s nonopers.
1035     */
1036     void
1037     sendto_realops_flags(unsigned int flags, int level, const char *pattern, ...)
1038     {
1039     struct Client *client_p;
1040     char nbuf[IRCD_BUFSIZE];
1041     dlink_node *ptr;
1042     va_list args;
1043    
1044     va_start(args, pattern);
1045     vsnprintf(nbuf, IRCD_BUFSIZE, pattern, args);
1046     va_end(args);
1047    
1048     DLINK_FOREACH(ptr, oper_list.head)
1049     {
1050     client_p = ptr->data;
1051     assert(client_p->umodes & UMODE_OPER);
1052    
1053     /* If we're sending it to opers and theyre an admin, skip.
1054     * If we're sending it to admins, and theyre not, skip.
1055     */
1056     if (((level == L_ADMIN) && !IsAdmin(client_p)) ||
1057     ((level == L_OPER) && IsAdmin(client_p)))
1058     continue;
1059    
1060     if (client_p->umodes & flags)
1061     sendto_one(client_p, ":%s NOTICE %s :*** Notice -- %s",
1062     me.name, client_p->name, nbuf);
1063     }
1064     }
1065    
1066     /* sendto_wallops_flags()
1067     *
1068     * inputs - flag types of messages to show to real opers
1069     * - client sending request
1070     * - var args input message
1071     * output - NONE
1072     * side effects - Send a wallops to local opers
1073     */
1074     void
1075     sendto_wallops_flags(unsigned int flags, struct Client *source_p,
1076     const char *pattern, ...)
1077     {
1078     struct Client *client_p;
1079     dlink_node *ptr;
1080     va_list args;
1081     char buffer[IRCD_BUFSIZE];
1082     int len;
1083    
1084     if (IsClient(source_p))
1085     len = ircsprintf(buffer, ":%s!%s@%s WALLOPS :",
1086     source_p->name, source_p->username, source_p->host);
1087     else
1088     len = ircsprintf(buffer, ":%s WALLOPS :", source_p->name);
1089    
1090     va_start(args, pattern);
1091     len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
1092     va_end(args);
1093    
1094     DLINK_FOREACH(ptr, oper_list.head)
1095     {
1096     client_p = ptr->data;
1097     assert(client_p->umodes & UMODE_OPER);
1098    
1099     if ((client_p->umodes & flags) && !IsDefunct(client_p))
1100     send_message(client_p, buffer, len);
1101     }
1102     }
1103    
1104     /* ts_warn()
1105     *
1106     * inputs - var args message
1107     * output - NONE
1108     * side effects - Call sendto_realops_flags, with some flood checking
1109     * (at most 5 warnings every 5 seconds)
1110     */
1111     void
1112     ts_warn(const char *pattern, ...)
1113     {
1114     va_list args;
1115     char buffer[LOG_BUFSIZE];
1116     static time_t last = 0;
1117     static int warnings = 0;
1118    
1119     /*
1120     ** if we're running with TS_WARNINGS enabled and someone does
1121     ** something silly like (remotely) connecting a nonTS server,
1122     ** we'll get a ton of warnings, so we make sure we don't send
1123     ** more than 5 every 5 seconds. -orabidoo
1124     */
1125    
1126     if (CurrentTime - last < 5)
1127     {
1128     if (++warnings > 5)
1129     return;
1130     }
1131     else
1132     {
1133     last = CurrentTime;
1134     warnings = 0;
1135     }
1136    
1137     va_start(args, pattern);
1138     vsprintf_irc(buffer, pattern, args);
1139     va_end(args);
1140    
1141     sendto_realops_flags(UMODE_ALL, L_ALL, "%s", buffer);
1142     ilog(L_CRIT, "%s", buffer);
1143     }
1144    
1145     /* kill_client()
1146     *
1147     * inputs - client to send kill towards
1148     * - pointer to client to kill
1149     * - reason for kill
1150     * output - NONE
1151     * side effects - NONE
1152     */
1153     void
1154     kill_client(struct Client *client_p, struct Client *diedie,
1155     const char *pattern, ...)
1156     {
1157     va_list args;
1158     char buffer[IRCD_BUFSIZE];
1159     int len;
1160    
1161     if (client_p->from != NULL)
1162     client_p = client_p->from;
1163     if (IsDead(client_p))
1164     return;
1165    
1166     len = ircsprintf(buffer, ":%s KILL %s :", ID_or_name(&me, client_p->from),
1167     ID_or_name(diedie, client_p));
1168    
1169     va_start(args, pattern);
1170     len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
1171     va_end(args);
1172    
1173     send_message(client_p, buffer, len);
1174     }
1175    
1176     /* kill_client_ll_serv_butone()
1177     *
1178     * inputs - pointer to client to not send to
1179     * - pointer to client to kill
1180     * output - NONE
1181     * side effects - Send a KILL for the given client
1182     * message to all connected servers
1183     * except the client 'one'. Also deal with
1184     * client being unknown to leaf, as in lazylink...
1185     */
1186     void
1187     kill_client_ll_serv_butone(struct Client *one, struct Client *source_p,
1188     const char *pattern, ...)
1189     {
1190     va_list args;
1191     int have_uid = 0;
1192     struct Client *client_p;
1193     dlink_node *ptr;
1194     char buf_uid[IRCD_BUFSIZE], buf_nick[IRCD_BUFSIZE];
1195     int len_uid = 0, len_nick;
1196    
1197     if (HasID(source_p) && (me.id[0] != '\0'))
1198     {
1199     have_uid = 1;
1200 adx 285 va_start(args, pattern);
1201 adx 30 len_uid = ircsprintf(buf_uid, ":%s KILL %s :", me.id, ID(source_p));
1202     len_uid += send_format(&buf_uid[len_uid], IRCD_BUFSIZE - len_uid, pattern,
1203     args);
1204 adx 285 va_end(args);
1205 adx 30 }
1206 adx 285
1207     va_start(args, pattern);
1208 adx 30 len_nick = ircsprintf(buf_nick, ":%s KILL %s :", me.name, source_p->name);
1209     len_nick += send_format(&buf_nick[len_nick], IRCD_BUFSIZE - len_nick, pattern,
1210     args);
1211     va_end(args);
1212    
1213     DLINK_FOREACH(ptr, serv_list.head)
1214     {
1215     client_p = ptr->data;
1216    
1217     if (one != NULL && (client_p == one->from))
1218     continue;
1219     if (IsDefunct(client_p))
1220     continue;
1221    
1222     /* XXX perhaps IsCapable should test for localClient itself ? -db */
1223     if (client_p->localClient == NULL || !IsCapable(client_p, CAP_LL) ||
1224     !ServerInfo.hub ||
1225     (source_p->lazyLinkClientExists & client_p->localClient->serverMask))
1226     {
1227     if (have_uid && IsCapable(client_p, CAP_TS6))
1228     send_message(client_p, buf_uid, len_uid);
1229     else
1230     send_message(client_p, buf_nick, len_nick);
1231     }
1232     }
1233     }

Properties

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