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