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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1011 - (show annotations)
Fri Sep 18 10:14:09 2009 UTC (11 years, 5 months ago) by michael
File MIME type: text/x-chdr
File size: 32075 byte(s)
- move list manipulation routines from tools.c to list.c
- mem_frob() goes to memory.c
- sort out redundant/unneeded header includes

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

Properties

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

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