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  * $Id$
21  */
22 #include "send.h"
23 #include "channel.h"
24 #include "class.h"
25 #include "client.h"
26 #include "ircd.h"
27 #include "ircd_snprintf.h"
28 #include "ircd_string.h"
29 #include "list.h"
30 #include "match.h"
31 #include "msg.h"
32 #include "numnicks.h"
33 #include "s_bsd.h"
34 #include "s_debug.h"
35 #include "s_misc.h"
36 #include "s_user.h"
37 #include "sprintf_irc.h"
38 #include "struct.h"
39 #include "sys.h"
40
41 #include <assert.h>
42 #include <stdio.h>
43 #include <string.h>
44
45
46 char sendbuf[2048];
47 static int sentalong[MAXCONNECTIONS];
48 static int sentalong_marker;
49 struct SLink *opsarray[32];     /* don't use highest bit unless you change
50                                    atoi to strtoul in sendto_op_mask() */
51 #ifdef GODMODE
52 char sendbuf2[2048];
53 int sdbflag;
54 #endif /* GODMODE */
55
56 /*
57  * dead_link
58  *
59  * An error has been detected. The link *must* be closed,
60  * but *cannot* call ExitClient (m_bye) from here.
61  * Instead, mark it with FLAGS_DEADSOCKET. This should
62  * generate ExitClient from the main loop.
63  *
64  * If 'notice' is not NULL, it is assumed to be a format
65  * for a message to local opers. It can contain only one
66  * '%s', which will be replaced by the sockhost field of
67  * the failing link.
68  *
69  * Also, the notice is skipped for "uninteresting" cases,
70  * like Persons and yet unknown connections...
71  */
72
73 static void dead_link(struct Client *to, char *notice)
74 {
75   to->flags |= FLAGS_DEADSOCKET;
76   /*
77    * If because of BUFFERPOOL problem then clean dbuf's now so that
78    * notices don't hurt operators below.
79    */
80   DBufClear(&to->recvQ);
81   DBufClear(&to->sendQ);
82
83   /*
84    * Keep a copy of the last comment, for later use...
85    */
86   ircd_strncpy(to->info, notice, REALLEN);
87
88   if (!IsUser(to) && !IsUnknown(to) && !(to->flags & FLAGS_CLOSING))
89     sendto_ops("%s for %s", to->info, to->name);
90   Debug((DEBUG_ERROR, to->info));
91 }
92
93 static int can_send(struct Client* to)
94 {
95   assert(0 != to);
96   return (IsDead(to) || IsMe(to) || -1 == to->fd) ? 0 : 1;
97 }
98
99 /*
100  * flush_connections
101  *
102  * Used to empty all output buffers for all connections. Should only
103  * be called once per scan of connections. There should be a select in
104  * here perhaps but that means either forcing a timeout or doing a poll.
105  * When flushing, all we do is empty the obuffer array for each local
106  * client and try to send it. if we cant send it, it goes into the sendQ
107  * -avalon
108  */
109 void flush_connections(struct Client* cptr)
110 {
111   if (cptr) {
112     send_queued(cptr);
113   }
114   else {
115     int i;
116     for (i = HighestFd; i >= 0; i--) {
117       if ((cptr = LocalClientArray[i]))
118         send_queued(cptr);
119     }
120   }
121 }
122
123 /*
124  * flush_sendq_except - run through local client array and flush
125  * the sendq for each client, if the address of the client sendq
126  * is the same as the one specified, it is skipped. This is used
127  * by dbuf_put to try to get some more memory before bailing and
128  * causing the client to be disconnected.
129  */
130 void flush_sendq_except(const struct DBuf* one)
131 {
132   int i;
133   struct Client* cptr;
134   for (i = HighestFd; i >= 0; i--) {
135     if ( (cptr = LocalClientArray[i]) && one != &cptr->sendQ)
136       send_queued(cptr);
137   }
138 }
139
140 /*
141  * send_queued
142  *
143  * This function is called from the main select-loop (or whatever)
144  * when there is a chance that some output would be possible. This
145  * attempts to empty the send queue as far as possible...
146  */
147 void send_queued(struct Client *to)
148 {
149   assert(0 != to);
150   assert(0 != to->local);
151
152   if (IsBlocked(to) || !can_send(to))
153     return;                     /* Don't bother */
154
155   while (DBufLength(&to->sendQ) > 0) {
156     unsigned int len;
157     const char* msg = dbuf_map(&to->sendQ, &len);
158
159     if ((len = deliver_it(to, msg, len))) {
160       dbuf_delete(&to->sendQ, len);
161       to->lastsq = DBufLength(&to->sendQ) / 1024;
162       if (IsBlocked(to))
163         break;
164     }
165     else {
166       if (IsDead(to)) {
167         char tmp[512];
168         sprintf(tmp,"Write error: %s",(strerror(to->error)) ? (strerror(to->error)) : "Unknown error" );
169         dead_link(to, tmp);
170       }
171       break;
172     }
173   }
174 }
175
176 /*
177  *  send message to single client
178  */
179 /* See sendcmdto_one, below */
180 void sendto_one(struct Client *to, const char* pattern, ...)
181 {
182   va_list vl;
183   va_start(vl, pattern);
184   vsendto_one(to, pattern, vl);
185   va_end(vl);
186 }
187
188 /* See vsendcmdto_one, below */
189 void vsendto_one(struct Client *to, const char* pattern, va_list vl)
190 {
191   vsprintf_irc(sendbuf, pattern, vl);
192   sendbufto_one(to);
193 }
194
195 #ifdef GODMODE
196 static void send_to_god(struct Client* to, const char* buf)
197 {
198   if (!sdbflag && !IsUser(to)) {
199     char sbuf2[BUFSIZE + 1];
200     unsigned int len = strlen(buf) - 2;   /* Remove "\r\n" */
201
202     sdbflag = 1;
203     len = IRCD_MIN(len, BUFSIZE);
204     ircd_strncpy(sbuf2, buf, len);
205     sbuf2[len] = '\0';
206
207     if (len > 402) {
208       char c = sbuf2[200];
209       sbuf2[200] = '\0';
210       sendto_ops("SND:%-8.8s(%.4d): \"%s...%s\"",
211                  to->name, len, sbuf2, &sbuf2[len - 200]);
212     }
213     else
214       sendto_ops("SND:%-8.8s(%.4d): \"%s\"", to->name, len, sbuf2);
215     sdbflag = 0;
216   }
217 }
218 #endif /* GODMODE */
219
220 void send_buffer(struct Client* to, char* buf)
221 {
222   unsigned int len;
223   assert(0 != to);
224   assert(0 != buf);
225
226   if (to->from)
227     to = to->from;
228
229   if (!can_send(to))
230     /*
231      * This socket has already been marked as dead
232      */
233     return;
234
235   if (DBufLength(&to->sendQ) > get_sendq(to)) {
236     if (IsServer(to))
237       sendto_ops("Max SendQ limit exceeded for %s: " SIZE_T_FMT " > " SIZE_T_FMT,
238                  to->name, DBufLength(&to->sendQ), get_sendq(to));
239     dead_link(to, "Max sendQ exceeded");
240     return;
241   }
242
243   Debug((DEBUG_SEND, "Sending [%s] to %s", buf, to->name));
244
245   len = strlen(buf);
246   if (buf[len - 1] != '\n') {
247     if (len > 510)
248       len = 510;
249     buf[len++] = '\r';
250     buf[len++] = '\n';
251     buf[len] = '\0';
252   }
253
254   if (0 == dbuf_put(&to->sendQ, buf, len)) {
255     dead_link(to, "Buffer allocation error");
256     return;
257   }
258
259 #ifdef GODMODE
260   send_to_god(to, buf);
261 #endif /* GODMODE */
262   /*
263    * Update statistics. The following is slightly incorrect
264    * because it counts messages even if queued, but bytes
265    * only really sent. Queued bytes get updated in SendQueued.
266    */
267   ++to->sendM;
268   ++me.sendM;
269   /*
270    * This little bit is to stop the sendQ from growing too large when
271    * there is no need for it to. Thus we call send_queued() every time
272    * 2k has been added to the queue since the last non-fatal write.
273    * Also stops us from deliberately building a large sendQ and then
274    * trying to flood that link with data (possible during the net
275    * relinking done by servers with a large load).
276    */
277   if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
278     send_queued(to);
279 }
280
281 void sendbufto_one(struct Client* to)
282 {
283   send_buffer(to, sendbuf);
284 }
285
286 /* See vsendcmdto_one, below */
287 static void vsendto_prefix_one(struct Client *to, struct Client *from,
288     const char* pattern, va_list vl)
289 {
290   if (to && from && MyUser(to) && IsUser(from))
291   {
292     static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
293     char *par;
294     int flag = 0;
295     struct User *user = from->user;
296
297     par = va_arg(vl, char *);
298     strcpy(sender, from->name);
299     if (user)
300     {
301       if (*user->username)
302       {
303         strcat(sender, "!");
304         strcat(sender, user->username);
305       }
306       if (*user->host && !MyConnect(from))
307       {
308         strcat(sender, "@");
309         strcat(sender, user->host);
310         flag = 1;
311       }
312     }
313     /*
314      * Flag is used instead of strchr(sender, '@') for speed and
315      * also since username/nick may have had a '@' in them. -avalon
316      */
317     if (!flag && MyConnect(from) && *user->host)
318     {
319       strcat(sender, "@");
320       strcat(sender, from->sockhost);
321     }
322     *sendbuf = ':';
323     strcpy(&sendbuf[1], sender);
324     /* Assuming 'pattern' always starts with ":%s ..." */
325     vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
326   }
327   else
328     vsprintf_irc(sendbuf, pattern, vl);
329   sendbufto_one(to);
330 }
331
332 /* See sendcmdto_channel_butone, below */
333 void sendto_channel_butone(struct Client *one, struct Client *from, struct Channel *chptr,
334     const char* pattern, ...)
335 {
336   va_list vl;
337   struct Membership* member;
338   struct Client *acptr;
339   int i;
340
341   va_start(vl, pattern);
342
343   ++sentalong_marker;
344   for (member = chptr->members; member; member = member->next_member)
345   {
346     acptr = member->user;
347     /* ...was the one I should skip */
348     if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
349       continue;
350     if (MyConnect(acptr))       /* (It is always a client) */
351       vsendto_prefix_one(acptr, from, pattern, vl);
352     else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
353     {
354       sentalong[i] = sentalong_marker;
355       /*
356        * Don't send channel messages to links that are still eating
357        * the net.burst: -- Run 2/1/1997
358        */
359       if (!IsBurstOrBurstAck(acptr->from))
360         vsendto_prefix_one(acptr, from, pattern, vl);
361     }
362   }
363   va_end(vl);
364 }
365
366 /* See sendcmdto_channel_butone, below */
367 void sendmsgto_channel_butone(struct Client *one, struct Client *from,
368                               struct Channel *chptr, const char *sender,
369                               const char *cmd, const char *chname, const char *msg)
370 {
371  /*
372   * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
373   * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
374   */
375   struct Membership* member;
376   struct Client *acptr;
377   char userbuf[2048];
378   char servbuf[2048];
379   int i;
380   int flag=-1;
381
382   assert(0 != cmd);
383   /* 
384    * Precalculate the buffers we sent to the clients instead of doing an
385    * expensive sprintf() per member that we send to.  We still have to
386    * use strcpy() which is evil.
387    */
388   if (IsServer(from)) {
389     sprintf(userbuf,":%s %s %s :%s",
390             from->name, ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
391     sprintf(servbuf,"%s %s %s :%s", NumServ(from), cmd, chname, msg);
392   }
393   else {
394     sprintf(userbuf,":%s!%s@%s %s %s :%s",
395             from->name, from->user->username, from->user->host,
396             ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
397     sprintf(servbuf,"%s%s %s %s :%s", NumNick(from), cmd, chname, msg);
398   }
399
400   ++sentalong_marker;
401   for (member = chptr->members; member; member = member->next_member)
402   {
403     acptr = member->user;
404     /* ...was the one I should skip */
405     if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
406       continue;
407     if (MyConnect(acptr)) {      /* (It is always a client) */
408         if (flag!=0)
409           strcpy(sendbuf,userbuf);
410         flag=0;
411         sendbufto_one(acptr);
412     }
413     else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
414     {
415       sentalong[i] = sentalong_marker;
416       if (!IsBurstOrBurstAck(acptr->from)) {
417         if (flag != 1)
418           strcpy(sendbuf,servbuf);
419         flag = 1;
420         sendbufto_one(acptr);
421   }
422     } /* of if MyConnect() */
423   } /* of for(members) */
424 }
425
426 /* See sendcmdto_channel_butone, below */
427 void sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
428     const char* pattern, ...)
429 {
430   va_list vl;
431   struct Membership* member;
432   struct Client *acptr;
433
434   va_start(vl, pattern);
435
436   for (member = chptr->members; member; member = member->next_member)
437   {
438     acptr = member->user;
439     /* ...was the one I should skip */
440     if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
441       continue;
442     if (MyConnect(acptr))       /* (It is always a client) */
443       vsendto_prefix_one(acptr, from, pattern, vl);
444   }
445   va_end(vl);
446   return;
447 }
448
449 /* See sendcmdto_channel_butone, below */
450 void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
451     const char* pattern, ...)
452 {
453   va_list vl;
454   struct Membership* member;
455   struct Client *acptr;
456   int i;
457 #ifndef NO_PROTOCOL9
458   char  target[128];
459   char* source;
460   char* tp;
461   char* msg;
462 #endif
463
464   va_start(vl, pattern);
465
466   ++sentalong_marker;
467   for (member = chptr->members; member; member = member->next_member)
468   {
469     acptr = member->user;
470     if (acptr->from == acptr || /* Skip local clients */
471 #ifndef NO_PROTOCOL9
472         Protocol(acptr->from) < 10 ||   /* Skip P09 links */
473 #endif
474         acptr->from == one ||   /* ...was the one I should skip */
475         !IsChanOp(member) ||   /* Skip non chanops */
476         IsZombie(member) || IsDeaf(acptr))
477       continue;
478     if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
479     {
480       sentalong[i] = sentalong_marker;
481       /* Don't send channel messages to links that are
482          still eating the net.burst: -- Run 2/1/1997 */
483       if (!IsBurstOrBurstAck(acptr->from))
484         vsendto_prefix_one(acptr, from, pattern, vl);
485     }
486   }
487
488 #ifndef NO_PROTOCOL9
489   /* Send message to all 2.9 servers */
490   /* This is a hack, because it assumes that we know how `vl' is build up */
491   source = va_arg(vl, char *);
492   tp = va_arg(vl, char *);      /* Channel */
493   msg = va_arg(vl, char *);
494   for (member = chptr->members; member; member = member->next_member)
495   {
496     acptr = member->user;
497     if (acptr->from == acptr || /* Skip local clients */
498         Protocol(acptr->from) > 9 ||    /* Skip P10 servers */
499         acptr->from == one ||   /* ...was the one I should skip */
500         !IsChanOp(member) ||   /* Skip non chanops */
501         IsZombie(member) || IsDeaf(acptr))
502       continue;
503     if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
504     {
505       sentalong[i] = sentalong_marker;
506       /* Don't send channel messages to links that are
507          still eating the net.burst: -- Run 2/1/1997 */
508       if (!IsBurstOrBurstAck(acptr->from))
509       {
510         struct Membership* other_member;
511         struct Client* acptr2;
512         tp = target;
513         *tp = 0;
514         /* Find all chanops in this direction: */
515         for (other_member = chptr->members; other_member; other_member = other_member->next_member)
516         {
517           acptr2 = other_member->user;
518           if (acptr2->from == acptr->from && acptr2->from != one &&
519               IsChanOp(other_member) && !IsZombie(other_member) &&
520               !IsDeaf(acptr2))
521           {
522             int len = strlen(acptr2->name);
523             if (tp + len + 2 > target + sizeof(target))
524             {
525               sendto_prefix_one(acptr, from,
526                   ":%s NOTICE %s :%s", source, target, msg);
527               tp = target;
528               *tp = 0;
529             }
530             if (*target)
531               strcpy(tp++, ",");
532             strcpy(tp, acptr2->name);
533             tp += len;
534           }
535         }
536         sendto_prefix_one(acptr, from,
537             ":%s NOTICE %s :%s", source, target, msg);
538       }
539     }
540   }
541 #endif
542
543   va_end(vl);
544   return;
545 }
546
547 /*
548  * sendto_serv_butone
549  *
550  * Send a message to all connected servers except the client 'one'.
551  */
552 /* See sendcmdto_serv_butone, below */
553 void sendto_serv_butone(struct Client *one, const char* pattern, ...)
554 {
555   va_list vl;
556   struct DLink *lp;
557
558   va_start(vl, pattern);
559   vsprintf_irc(sendbuf, pattern, vl);
560   va_end(vl);
561
562   for (lp = me.serv->down; lp; lp = lp->next)
563   {
564     if (one && lp->value.cptr == one->from)
565       continue;
566     sendbufto_one(lp->value.cptr);
567   }
568
569 }
570
571 /*
572  * sendbufto_serv_butone()
573  *
574  * Send prepared sendbuf to all connected servers except the client 'one'
575  *  -Ghostwolf 18-May-97
576  */
577 void sendbufto_serv_butone(struct Client *one)
578 {
579   struct DLink *lp;
580
581   for (lp = me.serv->down; lp; lp = lp->next)
582   {
583     if (one && lp->value.cptr == one->from)
584       continue;
585     sendbufto_one(lp->value.cptr);
586   }
587 }
588
589
590 /*
591  * sendto_common_channels()
592  *
593  * Sends a message to all people (inclusing `acptr') on local server
594  * who are in same channel with client `acptr'.
595  */
596 /* See sendcmdto_common_channels, below */
597 void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
598 {
599   va_list vl;
600   struct Membership* chan;
601   struct Membership* member;
602
603   assert(0 != acptr);
604   assert(0 != acptr->from);
605   assert(0 != pattern);
606
607   va_start(vl, pattern);
608
609   ++sentalong_marker;
610   if (-1 < acptr->from->fd)
611     sentalong[acptr->from->fd] = sentalong_marker;
612   /*
613    * loop through acptr's channels, and the members on their channels
614    */
615   if (acptr->user) {
616     for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
617       for (member = chan->channel->members; member; member = member->next_member) {
618         struct Client *cptr = member->user;
619         int    i;
620         if (MyConnect(cptr) && 
621             -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
622           sentalong[i] = sentalong_marker;
623           vsendto_prefix_one(cptr, acptr, pattern, vl);
624         }
625       }
626     }
627   }
628   if (MyConnect(acptr))
629     vsendto_prefix_one(acptr, acptr, pattern, vl);
630   va_end(vl);
631   return;
632 }
633
634 /*
635  * sendto_channel_butserv
636  *
637  * Send a message to all members of a channel that
638  * are connected to this server.
639  *
640  * This contains a subtle bug; after the first call to vsendto_prefix_one()
641  * below, vl is in an indeterminate state, according to ANSI; we'd have to
642  * move va_start() and va_end() into the loop to correct the problem.  It's
643  * easier, however, just to use sendcmdto_channel_butserv(), which builds a
644  * buffer and sends that prepared buffer to each channel member.
645  */
646 /* See sendcmdto_channel_butserv, below */
647 void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
648 {
649   va_list vl;
650   struct Membership* member;
651   struct Client *acptr;
652   
653   va_start(vl, pattern);
654
655   for (member = chptr->members; member; member = member->next_member) {
656     acptr = member->user;
657     if (MyConnect(acptr) && !IsZombie(member))
658       vsendto_prefix_one(acptr, from, pattern, vl);
659   }
660   va_end(vl);
661   return;
662 }
663
664 /*
665  * Send a msg to all ppl on servers/hosts that match a specified mask
666  * (used for enhanced PRIVMSGs)
667  *
668  *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
669  */
670
671 static int match_it(struct Client *one, const char *mask, int what)
672 {
673   switch (what)
674   {
675     case MATCH_HOST:
676       return (match(mask, one->user->host) == 0);
677     case MATCH_SERVER:
678     default:
679       return (match(mask, one->user->server->name) == 0);
680   }
681 }
682
683 /*
684  * sendto_match_butone
685  *
686  * Send to all clients which match the mask in a way defined on 'what';
687  * either by user hostname or user servername.
688  */
689 void sendto_match_butone(struct Client *one, struct Client *from,
690     const char *mask, int what, const char* pattern, ...)
691 {
692   va_list vl;
693   int i;
694   struct Client *cptr, *acptr;
695
696   va_start(vl, pattern);
697   for (i = 0; i <= HighestFd; i++)
698   {
699     if (!(cptr = LocalClientArray[i]))
700       continue;                 /* that clients are not mine */
701     if (cptr == one)            /* must skip the origin !! */
702       continue;
703     if (IsServer(cptr))
704     {
705       for (acptr = GlobalClientList; acptr; acptr = acptr->next)
706         if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
707           break;
708       /* a person on that server matches the mask, so we
709        *  send *one* msg to that server ...
710        */
711       if (acptr == NULL)
712         continue;
713       /* ... but only if there *IS* a matching person */
714     }
715     /* my client, does he match ? */
716     else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
717       continue;
718     vsendto_prefix_one(cptr, from, pattern, vl);
719   }
720   va_end(vl);
721
722   return;
723 }
724
725 /*
726  * sendto_lops_butone
727  *
728  * Send to *local* ops but one.
729  */
730 /* See sendto_opmask_butone, below */
731 void sendto_lops_butone(struct Client* one, const char* pattern, ...)
732 {
733   va_list         vl;
734   struct Client*  cptr;
735   struct Client** clients = me.serv->client_list;
736   int             i;
737   char            nbuf[1024];
738
739   assert(0 != clients);
740
741   sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
742   va_start(vl, pattern);
743   vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
744   va_end(vl);
745
746   for (i = 0; i <= me.serv->nn_mask; ++i) {
747     if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
748       sprintf_irc(sendbuf, nbuf, cptr->name);
749       sendbufto_one(cptr);
750     }
751   }
752 }
753
754 /*
755  * sendto_op_mask
756  *
757  * Sends message to the list indicated by the bitmask field.
758  * Don't try to send to more than one list! That is not supported.
759  * Xorath 5/1/97
760  */
761 #ifdef OLD_VSENDTO_OP_MASK
762 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
763 {
764   static char fmt[1024];
765   char *fmt_target;
766   int i = 0;            /* so that 1 points to opsarray[0] */
767   struct SLink *opslist;
768
769   while ((mask >>= 1))
770     i++;
771   if (!(opslist = opsarray[i]))
772     return;
773
774   fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
775   do
776   {
777     strcpy(fmt_target, opslist->value.cptr->name);
778     strcat(fmt_target, " :*** Notice -- ");
779     strcat(fmt_target, pattern);
780     vsendto_one(opslist->value.cptr, fmt, vl);
781     opslist = opslist->next;
782   }
783   while (opslist);
784 }
785 #else /* !OLD_VSENDTO_OP_MASK */
786 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
787 {
788   vsendto_opmask_butone(0, mask, pattern, vl);
789 }
790 #endif /* OLD_VSENDTO_OP_MASK */
791
792 /*
793  * sendbufto_op_mask
794  *
795  * Send a prepared sendbuf to the list indicated by the bitmask field.
796  * Ghostwolf 16-May-97
797  */
798 void sendbufto_op_mask(unsigned int mask)
799 {
800   int i = 0;            /* so that 1 points to opsarray[0] */
801   struct SLink *opslist;
802   while ((mask >>= 1))
803     i++;
804   if (!(opslist = opsarray[i]))
805     return;
806   do
807   {
808     sendbufto_one(opslist->value.cptr);
809     opslist = opslist->next;
810   }
811   while (opslist);
812 }
813
814
815 /*
816  * sendto_ops
817  *
818  * Send to *local* ops only.
819  */
820 /* See vsendto_opmask_butone, below */
821 void vsendto_ops(const char *pattern, va_list vl)
822 {
823   struct Client *cptr;
824   int i;
825   char fmt[1024];
826   char *fmt_target;
827
828   fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
829
830   for (i = 0; i <= HighestFd; i++)
831     if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
832         SendServNotice(cptr))
833     {
834       strcpy(fmt_target, cptr->name);
835       strcat(fmt_target, " :*** Notice -- ");
836       strcat(fmt_target, pattern);
837       vsendto_one(cptr, fmt, vl);
838     }
839 }
840
841 /* See sendto_opmask_butone, below */
842 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
843 {
844   va_list vl;
845   va_start(vl, pattern);
846   vsendto_op_mask(mask, pattern, vl);
847   va_end(vl);
848 }
849
850 /* See sendto_opmask_butone, below */
851 void sendto_ops(const char *pattern, ...)
852 {
853   va_list vl;
854   va_start(vl, pattern);
855   vsendto_op_mask(SNO_OLDSNO, pattern, vl);
856   va_end(vl);
857 }
858
859 /*
860  * sendto_ops_butone
861  *
862  * Send message to all operators.
863  * one - client not to send message to
864  * from- client which message is from *NEVER* NULL!!
865  */
866 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
867 {
868   va_list vl;
869   int i;
870   struct Client *cptr;
871
872   va_start(vl, pattern);
873   ++sentalong_marker;
874   for (cptr = GlobalClientList; cptr; cptr = cptr->next)
875   {
876     if (!SendWallops(cptr))
877       continue;
878     i = cptr->from->fd;         /* find connection oper is on */
879     if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
880       continue;
881     if (cptr->from == one)
882       continue;                 /* ...was the one I should skip */
883     sentalong[i] = sentalong_marker;
884     vsendto_prefix_one(cptr->from, from, pattern, vl);
885   }
886   va_end(vl);
887
888   return;
889 }
890
891 /*
892  * sendto_g_serv_butone
893  *
894  * Send message to all remote +g users (server links).
895  *
896  * one - server not to send message to.
897  */
898 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
899 {
900   va_list vl;
901   struct Client *cptr;
902   int i;
903
904   va_start(vl, pattern);
905   ++sentalong_marker;
906   vsprintf_irc(sendbuf, pattern, vl);
907   for (cptr = GlobalClientList; cptr; cptr = cptr->next)
908   {
909     if (!SendDebug(cptr))
910       continue;
911     i = cptr->from->fd;         /* find connection user is on */
912     if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
913       continue;
914     if (MyConnect(cptr))
915       continue;
916     sentalong[i] = sentalong_marker;
917     if (cptr->from == one)
918       continue;
919     sendbufto_one(cptr);
920   }
921   va_end(vl);
922
923   return;
924 }
925
926 /*
927  * sendto_prefix_one
928  *
929  * to - destination client
930  * from - client which message is from
931  *
932  * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
933  * -avalon
934  */
935 /* See sendcmdto_one, below */
936 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
937 {
938   va_list vl;
939   va_start(vl, pattern);
940   vsendto_prefix_one(to, from, pattern, vl);
941   va_end(vl);
942 }
943
944 /*
945  * sendto_realops
946  *
947  * Send to *local* ops only but NOT +s nonopers.
948  */
949 /* See sendto_opmask_butone, below */
950 void sendto_realops(const char *pattern, ...)
951 {
952   va_list vl;
953
954   va_start(vl, pattern);
955   vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
956
957   va_end(vl);
958   return;
959 }
960
961 /*
962  * Send message to all servers of protocol 'p' and lower.
963  */
964 /* See sendcmdto_serv_butone, below */
965 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
966 {
967   va_list vl;
968   struct DLink *lp;
969   va_start(vl, pattern);
970   for (lp = me.serv->down; lp; lp = lp->next)
971     if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
972       vsendto_one(lp->value.cptr, pattern, vl);
973   va_end(vl);
974 }
975
976 /*
977  * Send message to all servers of protocol 'p' and higher.
978  */
979 /* See sendcmdto_serv_butone, below */
980 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
981 {
982   va_list vl;
983   struct DLink *lp;
984   va_start(vl, pattern);
985   for (lp = me.serv->down; lp; lp = lp->next)
986     if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
987       vsendto_one(lp->value.cptr, pattern, vl);
988   va_end(vl);
989 }
990
991 /*
992  * Send a (prefixed) command to a single client; select which of <cmd>
993  * <tok> to use depending on if to is a server or not.  <from> is the
994  * originator of the command.
995  */
996 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
997                    struct Client *to, const char *pattern, ...)
998 {
999   va_list vl;
1000
1001   va_start(vl, pattern);
1002   vsendcmdto_one(from, cmd, tok, to, pattern, vl);
1003   va_end(vl);
1004 }
1005
1006 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
1007                     struct Client *to, const char *pattern, va_list vl)
1008 {
1009   struct VarData vd;
1010   char sndbuf[IRC_BUFSIZE];
1011
1012   vd.vd_format = pattern; /* set up the struct VarData for %v */
1013   vd.vd_args = vl;
1014
1015   if (MyUser(to)) { /* :nick!user@host form; use cmd */
1016     if (IsServer(from) || IsMe(from))
1017       ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, ":%s %s %v",
1018                     from->name, cmd, &vd);
1019     else
1020       ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, ":%s!%s@%s %s %v",
1021                     from->name, from->user->username, from->user->host,
1022                     cmd, &vd);
1023   } else /* numeric form; use tok */
1024     ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, "%C %s %v", from, tok, &vd);
1025
1026   send_buffer(to, sndbuf);
1027 }
1028
1029 /*
1030  * Send a (prefixed) command to all servers but one, using tok; cmd
1031  * is ignored in this particular function.
1032  */
1033 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
1034                            const char *tok, struct Client *one,
1035                            const char *pattern, ...)
1036 {
1037   struct VarData vd;
1038   char sndbuf[IRC_BUFSIZE];
1039   struct DLink *lp;
1040
1041   vd.vd_format = pattern; /* set up the struct VarData for %v */
1042   va_start(vd.vd_args, pattern);
1043
1044   /* use token */
1045   ircd_snprintf(&me, sndbuf, sizeof(sndbuf) - 2, "%C %s %v", from, tok, &vd);
1046   va_end(vd.vd_args);
1047
1048   /* send it to our downlinks */
1049   for (lp = me.serv->down; lp; lp = lp->next) {
1050     if (one && lp->value.cptr == one->from)
1051       continue;
1052     send_buffer(lp->value.cptr, sndbuf);
1053   }
1054 }
1055
1056 /*
1057  * Send a (prefix) command originating from <from> to all channels
1058  * <from> is locally on.  <from> must be a user. <tok> is ignored in
1059  * this function.
1060  */
1061 void sendcmdto_common_channels(struct Client *from, const char *cmd,
1062                                const char *tok, const char *pattern, ...)
1063 {
1064   struct VarData vd;
1065   char sndbuf[IRC_BUFSIZE];
1066   struct Membership *chan;
1067   struct Membership *member;
1068
1069   assert(0 != from);
1070   assert(0 != from->from);
1071   assert(0 != pattern);
1072   assert(!IsServer(from) && !IsMe(from));
1073
1074   vd.vd_format = pattern; /* set up the struct VarData for %v */
1075
1076   va_start(vd.vd_args, pattern);
1077
1078   /* build the buffer */
1079   ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s!%s@%s %s %v", from->name,
1080                 from->user->username, from->user->host, cmd, &vd);
1081   va_end(vd.vd_args);
1082
1083   sentalong_marker++;
1084   if (-1 < from->from->fd)
1085     sentalong[from->from->fd] = sentalong_marker;
1086   /*
1087    * loop through from's channels, and the members on their channels
1088    */
1089   for (chan = from->user->channel; chan; chan = chan->next_channel)
1090     for (member = chan->channel->members; member;
1091          member = member->next_member)
1092       if (MyConnect(member->user) && -1 < from->fd &&
1093           sentalong[member->user->from->fd] != sentalong_marker) {
1094         sentalong[member->user->from->fd] = sentalong_marker;
1095         send_buffer(member->user, sndbuf);
1096       }
1097
1098   if (MyConnect(from))
1099     send_buffer(from, sndbuf);
1100 }
1101
1102 /*
1103  * Send a (prefixed) command to all local users on the channel specified
1104  * by <to>; <tok> is ignored by this function
1105  */
1106 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
1107                                const char *tok, struct Channel *to,
1108                                const char *pattern, ...)
1109 {
1110   struct VarData vd;
1111   char sndbuf[IRC_BUFSIZE];
1112   struct Membership *member;
1113
1114   vd.vd_format = pattern; /* set up the struct VarData for %v */
1115   va_start(vd.vd_args, pattern);
1116
1117   /* build the buffer */
1118   if (IsServer(from) || IsMe(from))
1119     ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s %s %v", from->name,
1120                   cmd, &vd);
1121   else
1122     ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s!%s@%s %s %v", from->name,
1123                   from->user->username, from->user->host, cmd, &vd);
1124   va_end(vd.vd_args);
1125
1126   /* send the buffer to each local channel member */
1127   for (member = to->members; member; member = member->next_member) {
1128     if (MyConnect(member->user) && !IsZombie(member))
1129       send_buffer(member->user, sndbuf);
1130   }
1131 }
1132
1133 /*
1134  * Send a (prefixed) command to all users on this channel, including
1135  * remote users; users to skip may be specified by setting appropriate
1136  * flags in the <skip> argument.  <one> will also be skipped.
1137  */
1138 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
1139                               const char *tok, struct Channel *to,
1140                               struct Client *one, unsigned int skip,
1141                               const char *pattern, ...)
1142 {
1143   struct Membership *member;
1144   struct VarData vd;
1145   char userbuf[IRC_BUFSIZE];
1146   char servbuf[IRC_BUFSIZE];
1147
1148   vd.vd_format = pattern;
1149
1150   /* Build buffer to send to users */
1151   va_start(vd.vd_args, pattern);
1152   if (IsServer(from) || IsMe(from))
1153     ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, ":%s %s %v", from->name,
1154                   cmd, &vd);
1155   else
1156     ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, ":%s!%s@%s %s %v",
1157                   from->name, from->user->username, from->user->host, cmd,
1158                   &vd);
1159   va_end(vd.vd_args);
1160
1161   /* Build buffer to send to servers */
1162   va_start(vd.vd_args, pattern);
1163   ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1164   va_end(vd.vd_args);
1165
1166   /* send buffer along! */
1167   sentalong_marker++;
1168   for (member = to->members; member; member = member->next_member) {
1169     /* skip one, zombies, and deaf users... */
1170     if (member->user->from == one || IsZombie(member) ||
1171         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
1172         (skip & SKIP_NONOPS && !IsChanOp(member)))
1173       continue;
1174
1175     if (MyConnect(member->user))
1176       send_buffer(member->user, userbuf); /* send user buffer */
1177     else if (-1 < member->user->from->fd &&
1178              sentalong[member->user->from->fd] != sentalong_marker) {
1179       sentalong[member->user->from->fd] = sentalong_marker;
1180
1181       if (skip & SKIP_BURST && IsBurstOrBurstAck(member->user->from))
1182         continue; /* skip bursting servers */
1183
1184       send_buffer(member->user->from, servbuf); /* send server buffer */
1185     }
1186   }
1187 }
1188
1189 /*
1190  * Send a (prefixed) command to all users except <one> that have
1191  * <flag> set.
1192  */
1193 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
1194                            const char *tok, struct Client *one,
1195                            unsigned int flag, const char *pattern, ...)
1196 {
1197   struct VarData vd;
1198   struct Client *cptr;
1199   char userbuf[IRC_BUFSIZE];
1200   char servbuf[IRC_BUFSIZE];
1201
1202   vd.vd_format = pattern;
1203
1204   /* Build buffer to send to users */
1205   va_start(vd.vd_args, pattern);
1206   if (IsServer(from) || IsMe(from))
1207     ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, ":%s %s %v", from->name,
1208                   cmd, &vd);
1209   else
1210     ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, ":%s!%s@%s %s %v",
1211                   from->name, from->user->username, from->user->host, cmd,
1212                   &vd);
1213   va_end(vd.vd_args);
1214
1215   /* Build buffer to send to servers */
1216   va_start(vd.vd_args, pattern);
1217   ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1218   va_end(vd.vd_args);
1219
1220   /* send buffer along! */
1221   sentalong_marker++;
1222   for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1223     if (cptr->from == one || !(cptr->flags & flag) || cptr->from->fd < 0 ||
1224         sentalong[cptr->from->fd] == sentalong_marker)
1225       continue; /* skip it */
1226
1227     if (IsServer(cptr)) /* send right buffer */
1228       send_buffer(cptr, servbuf);
1229     else
1230       send_buffer(cptr, userbuf);
1231   }
1232 }
1233
1234 /*
1235  * Send a server notice to all users subscribing to the indicated <mask>
1236  * except for <one>
1237  */
1238 void sendto_opmask_butone(struct Client *one, unsigned int mask,
1239                           const char *pattern, ...)
1240 {
1241   va_list vl;
1242
1243   va_start(vl, pattern);
1244   vsendto_opmask_butone(one, mask, pattern, vl);
1245   va_end(vl);
1246 }
1247
1248 /*
1249  * Same as above, except called with a variable argument list
1250  */
1251 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
1252                            const char *pattern, va_list vl)
1253 {
1254   struct VarData vd;
1255   char sndbuf[IRC_BUFSIZE];
1256   int i = 0; /* so that 1 points to opsarray[0] */
1257   struct SLink *opslist;
1258
1259   while ((mask >>= 1))
1260     i++;
1261
1262   if (!(opslist = opsarray[i]))
1263     return;
1264
1265   /*
1266    * build string; I don't want to bother with client nicknames, so I hope
1267    * this is ok...
1268    */
1269   vd.vd_format = pattern;
1270   vd.vd_args = vl;
1271   ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s " MSG_NOTICE
1272                 " * :*** Notice -- %v", me.name, &vd);
1273
1274   for (; opslist; opslist = opslist->next)
1275     send_buffer(opslist->value.cptr, sndbuf);
1276 }