/[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 885 - (show annotations)
Wed Oct 31 18:09:24 2007 UTC (12 years, 1 month ago) by michael
File MIME type: text/x-chdr
File size: 32259 byte(s)
- Removed LazyLinks in 7.2 to stop people from asking why we keep
  broken code for half a decade. LL will be implemented in a smarter
  fashion in due time

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

Properties

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

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