cdd5de8adc87bff3d9ba78cfc7237cb6f72c0da5
[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       if (IsBlocked(to))
205         update_write(to);
206       return;
207     }
208   }
209
210   /* Ok, sendq is now empty... */
211   client_drop_sendq(cli_connect(to));
212   update_write(to);
213 }
214
215 /** Try to send a buffer to a client, queueing it if needed.
216  * @param[in,out] to Client to send message to.
217  * @param[in] buf Message to send.
218  * @param[in] prio If non-zero, send as high priority.
219  */
220 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
221 {
222   assert(0 != to);
223   assert(0 != buf);
224
225   if (cli_from(to))
226     to = cli_from(to);
227
228   if (!can_send(to))
229     /*
230      * This socket has already been marked as dead
231      */
232     return;
233
234   if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
235     if (IsServer(to))
236       sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
237                            "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
238                            get_sendq(to));
239     dead_link(to, "Max sendQ exceeded");
240     return;
241   }
242
243   Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
244
245   msgq_add(&(cli_sendQ(to)), buf, prio);
246   client_add_sendq(cli_connect(to), &send_queues);
247   update_write(to);
248
249   /*
250    * Update statistics. The following is slightly incorrect
251    * because it counts messages even if queued, but bytes
252    * only really sent. Queued bytes get updated in SendQueued.
253    */
254   ++(cli_sendM(to));
255   ++(cli_sendM(&me));
256   /*
257    * This little bit is to stop the sendQ from growing too large when
258    * there is no need for it to. Thus we call send_queued() every time
259    * 2k has been added to the queue since the last non-fatal write.
260    * Also stops us from deliberately building a large sendQ and then
261    * trying to flood that link with data (possible during the net
262    * relinking done by servers with a large load).
263    */
264   if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
265     send_queued(to);
266 }
267
268 /*
269  * Send a msg to all ppl on servers/hosts that match a specified mask
270  * (used for enhanced PRIVMSGs)
271  *
272  *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
273  */
274
275 /** Check whether a client matches a target mask.
276  * @param[in] from Client trying to send a message (ignored).
277  * @param[in] one Client being considered as a target.
278  * @param[in] mask Mask for matching against.
279  * @param[in] what Type of match (either MATCH_HOST or MATCH_SERVER).
280  * @return Non-zero if \a one matches, zero if not.
281  */
282 static int match_it(struct Client *from, struct Client *one, const char *mask, int what)
283 {
284   switch (what)
285   {
286     case MATCH_HOST:
287       return (match(mask, cli_user(one)->host) == 0 ||
288         (HasHiddenHost(one) && match(mask, cli_user(one)->realhost) == 0));
289     case MATCH_SERVER:
290     default:
291       return (match(mask, cli_name(cli_user(one)->server)) == 0);
292   }
293 }
294
295 /** Send an unprefixed line to a client.
296  * @param[in] to Client receiving message.
297  * @param[in] pattern Format string of message.
298  */
299 void sendrawto_one(struct Client *to, const char *pattern, ...)
300 {
301   struct MsgBuf *mb;
302   va_list vl;
303
304   va_start(vl, pattern);
305   mb = msgq_vmake(to, pattern, vl);
306   va_end(vl);
307
308   send_buffer(to, mb, 0);
309
310   msgq_clean(mb);
311 }
312
313 /** Send a (prefixed) command to a single client.
314  * @param[in] from Client sending the command.
315  * @param[in] cmd Long name of command (used if \a to is a user).
316  * @param[in] tok Short name of command (used if \a to is a server).
317  * @param[in] to Destination of command.
318  * @param[in] pattern Format string for command arguments.
319  */
320 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
321                    struct Client *to, const char *pattern, ...)
322 {
323   struct VarData vd;
324   struct MsgBuf *mb;
325
326   to = cli_from(to);
327
328   vd.vd_format = pattern; /* set up the struct VarData for %v */
329   va_start(vd.vd_args, pattern);
330
331   mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
332                  &vd);
333
334   va_end(vd.vd_args);
335
336   send_buffer(to, mb, 0);
337
338   msgq_clean(mb);
339 }
340
341 /**
342  * Send a (prefixed) command to a single client in the priority queue.
343  * @param[in] from Client sending the command.
344  * @param[in] cmd Long name of command (used if \a to is a user).
345  * @param[in] tok Short name of command (used if \a to is a server).
346  * @param[in] to Destination of command.
347  * @param[in] pattern Format string for command arguments.
348  */
349 void sendcmdto_prio_one(struct Client *from, const char *cmd, const char *tok,
350                         struct Client *to, const char *pattern, ...)
351 {
352   struct VarData vd;
353   struct MsgBuf *mb;
354
355   to = cli_from(to);
356
357   vd.vd_format = pattern; /* set up the struct VarData for %v */
358   va_start(vd.vd_args, pattern);
359
360   mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
361                  &vd);
362
363   va_end(vd.vd_args);
364
365   send_buffer(to, mb, 1);
366
367   msgq_clean(mb);
368 }
369
370 /**
371  * Send a (prefixed) command to all servers matching or not matching a
372  * flag but one.
373  * @param[in] from Client sending the command.
374  * @param[in] cmd Long name of command (ignored).
375  * @param[in] tok Short name of command.
376  * @param[in] one Client direction to skip (or NULL).
377  * @param[in] require Only send to servers with this Flag bit set.
378  * @param[in] forbid Do not send to servers with this Flag bit set.
379  * @param[in] pattern Format string for command arguments.
380  */
381 void sendcmdto_flag_serv_butone(struct Client *from, const char *cmd,
382                                 const char *tok, struct Client *one,
383                                 int require, int forbid,
384                                 const char *pattern, ...)
385 {
386   struct VarData vd;
387   struct MsgBuf *mb;
388   struct DLink *lp;
389
390   vd.vd_format = pattern; /* set up the struct VarData for %v */
391   va_start(vd.vd_args, pattern);
392
393   /* use token */
394   mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
395   va_end(vd.vd_args);
396
397   /* send it to our downlinks */
398   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
399     if (one && lp->value.cptr == cli_from(one))
400       continue;
401     if ((require < FLAG_LAST_FLAG) && !HasFlag(lp->value.cptr, require))
402       continue;
403     if ((forbid < FLAG_LAST_FLAG) && HasFlag(lp->value.cptr, forbid))
404       continue;
405     send_buffer(lp->value.cptr, mb, 0);
406   }
407
408   msgq_clean(mb);
409 }
410
411 /**
412  * Send a (prefixed) command to all servers but one.
413  * @param[in] from Client sending the command.
414  * @param[in] cmd Long name of command (ignored).
415  * @param[in] tok Short name of command.
416  * @param[in] one Client direction to skip (or NULL).
417  * @param[in] pattern Format string for command arguments.
418  */
419 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
420                            const char *tok, struct Client *one,
421                            const char *pattern, ...)
422 {
423   struct VarData vd;
424   struct MsgBuf *mb;
425   struct DLink *lp;
426
427   vd.vd_format = pattern; /* set up the struct VarData for %v */
428   va_start(vd.vd_args, pattern);
429
430   /* use token */
431   mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
432   va_end(vd.vd_args);
433
434   /* send it to our downlinks */
435   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
436     if (one && lp->value.cptr == cli_from(one))
437       continue;
438     send_buffer(lp->value.cptr, mb, 0);
439   }
440
441   msgq_clean(mb);
442 }
443
444 /** Safely increment the sentalong marker.
445  * This increments the sentalong marker.  Since new connections will
446  * have con_sentalong() == 0, and to avoid confusion when the counter
447  * wraps, we reset all sentalong markers to zero when the sentalong
448  * marker hits zero.
449  * @param[in,out] one Client to mark with new sentalong marker (if any).
450  */
451 static void
452 bump_sentalong(struct Client *one)
453 {
454   if (!++sentalong_marker)
455   {
456     int ii;
457     for (ii = 0; ii < HighestFd; ++ii)
458       if (LocalClientArray[ii])
459         cli_sentalong(LocalClientArray[ii]) = 0;
460     ++sentalong_marker;
461   }
462   if (one)
463     cli_sentalong(one) = sentalong_marker;
464 }
465
466 /** Send a (prefixed) command to all channels that \a from is on.
467  * @param[in] from Client originating the command.
468  * @param[in] cmd Long name of command.
469  * @param[in] tok Short name of command.
470  * @param[in] one Client direction to skip (or NULL).
471  * @param[in] pattern Format string for command arguments.
472  */
473 void sendcmdto_common_channels_butone(struct Client *from, const char *cmd,
474                                       const char *tok, struct Client *one,
475                                       const char *pattern, ...)
476 {
477   struct VarData vd;
478   struct MsgBuf *mb;
479   struct Membership *chan;
480   struct Membership *member;
481
482   assert(0 != from);
483   assert(0 != cli_from(from));
484   assert(0 != pattern);
485   assert(!IsServer(from) && !IsMe(from));
486
487   vd.vd_format = pattern; /* set up the struct VarData for %v */
488
489   va_start(vd.vd_args, pattern);
490
491   /* build the buffer */
492   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
493   va_end(vd.vd_args);
494
495   bump_sentalong(from);
496   /*
497    * loop through from's channels, and the members on their channels
498    */
499   for (chan = cli_user(from)->channel; chan; chan = chan->next_channel) {
500     if (IsZombie(chan) || IsDelayedJoin(chan))
501       continue;
502     for (member = chan->channel->members; member;
503          member = member->next_member)
504       if (MyConnect(member->user)
505           && -1 < cli_fd(cli_from(member->user))
506           && member->user != one
507           && cli_sentalong(member->user) != sentalong_marker) {
508         cli_sentalong(member->user) = sentalong_marker;
509         send_buffer(member->user, mb, 0);
510       }
511   }
512
513   if (MyConnect(from) && from != one)
514     send_buffer(from, mb, 0);
515
516   msgq_clean(mb);
517 }
518
519 /** Send a (prefixed) command to all channels that \a from is on. (Check audit)
520  * @param[in] from Client originating the command.
521  * @param[in] cmd Long name of command.
522  * @param[in] tok Short name of command.
523  * @param[in] one Client direction to skip (or NULL).
524  * @param[in] pattern Format string for command arguments.
525  */
526 void sendcmdto_common_channels_butone_audit(struct Client *from, const char *cmd,
527                                       const char *tok, struct Client *one,
528                                       const char *pattern, ...)
529 {
530   struct VarData vd;
531   struct MsgBuf *mb;
532   struct Membership *chan;
533   struct Membership *member;
534
535   assert(0 != from);
536   assert(0 != cli_from(from));
537   assert(0 != pattern);
538   assert(!IsServer(from) && !IsMe(from));
539
540   vd.vd_format = pattern; /* set up the struct VarData for %v */
541
542   va_start(vd.vd_args, pattern);
543
544   /* build the buffer */
545   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
546   va_end(vd.vd_args);
547
548   bump_sentalong(from);
549   /*
550    * loop through from's channels, and the members on their channels
551    */
552   for (chan = cli_user(from)->channel; chan; chan = chan->next_channel) {
553     if (IsZombie(chan) || IsDelayedJoin(chan))
554       continue;
555     for (member = chan->channel->members; member;
556          member = member->next_member)
557       if (MyConnect(member->user)
558           && -1 < cli_fd(cli_from(member->user))
559           && member->user != one
560           && cli_sentalong(member->user) != sentalong_marker
561           && (!(chan->channel->mode.mode & MODE_AUDITORIUM) || IsVoicedOrOpped(chan) || IsChanOp(member))) {
562         cli_sentalong(member->user) = sentalong_marker;
563         send_buffer(member->user, mb, 0);
564       }
565   }
566
567   if (MyConnect(from) && from != one)
568     send_buffer(from, mb, 0);
569
570   msgq_clean(mb);
571 }
572
573 /** Send a (prefixed) command to all local users on a channel.
574  * @param[in] from Client originating the command.
575  * @param[in] cmd Long name of command.
576  * @param[in] tok Short name of command (ignored).
577  * @param[in] to Destination channel.
578  * @param[in] one Client direction to skip (or NULL).
579  * @param[in] skip Bitmask of SKIP_DEAF, SKIP_NONOPS, SKIP_NONVOICES indicating which clients to skip.
580  * @param[in] pattern Format string for command arguments.
581  */
582 void sendcmdto_channel_butserv_butone(struct Client *from, const char *cmd,
583                                       const char *tok, struct Channel *to,
584                                       struct Client *one, unsigned int skip,
585                                       const char *pattern, ...)
586 {
587   struct VarData vd;
588   struct MsgBuf *mb;
589   struct Membership *member;
590
591   vd.vd_format = pattern; /* set up the struct VarData for %v */
592   va_start(vd.vd_args, pattern);
593
594   /* build the buffer */
595   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
596   va_end(vd.vd_args);
597
598   /* send the buffer to each local channel member */
599   for (member = to->members; member; member = member->next_member) {
600     if (!MyConnect(member->user)
601         || member->user == one 
602         || IsZombie(member)
603         || (skip & SKIP_DEAF && IsDeaf(member->user))
604         || (skip & SKIP_NONOPS && !IsChanOp(member))
605                 || (skip & SKIP_OPS && IsChanOp(member))
606         || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
607         continue;
608       send_buffer(member->user, mb, 0);
609   }
610
611   msgq_clean(mb);
612 }
613
614 /** Send a (prefixed) command to all servers with users on \a to.
615  * Skip \a from and \a one plus those indicated in \a skip.
616  * @param[in] from Client originating the command.
617  * @param[in] cmd Long name of command (ignored).
618  * @param[in] tok Short name of command.
619  * @param[in] to Destination channel.
620  * @param[in] one Client direction to skip (or NULL).
621  * @param[in] skip Bitmask of SKIP_NONOPS and SKIP_NONVOICES indicating which clients to skip.
622  * @param[in] pattern Format string for command arguments.
623  */
624 void sendcmdto_channel_servers_butone(struct Client *from, const char *cmd,
625                                       const char *tok, struct Channel *to,
626                                       struct Client *one, unsigned int skip,
627                                       const char *pattern, ...)
628 {
629   struct VarData vd;
630   struct MsgBuf *serv_mb;
631   struct Membership *member;
632
633   /* build the buffer */
634   vd.vd_format = pattern;
635   va_start(vd.vd_args, pattern);
636   serv_mb = msgq_make(&me, "%:#C %s %v", from, tok, &vd);
637   va_end(vd.vd_args);
638
639   /* send the buffer to each server */
640   bump_sentalong(one);
641   cli_sentalong(from) = sentalong_marker;
642   for (member = to->members; member; member = member->next_member) {
643     if (MyConnect(member->user)
644         || IsZombie(member)
645         || cli_fd(cli_from(member->user)) < 0
646         || cli_sentalong(member->user) == sentalong_marker
647         || (skip & SKIP_NONOPS && !IsChanOp(member))
648                 || (skip & SKIP_OPS && IsChanOp(member))
649         || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
650       continue;
651     cli_sentalong(member->user) = sentalong_marker;
652     send_buffer(member->user, serv_mb, 0);
653   }
654   msgq_clean(serv_mb);
655 }
656
657
658 /** Send a (prefixed) command to all users on this channel, except for
659  * \a one and those matching \a skip.
660  * @warning \a pattern must not contain %v.
661  * @param[in] from Client originating the command.
662  * @param[in] cmd Long name of command.
663  * @param[in] tok Short name of command.
664  * @param[in] to Destination channel.
665  * @param[in] one Client direction to skip (or NULL).
666  * @param[in] skip Bitmask of SKIP_NONOPS, SKIP_NONVOICES, SKIP_DEAF, SKIP_BURST.
667  * @param[in] pattern Format string for command arguments.
668  */
669 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
670                               const char *tok, struct Channel *to,
671                               struct Client *one, unsigned int skip,
672                               unsigned char prefix, const char *pattern, ...)
673 {
674   struct Membership *member;
675   struct VarData vd;
676   struct MsgBuf *user_mb;
677   struct MsgBuf *serv_mb;
678   struct Client *service;
679
680   vd.vd_format = pattern;
681
682   /* Build buffer to send to users */
683   va_start(vd.vd_args, pattern);
684   user_mb = msgq_make(0, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? "%:#C %s @%v" : "%:#C %s %v",
685                       from, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? MSG_NOTICE : cmd, &vd);
686   va_end(vd.vd_args);
687
688   /* Build buffer to send to servers */
689   va_start(vd.vd_args, pattern);
690   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
691   va_end(vd.vd_args);
692
693   /* send buffer along! */
694   bump_sentalong(one);
695   for (member = to->members; member; member = member->next_member) {
696     /* skip one, zombies, and deaf users... */
697     if (IsZombie(member) ||
698         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
699         (skip & SKIP_NONOPS && !IsChanOp(member)) ||
700                 (skip & SKIP_OPS && IsChanOp(member)) ||
701         (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)) ||
702         (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
703         cli_fd(cli_from(member->user)) < 0 ||
704         cli_sentalong(member->user) == sentalong_marker)
705       continue;
706     cli_sentalong(member->user) = sentalong_marker;
707
708     if (MyConnect(member->user)) /* pick right buffer to send */
709       send_buffer(member->user, user_mb, 0);
710     else
711       send_buffer(member->user, serv_mb, 0);
712   }
713
714   /* Consult service forwarding table. */
715   if(GlobalForwards[prefix]
716       && (service = FindServer(GlobalForwards[prefix]))
717       && cli_sentalong(service) != sentalong_marker) {
718       cli_sentalong(service) = sentalong_marker;
719       send_buffer(service, serv_mb, 0);
720   }
721   
722   if(GlobalForwards['*']
723       && (service = FindServer(GlobalForwards['*']))
724       && cli_sentalong(service) != sentalong_marker) {
725       cli_sentalong(service) = sentalong_marker;
726       send_buffer(service, serv_mb, 0);
727   }
728
729   msgq_clean(user_mb);
730   msgq_clean(serv_mb);
731 }
732
733 /** Send a (prefixed) WALL of type \a type to all users except \a one.
734  * @warning \a pattern must not contain %v.
735  * @param[in] from Source of the command.
736  * @param[in] type One of WALL_DESYNCH, WALL_WALLOPS or WALL_WALLUSERS.
737  * @param[in] one Client direction to skip (or NULL).
738  * @param[in] pattern Format string for command arguments.
739  */
740 void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
741                              const char *pattern, ...)
742 {
743   struct VarData vd;
744   struct Client *cptr;
745   struct MsgBuf *mb;
746   struct DLink *lp;
747   char *prefix=NULL;
748   char *tok=NULL;
749   int his_wallops;
750   int i;
751
752   vd.vd_format = pattern;
753
754   /* Build buffer to send to users */
755   va_start(vd.vd_args, pattern);
756   switch (type) {
757         case WALL_DESYNCH:
758                 prefix="";
759                 tok=TOK_DESYNCH;
760                 break;
761         case WALL_WALLOPS:
762                 prefix="* ";
763                 tok=TOK_WALLOPS;
764                 break;
765         case WALL_WALLUSERS:
766                 prefix="$ ";
767                 tok=TOK_WALLUSERS;
768                 break;
769         default:
770                 assert(0);
771   }
772   mb = msgq_make(0, "%:#C " MSG_WALLOPS " :%s%v", from, prefix,&vd);
773   va_end(vd.vd_args);
774
775   /* send buffer along! */
776   his_wallops = feature_bool(FEAT_HIS_WALLOPS);
777   for (i = 0; i <= HighestFd; i++)
778   {
779     if (!(cptr = LocalClientArray[i]) ||
780         (cli_fd(cli_from(cptr)) < 0) ||
781         (type == WALL_DESYNCH && !SendDebug(cptr)) ||
782         (type == WALL_WALLOPS &&
783          (!SendWallops(cptr) || (his_wallops && !IsAnOper(cptr)))) ||
784         (type == WALL_WALLUSERS && !SendWallops(cptr)))
785       continue; /* skip it */
786     send_buffer(cptr, mb, 1);
787   }
788
789   msgq_clean(mb);
790
791   /* Build buffer to send to servers */
792   va_start(vd.vd_args, pattern);
793   mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
794   va_end(vd.vd_args);
795
796   /* send buffer along! */
797   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
798     if (one && lp->value.cptr == cli_from(one))
799       continue;
800     send_buffer(lp->value.cptr, mb, 1);
801   }
802
803   msgq_clean(mb);
804 }
805
806 /** Send a (prefixed) command to all users matching \a to as \a who.
807  * @warning \a pattern must not contain %v.
808  * @param[in] from Source of the command.
809  * @param[in] cmd Long name of command.
810  * @param[in] tok Short name of command.
811  * @param[in] to Destination host/server mask.
812  * @param[in] one Client direction to skip (or NULL).
813  * @param[in] who Type of match for \a to (either MATCH_HOST or MATCH_SERVER).
814  * @param[in] pattern Format string for command arguments.
815  */
816 void sendcmdto_match_butone(struct Client *from, const char *cmd,
817                             const char *tok, const char *to,
818                             struct Client *one, unsigned int who,
819                             const char *pattern, ...)
820 {
821   struct VarData vd;
822   struct Client *cptr;
823   struct MsgBuf *user_mb;
824   struct MsgBuf *serv_mb;
825
826   vd.vd_format = pattern;
827
828   /* Build buffer to send to users */
829   va_start(vd.vd_args, pattern);
830   user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
831   va_end(vd.vd_args);
832
833   /* Build buffer to send to servers */
834   va_start(vd.vd_args, pattern);
835   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
836   va_end(vd.vd_args);
837
838   /* send buffer along */
839   bump_sentalong(one);
840   for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
841     if (!IsRegistered(cptr) || IsServer(cptr) || cli_fd(cli_from(cptr)) < 0 ||
842         cli_sentalong(cptr) == sentalong_marker ||
843         !match_it(from, cptr, to, who))
844       continue; /* skip it */
845     cli_sentalong(cptr) = sentalong_marker;
846
847     if (MyConnect(cptr)) /* send right buffer */
848       send_buffer(cptr, user_mb, 0);
849     else
850       send_buffer(cptr, serv_mb, 0);
851   }
852
853   msgq_clean(user_mb);
854   msgq_clean(serv_mb);
855 }
856
857 /** Send a server notice to all users subscribing to the indicated \a
858  * mask except for \a one.
859  * @param[in] one Client direction to skip (or NULL).
860  * @param[in] mask One of the SNO_* constants.
861  * @param[in] pattern Format string for server notice.
862  */
863 void sendto_opmask_butone(struct Client *one, unsigned int mask,
864                           const char *pattern, ...)
865 {
866   va_list vl;
867
868   va_start(vl, pattern);
869   vsendto_opmask_butone(one, mask, pattern, vl);
870   va_end(vl);
871 }
872
873 /** Send a server notice to all users subscribing to the indicated \a
874  * mask except for \a one, rate-limited to once per 30 seconds.
875  * @param[in] one Client direction to skip (or NULL).
876  * @param[in] mask One of the SNO_* constants.
877  * @param[in,out] rate Pointer to the last time the message was sent.
878  * @param[in] pattern Format string for server notice.
879  */
880 void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
881                                       time_t *rate, const char *pattern, ...)
882 {
883   va_list vl;
884
885   if ((CurrentTime - *rate) < 30)
886     return;
887   else
888     *rate = CurrentTime;
889
890   va_start(vl, pattern);
891   vsendto_opmask_butone(one, mask, pattern, vl);
892   va_end(vl);
893 }
894
895
896 /** Send a server notice to all users subscribing to the indicated \a
897  * mask except for \a one.
898  * @param[in] one Client direction to skip (or NULL).
899  * @param[in] mask One of the SNO_* constants.
900  * @param[in] pattern Format string for server notice.
901  * @param[in] vl Argument list for format string.
902  */
903 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
904                            const char *pattern, va_list vl)
905 {
906   struct VarData vd;
907   struct MsgBuf *mb;
908   int i = 0; /* so that 1 points to opsarray[0] */
909   struct SLink *opslist;
910
911   while ((mask >>= 1))
912     i++;
913
914   if (!(opslist = opsarray[i]))
915     return;
916
917   /*
918    * build string; I don't want to bother with client nicknames, so I hope
919    * this is ok...
920    */
921   vd.vd_format = pattern;
922   va_copy(vd.vd_args, vl);
923   mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
924                  &vd);
925
926   for (; opslist; opslist = opslist->next)
927     if (opslist->value.cptr != one)
928       send_buffer(opslist->value.cptr, mb, 0);
929
930   msgq_clean(mb);
931 }