]> jfr.im git - solanum.git/blob - ircd/send.c
free server_p->certfp, allocated in newconf.c
[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 /* sendto_channel_flags()
489 *
490 * inputs - server not to send to, flags needed, source, channel, va_args
491 * outputs - message is sent to channel members
492 * side effects -
493 */
494 void
495 sendto_channel_flags(struct Client *one, int type, struct Client *source_p,
496 struct Channel *chptr, const char *pattern, ...)
497 {
498 char buf[BUFSIZE];
499 va_list args;
500 buf_head_t rb_linebuf_local;
501 buf_head_t rb_linebuf_id;
502 struct Client *target_p;
503 struct membership *msptr;
504 rb_dlink_node *ptr;
505 rb_dlink_node *next_ptr;
506 int current_capmask = 0;
507 struct MsgBuf msgbuf;
508
509 rb_linebuf_newbuf(&rb_linebuf_local);
510 rb_linebuf_newbuf(&rb_linebuf_id);
511
512 current_serial++;
513
514 build_msgbuf_from(&msgbuf, source_p, NULL);
515
516 va_start(args, pattern);
517 vsnprintf(buf, sizeof buf, pattern, args);
518 va_end(args);
519
520 linebuf_put_msgbuf(&msgbuf, &rb_linebuf_local, NOCAPS, "%s", buf);
521 rb_linebuf_putmsg(&rb_linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);
522
523 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->members.head)
524 {
525 msptr = ptr->data;
526 target_p = msptr->client_p;
527
528 if(!MyClient(source_p) && (IsIOError(target_p->from) || target_p->from == one))
529 continue;
530
531 if(MyClient(source_p) && !IsCapable(source_p, CLICAP_ECHO_MESSAGE) && target_p == one)
532 continue;
533
534 if(type && ((msptr->flags & type) == 0))
535 continue;
536
537 if(IsDeaf(target_p))
538 continue;
539
540 if(!MyClient(target_p))
541 {
542 /* if we've got a specific type, target must support
543 * CHW.. --fl
544 */
545 if(type && NotCapable(target_p->from, CAP_CHW))
546 continue;
547
548 if(target_p->from->serial != current_serial)
549 {
550 send_linebuf_remote(target_p, source_p, &rb_linebuf_id);
551 target_p->from->serial = current_serial;
552 }
553 }
554 else
555 {
556 if (target_p->localClient->caps != current_capmask)
557 {
558 /* reset the linebuf */
559 rb_linebuf_donebuf(&rb_linebuf_local);
560 rb_linebuf_newbuf(&rb_linebuf_local);
561
562 /* render the new linebuf and attach it */
563 linebuf_put_msgbuf(&msgbuf, &rb_linebuf_local, target_p->localClient->caps, "%s", buf);
564 current_capmask = target_p->localClient->caps;
565 }
566
567 _send_linebuf(target_p, &rb_linebuf_local);
568 }
569 }
570
571 rb_linebuf_donebuf(&rb_linebuf_local);
572 rb_linebuf_donebuf(&rb_linebuf_id);
573 }
574
575 /* sendto_channel_flags()
576 *
577 * inputs - server not to send to, flags needed, source, channel, va_args
578 * outputs - message is sent to channel members
579 * side effects -
580 */
581 void
582 sendto_channel_opmod(struct Client *one, struct Client *source_p,
583 struct Channel *chptr, const char *command,
584 const char *text)
585 {
586 buf_head_t rb_linebuf_local;
587 buf_head_t rb_linebuf_old;
588 buf_head_t rb_linebuf_new;
589 struct Client *target_p;
590 struct membership *msptr;
591 rb_dlink_node *ptr;
592 rb_dlink_node *next_ptr;
593
594 rb_linebuf_newbuf(&rb_linebuf_local);
595 rb_linebuf_newbuf(&rb_linebuf_old);
596 rb_linebuf_newbuf(&rb_linebuf_new);
597
598 current_serial++;
599
600 if(IsServer(source_p))
601 rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL,
602 ":%s %s %s :%s",
603 source_p->name, command, chptr->chname, text);
604 else
605 rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL,
606 ":%s!%s@%s %s %s :%s",
607 source_p->name, source_p->username,
608 source_p->host, command, chptr->chname, text);
609
610 if (chptr->mode.mode & MODE_MODERATED)
611 rb_linebuf_putmsg(&rb_linebuf_old, NULL, NULL,
612 ":%s %s %s :%s",
613 use_id(source_p), command, chptr->chname, text);
614 else
615 rb_linebuf_putmsg(&rb_linebuf_old, NULL, NULL,
616 ":%s NOTICE @%s :<%s:%s> %s",
617 use_id(source_p->servptr), chptr->chname,
618 source_p->name, chptr->chname, text);
619 rb_linebuf_putmsg(&rb_linebuf_new, NULL, NULL,
620 ":%s %s =%s :%s",
621 use_id(source_p), command, chptr->chname, text);
622
623 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->members.head)
624 {
625 msptr = ptr->data;
626 target_p = msptr->client_p;
627
628 if(!MyClient(source_p) && (IsIOError(target_p->from) || target_p->from == one))
629 continue;
630
631 if(MyClient(source_p) && !IsCapable(source_p, CLICAP_ECHO_MESSAGE) && target_p == one)
632 continue;
633
634 if((msptr->flags & CHFL_CHANOP) == 0)
635 continue;
636
637 if(IsDeaf(target_p))
638 continue;
639
640 if(!MyClient(target_p))
641 {
642 /* if we've got a specific type, target must support
643 * CHW.. --fl
644 */
645 if(NotCapable(target_p->from, CAP_CHW))
646 continue;
647
648 if(target_p->from->serial != current_serial)
649 {
650 if (IsCapable(target_p->from, CAP_EOPMOD))
651 send_linebuf_remote(target_p, source_p, &rb_linebuf_new);
652 else
653 send_linebuf_remote(target_p, source_p, &rb_linebuf_old);
654 target_p->from->serial = current_serial;
655 }
656 }
657 else
658 _send_linebuf(target_p, &rb_linebuf_local);
659 }
660
661 rb_linebuf_donebuf(&rb_linebuf_local);
662 rb_linebuf_donebuf(&rb_linebuf_old);
663 rb_linebuf_donebuf(&rb_linebuf_new);
664 }
665
666 /* sendto_channel_local()
667 *
668 * inputs - flags to send to, channel to send to, va_args
669 * outputs - message to local channel members
670 * side effects -
671 */
672 void
673 sendto_channel_local(int type, struct Channel *chptr, const char *pattern, ...)
674 {
675 va_list args;
676 buf_head_t linebuf;
677 struct membership *msptr;
678 struct Client *target_p;
679 rb_dlink_node *ptr;
680 rb_dlink_node *next_ptr;
681
682 rb_linebuf_newbuf(&linebuf);
683
684 va_start(args, pattern);
685 rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
686 va_end(args);
687
688 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
689 {
690 msptr = ptr->data;
691 target_p = msptr->client_p;
692
693 if(IsIOError(target_p))
694 continue;
695
696 if(type == ONLY_OPERS)
697 {
698 if (!IsOper(target_p))
699 continue;
700 }
701 else if(type && ((msptr->flags & type) == 0))
702 continue;
703
704 _send_linebuf(target_p, &linebuf);
705 }
706
707 rb_linebuf_donebuf(&linebuf);
708 }
709
710 /*
711 * _sendto_channel_local_with_capability_butone()
712 *
713 * Shared implementation of sendto_channel_local_with_capability and sendto_channel_local_with_capability_butone
714 */
715 static void
716 _sendto_channel_local_with_capability_butone(struct Client *one, int type, int caps, int negcaps, struct Channel *chptr,
717 const char *pattern, va_list * args)
718 {
719 buf_head_t linebuf;
720 struct membership *msptr;
721 struct Client *target_p;
722 rb_dlink_node *ptr;
723 rb_dlink_node *next_ptr;
724
725 rb_linebuf_newbuf(&linebuf);
726 rb_linebuf_putmsg(&linebuf, pattern, args, NULL);
727
728 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
729 {
730 msptr = ptr->data;
731 target_p = msptr->client_p;
732
733 if (target_p == one)
734 continue;
735
736 if(IsIOError(target_p) ||
737 !IsCapable(target_p, caps) ||
738 !NotCapable(target_p, negcaps))
739 continue;
740
741 if(type && ((msptr->flags & type) == 0))
742 continue;
743
744 _send_linebuf(target_p, &linebuf);
745 }
746
747 rb_linebuf_donebuf(&linebuf);
748 }
749
750 /* sendto_channel_local_with_capability()
751 *
752 * inputs - flags to send to, caps, negate caps, channel to send to, va_args
753 * outputs - message to local channel members
754 * side effects -
755 */
756 void
757 sendto_channel_local_with_capability(int type, int caps, int negcaps, struct Channel *chptr, const char *pattern, ...)
758 {
759 va_list args;
760
761 va_start(args, pattern);
762 _sendto_channel_local_with_capability_butone(NULL, type, caps, negcaps, chptr, pattern, &args);
763 va_end(args);
764 }
765
766
767 /* sendto_channel_local_with_capability()
768 *
769 * inputs - flags to send to, caps, negate caps, channel to send to, va_args
770 * outputs - message to local channel members
771 * side effects -
772 */
773 void
774 sendto_channel_local_with_capability_butone(struct Client *one, int type, int caps, int negcaps, struct Channel *chptr,
775 const char *pattern, ...)
776 {
777 va_list args;
778
779 va_start(args, pattern);
780 _sendto_channel_local_with_capability_butone(one, type, caps, negcaps, chptr, pattern, &args);
781 va_end(args);
782 }
783
784
785 /* sendto_channel_local_butone()
786 *
787 * inputs - flags to send to, channel to send to, va_args
788 * - user to ignore when sending
789 * outputs - message to local channel members
790 * side effects -
791 */
792 void
793 sendto_channel_local_butone(struct Client *one, int type, struct Channel *chptr, const char *pattern, ...)
794 {
795 va_list args;
796 buf_head_t linebuf;
797 struct membership *msptr;
798 struct Client *target_p;
799 struct MsgBuf msgbuf;
800 rb_dlink_node *ptr;
801 rb_dlink_node *next_ptr;
802
803 rb_linebuf_newbuf(&linebuf);
804
805 build_msgbuf_from(&msgbuf, one, NULL);
806
807 va_start(args, pattern);
808 rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
809 va_end(args);
810
811 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
812 {
813 msptr = ptr->data;
814 target_p = msptr->client_p;
815
816 if(target_p == one)
817 continue;
818
819 if(IsIOError(target_p))
820 continue;
821
822 if(type && ((msptr->flags & type) == 0))
823 continue;
824
825 /* attach the present linebuf to the target */
826 _send_linebuf(target_p, &linebuf);
827 }
828
829 rb_linebuf_donebuf(&linebuf);
830 }
831
832 /*
833 * sendto_common_channels_local()
834 *
835 * inputs - pointer to client
836 * - capability mask
837 * - negated capability mask
838 * - pattern to send
839 * output - NONE
840 * side effects - Sends a message to all people on local server who are
841 * in same channel with user.
842 * used by m_nick.c and exit_one_client.
843 */
844 void
845 sendto_common_channels_local(struct Client *user, int cap, int negcap, const char *pattern, ...)
846 {
847 va_list args;
848 rb_dlink_node *ptr;
849 rb_dlink_node *next_ptr;
850 rb_dlink_node *uptr;
851 rb_dlink_node *next_uptr;
852 struct Channel *chptr;
853 struct Client *target_p;
854 struct membership *msptr;
855 struct membership *mscptr;
856 buf_head_t linebuf;
857
858 rb_linebuf_newbuf(&linebuf);
859 va_start(args, pattern);
860 rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
861 va_end(args);
862
863 ++current_serial;
864
865 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head)
866 {
867 mscptr = ptr->data;
868 chptr = mscptr->chptr;
869
870 RB_DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head)
871 {
872 msptr = uptr->data;
873 target_p = msptr->client_p;
874
875 if(IsIOError(target_p) ||
876 target_p->serial == current_serial ||
877 !IsCapable(target_p, cap) ||
878 !NotCapable(target_p, negcap))
879 continue;
880
881 target_p->serial = current_serial;
882 send_linebuf(target_p, &linebuf);
883 }
884 }
885
886 /* this can happen when the user isnt in any channels, but we still
887 * need to send them the data, ie a nick change
888 */
889 if(MyConnect(user) && (user->serial != current_serial))
890 send_linebuf(user, &linebuf);
891
892 rb_linebuf_donebuf(&linebuf);
893 }
894
895 /*
896 * sendto_common_channels_local_butone()
897 *
898 * inputs - pointer to client
899 * - capability mask
900 * - negated capability mask
901 * - pattern to send
902 * output - NONE
903 * side effects - Sends a message to all people on local server who are
904 * in same channel with user, except for user itself.
905 */
906 void
907 sendto_common_channels_local_butone(struct Client *user, int cap, int negcap, const char *pattern, ...)
908 {
909 va_list args;
910 rb_dlink_node *ptr;
911 rb_dlink_node *next_ptr;
912 rb_dlink_node *uptr;
913 rb_dlink_node *next_uptr;
914 struct Channel *chptr;
915 struct Client *target_p;
916 struct membership *msptr;
917 struct membership *mscptr;
918 buf_head_t linebuf;
919
920 rb_linebuf_newbuf(&linebuf);
921
922 va_start(args, pattern);
923 rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
924 va_end(args);
925
926 ++current_serial;
927 /* Skip them -- jilles */
928 user->serial = current_serial;
929
930 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head)
931 {
932 mscptr = ptr->data;
933 chptr = mscptr->chptr;
934
935 RB_DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head)
936 {
937 msptr = uptr->data;
938 target_p = msptr->client_p;
939
940 if(IsIOError(target_p) ||
941 target_p->serial == current_serial ||
942 !IsCapable(target_p, cap) ||
943 !NotCapable(target_p, negcap))
944 continue;
945
946 target_p->serial = current_serial;
947 send_linebuf(target_p, &linebuf);
948 }
949 }
950
951 rb_linebuf_donebuf(&linebuf);
952 }
953
954 /* sendto_match_butone()
955 *
956 * inputs - server not to send to, source, mask, type of mask, va_args
957 * output -
958 * side effects - message is sent to matching clients
959 */
960 void
961 sendto_match_butone(struct Client *one, struct Client *source_p,
962 const char *mask, int what, const char *pattern, ...)
963 {
964 static char buf[BUFSIZE];
965 va_list args;
966 struct Client *target_p;
967 rb_dlink_node *ptr;
968 rb_dlink_node *next_ptr;
969 buf_head_t rb_linebuf_local;
970 buf_head_t rb_linebuf_id;
971
972 rb_linebuf_newbuf(&rb_linebuf_local);
973 rb_linebuf_newbuf(&rb_linebuf_id);
974
975 va_start(args, pattern);
976 vsnprintf(buf, sizeof(buf), pattern, args);
977 va_end(args);
978
979 if(IsServer(source_p))
980 rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL,
981 ":%s %s", source_p->name, buf);
982 else
983 rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL,
984 ":%s!%s@%s %s",
985 source_p->name, source_p->username,
986 source_p->host, buf);
987
988 rb_linebuf_putmsg(&rb_linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);
989
990 if(what == MATCH_HOST)
991 {
992 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
993 {
994 target_p = ptr->data;
995
996 if(match(mask, target_p->host))
997 _send_linebuf(target_p, &rb_linebuf_local);
998 }
999 }
1000 /* what = MATCH_SERVER, if it doesnt match us, just send remote */
1001 else if(match(mask, me.name))
1002 {
1003 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
1004 {
1005 target_p = ptr->data;
1006 _send_linebuf(target_p, &rb_linebuf_local);
1007 }
1008 }
1009
1010 RB_DLINK_FOREACH(ptr, serv_list.head)
1011 {
1012 target_p = ptr->data;
1013
1014 if(target_p == one)
1015 continue;
1016
1017 send_linebuf_remote(target_p, source_p, &rb_linebuf_id);
1018 }
1019
1020 rb_linebuf_donebuf(&rb_linebuf_local);
1021 rb_linebuf_donebuf(&rb_linebuf_id);
1022 }
1023
1024 /* sendto_match_servs()
1025 *
1026 * inputs - source, mask to send to, caps needed, va_args
1027 * outputs -
1028 * side effects - message is sent to matching servers with caps.
1029 */
1030 void
1031 sendto_match_servs(struct Client *source_p, const char *mask, int cap,
1032 int nocap, const char *pattern, ...)
1033 {
1034 static char buf[BUFSIZE];
1035 va_list args;
1036 rb_dlink_node *ptr;
1037 struct Client *target_p;
1038 buf_head_t rb_linebuf_id;
1039
1040 if(EmptyString(mask))
1041 return;
1042
1043 rb_linebuf_newbuf(&rb_linebuf_id);
1044
1045 va_start(args, pattern);
1046 vsnprintf(buf, sizeof(buf), pattern, args);
1047 va_end(args);
1048
1049 rb_linebuf_putmsg(&rb_linebuf_id, NULL, NULL,
1050 ":%s %s", use_id(source_p), buf);
1051
1052 current_serial++;
1053
1054 RB_DLINK_FOREACH(ptr, global_serv_list.head)
1055 {
1056 target_p = ptr->data;
1057
1058 /* dont send to ourselves, or back to where it came from.. */
1059 if(IsMe(target_p) || target_p->from == source_p->from)
1060 continue;
1061
1062 if(target_p->from->serial == current_serial)
1063 continue;
1064
1065 if(match(mask, target_p->name))
1066 {
1067 /* if we set the serial here, then we'll never do
1068 * a match() again if !IsCapable()
1069 */
1070 target_p->from->serial = current_serial;
1071
1072 if(cap && !IsCapable(target_p->from, cap))
1073 continue;
1074
1075 if(nocap && !NotCapable(target_p->from, nocap))
1076 continue;
1077
1078 _send_linebuf(target_p->from, &rb_linebuf_id);
1079 }
1080 }
1081
1082 rb_linebuf_donebuf(&rb_linebuf_id);
1083 }
1084
1085 /* sendto_local_clients_with_capability()
1086 *
1087 * inputs - caps needed, pattern, va_args
1088 * outputs -
1089 * side effects - message is sent to matching local clients with caps.
1090 */
1091 void
1092 sendto_local_clients_with_capability(int cap, const char *pattern, ...)
1093 {
1094 va_list args;
1095 rb_dlink_node *ptr;
1096 struct Client *target_p;
1097 buf_head_t linebuf;
1098
1099 rb_linebuf_newbuf(&linebuf);
1100
1101 va_start(args, pattern);
1102 rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
1103 va_end(args);
1104
1105 RB_DLINK_FOREACH(ptr, lclient_list.head)
1106 {
1107 target_p = ptr->data;
1108
1109 if(IsIOError(target_p) || !IsCapable(target_p, cap))
1110 continue;
1111
1112 send_linebuf(target_p, &linebuf);
1113 }
1114
1115 rb_linebuf_donebuf(&linebuf);
1116 }
1117
1118 /* sendto_monitor()
1119 *
1120 * inputs - monitor nick to send to, format, va_args
1121 * outputs - message to local users monitoring the given nick
1122 * side effects -
1123 */
1124 void
1125 sendto_monitor(struct monitor *monptr, const char *pattern, ...)
1126 {
1127 va_list args;
1128 buf_head_t linebuf;
1129 struct Client *target_p;
1130 rb_dlink_node *ptr;
1131 rb_dlink_node *next_ptr;
1132
1133 rb_linebuf_newbuf(&linebuf);
1134
1135 va_start(args, pattern);
1136 rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
1137 va_end(args);
1138
1139 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, monptr->users.head)
1140 {
1141 target_p = ptr->data;
1142
1143 if(IsIOError(target_p))
1144 continue;
1145
1146 _send_linebuf(target_p, &linebuf);
1147 }
1148
1149 rb_linebuf_donebuf(&linebuf);
1150 }
1151
1152 /* sendto_anywhere()
1153 *
1154 * inputs - target, source, va_args
1155 * outputs -
1156 * side effects - client is sent message with correct prefix.
1157 */
1158 void
1159 sendto_anywhere(struct Client *target_p, struct Client *source_p,
1160 const char *command, const char *pattern, ...)
1161 {
1162 va_list args;
1163 buf_head_t linebuf;
1164
1165 rb_linebuf_newbuf(&linebuf);
1166
1167 va_start(args, pattern);
1168
1169 if(MyClient(target_p))
1170 {
1171 if(IsServer(source_p))
1172 rb_linebuf_putmsg(&linebuf, pattern, &args, ":%s %s %s ",
1173 source_p->name, command,
1174 target_p->name);
1175 else
1176 {
1177 struct MsgBuf msgbuf;
1178
1179 build_msgbuf_from(&msgbuf, source_p, command);
1180 msgbuf.target = target_p->name;
1181
1182 linebuf_put_msgvbuf(&msgbuf, &linebuf, target_p->localClient->caps, pattern, &args);
1183 }
1184 }
1185 else
1186 rb_linebuf_putmsg(&linebuf, pattern, &args, ":%s %s %s ",
1187 get_id(source_p, target_p), command,
1188 get_id(target_p, target_p));
1189 va_end(args);
1190
1191 if(MyClient(target_p))
1192 _send_linebuf(target_p, &linebuf);
1193 else
1194 send_linebuf_remote(target_p, source_p, &linebuf);
1195
1196 rb_linebuf_donebuf(&linebuf);
1197 }
1198
1199 /* sendto_realops_snomask()
1200 *
1201 * inputs - snomask needed, level (opers/admin), va_args
1202 * output -
1203 * side effects - message is sent to opers with matching snomasks
1204 */
1205 void
1206 sendto_realops_snomask(int flags, int level, const char *pattern, ...)
1207 {
1208 static char buf[BUFSIZE];
1209 char *snobuf;
1210 struct Client *client_p;
1211 rb_dlink_node *ptr;
1212 rb_dlink_node *next_ptr;
1213 va_list args;
1214 buf_head_t linebuf;
1215
1216 rb_linebuf_newbuf(&linebuf);
1217
1218 /* Be very sure not to do things like "Trying to send to myself"
1219 * L_NETWIDE, otherwise infinite recursion may result! -- jilles */
1220 if (level & L_NETWIDE && ConfigFileEntry.global_snotices)
1221 {
1222 /* rather a lot of copying around, oh well -- jilles */
1223 va_start(args, pattern);
1224 vsnprintf(buf, sizeof(buf), pattern, args);
1225 va_end(args);
1226 rb_linebuf_putmsg(&linebuf, pattern, NULL,
1227 ":%s NOTICE * :*** Notice -- %s", me.name, buf);
1228 snobuf = construct_snobuf(flags);
1229 if (snobuf[1] != '\0')
1230 sendto_server(NULL, NULL, CAP_ENCAP|CAP_TS6, NOCAPS,
1231 ":%s ENCAP * SNOTE %c :%s",
1232 me.id, snobuf[1], buf);
1233 }
1234 else if (remote_rehash_oper_p != NULL)
1235 {
1236 /* rather a lot of copying around, oh well -- jilles */
1237 va_start(args, pattern);
1238 vsnprintf(buf, sizeof(buf), pattern, args);
1239 va_end(args);
1240 rb_linebuf_putmsg(&linebuf, pattern, NULL,
1241 ":%s NOTICE * :*** Notice -- %s", me.name, buf);
1242 sendto_one_notice(remote_rehash_oper_p, ":*** Notice -- %s", buf);
1243 }
1244 else
1245 {
1246 va_start(args, pattern);
1247 rb_linebuf_putmsg(&linebuf, pattern, &args,
1248 ":%s NOTICE * :*** Notice -- ", me.name);
1249 va_end(args);
1250 }
1251 level &= ~L_NETWIDE;
1252
1253 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head)
1254 {
1255 client_p = ptr->data;
1256
1257 /* If we're sending it to opers and theyre an admin, skip.
1258 * If we're sending it to admins, and theyre not, skip.
1259 */
1260 if(((level == L_ADMIN) && !IsOperAdmin(client_p)) ||
1261 ((level == L_OPER) && IsOperAdmin(client_p)))
1262 continue;
1263
1264 if(client_p->snomask & flags)
1265 _send_linebuf(client_p, &linebuf);
1266 }
1267
1268 rb_linebuf_donebuf(&linebuf);
1269 }
1270 /* sendto_realops_snomask_from()
1271 *
1272 * inputs - snomask needed, level (opers/admin), source server, va_args
1273 * output -
1274 * side effects - message is sent to opers with matching snomask
1275 */
1276 void
1277 sendto_realops_snomask_from(int flags, int level, struct Client *source_p,
1278 const char *pattern, ...)
1279 {
1280 struct Client *client_p;
1281 rb_dlink_node *ptr;
1282 rb_dlink_node *next_ptr;
1283 va_list args;
1284 buf_head_t linebuf;
1285
1286 rb_linebuf_newbuf(&linebuf);
1287
1288 va_start(args, pattern);
1289 rb_linebuf_putmsg(&linebuf, pattern, &args,
1290 ":%s NOTICE * :*** Notice -- ", source_p->name);
1291 va_end(args);
1292
1293 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head)
1294 {
1295 client_p = ptr->data;
1296
1297 /* If we're sending it to opers and theyre an admin, skip.
1298 * If we're sending it to admins, and theyre not, skip.
1299 */
1300 if(((level == L_ADMIN) && !IsOperAdmin(client_p)) ||
1301 ((level == L_OPER) && IsOperAdmin(client_p)))
1302 continue;
1303
1304 if(client_p->snomask & flags)
1305 _send_linebuf(client_p, &linebuf);
1306 }
1307
1308 rb_linebuf_donebuf(&linebuf);
1309 }
1310
1311 /*
1312 * sendto_wallops_flags
1313 *
1314 * inputs - flag types of messages to show to real opers
1315 * - client sending request
1316 * - var args input message
1317 * output - NONE
1318 * side effects - Send a wallops to local opers
1319 */
1320 void
1321 sendto_wallops_flags(int flags, struct Client *source_p, const char *pattern, ...)
1322 {
1323 struct Client *client_p;
1324 rb_dlink_node *ptr;
1325 rb_dlink_node *next_ptr;
1326 va_list args;
1327 buf_head_t linebuf;
1328
1329 rb_linebuf_newbuf(&linebuf);
1330
1331 va_start(args, pattern);
1332
1333 if(IsPerson(source_p))
1334 rb_linebuf_putmsg(&linebuf, pattern, &args,
1335 ":%s!%s@%s WALLOPS :", source_p->name,
1336 source_p->username, source_p->host);
1337 else
1338 rb_linebuf_putmsg(&linebuf, pattern, &args, ":%s WALLOPS :", source_p->name);
1339
1340 va_end(args);
1341
1342 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, IsPerson(source_p) && flags == UMODE_WALLOP ? lclient_list.head : local_oper_list.head)
1343 {
1344 client_p = ptr->data;
1345
1346 if(client_p->umodes & flags)
1347 _send_linebuf(client_p, &linebuf);
1348 }
1349
1350 rb_linebuf_donebuf(&linebuf);
1351 }
1352
1353 /* kill_client()
1354 *
1355 * input - client to send kill to, client to kill, va_args
1356 * output -
1357 * side effects - we issue a kill for the client
1358 */
1359 void
1360 kill_client(struct Client *target_p, struct Client *diedie, const char *pattern, ...)
1361 {
1362 va_list args;
1363 buf_head_t linebuf;
1364
1365 rb_linebuf_newbuf(&linebuf);
1366
1367 va_start(args, pattern);
1368 rb_linebuf_putmsg(&linebuf, pattern, &args, ":%s KILL %s :",
1369 get_id(&me, target_p), get_id(diedie, target_p));
1370 va_end(args);
1371
1372 send_linebuf(target_p, &linebuf);
1373 rb_linebuf_donebuf(&linebuf);
1374 }
1375
1376
1377 /*
1378 * kill_client_serv_butone
1379 *
1380 * inputs - pointer to client to not send to
1381 * - pointer to client to kill
1382 * output - NONE
1383 * side effects - Send a KILL for the given client
1384 * message to all connected servers
1385 * except the client 'one'. Also deal with
1386 * client being unknown to leaf, as in lazylink...
1387 */
1388 void
1389 kill_client_serv_butone(struct Client *one, struct Client *target_p, const char *pattern, ...)
1390 {
1391 static char buf[BUFSIZE];
1392 va_list args;
1393 struct Client *client_p;
1394 rb_dlink_node *ptr;
1395 rb_dlink_node *next_ptr;
1396 buf_head_t rb_linebuf_id;
1397
1398 rb_linebuf_newbuf(&rb_linebuf_id);
1399
1400 va_start(args, pattern);
1401 vsnprintf(buf, sizeof(buf), pattern, args);
1402 va_end(args);
1403
1404 rb_linebuf_putmsg(&rb_linebuf_id, NULL, NULL, ":%s KILL %s :%s",
1405 use_id(&me), use_id(target_p), buf);
1406
1407 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, serv_list.head)
1408 {
1409 client_p = ptr->data;
1410
1411 /* ok, if the client we're supposed to not send to has an
1412 * ID, then we still want to issue the kill there..
1413 */
1414 if(one != NULL && (client_p == one->from) &&
1415 (!has_id(client_p) || !has_id(target_p)))
1416 continue;
1417
1418 _send_linebuf(client_p, &rb_linebuf_id);
1419 }
1420
1421 rb_linebuf_donebuf(&rb_linebuf_id);
1422 }