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_opmask_butone(0, SNO_OLDSNO, "%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_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
238                            "%zu > %zu", to, DBufLength(&to->sendQ),
239                            get_sendq(to));
240     dead_link(to, "Max sendQ exceeded");
241     return;
242   }
243
244   Debug((DEBUG_SEND, "Sending [%s] to %s", buf, to->name));
245
246   len = strlen(buf);
247   if (buf[len - 1] != '\n') {
248     if (len > 510)
249       len = 510;
250     buf[len++] = '\r';
251     buf[len++] = '\n';
252     buf[len] = '\0';
253   }
254
255   if (0 == dbuf_put(&to->sendQ, buf, len)) {
256     dead_link(to, "Buffer allocation error");
257     return;
258   }
259
260 #ifdef GODMODE
261   send_to_god(to, buf);
262 #endif /* GODMODE */
263   /*
264    * Update statistics. The following is slightly incorrect
265    * because it counts messages even if queued, but bytes
266    * only really sent. Queued bytes get updated in SendQueued.
267    */
268   ++to->sendM;
269   ++me.sendM;
270   /*
271    * This little bit is to stop the sendQ from growing too large when
272    * there is no need for it to. Thus we call send_queued() every time
273    * 2k has been added to the queue since the last non-fatal write.
274    * Also stops us from deliberately building a large sendQ and then
275    * trying to flood that link with data (possible during the net
276    * relinking done by servers with a large load).
277    */
278   if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
279     send_queued(to);
280 }
281
282 void sendbufto_one(struct Client* to)
283 {
284   send_buffer(to, sendbuf);
285 }
286
287 /* See vsendcmdto_one, below */
288 static void vsendto_prefix_one(struct Client *to, struct Client *from,
289     const char* pattern, va_list vl)
290 {
291   if (to && from && MyUser(to) && IsUser(from))
292   {
293     static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
294     char *par;
295     int flag = 0;
296     struct User *user = from->user;
297
298     par = va_arg(vl, char *);
299     strcpy(sender, from->name);
300     if (user)
301     {
302       if (*user->username)
303       {
304         strcat(sender, "!");
305         strcat(sender, user->username);
306       }
307       if (*user->host && !MyConnect(from))
308       {
309         strcat(sender, "@");
310         strcat(sender, user->host);
311         flag = 1;
312       }
313     }
314     /*
315      * Flag is used instead of strchr(sender, '@') for speed and
316      * also since username/nick may have had a '@' in them. -avalon
317      */
318     if (!flag && MyConnect(from) && *user->host)
319     {
320       strcat(sender, "@");
321       strcat(sender, from->sockhost);
322     }
323     *sendbuf = ':';
324     strcpy(&sendbuf[1], sender);
325     /* Assuming 'pattern' always starts with ":%s ..." */
326     vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
327   }
328   else
329     vsprintf_irc(sendbuf, pattern, vl);
330   sendbufto_one(to);
331 }
332
333 /* See sendcmdto_channel_butone, below */
334 void sendto_channel_butone(struct Client *one, struct Client *from, struct Channel *chptr,
335     const char* pattern, ...)
336 {
337   va_list vl;
338   struct Membership* member;
339   struct Client *acptr;
340   int i;
341
342   va_start(vl, pattern);
343
344   ++sentalong_marker;
345   for (member = chptr->members; member; member = member->next_member)
346   {
347     acptr = member->user;
348     /* ...was the one I should skip */
349     if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
350       continue;
351     if (MyConnect(acptr))       /* (It is always a client) */
352       vsendto_prefix_one(acptr, from, pattern, vl);
353     else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
354     {
355       sentalong[i] = sentalong_marker;
356       /*
357        * Don't send channel messages to links that are still eating
358        * the net.burst: -- Run 2/1/1997
359        */
360       if (!IsBurstOrBurstAck(acptr->from))
361         vsendto_prefix_one(acptr, from, pattern, vl);
362     }
363   }
364   va_end(vl);
365 }
366
367 /* See sendcmdto_channel_butone, below */
368 void sendmsgto_channel_butone(struct Client *one, struct Client *from,
369                               struct Channel *chptr, const char *sender,
370                               const char *cmd, const char *chname, const char *msg)
371 {
372  /*
373   * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
374   * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
375   */
376   struct Membership* member;
377   struct Client *acptr;
378   char userbuf[2048];
379   char servbuf[2048];
380   int i;
381   int flag=-1;
382
383   assert(0 != cmd);
384   /* 
385    * Precalculate the buffers we sent to the clients instead of doing an
386    * expensive sprintf() per member that we send to.  We still have to
387    * use strcpy() which is evil.
388    */
389   if (IsServer(from)) {
390     sprintf(userbuf,":%s %s %s :%s",
391             from->name, ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
392     sprintf(servbuf,"%s %s %s :%s", NumServ(from), cmd, chname, msg);
393   }
394   else {
395     sprintf(userbuf,":%s!%s@%s %s %s :%s",
396             from->name, from->user->username, from->user->host,
397             ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
398     sprintf(servbuf,"%s%s %s %s :%s", NumNick(from), cmd, chname, msg);
399   }
400
401   ++sentalong_marker;
402   for (member = chptr->members; member; member = member->next_member)
403   {
404     acptr = member->user;
405     /* ...was the one I should skip */
406     if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
407       continue;
408     if (MyConnect(acptr)) {      /* (It is always a client) */
409         if (flag!=0)
410           strcpy(sendbuf,userbuf);
411         flag=0;
412         sendbufto_one(acptr);
413     }
414     else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
415     {
416       sentalong[i] = sentalong_marker;
417       if (!IsBurstOrBurstAck(acptr->from)) {
418         if (flag != 1)
419           strcpy(sendbuf,servbuf);
420         flag = 1;
421         sendbufto_one(acptr);
422   }
423     } /* of if MyConnect() */
424   } /* of for(members) */
425 }
426
427 /* See sendcmdto_channel_butone, below */
428 void sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
429     const char* pattern, ...)
430 {
431   va_list vl;
432   struct Membership* member;
433   struct Client *acptr;
434
435   va_start(vl, pattern);
436
437   for (member = chptr->members; member; member = member->next_member)
438   {
439     acptr = member->user;
440     /* ...was the one I should skip */
441     if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
442       continue;
443     if (MyConnect(acptr))       /* (It is always a client) */
444       vsendto_prefix_one(acptr, from, pattern, vl);
445   }
446   va_end(vl);
447   return;
448 }
449
450 /* See sendcmdto_channel_butone, below */
451 void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
452     const char* pattern, ...)
453 {
454   va_list vl;
455   struct Membership* member;
456   struct Client *acptr;
457   int i;
458 #ifndef NO_PROTOCOL9
459   char  target[128];
460   char* source;
461   char* tp;
462   char* msg;
463 #endif
464
465   va_start(vl, pattern);
466
467   ++sentalong_marker;
468   for (member = chptr->members; member; member = member->next_member)
469   {
470     acptr = member->user;
471     if (acptr->from == acptr || /* Skip local clients */
472 #ifndef NO_PROTOCOL9
473         Protocol(acptr->from) < 10 ||   /* Skip P09 links */
474 #endif
475         acptr->from == one ||   /* ...was the one I should skip */
476         !IsChanOp(member) ||   /* Skip non chanops */
477         IsZombie(member) || IsDeaf(acptr))
478       continue;
479     if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
480     {
481       sentalong[i] = sentalong_marker;
482       /* Don't send channel messages to links that are
483          still eating the net.burst: -- Run 2/1/1997 */
484       if (!IsBurstOrBurstAck(acptr->from))
485         vsendto_prefix_one(acptr, from, pattern, vl);
486     }
487   }
488
489 #ifndef NO_PROTOCOL9
490   /* Send message to all 2.9 servers */
491   /* This is a hack, because it assumes that we know how `vl' is build up */
492   source = va_arg(vl, char *);
493   tp = va_arg(vl, char *);      /* Channel */
494   msg = va_arg(vl, char *);
495   for (member = chptr->members; member; member = member->next_member)
496   {
497     acptr = member->user;
498     if (acptr->from == acptr || /* Skip local clients */
499         Protocol(acptr->from) > 9 ||    /* Skip P10 servers */
500         acptr->from == one ||   /* ...was the one I should skip */
501         !IsChanOp(member) ||   /* Skip non chanops */
502         IsZombie(member) || IsDeaf(acptr))
503       continue;
504     if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
505     {
506       sentalong[i] = sentalong_marker;
507       /* Don't send channel messages to links that are
508          still eating the net.burst: -- Run 2/1/1997 */
509       if (!IsBurstOrBurstAck(acptr->from))
510       {
511         struct Membership* other_member;
512         struct Client* acptr2;
513         tp = target;
514         *tp = 0;
515         /* Find all chanops in this direction: */
516         for (other_member = chptr->members; other_member; other_member = other_member->next_member)
517         {
518           acptr2 = other_member->user;
519           if (acptr2->from == acptr->from && acptr2->from != one &&
520               IsChanOp(other_member) && !IsZombie(other_member) &&
521               !IsDeaf(acptr2))
522           {
523             int len = strlen(acptr2->name);
524             if (tp + len + 2 > target + sizeof(target))
525             {
526               sendto_prefix_one(acptr, from,
527                   ":%s NOTICE %s :%s", source, target, msg);
528               tp = target;
529               *tp = 0;
530             }
531             if (*target)
532               strcpy(tp++, ",");
533             strcpy(tp, acptr2->name);
534             tp += len;
535           }
536         }
537         sendto_prefix_one(acptr, from,
538             ":%s NOTICE %s :%s", source, target, msg);
539       }
540     }
541   }
542 #endif
543
544   va_end(vl);
545   return;
546 }
547
548 /*
549  * sendto_serv_butone
550  *
551  * Send a message to all connected servers except the client 'one'.
552  */
553 /* See sendcmdto_serv_butone, below */
554 void sendto_serv_butone(struct Client *one, const char* pattern, ...)
555 {
556   va_list vl;
557   struct DLink *lp;
558
559   va_start(vl, pattern);
560   vsprintf_irc(sendbuf, pattern, vl);
561   va_end(vl);
562
563   for (lp = me.serv->down; lp; lp = lp->next)
564   {
565     if (one && lp->value.cptr == one->from)
566       continue;
567     sendbufto_one(lp->value.cptr);
568   }
569
570 }
571
572 /*
573  * sendbufto_serv_butone()
574  *
575  * Send prepared sendbuf to all connected servers except the client 'one'
576  *  -Ghostwolf 18-May-97
577  */
578 void sendbufto_serv_butone(struct Client *one)
579 {
580   struct DLink *lp;
581
582   for (lp = me.serv->down; lp; lp = lp->next)
583   {
584     if (one && lp->value.cptr == one->from)
585       continue;
586     sendbufto_one(lp->value.cptr);
587   }
588 }
589
590
591 /*
592  * sendto_common_channels()
593  *
594  * Sends a message to all people (inclusing `acptr') on local server
595  * who are in same channel with client `acptr'.
596  */
597 /* See sendcmdto_common_channels, below */
598 void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
599 {
600   va_list vl;
601   struct Membership* chan;
602   struct Membership* member;
603
604   assert(0 != acptr);
605   assert(0 != acptr->from);
606   assert(0 != pattern);
607
608   va_start(vl, pattern);
609
610   ++sentalong_marker;
611   if (-1 < acptr->from->fd)
612     sentalong[acptr->from->fd] = sentalong_marker;
613   /*
614    * loop through acptr's channels, and the members on their channels
615    */
616   if (acptr->user) {
617     for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
618       for (member = chan->channel->members; member; member = member->next_member) {
619         struct Client *cptr = member->user;
620         int    i;
621         if (MyConnect(cptr) && 
622             -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
623           sentalong[i] = sentalong_marker;
624           vsendto_prefix_one(cptr, acptr, pattern, vl);
625         }
626       }
627     }
628   }
629   if (MyConnect(acptr))
630     vsendto_prefix_one(acptr, acptr, pattern, vl);
631   va_end(vl);
632   return;
633 }
634
635 /*
636  * sendto_channel_butserv
637  *
638  * Send a message to all members of a channel that
639  * are connected to this server.
640  *
641  * This contains a subtle bug; after the first call to vsendto_prefix_one()
642  * below, vl is in an indeterminate state, according to ANSI; we'd have to
643  * move va_start() and va_end() into the loop to correct the problem.  It's
644  * easier, however, just to use sendcmdto_channel_butserv(), which builds a
645  * buffer and sends that prepared buffer to each channel member.
646  */
647 /* See sendcmdto_channel_butserv, below */
648 void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
649 {
650   va_list vl;
651   struct Membership* member;
652   struct Client *acptr;
653   
654   va_start(vl, pattern);
655
656   for (member = chptr->members; member; member = member->next_member) {
657     acptr = member->user;
658     if (MyConnect(acptr) && !IsZombie(member))
659       vsendto_prefix_one(acptr, from, pattern, vl);
660   }
661   va_end(vl);
662   return;
663 }
664
665 /*
666  * Send a msg to all ppl on servers/hosts that match a specified mask
667  * (used for enhanced PRIVMSGs)
668  *
669  *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
670  */
671
672 static int match_it(struct Client *one, const char *mask, int what)
673 {
674   switch (what)
675   {
676     case MATCH_HOST:
677       return (match(mask, one->user->host) == 0);
678     case MATCH_SERVER:
679     default:
680       return (match(mask, one->user->server->name) == 0);
681   }
682 }
683
684 /*
685  * sendto_match_butone
686  *
687  * Send to all clients which match the mask in a way defined on 'what';
688  * either by user hostname or user servername.
689  */
690 /* See sendcmdto_match_butone, below */
691 void sendto_match_butone(struct Client *one, struct Client *from,
692     const char *mask, int what, const char* pattern, ...)
693 {
694   va_list vl;
695   int i;
696   struct Client *cptr, *acptr;
697
698   va_start(vl, pattern);
699   for (i = 0; i <= HighestFd; i++)
700   {
701     if (!(cptr = LocalClientArray[i]))
702       continue;                 /* that clients are not mine */
703     if (cptr == one)            /* must skip the origin !! */
704       continue;
705     if (IsServer(cptr))
706     {
707       for (acptr = GlobalClientList; acptr; acptr = acptr->next)
708         if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
709           break;
710       /* a person on that server matches the mask, so we
711        *  send *one* msg to that server ...
712        */
713       if (acptr == NULL)
714         continue;
715       /* ... but only if there *IS* a matching person */
716     }
717     /* my client, does he match ? */
718     else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
719       continue;
720     vsendto_prefix_one(cptr, from, pattern, vl);
721   }
722   va_end(vl);
723
724   return;
725 }
726
727 /*
728  * sendto_lops_butone
729  *
730  * Send to *local* ops but one.
731  */
732 /* See sendto_opmask_butone, below */
733 void sendto_lops_butone(struct Client* one, const char* pattern, ...)
734 {
735   va_list         vl;
736   struct Client*  cptr;
737   struct Client** clients = me.serv->client_list;
738   int             i;
739   char            nbuf[1024];
740
741   assert(0 != clients);
742
743   sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
744   va_start(vl, pattern);
745   vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
746   va_end(vl);
747
748   for (i = 0; i <= me.serv->nn_mask; ++i) {
749     if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
750       sprintf_irc(sendbuf, nbuf, cptr->name);
751       sendbufto_one(cptr);
752     }
753   }
754 }
755
756 /*
757  * sendto_op_mask
758  *
759  * Sends message to the list indicated by the bitmask field.
760  * Don't try to send to more than one list! That is not supported.
761  * Xorath 5/1/97
762  */
763 #ifdef OLD_VSENDTO_OP_MASK
764 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
765 {
766   static char fmt[1024];
767   char *fmt_target;
768   int i = 0;            /* so that 1 points to opsarray[0] */
769   struct SLink *opslist;
770
771   while ((mask >>= 1))
772     i++;
773   if (!(opslist = opsarray[i]))
774     return;
775
776   fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
777   do
778   {
779     strcpy(fmt_target, opslist->value.cptr->name);
780     strcat(fmt_target, " :*** Notice -- ");
781     strcat(fmt_target, pattern);
782     vsendto_one(opslist->value.cptr, fmt, vl);
783     opslist = opslist->next;
784   }
785   while (opslist);
786 }
787 #else /* !OLD_VSENDTO_OP_MASK */
788 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
789 {
790   vsendto_opmask_butone(0, mask, pattern, vl);
791 }
792 #endif /* OLD_VSENDTO_OP_MASK */
793
794 /*
795  * sendbufto_op_mask
796  *
797  * Send a prepared sendbuf to the list indicated by the bitmask field.
798  * Ghostwolf 16-May-97
799  */
800 void sendbufto_op_mask(unsigned int mask)
801 {
802   int i = 0;            /* so that 1 points to opsarray[0] */
803   struct SLink *opslist;
804   while ((mask >>= 1))
805     i++;
806   if (!(opslist = opsarray[i]))
807     return;
808   do
809   {
810     sendbufto_one(opslist->value.cptr);
811     opslist = opslist->next;
812   }
813   while (opslist);
814 }
815
816
817 /*
818  * sendto_ops
819  *
820  * Send to *local* ops only.
821  */
822 /* See vsendto_opmask_butone, below */
823 void vsendto_ops(const char *pattern, va_list vl)
824 {
825   struct Client *cptr;
826   int i;
827   char fmt[1024];
828   char *fmt_target;
829
830   fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
831
832   for (i = 0; i <= HighestFd; i++)
833     if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
834         SendServNotice(cptr))
835     {
836       strcpy(fmt_target, cptr->name);
837       strcat(fmt_target, " :*** Notice -- ");
838       strcat(fmt_target, pattern);
839       vsendto_one(cptr, fmt, vl);
840     }
841 }
842
843 /* See sendto_opmask_butone, below */
844 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
845 {
846   va_list vl;
847   va_start(vl, pattern);
848   vsendto_op_mask(mask, pattern, vl);
849   va_end(vl);
850 }
851
852 /* See sendto_opmask_butone, below */
853 void sendto_ops(const char *pattern, ...)
854 {
855   va_list vl;
856   va_start(vl, pattern);
857   vsendto_op_mask(SNO_OLDSNO, pattern, vl);
858   va_end(vl);
859 }
860
861 /*
862  * sendto_ops_butone
863  *
864  * Send message to all operators.
865  * one - client not to send message to
866  * from- client which message is from *NEVER* NULL!!
867  */
868 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
869 {
870   va_list vl;
871   int i;
872   struct Client *cptr;
873
874   va_start(vl, pattern);
875   ++sentalong_marker;
876   for (cptr = GlobalClientList; cptr; cptr = cptr->next)
877   {
878     if (!SendWallops(cptr))
879       continue;
880     i = cptr->from->fd;         /* find connection oper is on */
881     if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
882       continue;
883     if (cptr->from == one)
884       continue;                 /* ...was the one I should skip */
885     sentalong[i] = sentalong_marker;
886     vsendto_prefix_one(cptr->from, from, pattern, vl);
887   }
888   va_end(vl);
889
890   return;
891 }
892
893 /*
894  * sendto_g_serv_butone
895  *
896  * Send message to all remote +g users (server links).
897  *
898  * one - server not to send message to.
899  */
900 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
901 {
902   va_list vl;
903   struct Client *cptr;
904   int i;
905
906   va_start(vl, pattern);
907   ++sentalong_marker;
908   vsprintf_irc(sendbuf, pattern, vl);
909   for (cptr = GlobalClientList; cptr; cptr = cptr->next)
910   {
911     if (!SendDebug(cptr))
912       continue;
913     i = cptr->from->fd;         /* find connection user is on */
914     if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
915       continue;
916     if (MyConnect(cptr))
917       continue;
918     sentalong[i] = sentalong_marker;
919     if (cptr->from == one)
920       continue;
921     sendbufto_one(cptr);
922   }
923   va_end(vl);
924
925   return;
926 }
927
928 /*
929  * sendto_prefix_one
930  *
931  * to - destination client
932  * from - client which message is from
933  *
934  * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
935  * -avalon
936  */
937 /* See sendcmdto_one, below */
938 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
939 {
940   va_list vl;
941   va_start(vl, pattern);
942   vsendto_prefix_one(to, from, pattern, vl);
943   va_end(vl);
944 }
945
946 /*
947  * sendto_realops
948  *
949  * Send to *local* ops only but NOT +s nonopers.
950  */
951 /* See sendto_opmask_butone, below */
952 void sendto_realops(const char *pattern, ...)
953 {
954   va_list vl;
955
956   va_start(vl, pattern);
957   vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
958
959   va_end(vl);
960   return;
961 }
962
963 /*
964  * Send message to all servers of protocol 'p' and lower.
965  */
966 /* See sendcmdto_serv_butone, below */
967 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
968 {
969   va_list vl;
970   struct DLink *lp;
971   va_start(vl, pattern);
972   for (lp = me.serv->down; lp; lp = lp->next)
973     if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
974       vsendto_one(lp->value.cptr, pattern, vl);
975   va_end(vl);
976 }
977
978 /*
979  * Send message to all servers of protocol 'p' and higher.
980  */
981 /* See sendcmdto_serv_butone, below */
982 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
983 {
984   va_list vl;
985   struct DLink *lp;
986   va_start(vl, pattern);
987   for (lp = me.serv->down; lp; lp = lp->next)
988     if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
989       vsendto_one(lp->value.cptr, pattern, vl);
990   va_end(vl);
991 }
992
993 /*
994  * Send a raw command to a single client; use *ONLY* if you absolutely
995  * must send a command without a prefix.
996  */
997 void sendrawto_one(struct Client *to, const char *pattern, ...)
998 {
999   char sndbuf[IRC_BUFSIZE];
1000   va_list vl;
1001
1002   va_start(vl, pattern);
1003   ircd_vsnprintf(to, sndbuf, sizeof(sndbuf) - 2, pattern, vl);
1004   va_end(vl);
1005
1006   send_buffer(to, sndbuf);
1007 }
1008
1009 /*
1010  * Send a (prefixed) command to a single client; select which of <cmd>
1011  * <tok> to use depending on if to is a server or not.  <from> is the
1012  * originator of the command.
1013  */
1014 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
1015                    struct Client *to, const char *pattern, ...)
1016 {
1017   va_list vl;
1018
1019   va_start(vl, pattern);
1020   vsendcmdto_one(from, cmd, tok, to, pattern, vl);
1021   va_end(vl);
1022 }
1023
1024 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
1025                     struct Client *to, const char *pattern, va_list vl)
1026 {
1027   struct VarData vd;
1028   char sndbuf[IRC_BUFSIZE];
1029
1030   to = to->from;
1031
1032   vd.vd_format = pattern; /* set up the struct VarData for %v */
1033   vd.vd_args = vl;
1034
1035   ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from,
1036                 IsServer(to) || IsMe(to) ? tok : cmd, &vd);
1037
1038   send_buffer(to, sndbuf);
1039 }
1040
1041 /*
1042  * Send a (prefixed) command to all servers but one, using tok; cmd
1043  * is ignored in this particular function.
1044  */
1045 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
1046                            const char *tok, struct Client *one,
1047                            const char *pattern, ...)
1048 {
1049   struct VarData vd;
1050   char sndbuf[IRC_BUFSIZE];
1051   struct DLink *lp;
1052
1053   vd.vd_format = pattern; /* set up the struct VarData for %v */
1054   va_start(vd.vd_args, pattern);
1055
1056   /* use token */
1057   ircd_snprintf(&me, sndbuf, sizeof(sndbuf) - 2, "%C %s %v", from, tok, &vd);
1058   va_end(vd.vd_args);
1059
1060   /* send it to our downlinks */
1061   for (lp = me.serv->down; lp; lp = lp->next) {
1062     if (one && lp->value.cptr == one->from)
1063       continue;
1064     send_buffer(lp->value.cptr, sndbuf);
1065   }
1066 }
1067
1068 /*
1069  * Send a (prefix) command originating from <from> to all channels
1070  * <from> is locally on.  <from> must be a user. <tok> is ignored in
1071  * this function.
1072  */
1073 /* XXX sentalong_marker used XXX
1074  *
1075  * There is not an easy way to revoke the need for sentalong_marker
1076  * from this function.  Thoughts and ideas would be welcome... -Kev
1077  *
1078  * One possibility would be to internalize the sentalong array; that
1079  * could be prohibitively big, though.  We could get around that by
1080  * making one that's the number of connected servers or something...
1081  * or perhaps by adding a special flag to the servers we've sent a
1082  * message to, and then a final loop through the connected servers
1083  * to delete the flag. -Kev
1084  */
1085 void sendcmdto_common_channels(struct Client *from, const char *cmd,
1086                                const char *tok, const char *pattern, ...)
1087 {
1088   struct VarData vd;
1089   char sndbuf[IRC_BUFSIZE];
1090   struct Membership *chan;
1091   struct Membership *member;
1092
1093   assert(0 != from);
1094   assert(0 != from->from);
1095   assert(0 != pattern);
1096   assert(!IsServer(from) && !IsMe(from));
1097
1098   vd.vd_format = pattern; /* set up the struct VarData for %v */
1099
1100   va_start(vd.vd_args, pattern);
1101
1102   /* build the buffer */
1103   ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1104   va_end(vd.vd_args);
1105
1106   sentalong_marker++;
1107   if (-1 < from->from->fd)
1108     sentalong[from->from->fd] = sentalong_marker;
1109   /*
1110    * loop through from's channels, and the members on their channels
1111    */
1112   for (chan = from->user->channel; chan; chan = chan->next_channel)
1113     for (member = chan->channel->members; member;
1114          member = member->next_member)
1115       if (MyConnect(member->user) && -1 < member->user->from->fd &&
1116           sentalong[member->user->from->fd] != sentalong_marker) {
1117         sentalong[member->user->from->fd] = sentalong_marker;
1118         send_buffer(member->user, sndbuf);
1119       }
1120
1121   if (MyConnect(from))
1122     send_buffer(from, sndbuf);
1123 }
1124
1125 /*
1126  * Send a (prefixed) command to all local users on the channel specified
1127  * by <to>; <tok> is ignored by this function
1128  */
1129 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
1130                                const char *tok, struct Channel *to,
1131                                const char *pattern, ...)
1132 {
1133   struct VarData vd;
1134   char sndbuf[IRC_BUFSIZE];
1135   struct Membership *member;
1136
1137   vd.vd_format = pattern; /* set up the struct VarData for %v */
1138   va_start(vd.vd_args, pattern);
1139
1140   /* build the buffer */
1141   ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1142   va_end(vd.vd_args);
1143
1144   /* send the buffer to each local channel member */
1145   for (member = to->members; member; member = member->next_member) {
1146     if (MyConnect(member->user) && !IsZombie(member))
1147       send_buffer(member->user, sndbuf);
1148   }
1149 }
1150
1151 /*
1152  * Send a (prefixed) command to all users on this channel, including
1153  * remote users; users to skip may be specified by setting appropriate
1154  * flags in the <skip> argument.  <one> will also be skipped.
1155  */
1156 /* XXX sentalong_marker used XXX
1157  *
1158  * We can drop sentalong_marker from this function by adding a field to
1159  * channels and to connections; what we do is make a user's connection
1160  * a "member" of the channel by adding it to the new list, and we use
1161  * the struct Membership status as a reference count.  Then, to implement
1162  * this function, we just walk the list of connections.  Unfortunately,
1163  * this doesn't account for sending only to channel ops, or for not
1164  * sending to +d users; we could account for that by splitting those
1165  * counts out, but that would imply adding two more fields (at least) to
1166  * the struct Membership... -Kev
1167  */
1168 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
1169                               const char *tok, struct Channel *to,
1170                               struct Client *one, unsigned int skip,
1171                               const char *pattern, ...)
1172 {
1173   struct Membership *member;
1174   struct VarData vd;
1175   char userbuf[IRC_BUFSIZE];
1176   char servbuf[IRC_BUFSIZE];
1177
1178   vd.vd_format = pattern;
1179
1180   /* Build buffer to send to users */
1181   va_start(vd.vd_args, pattern);
1182   ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1183   va_end(vd.vd_args);
1184
1185   /* Build buffer to send to servers */
1186   va_start(vd.vd_args, pattern);
1187   ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1188   va_end(vd.vd_args);
1189
1190   /* send buffer along! */
1191   sentalong_marker++;
1192   for (member = to->members; member; member = member->next_member) {
1193     /* skip one, zombies, and deaf users... */
1194     if (member->user->from == one || IsZombie(member) ||
1195         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
1196         (skip & SKIP_NONOPS && !IsChanOp(member)) ||
1197         (skip & SKIP_BURST && IsBurstOrBurstAck(member->user->from)) ||
1198         member->user->from->fd < 0 ||
1199         sentalong[member->user->from->fd] == sentalong_marker)
1200       continue;
1201     sentalong[member->user->from->fd] = sentalong_marker;
1202
1203     if (MyConnect(member->user)) /* pick right buffer to send */
1204       send_buffer(member->user, userbuf);
1205     else
1206       send_buffer(member->user, servbuf);
1207   }
1208 }
1209
1210 /*
1211  * Send a (prefixed) command to all users except <one> that have
1212  * <flag> set.
1213  */
1214 /* XXX sentalong_marker used XXX
1215  *
1216  * Again, we can solve this use of sentalong_marker by adding a field
1217  * to connections--a count of the number of +w users, and another count
1218  * of +g users.  Then, just walk through the local clients to send
1219  * those messages, and another walk through the connected servers list,
1220  * sending only if there's a non-zero count.  No caveats here, either,
1221  * beyond remembering to decrement the count when a user /quit's or is
1222  * killed, or a server is squit. -Kev
1223  */
1224 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
1225                            const char *tok, struct Client *one,
1226                            unsigned int flag, const char *pattern, ...)
1227 {
1228   struct VarData vd;
1229   struct Client *cptr;
1230   char userbuf[IRC_BUFSIZE];
1231   char servbuf[IRC_BUFSIZE];
1232
1233   vd.vd_format = pattern;
1234
1235   /* Build buffer to send to users */
1236   va_start(vd.vd_args, pattern);
1237   ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C " MSG_WALLOPS " %v",
1238                 from, &vd);
1239   va_end(vd.vd_args);
1240
1241   /* Build buffer to send to servers */
1242   va_start(vd.vd_args, pattern);
1243   ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1244   va_end(vd.vd_args);
1245
1246   /* send buffer along! */
1247   sentalong_marker++;
1248   for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1249     if (cptr->from == one || IsServer(cptr) || !(cptr->flags & flag) ||
1250         cptr->from->fd < 0 || sentalong[cptr->from->fd] == sentalong_marker)
1251       continue; /* skip it */
1252     sentalong[cptr->from->fd] = sentalong_marker;
1253
1254     if (MyConnect(cptr)) /* send right buffer */
1255       send_buffer(cptr, userbuf);
1256     else
1257       send_buffer(cptr, servbuf);
1258   }
1259 }
1260
1261 /*
1262  * Send a (prefixed) command to all users who match <to>, under control
1263  * of <who>
1264  */
1265 /* XXX sentalong_marker used XXX
1266  *
1267  * This is also a difficult one to solve.  The basic approach would be
1268  * to walk the client list of each connected server until we find a
1269  * match--but then, we also have to walk the client list of all the
1270  * servers behind that one.  We could implement this recursively--or we
1271  * could add (yet another) field to the connection struct that would be
1272  * a linked list of clients introduced through that link, and just walk
1273  * that, making this into an iterative implementation.  Unfortunately,
1274  * we probably would not be able to use tail recursion for the recursive
1275  * solution, so a deep network could exhaust our stack space; therefore
1276  * I favor the extra linked list, even though that increases the
1277  * complexity of the database. -Kev
1278  */
1279 void sendcmdto_match_butone(struct Client *from, const char *cmd,
1280                             const char *tok, const char *to,
1281                             struct Client *one, unsigned int who,
1282                             const char *pattern, ...)
1283 {
1284   struct VarData vd;
1285   struct Client *cptr;
1286   char userbuf[IRC_BUFSIZE];
1287   char servbuf[IRC_BUFSIZE];
1288
1289   vd.vd_format = pattern;
1290
1291   /* Build buffer to send to users */
1292   va_start(vd.vd_args, pattern);
1293   ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1294   va_end(vd.vd_args);
1295
1296   /* Build buffer to send to servers */
1297   va_start(vd.vd_args, pattern);
1298   ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1299   va_end(vd.vd_args);
1300
1301   /* send buffer along */
1302   sentalong_marker++;
1303   for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1304     if (cptr->from == one || IsServer(cptr) || IsMe(cptr) ||
1305         !match_it(cptr, to, who) || cptr->from->fd < 0 ||
1306         sentalong[cptr->from->fd] == sentalong_marker)
1307       continue; /* skip it */
1308     sentalong[cptr->from->fd] = sentalong_marker;
1309
1310     if (MyConnect(cptr)) /* send right buffer */
1311       send_buffer(cptr, userbuf);
1312     else
1313       send_buffer(cptr, servbuf);
1314   }
1315 }
1316
1317 /*
1318  * Send a server notice to all users subscribing to the indicated <mask>
1319  * except for <one>
1320  */
1321 void sendto_opmask_butone(struct Client *one, unsigned int mask,
1322                           const char *pattern, ...)
1323 {
1324   va_list vl;
1325
1326   va_start(vl, pattern);
1327   vsendto_opmask_butone(one, mask, pattern, vl);
1328   va_end(vl);
1329 }
1330
1331 /*
1332  * Same as above, except called with a variable argument list
1333  */
1334 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
1335                            const char *pattern, va_list vl)
1336 {
1337   struct VarData vd;
1338   char sndbuf[IRC_BUFSIZE];
1339   int i = 0; /* so that 1 points to opsarray[0] */
1340   struct SLink *opslist;
1341
1342   while ((mask >>= 1))
1343     i++;
1344
1345   if (!(opslist = opsarray[i]))
1346     return;
1347
1348   /*
1349    * build string; I don't want to bother with client nicknames, so I hope
1350    * this is ok...
1351    */
1352   vd.vd_format = pattern;
1353   vd.vd_args = vl;
1354   ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s " MSG_NOTICE
1355                 " * :*** Notice -- %v", me.name, &vd);
1356
1357   for (; opslist; opslist = opslist->next)
1358     send_buffer(opslist->value.cptr, sndbuf);
1359 }