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