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