added basic ssl support to ircu
[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$
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 "numnicks.h"
39 #include "parse.h"
40 #include "s_bsd.h"
41 #include "s_debug.h"
42 #include "s_misc.h"
43 #include "s_user.h"
44 #include "struct.h"
45 #include "sys.h"
46
47 /* #include <assert.h> -- Now using assert in ircd_log.h */
48 #include <stdio.h>
49 #include <string.h>
50
51 /** Last used marker value. */
52 static int sentalong_marker;
53 /** Array of users with the corresponding server notice mask bit set. */
54 struct SLink *opsarray[32];     /* don't use highest bit unless you change
55                                    atoi to strtoul in sendto_op_mask() */
56 /** Linked list of all connections with data queued to send. */
57 static struct Connection *send_queues;
58
59 /*
60  * dead_link
61  *
62  * An error has been detected. The link *must* be closed,
63  * but *cannot* call ExitClient (m_bye) from here.
64  * Instead, mark it with FLAG_DEADSOCKET. This should
65  * generate ExitClient from the main loop.
66  *
67  * If 'notice' is not NULL, it is assumed to be a format
68  * for a message to local opers. It can contain only one
69  * '%s', which will be replaced by the sockhost field of
70  * the failing link.
71  *
72  * Also, the notice is skipped for "uninteresting" cases,
73  * like Persons and yet unknown connections...
74  */
75 /** Mark a client as dead, even if they are not the current message source.
76  * This is done by setting the DEADSOCKET flag on the user and letting the
77  * main loop perform the actual exit logic.
78  * @param[in,out] to Client being killed.
79  * @param[in] notice Message for local opers.
80  */
81 static void dead_link(struct Client *to, char *notice)
82 {
83   SetFlag(to, FLAG_DEADSOCKET);
84   /*
85    * If because of BUFFERPOOL problem then clean dbuf's now so that
86    * notices don't hurt operators below.
87    */
88   DBufClear(&(cli_recvQ(to)));
89   MsgQClear(&(cli_sendQ(to)));
90   client_drop_sendq(cli_connect(to));
91
92   /*
93    * Keep a copy of the last comment, for later use...
94    */
95   ircd_strncpy(cli_info(to), notice, REALLEN);
96
97   if (!IsUser(to) && !IsUnknown(to) && !HasFlag(to, FLAG_CLOSING))
98     sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
99   Debug((DEBUG_ERROR, cli_info(to)));
100 }
101
102 /** Test whether we can send to a client.
103  * @param[in] to Client we want to send to.
104  * @return Non-zero if we can send to the client.
105  */
106 static int can_send(struct Client* to)
107 {
108   assert(0 != to);
109   return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
110 }
111
112 /** Close the connection with the highest sendq.
113  * This should be called when we need to free buffer memory.
114  * @param[in] servers_too If non-zero, consider killing servers, too.
115  */
116 void
117 kill_highest_sendq(int servers_too)
118 {
119   int i;
120   unsigned int highest_sendq = 0;
121   struct Client *highest_client = 0;
122
123   for (i = HighestFd; i >= 0; i--)
124   {
125     if (!LocalClientArray[i] || (!servers_too && cli_serv(LocalClientArray[i])))
126       continue; /* skip servers */
127     
128     /* If this sendq is higher than one we last saw, remember it */
129     if (MsgQLength(&(cli_sendQ(LocalClientArray[i]))) > highest_sendq)
130     {
131       highest_client = LocalClientArray[i];
132       highest_sendq = MsgQLength(&(cli_sendQ(highest_client)));
133     }
134   }
135
136   if (highest_client)
137     dead_link(highest_client, "Buffer allocation error");
138 }
139
140 /*
141  * flush_connections
142  *
143  * Used to empty all output buffers for all connections. Should only
144  * be called once per scan of connections. There should be a select in
145  * here perhaps but that means either forcing a timeout or doing a poll.
146  * When flushing, all we do is empty the obuffer array for each local
147  * client and try to send it. if we cant send it, it goes into the sendQ
148  * -avalon
149  */
150 /** Flush data queued for one or all connections.
151  * @param[in] cptr Client to flush (if NULL, do all).
152  */
153 void flush_connections(struct Client* cptr)
154 {
155   struct SSLConnection *ssl = cli_connect(cptr)->con_ssl;
156   if (cptr) {
157     if(ssl)
158       ssl_connection_flush(ssl);
159     send_queued(cptr);
160   }
161   else {
162     struct Connection* con;
163     for (con = send_queues; con; con = con_next(con)) {
164       assert(0 < MsgQLength(&(con_sendQ(con))));
165       send_queued(con_client(con));
166     }
167     ssl_connection_flush(NULL);
168   }
169 }
170
171 /*
172  * send_queued
173  *
174  * This function is called from the main select-loop (or whatever)
175  * when there is a chance that some output would be possible. This
176  * attempts to empty the send queue as far as possible...
177  */
178 /** Attempt to send data queued for a client.
179  * @param[in] to Client to send data to.
180  */
181 void send_queued(struct Client *to)
182 {
183   assert(0 != to);
184   assert(0 != cli_local(to));
185
186   if (IsBlocked(to) || !can_send(to))
187     return;                     /* Don't bother */
188
189   while (MsgQLength(&(cli_sendQ(to))) > 0) {
190     unsigned int len;
191
192     if ((len = deliver_it(to, &(cli_sendQ(to))))) {
193       msgq_delete(&(cli_sendQ(to)), len);
194       cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
195       if (IsBlocked(to)) {
196         update_write(to);
197         return;
198       }
199     }
200     else {
201       if (IsDead(to)) {
202         char tmp[512];
203         sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
204         dead_link(to, tmp);
205       }
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 local users on a channel.
520  * @param[in] from Client originating the command.
521  * @param[in] cmd Long name of command.
522  * @param[in] tok Short name of command (ignored).
523  * @param[in] to Destination channel.
524  * @param[in] one Client direction to skip (or NULL).
525  * @param[in] skip Bitmask of SKIP_DEAF, SKIP_NONOPS, SKIP_NONVOICES indicating which clients to skip.
526  * @param[in] pattern Format string for command arguments.
527  */
528 void sendcmdto_channel_butserv_butone(struct Client *from, const char *cmd,
529                                       const char *tok, struct Channel *to,
530                                       struct Client *one, unsigned int skip,
531                                       const char *pattern, ...)
532 {
533   struct VarData vd;
534   struct MsgBuf *mb;
535   struct Membership *member;
536
537   vd.vd_format = pattern; /* set up the struct VarData for %v */
538   va_start(vd.vd_args, pattern);
539
540   /* build the buffer */
541   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
542   va_end(vd.vd_args);
543
544   /* send the buffer to each local channel member */
545   for (member = to->members; member; member = member->next_member) {
546     if (!MyConnect(member->user)
547         || member->user == one 
548         || IsZombie(member)
549         || (skip & SKIP_DEAF && IsDeaf(member->user))
550         || (skip & SKIP_NONOPS && !IsChanOp(member))
551         || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
552         continue;
553       send_buffer(member->user, mb, 0);
554   }
555
556   msgq_clean(mb);
557 }
558
559 /** Send a (prefixed) command to all servers with users on \a to.
560  * Skip \a from and \a one plus those indicated in \a skip.
561  * @param[in] from Client originating the command.
562  * @param[in] cmd Long name of command (ignored).
563  * @param[in] tok Short name of command.
564  * @param[in] to Destination channel.
565  * @param[in] one Client direction to skip (or NULL).
566  * @param[in] skip Bitmask of SKIP_NONOPS and SKIP_NONVOICES indicating which clients to skip.
567  * @param[in] pattern Format string for command arguments.
568  */
569 void sendcmdto_channel_servers_butone(struct Client *from, const char *cmd,
570                                       const char *tok, struct Channel *to,
571                                       struct Client *one, unsigned int skip,
572                                       const char *pattern, ...)
573 {
574   struct VarData vd;
575   struct MsgBuf *serv_mb;
576   struct Membership *member;
577
578   /* build the buffer */
579   vd.vd_format = pattern;
580   va_start(vd.vd_args, pattern);
581   serv_mb = msgq_make(&me, "%:#C %s %v", from, tok, &vd);
582   va_end(vd.vd_args);
583
584   /* send the buffer to each server */
585   bump_sentalong(one);
586   cli_sentalong(from) = sentalong_marker;
587   for (member = to->members; member; member = member->next_member) {
588     if (MyConnect(member->user)
589         || IsZombie(member)
590         || cli_fd(cli_from(member->user)) < 0
591         || cli_sentalong(member->user) == sentalong_marker
592         || (skip & SKIP_NONOPS && !IsChanOp(member))
593         || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
594       continue;
595     cli_sentalong(member->user) = sentalong_marker;
596     send_buffer(member->user, serv_mb, 0);
597   }
598   msgq_clean(serv_mb);
599 }
600
601
602 /** Send a (prefixed) command to all users on this channel, except for
603  * \a one and those matching \a skip.
604  * @warning \a pattern must not contain %v.
605  * @param[in] from Client originating the command.
606  * @param[in] cmd Long name of command.
607  * @param[in] tok Short name of command.
608  * @param[in] to Destination channel.
609  * @param[in] one Client direction to skip (or NULL).
610  * @param[in] skip Bitmask of SKIP_NONOPS, SKIP_NONVOICES, SKIP_DEAF, SKIP_BURST.
611  * @param[in] pattern Format string for command arguments.
612  */
613 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
614                               const char *tok, struct Channel *to,
615                               struct Client *one, unsigned int skip,
616                               const char *pattern, ...)
617 {
618   struct Membership *member;
619   struct VarData vd;
620   struct MsgBuf *user_mb;
621   struct MsgBuf *serv_mb;
622
623   vd.vd_format = pattern;
624
625   /* Build buffer to send to users */
626   va_start(vd.vd_args, pattern);
627   user_mb = msgq_make(0, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? "%:#C %s @%v" : "%:#C %s %v",
628                       from, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? MSG_NOTICE : cmd, &vd);
629   va_end(vd.vd_args);
630
631   /* Build buffer to send to servers */
632   va_start(vd.vd_args, pattern);
633   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
634   va_end(vd.vd_args);
635
636   /* send buffer along! */
637   bump_sentalong(one);
638   for (member = to->members; member; member = member->next_member) {
639     /* skip one, zombies, and deaf users... */
640     if (IsZombie(member) ||
641         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
642         (skip & SKIP_NONOPS && !IsChanOp(member)) ||
643         (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)) ||
644         (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
645         cli_fd(cli_from(member->user)) < 0 ||
646         cli_sentalong(member->user) == sentalong_marker)
647       continue;
648     cli_sentalong(member->user) = sentalong_marker;
649
650     if (MyConnect(member->user)) /* pick right buffer to send */
651       send_buffer(member->user, user_mb, 0);
652     else
653       send_buffer(member->user, serv_mb, 0);
654   }
655
656   msgq_clean(user_mb);
657   msgq_clean(serv_mb);
658 }
659
660 /** Send a (prefixed) WALL of type \a type to all users except \a one.
661  * @warning \a pattern must not contain %v.
662  * @param[in] from Source of the command.
663  * @param[in] type One of WALL_DESYNCH, WALL_WALLOPS or WALL_WALLUSERS.
664  * @param[in] one Client direction to skip (or NULL).
665  * @param[in] pattern Format string for command arguments.
666  */
667 void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
668                              const char *pattern, ...)
669 {
670   struct VarData vd;
671   struct Client *cptr;
672   struct MsgBuf *mb;
673   struct DLink *lp;
674   char *prefix=NULL;
675   char *tok=NULL;
676   int his_wallops;
677   int i;
678
679   vd.vd_format = pattern;
680
681   /* Build buffer to send to users */
682   va_start(vd.vd_args, pattern);
683   switch (type) {
684         case WALL_DESYNCH:
685                 prefix="";
686                 tok=TOK_DESYNCH;
687                 break;
688         case WALL_WALLOPS:
689                 prefix="* ";
690                 tok=TOK_WALLOPS;
691                 break;
692         case WALL_WALLUSERS:
693                 prefix="$ ";
694                 tok=TOK_WALLUSERS;
695                 break;
696         default:
697                 assert(0);
698   }
699   mb = msgq_make(0, "%:#C " MSG_WALLOPS " :%s%v", from, prefix,&vd);
700   va_end(vd.vd_args);
701
702   /* send buffer along! */
703   his_wallops = feature_bool(FEAT_HIS_WALLOPS);
704   for (i = 0; i <= HighestFd; i++)
705   {
706     if (!(cptr = LocalClientArray[i]) ||
707         (cli_fd(cli_from(cptr)) < 0) ||
708         (type == WALL_DESYNCH && !SendDebug(cptr)) ||
709         (type == WALL_WALLOPS &&
710          (!SendWallops(cptr) || (his_wallops && !IsAnOper(cptr)))) ||
711         (type == WALL_WALLUSERS && !SendWallops(cptr)))
712       continue; /* skip it */
713     send_buffer(cptr, mb, 1);
714   }
715
716   msgq_clean(mb);
717
718   /* Build buffer to send to servers */
719   va_start(vd.vd_args, pattern);
720   mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
721   va_end(vd.vd_args);
722
723   /* send buffer along! */
724   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
725     if (one && lp->value.cptr == cli_from(one))
726       continue;
727     send_buffer(lp->value.cptr, mb, 1);
728   }
729
730   msgq_clean(mb);
731 }
732
733 /** Send a (prefixed) command to all users matching \a to as \a who.
734  * @warning \a pattern must not contain %v.
735  * @param[in] from Source of the command.
736  * @param[in] cmd Long name of command.
737  * @param[in] tok Short name of command.
738  * @param[in] to Destination host/server mask.
739  * @param[in] one Client direction to skip (or NULL).
740  * @param[in] who Type of match for \a to (either MATCH_HOST or MATCH_SERVER).
741  * @param[in] pattern Format string for command arguments.
742  */
743 void sendcmdto_match_butone(struct Client *from, const char *cmd,
744                             const char *tok, const char *to,
745                             struct Client *one, unsigned int who,
746                             const char *pattern, ...)
747 {
748   struct VarData vd;
749   struct Client *cptr;
750   struct MsgBuf *user_mb;
751   struct MsgBuf *serv_mb;
752
753   vd.vd_format = pattern;
754
755   /* Build buffer to send to users */
756   va_start(vd.vd_args, pattern);
757   user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
758   va_end(vd.vd_args);
759
760   /* Build buffer to send to servers */
761   va_start(vd.vd_args, pattern);
762   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
763   va_end(vd.vd_args);
764
765   /* send buffer along */
766   bump_sentalong(one);
767   for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
768     if (!IsRegistered(cptr) || IsServer(cptr) || cli_fd(cli_from(cptr)) < 0 ||
769         cli_sentalong(cptr) == sentalong_marker ||
770         !match_it(from, cptr, to, who))
771       continue; /* skip it */
772     cli_sentalong(cptr) = sentalong_marker;
773
774     if (MyConnect(cptr)) /* send right buffer */
775       send_buffer(cptr, user_mb, 0);
776     else
777       send_buffer(cptr, serv_mb, 0);
778   }
779
780   msgq_clean(user_mb);
781   msgq_clean(serv_mb);
782 }
783
784 /** Send a server notice to all users subscribing to the indicated \a
785  * mask except for \a one.
786  * @param[in] one Client direction to skip (or NULL).
787  * @param[in] mask One of the SNO_* constants.
788  * @param[in] pattern Format string for server notice.
789  */
790 void sendto_opmask_butone(struct Client *one, unsigned int mask,
791                           const char *pattern, ...)
792 {
793   va_list vl;
794
795   va_start(vl, pattern);
796   vsendto_opmask_butone(one, mask, pattern, vl);
797   va_end(vl);
798 }
799
800 /** Send a server notice to all users subscribing to the indicated \a
801  * mask except for \a one, rate-limited to once per 30 seconds.
802  * @param[in] one Client direction to skip (or NULL).
803  * @param[in] mask One of the SNO_* constants.
804  * @param[in,out] rate Pointer to the last time the message was sent.
805  * @param[in] pattern Format string for server notice.
806  */
807 void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
808                                       time_t *rate, const char *pattern, ...)
809 {
810   va_list vl;
811
812   if ((CurrentTime - *rate) < 30)
813     return;
814   else
815     *rate = CurrentTime;
816
817   va_start(vl, pattern);
818   vsendto_opmask_butone(one, mask, pattern, vl);
819   va_end(vl);
820 }
821
822
823 /** Send a server notice to all users subscribing to the indicated \a
824  * mask except for \a one.
825  * @param[in] one Client direction to skip (or NULL).
826  * @param[in] mask One of the SNO_* constants.
827  * @param[in] pattern Format string for server notice.
828  * @param[in] vl Argument list for format string.
829  */
830 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
831                            const char *pattern, va_list vl)
832 {
833   struct VarData vd;
834   struct MsgBuf *mb;
835   int i = 0; /* so that 1 points to opsarray[0] */
836   struct SLink *opslist;
837
838   while ((mask >>= 1))
839     i++;
840
841   if (!(opslist = opsarray[i]))
842     return;
843
844   /*
845    * build string; I don't want to bother with client nicknames, so I hope
846    * this is ok...
847    */
848   vd.vd_format = pattern;
849   va_copy(vd.vd_args, vl);
850   mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
851                  &vd);
852
853   for (; opslist; opslist = opslist->next)
854     if (opslist->value.cptr != one)
855       send_buffer(opslist->value.cptr, mb, 0);
856
857   msgq_clean(mb);
858 }