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