ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/send.c
Revision: 32
Committed: Sun Oct 2 20:41:23 2005 UTC (18 years, 5 months ago) by knight
Content type: text/x-csrc
Original Path: ircd-hybrid/src/send.c
File size: 33727 byte(s)
Log Message:
- svn:keywords

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

Properties

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