ircu2.10.12 pk910 fork
[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 local users on a channel.
518  * @param[in] from Client originating the command.
519  * @param[in] cmd Long name of command.
520  * @param[in] tok Short name of command (ignored).
521  * @param[in] to Destination channel.
522  * @param[in] one Client direction to skip (or NULL).
523  * @param[in] skip Bitmask of SKIP_DEAF, SKIP_NONOPS, SKIP_NONVOICES indicating which clients to skip.
524  * @param[in] pattern Format string for command arguments.
525  */
526 void sendcmdto_channel_butserv_butone(struct Client *from, const char *cmd,
527                                       const char *tok, struct Channel *to,
528                                       struct Client *one, unsigned int skip,
529                                       const char *pattern, ...)
530 {
531   struct VarData vd;
532   struct MsgBuf *mb;
533   struct Membership *member;
534
535   vd.vd_format = pattern; /* set up the struct VarData for %v */
536   va_start(vd.vd_args, pattern);
537
538   /* build the buffer */
539   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
540   va_end(vd.vd_args);
541
542   /* send the buffer to each local channel member */
543   for (member = to->members; member; member = member->next_member) {
544     if (!MyConnect(member->user)
545         || member->user == one 
546         || IsZombie(member)
547         || (skip & SKIP_DEAF && IsDeaf(member->user))
548         || (skip & SKIP_NONOPS && !IsChanOp(member))
549         || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
550         continue;
551       send_buffer(member->user, mb, 0);
552   }
553
554   msgq_clean(mb);
555 }
556
557 /** Send a (prefixed) command to all servers with users on \a to.
558  * Skip \a from and \a one plus those indicated in \a skip.
559  * @param[in] from Client originating the command.
560  * @param[in] cmd Long name of command (ignored).
561  * @param[in] tok Short name of command.
562  * @param[in] to Destination channel.
563  * @param[in] one Client direction to skip (or NULL).
564  * @param[in] skip Bitmask of SKIP_NONOPS and SKIP_NONVOICES indicating which clients to skip.
565  * @param[in] pattern Format string for command arguments.
566  */
567 void sendcmdto_channel_servers_butone(struct Client *from, const char *cmd,
568                                       const char *tok, struct Channel *to,
569                                       struct Client *one, unsigned int skip,
570                                       const char *pattern, ...)
571 {
572   struct VarData vd;
573   struct MsgBuf *serv_mb;
574   struct Membership *member;
575
576   /* build the buffer */
577   vd.vd_format = pattern;
578   va_start(vd.vd_args, pattern);
579   serv_mb = msgq_make(&me, "%:#C %s %v", from, tok, &vd);
580   va_end(vd.vd_args);
581
582   /* send the buffer to each server */
583   bump_sentalong(one);
584   cli_sentalong(from) = sentalong_marker;
585   for (member = to->members; member; member = member->next_member) {
586     if (MyConnect(member->user)
587         || IsZombie(member)
588         || cli_fd(cli_from(member->user)) < 0
589         || cli_sentalong(member->user) == sentalong_marker
590         || (skip & SKIP_NONOPS && !IsChanOp(member))
591         || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
592       continue;
593     cli_sentalong(member->user) = sentalong_marker;
594     send_buffer(member->user, serv_mb, 0);
595   }
596   msgq_clean(serv_mb);
597 }
598
599
600 /** Send a (prefixed) command to all users on this channel, except for
601  * \a one and those matching \a skip.
602  * @warning \a pattern must not contain %v.
603  * @param[in] from Client originating the command.
604  * @param[in] cmd Long name of command.
605  * @param[in] tok Short name of command.
606  * @param[in] to Destination channel.
607  * @param[in] one Client direction to skip (or NULL).
608  * @param[in] skip Bitmask of SKIP_NONOPS, SKIP_NONVOICES, SKIP_DEAF, SKIP_BURST.
609  * @param[in] pattern Format string for command arguments.
610  */
611 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
612                               const char *tok, struct Channel *to,
613                               struct Client *one, unsigned int skip,
614                               unsigned char prefix, const char *pattern, ...)
615 {
616   struct Membership *member;
617   struct VarData vd;
618   struct MsgBuf *user_mb;
619   struct MsgBuf *serv_mb;
620   struct Client *service;
621
622   vd.vd_format = pattern;
623
624   /* Build buffer to send to users */
625   va_start(vd.vd_args, pattern);
626   user_mb = msgq_make(0, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? "%:#C %s @%v" : "%:#C %s %v",
627                       from, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? MSG_NOTICE : cmd, &vd);
628   va_end(vd.vd_args);
629
630   /* Build buffer to send to servers */
631   va_start(vd.vd_args, pattern);
632   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
633   va_end(vd.vd_args);
634
635   /* send buffer along! */
636   bump_sentalong(one);
637   for (member = to->members; member; member = member->next_member) {
638     /* skip one, zombies, and deaf users... */
639     if (IsZombie(member) ||
640         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
641         (skip & SKIP_NONOPS && !IsChanOp(member)) ||
642         (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)) ||
643         (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
644         cli_fd(cli_from(member->user)) < 0 ||
645         cli_sentalong(member->user) == sentalong_marker)
646       continue;
647     cli_sentalong(member->user) = sentalong_marker;
648
649     if (MyConnect(member->user)) /* pick right buffer to send */
650       send_buffer(member->user, user_mb, 0);
651     else
652       send_buffer(member->user, serv_mb, 0);
653   }
654
655   /* Consult service forwarding table. */
656   if(GlobalForwards[prefix]
657       && (service = FindServer(GlobalForwards[prefix]))
658       && cli_sentalong(service) != sentalong_marker) {
659       cli_sentalong(service) = sentalong_marker;
660       send_buffer(service, serv_mb, 0);
661   }
662   
663   if(GlobalForwards['*']
664       && (service = FindServer(GlobalForwards['*']))
665       && cli_sentalong(service) != sentalong_marker) {
666       cli_sentalong(service) = sentalong_marker;
667       send_buffer(service, serv_mb, 0);
668   }
669
670   msgq_clean(user_mb);
671   msgq_clean(serv_mb);
672 }
673
674 /** Send a (prefixed) WALL of type \a type to all users except \a one.
675  * @warning \a pattern must not contain %v.
676  * @param[in] from Source of the command.
677  * @param[in] type One of WALL_DESYNCH, WALL_WALLOPS or WALL_WALLUSERS.
678  * @param[in] one Client direction to skip (or NULL).
679  * @param[in] pattern Format string for command arguments.
680  */
681 void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
682                              const char *pattern, ...)
683 {
684   struct VarData vd;
685   struct Client *cptr;
686   struct MsgBuf *mb;
687   struct DLink *lp;
688   char *prefix=NULL;
689   char *tok=NULL;
690   int his_wallops;
691   int i;
692
693   vd.vd_format = pattern;
694
695   /* Build buffer to send to users */
696   va_start(vd.vd_args, pattern);
697   switch (type) {
698         case WALL_DESYNCH:
699                 prefix="";
700                 tok=TOK_DESYNCH;
701                 break;
702         case WALL_WALLOPS:
703                 prefix="* ";
704                 tok=TOK_WALLOPS;
705                 break;
706         case WALL_WALLUSERS:
707                 prefix="$ ";
708                 tok=TOK_WALLUSERS;
709                 break;
710         default:
711                 assert(0);
712   }
713   mb = msgq_make(0, "%:#C " MSG_WALLOPS " :%s%v", from, prefix,&vd);
714   va_end(vd.vd_args);
715
716   /* send buffer along! */
717   his_wallops = feature_bool(FEAT_HIS_WALLOPS);
718   for (i = 0; i <= HighestFd; i++)
719   {
720     if (!(cptr = LocalClientArray[i]) ||
721         (cli_fd(cli_from(cptr)) < 0) ||
722         (type == WALL_DESYNCH && !SendDebug(cptr)) ||
723         (type == WALL_WALLOPS &&
724          (!SendWallops(cptr) || (his_wallops && !IsAnOper(cptr)))) ||
725         (type == WALL_WALLUSERS && !SendWallops(cptr)))
726       continue; /* skip it */
727     send_buffer(cptr, mb, 1);
728   }
729
730   msgq_clean(mb);
731
732   /* Build buffer to send to servers */
733   va_start(vd.vd_args, pattern);
734   mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
735   va_end(vd.vd_args);
736
737   /* send buffer along! */
738   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
739     if (one && lp->value.cptr == cli_from(one))
740       continue;
741     send_buffer(lp->value.cptr, mb, 1);
742   }
743
744   msgq_clean(mb);
745 }
746
747 /** Send a (prefixed) command to all users matching \a to as \a who.
748  * @warning \a pattern must not contain %v.
749  * @param[in] from Source of the command.
750  * @param[in] cmd Long name of command.
751  * @param[in] tok Short name of command.
752  * @param[in] to Destination host/server mask.
753  * @param[in] one Client direction to skip (or NULL).
754  * @param[in] who Type of match for \a to (either MATCH_HOST or MATCH_SERVER).
755  * @param[in] pattern Format string for command arguments.
756  */
757 void sendcmdto_match_butone(struct Client *from, const char *cmd,
758                             const char *tok, const char *to,
759                             struct Client *one, unsigned int who,
760                             const char *pattern, ...)
761 {
762   struct VarData vd;
763   struct Client *cptr;
764   struct MsgBuf *user_mb;
765   struct MsgBuf *serv_mb;
766
767   vd.vd_format = pattern;
768
769   /* Build buffer to send to users */
770   va_start(vd.vd_args, pattern);
771   user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
772   va_end(vd.vd_args);
773
774   /* Build buffer to send to servers */
775   va_start(vd.vd_args, pattern);
776   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
777   va_end(vd.vd_args);
778
779   /* send buffer along */
780   bump_sentalong(one);
781   for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
782     if (!IsRegistered(cptr) || IsServer(cptr) || cli_fd(cli_from(cptr)) < 0 ||
783         cli_sentalong(cptr) == sentalong_marker ||
784         !match_it(from, cptr, to, who))
785       continue; /* skip it */
786     cli_sentalong(cptr) = sentalong_marker;
787
788     if (MyConnect(cptr)) /* send right buffer */
789       send_buffer(cptr, user_mb, 0);
790     else
791       send_buffer(cptr, serv_mb, 0);
792   }
793
794   msgq_clean(user_mb);
795   msgq_clean(serv_mb);
796 }
797
798 /** Send a server notice to all users subscribing to the indicated \a
799  * mask except for \a one.
800  * @param[in] one Client direction to skip (or NULL).
801  * @param[in] mask One of the SNO_* constants.
802  * @param[in] pattern Format string for server notice.
803  */
804 void sendto_opmask_butone(struct Client *one, unsigned int mask,
805                           const char *pattern, ...)
806 {
807   va_list vl;
808
809   va_start(vl, pattern);
810   vsendto_opmask_butone(one, mask, pattern, vl);
811   va_end(vl);
812 }
813
814 /** Send a server notice to all users subscribing to the indicated \a
815  * mask except for \a one, rate-limited to once per 30 seconds.
816  * @param[in] one Client direction to skip (or NULL).
817  * @param[in] mask One of the SNO_* constants.
818  * @param[in,out] rate Pointer to the last time the message was sent.
819  * @param[in] pattern Format string for server notice.
820  */
821 void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
822                                       time_t *rate, const char *pattern, ...)
823 {
824   va_list vl;
825
826   if ((CurrentTime - *rate) < 30)
827     return;
828   else
829     *rate = CurrentTime;
830
831   va_start(vl, pattern);
832   vsendto_opmask_butone(one, mask, pattern, vl);
833   va_end(vl);
834 }
835
836
837 /** Send a server notice to all users subscribing to the indicated \a
838  * mask except for \a one.
839  * @param[in] one Client direction to skip (or NULL).
840  * @param[in] mask One of the SNO_* constants.
841  * @param[in] pattern Format string for server notice.
842  * @param[in] vl Argument list for format string.
843  */
844 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
845                            const char *pattern, va_list vl)
846 {
847   struct VarData vd;
848   struct MsgBuf *mb;
849   int i = 0; /* so that 1 points to opsarray[0] */
850   struct SLink *opslist;
851
852   while ((mask >>= 1))
853     i++;
854
855   if (!(opslist = opsarray[i]))
856     return;
857
858   /*
859    * build string; I don't want to bother with client nicknames, so I hope
860    * this is ok...
861    */
862   vd.vd_format = pattern;
863   va_copy(vd.vd_args, vl);
864   mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
865                  &vd);
866
867   for (; opslist; opslist = opslist->next)
868     if (opslist->value.cptr != one)
869       send_buffer(opslist->value.cptr, mb, 0);
870
871   msgq_clean(mb);
872 }