Author: Bleep <tomh@inxpress.net>
[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_string.h"
28 #include "list.h"
29 #include "match.h"
30 #include "msg.h"
31 #include "numnicks.h"
32 #include "s_bsd.h"
33 #include "s_debug.h"
34 #include "s_misc.h"
35 #include "s_user.h"
36 #include "sprintf_irc.h"
37 #include "struct.h"
38 #include "sys.h"
39
40 #include <assert.h>
41 #include <stdio.h>
42 #include <string.h>
43
44
45 char sendbuf[2048];
46 static int sentalong[MAXCONNECTIONS];
47 static int sentalong_marker;
48 struct SLink *opsarray[32];     /* don't use highest bit unless you change
49                                    atoi to strtoul in sendto_op_mask() */
50 #ifdef GODMODE
51 char sendbuf2[2048];
52 int sdbflag;
53 #endif /* GODMODE */
54
55 /*
56  * dead_link
57  *
58  * An error has been detected. The link *must* be closed,
59  * but *cannot* call ExitClient (m_bye) from here.
60  * Instead, mark it with FLAGS_DEADSOCKET. This should
61  * generate ExitClient from the main loop.
62  *
63  * If 'notice' is not NULL, it is assumed to be a format
64  * for a message to local opers. I can contain only one
65  * '%s', which will be replaced by the sockhost field of
66  * the failing link.
67  *
68  * Also, the notice is skipped for "uninteresting" cases,
69  * like Persons and yet unknown connections...
70  */
71
72 static void dead_link(struct Client *to, char *notice)
73 {
74   to->flags |= FLAGS_DEADSOCKET;
75   /*
76    * If because of BUFFERPOOL problem then clean dbuf's now so that
77    * notices don't hurt operators below.
78    */
79   DBufClear(&to->recvQ);
80   DBufClear(&to->sendQ);
81
82   /*
83    * Keep a copy of the last comment, for later use...
84    */
85   ircd_strncpy(to->info, notice, REALLEN);
86
87   if (!IsUser(to) && !IsUnknown(to) && !(to->flags & FLAGS_CLOSING))
88     sendto_ops("%s for %s", to->info, to->name);
89   Debug((DEBUG_ERROR, to->info));
90 }
91
92 static int can_send(struct Client* to)
93 {
94   assert(0 != to);
95   return (IsDead(to) || IsMe(to) || -1 == to->fd) ? 0 : 1;
96 }
97
98 /*
99  * flush_connections
100  *
101  * Used to empty all output buffers for all connections. Should only
102  * be called once per scan of connections. There should be a select in
103  * here perhaps but that means either forcing a timeout or doing a poll.
104  * When flushing, all we do is empty the obuffer array for each local
105  * client and try to send it. if we cant send it, it goes into the sendQ
106  * -avalon
107  */
108 void flush_connections(struct Client* cptr)
109 {
110   if (cptr) {
111     send_queued(cptr);
112   }
113   else {
114     int i;
115     for (i = HighestFd; i >= 0; i--) {
116       if ((cptr = LocalClientArray[i]))
117         send_queued(cptr);
118     }
119   }
120 }
121
122 /*
123  * flush_sendq_except - run through local client array and flush
124  * the sendq for each client, if the address of the client sendq
125  * is the same as the one specified, it is skipped. This is used
126  * by dbuf_put to try to get some more memory before bailing and
127  * causing the client to be disconnected.
128  */
129 void flush_sendq_except(const struct DBuf* one)
130 {
131   int i;
132   struct Client* cptr;
133   for (i = HighestFd; i >= 0; i--) {
134     if ( (cptr = LocalClientArray[i]) && one != &cptr->sendQ)
135       send_queued(cptr);
136   }
137 }
138
139 /*
140  * send_queued
141  *
142  * This function is called from the main select-loop (or whatever)
143  * when there is a chance that some output would be possible. This
144  * attempts to empty the send queue as far as possible...
145  */
146 void send_queued(struct Client *to)
147 {
148   assert(0 != to);
149   assert(0 != to->local);
150
151   if (IsBlocked(to) || !can_send(to))
152     return;                     /* Don't bother */
153
154   while (DBufLength(&to->sendQ) > 0) {
155     unsigned int len;
156     const char* msg = dbuf_map(&to->sendQ, &len);
157
158     if ((len = deliver_it(to, msg, len))) {
159       dbuf_delete(&to->sendQ, len);
160       to->lastsq = DBufLength(&to->sendQ) / 1024;
161       if (IsBlocked(to))
162         break;
163     }
164     else {
165       if (IsDead(to))
166         dead_link(to, "Write error, closing link");
167       break;
168     }
169   }
170 }
171
172 /*
173  *  send message to single client
174  */
175 void sendto_one(struct Client *to, const char* pattern, ...)
176 {
177   va_list vl;
178   va_start(vl, pattern);
179   vsendto_one(to, pattern, vl);
180   va_end(vl);
181 }
182
183
184 void vsendto_one(struct Client *to, const char* pattern, va_list vl)
185 {
186   vsprintf_irc(sendbuf, pattern, vl);
187   sendbufto_one(to);
188 }
189
190 #ifdef GODMODE
191 static void send_to_god(struct Client* to, const char* buf)
192 {
193   if (!sdbflag && !IsUser(to)) {
194     char sbuf2[BUFSIZE + 1];
195     unsigned int len = strlen(buf) - 2;   /* Remove "\r\n" */
196
197     sdbflag = 1;
198     len = IRCD_MIN(len, BUFSIZE);
199     ircd_strncpy(sbuf2, buf, len);
200     sbuf2[len] = '\0';
201
202     if (len > 402) {
203       char c = sbuf2[200];
204       sbuf2[200] = '\0';
205       sendto_ops("SND:%-8.8s(%.4d): \"%s...%s\"",
206                  to->name, len, sbuf2, &sbuf2[len - 200]);
207     }
208     else
209       sendto_ops("SND:%-8.8s(%.4d): \"%s\"", to->name, len, sbuf2);
210     sdbflag = 0;
211   }
212 }
213 #endif /* GODMODE */
214
215 void send_buffer(struct Client* to, char* buf)
216 {
217   unsigned int len;
218   assert(0 != to);
219   assert(0 != buf);
220
221   if (to->from)
222     to = to->from;
223
224   if (!can_send(to))
225     /*
226      * This socket has already been marked as dead
227      */
228     return;
229
230   if (DBufLength(&to->sendQ) > get_sendq(to)) {
231     if (IsServer(to))
232       sendto_ops("Max SendQ limit exceeded for %s: " SIZE_T_FMT " > " SIZE_T_FMT,
233                  to->name, DBufLength(&to->sendQ), get_sendq(to));
234     dead_link(to, "Max sendQ exceeded");
235     return;
236   }
237
238   Debug((DEBUG_SEND, "Sending [%s] to %s", buf, to->name));
239
240   len = strlen(buf);
241   if (buf[len - 1] != '\n') {
242     if (len > 510)
243       len = 510;
244     buf[len++] = '\r';
245     buf[len++] = '\n';
246     buf[len] = '\0';
247   }
248
249   if (0 == dbuf_put(&to->sendQ, buf, len)) {
250     dead_link(to, "Buffer allocation error");
251     return;
252   }
253
254 #ifdef GODMODE
255   send_to_god(to, buf);
256 #endif /* GODMODE */
257   /*
258    * Update statistics. The following is slightly incorrect
259    * because it counts messages even if queued, but bytes
260    * only really sent. Queued bytes get updated in SendQueued.
261    */
262   ++to->sendM;
263   ++me.sendM;
264   /*
265    * This little bit is to stop the sendQ from growing too large when
266    * there is no need for it to. Thus we call send_queued() every time
267    * 2k has been added to the queue since the last non-fatal write.
268    * Also stops us from deliberately building a large sendQ and then
269    * trying to flood that link with data (possible during the net
270    * relinking done by servers with a large load).
271    */
272   if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
273     send_queued(to);
274 }
275
276 void sendbufto_one(struct Client* to)
277 {
278   send_buffer(to, sendbuf);
279 }
280
281 static void vsendto_prefix_one(struct Client *to, struct Client *from,
282     const char* pattern, va_list vl)
283 {
284   if (to && from && MyUser(to) && IsUser(from))
285   {
286     static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
287     char *par;
288     int flag = 0;
289     struct User *user = from->user;
290
291     par = va_arg(vl, char *);
292     strcpy(sender, from->name);
293     if (user)
294     {
295       if (*user->username)
296       {
297         strcat(sender, "!");
298         strcat(sender, user->username);
299       }
300       if (*user->host && !MyConnect(from))
301       {
302         strcat(sender, "@");
303         strcat(sender, user->host);
304         flag = 1;
305       }
306     }
307     /*
308      * Flag is used instead of strchr(sender, '@') for speed and
309      * also since username/nick may have had a '@' in them. -avalon
310      */
311     if (!flag && MyConnect(from) && *user->host)
312     {
313       strcat(sender, "@");
314       strcat(sender, from->sockhost);
315     }
316     *sendbuf = ':';
317     strcpy(&sendbuf[1], sender);
318     /* Assuming 'pattern' always starts with ":%s ..." */
319     vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
320   }
321   else
322     vsprintf_irc(sendbuf, pattern, vl);
323   sendbufto_one(to);
324 }
325
326 void sendto_channel_butone(struct Client *one, struct Client *from, struct Channel *chptr,
327     const char* pattern, ...)
328 {
329   va_list vl;
330   struct Membership* member;
331   struct Client *acptr;
332   int i;
333
334   va_start(vl, pattern);
335
336   ++sentalong_marker;
337   for (member = chptr->members; member; member = member->next_member)
338   {
339     acptr = member->user;
340     /* ...was the one I should skip */
341     if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
342       continue;
343     if (MyConnect(acptr))       /* (It is always a client) */
344       vsendto_prefix_one(acptr, from, pattern, vl);
345     else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
346     {
347       sentalong[i] = sentalong_marker;
348       /*
349        * Don't send channel messages to links that are still eating
350        * the net.burst: -- Run 2/1/1997
351        */
352       if (!IsBurstOrBurstAck(acptr->from))
353         vsendto_prefix_one(acptr, from, pattern, vl);
354     }
355   }
356   va_end(vl);
357 }
358
359
360 void sendmsgto_channel_butone(struct Client *one, struct Client *from,
361                               struct Channel *chptr, const char *sender,
362                               const char *cmd, const char *chname, const char *msg)
363 {
364  /*
365   * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
366   * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
367   */
368   struct Membership* member;
369   struct Client *acptr;
370   char userbuf[2048];
371   char servbuf[2048];
372   int i;
373   int flag=-1;
374
375   assert(0 != cmd);
376   /* 
377    * Precalculate the buffers we sent to the clients instead of doing an
378    * expensive sprintf() per member that we send to.  We still have to
379    * use strcpy() which is evil.
380    */
381   if (IsServer(from)) {
382     sprintf(userbuf,":%s %s %s :%s",
383             from->name, ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
384     sprintf(servbuf,"%s %s %s :%s", NumServ(from), cmd, chname, msg);
385   }
386   else {
387     sprintf(userbuf,":%s!%s@%s %s %s :%s",
388             from->name, from->username, from->user->host,
389             ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
390     sprintf(servbuf,"%s%s %s %s :%s", NumNick(from), cmd, chname, msg);
391   }
392
393   ++sentalong_marker;
394   for (member = chptr->members; member; member = member->next_member)
395   {
396     acptr = member->user;
397     /* ...was the one I should skip */
398     if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
399       continue;
400     if (MyConnect(acptr)) {      /* (It is always a client) */
401         if (flag!=0)
402           strcpy(sendbuf,userbuf);
403         flag=0;
404         sendbufto_one(acptr);
405     }
406     else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
407     {
408       sentalong[i] = sentalong_marker;
409       if (!IsBurstOrBurstAck(acptr->from)) {
410         if (flag != 1)
411           strcpy(sendbuf,servbuf);
412         flag = 1;
413         sendbufto_one(acptr);
414   }
415     } /* of if MyConnect() */
416   } /* of for(members) */
417 }
418
419 void sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
420     const char* pattern, ...)
421 {
422   va_list vl;
423   struct Membership* member;
424   struct Client *acptr;
425
426   va_start(vl, pattern);
427
428   for (member = chptr->members; member; member = member->next_member)
429   {
430     acptr = member->user;
431     /* ...was the one I should skip */
432     if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
433       continue;
434     if (MyConnect(acptr))       /* (It is always a client) */
435       vsendto_prefix_one(acptr, from, pattern, vl);
436   }
437   va_end(vl);
438   return;
439 }
440
441 void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
442     const char* pattern, ...)
443 {
444   va_list vl;
445   struct Membership* member;
446   struct Client *acptr;
447   int i;
448 #ifndef NO_PROTOCOL9
449   char  target[128];
450   char* source;
451   char* tp;
452   char* msg;
453 #endif
454
455   va_start(vl, pattern);
456
457   ++sentalong_marker;
458   for (member = chptr->members; member; member = member->next_member)
459   {
460     acptr = member->user;
461     if (acptr->from == acptr || /* Skip local clients */
462 #ifndef NO_PROTOCOL9
463         Protocol(acptr->from) < 10 ||   /* Skip P09 links */
464 #endif
465         acptr->from == one ||   /* ...was the one I should skip */
466         !IsChanOp(member) ||   /* Skip non chanops */
467         IsZombie(member) || IsDeaf(acptr))
468       continue;
469     if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
470     {
471       sentalong[i] = sentalong_marker;
472       /* Don't send channel messages to links that are
473          still eating the net.burst: -- Run 2/1/1997 */
474       if (!IsBurstOrBurstAck(acptr->from))
475         vsendto_prefix_one(acptr, from, pattern, vl);
476     }
477   }
478
479 #ifndef NO_PROTOCOL9
480   /* Send message to all 2.9 servers */
481   /* This is a hack, because it assumes that we know how `vl' is build up */
482   source = va_arg(vl, char *);
483   tp = va_arg(vl, char *);      /* Channel */
484   msg = va_arg(vl, char *);
485   for (member = chptr->members; member; member = member->next_member)
486   {
487     acptr = member->user;
488     if (acptr->from == acptr || /* Skip local clients */
489         Protocol(acptr->from) > 9 ||    /* Skip P10 servers */
490         acptr->from == one ||   /* ...was the one I should skip */
491         !IsChanOp(member) ||   /* Skip non chanops */
492         IsZombie(member) || IsDeaf(acptr))
493       continue;
494     if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
495     {
496       sentalong[i] = sentalong_marker;
497       /* Don't send channel messages to links that are
498          still eating the net.burst: -- Run 2/1/1997 */
499       if (!IsBurstOrBurstAck(acptr->from))
500       {
501         struct Membership* other_member;
502         struct Client* acptr2;
503         tp = target;
504         *tp = 0;
505         /* Find all chanops in this direction: */
506         for (other_member = chptr->members; other_member; other_member = other_member->next_member)
507         {
508           acptr2 = other_member->user;
509           if (acptr2->from == acptr->from && acptr2->from != one &&
510               IsChanOp(other_member) && !IsZombie(other_member) &&
511               !IsDeaf(acptr2))
512           {
513             int len = strlen(acptr2->name);
514             if (tp + len + 2 > target + sizeof(target))
515             {
516               sendto_prefix_one(acptr, from,
517                   ":%s NOTICE %s :%s", source, target, msg);
518               tp = target;
519               *tp = 0;
520             }
521             if (*target)
522               strcpy(tp++, ",");
523             strcpy(tp, acptr2->name);
524             tp += len;
525           }
526         }
527         sendto_prefix_one(acptr, from,
528             ":%s NOTICE %s :%s", source, target, msg);
529       }
530     }
531   }
532 #endif
533
534   va_end(vl);
535   return;
536 }
537
538 /*
539  * sendto_serv_butone
540  *
541  * Send a message to all connected servers except the client 'one'.
542  */
543 void sendto_serv_butone(struct Client *one, const char* pattern, ...)
544 {
545   va_list vl;
546   struct DLink *lp;
547
548   va_start(vl, pattern);
549   vsprintf_irc(sendbuf, pattern, vl);
550   va_end(vl);
551
552   for (lp = me.serv->down; lp; lp = lp->next)
553   {
554     if (one && lp->value.cptr == one->from)
555       continue;
556     sendbufto_one(lp->value.cptr);
557   }
558
559 }
560
561 /*
562  * sendbufto_serv_butone()
563  *
564  * Send prepared sendbuf to all connected servers except the client 'one'
565  *  -Ghostwolf 18-May-97
566  */
567 void sendbufto_serv_butone(struct Client *one)
568 {
569   struct DLink *lp;
570
571   for (lp = me.serv->down; lp; lp = lp->next)
572   {
573     if (one && lp->value.cptr == one->from)
574       continue;
575     sendbufto_one(lp->value.cptr);
576   }
577 }
578
579
580 /*
581  * sendto_common_channels()
582  *
583  * Sends a message to all people (inclusing `acptr') on local server
584  * who are in same channel with client `acptr'.
585  */
586 void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
587 {
588   va_list vl;
589   struct Membership* chan;
590   struct Membership* member;
591
592   assert(0 != acptr);
593   assert(0 != acptr->from);
594   assert(0 != pattern);
595
596   va_start(vl, pattern);
597
598   ++sentalong_marker;
599   if (-1 < acptr->from->fd)
600     sentalong[acptr->from->fd] = sentalong_marker;
601   /*
602    * loop through acptr's channels, and the members on their channels
603    */
604   if (acptr->user) {
605     for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
606       for (member = chan->channel->members; member; member = member->next_member) {
607         struct Client *cptr = member->user;
608         int    i;
609         if (MyConnect(cptr) && 
610             -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
611           sentalong[i] = sentalong_marker;
612           vsendto_prefix_one(cptr, acptr, pattern, vl);
613         }
614       }
615     }
616   }
617   if (MyConnect(acptr))
618     vsendto_prefix_one(acptr, acptr, pattern, vl);
619   va_end(vl);
620   return;
621 }
622
623 /*
624  * sendto_channel_butserv
625  *
626  * Send a message to all members of a channel that
627  * are connected to this server.
628  */
629 void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
630 {
631   va_list vl;
632   struct Membership* member;
633   struct Client *acptr;
634   
635   va_start(vl, pattern);
636
637   for (member = chptr->members; member; member = member->next_member) {
638     acptr = member->user;
639     if (MyConnect(acptr) && !IsZombie(member))
640       vsendto_prefix_one(acptr, from, pattern, vl);
641   }
642   va_end(vl);
643   return;
644 }
645
646 /*
647  * Send a msg to all ppl on servers/hosts that match a specified mask
648  * (used for enhanced PRIVMSGs)
649  *
650  *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
651  */
652
653 static int match_it(struct Client *one, const char *mask, int what)
654 {
655   switch (what)
656   {
657     case MATCH_HOST:
658       return (match(mask, one->user->host) == 0);
659     case MATCH_SERVER:
660     default:
661       return (match(mask, one->user->server->name) == 0);
662   }
663 }
664
665 /*
666  * sendto_match_butone
667  *
668  * Send to all clients which match the mask in a way defined on 'what';
669  * either by user hostname or user servername.
670  */
671 void sendto_match_butone(struct Client *one, struct Client *from,
672     const char *mask, int what, const char* pattern, ...)
673 {
674   va_list vl;
675   int i;
676   struct Client *cptr, *acptr;
677
678   va_start(vl, pattern);
679   for (i = 0; i <= HighestFd; i++)
680   {
681     if (!(cptr = LocalClientArray[i]))
682       continue;                 /* that clients are not mine */
683     if (cptr == one)            /* must skip the origin !! */
684       continue;
685     if (IsServer(cptr))
686     {
687       for (acptr = GlobalClientList; acptr; acptr = acptr->next)
688         if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
689           break;
690       /* a person on that server matches the mask, so we
691        *  send *one* msg to that server ...
692        */
693       if (acptr == NULL)
694         continue;
695       /* ... but only if there *IS* a matching person */
696     }
697     /* my client, does he match ? */
698     else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
699       continue;
700     vsendto_prefix_one(cptr, from, pattern, vl);
701   }
702   va_end(vl);
703
704   return;
705 }
706
707 /*
708  * sendto_lops_butone
709  *
710  * Send to *local* ops but one.
711  */
712 void sendto_lops_butone(struct Client* one, const char* pattern, ...)
713 {
714   va_list         vl;
715   struct Client*  cptr;
716   struct Client** clients = me.serv->client_list;
717   int             i;
718   char            nbuf[1024];
719
720   assert(0 != clients);
721
722   sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
723   va_start(vl, pattern);
724   vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
725   va_end(vl);
726
727   for (i = 0; i <= me.serv->nn_mask; ++i) {
728     if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
729       sprintf_irc(sendbuf, nbuf, cptr->name);
730       sendbufto_one(cptr);
731     }
732   }
733 }
734
735 /*
736  * sendto_op_mask
737  *
738  * Sends message to the list indicated by the bitmask field.
739  * Don't try to send to more than one list! That is not supported.
740  * Xorath 5/1/97
741  */
742 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
743 {
744   static char fmt[1024];
745   char *fmt_target;
746   int i = 0;            /* so that 1 points to opsarray[0] */
747   struct SLink *opslist;
748
749   while ((mask >>= 1))
750     i++;
751   if (!(opslist = opsarray[i]))
752     return;
753
754   fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
755   do
756   {
757     strcpy(fmt_target, opslist->value.cptr->name);
758     strcat(fmt_target, " :*** Notice -- ");
759     strcat(fmt_target, pattern);
760     vsendto_one(opslist->value.cptr, fmt, vl);
761     opslist = opslist->next;
762   }
763   while (opslist);
764 }
765
766 /*
767  * sendbufto_op_mask
768  *
769  * Send a prepared sendbuf to the list indicated by the bitmask field.
770  * Ghostwolf 16-May-97
771  */
772 void sendbufto_op_mask(unsigned int mask)
773 {
774   int i = 0;            /* so that 1 points to opsarray[0] */
775   struct SLink *opslist;
776   while ((mask >>= 1))
777     i++;
778   if (!(opslist = opsarray[i]))
779     return;
780   do
781   {
782     sendbufto_one(opslist->value.cptr);
783     opslist = opslist->next;
784   }
785   while (opslist);
786 }
787
788
789 /*
790  * sendto_ops
791  *
792  * Send to *local* ops only.
793  */
794 void vsendto_ops(const char *pattern, va_list vl)
795 {
796   struct Client *cptr;
797   int i;
798   char fmt[1024];
799   char *fmt_target;
800
801   fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
802
803   for (i = 0; i <= HighestFd; i++)
804     if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
805         SendServNotice(cptr))
806     {
807       strcpy(fmt_target, cptr->name);
808       strcat(fmt_target, " :*** Notice -- ");
809       strcat(fmt_target, pattern);
810       vsendto_one(cptr, fmt, vl);
811     }
812 }
813
814 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
815 {
816   va_list vl;
817   va_start(vl, pattern);
818   vsendto_op_mask(mask, pattern, vl);
819   va_end(vl);
820 }
821
822 void sendto_ops(const char *pattern, ...)
823 {
824   va_list vl;
825   va_start(vl, pattern);
826   vsendto_op_mask(SNO_OLDSNO, pattern, vl);
827   va_end(vl);
828 }
829
830 /*
831  * sendto_ops_butone
832  *
833  * Send message to all operators.
834  * one - client not to send message to
835  * from- client which message is from *NEVER* NULL!!
836  */
837 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
838 {
839   va_list vl;
840   int i;
841   struct Client *cptr;
842
843   va_start(vl, pattern);
844   ++sentalong_marker;
845   for (cptr = GlobalClientList; cptr; cptr = cptr->next)
846   {
847     if (!SendWallops(cptr))
848       continue;
849     i = cptr->from->fd;         /* find connection oper is on */
850     if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
851       continue;
852     if (cptr->from == one)
853       continue;                 /* ...was the one I should skip */
854     sentalong[i] = sentalong_marker;
855     vsendto_prefix_one(cptr->from, from, pattern, vl);
856   }
857   va_end(vl);
858
859   return;
860 }
861
862 /*
863  * sendto_g_serv_butone
864  *
865  * Send message to all remote +g users (server links).
866  *
867  * one - server not to send message to.
868  */
869 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
870 {
871   va_list vl;
872   struct Client *cptr;
873   int i;
874
875   va_start(vl, pattern);
876   ++sentalong_marker;
877   vsprintf_irc(sendbuf, pattern, vl);
878   for (cptr = GlobalClientList; cptr; cptr = cptr->next)
879   {
880     if (!SendDebug(cptr))
881       continue;
882     i = cptr->from->fd;         /* find connection user is on */
883     if (i < 0 || sentalong[i] == sentalong_marker)       /* sent message along it already ? */
884       continue;
885     if (MyConnect(cptr))
886       continue;
887     sentalong[i] = sentalong_marker;
888     if (cptr->from == one)
889       continue;
890     sendbufto_one(cptr);
891   }
892   va_end(vl);
893
894   return;
895 }
896
897 /*
898  * sendto_prefix_one
899  *
900  * to - destination client
901  * from - client which message is from
902  *
903  * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
904  * -avalon
905  */
906 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
907 {
908   va_list vl;
909   va_start(vl, pattern);
910   vsendto_prefix_one(to, from, pattern, vl);
911   va_end(vl);
912 }
913
914 /*
915  * sendto_realops
916  *
917  * Send to *local* ops only but NOT +s nonopers.
918  */
919 void sendto_realops(const char *pattern, ...)
920 {
921   va_list vl;
922
923   va_start(vl, pattern);
924   vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
925
926   va_end(vl);
927   return;
928 }
929
930 /*
931  * Send message to all servers of protocol 'p' and lower.
932  */
933 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
934 {
935   va_list vl;
936   struct DLink *lp;
937   va_start(vl, pattern);
938   for (lp = me.serv->down; lp; lp = lp->next)
939     if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
940       vsendto_one(lp->value.cptr, pattern, vl);
941   va_end(vl);
942 }
943
944 /*
945  * Send message to all servers of protocol 'p' and higher.
946  */
947 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
948 {
949   va_list vl;
950   struct DLink *lp;
951   va_start(vl, pattern);
952   for (lp = me.serv->down; lp; lp = lp->next)
953     if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
954       vsendto_one(lp->value.cptr, pattern, vl);
955   va_end(vl);
956 }