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 void sendto_one(struct Client *to, const char* pattern, ...)
180 {
181   va_list vl;
182   va_start(vl, pattern);
183   vsendto_one(to, pattern, vl);
184   va_end(vl);
185 }
186
187
188 void vsendto_one(struct Client *to, const char* pattern, va_list vl)
189 {
190   vsprintf_irc(sendbuf, pattern, vl);
191   sendbufto_one(to);
192 }
193
194 /*
195  * Send a (prefixed) command to a single client; select which of <cmd>
196  * <tok> to use depending on if to is a server or not.  <from> is the
197  * originator of the command.
198  */
199 void sendcmdto_one(struct Client *to, const char *cmd, const char *tok,
200                    struct Client *from, const char *pattern, ...)
201 {
202   va_list vl;
203
204   va_start(vl, pattern);
205   vsendcmdto_one(to, cmd, tok, from, pattern, vl);
206   va_end(vl);
207 }
208
209 void vsendcmdto_one(struct Client *to, const char *cmd, const char *tok,
210                     struct Client *from, const char *pattern, va_list vl)
211 {
212   struct VarData vd;
213   char sndbuf[IRC_BUFSIZE];
214
215   vd.vd_format = pattern; /* set up the struct VarData for %v */
216   vd.vd_args = vl;
217
218   if (MyUser(to)) { /* :nick!user@host form; use cmd */
219     if (IsServer(from) || IsMe(from))
220       ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, ":%s %s %v",
221                     from->name, cmd, &vd);
222     else
223       ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, ":%s!%s@%s %s %v",
224                     from->name, from->user->username, from->user->host,
225                     cmd, &vd);
226   } else /* numeric form; use tok */
227     ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, "%C %s %v", from, tok, &vd);
228
229   send_buffer(to, sndbuf);
230 }
231
232 #ifdef GODMODE
233 static void send_to_god(struct Client* to, const char* buf)
234 {
235   if (!sdbflag && !IsUser(to)) {
236     char sbuf2[BUFSIZE + 1];
237     unsigned int len = strlen(buf) - 2;   /* Remove "\r\n" */
238
239     sdbflag = 1;
240     len = IRCD_MIN(len, BUFSIZE);
241     ircd_strncpy(sbuf2, buf, len);
242     sbuf2[len] = '\0';
243
244     if (len > 402) {
245       char c = sbuf2[200];
246       sbuf2[200] = '\0';
247       sendto_ops("SND:%-8.8s(%.4d): \"%s...%s\"",
248                  to->name, len, sbuf2, &sbuf2[len - 200]);
249     }
250     else
251       sendto_ops("SND:%-8.8s(%.4d): \"%s\"", to->name, len, sbuf2);
252     sdbflag = 0;
253   }
254 }
255 #endif /* GODMODE */
256
257 void send_buffer(struct Client* to, char* buf)
258 {
259   unsigned int len;
260   assert(0 != to);
261   assert(0 != buf);
262
263   if (to->from)
264     to = to->from;
265
266   if (!can_send(to))
267     /*
268      * This socket has already been marked as dead
269      */
270     return;
271
272   if (DBufLength(&to->sendQ) > get_sendq(to)) {
273     if (IsServer(to))
274       sendto_ops("Max SendQ limit exceeded for %s: " SIZE_T_FMT " > " SIZE_T_FMT,
275                  to->name, DBufLength(&to->sendQ), get_sendq(to));
276     dead_link(to, "Max sendQ exceeded");
277     return;
278   }
279
280   Debug((DEBUG_SEND, "Sending [%s] to %s", buf, to->name));
281
282   len = strlen(buf);
283   if (buf[len - 1] != '\n') {
284     if (len > 510)
285       len = 510;
286     buf[len++] = '\r';
287     buf[len++] = '\n';
288     buf[len] = '\0';
289   }
290
291   if (0 == dbuf_put(&to->sendQ, buf, len)) {
292     dead_link(to, "Buffer allocation error");
293     return;
294   }
295
296 #ifdef GODMODE
297   send_to_god(to, buf);
298 #endif /* GODMODE */
299   /*
300    * Update statistics. The following is slightly incorrect
301    * because it counts messages even if queued, but bytes
302    * only really sent. Queued bytes get updated in SendQueued.
303    */
304   ++to->sendM;
305   ++me.sendM;
306   /*
307    * This little bit is to stop the sendQ from growing too large when
308    * there is no need for it to. Thus we call send_queued() every time
309    * 2k has been added to the queue since the last non-fatal write.
310    * Also stops us from deliberately building a large sendQ and then
311    * trying to flood that link with data (possible during the net
312    * relinking done by servers with a large load).
313    */
314   if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
315     send_queued(to);
316 }
317
318 void sendbufto_one(struct Client* to)
319 {
320   send_buffer(to, sendbuf);
321 }
322
323 static void vsendto_prefix_one(struct Client *to, struct Client *from,
324     const char* pattern, va_list vl)
325 {
326   if (to && from && MyUser(to) && IsUser(from))
327   {
328     static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
329     char *par;
330     int flag = 0;
331     struct User *user = from->user;
332
333     par = va_arg(vl, char *);
334     strcpy(sender, from->name);
335     if (user)
336     {
337       if (*user->username)
338       {
339         strcat(sender, "!");
340         strcat(sender, user->username);
341       }
342       if (*user->host && !MyConnect(from))
343       {
344         strcat(sender, "@");
345         strcat(sender, user->host);
346         flag = 1;
347       }
348     }
349     /*
350      * Flag is used instead of strchr(sender, '@') for speed and
351      * also since username/nick may have had a '@' in them. -avalon
352      */
353     if (!flag && MyConnect(from) && *user->host)
354     {
355       strcat(sender, "@");
356       strcat(sender, from->sockhost);
357     }
358     *sendbuf = ':';
359     strcpy(&sendbuf[1], sender);
360     /* Assuming 'pattern' always starts with ":%s ..." */
361     vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
362   }
363   else
364     vsprintf_irc(sendbuf, pattern, vl);
365   sendbufto_one(to);
366 }
367
368 void sendto_channel_butone(struct Client *one, struct Client *from, struct Channel *chptr,
369     const char* pattern, ...)
370 {
371   va_list vl;
372   struct Membership* member;
373   struct Client *acptr;
374   int i;
375
376   va_start(vl, pattern);
377
378   ++sentalong_marker;
379   for (member = chptr->members; member; member = member->next_member)
380   {
381     acptr = member->user;
382     /* ...was the one I should skip */
383     if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
384       continue;
385     if (MyConnect(acptr))       /* (It is always a client) */
386       vsendto_prefix_one(acptr, from, pattern, vl);
387     else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
388     {
389       sentalong[i] = sentalong_marker;
390       /*
391        * Don't send channel messages to links that are still eating
392        * the net.burst: -- Run 2/1/1997
393        */
394       if (!IsBurstOrBurstAck(acptr->from))
395         vsendto_prefix_one(acptr, from, pattern, vl);
396     }
397   }
398   va_end(vl);
399 }
400
401
402 void sendmsgto_channel_butone(struct Client *one, struct Client *from,
403                               struct Channel *chptr, const char *sender,
404                               const char *cmd, const char *chname, const char *msg)
405 {
406  /*
407   * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
408   * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
409   */
410   struct Membership* member;
411   struct Client *acptr;
412   char userbuf[2048];
413   char servbuf[2048];
414   int i;
415   int flag=-1;
416
417   assert(0 != cmd);
418   /* 
419    * Precalculate the buffers we sent to the clients instead of doing an
420    * expensive sprintf() per member that we send to.  We still have to
421    * use strcpy() which is evil.
422    */
423   if (IsServer(from)) {
424     sprintf(userbuf,":%s %s %s :%s",
425             from->name, ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
426     sprintf(servbuf,"%s %s %s :%s", NumServ(from), cmd, chname, msg);
427   }
428   else {
429     sprintf(userbuf,":%s!%s@%s %s %s :%s",
430             from->name, from->user->username, from->user->host,
431             ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
432     sprintf(servbuf,"%s%s %s %s :%s", NumNick(from), cmd, chname, msg);
433   }
434
435   ++sentalong_marker;
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->from == one || IsZombie(member) || IsDeaf(acptr))
441       continue;
442     if (MyConnect(acptr)) {      /* (It is always a client) */
443         if (flag!=0)
444           strcpy(sendbuf,userbuf);
445         flag=0;
446         sendbufto_one(acptr);
447     }
448     else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
449     {
450       sentalong[i] = sentalong_marker;
451       if (!IsBurstOrBurstAck(acptr->from)) {
452         if (flag != 1)
453           strcpy(sendbuf,servbuf);
454         flag = 1;
455         sendbufto_one(acptr);
456   }
457     } /* of if MyConnect() */
458   } /* of for(members) */
459 }
460
461 void sendcmdto_channel_butone(struct Client *one, struct Channel *chan,
462                               const char *cmd, const char *tok,
463                               struct Client *from, unsigned int skip,
464                               const char *pattern, ...)
465 {
466   struct Membership *member;
467   struct VarData vd;
468   char userbuf[IRC_BUFSIZE];
469   char servbuf[IRC_BUFSIZE];
470
471   vd.vd_format = pattern;
472
473   /* Build buffer to send to users */
474   va_start(vd.vd_args, pattern);
475   if (IsServer(from) || IsMe(from))
476     ircd_snprintf(from, userbuf, sizeof(userbuf) - 2, ":%s %s %v", from->name,
477                   cmd, &vd);
478   else
479     ircd_snprintf(from, userbuf, sizeof(userbuf) - 2, ":%s!%s@%s %s %v",
480                   from->name, from->user->username, from->user->host, cmd,
481                   &vd);
482   va_end(vd.vd_args);
483
484   /* Build buffer to send to servers */
485   va_start(vd.vd_args, pattern);
486   ircd_snprintf(from, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok,
487                 &vd);
488   va_end(vd.vd_args);
489
490   /* send buffer along! */
491   sentalong_marker++;
492   for (member = chan->members; member; member = member->next_member) {
493     /* skip one, zombies, and deaf users... */
494     if (member->user->from == one || IsZombie(member) ||
495         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
496         (skip & SKIP_NONOPS && !IsChanOp(member)))
497       continue;
498
499     if (MyConnect(member->user))
500       send_buffer(member->user, userbuf); /* send user buffer */
501     else if (-1 < member->user->from->fd &&
502              sentalong[member->user->from->fd] != sentalong_marker) {
503       sentalong[member->user->from->fd] = sentalong_marker;
504
505       if (skip & SKIP_BURST && IsBurstOrBurstAck(member->user->from))
506         continue; /* skip bursting servers */
507
508       send_buffer(member->user->from, servbuf); /* send server buffer */
509     }
510   }
511 }
512
513 void sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
514     const char* pattern, ...)
515 {
516   va_list vl;
517   struct Membership* member;
518   struct Client *acptr;
519
520   va_start(vl, pattern);
521
522   for (member = chptr->members; member; member = member->next_member)
523   {
524     acptr = member->user;
525     /* ...was the one I should skip */
526     if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
527       continue;
528     if (MyConnect(acptr))       /* (It is always a client) */
529       vsendto_prefix_one(acptr, from, pattern, vl);
530   }
531   va_end(vl);
532   return;
533 }
534
535 void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
536     const char* pattern, ...)
537 {
538   va_list vl;
539   struct Membership* member;
540   struct Client *acptr;
541   int i;
542 #ifndef NO_PROTOCOL9
543   char  target[128];
544   char* source;
545   char* tp;
546   char* msg;
547 #endif
548
549   va_start(vl, pattern);
550
551   ++sentalong_marker;
552   for (member = chptr->members; member; member = member->next_member)
553   {
554     acptr = member->user;
555     if (acptr->from == acptr || /* Skip local clients */
556 #ifndef NO_PROTOCOL9
557         Protocol(acptr->from) < 10 ||   /* Skip P09 links */
558 #endif
559         acptr->from == one ||   /* ...was the one I should skip */
560         !IsChanOp(member) ||   /* Skip non chanops */
561         IsZombie(member) || IsDeaf(acptr))
562       continue;
563     if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
564     {
565       sentalong[i] = sentalong_marker;
566       /* Don't send channel messages to links that are
567          still eating the net.burst: -- Run 2/1/1997 */
568       if (!IsBurstOrBurstAck(acptr->from))
569         vsendto_prefix_one(acptr, from, pattern, vl);
570     }
571   }
572
573 #ifndef NO_PROTOCOL9
574   /* Send message to all 2.9 servers */
575   /* This is a hack, because it assumes that we know how `vl' is build up */
576   source = va_arg(vl, char *);
577   tp = va_arg(vl, char *);      /* Channel */
578   msg = va_arg(vl, char *);
579   for (member = chptr->members; member; member = member->next_member)
580   {
581     acptr = member->user;
582     if (acptr->from == acptr || /* Skip local clients */
583         Protocol(acptr->from) > 9 ||    /* Skip P10 servers */
584         acptr->from == one ||   /* ...was the one I should skip */
585         !IsChanOp(member) ||   /* Skip non chanops */
586         IsZombie(member) || IsDeaf(acptr))
587       continue;
588     if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
589     {
590       sentalong[i] = sentalong_marker;
591       /* Don't send channel messages to links that are
592          still eating the net.burst: -- Run 2/1/1997 */
593       if (!IsBurstOrBurstAck(acptr->from))
594       {
595         struct Membership* other_member;
596         struct Client* acptr2;
597         tp = target;
598         *tp = 0;
599         /* Find all chanops in this direction: */
600         for (other_member = chptr->members; other_member; other_member = other_member->next_member)
601         {
602           acptr2 = other_member->user;
603           if (acptr2->from == acptr->from && acptr2->from != one &&
604               IsChanOp(other_member) && !IsZombie(other_member) &&
605               !IsDeaf(acptr2))
606           {
607             int len = strlen(acptr2->name);
608             if (tp + len + 2 > target + sizeof(target))
609             {
610               sendto_prefix_one(acptr, from,
611                   ":%s NOTICE %s :%s", source, target, msg);
612               tp = target;
613               *tp = 0;
614             }
615             if (*target)
616               strcpy(tp++, ",");
617             strcpy(tp, acptr2->name);
618             tp += len;
619           }
620         }
621         sendto_prefix_one(acptr, from,
622             ":%s NOTICE %s :%s", source, target, msg);
623       }
624     }
625   }
626 #endif
627
628   va_end(vl);
629   return;
630 }
631
632 /*
633  * sendto_serv_butone
634  *
635  * Send a message to all connected servers except the client 'one'.
636  */
637 void sendto_serv_butone(struct Client *one, const char* pattern, ...)
638 {
639   va_list vl;
640   struct DLink *lp;
641
642   va_start(vl, pattern);
643   vsprintf_irc(sendbuf, pattern, vl);
644   va_end(vl);
645
646   for (lp = me.serv->down; lp; lp = lp->next)
647   {
648     if (one && lp->value.cptr == one->from)
649       continue;
650     sendbufto_one(lp->value.cptr);
651   }
652
653 }
654
655 void sendcmdto_serv_butone(struct Client *one, const char *cmd,
656                            const char *tok, struct Client *from,
657                            const char *pattern, ...)
658 {
659   struct VarData vd;
660   char sndbuf[IRC_BUFSIZE];
661   struct DLink *lp;
662
663   vd.vd_format = pattern; /* set up the struct VarData for %v */
664   va_start(vd.vd_args, pattern);
665
666   /* use token */
667   ircd_snprintf(&me, sndbuf, sizeof(sndbuf) - 2, "%C %s %v", from, tok, &vd);
668   va_end(vd.vd_args);
669
670   /* send it to our downlinks */
671   for (lp = me.serv->down; lp; lp = lp->next) {
672     if (one && lp->value.cptr == one->from)
673       continue;
674     send_buffer(lp->value.cptr, sndbuf);
675   }
676 }
677
678 /*
679  * sendbufto_serv_butone()
680  *
681  * Send prepared sendbuf to all connected servers except the client 'one'
682  *  -Ghostwolf 18-May-97
683  */
684 void sendbufto_serv_butone(struct Client *one)
685 {
686   struct DLink *lp;
687
688   for (lp = me.serv->down; lp; lp = lp->next)
689   {
690     if (one && lp->value.cptr == one->from)
691       continue;
692     sendbufto_one(lp->value.cptr);
693   }
694 }
695
696
697 /*
698  * sendto_common_channels()
699  *
700  * Sends a message to all people (inclusing `acptr') on local server
701  * who are in same channel with client `acptr'.
702  */
703 void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
704 {
705   va_list vl;
706   struct Membership* chan;
707   struct Membership* member;
708
709   assert(0 != acptr);
710   assert(0 != acptr->from);
711   assert(0 != pattern);
712
713   va_start(vl, pattern);
714
715   ++sentalong_marker;
716   if (-1 < acptr->from->fd)
717     sentalong[acptr->from->fd] = sentalong_marker;
718   /*
719    * loop through acptr's channels, and the members on their channels
720    */
721   if (acptr->user) {
722     for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
723       for (member = chan->channel->members; member; member = member->next_member) {
724         struct Client *cptr = member->user;
725         int    i;
726         if (MyConnect(cptr) && 
727             -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
728           sentalong[i] = sentalong_marker;
729           vsendto_prefix_one(cptr, acptr, pattern, vl);
730         }
731       }
732     }
733   }
734   if (MyConnect(acptr))
735     vsendto_prefix_one(acptr, acptr, pattern, vl);
736   va_end(vl);
737   return;
738 }
739
740 void sendcmdto_common_channels(struct Client *cptr, const char *cmd,
741                                const char *tok, const char *pattern, ...)
742 {
743   struct VarData vd;
744   char sndbuf[IRC_BUFSIZE];
745   struct Membership *chan;
746   struct Membership *member;
747
748   assert(0 != cptr);
749   assert(0 != cptr->from);
750   assert(0 != pattern);
751   assert(!IsServer(cptr));
752
753   vd.vd_format = pattern; /* set up the struct VarData for %v */
754
755   va_start(vd.vd_args, pattern);
756
757   /* build the buffer */
758   ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s!%s@%s %s %v", cptr->name,
759                 cptr->user->username, cptr->user->host, cmd, &vd);
760   va_end(vd.vd_args);
761
762   sentalong_marker++;
763   if (-1 < cptr->from->fd)
764     sentalong[cptr->from->fd] = sentalong_marker;
765   /*
766    * loop through cptr's channels, and the members on their channels
767    */
768   for (chan = cptr->user->channel; chan; chan = chan->next_channel)
769     for (member = chan->channel->members; member;
770          member = member->next_member)
771       if (MyConnect(member->user) && -1 < cptr->fd &&
772           sentalong[cptr->fd] != sentalong_marker) {
773         sentalong[cptr->fd] = sentalong_marker;
774         send_buffer(member->user, sndbuf);
775       }
776
777   if (MyConnect(cptr))
778     send_buffer(cptr, sndbuf);
779 }
780
781 /*
782  * sendto_channel_butserv
783  *
784  * Send a message to all members of a channel that
785  * are connected to this server.
786  *
787  * This contains a subtle bug; after the first call to vsendto_prefix_one()
788  * below, vl is in an indeterminate state, according to ANSI; we'd have to
789  * move va_start() and va_end() into the loop to correct the problem.  It's
790  * easier, however, just to use sendcmdto_channel_butserv(), which builds a
791  * buffer and sends that prepared buffer to each channel member.
792  */
793 void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
794 {
795   va_list vl;
796   struct Membership* member;
797   struct Client *acptr;
798   
799   va_start(vl, pattern);
800
801   for (member = chptr->members; member; member = member->next_member) {
802     acptr = member->user;
803     if (MyConnect(acptr) && !IsZombie(member))
804       vsendto_prefix_one(acptr, from, pattern, vl);
805   }
806   va_end(vl);
807   return;
808 }
809
810 void sendcmdto_channel_butserv(struct Channel *chan, const char *cmd,
811                                const char *tok, struct Client *from,
812                                const char *pattern, ...)
813 {
814   struct VarData vd;
815   char sndbuf[IRC_BUFSIZE];
816   struct Membership *member;
817
818   vd.vd_format = pattern; /* set up the struct VarData for %v */
819   va_start(vd.vd_args, pattern);
820
821   /* build the buffer */
822   if (IsServer(from) || IsMe(from))
823     ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s %s %v", from->name,
824                   cmd, &vd);
825   else
826     ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s!%s@%s %s %v", from->name,
827                   from->user->username, from->user->host, cmd, &vd);
828   va_end(vd.vd_args);
829
830   /* send the buffer to each local channel member */
831   for (member = chan->members; member; member = member->next_member) {
832     if (MyConnect(member->user) && !IsZombie(member))
833       send_buffer(member->user, sndbuf);
834   }
835 }
836
837 /*
838  * Send a msg to all ppl on servers/hosts that match a specified mask
839  * (used for enhanced PRIVMSGs)
840  *
841  *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
842  */
843
844 static int match_it(struct Client *one, const char *mask, int what)
845 {
846   switch (what)
847   {
848     case MATCH_HOST:
849       return (match(mask, one->user->host) == 0);
850     case MATCH_SERVER:
851     default:
852       return (match(mask, one->user->server->name) == 0);
853   }
854 }
855
856 /*
857  * sendto_match_butone
858  *
859  * Send to all clients which match the mask in a way defined on 'what';
860  * either by user hostname or user servername.
861  */
862 void sendto_match_butone(struct Client *one, struct Client *from,
863     const char *mask, int what, const char* pattern, ...)
864 {
865   va_list vl;
866   int i;
867   struct Client *cptr, *acptr;
868
869   va_start(vl, pattern);
870   for (i = 0; i <= HighestFd; i++)
871   {
872     if (!(cptr = LocalClientArray[i]))
873       continue;                 /* that clients are not mine */
874     if (cptr == one)            /* must skip the origin !! */
875       continue;
876     if (IsServer(cptr))
877     {
878       for (acptr = GlobalClientList; acptr; acptr = acptr->next)
879         if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
880           break;
881       /* a person on that server matches the mask, so we
882        *  send *one* msg to that server ...
883        */
884       if (acptr == NULL)
885         continue;
886       /* ... but only if there *IS* a matching person */
887     }
888     /* my client, does he match ? */
889     else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
890       continue;
891     vsendto_prefix_one(cptr, from, pattern, vl);
892   }
893   va_end(vl);
894
895   return;
896 }
897
898 /*
899  * sendto_lops_butone
900  *
901  * Send to *local* ops but one.
902  */
903 void sendto_lops_butone(struct Client* one, const char* pattern, ...)
904 {
905   va_list         vl;
906   struct Client*  cptr;
907   struct Client** clients = me.serv->client_list;
908   int             i;
909   char            nbuf[1024];
910
911   assert(0 != clients);
912
913   sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
914   va_start(vl, pattern);
915   vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
916   va_end(vl);
917
918   for (i = 0; i <= me.serv->nn_mask; ++i) {
919     if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
920       sprintf_irc(sendbuf, nbuf, cptr->name);
921       sendbufto_one(cptr);
922     }
923   }
924 }
925
926 /*
927  * sendto_op_mask
928  *
929  * Sends message to the list indicated by the bitmask field.
930  * Don't try to send to more than one list! That is not supported.
931  * Xorath 5/1/97
932  */
933 #ifdef OLD_VSENDTO_OP_MASK
934 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
935 {
936   static char fmt[1024];
937   char *fmt_target;
938   int i = 0;            /* so that 1 points to opsarray[0] */
939   struct SLink *opslist;
940
941   while ((mask >>= 1))
942     i++;
943   if (!(opslist = opsarray[i]))
944     return;
945
946   fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
947   do
948   {
949     strcpy(fmt_target, opslist->value.cptr->name);
950     strcat(fmt_target, " :*** Notice -- ");
951     strcat(fmt_target, pattern);
952     vsendto_one(opslist->value.cptr, fmt, vl);
953     opslist = opslist->next;
954   }
955   while (opslist);
956 }
957 #else /* !OLD_VSENDTO_OP_MASK */
958 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
959 {
960   struct VarData vd;
961   char sndbuf[IRC_BUFSIZE];
962   int i = 0; /* so that 1 points to opsarray[0] */
963   struct SLink *opslist;
964
965   while ((mask >>= 1))
966     i++;
967
968   if (!(opslist = opsarray[i]))
969     return;
970
971   /*
972    * build string; I don't want to bother with client nicknames, so I hope
973    * this is ok...
974    */
975   vd.vd_format = pattern;
976   vd.vd_args = vl;
977   ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s " MSG_NOTICE
978                 " * :*** Notice -- %v", me.name, &vd);
979
980   for (; opslist; opslist = opslist->next)
981     send_buffer(opslist->value.cptr, sndbuf);
982 }
983 #endif /* OLD_VSENDTO_OP_MASK */
984
985 /*
986  * sendbufto_op_mask
987  *
988  * Send a prepared sendbuf to the list indicated by the bitmask field.
989  * Ghostwolf 16-May-97
990  */
991 void sendbufto_op_mask(unsigned int mask)
992 {
993   int i = 0;            /* so that 1 points to opsarray[0] */
994   struct SLink *opslist;
995   while ((mask >>= 1))
996     i++;
997   if (!(opslist = opsarray[i]))
998     return;
999   do
1000   {
1001     sendbufto_one(opslist->value.cptr);
1002     opslist = opslist->next;
1003   }
1004   while (opslist);
1005 }
1006
1007
1008 /*
1009  * sendto_ops
1010  *
1011  * Send to *local* ops only.
1012  */
1013 void vsendto_ops(const char *pattern, va_list vl)
1014 {
1015   struct Client *cptr;
1016   int i;
1017   char fmt[1024];
1018   char *fmt_target;
1019
1020   fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
1021
1022   for (i = 0; i <= HighestFd; i++)
1023     if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
1024         SendServNotice(cptr))
1025     {
1026       strcpy(fmt_target, cptr->name);
1027       strcat(fmt_target, " :*** Notice -- ");
1028       strcat(fmt_target, pattern);
1029       vsendto_one(cptr, fmt, vl);
1030     }
1031 }
1032
1033 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
1034 {
1035   va_list vl;
1036   va_start(vl, pattern);
1037   vsendto_op_mask(mask, pattern, vl);
1038   va_end(vl);
1039 }
1040
1041 void sendto_ops(const char *pattern, ...)
1042 {
1043   va_list vl;
1044   va_start(vl, pattern);
1045   vsendto_op_mask(SNO_OLDSNO, pattern, vl);
1046   va_end(vl);
1047 }
1048
1049 /*
1050  * sendto_ops_butone
1051  *
1052  * Send message to all operators.
1053  * one - client not to send message to
1054  * from- client which message is from *NEVER* NULL!!
1055  */
1056 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
1057 {
1058   va_list vl;
1059   int i;
1060   struct Client *cptr;
1061
1062   va_start(vl, pattern);
1063   ++sentalong_marker;
1064   for (cptr = GlobalClientList; cptr; cptr = cptr->next)
1065   {
1066     if (!SendWallops(cptr))
1067       continue;
1068     i = cptr->from->fd;         /* find connection oper is on */
1069     if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
1070       continue;
1071     if (cptr->from == one)
1072       continue;                 /* ...was the one I should skip */
1073     sentalong[i] = sentalong_marker;
1074     vsendto_prefix_one(cptr->from, from, pattern, vl);
1075   }
1076   va_end(vl);
1077
1078   return;
1079 }
1080
1081 /*
1082  * sendto_g_serv_butone
1083  *
1084  * Send message to all remote +g users (server links).
1085  *
1086  * one - server not to send message to.
1087  */
1088 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
1089 {
1090   va_list vl;
1091   struct Client *cptr;
1092   int i;
1093
1094   va_start(vl, pattern);
1095   ++sentalong_marker;
1096   vsprintf_irc(sendbuf, pattern, vl);
1097   for (cptr = GlobalClientList; cptr; cptr = cptr->next)
1098   {
1099     if (!SendDebug(cptr))
1100       continue;
1101     i = cptr->from->fd;         /* find connection user is on */
1102     if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
1103       continue;
1104     if (MyConnect(cptr))
1105       continue;
1106     sentalong[i] = sentalong_marker;
1107     if (cptr->from == one)
1108       continue;
1109     sendbufto_one(cptr);
1110   }
1111   va_end(vl);
1112
1113   return;
1114 }
1115
1116 /*
1117  * sendto_prefix_one
1118  *
1119  * to - destination client
1120  * from - client which message is from
1121  *
1122  * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
1123  * -avalon
1124  */
1125 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
1126 {
1127   va_list vl;
1128   va_start(vl, pattern);
1129   vsendto_prefix_one(to, from, pattern, vl);
1130   va_end(vl);
1131 }
1132
1133 /*
1134  * sendto_realops
1135  *
1136  * Send to *local* ops only but NOT +s nonopers.
1137  */
1138 void sendto_realops(const char *pattern, ...)
1139 {
1140   va_list vl;
1141
1142   va_start(vl, pattern);
1143   vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
1144
1145   va_end(vl);
1146   return;
1147 }
1148
1149 /*
1150  * Send message to all servers of protocol 'p' and lower.
1151  */
1152 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
1153 {
1154   va_list vl;
1155   struct DLink *lp;
1156   va_start(vl, pattern);
1157   for (lp = me.serv->down; lp; lp = lp->next)
1158     if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
1159       vsendto_one(lp->value.cptr, pattern, vl);
1160   va_end(vl);
1161 }
1162
1163 /*
1164  * Send message to all servers of protocol 'p' and higher.
1165  */
1166 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
1167 {
1168   va_list vl;
1169   struct DLink *lp;
1170   va_start(vl, pattern);
1171   for (lp = me.serv->down; lp; lp = lp->next)
1172     if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
1173       vsendto_one(lp->value.cptr, pattern, vl);
1174   va_end(vl);
1175 }