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