send QUIT's only to users seeing the leaving user on MODE_AUDITORIUM channels
[ircu2.10.12-pk.git] / ircd / send.c
1 /*
2  * IRC - Internet Relay Chat, common/send.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 1, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 /** @file
21  * @brief Send messages to certain targets.
22  * @version $Id: send.c 1835 2007-10-30 01:14:50Z entrope $
23  */
24 #include "config.h"
25
26 #include "send.h"
27 #include "channel.h"
28 #include "class.h"
29 #include "client.h"
30 #include "ircd.h"
31 #include "ircd_features.h"
32 #include "ircd_log.h"
33 #include "ircd_snprintf.h"
34 #include "ircd_string.h"
35 #include "list.h"
36 #include "match.h"
37 #include "msg.h"
38 #include "hash.h"
39 #include "numnicks.h"
40 #include "parse.h"
41 #include "s_bsd.h"
42 #include "s_debug.h"
43 #include "s_misc.h"
44 #include "s_user.h"
45 #include "struct.h"
46 #include "sys.h"
47
48 /* #include <assert.h> -- Now using assert in ircd_log.h */
49 #include <stdio.h>
50 #include <string.h>
51
52 /** Last used marker value. */
53 static int sentalong_marker;
54 /** Array of users with the corresponding server notice mask bit set. */
55 struct SLink *opsarray[32];     /* don't use highest bit unless you change
56                                    atoi to strtoul in sendto_op_mask() */
57 /** Linked list of all connections with data queued to send. */
58 static struct Connection *send_queues;
59 char *GlobalForwards[256];
60
61 /*
62  * dead_link
63  *
64  * An error has been detected. The link *must* be closed,
65  * but *cannot* call ExitClient (m_bye) from here.
66  * Instead, mark it with FLAG_DEADSOCKET. This should
67  * generate ExitClient from the main loop.
68  *
69  * If 'notice' is not NULL, it is assumed to be a format
70  * for a message to local opers. It can contain only one
71  * '%s', which will be replaced by the sockhost field of
72  * the failing link.
73  *
74  * Also, the notice is skipped for "uninteresting" cases,
75  * like Persons and yet unknown connections...
76  */
77 /** Mark a client as dead, even if they are not the current message source.
78  * This is done by setting the DEADSOCKET flag on the user and letting the
79  * main loop perform the actual exit logic.
80  * @param[in,out] to Client being killed.
81  * @param[in] notice Message for local opers.
82  */
83 static void dead_link(struct Client *to, char *notice)
84 {
85   SetFlag(to, FLAG_DEADSOCKET);
86   /*
87    * If because of BUFFERPOOL problem then clean dbuf's now so that
88    * notices don't hurt operators below.
89    */
90   DBufClear(&(cli_recvQ(to)));
91   MsgQClear(&(cli_sendQ(to)));
92   client_drop_sendq(cli_connect(to));
93
94   /*
95    * Keep a copy of the last comment, for later use...
96    */
97   ircd_strncpy(cli_info(to), notice, REALLEN);
98
99   if (!IsUser(to) && !IsUnknown(to) && !HasFlag(to, FLAG_CLOSING))
100     sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
101   Debug((DEBUG_ERROR, cli_info(to)));
102 }
103
104 /** Test whether we can send to a client.
105  * @param[in] to Client we want to send to.
106  * @return Non-zero if we can send to the client.
107  */
108 static int can_send(struct Client* to)
109 {
110   assert(0 != to);
111   return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
112 }
113
114 /** Close the connection with the highest sendq.
115  * This should be called when we need to free buffer memory.
116  * @param[in] servers_too If non-zero, consider killing servers, too.
117  */
118 void
119 kill_highest_sendq(int servers_too)
120 {
121   int i;
122   unsigned int highest_sendq = 0;
123   struct Client *highest_client = 0;
124
125   for (i = HighestFd; i >= 0; i--)
126   {
127     if (!LocalClientArray[i] || (!servers_too && cli_serv(LocalClientArray[i])))
128       continue; /* skip servers */
129     
130     /* If this sendq is higher than one we last saw, remember it */
131     if (MsgQLength(&(cli_sendQ(LocalClientArray[i]))) > highest_sendq)
132     {
133       highest_client = LocalClientArray[i];
134       highest_sendq = MsgQLength(&(cli_sendQ(highest_client)));
135     }
136   }
137
138   if (highest_client)
139     dead_link(highest_client, "Buffer allocation error");
140 }
141
142 /*
143  * flush_connections
144  *
145  * Used to empty all output buffers for all connections. Should only
146  * be called once per scan of connections. There should be a select in
147  * here perhaps but that means either forcing a timeout or doing a poll.
148  * When flushing, all we do is empty the obuffer array for each local
149  * client and try to send it. if we cant send it, it goes into the sendQ
150  * -avalon
151  */
152 /** Flush data queued for one or all connections.
153  * @param[in] cptr Client to flush (if NULL, do all).
154  */
155 void flush_connections(struct Client* cptr)
156 {
157   if (cptr) {
158     send_queued(cptr);
159   }
160   else {
161     struct Connection* con;
162     for (con = send_queues; con; con = con_next(con)) {
163       assert(0 < MsgQLength(&(con_sendQ(con))));
164       send_queued(con_client(con));
165     }
166   }
167 }
168
169 /*
170  * send_queued
171  *
172  * This function is called from the main select-loop (or whatever)
173  * when there is a chance that some output would be possible. This
174  * attempts to empty the send queue as far as possible...
175  */
176 /** Attempt to send data queued for a client.
177  * @param[in] to Client to send data to.
178  */
179 void send_queued(struct Client *to)
180 {
181   assert(0 != to);
182   assert(0 != cli_local(to));
183
184   if (IsBlocked(to) || !can_send(to))
185     return;                     /* Don't bother */
186
187   while (MsgQLength(&(cli_sendQ(to))) > 0) {
188     unsigned int len;
189
190     if ((len = deliver_it(to, &(cli_sendQ(to))))) {
191       msgq_delete(&(cli_sendQ(to)), len);
192       cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
193       if (IsBlocked(to)) {
194         update_write(to);
195         return;
196       }
197     }
198     else {
199       if (IsDead(to)) {
200         char tmp[512];
201         sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
202         dead_link(to, tmp);
203       }
204       return;
205     }
206   }
207
208   /* Ok, sendq is now empty... */
209   client_drop_sendq(cli_connect(to));
210   update_write(to);
211 }
212
213 /** Try to send a buffer to a client, queueing it if needed.
214  * @param[in,out] to Client to send message to.
215  * @param[in] buf Message to send.
216  * @param[in] prio If non-zero, send as high priority.
217  */
218 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
219 {
220   assert(0 != to);
221   assert(0 != buf);
222
223   if (cli_from(to))
224     to = cli_from(to);
225
226   if (!can_send(to))
227     /*
228      * This socket has already been marked as dead
229      */
230     return;
231
232   if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
233     if (IsServer(to))
234       sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
235                            "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
236                            get_sendq(to));
237     dead_link(to, "Max sendQ exceeded");
238     return;
239   }
240
241   Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
242
243   msgq_add(&(cli_sendQ(to)), buf, prio);
244   client_add_sendq(cli_connect(to), &send_queues);
245   update_write(to);
246
247   /*
248    * Update statistics. The following is slightly incorrect
249    * because it counts messages even if queued, but bytes
250    * only really sent. Queued bytes get updated in SendQueued.
251    */
252   ++(cli_sendM(to));
253   ++(cli_sendM(&me));
254   /*
255    * This little bit is to stop the sendQ from growing too large when
256    * there is no need for it to. Thus we call send_queued() every time
257    * 2k has been added to the queue since the last non-fatal write.
258    * Also stops us from deliberately building a large sendQ and then
259    * trying to flood that link with data (possible during the net
260    * relinking done by servers with a large load).
261    */
262   if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
263     send_queued(to);
264 }
265
266 /*
267  * Send a msg to all ppl on servers/hosts that match a specified mask
268  * (used for enhanced PRIVMSGs)
269  *
270  *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
271  */
272
273 /** Check whether a client matches a target mask.
274  * @param[in] from Client trying to send a message (ignored).
275  * @param[in] one Client being considered as a target.
276  * @param[in] mask Mask for matching against.
277  * @param[in] what Type of match (either MATCH_HOST or MATCH_SERVER).
278  * @return Non-zero if \a one matches, zero if not.
279  */
280 static int match_it(struct Client *from, struct Client *one, const char *mask, int what)
281 {
282   switch (what)
283   {
284     case MATCH_HOST:
285       return (match(mask, cli_user(one)->host) == 0 ||
286         (HasHiddenHost(one) && match(mask, cli_user(one)->realhost) == 0));
287     case MATCH_SERVER:
288     default:
289       return (match(mask, cli_name(cli_user(one)->server)) == 0);
290   }
291 }
292
293 /** Send an unprefixed line to a client.
294  * @param[in] to Client receiving message.
295  * @param[in] pattern Format string of message.
296  */
297 void sendrawto_one(struct Client *to, const char *pattern, ...)
298 {
299   struct MsgBuf *mb;
300   va_list vl;
301
302   va_start(vl, pattern);
303   mb = msgq_vmake(to, pattern, vl);
304   va_end(vl);
305
306   send_buffer(to, mb, 0);
307
308   msgq_clean(mb);
309 }
310
311 /** Send a (prefixed) command to a single client.
312  * @param[in] from Client sending the command.
313  * @param[in] cmd Long name of command (used if \a to is a user).
314  * @param[in] tok Short name of command (used if \a to is a server).
315  * @param[in] to Destination of command.
316  * @param[in] pattern Format string for command arguments.
317  */
318 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
319                    struct Client *to, const char *pattern, ...)
320 {
321   struct VarData vd;
322   struct MsgBuf *mb;
323
324   to = cli_from(to);
325
326   vd.vd_format = pattern; /* set up the struct VarData for %v */
327   va_start(vd.vd_args, pattern);
328
329   mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
330                  &vd);
331
332   va_end(vd.vd_args);
333
334   send_buffer(to, mb, 0);
335
336   msgq_clean(mb);
337 }
338
339 /**
340  * Send a (prefixed) command to a single client in the priority queue.
341  * @param[in] from Client sending the command.
342  * @param[in] cmd Long name of command (used if \a to is a user).
343  * @param[in] tok Short name of command (used if \a to is a server).
344  * @param[in] to Destination of command.
345  * @param[in] pattern Format string for command arguments.
346  */
347 void sendcmdto_prio_one(struct Client *from, const char *cmd, const char *tok,
348                         struct Client *to, const char *pattern, ...)
349 {
350   struct VarData vd;
351   struct MsgBuf *mb;
352
353   to = cli_from(to);
354
355   vd.vd_format = pattern; /* set up the struct VarData for %v */
356   va_start(vd.vd_args, pattern);
357
358   mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
359                  &vd);
360
361   va_end(vd.vd_args);
362
363   send_buffer(to, mb, 1);
364
365   msgq_clean(mb);
366 }
367
368 /**
369  * Send a (prefixed) command to all servers matching or not matching a
370  * flag but one.
371  * @param[in] from Client sending the command.
372  * @param[in] cmd Long name of command (ignored).
373  * @param[in] tok Short name of command.
374  * @param[in] one Client direction to skip (or NULL).
375  * @param[in] require Only send to servers with this Flag bit set.
376  * @param[in] forbid Do not send to servers with this Flag bit set.
377  * @param[in] pattern Format string for command arguments.
378  */
379 void sendcmdto_flag_serv_butone(struct Client *from, const char *cmd,
380                                 const char *tok, struct Client *one,
381                                 int require, int forbid,
382                                 const char *pattern, ...)
383 {
384   struct VarData vd;
385   struct MsgBuf *mb;
386   struct DLink *lp;
387
388   vd.vd_format = pattern; /* set up the struct VarData for %v */
389   va_start(vd.vd_args, pattern);
390
391   /* use token */
392   mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
393   va_end(vd.vd_args);
394
395   /* send it to our downlinks */
396   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
397     if (one && lp->value.cptr == cli_from(one))
398       continue;
399     if ((require < FLAG_LAST_FLAG) && !HasFlag(lp->value.cptr, require))
400       continue;
401     if ((forbid < FLAG_LAST_FLAG) && HasFlag(lp->value.cptr, forbid))
402       continue;
403     send_buffer(lp->value.cptr, mb, 0);
404   }
405
406   msgq_clean(mb);
407 }
408
409 /**
410  * Send a (prefixed) command to all servers but one.
411  * @param[in] from Client sending the command.
412  * @param[in] cmd Long name of command (ignored).
413  * @param[in] tok Short name of command.
414  * @param[in] one Client direction to skip (or NULL).
415  * @param[in] pattern Format string for command arguments.
416  */
417 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
418                            const char *tok, struct Client *one,
419                            const char *pattern, ...)
420 {
421   struct VarData vd;
422   struct MsgBuf *mb;
423   struct DLink *lp;
424
425   vd.vd_format = pattern; /* set up the struct VarData for %v */
426   va_start(vd.vd_args, pattern);
427
428   /* use token */
429   mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
430   va_end(vd.vd_args);
431
432   /* send it to our downlinks */
433   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
434     if (one && lp->value.cptr == cli_from(one))
435       continue;
436     send_buffer(lp->value.cptr, mb, 0);
437   }
438
439   msgq_clean(mb);
440 }
441
442 /** Safely increment the sentalong marker.
443  * This increments the sentalong marker.  Since new connections will
444  * have con_sentalong() == 0, and to avoid confusion when the counter
445  * wraps, we reset all sentalong markers to zero when the sentalong
446  * marker hits zero.
447  * @param[in,out] one Client to mark with new sentalong marker (if any).
448  */
449 static void
450 bump_sentalong(struct Client *one)
451 {
452   if (!++sentalong_marker)
453   {
454     int ii;
455     for (ii = 0; ii < HighestFd; ++ii)
456       if (LocalClientArray[ii])
457         cli_sentalong(LocalClientArray[ii]) = 0;
458     ++sentalong_marker;
459   }
460   if (one)
461     cli_sentalong(one) = sentalong_marker;
462 }
463
464 /** Send a (prefixed) command to all channels that \a from is on.
465  * @param[in] from Client originating the command.
466  * @param[in] cmd Long name of command.
467  * @param[in] tok Short name of command.
468  * @param[in] one Client direction to skip (or NULL).
469  * @param[in] pattern Format string for command arguments.
470  */
471 void sendcmdto_common_channels_butone(struct Client *from, const char *cmd,
472                                       const char *tok, struct Client *one,
473                                       const char *pattern, ...)
474 {
475   struct VarData vd;
476   struct MsgBuf *mb;
477   struct Membership *chan;
478   struct Membership *member;
479
480   assert(0 != from);
481   assert(0 != cli_from(from));
482   assert(0 != pattern);
483   assert(!IsServer(from) && !IsMe(from));
484
485   vd.vd_format = pattern; /* set up the struct VarData for %v */
486
487   va_start(vd.vd_args, pattern);
488
489   /* build the buffer */
490   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
491   va_end(vd.vd_args);
492
493   bump_sentalong(from);
494   /*
495    * loop through from's channels, and the members on their channels
496    */
497   for (chan = cli_user(from)->channel; chan; chan = chan->next_channel) {
498     if (IsZombie(chan) || IsDelayedJoin(chan))
499       continue;
500     for (member = chan->channel->members; member;
501          member = member->next_member)
502       if (MyConnect(member->user)
503           && -1 < cli_fd(cli_from(member->user))
504           && member->user != one
505           && cli_sentalong(member->user) != sentalong_marker) {
506         cli_sentalong(member->user) = sentalong_marker;
507         send_buffer(member->user, mb, 0);
508       }
509   }
510
511   if (MyConnect(from) && from != one)
512     send_buffer(from, mb, 0);
513
514   msgq_clean(mb);
515 }
516
517 /** Send a (prefixed) command to all channels that \a from is on. (Check audit)
518  * @param[in] from Client originating the command.
519  * @param[in] cmd Long name of command.
520  * @param[in] tok Short name of command.
521  * @param[in] one Client direction to skip (or NULL).
522  * @param[in] pattern Format string for command arguments.
523  */
524 void sendcmdto_common_channels_butone_audit(struct Client *from, const char *cmd,
525                                       const char *tok, struct Client *one,
526                                       const char *pattern, ...)
527 {
528   struct VarData vd;
529   struct MsgBuf *mb;
530   struct Membership *chan;
531   struct Membership *member;
532
533   assert(0 != from);
534   assert(0 != cli_from(from));
535   assert(0 != pattern);
536   assert(!IsServer(from) && !IsMe(from));
537
538   vd.vd_format = pattern; /* set up the struct VarData for %v */
539
540   va_start(vd.vd_args, pattern);
541
542   /* build the buffer */
543   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
544   va_end(vd.vd_args);
545
546   bump_sentalong(from);
547   /*
548    * loop through from's channels, and the members on their channels
549    */
550   for (chan = cli_user(from)->channel; chan; chan = chan->next_channel) {
551     if (IsZombie(chan) || IsDelayedJoin(chan))
552       continue;
553     for (member = chan->channel->members; member;
554          member = member->next_member)
555       if (MyConnect(member->user)
556           && -1 < cli_fd(cli_from(member->user))
557           && member->user != one
558           && cli_sentalong(member->user) != sentalong_marker
559           && (!(chan->channel->mode.mode & MODE_AUDITORIUM) || IsVoicedOrOpped(chan) || IsChanOp(member))) {
560         cli_sentalong(member->user) = sentalong_marker;
561         send_buffer(member->user, mb, 0);
562       }
563   }
564
565   if (MyConnect(from) && from != one)
566     send_buffer(from, mb, 0);
567
568   msgq_clean(mb);
569 }
570
571 /** Send a (prefixed) command to all local users on a channel.
572  * @param[in] from Client originating the command.
573  * @param[in] cmd Long name of command.
574  * @param[in] tok Short name of command (ignored).
575  * @param[in] to Destination channel.
576  * @param[in] one Client direction to skip (or NULL).
577  * @param[in] skip Bitmask of SKIP_DEAF, SKIP_NONOPS, SKIP_NONVOICES indicating which clients to skip.
578  * @param[in] pattern Format string for command arguments.
579  */
580 void sendcmdto_channel_butserv_butone(struct Client *from, const char *cmd,
581                                       const char *tok, struct Channel *to,
582                                       struct Client *one, unsigned int skip,
583                                       const char *pattern, ...)
584 {
585   struct VarData vd;
586   struct MsgBuf *mb;
587   struct Membership *member;
588
589   vd.vd_format = pattern; /* set up the struct VarData for %v */
590   va_start(vd.vd_args, pattern);
591
592   /* build the buffer */
593   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
594   va_end(vd.vd_args);
595
596   /* send the buffer to each local channel member */
597   for (member = to->members; member; member = member->next_member) {
598     if (!MyConnect(member->user)
599         || member->user == one 
600         || IsZombie(member)
601         || (skip & SKIP_DEAF && IsDeaf(member->user))
602         || (skip & SKIP_NONOPS && !IsChanOp(member))
603                 || (skip & SKIP_OPS && IsChanOp(member))
604         || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
605         continue;
606       send_buffer(member->user, mb, 0);
607   }
608
609   msgq_clean(mb);
610 }
611
612 /** Send a (prefixed) command to all servers with users on \a to.
613  * Skip \a from and \a one plus those indicated in \a skip.
614  * @param[in] from Client originating the command.
615  * @param[in] cmd Long name of command (ignored).
616  * @param[in] tok Short name of command.
617  * @param[in] to Destination channel.
618  * @param[in] one Client direction to skip (or NULL).
619  * @param[in] skip Bitmask of SKIP_NONOPS and SKIP_NONVOICES indicating which clients to skip.
620  * @param[in] pattern Format string for command arguments.
621  */
622 void sendcmdto_channel_servers_butone(struct Client *from, const char *cmd,
623                                       const char *tok, struct Channel *to,
624                                       struct Client *one, unsigned int skip,
625                                       const char *pattern, ...)
626 {
627   struct VarData vd;
628   struct MsgBuf *serv_mb;
629   struct Membership *member;
630
631   /* build the buffer */
632   vd.vd_format = pattern;
633   va_start(vd.vd_args, pattern);
634   serv_mb = msgq_make(&me, "%:#C %s %v", from, tok, &vd);
635   va_end(vd.vd_args);
636
637   /* send the buffer to each server */
638   bump_sentalong(one);
639   cli_sentalong(from) = sentalong_marker;
640   for (member = to->members; member; member = member->next_member) {
641     if (MyConnect(member->user)
642         || IsZombie(member)
643         || cli_fd(cli_from(member->user)) < 0
644         || cli_sentalong(member->user) == sentalong_marker
645         || (skip & SKIP_NONOPS && !IsChanOp(member))
646                 || (skip & SKIP_OPS && IsChanOp(member))
647         || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
648       continue;
649     cli_sentalong(member->user) = sentalong_marker;
650     send_buffer(member->user, serv_mb, 0);
651   }
652   msgq_clean(serv_mb);
653 }
654
655
656 /** Send a (prefixed) command to all users on this channel, except for
657  * \a one and those matching \a skip.
658  * @warning \a pattern must not contain %v.
659  * @param[in] from Client originating the command.
660  * @param[in] cmd Long name of command.
661  * @param[in] tok Short name of command.
662  * @param[in] to Destination channel.
663  * @param[in] one Client direction to skip (or NULL).
664  * @param[in] skip Bitmask of SKIP_NONOPS, SKIP_NONVOICES, SKIP_DEAF, SKIP_BURST.
665  * @param[in] pattern Format string for command arguments.
666  */
667 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
668                               const char *tok, struct Channel *to,
669                               struct Client *one, unsigned int skip,
670                               unsigned char prefix, const char *pattern, ...)
671 {
672   struct Membership *member;
673   struct VarData vd;
674   struct MsgBuf *user_mb;
675   struct MsgBuf *serv_mb;
676   struct Client *service;
677
678   vd.vd_format = pattern;
679
680   /* Build buffer to send to users */
681   va_start(vd.vd_args, pattern);
682   user_mb = msgq_make(0, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? "%:#C %s @%v" : "%:#C %s %v",
683                       from, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? MSG_NOTICE : cmd, &vd);
684   va_end(vd.vd_args);
685
686   /* Build buffer to send to servers */
687   va_start(vd.vd_args, pattern);
688   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
689   va_end(vd.vd_args);
690
691   /* send buffer along! */
692   bump_sentalong(one);
693   for (member = to->members; member; member = member->next_member) {
694     /* skip one, zombies, and deaf users... */
695     if (IsZombie(member) ||
696         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
697         (skip & SKIP_NONOPS && !IsChanOp(member)) ||
698                 (skip & SKIP_OPS && IsChanOp(member)) ||
699         (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)) ||
700         (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
701         cli_fd(cli_from(member->user)) < 0 ||
702         cli_sentalong(member->user) == sentalong_marker)
703       continue;
704     cli_sentalong(member->user) = sentalong_marker;
705
706     if (MyConnect(member->user)) /* pick right buffer to send */
707       send_buffer(member->user, user_mb, 0);
708     else
709       send_buffer(member->user, serv_mb, 0);
710   }
711
712   /* Consult service forwarding table. */
713   if(GlobalForwards[prefix]
714       && (service = FindServer(GlobalForwards[prefix]))
715       && cli_sentalong(service) != sentalong_marker) {
716       cli_sentalong(service) = sentalong_marker;
717       send_buffer(service, serv_mb, 0);
718   }
719   
720   if(GlobalForwards['*']
721       && (service = FindServer(GlobalForwards['*']))
722       && cli_sentalong(service) != sentalong_marker) {
723       cli_sentalong(service) = sentalong_marker;
724       send_buffer(service, serv_mb, 0);
725   }
726
727   msgq_clean(user_mb);
728   msgq_clean(serv_mb);
729 }
730
731 /** Send a (prefixed) WALL of type \a type to all users except \a one.
732  * @warning \a pattern must not contain %v.
733  * @param[in] from Source of the command.
734  * @param[in] type One of WALL_DESYNCH, WALL_WALLOPS or WALL_WALLUSERS.
735  * @param[in] one Client direction to skip (or NULL).
736  * @param[in] pattern Format string for command arguments.
737  */
738 void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
739                              const char *pattern, ...)
740 {
741   struct VarData vd;
742   struct Client *cptr;
743   struct MsgBuf *mb;
744   struct DLink *lp;
745   char *prefix=NULL;
746   char *tok=NULL;
747   int his_wallops;
748   int i;
749
750   vd.vd_format = pattern;
751
752   /* Build buffer to send to users */
753   va_start(vd.vd_args, pattern);
754   switch (type) {
755         case WALL_DESYNCH:
756                 prefix="";
757                 tok=TOK_DESYNCH;
758                 break;
759         case WALL_WALLOPS:
760                 prefix="* ";
761                 tok=TOK_WALLOPS;
762                 break;
763         case WALL_WALLUSERS:
764                 prefix="$ ";
765                 tok=TOK_WALLUSERS;
766                 break;
767         default:
768                 assert(0);
769   }
770   mb = msgq_make(0, "%:#C " MSG_WALLOPS " :%s%v", from, prefix,&vd);
771   va_end(vd.vd_args);
772
773   /* send buffer along! */
774   his_wallops = feature_bool(FEAT_HIS_WALLOPS);
775   for (i = 0; i <= HighestFd; i++)
776   {
777     if (!(cptr = LocalClientArray[i]) ||
778         (cli_fd(cli_from(cptr)) < 0) ||
779         (type == WALL_DESYNCH && !SendDebug(cptr)) ||
780         (type == WALL_WALLOPS &&
781          (!SendWallops(cptr) || (his_wallops && !IsAnOper(cptr)))) ||
782         (type == WALL_WALLUSERS && !SendWallops(cptr)))
783       continue; /* skip it */
784     send_buffer(cptr, mb, 1);
785   }
786
787   msgq_clean(mb);
788
789   /* Build buffer to send to servers */
790   va_start(vd.vd_args, pattern);
791   mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
792   va_end(vd.vd_args);
793
794   /* send buffer along! */
795   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
796     if (one && lp->value.cptr == cli_from(one))
797       continue;
798     send_buffer(lp->value.cptr, mb, 1);
799   }
800
801   msgq_clean(mb);
802 }
803
804 /** Send a (prefixed) command to all users matching \a to as \a who.
805  * @warning \a pattern must not contain %v.
806  * @param[in] from Source of the command.
807  * @param[in] cmd Long name of command.
808  * @param[in] tok Short name of command.
809  * @param[in] to Destination host/server mask.
810  * @param[in] one Client direction to skip (or NULL).
811  * @param[in] who Type of match for \a to (either MATCH_HOST or MATCH_SERVER).
812  * @param[in] pattern Format string for command arguments.
813  */
814 void sendcmdto_match_butone(struct Client *from, const char *cmd,
815                             const char *tok, const char *to,
816                             struct Client *one, unsigned int who,
817                             const char *pattern, ...)
818 {
819   struct VarData vd;
820   struct Client *cptr;
821   struct MsgBuf *user_mb;
822   struct MsgBuf *serv_mb;
823
824   vd.vd_format = pattern;
825
826   /* Build buffer to send to users */
827   va_start(vd.vd_args, pattern);
828   user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
829   va_end(vd.vd_args);
830
831   /* Build buffer to send to servers */
832   va_start(vd.vd_args, pattern);
833   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
834   va_end(vd.vd_args);
835
836   /* send buffer along */
837   bump_sentalong(one);
838   for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
839     if (!IsRegistered(cptr) || IsServer(cptr) || cli_fd(cli_from(cptr)) < 0 ||
840         cli_sentalong(cptr) == sentalong_marker ||
841         !match_it(from, cptr, to, who))
842       continue; /* skip it */
843     cli_sentalong(cptr) = sentalong_marker;
844
845     if (MyConnect(cptr)) /* send right buffer */
846       send_buffer(cptr, user_mb, 0);
847     else
848       send_buffer(cptr, serv_mb, 0);
849   }
850
851   msgq_clean(user_mb);
852   msgq_clean(serv_mb);
853 }
854
855 /** Send a server notice to all users subscribing to the indicated \a
856  * mask except for \a one.
857  * @param[in] one Client direction to skip (or NULL).
858  * @param[in] mask One of the SNO_* constants.
859  * @param[in] pattern Format string for server notice.
860  */
861 void sendto_opmask_butone(struct Client *one, unsigned int mask,
862                           const char *pattern, ...)
863 {
864   va_list vl;
865
866   va_start(vl, pattern);
867   vsendto_opmask_butone(one, mask, pattern, vl);
868   va_end(vl);
869 }
870
871 /** Send a server notice to all users subscribing to the indicated \a
872  * mask except for \a one, rate-limited to once per 30 seconds.
873  * @param[in] one Client direction to skip (or NULL).
874  * @param[in] mask One of the SNO_* constants.
875  * @param[in,out] rate Pointer to the last time the message was sent.
876  * @param[in] pattern Format string for server notice.
877  */
878 void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
879                                       time_t *rate, const char *pattern, ...)
880 {
881   va_list vl;
882
883   if ((CurrentTime - *rate) < 30)
884     return;
885   else
886     *rate = CurrentTime;
887
888   va_start(vl, pattern);
889   vsendto_opmask_butone(one, mask, pattern, vl);
890   va_end(vl);
891 }
892
893
894 /** Send a server notice to all users subscribing to the indicated \a
895  * mask except for \a one.
896  * @param[in] one Client direction to skip (or NULL).
897  * @param[in] mask One of the SNO_* constants.
898  * @param[in] pattern Format string for server notice.
899  * @param[in] vl Argument list for format string.
900  */
901 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
902                            const char *pattern, va_list vl)
903 {
904   struct VarData vd;
905   struct MsgBuf *mb;
906   int i = 0; /* so that 1 points to opsarray[0] */
907   struct SLink *opslist;
908
909   while ((mask >>= 1))
910     i++;
911
912   if (!(opslist = opsarray[i]))
913     return;
914
915   /*
916    * build string; I don't want to bother with client nicknames, so I hope
917    * this is ok...
918    */
919   vd.vd_format = pattern;
920   va_copy(vd.vd_args, vl);
921   mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
922                  &vd);
923
924   for (; opslist; opslist = opslist->next)
925     if (opslist->value.cptr != one)
926       send_buffer(opslist->value.cptr, mb, 0);
927
928   msgq_clean(mb);
929 }