ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
Revision: 2867
Committed: Sun Jan 19 16:58:07 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 31039 byte(s)
Log Message:
- send.c, send.h: made some things use an enum

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

Properties

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