ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
Revision: 1001
Committed: Sat Aug 29 22:44:44 2009 UTC (16 years ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/send.c
File size: 32094 byte(s)
Log Message:
- remove half done and broken win32 support

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

Properties

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