ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/send.c
Revision: 178
Committed: Sat Oct 22 10:02:43 2005 UTC (20 years, 9 months ago) by adx
Content type: text/x-csrc
File size: 33732 byte(s)
Log Message:
- somehow it didn't get committed, fixing

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

Properties

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