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