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   to->flags |= 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(&to->recvQ);
75   MsgQClear(&to->sendQ);
76
77   /*
78    * Keep a copy of the last comment, for later use...
79    */
80   ircd_strncpy(to->info, notice, REALLEN);
81
82   if (!IsUser(to) && !IsUnknown(to) && !(to->flags & FLAGS_CLOSING))
83     sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", to->info, to->name);
84   Debug((DEBUG_ERROR, to->info));
85 }
86
87 static int can_send(struct Client* to)
88 {
89   assert(0 != to);
90   return (IsDead(to) || IsMe(to) || -1 == to->fd) ? 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 != to->local);
145
146   if (IsBlocked(to) || !can_send(to))
147     return;                     /* Don't bother */
148
149   while (MsgQLength(&to->sendQ) > 0) {
150     unsigned int len;
151     const char* msg = msgq_map(&to->sendQ, &len);
152
153     if ((len = deliver_it(to, msg, len))) {
154       msgq_delete(&to->sendQ, len);
155       to->lastsq = MsgQLength(&to->sendQ) / 1024;
156       if (IsBlocked(to))
157         break;
158     }
159     else {
160       if (IsDead(to)) {
161         char tmp[512];
162         sprintf(tmp,"Write error: %s",(strerror(to->error)) ? (strerror(to->error)) : "Unknown error" );
163         dead_link(to, tmp);
164       }
165       break;
166     }
167   }
168 }
169
170 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
171 {
172   assert(0 != to);
173   assert(0 != buf);
174
175   if (to->from)
176     to = to->from;
177
178   if (!can_send(to))
179     /*
180      * This socket has already been marked as dead
181      */
182     return;
183
184   if (MsgQLength(&to->sendQ) > get_sendq(to)) {
185     if (IsServer(to))
186       sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
187                            "%zu > %zu", to, MsgQLength(&to->sendQ),
188                            get_sendq(to));
189     dead_link(to, "Max sendQ exceeded");
190     return;
191   }
192
193   Debug((DEBUG_SEND, "Sending [%p] to %s", buf, to->name));
194
195   msgq_add(&to->sendQ, buf, prio);
196
197   /*
198    * Update statistics. The following is slightly incorrect
199    * because it counts messages even if queued, but bytes
200    * only really sent. Queued bytes get updated in SendQueued.
201    */
202   ++to->sendM;
203   ++me.sendM;
204   /*
205    * This little bit is to stop the sendQ from growing too large when
206    * there is no need for it to. Thus we call send_queued() every time
207    * 2k has been added to the queue since the last non-fatal write.
208    * Also stops us from deliberately building a large sendQ and then
209    * trying to flood that link with data (possible during the net
210    * relinking done by servers with a large load).
211    */
212   if (MsgQLength(&to->sendQ) / 1024 > to->lastsq)
213     send_queued(to);
214 }
215
216 /*
217  * Send a msg to all ppl on servers/hosts that match a specified mask
218  * (used for enhanced PRIVMSGs)
219  *
220  *  addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
221  */
222
223 static int match_it(struct Client *one, const char *mask, int what)
224 {
225   switch (what)
226   {
227     case MATCH_HOST:
228       return (match(mask, one->user->host) == 0);
229     case MATCH_SERVER:
230     default:
231       return (match(mask, one->user->server->name) == 0);
232   }
233 }
234
235 /*
236  * Send a raw command to a single client; use *ONLY* if you absolutely
237  * must send a command without a prefix.
238  */
239 void sendrawto_one(struct Client *to, const char *pattern, ...)
240 {
241   struct MsgBuf *mb;
242   va_list vl;
243
244   va_start(vl, pattern);
245   mb = msgq_vmake(to, pattern, vl);
246   va_end(vl);
247
248   send_buffer(to, mb, 0);
249
250   msgq_clean(mb);
251 }
252
253 /*
254  * Send a (prefixed) command to a single client; select which of <cmd>
255  * <tok> to use depending on if to is a server or not.  <from> is the
256  * originator of the command.
257  */
258 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
259                    struct Client *to, const char *pattern, ...)
260 {
261   va_list vl;
262
263   va_start(vl, pattern);
264   vsendcmdto_one(from, cmd, tok, to, pattern, vl);
265   va_end(vl);
266 }
267
268 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
269                     struct Client *to, const char *pattern, va_list vl)
270 {
271   struct VarData vd;
272   struct MsgBuf *mb;
273
274   to = to->from;
275
276   vd.vd_format = pattern; /* set up the struct VarData for %v */
277   vd.vd_args = vl;
278
279   mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
280                  &vd);
281
282   send_buffer(to, mb, 0);
283
284   msgq_clean(mb);
285 }
286
287 /*
288  * Send a (prefixed) command to all servers but one, using tok; cmd
289  * is ignored in this particular function.
290  */
291 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
292                            const char *tok, struct Client *one,
293                            const char *pattern, ...)
294 {
295   struct VarData vd;
296   struct MsgBuf *mb;
297   struct DLink *lp;
298
299   vd.vd_format = pattern; /* set up the struct VarData for %v */
300   va_start(vd.vd_args, pattern);
301
302   /* use token */
303   mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
304   va_end(vd.vd_args);
305
306   /* send it to our downlinks */
307   for (lp = me.serv->down; lp; lp = lp->next) {
308     if (one && lp->value.cptr == one->from)
309       continue;
310     send_buffer(lp->value.cptr, mb, 0);
311   }
312
313   msgq_clean(mb);
314 }
315
316 /*
317  * Send a (prefix) command originating from <from> to all channels
318  * <from> is locally on.  <from> must be a user. <tok> is ignored in
319  * this function.
320  */
321 /* XXX sentalong_marker used XXX
322  *
323  * There is not an easy way to revoke the need for sentalong_marker
324  * from this function.  Thoughts and ideas would be welcome... -Kev
325  *
326  * One possibility would be to internalize the sentalong array; that
327  * could be prohibitively big, though.  We could get around that by
328  * making one that's the number of connected servers or something...
329  * or perhaps by adding a special flag to the servers we've sent a
330  * message to, and then a final loop through the connected servers
331  * to delete the flag. -Kev
332  */
333 void sendcmdto_common_channels(struct Client *from, const char *cmd,
334                                const char *tok, const char *pattern, ...)
335 {
336   struct VarData vd;
337   struct MsgBuf *mb;
338   struct Membership *chan;
339   struct Membership *member;
340
341   assert(0 != from);
342   assert(0 != from->from);
343   assert(0 != pattern);
344   assert(!IsServer(from) && !IsMe(from));
345
346   vd.vd_format = pattern; /* set up the struct VarData for %v */
347
348   va_start(vd.vd_args, pattern);
349
350   /* build the buffer */
351   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
352   va_end(vd.vd_args);
353
354   sentalong_marker++;
355   if (-1 < from->from->fd)
356     sentalong[from->from->fd] = sentalong_marker;
357   /*
358    * loop through from's channels, and the members on their channels
359    */
360   for (chan = from->user->channel; chan; chan = chan->next_channel)
361     for (member = chan->channel->members; member;
362          member = member->next_member)
363       if (MyConnect(member->user) && -1 < member->user->from->fd &&
364           sentalong[member->user->from->fd] != sentalong_marker) {
365         sentalong[member->user->from->fd] = sentalong_marker;
366         send_buffer(member->user, mb, 0);
367       }
368
369   if (MyConnect(from))
370     send_buffer(from, mb, 0);
371
372   msgq_clean(mb);
373 }
374
375 /*
376  * Send a (prefixed) command to all local users on the channel specified
377  * by <to>; <tok> is ignored by this function
378  */
379 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
380                                const char *tok, struct Channel *to,
381                                const char *pattern, ...)
382 {
383   struct VarData vd;
384   struct MsgBuf *mb;
385   struct Membership *member;
386
387   vd.vd_format = pattern; /* set up the struct VarData for %v */
388   va_start(vd.vd_args, pattern);
389
390   /* build the buffer */
391   mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
392   va_end(vd.vd_args);
393
394   /* send the buffer to each local channel member */
395   for (member = to->members; member; member = member->next_member) {
396     if (MyConnect(member->user) && !IsZombie(member))
397       send_buffer(member->user, mb, 0);
398   }
399
400   msgq_clean(mb);
401 }
402
403 /*
404  * Send a (prefixed) command to all users on this channel, including
405  * remote users; users to skip may be specified by setting appropriate
406  * flags in the <skip> argument.  <one> will also be skipped.
407  */
408 /* XXX sentalong_marker used XXX
409  *
410  * We can drop sentalong_marker from this function by adding a field to
411  * channels and to connections; what we do is make a user's connection
412  * a "member" of the channel by adding it to the new list, and we use
413  * the struct Membership status as a reference count.  Then, to implement
414  * this function, we just walk the list of connections.  Unfortunately,
415  * this doesn't account for sending only to channel ops, or for not
416  * sending to +d users; we could account for that by splitting those
417  * counts out, but that would imply adding two more fields (at least) to
418  * the struct Membership... -Kev
419  */
420 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
421                               const char *tok, struct Channel *to,
422                               struct Client *one, unsigned int skip,
423                               const char *pattern, ...)
424 {
425   struct Membership *member;
426   struct VarData vd;
427   struct MsgBuf *user_mb;
428   struct MsgBuf *serv_mb;
429
430   vd.vd_format = pattern;
431
432   /* Build buffer to send to users */
433   va_start(vd.vd_args, pattern);
434   user_mb = msgq_make(0, skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v",
435                       from, skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
436   va_end(vd.vd_args);
437
438   /* Build buffer to send to servers */
439   va_start(vd.vd_args, pattern);
440   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
441   va_end(vd.vd_args);
442
443   /* send buffer along! */
444   sentalong_marker++;
445   for (member = to->members; member; member = member->next_member) {
446     /* skip one, zombies, and deaf users... */
447     if (member->user->from == one || IsZombie(member) ||
448         (skip & SKIP_DEAF && IsDeaf(member->user)) ||
449         (skip & SKIP_NONOPS && !IsChanOp(member)) ||
450         (skip & SKIP_BURST && IsBurstOrBurstAck(member->user->from)) ||
451         member->user->from->fd < 0 ||
452         sentalong[member->user->from->fd] == sentalong_marker)
453       continue;
454     sentalong[member->user->from->fd] = sentalong_marker;
455
456     if (MyConnect(member->user)) /* pick right buffer to send */
457       send_buffer(member->user, user_mb, 0);
458     else
459       send_buffer(member->user, serv_mb, 0);
460   }
461
462   msgq_clean(user_mb);
463   msgq_clean(serv_mb);
464 }
465
466 /*
467  * Send a (prefixed) command to all users except <one> that have
468  * <flag> set.
469  */
470 /* XXX sentalong_marker used XXX
471  *
472  * Again, we can solve this use of sentalong_marker by adding a field
473  * to connections--a count of the number of +w users, and another count
474  * of +g users.  Then, just walk through the local clients to send
475  * those messages, and another walk through the connected servers list,
476  * sending only if there's a non-zero count.  No caveats here, either,
477  * beyond remembering to decrement the count when a user /quit's or is
478  * killed, or a server is squit. -Kev
479  */
480 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
481                            const char *tok, struct Client *one,
482                            unsigned int flag, const char *pattern, ...)
483 {
484   struct VarData vd;
485   struct Client *cptr;
486   struct MsgBuf *user_mb;
487   struct MsgBuf *serv_mb;
488
489   vd.vd_format = pattern;
490
491   /* Build buffer to send to users */
492   va_start(vd.vd_args, pattern);
493   user_mb = msgq_make(0, "%:#C " MSG_WALLOPS " %v", from, &vd);
494   va_end(vd.vd_args);
495
496   /* Build buffer to send to servers */
497   va_start(vd.vd_args, pattern);
498   serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
499   va_end(vd.vd_args);
500
501   /* send buffer along! */
502   sentalong_marker++;
503   for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
504     if (cptr->from == one || IsServer(cptr) || !(cptr->flags & flag) ||
505         cptr->from->fd < 0 || sentalong[cptr->from->fd] == sentalong_marker)
506       continue; /* skip it */
507     sentalong[cptr->from->fd] = 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 = cptr->next) {
562     if (cptr->from == one || IsServer(cptr) || IsMe(cptr) ||
563         !match_it(cptr, to, who) || cptr->from->fd < 0 ||
564         sentalong[cptr->from->fd] == sentalong_marker)
565       continue; /* skip it */
566     sentalong[cptr->from->fd] = 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", me.name, &vd);
616
617   for (; opslist; opslist = opslist->next)
618     send_buffer(opslist->value.cptr, mb, 0);
619
620   msgq_clean(mb);
621 }