ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
Revision: 2888
Committed: Tue Jan 21 17:47:11 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 30719 byte(s)
Log Message:
- Fixed improper use of the ID_or_name macro in several places

File Contents

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

Properties

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