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