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