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 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 /*
51  * dead_link
52  *
53  * An error has been detected. The link *must* be closed,
54  * but *cannot* call ExitClient (m_bye) from here.
55  * Instead, mark it with FLAGS_DEADSOCKET. This should
56  * generate ExitClient from the main loop.
57  *
58  * If 'notice' is not NULL, it is assumed to be a format
59  * for a message to local opers. It can contain only one
60  * '%s', which will be replaced by the sockhost field of
61  * the failing link.
62  *
63  * Also, the notice is skipped for "uninteresting" cases,
64  * like Persons and yet unknown connections...
65  */
66
67 static void dead_link(struct Client *to, char *notice)
68 {
69   cli_flags(to) |= FLAGS_DEADSOCKET;
70   /*
71    * If because of BUFFERPOOL problem then clean dbuf's now so that
72    * notices don't hurt operators below.
73    */
74   DBufClear(&(cli_recvQ(to)));
75   MsgQClear(&(cli_sendQ(to)));
76
77   /*
78    * Keep a copy of the last comment, for later use...
79    */
80   ircd_strncpy(cli_info(to), notice, REALLEN);
81
82   if (!IsUser(to) && !IsUnknown(to) && !(cli_flags(to) & FLAGS_CLOSING))
83     sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
84   Debug((DEBUG_ERROR, cli_info(to)));
85 }
86
87 static int can_send(struct Client* to)
88 {
89   assert(0 != to);
90   return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
91 }
92
93 /*
94  * flush_connections
95  *
96  * Used to empty all output buffers for all connections. Should only
97  * be called once per scan of connections. There should be a select in
98  * here perhaps but that means either forcing a timeout or doing a poll.
99  * When flushing, all we do is empty the obuffer array for each local
100  * client and try to send it. if we cant send it, it goes into the sendQ
101  * -avalon
102  */
103 void flush_connections(struct Client* cptr)
104 {
105   if (cptr) {
106     send_queued(cptr);
107   }
108   else {
109     int i;
110     for (i = HighestFd; i >= 0; i--) {
111       if ((cptr = LocalClientArray[i]))
112         send_queued(cptr);
113     }
114   }
115 }
116
117 /*
118  * flush_sendq_except - run through local client array and flush
119  * the sendq for each client, if the address of the client sendq
120  * is the same as the one specified, it is skipped. This is used
121  * by dbuf_put to try to get some more memory before bailing and
122  * causing the client to be disconnected.
123  */
124 void flush_sendq_except(void)
125 {
126   int i;
127   struct Client* cptr;
128   for (i = HighestFd; i >= 0; i--) {
129     if ( (cptr = LocalClientArray[i]))
130       send_queued(cptr);
131   }
132 }
133
134 /*
135  * send_queued
136  *
137  * This function is called from the main select-loop (or whatever)
138  * when there is a chance that some output would be possible. This
139  * attempts to empty the send queue as far as possible...
140  */
141 void send_queued(struct Client *to)
142 {
143   assert(0 != to);
144   assert(0 != cli_local(to));
145
146   if (IsBlocked(to) || !can_send(to))
147     return;                     /* Don't bother */
148
149   while (MsgQLength(&(cli_sendQ(to))) > 0) {
150     unsigned int len;
151
152     if ((len = deliver_it(to, &(cli_sendQ(to))))) {
153       msgq_delete(&(cli_sendQ(to)), len);
154       cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
155       if (IsBlocked(to))
156         break;
157     }
158     else {
159       if (IsDead(to)) {
160         char tmp[512];
161         sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
162         dead_link(to, tmp);
163       }
164       break;
165     }
166   }
167 }
168
169 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
170 {
171   assert(0 != to);
172   assert(0 != buf);
173
174   if (cli_from(to))
175     to = cli_from(to);
176
177   if (!can_send(to))
178     /*
179      * This socket has already been marked as dead
180      */
181     return;
182
183   if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
184     if (IsServer(to))
185       sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
186                            "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
187                            get_sendq(to));
188     dead_link(to, "Max sendQ exceeded");
189     return;
190   }
191
192   Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
193
194   msgq_add(&(cli_sendQ(to)), buf, prio);
195
196   /*
197    * Update statistics. The following is slightly incorrect
198    * because it counts messages even if queued, but bytes
199    * only really sent. Queued bytes get updated in SendQueued.
200    */
201   ++(cli_sendM(to));
202   ++(cli_sendM(&me));
203   /*
204    * This little bit is to stop the sendQ from growing too large when
205    * there is no need for it to. Thus we call send_queued() every time
206    * 2k has been added to the queue since the last non-fatal write.
207    * Also stops us from deliberately building a large sendQ and then
208    * trying to flood that link with data (possible during the net
209    * relinking done by servers with a large load).
210    */
211   if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
212     send_queued(to);
213 }
214
215 /*
216  * Send a msg to all ppl on servers/hosts that match a specified mask
217  * (used for enhanced PRIVMSGs)
218  *
219  *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
220  */
221
222 static int match_it(struct Client *one, const char *mask, int what)
223 {
224   switch (what)
225   {
226     case MATCH_HOST:
227       return (match(mask, cli_user(one)->host) == 0);
228     case MATCH_SERVER:
229     default:
230       return (match(mask, cli_name(cli_user(one)->server)) == 0);
231   }
232 }
233
234 /*
235  * Send a raw command to a single client; use *ONLY* if you absolutely
236  * must send a command without a prefix.
237  */
238 void sendrawto_one(struct Client *to, const char *pattern, ...)
239 {
240   struct MsgBuf *mb;
241   va_list vl;
242
243   va_start(vl, pattern);
244   mb = msgq_vmake(to, pattern, vl);
245   va_end(vl);
246
247   send_buffer(to, mb, 0);
248
249   msgq_clean(mb);
250 }
251
252 /*
253  * Send a (prefixed) command to a single client; select which of <cmd>
254  * <tok> to use depending on if to is a server or not.  <from> is the
255  * originator of the command.
256  */
257 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
258                    struct Client *to, const char *pattern, ...)
259 {
260   va_list vl;
261
262   va_start(vl, pattern);
263   vsendcmdto_one(from, cmd, tok, to, pattern, vl);
264   va_end(vl);
265 }
266
267 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
268                     struct Client *to, const char *pattern, va_list vl)
269 {
270   struct VarData vd;
271   struct MsgBuf *mb;
272
273   to = cli_from(to);
274
275   vd.vd_format = pattern; /* set up the struct VarData for %v */
276   vd.vd_args = vl;
277
278   mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
279                  &vd);
280
281   send_buffer(to, mb, 0);
282
283   msgq_clean(mb);
284 }
285
286 /*
287  * Send a (prefixed) command to all servers but one, using tok; cmd
288  * is ignored in this particular function.
289  */
290 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
291                            const char *tok, struct Client *one,
292                            const char *pattern, ...)
293 {
294   struct VarData vd;
295   struct MsgBuf *mb;
296   struct DLink *lp;
297
298   vd.vd_format = pattern; /* set up the struct VarData for %v */
299   va_start(vd.vd_args, pattern);
300
301   /* use token */
302   mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
303   va_end(vd.vd_args);
304
305   /* send it to our downlinks */
306   for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
307     if (one && lp->value.cptr == cli_from(one))
308       continue;
309     send_buffer(lp->value.cptr, mb, 0);
310   }
311
312   msgq_clean(mb);
313 }
314
315 /*
316  * Send a (prefix) command originating from <from> to all channels
317  * <from> is locally on.  <from> must be a user. <tok> is ignored in
318  * this function.
319  */
320 /* XXX sentalong_marker used XXX
321  *
322  * There is not an easy way to revoke the need for sentalong_marker
323  * from this function.  Thoughts and ideas would be welcome... -Kev
324  *
325  * One possibility would be to internalize the sentalong array; that
326  * could be prohibitively big, though.  We could get around that by
327  * making one that's the number of connected servers or something...
328  * or perhaps by adding a special flag to the servers we've sent a
329  * message to, and then a final loop through the connected servers
330  * to delete the flag. -Kev
331  */
332 void sendcmdto_common_channels(struct Client *from, const char *cmd,
333                                const char *tok, const char *pattern, ...)
334 {
335   struct VarData vd;
336   struct MsgBuf *mb;
337   struct Membership *chan;
338   struct Membership *member;
339
340   assert(0 != from);
341   assert(0 != cli_from(from));
342   assert(0 != pattern);
343   assert(!IsServer(from) && !IsMe(from));
344
345   vd.vd_format = pattern; /* set up the struct VarData for %v */
346
347   va_start(vd.vd_args, pattern);
348
349   /* build the buffer */
350   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
351   va_end(vd.vd_args);
352
353   sentalong_marker++;
354   if (-1 < cli_fd(cli_from(from)))
355     sentalong[cli_fd(cli_from(from))] = sentalong_marker;
356   /*
357    * loop through from's channels, and the members on their channels
358    */
359   for (chan = cli_user(from)->channel; chan; chan = chan->next_channel)
360     for (member = chan->channel->members; member;
361          member = member->next_member)
362       if (MyConnect(member->user) && -1 < cli_fd(cli_from(member->user)) &&
363           sentalong[cli_fd(cli_from(member->user))] != sentalong_marker) {
364         sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
365         send_buffer(member->user, mb, 0);
366       }
367
368   if (MyConnect(from))
369     send_buffer(from, mb, 0);
370
371   msgq_clean(mb);
372 }
373
374 /*
375  * Send a (prefixed) command to all local users on the channel specified
376  * by <to>; <tok> is ignored by this function
377  */
378 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
379                                const char *tok, struct Channel *to,
380                                const char *pattern, ...)
381 {
382   struct VarData vd;
383   struct MsgBuf *mb;
384   struct Membership *member;
385
386   vd.vd_format = pattern; /* set up the struct VarData for %v */
387   va_start(vd.vd_args, pattern);
388
389   /* build the buffer */
390   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
391   va_end(vd.vd_args);
392
393   /* send the buffer to each local channel member */
394   for (member = to->members; member; member = member->next_member) {
395     if (MyConnect(member->user) && !IsZombie(member))
396       send_buffer(member->user, mb, 0);
397   }
398
399   msgq_clean(mb);
400 }
401
402 /*
403  * Send a (prefixed) command to all users on this channel, including
404  * remote users; users to skip may be specified by setting appropriate
405  * flags in the <skip> argument.  <one> will also be skipped.
406  */
407 /* XXX sentalong_marker used XXX
408  *
409  * We can drop sentalong_marker from this function by adding a field to
410  * channels and to connections; what we do is make a user's connection
411  * a "member" of the channel by adding it to the new list, and we use
412  * the struct Membership status as a reference count.  Then, to implement
413  * this function, we just walk the list of connections.  Unfortunately,
414  * this doesn't account for sending only to channel ops, or for not
415  * sending to +d users; we could account for that by splitting those
416  * counts out, but that would imply adding two more fields (at least) to
417  * the struct Membership... -Kev
418  */
419 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
420                               const char *tok, struct Channel *to,
421                               struct Client *one, unsigned int skip,
422                               const char *pattern, ...)
423 {
424   struct Membership *member;
425   struct VarData vd;
426   struct MsgBuf *user_mb;
427   struct MsgBuf *serv_mb;
428
429   vd.vd_format = pattern;
430
431   /* Build buffer to send to users */
432   va_start(vd.vd_args, pattern);
433   user_mb = msgq_make(0, skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v",
434                       from, skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
435   va_end(vd.vd_args);
436
437   /* Build buffer to send to servers */
438   va_start(vd.vd_args, pattern);
439   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
440   va_end(vd.vd_args);
441
442   /* send buffer along! */
443   sentalong_marker++;
444   for (member = to->members; member; member = member->next_member) {
445     /* skip one, zombies, and deaf users... */
446     if (cli_from(member->user) == one || IsZombie(member) ||
447         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
448         (skip & SKIP_NONOPS && !IsChanOp(member)) ||
449         (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
450         cli_fd(cli_from(member->user)) < 0 ||
451         sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
452       continue;
453     sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
454
455     if (MyConnect(member->user)) /* pick right buffer to send */
456       send_buffer(member->user, user_mb, 0);
457     else
458       send_buffer(member->user, serv_mb, 0);
459   }
460
461   msgq_clean(user_mb);
462   msgq_clean(serv_mb);
463 }
464
465 /*
466  * Send a (prefixed) command to all users except <one> that have
467  * <flag> set.
468  */
469 /* XXX sentalong_marker used XXX
470  *
471  * Again, we can solve this use of sentalong_marker by adding a field
472  * to connections--a count of the number of +w users, and another count
473  * of +g users.  Then, just walk through the local clients to send
474  * those messages, and another walk through the connected servers list,
475  * sending only if there's a non-zero count.  No caveats here, either,
476  * beyond remembering to decrement the count when a user /quit's or is
477  * killed, or a server is squit. -Kev
478  */
479 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
480                            const char *tok, struct Client *one,
481                            unsigned int flag, const char *pattern, ...)
482 {
483   struct VarData vd;
484   struct Client *cptr;
485   struct MsgBuf *user_mb;
486   struct MsgBuf *serv_mb;
487
488   vd.vd_format = pattern;
489
490   /* Build buffer to send to users */
491   va_start(vd.vd_args, pattern);
492   user_mb = msgq_make(0, "%:#C " MSG_WALLOPS " %v", from, &vd);
493   va_end(vd.vd_args);
494
495   /* Build buffer to send to servers */
496   va_start(vd.vd_args, pattern);
497   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
498   va_end(vd.vd_args);
499
500   /* send buffer along! */
501   sentalong_marker++;
502   for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
503     if (cli_from(cptr) == one || IsServer(cptr) || !(cli_flags(cptr) & flag) ||
504         cli_fd(cli_from(cptr)) < 0 ||
505         sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
506       continue; /* skip it */
507     sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
508
509     if (MyConnect(cptr)) /* send right buffer */
510       send_buffer(cptr, user_mb, 1);
511     else
512       send_buffer(cptr, serv_mb, 1);
513   }
514
515   msgq_clean(user_mb);
516   msgq_clean(serv_mb);
517 }
518
519 /*
520  * Send a (prefixed) command to all users who match <to>, under control
521  * of <who>
522  */
523 /* XXX sentalong_marker used XXX
524  *
525  * This is also a difficult one to solve.  The basic approach would be
526  * to walk the client list of each connected server until we find a
527  * match--but then, we also have to walk the client list of all the
528  * servers behind that one.  We could implement this recursively--or we
529  * could add (yet another) field to the connection struct that would be
530  * a linked list of clients introduced through that link, and just walk
531  * that, making this into an iterative implementation.  Unfortunately,
532  * we probably would not be able to use tail recursion for the recursive
533  * solution, so a deep network could exhaust our stack space; therefore
534  * I favor the extra linked list, even though that increases the
535  * complexity of the database. -Kev
536  */
537 void sendcmdto_match_butone(struct Client *from, const char *cmd,
538                             const char *tok, const char *to,
539                             struct Client *one, unsigned int who,
540                             const char *pattern, ...)
541 {
542   struct VarData vd;
543   struct Client *cptr;
544   struct MsgBuf *user_mb;
545   struct MsgBuf *serv_mb;
546
547   vd.vd_format = pattern;
548
549   /* Build buffer to send to users */
550   va_start(vd.vd_args, pattern);
551   user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
552   va_end(vd.vd_args);
553
554   /* Build buffer to send to servers */
555   va_start(vd.vd_args, pattern);
556   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
557   va_end(vd.vd_args);
558
559   /* send buffer along */
560   sentalong_marker++;
561   for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
562     if (cli_from(cptr) == one || IsServer(cptr) || IsMe(cptr) ||
563         !match_it(cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
564         sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
565       continue; /* skip it */
566     sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
567
568     if (MyConnect(cptr)) /* send right buffer */
569       send_buffer(cptr, user_mb, 0);
570     else
571       send_buffer(cptr, serv_mb, 0);
572   }
573
574   msgq_clean(user_mb);
575   msgq_clean(serv_mb);
576 }
577
578 /*
579  * Send a server notice to all users subscribing to the indicated <mask>
580  * except for <one>
581  */
582 void sendto_opmask_butone(struct Client *one, unsigned int mask,
583                           const char *pattern, ...)
584 {
585   va_list vl;
586
587   va_start(vl, pattern);
588   vsendto_opmask_butone(one, mask, pattern, vl);
589   va_end(vl);
590 }
591
592 /*
593  * Same as above, except called with a variable argument list
594  */
595 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
596                            const char *pattern, va_list vl)
597 {
598   struct VarData vd;
599   struct MsgBuf *mb;
600   int i = 0; /* so that 1 points to opsarray[0] */
601   struct SLink *opslist;
602
603   while ((mask >>= 1))
604     i++;
605
606   if (!(opslist = opsarray[i]))
607     return;
608
609   /*
610    * build string; I don't want to bother with client nicknames, so I hope
611    * this is ok...
612    */
613   vd.vd_format = pattern;
614   vd.vd_args = vl;
615   mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
616                  &vd);
617
618   for (; opslist; opslist = opslist->next)
619     send_buffer(opslist->value.cptr, mb, 0);
620
621   msgq_clean(mb);
622 }