/[svn]/ircd-hybrid/src/send.c
ViewVC logotype

Contents of /ircd-hybrid/src/send.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 628 - (show annotations)
Mon May 29 20:04:32 2006 UTC (16 years, 6 months ago) by adx
File MIME type: text/x-chdr
File size: 32214 byte(s)
Added contrib module for character set recoding

Note it doesn't work at the moment because listen{} itself
is not yet modularised.  Will probably also need some modifications
to include iconv headers and libraries in the build process.

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

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28