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