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);
83 * Keep a copy of the last comment, for later use...
85 ircd_strncpy(to->info, notice, REALLEN);
87 if (!IsUser(to) && !IsUnknown(to) && !(to->flags & FLAGS_CLOSING))
88 sendto_ops("%s for %s", to->info, to->name);
89 Debug((DEBUG_ERROR, to->info));
92 static int can_send(struct Client* to)
95 return (IsDead(to) || IsMe(to) || -1 == to->fd) ? 0 : 1;
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
108 void flush_connections(struct Client* cptr)
115 for (i = HighestFd; i >= 0; i--) {
116 if ((cptr = LocalClientArray[i]))
123 * flush_sendq_except - run through local client array and flush
124 * the sendq for each client, if the address of the client sendq
125 * is the same as the one specified, it is skipped. This is used
126 * by dbuf_put to try to get some more memory before bailing and
127 * causing the client to be disconnected.
129 void flush_sendq_except(const struct DBuf* one)
133 for (i = HighestFd; i >= 0; i--) {
134 if ( (cptr = LocalClientArray[i]) && one != &cptr->sendQ)
142 * This function is called from the main select-loop (or whatever)
143 * when there is a chance that some output would be possible. This
144 * attempts to empty the send queue as far as possible...
146 void send_queued(struct Client *to)
149 assert(0 != to->local);
151 if (IsBlocked(to) || !can_send(to))
152 return; /* Don't bother */
154 while (DBufLength(&to->sendQ) > 0) {
156 const char* msg = dbuf_map(&to->sendQ, &len);
158 if ((len = deliver_it(to, msg, len))) {
159 dbuf_delete(&to->sendQ, len);
160 to->lastsq = DBufLength(&to->sendQ) / 1024;
166 dead_link(to, "Write error, closing link");
173 * send message to single client
175 void sendto_one(struct Client *to, const char* pattern, ...)
178 va_start(vl, pattern);
179 vsendto_one(to, pattern, vl);
184 void vsendto_one(struct Client *to, const char* pattern, va_list vl)
186 vsprintf_irc(sendbuf, pattern, vl);
191 static void send_to_god(struct Client* to, const char* buf)
193 if (!sdbflag && !IsUser(to)) {
194 char sbuf2[BUFSIZE + 1];
195 unsigned int len = strlen(buf) - 2; /* Remove "\r\n" */
198 len = IRCD_MIN(len, BUFSIZE);
199 ircd_strncpy(sbuf2, buf, len);
205 sendto_ops("SND:%-8.8s(%.4d): \"%s...%s\"",
206 to->name, len, sbuf2, &sbuf2[len - 200]);
209 sendto_ops("SND:%-8.8s(%.4d): \"%s\"", to->name, len, sbuf2);
215 void send_buffer(struct Client* to, char* buf)
226 * This socket has already been marked as dead
230 if (DBufLength(&to->sendQ) > get_sendq(to)) {
232 sendto_ops("Max SendQ limit exceeded for %s: " SIZE_T_FMT " > " SIZE_T_FMT,
233 to->name, DBufLength(&to->sendQ), get_sendq(to));
234 dead_link(to, "Max sendQ exceeded");
238 Debug((DEBUG_SEND, "Sending [%s] to %s", buf, to->name));
241 if (buf[len - 1] != '\n') {
249 if (0 == dbuf_put(&to->sendQ, buf, len)) {
250 dead_link(to, "Buffer allocation error");
255 send_to_god(to, buf);
258 * Update statistics. The following is slightly incorrect
259 * because it counts messages even if queued, but bytes
260 * only really sent. Queued bytes get updated in SendQueued.
265 * This little bit is to stop the sendQ from growing too large when
266 * there is no need for it to. Thus we call send_queued() every time
267 * 2k has been added to the queue since the last non-fatal write.
268 * Also stops us from deliberately building a large sendQ and then
269 * trying to flood that link with data (possible during the net
270 * relinking done by servers with a large load).
272 if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
276 void sendbufto_one(struct Client* to)
278 send_buffer(to, sendbuf);
281 static void vsendto_prefix_one(struct Client *to, struct Client *from,
282 const char* pattern, va_list vl)
284 if (to && from && MyUser(to) && IsUser(from))
286 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
289 struct User *user = from->user;
291 par = va_arg(vl, char *);
292 strcpy(sender, from->name);
298 strcat(sender, user->username);
300 if (*user->host && !MyConnect(from))
303 strcat(sender, user->host);
308 * Flag is used instead of strchr(sender, '@') for speed and
309 * also since username/nick may have had a '@' in them. -avalon
311 if (!flag && MyConnect(from) && *user->host)
314 strcat(sender, from->sockhost);
317 strcpy(&sendbuf[1], sender);
318 /* Assuming 'pattern' always starts with ":%s ..." */
319 vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
322 vsprintf_irc(sendbuf, pattern, vl);
326 void sendto_channel_butone(struct Client *one, struct Client *from, struct Channel *chptr,
327 const char* pattern, ...)
330 struct Membership* member;
331 struct Client *acptr;
334 va_start(vl, pattern);
337 for (member = chptr->members; member; member = member->next_member)
339 acptr = member->user;
340 /* ...was the one I should skip */
341 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
343 if (MyConnect(acptr)) /* (It is always a client) */
344 vsendto_prefix_one(acptr, from, pattern, vl);
345 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
347 sentalong[i] = sentalong_marker;
349 * Don't send channel messages to links that are still eating
350 * the net.burst: -- Run 2/1/1997
352 if (!IsBurstOrBurstAck(acptr->from))
353 vsendto_prefix_one(acptr, from, pattern, vl);
360 void sendmsgto_channel_butone(struct Client *one, struct Client *from,
361 struct Channel *chptr, const char *sender,
362 const char *cmd, const char *chname, const char *msg)
365 * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
366 * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
368 struct Membership* member;
369 struct Client *acptr;
377 * Precalculate the buffers we sent to the clients instead of doing an
378 * expensive sprintf() per member that we send to. We still have to
379 * use strcpy() which is evil.
381 if (IsServer(from)) {
382 sprintf(userbuf,":%s %s %s :%s",
383 from->name, ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
384 sprintf(servbuf,"%s %s %s :%s", NumServ(from), cmd, chname, msg);
387 sprintf(userbuf,":%s!%s@%s %s %s :%s",
388 from->name, from->username, from->user->host,
389 ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
390 sprintf(servbuf,"%s%s %s %s :%s", NumNick(from), cmd, chname, msg);
394 for (member = chptr->members; member; member = member->next_member)
396 acptr = member->user;
397 /* ...was the one I should skip */
398 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
400 if (MyConnect(acptr)) { /* (It is always a client) */
402 strcpy(sendbuf,userbuf);
404 sendbufto_one(acptr);
406 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
408 sentalong[i] = sentalong_marker;
409 if (!IsBurstOrBurstAck(acptr->from)) {
411 strcpy(sendbuf,servbuf);
413 sendbufto_one(acptr);
415 } /* of if MyConnect() */
416 } /* of for(members) */
419 void sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
420 const char* pattern, ...)
423 struct Membership* member;
424 struct Client *acptr;
426 va_start(vl, pattern);
428 for (member = chptr->members; member; member = member->next_member)
430 acptr = member->user;
431 /* ...was the one I should skip */
432 if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
434 if (MyConnect(acptr)) /* (It is always a client) */
435 vsendto_prefix_one(acptr, from, pattern, vl);
441 void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
442 const char* pattern, ...)
445 struct Membership* member;
446 struct Client *acptr;
455 va_start(vl, pattern);
458 for (member = chptr->members; member; member = member->next_member)
460 acptr = member->user;
461 if (acptr->from == acptr || /* Skip local clients */
463 Protocol(acptr->from) < 10 || /* Skip P09 links */
465 acptr->from == one || /* ...was the one I should skip */
466 !IsChanOp(member) || /* Skip non chanops */
467 IsZombie(member) || IsDeaf(acptr))
469 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
471 sentalong[i] = sentalong_marker;
472 /* Don't send channel messages to links that are
473 still eating the net.burst: -- Run 2/1/1997 */
474 if (!IsBurstOrBurstAck(acptr->from))
475 vsendto_prefix_one(acptr, from, pattern, vl);
480 /* Send message to all 2.9 servers */
481 /* This is a hack, because it assumes that we know how `vl' is build up */
482 source = va_arg(vl, char *);
483 tp = va_arg(vl, char *); /* Channel */
484 msg = va_arg(vl, char *);
485 for (member = chptr->members; member; member = member->next_member)
487 acptr = member->user;
488 if (acptr->from == acptr || /* Skip local clients */
489 Protocol(acptr->from) > 9 || /* Skip P10 servers */
490 acptr->from == one || /* ...was the one I should skip */
491 !IsChanOp(member) || /* Skip non chanops */
492 IsZombie(member) || IsDeaf(acptr))
494 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
496 sentalong[i] = sentalong_marker;
497 /* Don't send channel messages to links that are
498 still eating the net.burst: -- Run 2/1/1997 */
499 if (!IsBurstOrBurstAck(acptr->from))
501 struct Membership* other_member;
502 struct Client* acptr2;
505 /* Find all chanops in this direction: */
506 for (other_member = chptr->members; other_member; other_member = other_member->next_member)
508 acptr2 = other_member->user;
509 if (acptr2->from == acptr->from && acptr2->from != one &&
510 IsChanOp(other_member) && !IsZombie(other_member) &&
513 int len = strlen(acptr2->name);
514 if (tp + len + 2 > target + sizeof(target))
516 sendto_prefix_one(acptr, from,
517 ":%s NOTICE %s :%s", source, target, msg);
523 strcpy(tp, acptr2->name);
527 sendto_prefix_one(acptr, from,
528 ":%s NOTICE %s :%s", source, target, msg);
541 * Send a message to all connected servers except the client 'one'.
543 void sendto_serv_butone(struct Client *one, const char* pattern, ...)
548 va_start(vl, pattern);
549 vsprintf_irc(sendbuf, pattern, vl);
552 for (lp = me.serv->down; lp; lp = lp->next)
554 if (one && lp->value.cptr == one->from)
556 sendbufto_one(lp->value.cptr);
562 * sendbufto_serv_butone()
564 * Send prepared sendbuf to all connected servers except the client 'one'
565 * -Ghostwolf 18-May-97
567 void sendbufto_serv_butone(struct Client *one)
571 for (lp = me.serv->down; lp; lp = lp->next)
573 if (one && lp->value.cptr == one->from)
575 sendbufto_one(lp->value.cptr);
581 * sendto_common_channels()
583 * Sends a message to all people (inclusing `acptr') on local server
584 * who are in same channel with client `acptr'.
586 void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
589 struct Membership* chan;
590 struct Membership* member;
593 assert(0 != acptr->from);
594 assert(0 != pattern);
596 va_start(vl, pattern);
599 if (-1 < acptr->from->fd)
600 sentalong[acptr->from->fd] = sentalong_marker;
602 * loop through acptr's channels, and the members on their channels
605 for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
606 for (member = chan->channel->members; member; member = member->next_member) {
607 struct Client *cptr = member->user;
609 if (MyConnect(cptr) &&
610 -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
611 sentalong[i] = sentalong_marker;
612 vsendto_prefix_one(cptr, acptr, pattern, vl);
617 if (MyConnect(acptr))
618 vsendto_prefix_one(acptr, acptr, pattern, vl);
624 * sendto_channel_butserv
626 * Send a message to all members of a channel that
627 * are connected to this server.
629 void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
632 struct Membership* member;
633 struct Client *acptr;
635 va_start(vl, pattern);
637 for (member = chptr->members; member; member = member->next_member) {
638 acptr = member->user;
639 if (MyConnect(acptr) && !IsZombie(member))
640 vsendto_prefix_one(acptr, from, pattern, vl);
647 * Send a msg to all ppl on servers/hosts that match a specified mask
648 * (used for enhanced PRIVMSGs)
650 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
653 static int match_it(struct Client *one, const char *mask, int what)
658 return (match(mask, one->user->host) == 0);
661 return (match(mask, one->user->server->name) == 0);
666 * sendto_match_butone
668 * Send to all clients which match the mask in a way defined on 'what';
669 * either by user hostname or user servername.
671 void sendto_match_butone(struct Client *one, struct Client *from,
672 const char *mask, int what, const char* pattern, ...)
676 struct Client *cptr, *acptr;
678 va_start(vl, pattern);
679 for (i = 0; i <= HighestFd; i++)
681 if (!(cptr = LocalClientArray[i]))
682 continue; /* that clients are not mine */
683 if (cptr == one) /* must skip the origin !! */
687 for (acptr = GlobalClientList; acptr; acptr = acptr->next)
688 if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
690 /* a person on that server matches the mask, so we
691 * send *one* msg to that server ...
695 /* ... but only if there *IS* a matching person */
697 /* my client, does he match ? */
698 else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
700 vsendto_prefix_one(cptr, from, pattern, vl);
710 * Send to *local* ops but one.
712 void sendto_lops_butone(struct Client* one, const char* pattern, ...)
716 struct Client** clients = me.serv->client_list;
720 assert(0 != clients);
722 sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
723 va_start(vl, pattern);
724 vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
727 for (i = 0; i <= me.serv->nn_mask; ++i) {
728 if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
729 sprintf_irc(sendbuf, nbuf, cptr->name);
738 * Sends message to the list indicated by the bitmask field.
739 * Don't try to send to more than one list! That is not supported.
742 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
744 static char fmt[1024];
746 int i = 0; /* so that 1 points to opsarray[0] */
747 struct SLink *opslist;
751 if (!(opslist = opsarray[i]))
754 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
757 strcpy(fmt_target, opslist->value.cptr->name);
758 strcat(fmt_target, " :*** Notice -- ");
759 strcat(fmt_target, pattern);
760 vsendto_one(opslist->value.cptr, fmt, vl);
761 opslist = opslist->next;
769 * Send a prepared sendbuf to the list indicated by the bitmask field.
770 * Ghostwolf 16-May-97
772 void sendbufto_op_mask(unsigned int mask)
774 int i = 0; /* so that 1 points to opsarray[0] */
775 struct SLink *opslist;
778 if (!(opslist = opsarray[i]))
782 sendbufto_one(opslist->value.cptr);
783 opslist = opslist->next;
792 * Send to *local* ops only.
794 void vsendto_ops(const char *pattern, va_list vl)
801 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
803 for (i = 0; i <= HighestFd; i++)
804 if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
805 SendServNotice(cptr))
807 strcpy(fmt_target, cptr->name);
808 strcat(fmt_target, " :*** Notice -- ");
809 strcat(fmt_target, pattern);
810 vsendto_one(cptr, fmt, vl);
814 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
817 va_start(vl, pattern);
818 vsendto_op_mask(mask, pattern, vl);
822 void sendto_ops(const char *pattern, ...)
825 va_start(vl, pattern);
826 vsendto_op_mask(SNO_OLDSNO, pattern, vl);
833 * Send message to all operators.
834 * one - client not to send message to
835 * from- client which message is from *NEVER* NULL!!
837 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
843 va_start(vl, pattern);
845 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
847 if (!SendWallops(cptr))
849 i = cptr->from->fd; /* find connection oper is on */
850 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
852 if (cptr->from == one)
853 continue; /* ...was the one I should skip */
854 sentalong[i] = sentalong_marker;
855 vsendto_prefix_one(cptr->from, from, pattern, vl);
863 * sendto_g_serv_butone
865 * Send message to all remote +g users (server links).
867 * one - server not to send message to.
869 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
875 va_start(vl, pattern);
877 vsprintf_irc(sendbuf, pattern, vl);
878 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
880 if (!SendDebug(cptr))
882 i = cptr->from->fd; /* find connection user is on */
883 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
887 sentalong[i] = sentalong_marker;
888 if (cptr->from == one)
900 * to - destination client
901 * from - client which message is from
903 * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
906 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
909 va_start(vl, pattern);
910 vsendto_prefix_one(to, from, pattern, vl);
917 * Send to *local* ops only but NOT +s nonopers.
919 void sendto_realops(const char *pattern, ...)
923 va_start(vl, pattern);
924 vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
931 * Send message to all servers of protocol 'p' and lower.
933 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
937 va_start(vl, pattern);
938 for (lp = me.serv->down; lp; lp = lp->next)
939 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
940 vsendto_one(lp->value.cptr, pattern, vl);
945 * Send message to all servers of protocol 'p' and higher.
947 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
951 va_start(vl, pattern);
952 for (lp = me.serv->down; lp; lp = lp->next)
953 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
954 vsendto_one(lp->value.cptr, pattern, vl);