ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/send.c
Revision: 1243
Committed: Fri Sep 30 10:47:53 2011 UTC (13 years, 11 months ago) by michael
Content type: text/x-csrc
File size: 32913 byte(s)
Log Message:
- move content of msg.h, ircd_handler.h and handlers.h into parse.h and
  remove headers accordingly
- killed common.h
- remove m_killhost.c and m_flags.c from contrib/
- sort out unused header includes here and there

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

Properties

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