]> jfr.im git - irc/rqf/shadowircd.git/blame - src/send.c
macro replacement
[irc/rqf/shadowircd.git] / src / send.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * send.c: Functions for sending messages.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
8aba962d 24 * $Id: send.c 3520 2007-06-30 22:15:35Z jilles $
212380e3 25 */
26
27#include "stdinc.h"
28#include "tools.h"
29#include "send.h"
30#include "channel.h"
31#include "class.h"
32#include "client.h"
33#include "common.h"
34#include "irc_string.h"
35#include "ircd.h"
36#include "numeric.h"
37#include "commio.h"
38#include "s_serv.h"
39#include "sprintf_irc.h"
40#include "s_conf.h"
41#include "s_newconf.h"
42#include "linebuf.h"
43#include "s_log.h"
44#include "memory.h"
45#include "hook.h"
8aba962d 46#include "monitor.h"
212380e3 47
48#define LOG_BUFSIZE 2048
49
50/* send the message to the link the target is attached to */
51#define send_linebuf(a,b) _send_linebuf((a->from ? a->from : a) ,b)
52
53unsigned long current_serial = 0L;
54
55/* send_linebuf()
56 *
57 * inputs - client to send to, linebuf to attach
58 * outputs -
59 * side effects - linebuf is attached to client
60 */
61static int
62_send_linebuf(struct Client *to, buf_head_t *linebuf)
63{
64 if(IsMe(to))
65 {
66 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send message to myself!");
67 return 0;
68 }
69
70 if(!MyConnect(to) || IsIOError(to))
71 return 0;
72
73 if(linebuf_len(&to->localClient->buf_sendq) > get_sendq(to))
74 {
75 if(IsServer(to))
76 {
77 sendto_realops_snomask(SNO_GENERAL, L_ALL,
78 "Max SendQ limit exceeded for %s: %u > %lu",
79 get_server_name(to, HIDE_IP),
80 linebuf_len(&to->localClient->buf_sendq),
81 get_sendq(to));
82
83 ilog(L_SERVER, "Max SendQ limit exceeded for %s: %u > %lu",
84 log_client_name(to, SHOW_IP),
85 linebuf_len(&to->localClient->buf_sendq),
86 get_sendq(to));
87 }
88
89 if(IsClient(to))
90 to->flags |= FLAGS_SENDQEX;
91
92 dead_link(to);
93 return -1;
94 }
95 else
96 {
97 /* just attach the linebuf to the sendq instead of
98 * generating a new one
99 */
100 linebuf_attach(&to->localClient->buf_sendq, linebuf);
101 }
102
103 /*
104 ** Update statistics. The following is slightly incorrect
105 ** because it counts messages even if queued, but bytes
106 ** only really sent. Queued bytes get updated in SendQueued.
107 */
108 to->localClient->sendM += 1;
109 me.localClient->sendM += 1;
110 if(linebuf_len(&to->localClient->buf_sendq) > 0)
de057244 111 send_queued_write(to->localClient->F->fd, to);
212380e3 112 return 0;
113}
114
115/* send_linebuf_remote()
116 *
117 * inputs - client to attach to, sender, linebuf
118 * outputs -
119 * side effects - client has linebuf attached
120 */
121static void
122send_linebuf_remote(struct Client *to, struct Client *from, buf_head_t *linebuf)
123{
124 if(to->from)
125 to = to->from;
126
2386ae04 127 /* we assume the caller has already tested for fake direction */
212380e3 128
129 _send_linebuf(to, linebuf);
130 return;
131}
132
133/* send_queued_write()
134 *
135 * inputs - fd to have queue sent, client we're sending to
136 * outputs - contents of queue
137 * side effects - write is rescheduled if queue isnt emptied
138 */
139void
140send_queued_write(int fd, void *data)
141{
142 struct Client *to = data;
143 int retlen;
144 int flags;
145#ifdef USE_IODEBUG_HOOKS
146 hook_data_int hd;
147#endif
38e6acdd 148 fde_t *F = rb_locate_fd(to->localClient->F->fd);
5cd74a3b
WP
149 if (!F)
150 return;
151
212380e3 152 /* cant write anything to a dead socket. */
153 if(IsIOError(to))
154 return;
155
156#ifdef USE_IODEBUG_HOOKS
157 hd.client = to;
158 if(to->localClient->buf_sendq.list.head)
159 hd.arg1 = ((buf_line_t *) to->localClient->buf_sendq.list.head->data)->buf +
160 to->localClient->buf_sendq.writeofs;
161#endif
162
163 if(linebuf_len(&to->localClient->buf_sendq))
164 {
165 while ((retlen =
5cd74a3b 166 linebuf_flush(F, &to->localClient->buf_sendq)) > 0)
212380e3 167 {
168 /* We have some data written .. update counters */
169#ifdef USE_IODEBUG_HOOKS
170 hd.arg2 = retlen;
171 call_hook(h_iosend_id, &hd);
172
173 if(to->localClient->buf_sendq.list.head)
174 hd.arg1 =
175 ((buf_line_t *) to->localClient->buf_sendq.list.head->
176 data)->buf + to->localClient->buf_sendq.writeofs;
177#endif
178
179
180 to->localClient->sendB += retlen;
181 me.localClient->sendB += retlen;
182 if(to->localClient->sendB > 1023)
183 {
184 to->localClient->sendK += (to->localClient->sendB >> 10);
185 to->localClient->sendB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
186 }
187 else if(me.localClient->sendB > 1023)
188 {
189 me.localClient->sendK += (me.localClient->sendB >> 10);
190 me.localClient->sendB &= 0x03ff;
191 }
192 }
193
194 if(retlen == 0 || (retlen < 0 && !ignoreErrno(errno)))
195 {
196 dead_link(to);
197 return;
198 }
199 }
200 if(ignoreErrno(errno))
201 flags = COMM_SELECT_WRITE|COMM_SELECT_RETRY;
202 else
203 flags = COMM_SELECT_WRITE;
204 if(linebuf_len(&to->localClient->buf_sendq))
38e6acdd 205 rb_setselect(fd, FDLIST_IDLECLIENT, flags,
212380e3 206 send_queued_write, to, 0);
207}
208
209/* send_queued_slink_write()
210 *
211 * inputs - fd to have queue sent, client we're sending to
212 * outputs - contents of queue
213 * side effects - write is rescheduled if queue isnt emptied
214 */
215void
216send_queued_slink_write(int fd, void *data)
217{
218 struct Client *to = data;
219 int retlen;
220
221 /*
222 ** Once socket is marked dead, we cannot start writing to it,
223 ** even if the error is removed...
224 */
225 if(IsIOError(to))
226 return;
227
228 /* Next, lets try to write some data */
229 if(to->localClient->slinkq)
230 {
231 retlen = write(to->localClient->ctrlfd,
232 to->localClient->slinkq + to->localClient->slinkq_ofs,
233 to->localClient->slinkq_len);
234
235 if(retlen < 0)
236 {
237 /* If we have a fatal error */
238 if(!ignoreErrno(errno))
239 {
240 dead_link(to);
241 return;
242 }
243 }
244 /* 0 bytes is an EOF .. */
245 else if(retlen == 0)
246 {
247 dead_link(to);
248 return;
249 }
250 else
251 {
252 to->localClient->slinkq_len -= retlen;
253
254 s_assert(to->localClient->slinkq_len >= 0);
255 if(to->localClient->slinkq_len)
256 to->localClient->slinkq_ofs += retlen;
257 else
258 {
259 to->localClient->slinkq_ofs = 0;
260 MyFree(to->localClient->slinkq);
261 to->localClient->slinkq = NULL;
262 }
263 }
264 }
265
266 /* if we have any more data, reschedule a write */
267 if(to->localClient->slinkq_len)
38e6acdd 268 rb_setselect(to->localClient->ctrlfd, FDLIST_IDLECLIENT,
212380e3 269 COMM_SELECT_WRITE|COMM_SELECT_RETRY, send_queued_slink_write, to, 0);
270}
271
272/* sendto_one()
273 *
274 * inputs - client to send to, va_args
275 * outputs - client has message put into its queue
276 * side effects -
277 */
278void
279sendto_one(struct Client *target_p, const char *pattern, ...)
280{
281 va_list args;
282 buf_head_t linebuf;
283
284 /* send remote if to->from non NULL */
285 if(target_p->from != NULL)
286 target_p = target_p->from;
287
288 if(IsIOError(target_p))
289 return;
290
291 linebuf_newbuf(&linebuf);
292
293 va_start(args, pattern);
294 linebuf_putmsg(&linebuf, pattern, &args, NULL);
295 va_end(args);
296
297 _send_linebuf(target_p, &linebuf);
298
299 linebuf_donebuf(&linebuf);
300
301}
302
303/* sendto_one_prefix()
304 *
305 * inputs - client to send to, va_args
306 * outputs - client has message put into its queue
307 * side effects - source(us)/target is chosen based on TS6 capability
308 */
309void
310sendto_one_prefix(struct Client *target_p, struct Client *source_p,
311 const char *command, const char *pattern, ...)
312{
313 struct Client *dest_p;
314 va_list args;
315 buf_head_t linebuf;
316
317 /* send remote if to->from non NULL */
318 if(target_p->from != NULL)
319 dest_p = target_p->from;
320 else
321 dest_p = target_p;
322
323 if(IsIOError(dest_p))
324 return;
325
326 if(IsMe(dest_p))
327 {
328 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!");
329 return;
330 }
331
332 linebuf_newbuf(&linebuf);
333 va_start(args, pattern);
334 linebuf_putmsg(&linebuf, pattern, &args,
335 ":%s %s %s ",
336 get_id(source_p, target_p),
337 command, get_id(target_p, target_p));
338 va_end(args);
339
340 _send_linebuf(dest_p, &linebuf);
341 linebuf_donebuf(&linebuf);
342}
343
344/* sendto_one_notice()
345 *
346 * inputs - client to send to, va_args
347 * outputs - client has a NOTICE put into its queue
348 * side effects - source(us)/target is chosen based on TS6 capability
349 */
350void
351sendto_one_notice(struct Client *target_p, const char *pattern, ...)
352{
353 struct Client *dest_p;
354 va_list args;
355 buf_head_t linebuf;
5366977b 356 char *to;
212380e3 357
358 /* send remote if to->from non NULL */
359 if(target_p->from != NULL)
360 dest_p = target_p->from;
361 else
362 dest_p = target_p;
363
364 if(IsIOError(dest_p))
365 return;
366
367 if(IsMe(dest_p))
368 {
369 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!");
370 return;
371 }
372
373 linebuf_newbuf(&linebuf);
374 va_start(args, pattern);
375 linebuf_putmsg(&linebuf, pattern, &args,
376 ":%s NOTICE %s ",
5366977b 377 get_id(&me, target_p), *(to = get_id(target_p, target_p)) != '\0' ? to : "*");
212380e3 378 va_end(args);
379
380 _send_linebuf(dest_p, &linebuf);
381 linebuf_donebuf(&linebuf);
382}
383
384
385/* sendto_one_numeric()
386 *
387 * inputs - client to send to, va_args
388 * outputs - client has message put into its queue
389 * side effects - source/target is chosen based on TS6 capability
390 */
391void
392sendto_one_numeric(struct Client *target_p, int numeric, const char *pattern, ...)
393{
394 struct Client *dest_p;
395 va_list args;
396 buf_head_t linebuf;
5366977b 397 char *to;
212380e3 398
399 /* send remote if to->from non NULL */
400 if(target_p->from != NULL)
401 dest_p = target_p->from;
402 else
403 dest_p = target_p;
404
405 if(IsIOError(dest_p))
406 return;
407
408 if(IsMe(dest_p))
409 {
410 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!");
411 return;
412 }
413
414 linebuf_newbuf(&linebuf);
415 va_start(args, pattern);
416 linebuf_putmsg(&linebuf, pattern, &args,
417 ":%s %03d %s ",
418 get_id(&me, target_p),
5366977b 419 numeric, *(to = get_id(target_p, target_p)) != '\0' ? to : "*");
212380e3 420 va_end(args);
421
422 _send_linebuf(dest_p, &linebuf);
423 linebuf_donebuf(&linebuf);
424}
425
426/*
427 * sendto_server
428 *
429 * inputs - pointer to client to NOT send to
430 * - caps or'd together which must ALL be present
431 * - caps or'd together which must ALL NOT be present
432 * - printf style format string
433 * - args to format string
434 * output - NONE
435 * side effects - Send a message to all connected servers, except the
436 * client 'one' (if non-NULL), as long as the servers
437 * support ALL capabs in 'caps', and NO capabs in 'nocaps'.
438 *
439 * This function was written in an attempt to merge together the other
440 * billion sendto_*serv*() functions, which sprung up with capabs, uids etc
441 * -davidt
442 */
443void
444sendto_server(struct Client *one, struct Channel *chptr, unsigned long caps,
445 unsigned long nocaps, const char *format, ...)
446{
447 va_list args;
448 struct Client *target_p;
af81d5a0
WP
449 rb_dlink_node *ptr;
450 rb_dlink_node *next_ptr;
212380e3 451 buf_head_t linebuf;
452
453 /* noone to send to.. */
af81d5a0 454 if(rb_dlink_list_length(&serv_list) == 0)
212380e3 455 return;
456
457 if(chptr != NULL && *chptr->chname != '#')
458 return;
459
460 linebuf_newbuf(&linebuf);
461 va_start(args, format);
462 linebuf_putmsg(&linebuf, format, &args, NULL);
463 va_end(args);
464
8e69bb4e 465 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, serv_list.head)
212380e3 466 {
467 target_p = ptr->data;
468
469 /* check against 'one' */
470 if(one != NULL && (target_p == one->from))
471 continue;
472
473 /* check we have required capabs */
474 if(!IsCapable(target_p, caps))
475 continue;
476
477 /* check we don't have any forbidden capabs */
478 if(!NotCapable(target_p, nocaps))
479 continue;
480
481 _send_linebuf(target_p, &linebuf);
482 }
483
484 linebuf_donebuf(&linebuf);
485
486}
487
488/* sendto_channel_flags()
489 *
490 * inputs - server not to send to, flags needed, source, channel, va_args
491 * outputs - message is sent to channel members
492 * side effects -
493 */
494void
495sendto_channel_flags(struct Client *one, int type, struct Client *source_p,
496 struct Channel *chptr, const char *pattern, ...)
497{
498 static char buf[BUFSIZE];
499 va_list args;
500 buf_head_t linebuf_local;
501 buf_head_t linebuf_name;
502 buf_head_t linebuf_id;
503 struct Client *target_p;
504 struct membership *msptr;
af81d5a0
WP
505 rb_dlink_node *ptr;
506 rb_dlink_node *next_ptr;
212380e3 507
508 linebuf_newbuf(&linebuf_local);
509 linebuf_newbuf(&linebuf_name);
510 linebuf_newbuf(&linebuf_id);
511
512 current_serial++;
513
514 va_start(args, pattern);
515 ircvsnprintf(buf, sizeof(buf), pattern, args);
516 va_end(args);
517
518 if(IsServer(source_p))
519 linebuf_putmsg(&linebuf_local, NULL, NULL,
520 ":%s %s", source_p->name, buf);
521 else
522 linebuf_putmsg(&linebuf_local, NULL, NULL,
523 ":%s!%s@%s %s",
524 source_p->name, source_p->username,
525 source_p->host, buf);
526
527 linebuf_putmsg(&linebuf_name, NULL, NULL, ":%s %s", source_p->name, buf);
528 linebuf_putmsg(&linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);
529
8e69bb4e 530 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->members.head)
212380e3 531 {
532 msptr = ptr->data;
533 target_p = msptr->client_p;
534
535 if(IsIOError(target_p->from) || target_p->from == one)
536 continue;
537
538 if(type && ((msptr->flags & type) == 0))
539 continue;
540
541 if(IsDeaf(target_p))
542 continue;
543
544 if(!MyClient(target_p))
545 {
546 /* if we've got a specific type, target must support
547 * CHW.. --fl
548 */
549 if(type && NotCapable(target_p->from, CAP_CHW))
550 continue;
551
552 if(target_p->from->serial != current_serial)
553 {
554 if(has_id(target_p->from))
555 send_linebuf_remote(target_p, source_p, &linebuf_id);
556 else
557 send_linebuf_remote(target_p, source_p, &linebuf_name);
558
559 target_p->from->serial = current_serial;
560 }
561 }
562 else
563 _send_linebuf(target_p, &linebuf_local);
564 }
565
566 linebuf_donebuf(&linebuf_local);
567 linebuf_donebuf(&linebuf_name);
568 linebuf_donebuf(&linebuf_id);
569}
570
571
572/* sendto_channel_local()
573 *
574 * inputs - flags to send to, channel to send to, va_args
575 * outputs - message to local channel members
576 * side effects -
577 */
578void
579sendto_channel_local(int type, struct Channel *chptr, const char *pattern, ...)
580{
581 va_list args;
582 buf_head_t linebuf;
583 struct membership *msptr;
584 struct Client *target_p;
af81d5a0
WP
585 rb_dlink_node *ptr;
586 rb_dlink_node *next_ptr;
212380e3 587
588 linebuf_newbuf(&linebuf);
589
590 va_start(args, pattern);
591 linebuf_putmsg(&linebuf, pattern, &args, NULL);
592 va_end(args);
593
8e69bb4e 594 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
212380e3 595 {
596 msptr = ptr->data;
597 target_p = msptr->client_p;
598
599 if(IsIOError(target_p))
600 continue;
601
602 if(type && ((msptr->flags & type) == 0))
603 continue;
604
605 _send_linebuf(target_p, &linebuf);
606 }
607
608 linebuf_donebuf(&linebuf);
609}
610
611/* sendto_channel_local_butone()
612 *
613 * inputs - flags to send to, channel to send to, va_args
614 * - user to ignore when sending
615 * outputs - message to local channel members
616 * side effects -
617 */
618void
619sendto_channel_local_butone(struct Client *one, int type, struct Channel *chptr, const char *pattern, ...)
620{
621 va_list args;
622 buf_head_t linebuf;
623 struct membership *msptr;
624 struct Client *target_p;
af81d5a0
WP
625 rb_dlink_node *ptr;
626 rb_dlink_node *next_ptr;
212380e3 627
628 linebuf_newbuf(&linebuf);
629
630 va_start(args, pattern);
631 linebuf_putmsg(&linebuf, pattern, &args, NULL);
632 va_end(args);
633
8e69bb4e 634 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
212380e3 635 {
636 msptr = ptr->data;
637 target_p = msptr->client_p;
638
639 if(target_p == one)
640 continue;
641
642 if(IsIOError(target_p))
643 continue;
644
645 if(type && ((msptr->flags & type) == 0))
646 continue;
647
648 _send_linebuf(target_p, &linebuf);
649 }
650
651 linebuf_donebuf(&linebuf);
652}
653
654/*
655 * sendto_common_channels_local()
656 *
657 * inputs - pointer to client
658 * - pattern to send
659 * output - NONE
660 * side effects - Sends a message to all people on local server who are
661 * in same channel with user.
662 * used by m_nick.c and exit_one_client.
663 */
664void
665sendto_common_channels_local(struct Client *user, const char *pattern, ...)
666{
667 va_list args;
af81d5a0
WP
668 rb_dlink_node *ptr;
669 rb_dlink_node *next_ptr;
670 rb_dlink_node *uptr;
671 rb_dlink_node *next_uptr;
212380e3 672 struct Channel *chptr;
673 struct Client *target_p;
674 struct membership *msptr;
675 struct membership *mscptr;
676 buf_head_t linebuf;
677
678 linebuf_newbuf(&linebuf);
679 va_start(args, pattern);
680 linebuf_putmsg(&linebuf, pattern, &args, NULL);
681 va_end(args);
682
683 ++current_serial;
684
8e69bb4e 685 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head)
212380e3 686 {
687 mscptr = ptr->data;
688 chptr = mscptr->chptr;
689
8e69bb4e 690 RB_DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head)
212380e3 691 {
692 msptr = uptr->data;
693 target_p = msptr->client_p;
694
695 if(IsIOError(target_p) ||
696 target_p->serial == current_serial)
697 continue;
698
699 target_p->serial = current_serial;
700 send_linebuf(target_p, &linebuf);
701 }
702 }
703
704 /* this can happen when the user isnt in any channels, but we still
705 * need to send them the data, ie a nick change
706 */
707 if(MyConnect(user) && (user->serial != current_serial))
708 send_linebuf(user, &linebuf);
709
710 linebuf_donebuf(&linebuf);
711}
712
713/*
714 * sendto_common_channels_local_butone()
715 *
716 * inputs - pointer to client
717 * - pattern to send
718 * output - NONE
719 * side effects - Sends a message to all people on local server who are
720 * in same channel with user, except for user itself.
721 */
722void
723sendto_common_channels_local_butone(struct Client *user, const char *pattern, ...)
724{
725 va_list args;
af81d5a0
WP
726 rb_dlink_node *ptr;
727 rb_dlink_node *next_ptr;
728 rb_dlink_node *uptr;
729 rb_dlink_node *next_uptr;
212380e3 730 struct Channel *chptr;
731 struct Client *target_p;
732 struct membership *msptr;
733 struct membership *mscptr;
734 buf_head_t linebuf;
735
736 linebuf_newbuf(&linebuf);
737 va_start(args, pattern);
738 linebuf_putmsg(&linebuf, pattern, &args, NULL);
739 va_end(args);
740
741 ++current_serial;
742 /* Skip them -- jilles */
743 user->serial = current_serial;
744
8e69bb4e 745 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head)
212380e3 746 {
747 mscptr = ptr->data;
748 chptr = mscptr->chptr;
749
8e69bb4e 750 RB_DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head)
212380e3 751 {
752 msptr = uptr->data;
753 target_p = msptr->client_p;
754
755 if(IsIOError(target_p) ||
756 target_p->serial == current_serial)
757 continue;
758
759 target_p->serial = current_serial;
760 send_linebuf(target_p, &linebuf);
761 }
762 }
763
764 linebuf_donebuf(&linebuf);
765}
766
767/* sendto_match_butone()
768 *
769 * inputs - server not to send to, source, mask, type of mask, va_args
770 * output -
771 * side effects - message is sent to matching clients
772 */
773void
774sendto_match_butone(struct Client *one, struct Client *source_p,
775 const char *mask, int what, const char *pattern, ...)
776{
777 static char buf[BUFSIZE];
778 va_list args;
779 struct Client *target_p;
af81d5a0
WP
780 rb_dlink_node *ptr;
781 rb_dlink_node *next_ptr;
212380e3 782 buf_head_t linebuf_local;
783 buf_head_t linebuf_name;
784 buf_head_t linebuf_id;
785
786 linebuf_newbuf(&linebuf_local);
787 linebuf_newbuf(&linebuf_name);
788 linebuf_newbuf(&linebuf_id);
789
790 va_start(args, pattern);
791 ircvsnprintf(buf, sizeof(buf), pattern, args);
792 va_end(args);
793
794 if(IsServer(source_p))
795 linebuf_putmsg(&linebuf_local, NULL, NULL,
796 ":%s %s", source_p->name, buf);
797 else
798 linebuf_putmsg(&linebuf_local, NULL, NULL,
799 ":%s!%s@%s %s",
800 source_p->name, source_p->username,
801 source_p->host, buf);
802
803 linebuf_putmsg(&linebuf_name, NULL, NULL, ":%s %s", source_p->name, buf);
804 linebuf_putmsg(&linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);
805
806 if(what == MATCH_HOST)
807 {
8e69bb4e 808 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
212380e3 809 {
810 target_p = ptr->data;
811
812 if(match(mask, target_p->host))
813 _send_linebuf(target_p, &linebuf_local);
814 }
815 }
816 /* what = MATCH_SERVER, if it doesnt match us, just send remote */
817 else if(match(mask, me.name))
818 {
8e69bb4e 819 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
212380e3 820 {
821 target_p = ptr->data;
822 _send_linebuf(target_p, &linebuf_local);
823 }
824 }
825
8e69bb4e 826 RB_DLINK_FOREACH(ptr, serv_list.head)
212380e3 827 {
828 target_p = ptr->data;
829
830 if(target_p == one)
831 continue;
832
833 if(has_id(target_p))
834 send_linebuf_remote(target_p, source_p, &linebuf_id);
835 else
836 send_linebuf_remote(target_p, source_p, &linebuf_name);
837 }
838
839 linebuf_donebuf(&linebuf_local);
840 linebuf_donebuf(&linebuf_id);
841 linebuf_donebuf(&linebuf_name);
842}
843
844/* sendto_match_servs()
845 *
846 * inputs - source, mask to send to, caps needed, va_args
847 * outputs -
848 * side effects - message is sent to matching servers with caps.
849 */
850void
851sendto_match_servs(struct Client *source_p, const char *mask, int cap,
852 int nocap, const char *pattern, ...)
853{
854 static char buf[BUFSIZE];
855 va_list args;
af81d5a0 856 rb_dlink_node *ptr;
212380e3 857 struct Client *target_p;
858 buf_head_t linebuf_id;
859 buf_head_t linebuf_name;
860
861 if(EmptyString(mask))
862 return;
863
864 linebuf_newbuf(&linebuf_id);
865 linebuf_newbuf(&linebuf_name);
866
867 va_start(args, pattern);
868 ircvsnprintf(buf, sizeof(buf), pattern, args);
869 va_end(args);
870
871 linebuf_putmsg(&linebuf_id, NULL, NULL,
872 ":%s %s", use_id(source_p), buf);
873 linebuf_putmsg(&linebuf_name, NULL, NULL,
874 ":%s %s", source_p->name, buf);
875
876 current_serial++;
877
8e69bb4e 878 RB_DLINK_FOREACH(ptr, global_serv_list.head)
212380e3 879 {
880 target_p = ptr->data;
881
882 /* dont send to ourselves, or back to where it came from.. */
883 if(IsMe(target_p) || target_p->from == source_p->from)
884 continue;
885
886 if(target_p->from->serial == current_serial)
887 continue;
888
889 if(match(mask, target_p->name))
890 {
891 /* if we set the serial here, then we'll never do
892 * a match() again if !IsCapable()
893 */
894 target_p->from->serial = current_serial;
895
896 if(cap && !IsCapable(target_p->from, cap))
897 continue;
898
899 if(nocap && !NotCapable(target_p->from, nocap))
900 continue;
901
902 if(has_id(target_p->from))
903 _send_linebuf(target_p->from, &linebuf_id);
904 else
905 _send_linebuf(target_p->from, &linebuf_name);
906 }
907 }
908
909 linebuf_donebuf(&linebuf_id);
910 linebuf_donebuf(&linebuf_name);
911}
912
8aba962d 913/* sendto_monitor()
914 *
915 * inputs - monitor nick to send to, format, va_args
916 * outputs - message to local users monitoring the given nick
917 * side effects -
918 */
919void
920sendto_monitor(struct monitor *monptr, const char *pattern, ...)
921{
922 va_list args;
923 buf_head_t linebuf;
924 struct Client *target_p;
af81d5a0
WP
925 rb_dlink_node *ptr;
926 rb_dlink_node *next_ptr;
8aba962d 927
928 linebuf_newbuf(&linebuf);
929
930 va_start(args, pattern);
931 linebuf_putmsg(&linebuf, pattern, &args, NULL);
932 va_end(args);
933
8e69bb4e 934 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, monptr->users.head)
8aba962d 935 {
936 target_p = ptr->data;
937
938 if(IsIOError(target_p))
939 continue;
940
941 _send_linebuf(target_p, &linebuf);
942 }
943
944 linebuf_donebuf(&linebuf);
945}
946
212380e3 947/* sendto_anywhere()
948 *
949 * inputs - target, source, va_args
950 * outputs -
951 * side effects - client is sent message with correct prefix.
952 */
953void
954sendto_anywhere(struct Client *target_p, struct Client *source_p,
955 const char *command, const char *pattern, ...)
956{
957 va_list args;
958 buf_head_t linebuf;
959
960 linebuf_newbuf(&linebuf);
961
962 va_start(args, pattern);
963
964 if(MyClient(target_p))
965 {
966 if(IsServer(source_p))
967 linebuf_putmsg(&linebuf, pattern, &args, ":%s %s %s ",
968 source_p->name, command,
969 target_p->name);
970 else
971 linebuf_putmsg(&linebuf, pattern, &args,
972 ":%s!%s@%s %s %s ",
973 source_p->name, source_p->username,
974 source_p->host, command,
975 target_p->name);
976 }
977 else
978 linebuf_putmsg(&linebuf, pattern, &args, ":%s %s %s ",
979 get_id(source_p, target_p), command,
980 get_id(target_p, target_p));
981 va_end(args);
982
983 if(MyClient(target_p))
984 _send_linebuf(target_p, &linebuf);
985 else
986 send_linebuf_remote(target_p, source_p, &linebuf);
987
988 linebuf_donebuf(&linebuf);
989}
990
991/* sendto_realops_flags()
992 *
993 * inputs - umode needed, level (opers/admin), va_args
994 * output -
995 * side effects - message is sent to opers with matching umodes
996 */
997void
998sendto_realops_flags(int flags, int level, const char *pattern, ...)
999{
1000 struct Client *client_p;
af81d5a0
WP
1001 rb_dlink_node *ptr;
1002 rb_dlink_node *next_ptr;
212380e3 1003 va_list args;
1004 buf_head_t linebuf;
1005
1006 linebuf_newbuf(&linebuf);
1007
1008 va_start(args, pattern);
1009 linebuf_putmsg(&linebuf, pattern, &args,
1010 ":%s NOTICE * :*** Notice -- ", me.name);
1011 va_end(args);
1012
8e69bb4e 1013 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head)
212380e3 1014 {
1015 client_p = ptr->data;
1016
1017 /* If we're sending it to opers and theyre an admin, skip.
1018 * If we're sending it to admins, and theyre not, skip.
1019 */
1020 if(((level == L_ADMIN) && !IsOperAdmin(client_p)) ||
1021 ((level == L_OPER) && IsOperAdmin(client_p)))
1022 continue;
1023
1024 if(client_p->umodes & flags)
1025 _send_linebuf(client_p, &linebuf);
1026 }
1027
1028 linebuf_donebuf(&linebuf);
1029}
1030
1031/* sendto_realops_snomask()
1032 *
1033 * inputs - snomask needed, level (opers/admin), va_args
1034 * output -
1035 * side effects - message is sent to opers with matching snomasks
1036 */
1037void
1038sendto_realops_snomask(int flags, int level, const char *pattern, ...)
1039{
1040 static char buf[BUFSIZE];
1041 char *snobuf;
1042 struct Client *client_p;
af81d5a0
WP
1043 rb_dlink_node *ptr;
1044 rb_dlink_node *next_ptr;
212380e3 1045 va_list args;
1046 buf_head_t linebuf;
1047
1048 linebuf_newbuf(&linebuf);
1049
1050 /* Be very sure not to do things like "Trying to send to myself"
1051 * L_NETWIDE, otherwise infinite recursion may result! -- jilles */
1052 if (level & L_NETWIDE && ConfigFileEntry.global_snotices)
1053 {
1054 /* rather a lot of copying around, oh well -- jilles */
1055 va_start(args, pattern);
1056 ircvsnprintf(buf, sizeof(buf), pattern, args);
1057 va_end(args);
1058 linebuf_putmsg(&linebuf, pattern, NULL,
1059 ":%s NOTICE * :*** Notice -- %s", me.name, buf);
1060 snobuf = construct_snobuf(flags);
1061 if (snobuf[1] != '\0')
1062 {
1063 sendto_server(NULL, NULL, CAP_ENCAP|CAP_TS6, NOCAPS,
1064 ":%s ENCAP * SNOTE %c :%s",
1065 me.id, snobuf[1], buf);
1066 sendto_server(NULL, NULL, CAP_ENCAP, CAP_TS6,
1067 ":%s ENCAP * SNOTE %c :%s",
1068 me.name, snobuf[1], buf);
1069 }
1070 }
1071 else
1072 {
1073 va_start(args, pattern);
1074 linebuf_putmsg(&linebuf, pattern, &args,
1075 ":%s NOTICE * :*** Notice -- ", me.name);
1076 va_end(args);
1077 }
1078 level &= ~L_NETWIDE;
1079
8e69bb4e 1080 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head)
212380e3 1081 {
1082 client_p = ptr->data;
1083
1084 /* If we're sending it to opers and theyre an admin, skip.
1085 * If we're sending it to admins, and theyre not, skip.
1086 */
1087 if(((level == L_ADMIN) && !IsOperAdmin(client_p)) ||
1088 ((level == L_OPER) && IsOperAdmin(client_p)))
1089 continue;
1090
1091 if(client_p->snomask & flags)
1092 _send_linebuf(client_p, &linebuf);
1093 }
1094
1095 linebuf_donebuf(&linebuf);
1096}
1097/* sendto_realops_snomask_from()
1098 *
1099 * inputs - snomask needed, level (opers/admin), source server, va_args
1100 * output -
1101 * side effects - message is sent to opers with matching snomask
1102 */
1103void
1104sendto_realops_snomask_from(int flags, int level, struct Client *source_p,
1105 const char *pattern, ...)
1106{
1107 struct Client *client_p;
af81d5a0
WP
1108 rb_dlink_node *ptr;
1109 rb_dlink_node *next_ptr;
212380e3 1110 va_list args;
1111 buf_head_t linebuf;
1112
1113 linebuf_newbuf(&linebuf);
1114
1115 va_start(args, pattern);
1116 linebuf_putmsg(&linebuf, pattern, &args,
1117 ":%s NOTICE * :*** Notice -- ", source_p->name);
1118 va_end(args);
1119
8e69bb4e 1120 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head)
212380e3 1121 {
1122 client_p = ptr->data;
1123
1124 /* If we're sending it to opers and theyre an admin, skip.
1125 * If we're sending it to admins, and theyre not, skip.
1126 */
1127 if(((level == L_ADMIN) && !IsOperAdmin(client_p)) ||
1128 ((level == L_OPER) && IsOperAdmin(client_p)))
1129 continue;
1130
1131 if(client_p->snomask & flags)
1132 _send_linebuf(client_p, &linebuf);
1133 }
1134
1135 linebuf_donebuf(&linebuf);
1136}
1137
1138/*
1139 * sendto_wallops_flags
1140 *
1141 * inputs - flag types of messages to show to real opers
1142 * - client sending request
1143 * - var args input message
1144 * output - NONE
1145 * side effects - Send a wallops to local opers
1146 */
1147void
1148sendto_wallops_flags(int flags, struct Client *source_p, const char *pattern, ...)
1149{
1150 struct Client *client_p;
af81d5a0
WP
1151 rb_dlink_node *ptr;
1152 rb_dlink_node *next_ptr;
212380e3 1153 va_list args;
1154 buf_head_t linebuf;
1155
1156 linebuf_newbuf(&linebuf);
1157
1158 va_start(args, pattern);
1159
1160 if(IsPerson(source_p))
1161 linebuf_putmsg(&linebuf, pattern, &args,
1162 ":%s!%s@%s WALLOPS :", source_p->name,
1163 source_p->username, source_p->host);
1164 else
1165 linebuf_putmsg(&linebuf, pattern, &args, ":%s WALLOPS :", source_p->name);
1166
1167 va_end(args);
1168
8e69bb4e 1169 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, IsPerson(source_p) && flags == UMODE_WALLOP ? lclient_list.head : local_oper_list.head)
212380e3 1170 {
1171 client_p = ptr->data;
1172
1173 if(client_p->umodes & flags)
1174 _send_linebuf(client_p, &linebuf);
1175 }
1176
1177 linebuf_donebuf(&linebuf);
1178}
1179
1180/* kill_client()
1181 *
1182 * input - client to send kill to, client to kill, va_args
1183 * output -
1184 * side effects - we issue a kill for the client
1185 */
1186void
1187kill_client(struct Client *target_p, struct Client *diedie, const char *pattern, ...)
1188{
1189 va_list args;
1190 buf_head_t linebuf;
1191
1192 linebuf_newbuf(&linebuf);
1193
1194 va_start(args, pattern);
1195 linebuf_putmsg(&linebuf, pattern, &args, ":%s KILL %s :",
1196 get_id(&me, target_p), get_id(diedie, target_p));
1197 va_end(args);
1198
1199 send_linebuf(target_p, &linebuf);
1200 linebuf_donebuf(&linebuf);
1201}
1202
1203
1204/*
1205 * kill_client_serv_butone
1206 *
1207 * inputs - pointer to client to not send to
1208 * - pointer to client to kill
1209 * output - NONE
1210 * side effects - Send a KILL for the given client
1211 * message to all connected servers
1212 * except the client 'one'. Also deal with
1213 * client being unknown to leaf, as in lazylink...
1214 */
1215void
1216kill_client_serv_butone(struct Client *one, struct Client *target_p, const char *pattern, ...)
1217{
1218 static char buf[BUFSIZE];
1219 va_list args;
1220 struct Client *client_p;
af81d5a0
WP
1221 rb_dlink_node *ptr;
1222 rb_dlink_node *next_ptr;
212380e3 1223 buf_head_t linebuf_id;
1224 buf_head_t linebuf_name;
1225
1226 linebuf_newbuf(&linebuf_name);
1227 linebuf_newbuf(&linebuf_id);
1228
1229 va_start(args, pattern);
1230 ircvsnprintf(buf, sizeof(buf), pattern, args);
1231 va_end(args);
1232
1233 linebuf_putmsg(&linebuf_name, NULL, NULL, ":%s KILL %s :%s",
1234 me.name, target_p->name, buf);
1235 linebuf_putmsg(&linebuf_id, NULL, NULL, ":%s KILL %s :%s",
1236 use_id(&me), use_id(target_p), buf);
1237
8e69bb4e 1238 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, serv_list.head)
212380e3 1239 {
1240 client_p = ptr->data;
1241
1242 /* ok, if the client we're supposed to not send to has an
1243 * ID, then we still want to issue the kill there..
1244 */
1245 if(one != NULL && (client_p == one->from) &&
1246 (!has_id(client_p) || !has_id(target_p)))
1247 continue;
1248
1249 if(has_id(client_p))
1250 _send_linebuf(client_p, &linebuf_id);
1251 else
1252 _send_linebuf(client_p, &linebuf_name);
1253 }
1254
1255 linebuf_donebuf(&linebuf_id);
1256 linebuf_donebuf(&linebuf_name);
1257}