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-2004 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
22  *  USA
23  *
24  *  $Id: send.c 26094 2008-09-19 15:33:46Z androsyn $
25  */
26 
27 #include "stdinc.h"
28 #include "struct.h"
29 #include "send.h"
30 #include "channel.h"
31 #include "class.h"
32 #include "client.h"
33 #include "common.h"
34 #include "match.h"
35 #include "ircd.h"
36 #include "numeric.h"
37 #include "s_serv.h"
38 #include "s_conf.h"
39 #include "s_newconf.h"
40 #include "s_log.h"
41 #include "hook.h"
42 #include "monitor.h"
43 
44 #define LOG_BUFSIZE 2048
45 
46 uint32_t current_serial = 0L;
47 static void send_queued_write(rb_fde_t *F, void *data);
48 static void send_queued(struct Client *to);
49 
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
send_linebuf(struct Client * to,buf_head_t * linebuf)58 send_linebuf(struct Client *to, buf_head_t * linebuf)
59 {
60 	if(IsMe(to))
61 	{
62 		sendto_realops_flags(UMODE_ALL, 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_flags(UMODE_ALL, 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), get_sendq(to));
82 		}
83 
84 		dead_link(to, 1);
85 		return -1;
86 	}
87 	else
88 	{
89 		/* just attach the linebuf to the sendq instead of
90 		 * generating a new one
91 		 */
92 		rb_linebuf_attach(&to->localClient->buf_sendq, linebuf);
93 	}
94 
95 	/*
96 	 ** Update statistics. The following is slightly incorrect
97 	 ** because it counts messages even if queued, but bytes
98 	 ** only really sent. Queued bytes get updated in SendQueued.
99 	 */
100 	to->localClient->sendM += 1;
101 	me.localClient->sendM += 1;
102 
103 	if(rb_linebuf_len(&to->localClient->buf_sendq) > 0)
104 		send_queued(to);
105 	return 0;
106 }
107 
108 void
send_pop_queue(struct Client * to)109 send_pop_queue(struct Client *to)
110 {
111 	if(to->from != NULL)
112 		to = to->from;
113 	if(!MyConnect(to) || IsIOError(to))
114 		return;
115 	if(rb_linebuf_len(&to->localClient->buf_sendq) > 0)
116 		send_queued(to);
117 }
118 
119 /* send_rb_linebuf_remote()
120  *
121  * inputs	- client to attach to, sender, linebuf
122  * outputs	-
123  * side effects - client has linebuf attached
124  */
125 static void
send_rb_linebuf_remote(struct Client * to,struct Client * from,buf_head_t * linebuf)126 send_rb_linebuf_remote(struct Client *to, struct Client *from, buf_head_t * linebuf)
127 {
128 	if(to->from)
129 		to = to->from;
130 
131 	/* test for fake direction */
132 	if(!MyClient(from) && IsClient(to) && (to == from->from))
133 	{
134 		if(IsServer(from))
135 		{
136 			sendto_realops_flags(UMODE_ALL, L_ALL,
137 					     "Send message to %s[%s] dropped from %s(Fake Dir)",
138 					     to->name, to->from->name, from->name);
139 			return;
140 		}
141 
142 		sendto_realops_flags(UMODE_ALL, L_ALL,
143 				     "Ghosted: %s[%s@%s] from %s[%s@%s] (%s)",
144 				     to->name, to->username, to->host,
145 				     from->name, from->username, from->host, to->from->name);
146 		kill_client_serv_butone(NULL, to, "%s (%s[%s@%s] Ghosted %s)",
147 					me.name, to->name, to->username, to->host, to->from->name);
148 
149 		to->flags |= FLAGS_KILLED;
150 
151 		exit_client(NULL, to, &me, "Ghosted client");
152 		return;
153 	}
154 
155 	send_linebuf(to, linebuf);
156 	return;
157 }
158 
159 static void
send_queued(struct Client * to)160 send_queued(struct Client *to)
161 {
162 	int retlen;
163 #ifdef USE_IODEBUG_HOOKS
164 	hook_data_int hd;
165 #endif
166 	/* cant write anything to a dead socket. */
167 	if(IsIOError(to))
168 		return;
169 
170 	/* Something wants us to not send anything currently */
171 	if(IsCork(to))
172 		return;
173 
174 	/* try to flush later when the write event resets this */
175 	if(IsFlush(to))
176 		return;
177 
178 #ifdef USE_IODEBUG_HOOKS
179 	hd.client = to;
180 	if(to->localClient->buf_sendq.list.head)
181 		hd.arg1 = ((buf_line_t *) to->localClient->buf_sendq.list.head->data)->buf +
182 			to->localClient->buf_sendq.writeofs;
183 #endif
184 
185 	if(rb_linebuf_len(&to->localClient->buf_sendq))
186 	{
187 		while((retlen =
188 		       rb_linebuf_flush(to->localClient->F, &to->localClient->buf_sendq)) > 0)
189 		{
190 			/* We have some data written .. update counters */
191 #ifdef USE_IODEBUG_HOOKS
192 			hd.arg2 = retlen;
193 			call_hook(h_iosend_id, &hd);
194 
195 			if(to->localClient->buf_sendq.list.head)
196 				hd.arg1 =
197 					((buf_line_t *) to->localClient->buf_sendq.list.
198 					 head->data)->buf + to->localClient->buf_sendq.writeofs;
199 #endif
200 
201 			ClearFlush(to);
202 
203 			to->localClient->sendB += retlen;
204 			me.localClient->sendB += retlen;
205 		}
206 
207 		if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
208 		{
209 			dead_link(to, 0);
210 			return;
211 		}
212 	}
213 	if(rb_linebuf_len(&to->localClient->buf_sendq))
214 	{
215 		SetFlush(to);
216 		rb_setselect(to->localClient->F, RB_SELECT_WRITE, send_queued_write, to);
217 	}
218 	else
219 		ClearFlush(to);
220 
221 }
222 
223 /* send_queued_write()
224  *
225  * inputs	- fd to have queue sent, client we're sending to
226  * outputs	- contents of queue
227  * side effects - write is scheduled if queue isnt emptied
228  */
229 static void
send_queued_write(rb_fde_t * F,void * data)230 send_queued_write(rb_fde_t *F, void *data)
231 {
232 	struct Client *to = data;
233 	ClearFlush(to);
234 	send_queued(to);
235 }
236 
237 /* sendto_one_buffer()
238  *
239  * inputs	- client to send to, buffer
240  * outputs	- client has message put into its queue
241  */
242 void
sendto_one_buffer(struct Client * target_p,const char * buffer)243 sendto_one_buffer(struct Client *target_p, const char *buffer)
244 {
245 	buf_head_t linebuf;
246 
247 	if(target_p->from != NULL)
248 		target_p = target_p->from;
249 
250 	if(IsIOError(target_p))
251 		return;
252 
253 	rb_linebuf_newbuf(&linebuf);
254 	rb_linebuf_putbuf(&linebuf, buffer);
255 	send_linebuf(target_p, &linebuf);
256 	rb_linebuf_donebuf(&linebuf);
257 }
258 
259 /* sendto_one()
260  *
261  * inputs	- client to send to, va_args
262  * outputs	- client has message put into its queue
263  * side effects -
264  */
265 void
sendto_one(struct Client * target_p,const char * pattern,...)266 sendto_one(struct Client *target_p, const char *pattern, ...)
267 {
268 	va_list args;
269 	buf_head_t linebuf;
270 
271 	/* send remote if to->from non NULL */
272 	if(target_p->from != NULL)
273 		target_p = target_p->from;
274 
275 	if(IsIOError(target_p))
276 		return;
277 
278 	rb_linebuf_newbuf(&linebuf);
279 
280 	va_start(args, pattern);
281 	rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
282 	va_end(args);
283 
284 	send_linebuf(target_p, &linebuf);
285 
286 	rb_linebuf_donebuf(&linebuf);
287 
288 }
289 
290 
291 /* sendto_one_prefix()
292  *
293  * inputs	- client to send to, va_args
294  * outputs	- client has message put into its queue
295  * side effects - source(us)/target is chosen based on TS6 capability
296  */
297 void
sendto_one_prefix(struct Client * target_p,struct Client * source_p,const char * command,const char * pattern,...)298 sendto_one_prefix(struct Client *target_p, struct Client *source_p,
299 		  const char *command, const char *pattern, ...)
300 {
301 	struct Client *dest_p;
302 	va_list args;
303 	buf_head_t linebuf;
304 
305 	/* send remote if to->from non NULL */
306 	if(target_p->from != NULL)
307 		dest_p = target_p->from;
308 	else
309 		dest_p = target_p;
310 
311 	if(IsIOError(dest_p))
312 		return;
313 
314 	if(IsMe(dest_p))
315 	{
316 		sendto_realops_flags(UMODE_ALL, L_ALL, "Trying to send to myself!");
317 		return;
318 	}
319 
320 	rb_linebuf_newbuf(&linebuf);
321 	va_start(args, pattern);
322 	rb_linebuf_putmsg(&linebuf, pattern, &args,
323 			  ":%s %s %s ",
324 			  get_id(source_p, target_p), command, get_id(target_p, target_p));
325 	va_end(args);
326 
327 	send_linebuf(dest_p, &linebuf);
328 	rb_linebuf_donebuf(&linebuf);
329 }
330 
331 /*
332  * sendto_one_notice_local()
333  * inputs	- client to send to, va_args
334  * outputs	- client has a NOTICE put into its queue
335  * side effects - fast path for local clients
336  */
337 static void
sendto_one_notice_local(struct Client * target_p,const char * pattern,va_list * ap)338 sendto_one_notice_local(struct Client *target_p, const char *pattern, va_list * ap)
339 {
340 	buf_head_t linebuf;
341 	rb_linebuf_newbuf(&linebuf);
342 	rb_linebuf_putmsg(&linebuf, pattern, ap, ":%s NOTICE %s ", me.name, target_p->name);
343 	send_linebuf(target_p, &linebuf);
344 	rb_linebuf_donebuf(&linebuf);
345 }
346 
347 /* sendto_one_notice()
348  *
349  * inputs	- client to send to, va_args
350  * outputs	- client has a NOTICE put into its queue
351  * side effects - source(us)/target is chosen based on TS6 capability
352  */
353 void
sendto_one_notice(struct Client * target_p,const char * pattern,...)354 sendto_one_notice(struct Client *target_p, const char *pattern, ...)
355 {
356 	struct Client *dest_p;
357 	va_list args;
358 	buf_head_t linebuf;
359 
360 	if(MyConnect(target_p))
361 	{
362 		if(IsIOError(target_p))
363 			return;
364 		va_start(args, pattern);
365 		sendto_one_notice_local(target_p, pattern, &args);
366 		va_end(args);
367 		return;
368 	}
369 
370 	dest_p = target_p->from;
371 
372 	if(IsIOError(dest_p))
373 		return;
374 
375 	if(IsMe(dest_p))
376 	{
377 		sendto_realops_flags(UMODE_ALL, L_ALL, "Trying to send to myself!");
378 		return;
379 	}
380 
381 	rb_linebuf_newbuf(&linebuf);
382 	va_start(args, pattern);
383 	rb_linebuf_putmsg(&linebuf, pattern, &args,
384 			  ":%s NOTICE %s ", get_id(&me, target_p), get_id(target_p, target_p));
385 	va_end(args);
386 
387 	send_linebuf(dest_p, &linebuf);
388 	rb_linebuf_donebuf(&linebuf);
389 }
390 
391 
392 /* sendto_one_numeric()
393  *
394  * inputs	- client to send to, va_args
395  * outputs	- client has message put into its queue
396  * side effects - source/target is chosen based on TS6 capability
397  */
398 void
sendto_one_numeric(struct Client * target_p,int numeric,const char * pattern,...)399 sendto_one_numeric(struct Client *target_p, int numeric, const char *pattern, ...)
400 {
401 	struct Client *dest_p;
402 	va_list args;
403 	buf_head_t linebuf;
404 
405 	/* send remote if to->from non NULL */
406 	if(target_p->from != NULL)
407 		dest_p = target_p->from;
408 	else
409 		dest_p = target_p;
410 
411 	if(IsIOError(dest_p))
412 		return;
413 
414 	if(IsMe(dest_p))
415 	{
416 		sendto_realops_flags(UMODE_ALL, L_ALL, "Trying to send to myself!");
417 		return;
418 	}
419 
420 	rb_linebuf_newbuf(&linebuf);
421 	va_start(args, pattern);
422 	rb_linebuf_putmsg(&linebuf, pattern, &args,
423 			  ":%s %03d %s ",
424 			  get_id(&me, target_p), numeric, get_id(target_p, target_p));
425 	va_end(args);
426 
427 	send_linebuf(dest_p, &linebuf);
428 	rb_linebuf_donebuf(&linebuf);
429 }
430 
431 /*
432  * sendto_server
433  *
434  * inputs       - pointer to client to NOT send to
435  *              - caps or'd together which must ALL be present
436  *              - caps or'd together which must ALL NOT be present
437  *              - printf style format string
438  *              - args to format string
439  * output       - NONE
440  * side effects - Send a message to all connected servers, except the
441  *                client 'one' (if non-NULL), as long as the servers
442  *                support ALL capabs in 'caps', and NO capabs in 'nocaps'.
443  *
444  * This function was written in an attempt to merge together the other
445  * billion sendto_*serv*() functions, which sprung up with capabs, uids etc
446  * -davidt
447  */
448 void
sendto_server(struct Client * one,struct Channel * chptr,unsigned long caps,unsigned long nocaps,const char * format,...)449 sendto_server(struct Client *one, struct Channel *chptr, unsigned long caps,
450 	      unsigned long nocaps, const char *format, ...)
451 {
452 	va_list args;
453 	struct Client *target_p;
454 	rb_dlink_node *ptr;
455 	rb_dlink_node *next_ptr;
456 	buf_head_t linebuf;
457 
458 	/* noone to send to.. */
459 	if(rb_dlink_list_length(&serv_list) == 0)
460 		return;
461 
462 	if(chptr != NULL && *chptr->chname != '#')
463 		return;
464 
465 	rb_linebuf_newbuf(&linebuf);
466 	va_start(args, format);
467 	rb_linebuf_putmsg(&linebuf, format, &args, NULL);
468 	va_end(args);
469 
470 	RB_DLINK_FOREACH_SAFE(ptr, next_ptr, serv_list.head)
471 	{
472 		target_p = ptr->data;
473 
474 		/* check against 'one' */
475 		if(one != NULL && (target_p == one->from))
476 			continue;
477 
478 		/* check we have required capabs */
479 		if(!IsCapable(target_p, caps))
480 			continue;
481 
482 		/* check we don't have any forbidden capabs */
483 		if(!NotCapable(target_p, nocaps))
484 			continue;
485 
486 		send_linebuf(target_p, &linebuf);
487 	}
488 
489 	rb_linebuf_donebuf(&linebuf);
490 
491 }
492 
493 /* sendto_channel_flags()
494  *
495  * inputs	- server not to send to, flags needed, source, channel, va_args
496  * outputs	- message is sent to channel members
497  * side effects -
498  */
499 void
sendto_channel_flags(struct Client * one,int type,struct Client * source_p,struct Channel * chptr,const char * pattern,...)500 sendto_channel_flags(struct Client *one, int type, struct Client *source_p,
501 		     struct Channel *chptr, const char *pattern, ...)
502 {
503 	static char buf[BUFSIZE];
504 	va_list args;
505 	buf_head_t rb_linebuf_local;
506 	buf_head_t rb_linebuf_name;
507 	buf_head_t rb_linebuf_id;
508 	struct Client *target_p;
509 	struct membership *msptr;
510 	rb_dlink_node *ptr;
511 	rb_dlink_node *next_ptr;
512 
513 	rb_linebuf_newbuf(&rb_linebuf_local);
514 	rb_linebuf_newbuf(&rb_linebuf_name);
515 	rb_linebuf_newbuf(&rb_linebuf_id);
516 
517 	current_serial++;
518 
519 	va_start(args, pattern);
520 	rb_vsnprintf(buf, sizeof(buf), pattern, args);
521 	va_end(args);
522 
523 	if(IsServer(source_p))
524 		rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL, ":%s %s", source_p->name, buf);
525 	else
526 		rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL,
527 				  ":%s!%s@%s %s",
528 				  source_p->name, source_p->username, source_p->host, buf);
529 
530 	rb_linebuf_putmsg(&rb_linebuf_name, NULL, NULL, ":%s %s", source_p->name, buf);
531 	rb_linebuf_putmsg(&rb_linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);
532 
533 	RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->members.head)
534 	{
535 		msptr = ptr->data;
536 		target_p = msptr->client_p;
537 
538 		if(IsIOError(target_p->from) || target_p->from == one)
539 			continue;
540 
541 		if(type && ((msptr->flags & type) == 0))
542 			continue;
543 
544 		if(IsDeaf(target_p))
545 			continue;
546 
547 		if(!MyClient(target_p))
548 		{
549 			/* if we've got a specific type, target must support
550 			 * CHW.. --fl
551 			 */
552 			if(type && NotCapable(target_p->from, CAP_CHW))
553 				continue;
554 
555 			if(target_p->from->localClient->serial != current_serial)
556 			{
557 				if(has_id(target_p->from))
558 					send_rb_linebuf_remote(target_p, source_p, &rb_linebuf_id);
559 				else
560 					send_rb_linebuf_remote(target_p, source_p,
561 							       &rb_linebuf_name);
562 
563 				target_p->from->localClient->serial = current_serial;
564 			}
565 		}
566 		else
567 			send_linebuf(target_p, &rb_linebuf_local);
568 	}
569 
570 	rb_linebuf_donebuf(&rb_linebuf_local);
571 	rb_linebuf_donebuf(&rb_linebuf_name);
572 	rb_linebuf_donebuf(&rb_linebuf_id);
573 }
574 
575 
576 /* sendto_channel_local()
577  *
578  * inputs	- flags to send to, channel to send to, va_args
579  * outputs	- message to local channel members
580  * side effects -
581  */
582 void
sendto_channel_local(int type,struct Channel * chptr,const char * pattern,...)583 sendto_channel_local(int type, struct Channel *chptr, const char *pattern, ...)
584 {
585 	va_list args;
586 	buf_head_t linebuf;
587 	struct membership *msptr;
588 	struct Client *target_p;
589 	rb_dlink_node *ptr;
590 	rb_dlink_node *next_ptr;
591 
592 	rb_linebuf_newbuf(&linebuf);
593 
594 	va_start(args, pattern);
595 	rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
596 	va_end(args);
597 
598 	RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
599 	{
600 		msptr = ptr->data;
601 		target_p = msptr->client_p;
602 
603 		if(IsIOError(target_p))
604 			continue;
605 
606 		if(type && ((msptr->flags & type) == 0))
607 			continue;
608 
609 		send_linebuf(target_p, &linebuf);
610 	}
611 
612 	rb_linebuf_donebuf(&linebuf);
613 }
614 
615 /*
616  * sendto_common_channels_local()
617  *
618  * inputs	- pointer to client
619  *		- pattern to send
620  * output	- NONE
621  * side effects	- Sends a message to all people on local server who are
622  * 		  in same channel with user.
623  *		  used by m_nick.c and exit_one_client.
624  */
625 void
sendto_common_channels_local(struct Client * user,const char * pattern,...)626 sendto_common_channels_local(struct Client *user, const char *pattern, ...)
627 {
628 	va_list args;
629 	rb_dlink_node *ptr;
630 	rb_dlink_node *next_ptr;
631 	rb_dlink_node *uptr;
632 	rb_dlink_node *next_uptr;
633 	struct Channel *chptr;
634 	struct Client *target_p;
635 	struct membership *msptr;
636 	struct membership *mscptr;
637 	buf_head_t linebuf;
638 
639 	rb_linebuf_newbuf(&linebuf);
640 	va_start(args, pattern);
641 	rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
642 	va_end(args);
643 
644 	++current_serial;
645 
646 	RB_DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head)
647 	{
648 		mscptr = ptr->data;
649 		chptr = mscptr->chptr;
650 
651 		RB_DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head)
652 		{
653 			msptr = uptr->data;
654 			target_p = msptr->client_p;
655 
656 			if(IsIOError(target_p) || target_p->localClient->serial == current_serial)
657 				continue;
658 
659 			target_p->localClient->serial = current_serial;
660 			send_linebuf(target_p->from ? target_p->from : target_p, &linebuf);
661 		}
662 	}
663 
664 	/* this can happen when the user isnt in any channels, but we still
665 	 * need to send them the data, ie a nick change
666 	 */
667 	if(MyConnect(user) && (user->localClient->serial != current_serial))
668 		send_linebuf(user->from ? user->from : user, &linebuf);
669 
670 	rb_linebuf_donebuf(&linebuf);
671 }
672 
673 /* sendto_match_butone()
674  *
675  * inputs	- server not to send to, source, mask, type of mask, va_args
676  * output	-
677  * side effects - message is sent to matching clients
678  */
679 void
sendto_match_butone(struct Client * one,struct Client * source_p,const char * mask,int what,const char * pattern,...)680 sendto_match_butone(struct Client *one, struct Client *source_p,
681 		    const char *mask, int what, const char *pattern, ...)
682 {
683 	static char buf[BUFSIZE];
684 	va_list args;
685 	struct Client *target_p;
686 	rb_dlink_node *ptr;
687 	rb_dlink_node *next_ptr;
688 	buf_head_t rb_linebuf_local;
689 	buf_head_t rb_linebuf_name;
690 	buf_head_t rb_linebuf_id;
691 
692 	rb_linebuf_newbuf(&rb_linebuf_local);
693 	rb_linebuf_newbuf(&rb_linebuf_name);
694 	rb_linebuf_newbuf(&rb_linebuf_id);
695 
696 	va_start(args, pattern);
697 	rb_vsnprintf(buf, sizeof(buf), pattern, args);
698 	va_end(args);
699 
700 	if(IsServer(source_p))
701 		rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL, ":%s %s", source_p->name, buf);
702 	else
703 		rb_linebuf_putmsg(&rb_linebuf_local, NULL, NULL,
704 				  ":%s!%s@%s %s",
705 				  source_p->name, source_p->username, source_p->host, buf);
706 
707 	rb_linebuf_putmsg(&rb_linebuf_name, NULL, NULL, ":%s %s", source_p->name, buf);
708 	rb_linebuf_putmsg(&rb_linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);
709 
710 	if(what == MATCH_HOST)
711 	{
712 		RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
713 		{
714 			target_p = ptr->data;
715 
716 			if(match(mask, target_p->host))
717 				send_linebuf(target_p, &rb_linebuf_local);
718 		}
719 	}
720 	/* what = MATCH_SERVER, if it doesnt match us, just send remote */
721 	else if(match(mask, me.name))
722 	{
723 		RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
724 		{
725 			target_p = ptr->data;
726 			send_linebuf(target_p, &rb_linebuf_local);
727 		}
728 	}
729 
730 	RB_DLINK_FOREACH(ptr, serv_list.head)
731 	{
732 		target_p = ptr->data;
733 
734 		if(target_p == one)
735 			continue;
736 
737 		if(has_id(target_p))
738 			send_rb_linebuf_remote(target_p, source_p, &rb_linebuf_id);
739 		else
740 			send_rb_linebuf_remote(target_p, source_p, &rb_linebuf_name);
741 	}
742 
743 	rb_linebuf_donebuf(&rb_linebuf_local);
744 	rb_linebuf_donebuf(&rb_linebuf_id);
745 	rb_linebuf_donebuf(&rb_linebuf_name);
746 }
747 
748 /* sendto_match_servs()
749  *
750  * inputs       - source, mask to send to, caps needed, va_args
751  * outputs      -
752  * side effects - message is sent to matching servers with caps.
753  */
754 void
sendto_match_servs(struct Client * source_p,const char * mask,int cap,int nocap,const char * pattern,...)755 sendto_match_servs(struct Client *source_p, const char *mask, int cap,
756 		   int nocap, const char *pattern, ...)
757 {
758 	static char buf[BUFSIZE];
759 	va_list args;
760 	rb_dlink_node *ptr;
761 	struct Client *target_p;
762 	buf_head_t rb_linebuf_id;
763 	buf_head_t rb_linebuf_name;
764 
765 	if(EmptyString(mask))
766 		return;
767 
768 	rb_linebuf_newbuf(&rb_linebuf_id);
769 	rb_linebuf_newbuf(&rb_linebuf_name);
770 
771 	va_start(args, pattern);
772 	rb_vsnprintf(buf, sizeof(buf), pattern, args);
773 	va_end(args);
774 
775 	rb_linebuf_putmsg(&rb_linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);
776 	rb_linebuf_putmsg(&rb_linebuf_name, NULL, NULL, ":%s %s", source_p->name, buf);
777 
778 	current_serial++;
779 
780 	RB_DLINK_FOREACH(ptr, global_serv_list.head)
781 	{
782 		target_p = ptr->data;
783 
784 		/* dont send to ourselves, or back to where it came from.. */
785 		if(IsMe(target_p) || target_p->from == source_p->from)
786 			continue;
787 
788 		if(target_p->from->localClient->serial == current_serial)
789 			continue;
790 
791 		if(match(mask, target_p->name))
792 		{
793 			/* if we set the serial here, then we'll never do
794 			 * a match() again if !IsCapable()
795 			 */
796 			target_p->from->localClient->serial = current_serial;
797 
798 			if(cap && !IsCapable(target_p->from, cap))
799 				continue;
800 
801 			if(nocap && !NotCapable(target_p->from, nocap))
802 				continue;
803 
804 			if(has_id(target_p->from))
805 				send_linebuf(target_p->from, &rb_linebuf_id);
806 			else
807 				send_linebuf(target_p->from, &rb_linebuf_name);
808 		}
809 	}
810 
811 	rb_linebuf_donebuf(&rb_linebuf_id);
812 	rb_linebuf_donebuf(&rb_linebuf_name);
813 }
814 
815 /* sendto_monitor()
816  *
817  * inputs	- monitor nick to send to, format, va_args
818  * outputs	- message to local users monitoring the given nick
819  * side effects -
820  */
821 void
sendto_monitor(struct monitor * monptr,const char * pattern,...)822 sendto_monitor(struct monitor *monptr, const char *pattern, ...)
823 {
824 	va_list args;
825 	buf_head_t linebuf;
826 	struct Client *target_p;
827 	rb_dlink_node *ptr;
828 	rb_dlink_node *next_ptr;
829 
830 	rb_linebuf_newbuf(&linebuf);
831 
832 	va_start(args, pattern);
833 	rb_linebuf_putmsg(&linebuf, pattern, &args, NULL);
834 	va_end(args);
835 
836 	RB_DLINK_FOREACH_SAFE(ptr, next_ptr, monptr->users.head)
837 	{
838 		target_p = ptr->data;
839 
840 		if(IsIOError(target_p))
841 			continue;
842 
843 		send_linebuf(target_p, &linebuf);
844 	}
845 
846 	rb_linebuf_donebuf(&linebuf);
847 }
848 
849 /* sendto_anywhere()
850  *
851  * inputs	- target, source, va_args
852  * outputs	-
853  * side effects - client is sent message with correct prefix.
854  */
855 void
sendto_anywhere(struct Client * target_p,struct Client * source_p,const char * command,const char * pattern,...)856 sendto_anywhere(struct Client *target_p, struct Client *source_p,
857 		const char *command, const char *pattern, ...)
858 {
859 	va_list args;
860 	buf_head_t linebuf;
861 
862 	rb_linebuf_newbuf(&linebuf);
863 
864 	va_start(args, pattern);
865 
866 	if(MyClient(target_p))
867 	{
868 		if(IsServer(source_p))
869 			rb_linebuf_putmsg(&linebuf, pattern, &args, ":%s %s %s ",
870 					  source_p->name, command, target_p->name);
871 		else
872 			rb_linebuf_putmsg(&linebuf, pattern, &args,
873 					  ":%s!%s@%s %s %s ",
874 					  source_p->name, source_p->username,
875 					  source_p->host, command, target_p->name);
876 	}
877 	else
878 		rb_linebuf_putmsg(&linebuf, pattern, &args, ":%s %s %s ",
879 				  get_id(source_p, target_p), command, get_id(target_p, target_p));
880 	va_end(args);
881 
882 	if(MyClient(target_p))
883 		send_linebuf(target_p, &linebuf);
884 	else
885 		send_rb_linebuf_remote(target_p, source_p, &linebuf);
886 
887 	rb_linebuf_donebuf(&linebuf);
888 }
889 
890 /* sendto_realops_flags()
891  *
892  * inputs	- umode needed, level (opers/admin), va_args
893  * output	-
894  * side effects - message is sent to opers with matching umodes
895  */
896 void
sendto_realops_flags(int flags,int level,const char * pattern,...)897 sendto_realops_flags(int flags, int level, const char *pattern, ...)
898 {
899 	struct Client *client_p;
900 	rb_dlink_node *ptr;
901 	rb_dlink_node *next_ptr;
902 	va_list args;
903 	buf_head_t linebuf;
904 
905 	if(EmptyString(me.name))
906 		return;
907 
908 	rb_linebuf_newbuf(&linebuf);
909 
910 	va_start(args, pattern);
911 	rb_linebuf_putmsg(&linebuf, pattern, &args, ":%s NOTICE * :*** Notice -- ", me.name);
912 	va_end(args);
913 
914 	RB_DLINK_FOREACH_SAFE(ptr, next_ptr, oper_list.head)
915 	{
916 		client_p = ptr->data;
917 
918 		/* If we're sending it to opers and theyre an admin, skip.
919 		 * If we're sending it to admins, and theyre not, skip.
920 		 */
921 		if(((level == L_ADMIN) && !IsAdmin(client_p)) ||
922 		   ((level == L_OPER) && IsAdmin(client_p)))
923 			continue;
924 
925 		if(client_p->umodes & flags)
926 			send_linebuf(client_p, &linebuf);
927 	}
928 
929 	rb_linebuf_donebuf(&linebuf);
930 }
931 
932 /*
933  * sendto_wallops_flags
934  *
935  * inputs       - flag types of messages to show to real opers
936  *              - client sending request
937  *              - var args input message
938  * output       - NONE
939  * side effects - Send a wallops to local opers
940  */
941 void
sendto_wallops_flags(int flags,struct Client * source_p,const char * pattern,...)942 sendto_wallops_flags(int flags, struct Client *source_p, const char *pattern, ...)
943 {
944 	struct Client *client_p;
945 	rb_dlink_node *ptr;
946 	rb_dlink_node *next_ptr;
947 	va_list args;
948 	buf_head_t linebuf;
949 
950 	rb_linebuf_newbuf(&linebuf);
951 
952 	va_start(args, pattern);
953 
954 	if(IsClient(source_p))
955 		rb_linebuf_putmsg(&linebuf, pattern, &args,
956 				  ":%s!%s@%s WALLOPS :", source_p->name,
957 				  source_p->username, source_p->host);
958 	else
959 		rb_linebuf_putmsg(&linebuf, pattern, &args, ":%s WALLOPS :", source_p->name);
960 
961 	va_end(args);
962 
963 	RB_DLINK_FOREACH_SAFE(ptr, next_ptr, oper_list.head)
964 	{
965 		client_p = ptr->data;
966 		if(client_p->umodes & flags)
967 			send_linebuf(client_p, &linebuf);
968 	}
969 
970 	rb_linebuf_donebuf(&linebuf);
971 }
972 
973 /* kill_client()
974  *
975  * input	- client to send kill to, client to kill, va_args
976  * output	-
977  * side effects - we issue a kill for the client
978  */
979 void
kill_client(struct Client * target_p,struct Client * diedie,const char * pattern,...)980 kill_client(struct Client *target_p, struct Client *diedie, const char *pattern, ...)
981 {
982 	va_list args;
983 	buf_head_t linebuf;
984 
985 	rb_linebuf_newbuf(&linebuf);
986 
987 	va_start(args, pattern);
988 	rb_linebuf_putmsg(&linebuf, pattern, &args, ":%s KILL %s :",
989 			  get_id(&me, target_p), get_id(diedie, target_p));
990 	va_end(args);
991 
992 	send_linebuf(target_p->from ? target_p->from : target_p, &linebuf);
993 	rb_linebuf_donebuf(&linebuf);
994 }
995 
996 
997 /*
998  * kill_client_serv_butone
999  *
1000  * inputs	- pointer to client to not send to
1001  *		- pointer to client to kill
1002  * output	- NONE
1003  * side effects	- Send a KILL for the given client
1004  *		  message to all connected servers
1005  *                except the client 'one'. Also deal with
1006  *		  client being unknown to leaf, as in lazylink...
1007  */
1008 void
kill_client_serv_butone(struct Client * one,struct Client * target_p,const char * pattern,...)1009 kill_client_serv_butone(struct Client *one, struct Client *target_p, const char *pattern, ...)
1010 {
1011 	static char buf[BUFSIZE];
1012 	va_list args;
1013 	struct Client *client_p;
1014 	rb_dlink_node *ptr;
1015 	rb_dlink_node *next_ptr;
1016 	buf_head_t rb_linebuf_id;
1017 	buf_head_t rb_linebuf_name;
1018 
1019 	rb_linebuf_newbuf(&rb_linebuf_name);
1020 	rb_linebuf_newbuf(&rb_linebuf_id);
1021 
1022 	va_start(args, pattern);
1023 	rb_vsnprintf(buf, sizeof(buf), pattern, args);
1024 	va_end(args);
1025 
1026 	rb_linebuf_putmsg(&rb_linebuf_name, NULL, NULL, ":%s KILL %s :%s",
1027 			  me.name, target_p->name, buf);
1028 	rb_linebuf_putmsg(&rb_linebuf_id, NULL, NULL, ":%s KILL %s :%s",
1029 			  use_id(&me), use_id(target_p), buf);
1030 
1031 	RB_DLINK_FOREACH_SAFE(ptr, next_ptr, serv_list.head)
1032 	{
1033 		client_p = ptr->data;
1034 
1035 		/* ok, if the client we're supposed to not send to has an
1036 		 * ID, then we still want to issue the kill there..
1037 		 */
1038 		if(one != NULL && (client_p == one->from) &&
1039 		   (!has_id(client_p) || !has_id(target_p)))
1040 			continue;
1041 
1042 		if(has_id(client_p))
1043 			send_linebuf(client_p, &rb_linebuf_id);
1044 		else
1045 			send_linebuf(client_p, &rb_linebuf_name);
1046 	}
1047 
1048 	rb_linebuf_donebuf(&rb_linebuf_id);
1049 	rb_linebuf_donebuf(&rb_linebuf_name);
1050 }
1051