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"
50 /** Last used marker value. */
51 static int sentalong_marker;
52 /** Array of users with the corresponding server notice mask bit set. */
53 struct SLink *opsarray[32]; /* don't use highest bit unless you change
54 atoi to strtoul in sendto_op_mask() */
55 /** Linked list of all connections with data queued to send. */
56 static struct Connection *send_queues;
61 * An error has been detected. The link *must* be closed,
62 * but *cannot* call ExitClient (m_bye) from here.
63 * Instead, mark it with FLAG_DEADSOCKET. This should
64 * generate ExitClient from the main loop.
66 * If 'notice' is not NULL, it is assumed to be a format
67 * for a message to local opers. It can contain only one
68 * '%s', which will be replaced by the sockhost field of
71 * Also, the notice is skipped for "uninteresting" cases,
72 * like Persons and yet unknown connections...
74 /** Mark a client as dead, even if they are not the current message source.
75 * This is done by setting the DEADSOCKET flag on the user and letting the
76 * main loop perform the actual exit logic.
77 * @param[in,out] to Client being killed.
78 * @param[in] notice Message for local opers.
80 static void dead_link(struct Client *to, char *notice)
82 SetFlag(to, FLAG_DEADSOCKET);
84 * If because of BUFFERPOOL problem then clean dbuf's now so that
85 * notices don't hurt operators below.
87 DBufClear(&(cli_recvQ(to)));
88 MsgQClear(&(cli_sendQ(to)));
89 client_drop_sendq(cli_connect(to));
92 * Keep a copy of the last comment, for later use...
94 ircd_strncpy(cli_info(to), notice, REALLEN);
96 if (!IsUser(to) && !IsUnknown(to) && !HasFlag(to, FLAG_CLOSING))
97 sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
98 Debug((DEBUG_ERROR, cli_info(to)));
101 /** Test whether we can send to a client.
102 * @param[in] to Client we want to send to.
103 * @return Non-zero if we can send to the client.
105 static int can_send(struct Client* to)
108 return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
111 /** Close the connection with the highest sendq.
112 * This should be called when we need to free buffer memory.
113 * @param[in] servers_too If non-zero, consider killing servers, too.
116 kill_highest_sendq(int servers_too)
119 unsigned int highest_sendq = 0;
120 struct Client *highest_client = 0;
122 for (i = HighestFd; i >= 0; i--)
124 if (!LocalClientArray[i] || (!servers_too && cli_serv(LocalClientArray[i])))
125 continue; /* skip servers */
127 /* If this sendq is higher than one we last saw, remember it */
128 if (MsgQLength(&(cli_sendQ(LocalClientArray[i]))) > highest_sendq)
130 highest_client = LocalClientArray[i];
131 highest_sendq = MsgQLength(&(cli_sendQ(highest_client)));
136 dead_link(highest_client, "Buffer allocation error");
142 * Used to empty all output buffers for all connections. Should only
143 * be called once per scan of connections. There should be a select in
144 * here perhaps but that means either forcing a timeout or doing a poll.
145 * When flushing, all we do is empty the obuffer array for each local
146 * client and try to send it. if we cant send it, it goes into the sendQ
149 /** Flush data queued for one or all connections.
150 * @param[in] cptr Client to flush (if NULL, do all).
152 void flush_connections(struct Client* cptr)
158 struct Connection* con;
159 for (con = send_queues; con; con = con_next(con)) {
160 assert(0 < MsgQLength(&(con_sendQ(con))));
161 send_queued(con_client(con));
169 * This function is called from the main select-loop (or whatever)
170 * when there is a chance that some output would be possible. This
171 * attempts to empty the send queue as far as possible...
173 /** Attempt to send data queued for a client.
174 * @param[in] to Client to send data to.
176 void send_queued(struct Client *to)
179 assert(0 != cli_local(to));
181 if (IsBlocked(to) || !can_send(to))
182 return; /* Don't bother */
184 while (MsgQLength(&(cli_sendQ(to))) > 0) {
187 if ((len = deliver_it(to, &(cli_sendQ(to))))) {
188 msgq_delete(&(cli_sendQ(to)), len);
189 cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
198 sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
205 /* Ok, sendq is now empty... */
206 client_drop_sendq(cli_connect(to));
210 /** Try to send a buffer to a client, queueing it if needed.
211 * @param[in,out] to Client to send message to.
212 * @param[in] buf Message to send.
213 * @param[in] prio If non-zero, send as high priority.
215 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
225 * This socket has already been marked as dead
229 if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
231 sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
232 "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
234 dead_link(to, "Max sendQ exceeded");
238 Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
240 msgq_add(&(cli_sendQ(to)), buf, prio);
241 client_add_sendq(cli_connect(to), &send_queues);
245 * Update statistics. The following is slightly incorrect
246 * because it counts messages even if queued, but bytes
247 * only really sent. Queued bytes get updated in SendQueued.
252 * This little bit is to stop the sendQ from growing too large when
253 * there is no need for it to. Thus we call send_queued() every time
254 * 2k has been added to the queue since the last non-fatal write.
255 * Also stops us from deliberately building a large sendQ and then
256 * trying to flood that link with data (possible during the net
257 * relinking done by servers with a large load).
259 if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
264 * Send a msg to all ppl on servers/hosts that match a specified mask
265 * (used for enhanced PRIVMSGs)
267 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
270 /** Check whether a client matches a target mask.
271 * @param[in] from Client trying to send a message (ignored).
272 * @param[in] one Client being considered as a target.
273 * @param[in] mask Mask for matching against.
274 * @param[in] what Type of match (either MATCH_HOST or MATCH_SERVER).
275 * @return Non-zero if \a one matches, zero if not.
277 static int match_it(struct Client *from, struct Client *one, const char *mask, int what)
282 return (match(mask, cli_user(one)->host) == 0 ||
283 (HasHiddenHost(one) && match(mask, cli_user(one)->realhost) == 0));
286 return (match(mask, cli_name(cli_user(one)->server)) == 0);
290 /** Send an unprefixed line to a client.
291 * @param[in] to Client receiving message.
292 * @param[in] pattern Format string of message.
294 void sendrawto_one(struct Client *to, const char *pattern, ...)
299 va_start(vl, pattern);
300 mb = msgq_vmake(to, pattern, vl);
303 send_buffer(to, mb, 0);
308 /** Send a (prefixed) command to a single client.
309 * @param[in] from Client sending the command.
310 * @param[in] cmd Long name of command (used if \a to is a user).
311 * @param[in] tok Short name of command (used if \a to is a server).
312 * @param[in] to Destination of command.
313 * @param[in] pattern Format string for command arguments.
315 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
316 struct Client *to, const char *pattern, ...)
323 vd.vd_format = pattern; /* set up the struct VarData for %v */
324 va_start(vd.vd_args, pattern);
326 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
331 send_buffer(to, mb, 0);
337 * Send a (prefixed) command to a single client in the priority queue.
338 * @param[in] from Client sending the command.
339 * @param[in] cmd Long name of command (used if \a to is a user).
340 * @param[in] tok Short name of command (used if \a to is a server).
341 * @param[in] to Destination of command.
342 * @param[in] pattern Format string for command arguments.
344 void sendcmdto_prio_one(struct Client *from, const char *cmd, const char *tok,
345 struct Client *to, const char *pattern, ...)
352 vd.vd_format = pattern; /* set up the struct VarData for %v */
353 va_start(vd.vd_args, pattern);
355 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
360 send_buffer(to, mb, 1);
366 * Send a (prefixed) command to all servers but one.
367 * @param[in] from Client sending the command.
368 * @param[in] cmd Long name of command (ignored).
369 * @param[in] tok Short name of command.
370 * @param[in] one Client direction to skip (or NULL).
371 * @param[in] pattern Format string for command arguments.
373 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
374 const char *tok, struct Client *one,
375 const char *pattern, ...)
381 vd.vd_format = pattern; /* set up the struct VarData for %v */
382 va_start(vd.vd_args, pattern);
385 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
388 /* send it to our downlinks */
389 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
390 if (one && lp->value.cptr == cli_from(one))
392 send_buffer(lp->value.cptr, mb, 0);
398 /** Safely increment the sentalong marker.
399 * This increments the sentalong marker. Since new connections will
400 * have con_sentalong() == 0, and to avoid confusion when the counter
401 * wraps, we reset all sentalong markers to zero when the sentalong
403 * @param[in,out] one Client to mark with new sentalong marker (if any).
406 bump_sentalong(struct Client *one)
408 if (!++sentalong_marker)
411 for (ii = 0; ii < HighestFd; ++ii)
412 if (LocalClientArray[ii])
413 cli_sentalong(LocalClientArray[ii]) = 0;
417 cli_sentalong(one) = sentalong_marker;
420 /** Send a (prefixed) command to all channels that \a from is on.
421 * @param[in] from Client originating the command.
422 * @param[in] cmd Long name of command.
423 * @param[in] tok Short name of command.
424 * @param[in] one Client direction to skip (or NULL).
425 * @param[in] pattern Format string for command arguments.
427 void sendcmdto_common_channels_butone(struct Client *from, const char *cmd,
428 const char *tok, struct Client *one,
429 const char *pattern, ...)
433 struct Membership *chan;
434 struct Membership *member;
437 assert(0 != cli_from(from));
438 assert(0 != pattern);
439 assert(!IsServer(from) && !IsMe(from));
441 vd.vd_format = pattern; /* set up the struct VarData for %v */
443 va_start(vd.vd_args, pattern);
445 /* build the buffer */
446 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
449 bump_sentalong(from);
451 * loop through from's channels, and the members on their channels
453 for (chan = cli_user(from)->channel; chan; chan = chan->next_channel) {
454 if (IsZombie(chan) || IsDelayedJoin(chan))
456 for (member = chan->channel->members; member;
457 member = member->next_member)
458 if (MyConnect(member->user)
459 && -1 < cli_fd(cli_from(member->user))
460 && member->user != one
461 && cli_sentalong(member->user) != sentalong_marker) {
462 cli_sentalong(member->user) = sentalong_marker;
463 send_buffer(member->user, mb, 0);
467 if (MyConnect(from) && from != one)
468 send_buffer(from, mb, 0);
473 /** Send a (prefixed) command to all local users on a channel.
474 * @param[in] from Client originating the command.
475 * @param[in] cmd Long name of command.
476 * @param[in] tok Short name of command (ignored).
477 * @param[in] to Destination channel.
478 * @param[in] one Client direction to skip (or NULL).
479 * @param[in] skip Bitmask of SKIP_DEAF, SKIP_NONOPS, SKIP_NONVOICES indicating which clients to skip.
480 * @param[in] pattern Format string for command arguments.
482 void sendcmdto_channel_butserv_butone(struct Client *from, const char *cmd,
483 const char *tok, struct Channel *to,
484 struct Client *one, unsigned int skip,
485 const char *pattern, ...)
489 struct Membership *member;
491 vd.vd_format = pattern; /* set up the struct VarData for %v */
492 va_start(vd.vd_args, pattern);
494 /* build the buffer */
495 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
498 /* send the buffer to each local channel member */
499 for (member = to->members; member; member = member->next_member) {
500 if (!MyConnect(member->user)
501 || member->user == one
503 || (skip & SKIP_DEAF && IsDeaf(member->user))
504 || (skip & SKIP_NONOPS && !IsChanOp(member))
505 || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
507 send_buffer(member->user, mb, 0);
513 /** Send a (prefixed) command to all servers with users on \a to.
514 * Skip \a from and \a one plus those indicated in \a skip.
515 * @param[in] from Client originating the command.
516 * @param[in] cmd Long name of command (ignored).
517 * @param[in] tok Short name of command.
518 * @param[in] to Destination channel.
519 * @param[in] one Client direction to skip (or NULL).
520 * @param[in] skip Bitmask of SKIP_NONOPS and SKIP_NONVOICES indicating which clients to skip.
521 * @param[in] pattern Format string for command arguments.
523 void sendcmdto_channel_servers_butone(struct Client *from, const char *cmd,
524 const char *tok, struct Channel *to,
525 struct Client *one, unsigned int skip,
526 const char *pattern, ...)
529 struct MsgBuf *serv_mb;
530 struct Membership *member;
532 /* build the buffer */
533 vd.vd_format = pattern;
534 va_start(vd.vd_args, pattern);
535 serv_mb = msgq_make(&me, "%:#C %s %v", from, tok, &vd);
538 /* send the buffer to each server */
540 cli_sentalong(from) = sentalong_marker;
541 for (member = to->members; member; member = member->next_member) {
542 if (MyConnect(member->user)
544 || cli_fd(cli_from(member->user)) < 0
545 || cli_sentalong(member->user) == sentalong_marker
546 || (skip & SKIP_NONOPS && !IsChanOp(member))
547 || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
549 cli_sentalong(member->user) = sentalong_marker;
550 send_buffer(member->user, serv_mb, 0);
556 /** Send a (prefixed) command to all users on this channel, except for
557 * \a one and those matching \a skip.
558 * @param[in] from Client originating the command.
559 * @param[in] cmd Long name of command.
560 * @param[in] tok Short name of command.
561 * @param[in] to Destination channel.
562 * @param[in] one Client direction to skip (or NULL).
563 * @param[in] skip Bitmask of SKIP_NONOPS, SKIP_NONVOICES, SKIP_DEAF, SKIP_BURST.
564 * @param[in] pattern Format string for command arguments.
566 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
567 const char *tok, struct Channel *to,
568 struct Client *one, unsigned int skip,
569 const char *pattern, ...)
571 struct Membership *member;
573 struct MsgBuf *user_mb;
574 struct MsgBuf *serv_mb;
576 vd.vd_format = pattern;
578 /* Build buffer to send to users */
579 va_start(vd.vd_args, pattern);
580 user_mb = msgq_make(0, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? "%:#C %s @%v" : "%:#C %s %v",
581 from, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? MSG_NOTICE : cmd, &vd);
584 /* Build buffer to send to servers */
585 va_start(vd.vd_args, pattern);
586 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
589 /* send buffer along! */
591 for (member = to->members; member; member = member->next_member) {
592 /* skip one, zombies, and deaf users... */
593 if (IsZombie(member) ||
594 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
595 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
596 (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)) ||
597 (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
598 cli_fd(cli_from(member->user)) < 0 ||
599 cli_sentalong(member->user) == sentalong_marker)
601 cli_sentalong(member->user) = sentalong_marker;
603 if (MyConnect(member->user)) /* pick right buffer to send */
604 send_buffer(member->user, user_mb, 0);
606 send_buffer(member->user, serv_mb, 0);
613 /** Send a (prefixed) WALL of type \a type to all users except \a one.
614 * @param[in] from Source of the command.
615 * @param[in] type One of WALL_DESYNCH, WALL_WALLOPS or WALL_WALLUSERS.
616 * @param[in] one Client direction to skip (or NULL).
617 * @param[in] pattern Format string for command arguments.
619 void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
620 const char *pattern, ...)
630 vd.vd_format = pattern;
632 /* Build buffer to send to users */
633 va_start(vd.vd_args, pattern);
650 mb = msgq_make(0, "%:#C " MSG_WALLOPS " :%s%v", from, prefix,&vd);
653 /* send buffer along! */
654 for (i = 0; i <= HighestFd; i++)
656 if (!(cptr = LocalClientArray[i]) ||
657 (cli_fd(cli_from(cptr)) < 0) ||
658 (type == WALL_DESYNCH && !HasFlag(cptr, FLAG_DEBUG)) ||
659 (type == WALL_WALLOPS &&
660 (!HasFlag(cptr, FLAG_WALLOP) || (feature_bool(FEAT_HIS_WALLOPS) &&
661 !IsAnOper(cptr)))) ||
662 (type == WALL_WALLUSERS && !HasFlag(cptr, FLAG_WALLOP)))
663 continue; /* skip it */
664 send_buffer(cptr, mb, 1);
669 /* Build buffer to send to servers */
670 va_start(vd.vd_args, pattern);
671 mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
674 /* send buffer along! */
675 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
676 if (one && lp->value.cptr == cli_from(one))
678 send_buffer(lp->value.cptr, mb, 1);
684 /** Send a (prefixed) command to all users matching \a to as \a who.
685 * @param[in] from Source of the command.
686 * @param[in] cmd Long name of command.
687 * @param[in] tok Short name of command.
688 * @param[in] to Destination host/server mask.
689 * @param[in] one Client direction to skip (or NULL).
690 * @param[in] who Type of match for \a to (either MATCH_HOST or MATCH_SERVER).
691 * @param[in] pattern Format string for command arguments.
693 void sendcmdto_match_butone(struct Client *from, const char *cmd,
694 const char *tok, const char *to,
695 struct Client *one, unsigned int who,
696 const char *pattern, ...)
700 struct MsgBuf *user_mb;
701 struct MsgBuf *serv_mb;
703 vd.vd_format = pattern;
705 /* Build buffer to send to users */
706 va_start(vd.vd_args, pattern);
707 user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
710 /* Build buffer to send to servers */
711 va_start(vd.vd_args, pattern);
712 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
715 /* send buffer along */
717 for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
718 if (!IsRegistered(cptr) || IsServer(cptr) ||
719 !match_it(from, cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
720 cli_sentalong(cptr) == sentalong_marker)
721 continue; /* skip it */
722 cli_sentalong(cptr) = sentalong_marker;
724 if (MyConnect(cptr)) /* send right buffer */
725 send_buffer(cptr, user_mb, 0);
727 send_buffer(cptr, serv_mb, 0);
734 /** Send a server notice to all users subscribing to the indicated \a
735 * mask except for \a one.
736 * @param[in] one Client direction to skip (or NULL).
737 * @param[in] mask One of the SNO_* constants.
738 * @param[in] pattern Format string for server notice.
740 void sendto_opmask_butone(struct Client *one, unsigned int mask,
741 const char *pattern, ...)
745 va_start(vl, pattern);
746 vsendto_opmask_butone(one, mask, pattern, vl);
750 /** Send a server notice to all users subscribing to the indicated \a
751 * mask except for \a one, rate-limited to once per 30 seconds.
752 * @param[in] one Client direction to skip (or NULL).
753 * @param[in] mask One of the SNO_* constants.
754 * @param[in,out] rate Pointer to the last time the message was sent.
755 * @param[in] pattern Format string for server notice.
757 void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
758 time_t *rate, const char *pattern, ...)
762 if ((CurrentTime - *rate) < 30)
767 va_start(vl, pattern);
768 vsendto_opmask_butone(one, mask, pattern, vl);
773 /** Send a server notice to all users subscribing to the indicated \a
774 * mask except for \a one.
775 * @param[in] one Client direction to skip (or NULL).
776 * @param[in] mask One of the SNO_* constants.
777 * @param[in] pattern Format string for server notice.
778 * @param[in] vl Argument list for format string.
780 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
781 const char *pattern, va_list vl)
785 int i = 0; /* so that 1 points to opsarray[0] */
786 struct SLink *opslist;
791 if (!(opslist = opsarray[i]))
795 * build string; I don't want to bother with client nicknames, so I hope
798 vd.vd_format = pattern;
799 va_copy(vd.vd_args, vl);
800 mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
803 for (; opslist; opslist = opslist->next)
804 if (opslist->value.cptr != one)
805 send_buffer(opslist->value.cptr, mb, 0);