Author: Ghostwolf <foxxe@wtfs.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 "config.h"
23
24 #include "send.h"
25 #include "channel.h"
26 #include "class.h"
27 #include "client.h"
28 #include "ircd.h"
29 #include "ircd_policy.h"
30 #include "ircd_snprintf.h"
31 #include "ircd_string.h"
32 #include "list.h"
33 #include "match.h"
34 #include "msg.h"
35 #include "numnicks.h"
36 #include "parse.h"
37 #include "s_bsd.h"
38 #include "s_debug.h"
39 #include "s_misc.h"
40 #include "s_user.h"
41 #include "struct.h"
42 #include "sys.h"
43
44 #include <assert.h>
45 #include <stdio.h>
46 #include <string.h>
47
48
49 static int sentalong[MAXCONNECTIONS];
50 static int sentalong_marker;
51 struct SLink *opsarray[32];     /* don't use highest bit unless you change
52                                    atoi to strtoul in sendto_op_mask() */
53 static struct Connection *send_queues = 0;
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. It 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   cli_flags(to) |= 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(&(cli_recvQ(to)));
80   MsgQClear(&(cli_sendQ(to)));
81   client_drop_sendq(cli_connect(to));
82
83   /*
84    * Keep a copy of the last comment, for later use...
85    */
86   ircd_strncpy(cli_info(to), notice, REALLEN);
87
88   if (!IsUser(to) && !IsUnknown(to) && !(cli_flags(to) & FLAGS_CLOSING))
89     sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
90   Debug((DEBUG_ERROR, cli_info(to)));
91 }
92
93 static int can_send(struct Client* to)
94 {
95   assert(0 != to);
96   return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 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     struct Connection* con;
116     for (con = send_queues; con; con = con_next(con)) {
117       assert(0 < MsgQLength(&(con_sendQ(con))));
118       send_queued(con_client(con));
119     }
120   }
121 }
122
123 /*
124  * send_queued
125  *
126  * This function is called from the main select-loop (or whatever)
127  * when there is a chance that some output would be possible. This
128  * attempts to empty the send queue as far as possible...
129  */
130 void send_queued(struct Client *to)
131 {
132   assert(0 != to);
133   assert(0 != cli_local(to));
134
135   if (IsBlocked(to) || !can_send(to))
136     return;                     /* Don't bother */
137
138   while (MsgQLength(&(cli_sendQ(to))) > 0) {
139     unsigned int len;
140
141     if ((len = deliver_it(to, &(cli_sendQ(to))))) {
142       msgq_delete(&(cli_sendQ(to)), len);
143       cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
144       if (IsBlocked(to)) {
145         update_write(to);
146         return;
147       }
148     }
149     else {
150       if (IsDead(to)) {
151         char tmp[512];
152         sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
153         dead_link(to, tmp);
154       }
155       return;
156     }
157   }
158
159   /* Ok, sendq is now empty... */
160   client_drop_sendq(cli_connect(to));
161   update_write(to);
162 }
163
164 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
165 {
166   assert(0 != to);
167   assert(0 != buf);
168
169   if (cli_from(to))
170     to = cli_from(to);
171
172   if (!can_send(to))
173     /*
174      * This socket has already been marked as dead
175      */
176     return;
177
178   if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
179     if (IsServer(to))
180       sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
181                            "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
182                            get_sendq(to));
183     dead_link(to, "Max sendQ exceeded");
184     return;
185   }
186
187   Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
188
189   msgq_add(&(cli_sendQ(to)), buf, prio);
190   client_add_sendq(cli_connect(to), &send_queues);
191   update_write(to);
192
193   /*
194    * Update statistics. The following is slightly incorrect
195    * because it counts messages even if queued, but bytes
196    * only really sent. Queued bytes get updated in SendQueued.
197    */
198   ++(cli_sendM(to));
199   ++(cli_sendM(&me));
200   /*
201    * This little bit is to stop the sendQ from growing too large when
202    * there is no need for it to. Thus we call send_queued() every time
203    * 2k has been added to the queue since the last non-fatal write.
204    * Also stops us from deliberately building a large sendQ and then
205    * trying to flood that link with data (possible during the net
206    * relinking done by servers with a large load).
207    */
208   if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
209     send_queued(to);
210 }
211
212 /*
213  * Send a msg to all ppl on servers/hosts that match a specified mask
214  * (used for enhanced PRIVMSGs)
215  *
216  *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
217  */
218
219 static int match_it(struct Client *from, struct Client *one, const char *mask, int what)
220 {
221   switch (what)
222   {
223     case MATCH_HOST:
224       return (match(mask, cli_user(one)->host) == 0 ||
225         (HasHiddenHost(one) && match(mask, cli_user(one)->realhost) == 0));
226     case MATCH_SERVER:
227     default:
228       return (match(mask, cli_name(cli_user(one)->server)) == 0);
229   }
230 }
231
232 /*
233  * Send a raw command to a single client; use *ONLY* if you absolutely
234  * must send a command without a prefix.
235  */
236 void sendrawto_one(struct Client *to, const char *pattern, ...)
237 {
238   struct MsgBuf *mb;
239   va_list vl;
240
241   va_start(vl, pattern);
242   mb = msgq_vmake(to, pattern, vl);
243   va_end(vl);
244
245   send_buffer(to, mb, 0);
246
247   msgq_clean(mb);
248 }
249
250 /*
251  * Send a (prefixed) command to a single client; select which of <cmd>
252  * <tok> to use depending on if to is a server or not.  <from> is the
253  * originator of the command.
254  */
255 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
256                    struct Client *to, const char *pattern, ...)
257 {
258   struct VarData vd;
259   struct MsgBuf *mb;
260
261   to = cli_from(to);
262
263   vd.vd_format = pattern; /* set up the struct VarData for %v */
264   va_start(vd.vd_args, pattern);
265
266   mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
267                  &vd);
268
269   va_end(vd.vd_args);
270
271   send_buffer(to, mb, 0);
272
273   msgq_clean(mb);
274 }
275
276 /*
277  * Send a (prefixed) command to a single client in the priority queue;
278  * select  which of <cmd> <tok> to use depending on if to is a server
279  *or not.  <from> is the originator of the command.
280  */
281 void sendcmdto_prio_one(struct Client *from, const char *cmd, const char *tok,
282                         struct Client *to, const char *pattern, ...)
283 {
284   struct VarData vd;
285   struct MsgBuf *mb;
286
287   to = cli_from(to);
288
289   vd.vd_format = pattern; /* set up the struct VarData for %v */
290   va_start(vd.vd_args, pattern);
291
292   mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
293                  &vd);
294
295   va_end(vd.vd_args);
296
297   send_buffer(to, mb, 1);
298
299   msgq_clean(mb);
300 }
301
302 /*
303  * Send a (prefixed) command to all servers but one, using tok; cmd
304  * is ignored in this particular function.
305  */
306 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
307                            const char *tok, struct Client *one,
308                            const char *pattern, ...)
309 {
310   struct VarData vd;
311   struct MsgBuf *mb;
312   struct DLink *lp;
313
314   vd.vd_format = pattern; /* set up the struct VarData for %v */
315   va_start(vd.vd_args, pattern);
316
317   /* use token */
318   mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
319   va_end(vd.vd_args);
320
321   /* send it to our downlinks */
322   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
323     if (one && lp->value.cptr == cli_from(one))
324       continue;
325     send_buffer(lp->value.cptr, mb, 0);
326   }
327
328   msgq_clean(mb);
329 }
330
331 /*
332  * Send a (prefix) command originating from <from> to all channels
333  * <from> is locally on.  <from> must be a user. <tok> is ignored in
334  * this function.
335  *
336  * Update: don't send to 'one', if any. --Vampire
337  */
338 /* XXX sentalong_marker used XXX
339  *
340  * There is not an easy way to revoke the need for sentalong_marker
341  * from this function.  Thoughts and ideas would be welcome... -Kev
342  *
343  * One possibility would be to internalize the sentalong array; that
344  * could be prohibitively big, though.  We could get around that by
345  * making one that's the number of connected servers or something...
346  * or perhaps by adding a special flag to the servers we've sent a
347  * message to, and then a final loop through the connected servers
348  * to delete the flag. -Kev
349  */
350 void sendcmdto_common_channels_butone(struct Client *from, const char *cmd,
351                                       const char *tok, struct Client *one,
352                                       const char *pattern, ...)
353 {
354   struct VarData vd;
355   struct MsgBuf *mb;
356   struct Membership *chan;
357   struct Membership *member;
358
359   assert(0 != from);
360   assert(0 != cli_from(from));
361   assert(0 != pattern);
362   assert(!IsServer(from) && !IsMe(from));
363
364   vd.vd_format = pattern; /* set up the struct VarData for %v */
365
366   va_start(vd.vd_args, pattern);
367
368   /* build the buffer */
369   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
370   va_end(vd.vd_args);
371
372   sentalong_marker++;
373   if (-1 < cli_fd(cli_from(from)))
374     sentalong[cli_fd(cli_from(from))] = sentalong_marker;
375   /*
376    * loop through from's channels, and the members on their channels
377    */
378   for (chan = cli_user(from)->channel; chan; chan = chan->next_channel) {
379     if (IsZombie(chan))
380       continue;
381     for (member = chan->channel->members; member;
382          member = member->next_member)
383       if (MyConnect(member->user) && -1 < cli_fd(cli_from(member->user)) &&
384           member->user != one &&
385           sentalong[cli_fd(cli_from(member->user))] != sentalong_marker) {
386         sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
387         send_buffer(member->user, mb, 0);
388       }
389   }
390
391   if (MyConnect(from) && from != one)
392     send_buffer(from, mb, 0);
393
394   msgq_clean(mb);
395 }
396
397 /*
398  * Send a (prefixed) command to all local users on the channel specified
399  * by <to>; <tok> is ignored by this function
400  *
401  * Update: don't send to 'one', if any. --Vampire
402  */
403 void sendcmdto_channel_butserv_butone(struct Client *from, const char *cmd,
404                                       const char *tok, struct Channel *to,
405                                       struct Client *one, const char *pattern,
406                                       ...)
407 {
408   struct VarData vd;
409   struct MsgBuf *mb;
410   struct Membership *member;
411
412   vd.vd_format = pattern; /* set up the struct VarData for %v */
413   va_start(vd.vd_args, pattern);
414
415   /* build the buffer */
416   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
417   va_end(vd.vd_args);
418
419   /* send the buffer to each local channel member */
420   for (member = to->members; member; member = member->next_member) {
421     if (MyConnect(member->user) && member->user != one && !IsZombie(member))
422       send_buffer(member->user, mb, 0);
423   }
424
425   msgq_clean(mb);
426 }
427
428 /*
429  * Send a (prefixed) command to all users on this channel, including
430  * remote users; users to skip may be specified by setting appropriate
431  * flags in the <skip> argument.  <one> will also be skipped.
432  */
433 /* XXX sentalong_marker used XXX
434  *
435  * We can drop sentalong_marker from this function by adding a field to
436  * channels and to connections; what we do is make a user's connection
437  * a "member" of the channel by adding it to the new list, and we use
438  * the struct Membership status as a reference count.  Then, to implement
439  * this function, we just walk the list of connections.  Unfortunately,
440  * this doesn't account for sending only to channel ops, or for not
441  * sending to +d users; we could account for that by splitting those
442  * counts out, but that would imply adding two more fields (at least) to
443  * the struct Membership... -Kev
444  */
445 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
446                               const char *tok, struct Channel *to,
447                               struct Client *one, unsigned int skip,
448                               const char *pattern, ...)
449 {
450   struct Membership *member;
451   struct VarData vd;
452   struct MsgBuf *user_mb;
453   struct MsgBuf *serv_mb;
454
455   vd.vd_format = pattern;
456
457   /* Build buffer to send to users */
458   va_start(vd.vd_args, pattern);
459   user_mb = msgq_make(0, skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v",
460                       from, skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
461   va_end(vd.vd_args);
462
463   /* Build buffer to send to servers */
464   va_start(vd.vd_args, pattern);
465   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
466   va_end(vd.vd_args);
467
468   /* send buffer along! */
469   sentalong_marker++;
470   for (member = to->members; member; member = member->next_member) {
471     /* skip one, zombies, and deaf users... */
472     if (cli_from(member->user) == one || IsZombie(member) ||
473         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
474         (skip & SKIP_NONOPS && !IsChanOp(member)) ||
475         (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
476         cli_fd(cli_from(member->user)) < 0 ||
477         sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
478       continue;
479     sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
480
481     if (MyConnect(member->user)) /* pick right buffer to send */
482       send_buffer(member->user, user_mb, 0);
483     else
484       send_buffer(member->user, serv_mb, 0);
485   }
486
487   msgq_clean(user_mb);
488   msgq_clean(serv_mb);
489 }
490
491 /*
492  * Send a (prefixed) command to all users except <one> that have
493  * <flag> set.
494  */
495 void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
496                              const char *pattern, ...)
497 {
498   struct VarData vd;
499   struct Client *cptr;
500   struct MsgBuf *mb;
501   struct DLink *lp;
502   char *prefix=NULL;
503   char *tok=NULL;
504   int i;
505
506   vd.vd_format = pattern;
507
508   /* Build buffer to send to users */
509   va_start(vd.vd_args, pattern);
510   switch (type) {
511         case WALL_DESYNCH:
512                 prefix="";
513                 tok=TOK_DESYNCH;
514                 break;
515         case WALL_WALLOPS:
516                 prefix="* ";
517                 tok=TOK_WALLOPS;
518                 break;
519         case WALL_WALLUSERS:
520                 prefix="$ ";
521                 tok=TOK_WALLUSERS;
522                 break;
523         default:
524                 assert(0);
525   }
526   mb = msgq_make(0, "%:#C " MSG_WALLOPS " :%s%v", from, prefix,&vd);
527   va_end(vd.vd_args);
528
529   /* send buffer along! */
530   for (i = 0; i <= HighestFd; i++) {
531     if (!(cptr = LocalClientArray[i]) ||
532         (cli_fd(cli_from(cptr)) < 0) ||
533         (type == WALL_DESYNCH && !(cli_flags(cptr) & FLAGS_DEBUG)) ||
534 #ifdef HEAD_IN_SAND_WALLOPS
535         (type == WALL_WALLOPS && (!(cli_flags(cptr) & FLAGS_WALLOP) ||
536                                   !IsAnOper(cptr))) ||
537 #else
538         (type == WALL_WALLOPS && !(cli_flags(cptr) & FLAGS_WALLOP)) ||
539 #endif
540         (type == WALL_WALLUSERS && !(cli_flags(cptr) & FLAGS_WALLOP)))
541       continue; /* skip it */
542     send_buffer(cptr, mb, 1);
543   }
544
545   msgq_clean(mb);
546
547   /* Build buffer to send to servers */
548   va_start(vd.vd_args, pattern);
549   mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
550   va_end(vd.vd_args);
551
552   /* send buffer along! */
553   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
554     if (one && lp->value.cptr == cli_from(one))
555       continue;
556     send_buffer(lp->value.cptr, mb, 1);
557   }
558
559   msgq_clean(mb);
560 }
561
562 /*
563  * Send a (prefixed) command to all users who match <to>, under control
564  * of <who>
565  */
566 /* XXX sentalong_marker used XXX
567  *
568  * This is also a difficult one to solve.  The basic approach would be
569  * to walk the client list of each connected server until we find a
570  * match--but then, we also have to walk the client list of all the
571  * servers behind that one.  We could implement this recursively--or we
572  * could add (yet another) field to the connection struct that would be
573  * a linked list of clients introduced through that link, and just walk
574  * that, making this into an iterative implementation.  Unfortunately,
575  * we probably would not be able to use tail recursion for the recursive
576  * solution, so a deep network could exhaust our stack space; therefore
577  * I favor the extra linked list, even though that increases the
578  * complexity of the database. -Kev
579  */
580 void sendcmdto_match_butone(struct Client *from, const char *cmd,
581                             const char *tok, const char *to,
582                             struct Client *one, unsigned int who,
583                             const char *pattern, ...)
584 {
585   struct VarData vd;
586   struct Client *cptr;
587   struct MsgBuf *user_mb;
588   struct MsgBuf *serv_mb;
589
590   vd.vd_format = pattern;
591
592   /* Build buffer to send to users */
593   va_start(vd.vd_args, pattern);
594   user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
595   va_end(vd.vd_args);
596
597   /* Build buffer to send to servers */
598   va_start(vd.vd_args, pattern);
599   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
600   va_end(vd.vd_args);
601
602   /* send buffer along */
603   sentalong_marker++;
604   for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
605     if (!IsRegistered(cptr) || cli_from(cptr) == one || IsServer(cptr) ||
606         IsMe(cptr) || !match_it(from, cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
607         sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
608       continue; /* skip it */
609     sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
610
611     if (MyConnect(cptr)) /* send right buffer */
612       send_buffer(cptr, user_mb, 0);
613     else
614       send_buffer(cptr, serv_mb, 0);
615   }
616
617   msgq_clean(user_mb);
618   msgq_clean(serv_mb);
619 }
620
621 /*
622  * Send a server notice to all users subscribing to the indicated <mask>
623  * except for <one>
624  */
625 void sendto_opmask_butone(struct Client *one, unsigned int mask,
626                           const char *pattern, ...)
627 {
628   va_list vl;
629
630   va_start(vl, pattern);
631   vsendto_opmask_butone(one, mask, pattern, vl);
632   va_end(vl);
633 }
634
635 /*
636  * Send a server notice to all users subscribing to the indicated <mask>
637  * except for <one> - Ratelimited 1 / 30sec
638  */
639 void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
640                                       time_t *rate, const char *pattern, ...)
641 {
642   va_list vl;
643
644   if ((CurrentTime - *rate) < 30) 
645     return;
646   else 
647     *rate = CurrentTime;
648
649   va_start(vl, pattern);
650   vsendto_opmask_butone(one, mask, pattern, vl);
651   va_end(vl);
652     
653 }
654
655
656 /*
657  * Same as above, except called with a variable argument list
658  */
659 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
660                            const char *pattern, va_list vl)
661 {
662   struct VarData vd;
663   struct MsgBuf *mb;
664   int i = 0; /* so that 1 points to opsarray[0] */
665   struct SLink *opslist;
666
667   while ((mask >>= 1))
668     i++;
669
670   if (!(opslist = opsarray[i]))
671     return;
672
673   /*
674    * build string; I don't want to bother with client nicknames, so I hope
675    * this is ok...
676    */
677   vd.vd_format = pattern;
678   vd.vd_args = vl;
679   mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
680                  &vd);
681
682   for (; opslist; opslist = opslist->next)
683     send_buffer(opslist->value.cptr, mb, 0);
684
685   msgq_clean(mb);
686 }