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.
21 * @brief Send messages to certain targets.
31 #include "ircd_features.h"
32 #include "ircd_snprintf.h"
33 #include "ircd_string.h"
51 /** Marker values for last message sent to a particular connection. */
52 static int sentalong[MAXCONNECTIONS];
53 /** Last used marker value. */
54 static int sentalong_marker;
55 /** Array of users with the corresponding server notice mask bit set. */
56 struct SLink *opsarray[32]; /* don't use highest bit unless you change
57 atoi to strtoul in sendto_op_mask() */
58 /** Linked list of all connections with data queued to send. */
59 static struct Connection *send_queues;
64 * An error has been detected. The link *must* be closed,
65 * but *cannot* call ExitClient (m_bye) from here.
66 * Instead, mark it with FLAG_DEADSOCKET. This should
67 * generate ExitClient from the main loop.
69 * If 'notice' is not NULL, it is assumed to be a format
70 * for a message to local opers. It can contain only one
71 * '%s', which will be replaced by the sockhost field of
74 * Also, the notice is skipped for "uninteresting" cases,
75 * like Persons and yet unknown connections...
77 /** Mark a client as dead, even if they are not the current message source.
78 * This is done by setting the DEADSOCKET flag on the user and letting the
79 * main loop perform the actual exit logic.
80 * @param[in,out] to Client being killed.
81 * @param[in] notice Message for local opers.
83 static void dead_link(struct Client *to, char *notice)
85 SetFlag(to, FLAG_DEADSOCKET);
87 * If because of BUFFERPOOL problem then clean dbuf's now so that
88 * notices don't hurt operators below.
90 DBufClear(&(cli_recvQ(to)));
91 MsgQClear(&(cli_sendQ(to)));
92 client_drop_sendq(cli_connect(to));
95 * Keep a copy of the last comment, for later use...
97 ircd_strncpy(cli_info(to), notice, REALLEN);
99 if (!IsUser(to) && !IsUnknown(to) && !HasFlag(to, FLAG_CLOSING))
100 sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
101 Debug((DEBUG_ERROR, cli_info(to)));
104 /** Test whether we can send to a client.
105 * @param[in] to Client we want to send to.
106 * @return Non-zero if we can send to the client.
108 static int can_send(struct Client* to)
111 return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
114 /** Close the connection with the highest sendq.
115 * This should be called when we need to free buffer memory.
116 * @param[in] servers_too If non-zero, consider killing servers, too.
119 kill_highest_sendq(int servers_too)
122 unsigned int highest_sendq = 0;
123 struct Client *highest_client = 0;
125 for (i = HighestFd; i >= 0; i--)
127 if (!LocalClientArray[i] || (!servers_too && cli_serv(LocalClientArray[i])))
128 continue; /* skip servers */
130 /* If this sendq is higher than one we last saw, remember it */
131 if (MsgQLength(&(cli_sendQ(LocalClientArray[i]))) > highest_sendq)
133 highest_client = LocalClientArray[i];
134 highest_sendq = MsgQLength(&(cli_sendQ(highest_client)));
139 dead_link(highest_client, "Buffer allocation error");
145 * Used to empty all output buffers for all connections. Should only
146 * be called once per scan of connections. There should be a select in
147 * here perhaps but that means either forcing a timeout or doing a poll.
148 * When flushing, all we do is empty the obuffer array for each local
149 * client and try to send it. if we cant send it, it goes into the sendQ
152 /** Flush data queued for one or all connections.
153 * @param[in] cptr Client to flush (if NULL, do all).
155 void flush_connections(struct Client* cptr)
161 struct Connection* con;
162 for (con = send_queues; con; con = con_next(con)) {
163 assert(0 < MsgQLength(&(con_sendQ(con))));
164 send_queued(con_client(con));
172 * This function is called from the main select-loop (or whatever)
173 * when there is a chance that some output would be possible. This
174 * attempts to empty the send queue as far as possible...
176 /** Attempt to send data queued for a client.
177 * @param[in] to Client to send data to.
179 void send_queued(struct Client *to)
182 assert(0 != cli_local(to));
184 if (IsBlocked(to) || !can_send(to))
185 return; /* Don't bother */
187 while (MsgQLength(&(cli_sendQ(to))) > 0) {
190 if ((len = deliver_it(to, &(cli_sendQ(to))))) {
191 msgq_delete(&(cli_sendQ(to)), len);
192 cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
201 sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
208 /* Ok, sendq is now empty... */
209 client_drop_sendq(cli_connect(to));
213 /** Try to send a buffer to a client, queueing it if needed.
214 * @param[in,out] to Client to send message to.
215 * @param[in] buf Message to send.
216 * @param[in] prio If non-zero, send as high priority.
218 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
228 * This socket has already been marked as dead
232 if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
234 sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
235 "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
237 dead_link(to, "Max sendQ exceeded");
241 Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
243 msgq_add(&(cli_sendQ(to)), buf, prio);
244 client_add_sendq(cli_connect(to), &send_queues);
248 * Update statistics. The following is slightly incorrect
249 * because it counts messages even if queued, but bytes
250 * only really sent. Queued bytes get updated in SendQueued.
255 * This little bit is to stop the sendQ from growing too large when
256 * there is no need for it to. Thus we call send_queued() every time
257 * 2k has been added to the queue since the last non-fatal write.
258 * Also stops us from deliberately building a large sendQ and then
259 * trying to flood that link with data (possible during the net
260 * relinking done by servers with a large load).
262 if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
267 * Send a msg to all ppl on servers/hosts that match a specified mask
268 * (used for enhanced PRIVMSGs)
270 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
273 /** Check whether a client matches a target mask.
274 * @param[in] from Client trying to send a message (ignored).
275 * @param[in] one Client being considered as a target.
276 * @param[in] mask Mask for matching against.
277 * @param[in] what Type of match (either MATCH_HOST or MATCH_SERVER).
278 * @return Non-zero if \a one matches, zero if not.
280 static int match_it(struct Client *from, struct Client *one, const char *mask, int what)
285 return (match(mask, cli_user(one)->host) == 0 ||
286 (HasHiddenHost(one) && match(mask, cli_user(one)->realhost) == 0));
289 return (match(mask, cli_name(cli_user(one)->server)) == 0);
293 /** Send an unprefixed line to a client.
294 * @param[in] to Client receiving message.
295 * @param[in] pattern Format string of message.
297 void sendrawto_one(struct Client *to, const char *pattern, ...)
302 va_start(vl, pattern);
303 mb = msgq_vmake(to, pattern, vl);
306 send_buffer(to, mb, 0);
311 /** Send a (prefixed) command to a single client.
312 * @param[in] from Client sending the command.
313 * @param[in] cmd Long name of command (used if \a to is a user).
314 * @param[in] tok Short name of command (used if \a to is a server).
315 * @param[in] to Destination of command.
316 * @param[in] pattern Format string for command arguments.
318 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
319 struct Client *to, const char *pattern, ...)
326 vd.vd_format = pattern; /* set up the struct VarData for %v */
327 va_start(vd.vd_args, pattern);
329 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
334 send_buffer(to, mb, 0);
340 * Send a (prefixed) command to a single client in the priority queue.
341 * @param[in] from Client sending the command.
342 * @param[in] cmd Long name of command (used if \a to is a user).
343 * @param[in] tok Short name of command (used if \a to is a server).
344 * @param[in] to Destination of command.
345 * @param[in] pattern Format string for command arguments.
347 void sendcmdto_prio_one(struct Client *from, const char *cmd, const char *tok,
348 struct Client *to, const char *pattern, ...)
355 vd.vd_format = pattern; /* set up the struct VarData for %v */
356 va_start(vd.vd_args, pattern);
358 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
363 send_buffer(to, mb, 1);
369 * Send a (prefixed) command to all servers but one.
370 * @param[in] from Client sending the command.
371 * @param[in] cmd Long name of command (ignored).
372 * @param[in] tok Short name of command.
373 * @param[in] one Client direction to skip (or NULL).
374 * @param[in] pattern Format string for command arguments.
376 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
377 const char *tok, struct Client *one,
378 const char *pattern, ...)
384 vd.vd_format = pattern; /* set up the struct VarData for %v */
385 va_start(vd.vd_args, pattern);
388 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
391 /* send it to our downlinks */
392 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
393 if (one && lp->value.cptr == cli_from(one))
395 send_buffer(lp->value.cptr, mb, 0);
401 /* XXX sentalong_marker used XXX
403 * There is not an easy way to revoke the need for sentalong_marker
404 * from this function. Thoughts and ideas would be welcome... -Kev
406 * One possibility would be to internalize the sentalong array; that
407 * could be prohibitively big, though. We could get around that by
408 * making one that's the number of connected servers or something...
409 * or perhaps by adding a special flag to the servers we've sent a
410 * message to, and then a final loop through the connected servers
411 * to delete the flag. -Kev
413 /** Send a (prefixed) command to all channels that \a from is on.
414 * @param[in] from Client originating the command.
415 * @param[in] cmd Long name of command.
416 * @param[in] tok Short name of command.
417 * @param[in] one Client direction to skip (or NULL).
418 * @param[in] pattern Format string for command arguments.
420 void sendcmdto_common_channels_butone(struct Client *from, const char *cmd,
421 const char *tok, struct Client *one,
422 const char *pattern, ...)
426 struct Membership *chan;
427 struct Membership *member;
430 assert(0 != cli_from(from));
431 assert(0 != pattern);
432 assert(!IsServer(from) && !IsMe(from));
434 vd.vd_format = pattern; /* set up the struct VarData for %v */
436 va_start(vd.vd_args, pattern);
438 /* build the buffer */
439 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
443 if (-1 < cli_fd(cli_from(from)))
444 sentalong[cli_fd(cli_from(from))] = sentalong_marker;
446 * loop through from's channels, and the members on their channels
448 for (chan = cli_user(from)->channel; chan; chan = chan->next_channel) {
449 if (IsZombie(chan) || IsDelayedJoin(chan))
451 for (member = chan->channel->members; member;
452 member = member->next_member)
453 if (MyConnect(member->user) && -1 < cli_fd(cli_from(member->user)) &&
454 member->user != one &&
455 sentalong[cli_fd(cli_from(member->user))] != sentalong_marker) {
456 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
457 send_buffer(member->user, mb, 0);
461 if (MyConnect(from) && from != one)
462 send_buffer(from, mb, 0);
467 /** Send a (prefixed) command to all local users on a channel.
468 * @param[in] from Client originating the command.
469 * @param[in] cmd Long name of command.
470 * @param[in] tok Short name of command (ignored).
471 * @param[in] to Destination channel.
472 * @param[in] one Client direction to skip (or NULL).
473 * @param[in] skip Bitmask of SKIP_DEAF, SKIP_NONOPS, SKIP_NONVOICES indicating which clients to skip.
474 * @param[in] pattern Format string for command arguments.
476 void sendcmdto_channel_butserv_butone(struct Client *from, const char *cmd,
477 const char *tok, struct Channel *to,
478 struct Client *one, unsigned int skip,
479 const char *pattern, ...)
483 struct Membership *member;
485 vd.vd_format = pattern; /* set up the struct VarData for %v */
486 va_start(vd.vd_args, pattern);
488 /* build the buffer */
489 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
492 /* send the buffer to each local channel member */
493 for (member = to->members; member; member = member->next_member) {
494 if (!MyConnect(member->user)
495 || member->user == one
497 || (skip & SKIP_DEAF && IsDeaf(member->user))
498 || (skip & SKIP_NONOPS && !IsChanOp(member))
499 || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
501 send_buffer(member->user, mb, 0);
508 * Send a (prefixed) command to all servers with users on the channel
509 * specified by <to>; <cmd> and <skip> are ignored by this function.
511 * XXX sentalong_marker used XXX
513 /** Send a (prefixed) command to all servers with users on \a to.
514 * @param[in] from Client originating the command.
515 * @param[in] cmd Long name of command (ignored).
516 * @param[in] tok Short name of command.
517 * @param[in] to Destination channel.
518 * @param[in] one Client direction to skip (or NULL).
519 * @param[in] skip Ignored field.
520 * @param[in] pattern Format string for command arguments.
522 void sendcmdto_channel_servers_butone(struct Client *from, const char *cmd,
523 const char *tok, struct Channel *to,
524 struct Client *one, unsigned int skip,
525 const char *pattern, ...)
528 struct MsgBuf *serv_mb;
529 struct Membership *member;
531 /* build the buffer */
532 vd.vd_format = pattern;
533 va_start(vd.vd_args, pattern);
534 serv_mb = msgq_make(&me, "%:#C %s %v", from, tok, &vd);
537 /* send the buffer to each server */
539 for (member = to->members; member; member = member->next_member) {
540 if (cli_from(member->user) == one
541 || MyConnect(member->user)
543 || cli_fd(cli_from(member->user)) < 0
544 || sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
546 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
547 send_buffer(member->user, serv_mb, 0);
554 * Send a (prefixed) command to all users on this channel, including
555 * remote users; users to skip may be specified by setting appropriate
556 * flags in the <skip> argument. <one> will also be skipped.
558 /* XXX sentalong_marker used XXX
560 * We can drop sentalong_marker from this function by adding a field to
561 * channels and to connections; what we do is make a user's connection
562 * a "member" of the channel by adding it to the new list, and we use
563 * the struct Membership status as a reference count. Then, to implement
564 * this function, we just walk the list of connections. Unfortunately,
565 * this doesn't account for sending only to channel ops, or for not
566 * sending to +d users; we could account for that by splitting those
567 * counts out, but that would imply adding two more fields (at least) to
568 * the struct Membership... -Kev
570 /** Send a (prefixed) command to all users on this channel, except for
571 * \a one and those matching \a skip.
572 * @param[in] from Client originating the command.
573 * @param[in] cmd Long name of command.
574 * @param[in] tok Short name of command.
575 * @param[in] to Destination channel.
576 * @param[in] one Client direction to skip (or NULL).
577 * @param[in] skip Bitmask of SKIP_NONOPS, SKIP_NONVOICES, SKIP_DEAF, SKIP_BURST.
578 * @param[in] pattern Format string for command arguments.
580 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
581 const char *tok, struct Channel *to,
582 struct Client *one, unsigned int skip,
583 const char *pattern, ...)
585 struct Membership *member;
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, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? "%:#C %s @%v" : "%:#C %s %v",
595 from, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? MSG_NOTICE : cmd, &vd);
598 /* Build buffer to send to servers */
599 va_start(vd.vd_args, pattern);
600 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
603 /* send buffer along! */
605 for (member = to->members; member; member = member->next_member) {
606 /* skip one, zombies, and deaf users... */
607 if (cli_from(member->user) == one || IsZombie(member) ||
608 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
609 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
610 (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)) ||
611 (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
612 cli_fd(cli_from(member->user)) < 0 ||
613 sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
615 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
617 if (MyConnect(member->user)) /* pick right buffer to send */
618 send_buffer(member->user, user_mb, 0);
620 send_buffer(member->user, serv_mb, 0);
627 /** Send a (prefixed) WALL of type \a type to all users except \a one.
628 * @param[in] from Source of the command.
629 * @param[in] type One of WALL_DESYNCH, WALL_WALLOPS or WALL_WALLUSERS.
630 * @param[in] one Client direction to skip (or NULL).
631 * @param[in] pattern Format string for command arguments.
633 void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
634 const char *pattern, ...)
644 vd.vd_format = pattern;
646 /* Build buffer to send to users */
647 va_start(vd.vd_args, pattern);
664 mb = msgq_make(0, "%:#C " MSG_WALLOPS " :%s%v", from, prefix,&vd);
667 /* send buffer along! */
668 for (i = 0; i <= HighestFd; i++)
670 if (!(cptr = LocalClientArray[i]) ||
671 (cli_fd(cli_from(cptr)) < 0) ||
672 (type == WALL_DESYNCH && !HasFlag(cptr, FLAG_DEBUG)) ||
673 (type == WALL_WALLOPS &&
674 (!HasFlag(cptr, FLAG_WALLOP) || (feature_bool(FEAT_HIS_WALLOPS) &&
675 !IsAnOper(cptr)))) ||
676 (type == WALL_WALLUSERS && !HasFlag(cptr, FLAG_WALLOP)))
677 continue; /* skip it */
678 send_buffer(cptr, mb, 1);
683 /* Build buffer to send to servers */
684 va_start(vd.vd_args, pattern);
685 mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
688 /* send buffer along! */
689 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
690 if (one && lp->value.cptr == cli_from(one))
692 send_buffer(lp->value.cptr, mb, 1);
699 * Send a (prefixed) command to all users who match <to>, under control
702 /* XXX sentalong_marker used XXX
704 * This is also a difficult one to solve. The basic approach would be
705 * to walk the client list of each connected server until we find a
706 * match--but then, we also have to walk the client list of all the
707 * servers behind that one. We could implement this recursively--or we
708 * could add (yet another) field to the connection struct that would be
709 * a linked list of clients introduced through that link, and just walk
710 * that, making this into an iterative implementation. Unfortunately,
711 * we probably would not be able to use tail recursion for the recursive
712 * solution, so a deep network could exhaust our stack space; therefore
713 * I favor the extra linked list, even though that increases the
714 * complexity of the database. -Kev
716 /** Send a (prefixed) command to all users matching \a to as \a who.
717 * @param[in] from Source of the command.
718 * @param[in] cmd Long name of command.
719 * @param[in] tok Short name of command.
720 * @param[in] to Destination host/server mask.
721 * @param[in] one Client direction to skip (or NULL).
722 * @param[in] who Type of match for \a to (either MATCH_HOST or MATCH_SERVER).
723 * @param[in] pattern Format string for command arguments.
725 void sendcmdto_match_butone(struct Client *from, const char *cmd,
726 const char *tok, const char *to,
727 struct Client *one, unsigned int who,
728 const char *pattern, ...)
732 struct MsgBuf *user_mb;
733 struct MsgBuf *serv_mb;
735 vd.vd_format = pattern;
737 /* Build buffer to send to users */
738 va_start(vd.vd_args, pattern);
739 user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
742 /* Build buffer to send to servers */
743 va_start(vd.vd_args, pattern);
744 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
747 /* send buffer along */
749 for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
750 if (!IsRegistered(cptr) || cli_from(cptr) == one || IsServer(cptr) ||
751 IsMe(cptr) || !match_it(from, cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
752 sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
753 continue; /* skip it */
754 sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
756 if (MyConnect(cptr)) /* send right buffer */
757 send_buffer(cptr, user_mb, 0);
759 send_buffer(cptr, serv_mb, 0);
766 /** Send a server notice to all users subscribing to the indicated \a
767 * mask except for \a one.
768 * @param[in] one Client direction to skip (or NULL).
769 * @param[in] mask One of the SNO_* constants.
770 * @param[in] pattern Format string for server notice.
772 void sendto_opmask_butone(struct Client *one, unsigned int mask,
773 const char *pattern, ...)
777 va_start(vl, pattern);
778 vsendto_opmask_butone(one, mask, pattern, vl);
782 /** Send a server notice to all users subscribing to the indicated \a
783 * mask except for \a one, rate-limited to once per 30 seconds.
784 * @param[in] one Client direction to skip (or NULL).
785 * @param[in] mask One of the SNO_* constants.
786 * @param[in,out] rate Pointer to the last time the message was sent.
787 * @param[in] pattern Format string for server notice.
789 void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
790 time_t *rate, const char *pattern, ...)
794 if ((CurrentTime - *rate) < 30)
799 va_start(vl, pattern);
800 vsendto_opmask_butone(one, mask, pattern, vl);
805 /** Send a server notice to all users subscribing to the indicated \a
806 * mask except for \a one.
807 * @param[in] one Client direction to skip (or NULL).
808 * @param[in] mask One of the SNO_* constants.
809 * @param[in] pattern Format string for server notice.
810 * @param[in] vl Argument list for format string.
812 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
813 const char *pattern, va_list vl)
817 int i = 0; /* so that 1 points to opsarray[0] */
818 struct SLink *opslist;
823 if (!(opslist = opsarray[i]))
827 * build string; I don't want to bother with client nicknames, so I hope
830 vd.vd_format = pattern;
831 va_copy(vd.vd_args, vl);
832 mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
835 for (; opslist; opslist = opslist->next)
836 if (opslist->value.cptr != one)
837 send_buffer(opslist->value.cptr, mb, 0);