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