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