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