2 * IRC - Internet Relay Chat, common/send.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
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)
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.
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.
29 #include "ircd_policy.h"
30 #include "ircd_snprintf.h"
31 #include "ircd_string.h"
49 static int sentalong[MAXCONNECTIONS];
50 static int sentalong_marker;
51 struct SLink *opsarray[32]; /* don't use highest bit unless you change
52 atoi to strtoul in sendto_op_mask() */
53 static struct Connection *send_queues = 0;
58 * An error has been detected. The link *must* be closed,
59 * but *cannot* call ExitClient (m_bye) from here.
60 * Instead, mark it with FLAGS_DEADSOCKET. This should
61 * generate ExitClient from the main loop.
63 * If 'notice' is not NULL, it is assumed to be a format
64 * for a message to local opers. It can contain only one
65 * '%s', which will be replaced by the sockhost field of
68 * Also, the notice is skipped for "uninteresting" cases,
69 * like Persons and yet unknown connections...
72 static void dead_link(struct Client *to, char *notice)
74 cli_flags(to) |= FLAGS_DEADSOCKET;
76 * If because of BUFFERPOOL problem then clean dbuf's now so that
77 * notices don't hurt operators below.
79 DBufClear(&(cli_recvQ(to)));
80 MsgQClear(&(cli_sendQ(to)));
81 client_drop_sendq(cli_connect(to));
84 * Keep a copy of the last comment, for later use...
86 ircd_strncpy(cli_info(to), notice, REALLEN);
88 if (!IsUser(to) && !IsUnknown(to) && !(cli_flags(to) & FLAGS_CLOSING))
89 sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
90 Debug((DEBUG_ERROR, cli_info(to)));
93 static int can_send(struct Client* to)
96 return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
102 * Used to empty all output buffers for all connections. Should only
103 * be called once per scan of connections. There should be a select in
104 * here perhaps but that means either forcing a timeout or doing a poll.
105 * When flushing, all we do is empty the obuffer array for each local
106 * client and try to send it. if we cant send it, it goes into the sendQ
109 void flush_connections(struct Client* cptr)
115 struct Connection* con;
116 for (con = send_queues; con; con = con_next(con)) {
117 assert(0 < MsgQLength(&(con_sendQ(con))));
118 send_queued(con_client(con));
126 * This function is called from the main select-loop (or whatever)
127 * when there is a chance that some output would be possible. This
128 * attempts to empty the send queue as far as possible...
130 void send_queued(struct Client *to)
133 assert(0 != cli_local(to));
135 if (IsBlocked(to) || !can_send(to))
136 return; /* Don't bother */
138 while (MsgQLength(&(cli_sendQ(to))) > 0) {
141 if ((len = deliver_it(to, &(cli_sendQ(to))))) {
142 msgq_delete(&(cli_sendQ(to)), len);
143 cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
152 sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
159 /* Ok, sendq is now empty... */
160 client_drop_sendq(cli_connect(to));
164 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
174 * This socket has already been marked as dead
178 if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
180 sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
181 "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
183 dead_link(to, "Max sendQ exceeded");
187 Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
189 msgq_add(&(cli_sendQ(to)), buf, prio);
190 client_add_sendq(cli_connect(to), &send_queues);
194 * Update statistics. The following is slightly incorrect
195 * because it counts messages even if queued, but bytes
196 * only really sent. Queued bytes get updated in SendQueued.
201 * This little bit is to stop the sendQ from growing too large when
202 * there is no need for it to. Thus we call send_queued() every time
203 * 2k has been added to the queue since the last non-fatal write.
204 * Also stops us from deliberately building a large sendQ and then
205 * trying to flood that link with data (possible during the net
206 * relinking done by servers with a large load).
208 if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
213 * Send a msg to all ppl on servers/hosts that match a specified mask
214 * (used for enhanced PRIVMSGs)
216 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
219 static int match_it(struct Client *from, struct Client *one, const char *mask, int what)
224 return (match(mask, cli_user(one)->host) == 0 ||
225 (HasHiddenHost(one) && match(mask, cli_user(one)->realhost) == 0));
228 return (match(mask, cli_name(cli_user(one)->server)) == 0);
233 * Send a raw command to a single client; use *ONLY* if you absolutely
234 * must send a command without a prefix.
236 void sendrawto_one(struct Client *to, const char *pattern, ...)
241 va_start(vl, pattern);
242 mb = msgq_vmake(to, pattern, vl);
245 send_buffer(to, mb, 0);
251 * Send a (prefixed) command to a single client; select which of <cmd>
252 * <tok> to use depending on if to is a server or not. <from> is the
253 * originator of the command.
255 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
256 struct Client *to, const char *pattern, ...)
263 vd.vd_format = pattern; /* set up the struct VarData for %v */
264 va_start(vd.vd_args, pattern);
266 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
271 send_buffer(to, mb, 0);
277 * Send a (prefixed) command to a single client in the priority queue;
278 * select which of <cmd> <tok> to use depending on if to is a server
279 *or not. <from> is the originator of the command.
281 void sendcmdto_prio_one(struct Client *from, const char *cmd, const char *tok,
282 struct Client *to, const char *pattern, ...)
289 vd.vd_format = pattern; /* set up the struct VarData for %v */
290 va_start(vd.vd_args, pattern);
292 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
297 send_buffer(to, mb, 1);
303 * Send a (prefixed) command to all servers but one, using tok; cmd
304 * is ignored in this particular function.
306 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
307 const char *tok, struct Client *one,
308 const char *pattern, ...)
314 vd.vd_format = pattern; /* set up the struct VarData for %v */
315 va_start(vd.vd_args, pattern);
318 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
321 /* send it to our downlinks */
322 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
323 if (one && lp->value.cptr == cli_from(one))
325 send_buffer(lp->value.cptr, mb, 0);
332 * Send a (prefix) command originating from <from> to all channels
333 * <from> is locally on. <from> must be a user. <tok> is ignored in
336 * Update: don't send to 'one', if any. --Vampire
338 /* XXX sentalong_marker used XXX
340 * There is not an easy way to revoke the need for sentalong_marker
341 * from this function. Thoughts and ideas would be welcome... -Kev
343 * One possibility would be to internalize the sentalong array; that
344 * could be prohibitively big, though. We could get around that by
345 * making one that's the number of connected servers or something...
346 * or perhaps by adding a special flag to the servers we've sent a
347 * message to, and then a final loop through the connected servers
348 * to delete the flag. -Kev
350 void sendcmdto_common_channels_butone(struct Client *from, const char *cmd,
351 const char *tok, struct Client *one,
352 const char *pattern, ...)
356 struct Membership *chan;
357 struct Membership *member;
360 assert(0 != cli_from(from));
361 assert(0 != pattern);
362 assert(!IsServer(from) && !IsMe(from));
364 vd.vd_format = pattern; /* set up the struct VarData for %v */
366 va_start(vd.vd_args, pattern);
368 /* build the buffer */
369 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
373 if (-1 < cli_fd(cli_from(from)))
374 sentalong[cli_fd(cli_from(from))] = sentalong_marker;
376 * loop through from's channels, and the members on their channels
378 for (chan = cli_user(from)->channel; chan; chan = chan->next_channel) {
381 for (member = chan->channel->members; member;
382 member = member->next_member)
383 if (MyConnect(member->user) && -1 < cli_fd(cli_from(member->user)) &&
384 member->user != one &&
385 sentalong[cli_fd(cli_from(member->user))] != sentalong_marker) {
386 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
387 send_buffer(member->user, mb, 0);
391 if (MyConnect(from) && from != one)
392 send_buffer(from, mb, 0);
398 * Send a (prefixed) command to all local users on the channel specified
399 * by <to>; <tok> is ignored by this function
401 * Update: don't send to 'one', if any. --Vampire
403 void sendcmdto_channel_butserv_butone(struct Client *from, const char *cmd,
404 const char *tok, struct Channel *to,
405 struct Client *one, const char *pattern,
410 struct Membership *member;
412 vd.vd_format = pattern; /* set up the struct VarData for %v */
413 va_start(vd.vd_args, pattern);
415 /* build the buffer */
416 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
419 /* send the buffer to each local channel member */
420 for (member = to->members; member; member = member->next_member) {
421 if (MyConnect(member->user) && member->user != one && !IsZombie(member))
422 send_buffer(member->user, mb, 0);
429 * Send a (prefixed) command to all users on this channel, including
430 * remote users; users to skip may be specified by setting appropriate
431 * flags in the <skip> argument. <one> will also be skipped.
433 /* XXX sentalong_marker used XXX
435 * We can drop sentalong_marker from this function by adding a field to
436 * channels and to connections; what we do is make a user's connection
437 * a "member" of the channel by adding it to the new list, and we use
438 * the struct Membership status as a reference count. Then, to implement
439 * this function, we just walk the list of connections. Unfortunately,
440 * this doesn't account for sending only to channel ops, or for not
441 * sending to +d users; we could account for that by splitting those
442 * counts out, but that would imply adding two more fields (at least) to
443 * the struct Membership... -Kev
445 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
446 const char *tok, struct Channel *to,
447 struct Client *one, unsigned int skip,
448 const char *pattern, ...)
450 struct Membership *member;
452 struct MsgBuf *user_mb;
453 struct MsgBuf *serv_mb;
455 vd.vd_format = pattern;
457 /* Build buffer to send to users */
458 va_start(vd.vd_args, pattern);
459 user_mb = msgq_make(0, skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v",
460 from, skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
463 /* Build buffer to send to servers */
464 va_start(vd.vd_args, pattern);
465 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
468 /* send buffer along! */
470 for (member = to->members; member; member = member->next_member) {
471 /* skip one, zombies, and deaf users... */
472 if (cli_from(member->user) == one || IsZombie(member) ||
473 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
474 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
475 (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
476 cli_fd(cli_from(member->user)) < 0 ||
477 sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
479 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
481 if (MyConnect(member->user)) /* pick right buffer to send */
482 send_buffer(member->user, user_mb, 0);
484 send_buffer(member->user, serv_mb, 0);
492 * Send a (prefixed) command to all users except <one> that have
495 void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
496 const char *pattern, ...)
506 vd.vd_format = pattern;
508 /* Build buffer to send to users */
509 va_start(vd.vd_args, pattern);
526 mb = msgq_make(0, "%:#C " MSG_WALLOPS " :%s%v", from, prefix,&vd);
529 /* send buffer along! */
530 for (i = 0; i <= HighestFd; i++) {
531 if (!(cptr = LocalClientArray[i]) ||
532 (cli_fd(cli_from(cptr)) < 0) ||
533 (type == WALL_DESYNCH && !(cli_flags(cptr) & FLAGS_DEBUG)) ||
534 #ifdef HEAD_IN_SAND_WALLOPS
535 (type == WALL_WALLOPS && (!(cli_flags(cptr) & FLAGS_WALLOP) ||
538 (type == WALL_WALLOPS && !(cli_flags(cptr) & FLAGS_WALLOP)) ||
540 (type == WALL_WALLUSERS && !(cli_flags(cptr) & FLAGS_WALLOP)))
541 continue; /* skip it */
542 send_buffer(cptr, mb, 1);
547 /* Build buffer to send to servers */
548 va_start(vd.vd_args, pattern);
549 mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
552 /* send buffer along! */
553 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
554 if (one && lp->value.cptr == cli_from(one))
556 send_buffer(lp->value.cptr, mb, 1);
563 * Send a (prefixed) command to all users who match <to>, under control
566 /* XXX sentalong_marker used XXX
568 * This is also a difficult one to solve. The basic approach would be
569 * to walk the client list of each connected server until we find a
570 * match--but then, we also have to walk the client list of all the
571 * servers behind that one. We could implement this recursively--or we
572 * could add (yet another) field to the connection struct that would be
573 * a linked list of clients introduced through that link, and just walk
574 * that, making this into an iterative implementation. Unfortunately,
575 * we probably would not be able to use tail recursion for the recursive
576 * solution, so a deep network could exhaust our stack space; therefore
577 * I favor the extra linked list, even though that increases the
578 * complexity of the database. -Kev
580 void sendcmdto_match_butone(struct Client *from, const char *cmd,
581 const char *tok, const char *to,
582 struct Client *one, unsigned int who,
583 const char *pattern, ...)
587 struct MsgBuf *user_mb;
588 struct MsgBuf *serv_mb;
590 vd.vd_format = pattern;
592 /* Build buffer to send to users */
593 va_start(vd.vd_args, pattern);
594 user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
597 /* Build buffer to send to servers */
598 va_start(vd.vd_args, pattern);
599 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
602 /* send buffer along */
604 for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
605 if (!IsRegistered(cptr) || cli_from(cptr) == one || IsServer(cptr) ||
606 IsMe(cptr) || !match_it(from, cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
607 sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
608 continue; /* skip it */
609 sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
611 if (MyConnect(cptr)) /* send right buffer */
612 send_buffer(cptr, user_mb, 0);
614 send_buffer(cptr, serv_mb, 0);
622 * Send a server notice to all users subscribing to the indicated <mask>
625 void sendto_opmask_butone(struct Client *one, unsigned int mask,
626 const char *pattern, ...)
630 va_start(vl, pattern);
631 vsendto_opmask_butone(one, mask, pattern, vl);
636 * Send a server notice to all users subscribing to the indicated <mask>
637 * except for <one> - Ratelimited 1 / 30sec
639 void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
640 time_t *rate, const char *pattern, ...)
644 if ((CurrentTime - *rate) < 30)
649 va_start(vl, pattern);
650 vsendto_opmask_butone(one, mask, pattern, vl);
657 * Same as above, except called with a variable argument list
659 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
660 const char *pattern, va_list vl)
664 int i = 0; /* so that 1 points to opsarray[0] */
665 struct SLink *opslist;
670 if (!(opslist = opsarray[i]))
674 * build string; I don't want to bother with client nicknames, so I hope
677 vd.vd_format = pattern;
679 mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
682 for (; opslist; opslist = opslist->next)
683 send_buffer(opslist->value.cptr, mb, 0);