ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
Revision: 2654
Committed: Wed Dec 11 17:45:34 2013 UTC (11 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 30285 byte(s)
Log Message:
- send.c:send_queued_write(): removed pointless cast

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

Properties

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