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