Import from http://svn.freenode.net/ircd-seven/private/beu/seven (r196).
[seven-1.x.git] / src / send.c
blob170b517a0fb2087972478f70f03e8d3def7d78d9
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * send.c: Functions for sending messages.
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
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.
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.
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
24 * $Id: send.c 108 2006-10-14 18:12:28Z spb $
27 #include "stdinc.h"
28 #include "tools.h"
29 #include "send.h"
30 #include "channel.h"
31 #include "class.h"
32 #include "client.h"
33 #include "common.h"
34 #include "irc_string.h"
35 #include "ircd.h"
36 #include "numeric.h"
37 #include "commio.h"
38 #include "s_serv.h"
39 #include "sprintf_irc.h"
40 #include "s_conf.h"
41 #include "s_newconf.h"
42 #include "linebuf.h"
43 #include "s_log.h"
44 #include "memory.h"
45 #include "hook.h"
47 #define LOG_BUFSIZE 2048
49 /* send the message to the link the target is attached to */
50 #define send_linebuf(a,b) _send_linebuf((a->from ? a->from : a) ,b)
52 unsigned long current_serial = 0L;
54 /* send_linebuf()
56 * inputs - client to send to, linebuf to attach
57 * outputs -
58 * side effects - linebuf is attached to client
60 static int
61 _send_linebuf(struct Client *to, buf_head_t *linebuf)
63 if(IsMe(to))
65 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send message to myself!");
66 return 0;
69 if(!MyConnect(to) || IsIOError(to))
70 return 0;
72 if(linebuf_len(&to->localClient->buf_sendq) > get_sendq(to))
74 if(IsServer(to))
76 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
77 "Max SendQ limit exceeded for %s: %u > %lu",
78 get_server_name(to, HIDE_IP),
79 linebuf_len(&to->localClient->buf_sendq),
80 get_sendq(to));
82 ilog(L_SERVER, "Max SendQ limit exceeded for %s: %u > %lu",
83 log_client_name(to, SHOW_IP),
84 linebuf_len(&to->localClient->buf_sendq),
85 get_sendq(to));
88 if(IsClient(to))
89 to->flags |= FLAGS_SENDQEX;
91 dead_link(to);
92 return -1;
94 else
96 /* just attach the linebuf to the sendq instead of
97 * generating a new one
99 linebuf_attach(&to->localClient->buf_sendq, linebuf);
103 ** Update statistics. The following is slightly incorrect
104 ** because it counts messages even if queued, but bytes
105 ** only really sent. Queued bytes get updated in SendQueued.
107 to->localClient->sendM += 1;
108 me.localClient->sendM += 1;
109 if(linebuf_len(&to->localClient->buf_sendq) > 0)
110 send_queued_write(to->localClient->fd, to);
111 return 0;
114 /* send_linebuf_remote()
116 * inputs - client to attach to, sender, linebuf
117 * outputs -
118 * side effects - client has linebuf attached
120 static void
121 send_linebuf_remote(struct Client *to, struct Client *from, buf_head_t *linebuf)
123 if(to->from)
124 to = to->from;
126 /* test for fake direction */
127 if(!MyClient(from) && IsPerson(to) && (to == from->from))
129 if(IsServer(from))
131 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
132 "Send message to %s[%s] dropped from %s(Fake Dir)",
133 to->name, to->from->name, from->name);
134 return;
137 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
138 "Ghosted: %s[%s@%s] from %s[%s@%s] (%s)",
139 to->name, to->username, to->host,
140 from->name, from->username, from->host, to->from->name);
141 kill_client_serv_butone(NULL, to, "%s (%s[%s@%s] Ghosted %s)",
142 me.name, to->name, to->username,
143 to->host, to->from->name);
145 to->flags |= FLAGS_KILLED;
147 exit_client(NULL, to, &me, "Ghosted client");
148 return;
151 _send_linebuf(to, linebuf);
152 return;
155 /* send_queued_write()
157 * inputs - fd to have queue sent, client we're sending to
158 * outputs - contents of queue
159 * side effects - write is rescheduled if queue isnt emptied
161 void
162 send_queued_write(int fd, void *data)
164 struct Client *to = data;
165 int retlen;
166 int flags;
167 #ifdef USE_IODEBUG_HOOKS
168 hook_data_int hd;
169 #endif
170 /* cant write anything to a dead socket. */
171 if(IsIOError(to))
172 return;
174 #ifdef USE_IODEBUG_HOOKS
175 hd.client = to;
176 if(to->localClient->buf_sendq.list.head)
177 hd.arg1 = ((buf_line_t *) to->localClient->buf_sendq.list.head->data)->buf +
178 to->localClient->buf_sendq.writeofs;
179 #endif
181 if(linebuf_len(&to->localClient->buf_sendq))
183 while ((retlen =
184 linebuf_flush(to->localClient->fd, &to->localClient->buf_sendq)) > 0)
186 /* We have some data written .. update counters */
187 #ifdef USE_IODEBUG_HOOKS
188 hd.arg2 = retlen;
189 call_hook(h_iosend_id, &hd);
191 if(to->localClient->buf_sendq.list.head)
192 hd.arg1 =
193 ((buf_line_t *) to->localClient->buf_sendq.list.head->
194 data)->buf + to->localClient->buf_sendq.writeofs;
195 #endif
198 to->localClient->sendB += retlen;
199 me.localClient->sendB += retlen;
200 if(to->localClient->sendB > 1023)
202 to->localClient->sendK += (to->localClient->sendB >> 10);
203 to->localClient->sendB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
205 else if(me.localClient->sendB > 1023)
207 me.localClient->sendK += (me.localClient->sendB >> 10);
208 me.localClient->sendB &= 0x03ff;
212 if(retlen == 0 || (retlen < 0 && !ignoreErrno(errno)))
214 dead_link(to);
215 return;
218 if(ignoreErrno(errno))
219 flags = COMM_SELECT_WRITE|COMM_SELECT_RETRY;
220 else
221 flags = COMM_SELECT_WRITE;
222 if(linebuf_len(&to->localClient->buf_sendq))
223 comm_setselect(fd, FDLIST_IDLECLIENT, flags,
224 send_queued_write, to, 0);
227 /* send_queued_slink_write()
229 * inputs - fd to have queue sent, client we're sending to
230 * outputs - contents of queue
231 * side effects - write is rescheduled if queue isnt emptied
233 void
234 send_queued_slink_write(int fd, void *data)
236 struct Client *to = data;
237 int retlen;
240 ** Once socket is marked dead, we cannot start writing to it,
241 ** even if the error is removed...
243 if(IsIOError(to))
244 return;
246 /* Next, lets try to write some data */
247 if(to->localClient->slinkq)
249 retlen = write(to->localClient->ctrlfd,
250 to->localClient->slinkq + to->localClient->slinkq_ofs,
251 to->localClient->slinkq_len);
253 if(retlen < 0)
255 /* If we have a fatal error */
256 if(!ignoreErrno(errno))
258 dead_link(to);
259 return;
262 /* 0 bytes is an EOF .. */
263 else if(retlen == 0)
265 dead_link(to);
266 return;
268 else
270 to->localClient->slinkq_len -= retlen;
272 s_assert(to->localClient->slinkq_len >= 0);
273 if(to->localClient->slinkq_len)
274 to->localClient->slinkq_ofs += retlen;
275 else
277 to->localClient->slinkq_ofs = 0;
278 MyFree(to->localClient->slinkq);
279 to->localClient->slinkq = NULL;
284 /* if we have any more data, reschedule a write */
285 if(to->localClient->slinkq_len)
286 comm_setselect(to->localClient->ctrlfd, FDLIST_IDLECLIENT,
287 COMM_SELECT_WRITE|COMM_SELECT_RETRY, send_queued_slink_write, to, 0);
290 /* sendto_one()
292 * inputs - client to send to, va_args
293 * outputs - client has message put into its queue
294 * side effects -
296 void
297 sendto_one(struct Client *target_p, const char *pattern, ...)
299 va_list args;
300 buf_head_t linebuf;
302 /* send remote if to->from non NULL */
303 if(target_p->from != NULL)
304 target_p = target_p->from;
306 if(IsIOError(target_p))
307 return;
309 linebuf_newbuf(&linebuf);
311 va_start(args, pattern);
312 linebuf_putmsg(&linebuf, pattern, &args, NULL);
313 va_end(args);
315 _send_linebuf(target_p, &linebuf);
317 linebuf_donebuf(&linebuf);
321 /* sendto_one_prefix()
323 * inputs - client to send to, va_args
324 * outputs - client has message put into its queue
325 * side effects - source(us)/target is chosen based on TS6 capability
327 void
328 sendto_one_prefix(struct Client *target_p, struct Client *source_p,
329 const char *command, const char *pattern, ...)
331 struct Client *dest_p;
332 va_list args;
333 buf_head_t linebuf;
335 /* send remote if to->from non NULL */
336 if(target_p->from != NULL)
337 dest_p = target_p->from;
338 else
339 dest_p = target_p;
341 if(IsIOError(dest_p))
342 return;
344 if(IsMe(dest_p))
346 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!");
347 return;
350 linebuf_newbuf(&linebuf);
351 va_start(args, pattern);
352 linebuf_putmsg(&linebuf, pattern, &args,
353 ":%s %s %s ",
354 get_id(source_p, target_p),
355 command, get_id(target_p, target_p));
356 va_end(args);
358 _send_linebuf(dest_p, &linebuf);
359 linebuf_donebuf(&linebuf);
362 /* sendto_one_notice()
364 * inputs - client to send to, va_args
365 * outputs - client has a NOTICE put into its queue
366 * side effects - source(us)/target is chosen based on TS6 capability
368 void
369 sendto_one_notice(struct Client *target_p, const char *pattern, ...)
371 struct Client *dest_p;
372 va_list args;
373 buf_head_t linebuf;
375 /* send remote if to->from non NULL */
376 if(target_p->from != NULL)
377 dest_p = target_p->from;
378 else
379 dest_p = target_p;
381 if(IsIOError(dest_p))
382 return;
384 if(IsMe(dest_p))
386 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!");
387 return;
390 linebuf_newbuf(&linebuf);
391 va_start(args, pattern);
392 linebuf_putmsg(&linebuf, pattern, &args,
393 ":%s NOTICE %s ",
394 get_id(&me, target_p), get_id(target_p, target_p));
395 va_end(args);
397 _send_linebuf(dest_p, &linebuf);
398 linebuf_donebuf(&linebuf);
402 /* sendto_one_numeric()
404 * inputs - client to send to, va_args
405 * outputs - client has message put into its queue
406 * side effects - source/target is chosen based on TS6 capability
408 void
409 sendto_one_numeric(struct Client *target_p, int numeric, const char *pattern, ...)
411 struct Client *dest_p;
412 va_list args;
413 buf_head_t linebuf;
415 /* send remote if to->from non NULL */
416 if(target_p->from != NULL)
417 dest_p = target_p->from;
418 else
419 dest_p = target_p;
421 if(IsIOError(dest_p))
422 return;
424 if(IsMe(dest_p))
426 sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!");
427 return;
430 linebuf_newbuf(&linebuf);
431 va_start(args, pattern);
432 linebuf_putmsg(&linebuf, pattern, &args,
433 ":%s %03d %s ",
434 get_id(&me, target_p),
435 numeric, get_id(target_p, target_p));
436 va_end(args);
438 _send_linebuf(dest_p, &linebuf);
439 linebuf_donebuf(&linebuf);
443 * sendto_server
445 * inputs - pointer to client to NOT send to
446 * - caps or'd together which must ALL be present
447 * - caps or'd together which must ALL NOT be present
448 * - printf style format string
449 * - args to format string
450 * output - NONE
451 * side effects - Send a message to all connected servers, except the
452 * client 'one' (if non-NULL), as long as the servers
453 * support ALL capabs in 'caps', and NO capabs in 'nocaps'.
455 * This function was written in an attempt to merge together the other
456 * billion sendto_*serv*() functions, which sprung up with capabs, uids etc
457 * -davidt
459 void
460 sendto_server(struct Client *one, struct Channel *chptr, unsigned long caps,
461 unsigned long nocaps, const char *format, ...)
463 va_list args;
464 struct Client *target_p;
465 dlink_node *ptr;
466 dlink_node *next_ptr;
467 buf_head_t linebuf;
469 /* noone to send to.. */
470 if(dlink_list_length(&serv_list) == 0)
471 return;
473 if(chptr != NULL && *chptr->chname != '#')
474 return;
476 linebuf_newbuf(&linebuf);
477 va_start(args, format);
478 linebuf_putmsg(&linebuf, format, &args, NULL);
479 va_end(args);
481 DLINK_FOREACH_SAFE(ptr, next_ptr, serv_list.head)
483 target_p = ptr->data;
485 /* check against 'one' */
486 if(one != NULL && (target_p == one->from))
487 continue;
489 /* check we have required capabs */
490 if(!IsCapable(target_p, caps))
491 continue;
493 /* check we don't have any forbidden capabs */
494 if(!NotCapable(target_p, nocaps))
495 continue;
497 _send_linebuf(target_p, &linebuf);
500 linebuf_donebuf(&linebuf);
504 /* sendto_channel_flags()
506 * inputs - server not to send to, flags needed, source, channel, va_args
507 * outputs - message is sent to channel members
508 * side effects -
510 void
511 sendto_channel_flags(struct Client *one, int type, struct Client *source_p,
512 struct Channel *chptr, const char *pattern, ...)
514 static char buf[BUFSIZE];
515 va_list args;
516 buf_head_t linebuf_local;
517 buf_head_t linebuf_name;
518 buf_head_t linebuf_id;
519 struct Client *target_p;
520 struct membership *msptr;
521 dlink_node *ptr;
522 dlink_node *next_ptr;
524 linebuf_newbuf(&linebuf_local);
525 linebuf_newbuf(&linebuf_name);
526 linebuf_newbuf(&linebuf_id);
528 current_serial++;
530 va_start(args, pattern);
531 ircvsnprintf(buf, sizeof(buf), pattern, args);
532 va_end(args);
534 if(IsServer(source_p))
535 linebuf_putmsg(&linebuf_local, NULL, NULL,
536 ":%s %s", source_p->name, buf);
537 else
538 linebuf_putmsg(&linebuf_local, NULL, NULL,
539 ":%s!%s@%s %s",
540 source_p->name, source_p->username,
541 source_p->host, buf);
543 linebuf_putmsg(&linebuf_name, NULL, NULL, ":%s %s", source_p->name, buf);
544 linebuf_putmsg(&linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);
546 DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->members.head)
548 msptr = ptr->data;
549 target_p = msptr->client_p;
551 if(IsIOError(target_p->from) || target_p->from == one)
552 continue;
554 if(type && ((msptr->flags & type) == 0))
555 continue;
557 if(IsDeaf(target_p))
558 continue;
560 if(!MyClient(target_p))
562 /* if we've got a specific type, target must support
563 * CHW.. --fl
565 if(type && NotCapable(target_p->from, CAP_CHW))
566 continue;
568 if(target_p->from->serial != current_serial)
570 if(has_id(target_p->from))
571 send_linebuf_remote(target_p, source_p, &linebuf_id);
572 else
573 send_linebuf_remote(target_p, source_p, &linebuf_name);
575 target_p->from->serial = current_serial;
578 else
579 _send_linebuf(target_p, &linebuf_local);
582 linebuf_donebuf(&linebuf_local);
583 linebuf_donebuf(&linebuf_name);
584 linebuf_donebuf(&linebuf_id);
588 /* sendto_channel_local()
590 * inputs - flags to send to, channel to send to, va_args
591 * outputs - message to local channel members
592 * side effects -
594 void
595 sendto_channel_local(int type, struct Channel *chptr, const char *pattern, ...)
597 va_list args;
598 buf_head_t linebuf;
599 struct membership *msptr;
600 struct Client *target_p;
601 dlink_node *ptr;
602 dlink_node *next_ptr;
604 linebuf_newbuf(&linebuf);
606 va_start(args, pattern);
607 linebuf_putmsg(&linebuf, pattern, &args, NULL);
608 va_end(args);
610 DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
612 msptr = ptr->data;
613 target_p = msptr->client_p;
615 if(IsIOError(target_p))
616 continue;
618 if(type && ((msptr->flags & type) == 0))
619 continue;
621 _send_linebuf(target_p, &linebuf);
624 linebuf_donebuf(&linebuf);
627 /* sendto_channel_local_butone()
629 * inputs - flags to send to, channel to send to, va_args
630 * - user to ignore when sending
631 * outputs - message to local channel members
632 * side effects -
634 void
635 sendto_channel_local_butone(struct Client *one, int type, struct Channel *chptr, const char *pattern, ...)
637 va_list args;
638 buf_head_t linebuf;
639 struct membership *msptr;
640 struct Client *target_p;
641 dlink_node *ptr;
642 dlink_node *next_ptr;
644 linebuf_newbuf(&linebuf);
646 va_start(args, pattern);
647 linebuf_putmsg(&linebuf, pattern, &args, NULL);
648 va_end(args);
650 DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
652 msptr = ptr->data;
653 target_p = msptr->client_p;
655 if(target_p == one)
656 continue;
658 if(IsIOError(target_p))
659 continue;
661 if(type && ((msptr->flags & type) == 0))
662 continue;
664 _send_linebuf(target_p, &linebuf);
667 linebuf_donebuf(&linebuf);
671 * sendto_common_channels_local()
673 * inputs - pointer to client
674 * - pattern to send
675 * output - NONE
676 * side effects - Sends a message to all people on local server who are
677 * in same channel with user.
678 * used by m_nick.c and exit_one_client.
680 void
681 sendto_common_channels_local(struct Client *user, const char *pattern, ...)
683 va_list args;
684 dlink_node *ptr;
685 dlink_node *next_ptr;
686 dlink_node *uptr;
687 dlink_node *next_uptr;
688 struct Channel *chptr;
689 struct Client *target_p;
690 struct membership *msptr;
691 struct membership *mscptr;
692 buf_head_t linebuf;
694 linebuf_newbuf(&linebuf);
695 va_start(args, pattern);
696 linebuf_putmsg(&linebuf, pattern, &args, NULL);
697 va_end(args);
699 ++current_serial;
701 DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head)
703 mscptr = ptr->data;
704 chptr = mscptr->chptr;
706 DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head)
708 msptr = uptr->data;
709 target_p = msptr->client_p;
711 if(IsIOError(target_p) ||
712 target_p->serial == current_serial)
713 continue;
715 target_p->serial = current_serial;
716 send_linebuf(target_p, &linebuf);
720 /* this can happen when the user isnt in any channels, but we still
721 * need to send them the data, ie a nick change
723 if(MyConnect(user) && (user->serial != current_serial))
724 send_linebuf(user, &linebuf);
726 linebuf_donebuf(&linebuf);
730 * sendto_common_channels_local_butone()
732 * inputs - pointer to client
733 * - pattern to send
734 * output - NONE
735 * side effects - Sends a message to all people on local server who are
736 * in same channel with user, except for user itself.
738 void
739 sendto_common_channels_local_butone(struct Client *user, const char *pattern, ...)
741 va_list args;
742 dlink_node *ptr;
743 dlink_node *next_ptr;
744 dlink_node *uptr;
745 dlink_node *next_uptr;
746 struct Channel *chptr;
747 struct Client *target_p;
748 struct membership *msptr;
749 struct membership *mscptr;
750 buf_head_t linebuf;
752 linebuf_newbuf(&linebuf);
753 va_start(args, pattern);
754 linebuf_putmsg(&linebuf, pattern, &args, NULL);
755 va_end(args);
757 ++current_serial;
758 /* Skip them -- jilles */
759 user->serial = current_serial;
761 DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head)
763 mscptr = ptr->data;
764 chptr = mscptr->chptr;
766 DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head)
768 msptr = uptr->data;
769 target_p = msptr->client_p;
771 if(IsIOError(target_p) ||
772 target_p->serial == current_serial)
773 continue;
775 target_p->serial = current_serial;
776 send_linebuf(target_p, &linebuf);
780 linebuf_donebuf(&linebuf);
783 /* sendto_match_butone()
785 * inputs - server not to send to, source, mask, type of mask, va_args
786 * output -
787 * side effects - message is sent to matching clients
789 void
790 sendto_match_butone(struct Client *one, struct Client *source_p,
791 const char *mask, int what, const char *pattern, ...)
793 static char buf[BUFSIZE];
794 va_list args;
795 struct Client *target_p;
796 dlink_node *ptr;
797 dlink_node *next_ptr;
798 buf_head_t linebuf_local;
799 buf_head_t linebuf_name;
800 buf_head_t linebuf_id;
802 linebuf_newbuf(&linebuf_local);
803 linebuf_newbuf(&linebuf_name);
804 linebuf_newbuf(&linebuf_id);
806 va_start(args, pattern);
807 ircvsnprintf(buf, sizeof(buf), pattern, args);
808 va_end(args);
810 if(IsServer(source_p))
811 linebuf_putmsg(&linebuf_local, NULL, NULL,
812 ":%s %s", source_p->name, buf);
813 else
814 linebuf_putmsg(&linebuf_local, NULL, NULL,
815 ":%s!%s@%s %s",
816 source_p->name, source_p->username,
817 source_p->host, buf);
819 linebuf_putmsg(&linebuf_name, NULL, NULL, ":%s %s", source_p->name, buf);
820 linebuf_putmsg(&linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf);
822 if(what == MATCH_HOST)
824 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
826 target_p = ptr->data;
828 if(match(mask, target_p->host))
829 _send_linebuf(target_p, &linebuf_local);
832 /* what = MATCH_SERVER, if it doesnt match us, just send remote */
833 else if(match(mask, me.name))
835 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
837 target_p = ptr->data;
838 _send_linebuf(target_p, &linebuf_local);
842 DLINK_FOREACH(ptr, serv_list.head)
844 target_p = ptr->data;
846 if(target_p == one)
847 continue;
849 if(has_id(target_p))
850 send_linebuf_remote(target_p, source_p, &linebuf_id);
851 else
852 send_linebuf_remote(target_p, source_p, &linebuf_name);
855 linebuf_donebuf(&linebuf_local);
856 linebuf_donebuf(&linebuf_id);
857 linebuf_donebuf(&linebuf_name);
860 /* sendto_match_servs()
862 * inputs - source, mask to send to, caps needed, va_args
863 * outputs -
864 * side effects - message is sent to matching servers with caps.
866 void
867 sendto_match_servs(struct Client *source_p, const char *mask, int cap,
868 int nocap, const char *pattern, ...)
870 static char buf[BUFSIZE];
871 va_list args;
872 dlink_node *ptr;
873 struct Client *target_p;
874 buf_head_t linebuf_id;
875 buf_head_t linebuf_name;
877 if(EmptyString(mask))
878 return;
880 linebuf_newbuf(&linebuf_id);
881 linebuf_newbuf(&linebuf_name);
883 va_start(args, pattern);
884 ircvsnprintf(buf, sizeof(buf), pattern, args);
885 va_end(args);
887 linebuf_putmsg(&linebuf_id, NULL, NULL,
888 ":%s %s", use_id(source_p), buf);
889 linebuf_putmsg(&linebuf_name, NULL, NULL,
890 ":%s %s", source_p->name, buf);
892 current_serial++;
894 DLINK_FOREACH(ptr, global_serv_list.head)
896 target_p = ptr->data;
898 /* dont send to ourselves, or back to where it came from.. */
899 if(IsMe(target_p) || target_p->from == source_p->from)
900 continue;
902 if(target_p->from->serial == current_serial)
903 continue;
905 if(match(mask, target_p->name))
907 /* if we set the serial here, then we'll never do
908 * a match() again if !IsCapable()
910 target_p->from->serial = current_serial;
912 if(cap && !IsCapable(target_p->from, cap))
913 continue;
915 if(nocap && !NotCapable(target_p->from, nocap))
916 continue;
918 if(has_id(target_p->from))
919 _send_linebuf(target_p->from, &linebuf_id);
920 else
921 _send_linebuf(target_p->from, &linebuf_name);
925 linebuf_donebuf(&linebuf_id);
926 linebuf_donebuf(&linebuf_name);
929 /* sendto_anywhere()
931 * inputs - target, source, va_args
932 * outputs -
933 * side effects - client is sent message with correct prefix.
935 void
936 sendto_anywhere(struct Client *target_p, struct Client *source_p,
937 const char *command, const char *pattern, ...)
939 va_list args;
940 buf_head_t linebuf;
942 linebuf_newbuf(&linebuf);
944 va_start(args, pattern);
946 if(MyClient(target_p))
948 if(IsServer(source_p))
949 linebuf_putmsg(&linebuf, pattern, &args, ":%s %s %s ",
950 source_p->name, command,
951 target_p->name);
952 else
953 linebuf_putmsg(&linebuf, pattern, &args,
954 ":%s!%s@%s %s %s ",
955 source_p->name, source_p->username,
956 source_p->host, command,
957 target_p->name);
959 else
960 linebuf_putmsg(&linebuf, pattern, &args, ":%s %s %s ",
961 get_id(source_p, target_p), command,
962 get_id(target_p, target_p));
963 va_end(args);
965 if(MyClient(target_p))
966 _send_linebuf(target_p, &linebuf);
967 else
968 send_linebuf_remote(target_p, source_p, &linebuf);
970 linebuf_donebuf(&linebuf);
973 /* sendto_realops_flags()
975 * inputs - umode needed, level (opers/admin), va_args
976 * output -
977 * side effects - message is sent to opers with matching umodes
979 void
980 sendto_realops_flags(int flags, int level, const char *pattern, ...)
982 struct Client *client_p;
983 dlink_node *ptr;
984 dlink_node *next_ptr;
985 va_list args;
986 buf_head_t linebuf;
988 linebuf_newbuf(&linebuf);
990 va_start(args, pattern);
991 linebuf_putmsg(&linebuf, pattern, &args,
992 ":%s NOTICE * :*** Notice -- ", me.name);
993 va_end(args);
995 DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head)
997 client_p = ptr->data;
999 /* If we're sending it to opers and theyre an admin, skip.
1000 * If we're sending it to admins, and theyre not, skip.
1002 if(((level == L_ADMIN) && !IsOperAdmin(client_p)) ||
1003 ((level == L_OPER) && IsOperAdmin(client_p)))
1004 continue;
1006 if(client_p->umodes & flags)
1007 _send_linebuf(client_p, &linebuf);
1010 linebuf_donebuf(&linebuf);
1013 /* sendto_realops_snomask()
1015 * inputs - snomask needed, level (opers/admin), va_args
1016 * output -
1017 * side effects - message is sent to opers with matching snomasks
1019 void
1020 sendto_realops_snomask(int flags, int level, const char *pattern, ...)
1022 static char buf[BUFSIZE];
1023 char *snobuf;
1024 struct Client *client_p;
1025 dlink_node *ptr;
1026 dlink_node *next_ptr;
1027 va_list args;
1028 buf_head_t linebuf;
1030 linebuf_newbuf(&linebuf);
1032 /* Be very sure not to do things like "Trying to send to myself"
1033 * L_NETWIDE, otherwise infinite recursion may result! -- jilles */
1034 if (level & L_NETWIDE && ConfigFileEntry.global_snotices)
1036 /* rather a lot of copying around, oh well -- jilles */
1037 va_start(args, pattern);
1038 ircvsnprintf(buf, sizeof(buf), pattern, args);
1039 va_end(args);
1040 linebuf_putmsg(&linebuf, pattern, NULL,
1041 ":%s NOTICE * :*** Notice -- %s", me.name, buf);
1042 snobuf = construct_snobuf(flags);
1043 if (snobuf[1] != '\0')
1045 sendto_server(NULL, NULL, CAP_ENCAP|CAP_TS6, NOCAPS,
1046 ":%s ENCAP * SNOTE %c :%s",
1047 me.id, snobuf[1], buf);
1048 sendto_server(NULL, NULL, CAP_ENCAP, CAP_TS6,
1049 ":%s ENCAP * SNOTE %c :%s",
1050 me.name, snobuf[1], buf);
1053 else
1055 va_start(args, pattern);
1056 linebuf_putmsg(&linebuf, pattern, &args,
1057 ":%s NOTICE * :*** Notice -- ", me.name);
1058 va_end(args);
1060 level &= ~L_NETWIDE;
1062 DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head)
1064 client_p = ptr->data;
1066 /* If we're sending it to opers and theyre an admin, skip.
1067 * If we're sending it to admins, and theyre not, skip.
1069 if(((level == L_ADMIN) && !IsOperAdmin(client_p)) ||
1070 ((level == L_OPER) && IsOperAdmin(client_p)))
1071 continue;
1073 if(client_p->snomask & flags)
1074 _send_linebuf(client_p, &linebuf);
1077 linebuf_donebuf(&linebuf);
1079 /* sendto_realops_snomask_from()
1081 * inputs - snomask needed, level (opers/admin), source server, va_args
1082 * output -
1083 * side effects - message is sent to opers with matching snomask
1085 void
1086 sendto_realops_snomask_from(int flags, int level, struct Client *source_p,
1087 const char *pattern, ...)
1089 struct Client *client_p;
1090 dlink_node *ptr;
1091 dlink_node *next_ptr;
1092 va_list args;
1093 buf_head_t linebuf;
1095 linebuf_newbuf(&linebuf);
1097 va_start(args, pattern);
1098 linebuf_putmsg(&linebuf, pattern, &args,
1099 ":%s NOTICE * :*** Notice -- ", source_p->name);
1100 va_end(args);
1102 DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head)
1104 client_p = ptr->data;
1106 /* If we're sending it to opers and theyre an admin, skip.
1107 * If we're sending it to admins, and theyre not, skip.
1109 if(((level == L_ADMIN) && !IsOperAdmin(client_p)) ||
1110 ((level == L_OPER) && IsOperAdmin(client_p)))
1111 continue;
1113 if(client_p->snomask & flags)
1114 _send_linebuf(client_p, &linebuf);
1117 linebuf_donebuf(&linebuf);
1121 * sendto_wallops_flags
1123 * inputs - flag types of messages to show to real opers
1124 * - client sending request
1125 * - var args input message
1126 * output - NONE
1127 * side effects - Send a wallops to local opers
1129 void
1130 sendto_wallops_flags(int flags, struct Client *source_p, const char *pattern, ...)
1132 struct Client *client_p;
1133 dlink_node *ptr;
1134 dlink_node *next_ptr;
1135 va_list args;
1136 buf_head_t linebuf;
1138 linebuf_newbuf(&linebuf);
1140 va_start(args, pattern);
1142 if(IsPerson(source_p))
1143 linebuf_putmsg(&linebuf, pattern, &args,
1144 ":%s!%s@%s WALLOPS :", source_p->name,
1145 source_p->username, source_p->host);
1146 else
1147 linebuf_putmsg(&linebuf, pattern, &args, ":%s WALLOPS :", source_p->name);
1149 va_end(args);
1151 DLINK_FOREACH_SAFE(ptr, next_ptr, IsPerson(source_p) && flags == UMODE_WALLOP ? lclient_list.head : local_oper_list.head)
1153 client_p = ptr->data;
1155 if(client_p->umodes & flags)
1156 _send_linebuf(client_p, &linebuf);
1159 linebuf_donebuf(&linebuf);
1162 /* kill_client()
1164 * input - client to send kill to, client to kill, va_args
1165 * output -
1166 * side effects - we issue a kill for the client
1168 void
1169 kill_client(struct Client *target_p, struct Client *diedie, const char *pattern, ...)
1171 va_list args;
1172 buf_head_t linebuf;
1174 linebuf_newbuf(&linebuf);
1176 va_start(args, pattern);
1177 linebuf_putmsg(&linebuf, pattern, &args, ":%s KILL %s :",
1178 get_id(&me, target_p), get_id(diedie, target_p));
1179 va_end(args);
1181 send_linebuf(target_p, &linebuf);
1182 linebuf_donebuf(&linebuf);
1187 * kill_client_serv_butone
1189 * inputs - pointer to client to not send to
1190 * - pointer to client to kill
1191 * output - NONE
1192 * side effects - Send a KILL for the given client
1193 * message to all connected servers
1194 * except the client 'one'. Also deal with
1195 * client being unknown to leaf, as in lazylink...
1197 void
1198 kill_client_serv_butone(struct Client *one, struct Client *target_p, const char *pattern, ...)
1200 static char buf[BUFSIZE];
1201 va_list args;
1202 struct Client *client_p;
1203 dlink_node *ptr;
1204 dlink_node *next_ptr;
1205 buf_head_t linebuf_id;
1206 buf_head_t linebuf_name;
1208 linebuf_newbuf(&linebuf_name);
1209 linebuf_newbuf(&linebuf_id);
1211 va_start(args, pattern);
1212 ircvsnprintf(buf, sizeof(buf), pattern, args);
1213 va_end(args);
1215 linebuf_putmsg(&linebuf_name, NULL, NULL, ":%s KILL %s :%s",
1216 me.name, target_p->name, buf);
1217 linebuf_putmsg(&linebuf_id, NULL, NULL, ":%s KILL %s :%s",
1218 use_id(&me), use_id(target_p), buf);
1220 DLINK_FOREACH_SAFE(ptr, next_ptr, serv_list.head)
1222 client_p = ptr->data;
1224 /* ok, if the client we're supposed to not send to has an
1225 * ID, then we still want to issue the kill there..
1227 if(one != NULL && (client_p == one->from) &&
1228 (!has_id(client_p) || !has_id(target_p)))
1229 continue;
1231 if(has_id(client_p))
1232 _send_linebuf(client_p, &linebuf_id);
1233 else
1234 _send_linebuf(client_p, &linebuf_name);
1237 linebuf_donebuf(&linebuf_id);
1238 linebuf_donebuf(&linebuf_name);