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.
27 #include "ircd_string.h"
36 #include "sprintf_irc.h"
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() */
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. I 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 to->flags |= FLAGS_DEADSOCKET;
76 * If because of BUFFERPOOL problem then clean dbuf's now so that
77 * notices don't hurt operators below.
79 DBufClear(&to->recvQ);
80 DBufClear(&to->sendQ);
82 /* Keep a copy of the last comment, for later use... */
83 ircd_strncpy(LastDeadComment(to), notice, sizeof(LastDeadComment(to) - 1));
84 LastDeadComment(to)[sizeof(LastDeadComment(to)) - 1] = '\0';
86 if (!IsUser(to) && !IsUnknown(to) && !(to->flags & FLAGS_CLOSING))
87 sendto_ops("%s for %s", LastDeadComment(to), to->name);
88 Debug((DEBUG_ERROR, LastDeadComment(to)));
91 static int can_send(struct Client* to)
94 return (IsDead(to) || IsMe(to) || -1 == to->fd) ? 0 : 1;
100 * Used to empty all output buffers for all connections. Should only
101 * be called once per scan of connections. There should be a select in
102 * here perhaps but that means either forcing a timeout or doing a poll.
103 * When flushing, all we do is empty the obuffer array for each local
104 * client and try to send it. if we cant send it, it goes into the sendQ
107 void flush_connections(struct Client* cptr)
114 for (i = HighestFd; i >= 0; i--) {
115 if ((cptr = LocalClientArray[i]))
122 * flush_sendq_except - run through local client array and flush
123 * the sendq for each client, if the address of the client sendq
124 * is the same as the one specified, it is skipped. This is used
125 * by dbuf_put to try to get some more memory before bailing and
126 * causing the client to be disconnected.
128 void flush_sendq_except(const struct DBuf* one)
132 for (i = HighestFd; i >= 0; i--) {
133 if ( (cptr = LocalClientArray[i]) && one != &cptr->sendQ)
141 * This function is called from the main select-loop (or whatever)
142 * when there is a chance that some output would be possible. This
143 * attempts to empty the send queue as far as possible...
145 void send_queued(struct Client *to)
148 assert(0 != to->local);
150 if (IsBlocked(to) || !can_send(to))
151 return; /* Don't bother */
153 while (DBufLength(&to->sendQ) > 0) {
155 const char* msg = dbuf_map(&to->sendQ, &len);
157 if ((len = deliver_it(to, msg, len))) {
158 dbuf_delete(&to->sendQ, len);
159 to->lastsq = DBufLength(&to->sendQ) / 1024;
165 dead_link(to, "Write error, closing link");
172 * send message to single client
174 void sendto_one(struct Client *to, const char* pattern, ...)
177 va_start(vl, pattern);
178 vsendto_one(to, pattern, vl);
183 void vsendto_one(struct Client *to, const char* pattern, va_list vl)
185 vsprintf_irc(sendbuf, pattern, vl);
190 static void send_to_god(struct Client* to, const char* buf)
192 if (!sdbflag && !IsUser(to)) {
193 char sbuf2[BUFSIZE + 1];
194 unsigned int len = strlen(buf) - 2; /* Remove "\r\n" */
197 len = IRCD_MIN(len, BUFSIZE);
198 ircd_strncpy(sbuf2, buf, len);
204 sendto_ops("SND:%-8.8s(%.4d): \"%s...%s\"",
205 to->name, len, sbuf2, &sbuf2[len - 200]);
208 sendto_ops("SND:%-8.8s(%.4d): \"%s\"", to->name, len, sbuf2);
214 void send_buffer(struct Client* to, char* buf)
225 * This socket has already been marked as dead
229 if (DBufLength(&to->sendQ) > get_sendq(to)) {
231 sendto_ops("Max SendQ limit exceeded for %s: " SIZE_T_FMT " > " SIZE_T_FMT,
232 to->name, DBufLength(&to->sendQ), get_sendq(to));
233 dead_link(to, "Max sendQ exceeded");
237 Debug((DEBUG_SEND, "Sending [%s] to %s", buf, to->name));
240 if (buf[len - 1] != '\n') {
248 if (0 == dbuf_put(&to->sendQ, buf, len)) {
249 dead_link(to, "Buffer allocation error");
254 send_to_god(to, buf);
257 * Update statistics. The following is slightly incorrect
258 * because it counts messages even if queued, but bytes
259 * only really sent. Queued bytes get updated in SendQueued.
264 * This little bit is to stop the sendQ from growing too large when
265 * there is no need for it to. Thus we call send_queued() every time
266 * 2k has been added to the queue since the last non-fatal write.
267 * Also stops us from deliberately building a large sendQ and then
268 * trying to flood that link with data (possible during the net
269 * relinking done by servers with a large load).
271 if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
275 void sendbufto_one(struct Client* to)
277 send_buffer(to, sendbuf);
280 static void vsendto_prefix_one(struct Client *to, struct Client *from,
281 const char* pattern, va_list vl)
283 if (to && from && MyUser(to) && IsUser(from))
285 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
288 struct User *user = from->user;
290 par = va_arg(vl, char *);
291 strcpy(sender, from->name);
297 strcat(sender, user->username);
299 if (*user->host && !MyConnect(from))
302 strcat(sender, user->host);
307 * Flag is used instead of strchr(sender, '@') for speed and
308 * also since username/nick may have had a '@' in them. -avalon
310 if (!flag && MyConnect(from) && *user->host)
313 strcat(sender, from->sockhost);
316 strcpy(&sendbuf[1], sender);
317 /* Assuming 'pattern' always starts with ":%s ..." */
318 vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
321 vsprintf_irc(sendbuf, pattern, vl);
325 void sendto_channel_butone(struct Client *one, struct Client *from, struct Channel *chptr,
326 const char* pattern, ...)
329 struct Membership* member;
330 struct Client *acptr;
333 va_start(vl, pattern);
336 for (member = chptr->members; member; member = member->next_member)
338 acptr = member->user;
339 /* ...was the one I should skip */
340 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
342 if (MyConnect(acptr)) /* (It is always a client) */
343 vsendto_prefix_one(acptr, from, pattern, vl);
344 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
346 sentalong[i] = sentalong_marker;
348 * Don't send channel messages to links that are still eating
349 * the net.burst: -- Run 2/1/1997
351 if (!IsBurstOrBurstAck(acptr->from))
352 vsendto_prefix_one(acptr, from, pattern, vl);
359 void sendmsgto_channel_butone(struct Client *one, struct Client *from,
360 struct Channel *chptr, const char *sender,
361 const char *cmd, const char *chname, const char *msg)
364 * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
365 * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
367 struct Membership* member;
368 struct Client *acptr;
375 * Precalculate the buffers we sent to the clients instead of doing an
376 * expensive sprintf() per member that we send to. We still have to
377 * use strcpy() which is evil.
379 if (IsServer(from)) {
380 sprintf(userbuf,":%s %s %s :%s",
382 ((cmd[0] == 'P') ? MSG_PRIVATE : MSG_NOTICE),
384 sprintf(servbuf,"%s " TOK_PRIVATE " %s :%s",
385 NumServ(from), chname, msg);
387 sprintf(userbuf,":%s!%s@%s %s %s :%s",
388 from->name, from->username, from->user->host,
389 ((cmd[0] == 'P') ? MSG_PRIVATE : MSG_NOTICE),
391 sprintf(servbuf,"%s%s %s %s :%s",
393 ((cmd[0] == 'P') ? TOK_PRIVATE : TOK_NOTICE),
398 for (member = chptr->members; member; member = member->next_member)
400 acptr = member->user;
401 /* ...was the one I should skip */
402 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
404 if (MyConnect(acptr)) { /* (It is always a client) */
406 strcpy(sendbuf,userbuf);
408 sendbufto_one(acptr);
410 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
412 sentalong[i] = sentalong_marker;
413 if (!IsBurstOrBurstAck(acptr->from)) {
415 strcpy(sendbuf,servbuf);
417 sendbufto_one(acptr);
419 } /* of if MyConnect() */
420 } /* of for(members) */
423 void sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
424 const char* pattern, ...)
427 struct Membership* member;
428 struct Client *acptr;
430 va_start(vl, pattern);
432 for (member = chptr->members; member; member = member->next_member)
434 acptr = member->user;
435 /* ...was the one I should skip */
436 if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
438 if (MyConnect(acptr)) /* (It is always a client) */
439 vsendto_prefix_one(acptr, from, pattern, vl);
445 void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
446 const char* pattern, ...)
449 struct Membership* member;
450 struct Client *acptr;
459 va_start(vl, pattern);
462 for (member = chptr->members; member; member = member->next_member)
464 acptr = member->user;
465 if (acptr->from == acptr || /* Skip local clients */
467 Protocol(acptr->from) < 10 || /* Skip P09 links */
469 acptr->from == one || /* ...was the one I should skip */
470 !IsChanOp(member) || /* Skip non chanops */
471 IsZombie(member) || IsDeaf(acptr))
473 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
475 sentalong[i] = sentalong_marker;
476 /* Don't send channel messages to links that are
477 still eating the net.burst: -- Run 2/1/1997 */
478 if (!IsBurstOrBurstAck(acptr->from))
479 vsendto_prefix_one(acptr, from, pattern, vl);
484 /* Send message to all 2.9 servers */
485 /* This is a hack, because it assumes that we know how `vl' is build up */
486 source = va_arg(vl, char *);
487 tp = va_arg(vl, char *); /* Channel */
488 msg = va_arg(vl, char *);
489 for (member = chptr->members; member; member = member->next_member)
491 acptr = member->user;
492 if (acptr->from == acptr || /* Skip local clients */
493 Protocol(acptr->from) > 9 || /* Skip P10 servers */
494 acptr->from == one || /* ...was the one I should skip */
495 !IsChanOp(member) || /* Skip non chanops */
496 IsZombie(member) || IsDeaf(acptr))
498 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
500 sentalong[i] = sentalong_marker;
501 /* Don't send channel messages to links that are
502 still eating the net.burst: -- Run 2/1/1997 */
503 if (!IsBurstOrBurstAck(acptr->from))
505 struct Membership* other_member;
506 struct Client* acptr2;
509 /* Find all chanops in this direction: */
510 for (other_member = chptr->members; other_member; other_member = other_member->next_member)
512 acptr2 = other_member->user;
513 if (acptr2->from == acptr->from && acptr2->from != one &&
514 IsChanOp(other_member) && !IsZombie(other_member) &&
517 int len = strlen(acptr2->name);
518 if (tp + len + 2 > target + sizeof(target))
520 sendto_prefix_one(acptr, from,
521 ":%s NOTICE %s :%s", source, target, msg);
527 strcpy(tp, acptr2->name);
531 sendto_prefix_one(acptr, from,
532 ":%s NOTICE %s :%s", source, target, msg);
543 * sendto_server_butone
545 * Send a message to all connected servers except the client 'one'.
547 void sendto_serv_butone(struct Client *one, const char* pattern, ...)
552 va_start(vl, pattern);
553 vsprintf_irc(sendbuf, pattern, vl);
556 for (lp = me.serv->down; lp; lp = lp->next)
558 if (one && lp->value.cptr == one->from)
560 sendbufto_one(lp->value.cptr);
566 * sendbufto_serv_butone()
568 * Send prepared sendbuf to all connected servers except the client 'one'
569 * -Ghostwolf 18-May-97
571 void sendbufto_serv_butone(struct Client *one)
575 for (lp = me.serv->down; lp; lp = lp->next)
577 if (one && lp->value.cptr == one->from)
579 sendbufto_one(lp->value.cptr);
585 * sendto_common_channels()
587 * Sends a message to all people (inclusing `acptr') on local server
588 * who are in same channel with client `acptr'.
590 void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
593 struct Membership* chan;
594 struct Membership* member;
597 assert(0 != acptr->from);
598 assert(0 != pattern);
600 va_start(vl, pattern);
603 if (-1 < acptr->from->fd)
604 sentalong[acptr->from->fd] = sentalong_marker;
606 * loop through acptr's channels, and the members on their channels
609 for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
610 for (member = chan->channel->members; member; member = member->next_member) {
611 struct Client *cptr = member->user;
613 if (MyConnect(cptr) &&
614 -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
615 sentalong[i] = sentalong_marker;
616 vsendto_prefix_one(cptr, acptr, pattern, vl);
621 if (MyConnect(acptr))
622 vsendto_prefix_one(acptr, acptr, pattern, vl);
628 * sendto_channel_butserv
630 * Send a message to all members of a channel that
631 * are connected to this server.
633 void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
636 struct Membership* member;
637 struct Client *acptr;
639 va_start(vl, pattern);
641 for (member = chptr->members; member; member = member->next_member) {
642 acptr = member->user;
643 if (MyConnect(acptr) && !IsZombie(member))
644 vsendto_prefix_one(acptr, from, pattern, vl);
651 * Send a msg to all ppl on servers/hosts that match a specified mask
652 * (used for enhanced PRIVMSGs)
654 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
657 static int match_it(struct Client *one, const char *mask, int what)
662 return (match(mask, one->user->host) == 0);
665 return (match(mask, one->user->server->name) == 0);
670 * sendto_match_butone
672 * Send to all clients which match the mask in a way defined on 'what';
673 * either by user hostname or user servername.
675 void sendto_match_butone(struct Client *one, struct Client *from,
676 const char *mask, int what, const char* pattern, ...)
680 struct Client *cptr, *acptr;
682 va_start(vl, pattern);
683 for (i = 0; i <= HighestFd; i++)
685 if (!(cptr = LocalClientArray[i]))
686 continue; /* that clients are not mine */
687 if (cptr == one) /* must skip the origin !! */
691 for (acptr = GlobalClientList; acptr; acptr = acptr->next)
692 if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
694 /* a person on that server matches the mask, so we
695 * send *one* msg to that server ...
699 /* ... but only if there *IS* a matching person */
701 /* my client, does he match ? */
702 else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
704 vsendto_prefix_one(cptr, from, pattern, vl);
714 * Send to *local* ops but one.
716 void sendto_lops_butone(struct Client* one, const char* pattern, ...)
720 struct Client** clients = me.serv->client_list;
724 assert(0 != clients);
726 sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
727 va_start(vl, pattern);
728 vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
731 for (i = 0; i <= me.serv->nn_mask; ++i) {
732 if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
733 sprintf_irc(sendbuf, nbuf, cptr->name);
742 * Sends message to the list indicated by the bitmask field.
743 * Don't try to send to more than one list! That is not supported.
746 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
748 static char fmt[1024];
750 int i = 0; /* so that 1 points to opsarray[0] */
751 struct SLink *opslist;
755 if (!(opslist = opsarray[i]))
758 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
761 strcpy(fmt_target, opslist->value.cptr->name);
762 strcat(fmt_target, " :*** Notice -- ");
763 strcat(fmt_target, pattern);
764 vsendto_one(opslist->value.cptr, fmt, vl);
765 opslist = opslist->next;
773 * Send a prepared sendbuf to the list indicated by the bitmask field.
774 * Ghostwolf 16-May-97
776 void sendbufto_op_mask(unsigned int mask)
778 int i = 0; /* so that 1 points to opsarray[0] */
779 struct SLink *opslist;
782 if (!(opslist = opsarray[i]))
786 sendbufto_one(opslist->value.cptr);
787 opslist = opslist->next;
796 * Send to *local* ops only.
798 void vsendto_ops(const char *pattern, va_list vl)
805 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
807 for (i = 0; i <= HighestFd; i++)
808 if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
809 SendServNotice(cptr))
811 strcpy(fmt_target, cptr->name);
812 strcat(fmt_target, " :*** Notice -- ");
813 strcat(fmt_target, pattern);
814 vsendto_one(cptr, fmt, vl);
818 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
821 va_start(vl, pattern);
822 vsendto_op_mask(mask, pattern, vl);
826 void sendto_ops(const char *pattern, ...)
829 va_start(vl, pattern);
830 vsendto_op_mask(SNO_OLDSNO, pattern, vl);
837 * Send message to all operators.
838 * one - client not to send message to
839 * from- client which message is from *NEVER* NULL!!
841 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
847 va_start(vl, pattern);
849 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
851 if (!SendWallops(cptr))
853 i = cptr->from->fd; /* find connection oper is on */
854 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
856 if (cptr->from == one)
857 continue; /* ...was the one I should skip */
858 sentalong[i] = sentalong_marker;
859 vsendto_prefix_one(cptr->from, from, pattern, vl);
867 * sendto_g_serv_butone
869 * Send message to all remote +g users (server links).
871 * one - server not to send message to.
873 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
879 va_start(vl, pattern);
881 vsprintf_irc(sendbuf, pattern, vl);
882 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
884 if (!SendDebug(cptr))
886 i = cptr->from->fd; /* find connection user is on */
887 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
891 sentalong[i] = sentalong_marker;
892 if (cptr->from == one)
904 * to - destination client
905 * from - client which message is from
907 * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
910 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
913 va_start(vl, pattern);
914 vsendto_prefix_one(to, from, pattern, vl);
921 * Send to *local* ops only but NOT +s nonopers.
923 void sendto_realops(const char *pattern, ...)
927 va_start(vl, pattern);
928 vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
935 * Send message to all servers of protocol 'p' and lower.
937 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
941 va_start(vl, pattern);
942 for (lp = me.serv->down; lp; lp = lp->next)
943 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
944 vsendto_one(lp->value.cptr, pattern, vl);
949 * Send message to all servers of protocol 'p' and higher.
951 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
955 va_start(vl, pattern);
956 for (lp = me.serv->down; lp; lp = lp->next)
957 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
958 vsendto_one(lp->value.cptr, pattern, vl);