ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
Revision: 572
Committed: Sun Apr 30 16:57:48 2006 UTC (19 years, 4 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/send.c
File size: 34370 byte(s)
Log Message:
- Backported changes made in HEAD to get rid of Channel::locmembers.
  This is mainly to save about 5megs of ram on networks like efnet where
  we have about 600k allocated Membership structures.

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

Properties

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