ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/send.c
Revision: 187
Committed: Sun Oct 23 20:30:07 2005 UTC (20 years, 11 months ago) by michael
Content type: text/x-csrc
File size: 33863 byte(s)
Log Message:
- Fixed bug in sendto_channel_remote() that would cause
  multiple copies of the same message to be sent to the
  uplinked server.
  That one needs propably to be backported to 7.1.4 and 7.2

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

Properties

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