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