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 /* See sendcmdto_match_butone, below */
690 void sendto_match_butone(struct Client *one, struct Client *from,
691     const char *mask, int what, const char* pattern, ...)
692 {
693   va_list vl;
694   int i;
695   struct Client *cptr, *acptr;
696
697   va_start(vl, pattern);
698   for (i = 0; i <= HighestFd; i++)
699   {
700     if (!(cptr = LocalClientArray[i]))
701       continue;                 /* that clients are not mine */
702     if (cptr == one)            /* must skip the origin !! */
703       continue;
704     if (IsServer(cptr))
705     {
706       for (acptr = GlobalClientList; acptr; acptr = acptr->next)
707         if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
708           break;
709       /* a person on that server matches the mask, so we
710        *  send *one* msg to that server ...
711        */
712       if (acptr == NULL)
713         continue;
714       /* ... but only if there *IS* a matching person */
715     }
716     /* my client, does he match ? */
717     else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
718       continue;
719     vsendto_prefix_one(cptr, from, pattern, vl);
720   }
721   va_end(vl);
722
723   return;
724 }
725
726 /*
727  * sendto_lops_butone
728  *
729  * Send to *local* ops but one.
730  */
731 /* See sendto_opmask_butone, below */
732 void sendto_lops_butone(struct Client* one, const char* pattern, ...)
733 {
734   va_list         vl;
735   struct Client*  cptr;
736   struct Client** clients = me.serv->client_list;
737   int             i;
738   char            nbuf[1024];
739
740   assert(0 != clients);
741
742   sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
743   va_start(vl, pattern);
744   vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
745   va_end(vl);
746
747   for (i = 0; i <= me.serv->nn_mask; ++i) {
748     if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
749       sprintf_irc(sendbuf, nbuf, cptr->name);
750       sendbufto_one(cptr);
751     }
752   }
753 }
754
755 /*
756  * sendto_op_mask
757  *
758  * Sends message to the list indicated by the bitmask field.
759  * Don't try to send to more than one list! That is not supported.
760  * Xorath 5/1/97
761  */
762 #ifdef OLD_VSENDTO_OP_MASK
763 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
764 {
765   static char fmt[1024];
766   char *fmt_target;
767   int i = 0;            /* so that 1 points to opsarray[0] */
768   struct SLink *opslist;
769
770   while ((mask >>= 1))
771     i++;
772   if (!(opslist = opsarray[i]))
773     return;
774
775   fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
776   do
777   {
778     strcpy(fmt_target, opslist->value.cptr->name);
779     strcat(fmt_target, " :*** Notice -- ");
780     strcat(fmt_target, pattern);
781     vsendto_one(opslist->value.cptr, fmt, vl);
782     opslist = opslist->next;
783   }
784   while (opslist);
785 }
786 #else /* !OLD_VSENDTO_OP_MASK */
787 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
788 {
789   vsendto_opmask_butone(0, mask, pattern, vl);
790 }
791 #endif /* OLD_VSENDTO_OP_MASK */
792
793 /*
794  * sendbufto_op_mask
795  *
796  * Send a prepared sendbuf to the list indicated by the bitmask field.
797  * Ghostwolf 16-May-97
798  */
799 void sendbufto_op_mask(unsigned int mask)
800 {
801   int i = 0;            /* so that 1 points to opsarray[0] */
802   struct SLink *opslist;
803   while ((mask >>= 1))
804     i++;
805   if (!(opslist = opsarray[i]))
806     return;
807   do
808   {
809     sendbufto_one(opslist->value.cptr);
810     opslist = opslist->next;
811   }
812   while (opslist);
813 }
814
815
816 /*
817  * sendto_ops
818  *
819  * Send to *local* ops only.
820  */
821 /* See vsendto_opmask_butone, below */
822 void vsendto_ops(const char *pattern, va_list vl)
823 {
824   struct Client *cptr;
825   int i;
826   char fmt[1024];
827   char *fmt_target;
828
829   fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
830
831   for (i = 0; i <= HighestFd; i++)
832     if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
833         SendServNotice(cptr))
834     {
835       strcpy(fmt_target, cptr->name);
836       strcat(fmt_target, " :*** Notice -- ");
837       strcat(fmt_target, pattern);
838       vsendto_one(cptr, fmt, vl);
839     }
840 }
841
842 /* See sendto_opmask_butone, below */
843 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
844 {
845   va_list vl;
846   va_start(vl, pattern);
847   vsendto_op_mask(mask, pattern, vl);
848   va_end(vl);
849 }
850
851 /* See sendto_opmask_butone, below */
852 void sendto_ops(const char *pattern, ...)
853 {
854   va_list vl;
855   va_start(vl, pattern);
856   vsendto_op_mask(SNO_OLDSNO, pattern, vl);
857   va_end(vl);
858 }
859
860 /*
861  * sendto_ops_butone
862  *
863  * Send message to all operators.
864  * one - client not to send message to
865  * from- client which message is from *NEVER* NULL!!
866  */
867 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
868 {
869   va_list vl;
870   int i;
871   struct Client *cptr;
872
873   va_start(vl, pattern);
874   ++sentalong_marker;
875   for (cptr = GlobalClientList; cptr; cptr = cptr->next)
876   {
877     if (!SendWallops(cptr))
878       continue;
879     i = cptr->from->fd;         /* find connection oper is on */
880     if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
881       continue;
882     if (cptr->from == one)
883       continue;                 /* ...was the one I should skip */
884     sentalong[i] = sentalong_marker;
885     vsendto_prefix_one(cptr->from, from, pattern, vl);
886   }
887   va_end(vl);
888
889   return;
890 }
891
892 /*
893  * sendto_g_serv_butone
894  *
895  * Send message to all remote +g users (server links).
896  *
897  * one - server not to send message to.
898  */
899 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
900 {
901   va_list vl;
902   struct Client *cptr;
903   int i;
904
905   va_start(vl, pattern);
906   ++sentalong_marker;
907   vsprintf_irc(sendbuf, pattern, vl);
908   for (cptr = GlobalClientList; cptr; cptr = cptr->next)
909   {
910     if (!SendDebug(cptr))
911       continue;
912     i = cptr->from->fd;         /* find connection user is on */
913     if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
914       continue;
915     if (MyConnect(cptr))
916       continue;
917     sentalong[i] = sentalong_marker;
918     if (cptr->from == one)
919       continue;
920     sendbufto_one(cptr);
921   }
922   va_end(vl);
923
924   return;
925 }
926
927 /*
928  * sendto_prefix_one
929  *
930  * to - destination client
931  * from - client which message is from
932  *
933  * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
934  * -avalon
935  */
936 /* See sendcmdto_one, below */
937 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
938 {
939   va_list vl;
940   va_start(vl, pattern);
941   vsendto_prefix_one(to, from, pattern, vl);
942   va_end(vl);
943 }
944
945 /*
946  * sendto_realops
947  *
948  * Send to *local* ops only but NOT +s nonopers.
949  */
950 /* See sendto_opmask_butone, below */
951 void sendto_realops(const char *pattern, ...)
952 {
953   va_list vl;
954
955   va_start(vl, pattern);
956   vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
957
958   va_end(vl);
959   return;
960 }
961
962 /*
963  * Send message to all servers of protocol 'p' and lower.
964  */
965 /* See sendcmdto_serv_butone, below */
966 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
967 {
968   va_list vl;
969   struct DLink *lp;
970   va_start(vl, pattern);
971   for (lp = me.serv->down; lp; lp = lp->next)
972     if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
973       vsendto_one(lp->value.cptr, pattern, vl);
974   va_end(vl);
975 }
976
977 /*
978  * Send message to all servers of protocol 'p' and higher.
979  */
980 /* See sendcmdto_serv_butone, below */
981 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
982 {
983   va_list vl;
984   struct DLink *lp;
985   va_start(vl, pattern);
986   for (lp = me.serv->down; lp; lp = lp->next)
987     if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
988       vsendto_one(lp->value.cptr, pattern, vl);
989   va_end(vl);
990 }
991
992 /*
993  * Send a (prefixed) command to a single client; select which of <cmd>
994  * <tok> to use depending on if to is a server or not.  <from> is the
995  * originator of the command.
996  */
997 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
998                    struct Client *to, const char *pattern, ...)
999 {
1000   va_list vl;
1001
1002   va_start(vl, pattern);
1003   vsendcmdto_one(from, cmd, tok, to, pattern, vl);
1004   va_end(vl);
1005 }
1006
1007 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
1008                     struct Client *to, const char *pattern, va_list vl)
1009 {
1010   struct VarData vd;
1011   char sndbuf[IRC_BUFSIZE];
1012
1013   vd.vd_format = pattern; /* set up the struct VarData for %v */
1014   vd.vd_args = vl;
1015
1016   ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from,
1017                 IsServer(to) || IsMe(to) ? tok : cmd, &vd);
1018
1019   send_buffer(to, sndbuf);
1020 }
1021
1022 /*
1023  * Send a (prefixed) command to all servers but one, using tok; cmd
1024  * is ignored in this particular function.
1025  */
1026 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
1027                            const char *tok, struct Client *one,
1028                            const char *pattern, ...)
1029 {
1030   struct VarData vd;
1031   char sndbuf[IRC_BUFSIZE];
1032   struct DLink *lp;
1033
1034   vd.vd_format = pattern; /* set up the struct VarData for %v */
1035   va_start(vd.vd_args, pattern);
1036
1037   /* use token */
1038   ircd_snprintf(&me, sndbuf, sizeof(sndbuf) - 2, "%C %s %v", from, tok, &vd);
1039   va_end(vd.vd_args);
1040
1041   /* send it to our downlinks */
1042   for (lp = me.serv->down; lp; lp = lp->next) {
1043     if (one && lp->value.cptr == one->from)
1044       continue;
1045     send_buffer(lp->value.cptr, sndbuf);
1046   }
1047 }
1048
1049 /*
1050  * Send a (prefix) command originating from <from> to all channels
1051  * <from> is locally on.  <from> must be a user. <tok> is ignored in
1052  * this function.
1053  */
1054 /* XXX sentalong_marker used XXX
1055  *
1056  * There is not an easy way to revoke the need for sentalong_marker
1057  * from this function.  Thoughts and ideas would be welcome... -Kev
1058  *
1059  * One possibility would be to internalize the sentalong array; that
1060  * could be prohibitively big, though.  We could get around that by
1061  * making one that's the number of connected servers or something...
1062  * or perhaps by adding a special flag to the servers we've sent a
1063  * message to, and then a final loop through the connected servers
1064  * to delete the flag. -Kev
1065  */
1066 void sendcmdto_common_channels(struct Client *from, const char *cmd,
1067                                const char *tok, const char *pattern, ...)
1068 {
1069   struct VarData vd;
1070   char sndbuf[IRC_BUFSIZE];
1071   struct Membership *chan;
1072   struct Membership *member;
1073
1074   assert(0 != from);
1075   assert(0 != from->from);
1076   assert(0 != pattern);
1077   assert(!IsServer(from) && !IsMe(from));
1078
1079   vd.vd_format = pattern; /* set up the struct VarData for %v */
1080
1081   va_start(vd.vd_args, pattern);
1082
1083   /* build the buffer */
1084   ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1085   va_end(vd.vd_args);
1086
1087   sentalong_marker++;
1088   if (-1 < from->from->fd)
1089     sentalong[from->from->fd] = sentalong_marker;
1090   /*
1091    * loop through from's channels, and the members on their channels
1092    */
1093   for (chan = from->user->channel; chan; chan = chan->next_channel)
1094     for (member = chan->channel->members; member;
1095          member = member->next_member)
1096       if (MyConnect(member->user) && -1 < from->fd &&
1097           sentalong[member->user->from->fd] != sentalong_marker) {
1098         sentalong[member->user->from->fd] = sentalong_marker;
1099         send_buffer(member->user, sndbuf);
1100       }
1101
1102   if (MyConnect(from))
1103     send_buffer(from, sndbuf);
1104 }
1105
1106 /*
1107  * Send a (prefixed) command to all local users on the channel specified
1108  * by <to>; <tok> is ignored by this function
1109  */
1110 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
1111                                const char *tok, struct Channel *to,
1112                                const char *pattern, ...)
1113 {
1114   struct VarData vd;
1115   char sndbuf[IRC_BUFSIZE];
1116   struct Membership *member;
1117
1118   vd.vd_format = pattern; /* set up the struct VarData for %v */
1119   va_start(vd.vd_args, pattern);
1120
1121   /* build the buffer */
1122   ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1123   va_end(vd.vd_args);
1124
1125   /* send the buffer to each local channel member */
1126   for (member = to->members; member; member = member->next_member) {
1127     if (MyConnect(member->user) && !IsZombie(member))
1128       send_buffer(member->user, sndbuf);
1129   }
1130 }
1131
1132 /*
1133  * Send a (prefixed) command to all users on this channel, including
1134  * remote users; users to skip may be specified by setting appropriate
1135  * flags in the <skip> argument.  <one> will also be skipped.
1136  */
1137 /* XXX sentalong_marker used XXX
1138  *
1139  * We can drop sentalong_marker from this function by adding a field to
1140  * channels and to connections; what we do is make a user's connection
1141  * a "member" of the channel by adding it to the new list, and we use
1142  * the struct Membership status as a reference count.  Then, to implement
1143  * this function, we just walk the list of connections.  Unfortunately,
1144  * this doesn't account for sending only to channel ops, or for not
1145  * sending to +d users; we could account for that by splitting those
1146  * counts out, but that would imply adding two more fields (at least) to
1147  * the struct Membership... -Kev
1148  */
1149 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
1150                               const char *tok, struct Channel *to,
1151                               struct Client *one, unsigned int skip,
1152                               const char *pattern, ...)
1153 {
1154   struct Membership *member;
1155   struct VarData vd;
1156   char userbuf[IRC_BUFSIZE];
1157   char servbuf[IRC_BUFSIZE];
1158
1159   vd.vd_format = pattern;
1160
1161   /* Build buffer to send to users */
1162   va_start(vd.vd_args, pattern);
1163   ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1164   va_end(vd.vd_args);
1165
1166   /* Build buffer to send to servers */
1167   va_start(vd.vd_args, pattern);
1168   ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1169   va_end(vd.vd_args);
1170
1171   /* send buffer along! */
1172   sentalong_marker++;
1173   for (member = to->members; member; member = member->next_member) {
1174     /* skip one, zombies, and deaf users... */
1175     if (member->user->from == one || IsZombie(member) ||
1176         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
1177         (skip & SKIP_NONOPS && !IsChanOp(member)) ||
1178         (skip & SKIP_BURST && IsBurstOrBurstAck(member->user->from)) ||
1179         member->user->from->fd < -1 ||
1180         sentalong[member->user->from->fd] == sentalong_marker)
1181       continue;
1182
1183     if (MyConnect(member->user)) /* pick right buffer to send */
1184       send_buffer(member->user, userbuf);
1185     else
1186       send_buffer(member->user, servbuf);
1187   }
1188 }
1189
1190 /*
1191  * Send a (prefixed) command to all users except <one> that have
1192  * <flag> set.
1193  */
1194 /* XXX sentalong_marker used XXX
1195  *
1196  * Again, we can solve this use of sentalong_marker by adding a field
1197  * to connections--a count of the number of +w users, and another count
1198  * of +g users.  Then, just walk through the local clients to send
1199  * those messages, and another walk through the connected servers list,
1200  * sending only if there's a non-zero count.  No caveats here, either,
1201  * beyond remembering to decrement the count when a user /quit's or is
1202  * killed, or a server is squit. -Kev
1203  */
1204 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
1205                            const char *tok, struct Client *one,
1206                            unsigned int flag, const char *pattern, ...)
1207 {
1208   struct VarData vd;
1209   struct Client *cptr;
1210   char userbuf[IRC_BUFSIZE];
1211   char servbuf[IRC_BUFSIZE];
1212
1213   vd.vd_format = pattern;
1214
1215   /* Build buffer to send to users */
1216   va_start(vd.vd_args, pattern);
1217   ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1218   va_end(vd.vd_args);
1219
1220   /* Build buffer to send to servers */
1221   va_start(vd.vd_args, pattern);
1222   ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1223   va_end(vd.vd_args);
1224
1225   /* send buffer along! */
1226   sentalong_marker++;
1227   for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1228     if (cptr->from == one || IsServer(cptr) || !(cptr->flags & flag) ||
1229         cptr->from->fd < 0 || sentalong[cptr->from->fd] == sentalong_marker)
1230       continue; /* skip it */
1231
1232     if (MyConnect(cptr)) /* send right buffer */
1233       send_buffer(cptr, userbuf);
1234     else
1235       send_buffer(cptr, servbuf);
1236   }
1237 }
1238
1239 /*
1240  * Send a (prefixed) command to all users who match <to>, under control
1241  * of <who>
1242  */
1243 /* XXX sentalong_marker used XXX
1244  *
1245  * This is also a difficult one to solve.  The basic approach would be
1246  * to walk the client list of each connected server until we find a
1247  * match--but then, we also have to walk the client list of all the
1248  * servers behind that one.  We could implement this recursively--or we
1249  * could add (yet another) field to the connection struct that would be
1250  * a linked list of clients introduced through that link, and just walk
1251  * that, making this into an iterative implementation.  Unfortunately,
1252  * we probably would not be able to use tail recursion for the recursive
1253  * solution, so a deep network could exhaust our stack space; therefore
1254  * I favor the extra linked list, even though that increases the
1255  * complexity of the database. -Kev
1256  */
1257 void sendcmdto_match_butone(struct Client *from, const char *cmd,
1258                             const char *tok, const char *to,
1259                             struct Client *one, unsigned int who,
1260                             const char *pattern, ...)
1261 {
1262   struct VarData vd;
1263   struct Client *cptr;
1264   char userbuf[IRC_BUFSIZE];
1265   char servbuf[IRC_BUFSIZE];
1266
1267   vd.vd_format = pattern;
1268
1269   /* Build buffer to send to users */
1270   va_start(vd.vd_args, pattern);
1271   ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1272   va_end(vd.vd_args);
1273
1274   /* Build buffer to send to servers */
1275   va_start(vd.vd_args, pattern);
1276   ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1277   va_end(vd.vd_args);
1278
1279   /* send buffer along */
1280   sentalong_marker++;
1281   for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1282     if (cptr->from == one || IsServer(cptr) || !match_it(cptr, to, who) ||
1283         cptr->from->fd < 0 || sentalong[cptr->from->fd] == sentalong_marker)
1284       continue; /* skip it */
1285
1286     if (MyConnect(cptr)) /* send right buffer */
1287       send_buffer(cptr, userbuf);
1288     else
1289       send_buffer(cptr, servbuf);
1290   }
1291 }
1292
1293 /*
1294  * Send a server notice to all users subscribing to the indicated <mask>
1295  * except for <one>
1296  */
1297 void sendto_opmask_butone(struct Client *one, unsigned int mask,
1298                           const char *pattern, ...)
1299 {
1300   va_list vl;
1301
1302   va_start(vl, pattern);
1303   vsendto_opmask_butone(one, mask, pattern, vl);
1304   va_end(vl);
1305 }
1306
1307 /*
1308  * Same as above, except called with a variable argument list
1309  */
1310 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
1311                            const char *pattern, va_list vl)
1312 {
1313   struct VarData vd;
1314   char sndbuf[IRC_BUFSIZE];
1315   int i = 0; /* so that 1 points to opsarray[0] */
1316   struct SLink *opslist;
1317
1318   while ((mask >>= 1))
1319     i++;
1320
1321   if (!(opslist = opsarray[i]))
1322     return;
1323
1324   /*
1325    * build string; I don't want to bother with client nicknames, so I hope
1326    * this is ok...
1327    */
1328   vd.vd_format = pattern;
1329   vd.vd_args = vl;
1330   ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s " MSG_NOTICE
1331                 " * :*** Notice -- %v", me.name, &vd);
1332
1333   for (; opslist; opslist = opslist->next)
1334     send_buffer(opslist->value.cptr, sndbuf);
1335 }