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. 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 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;
167 sprintf(tmp,"Write error: %s",(strerror(to->error)) ? (strerror(to->error)) : "Unknown error" );
176 * send message to single client
178 void sendto_one(struct Client *to, const char* pattern, ...)
181 va_start(vl, pattern);
182 vsendto_one(to, pattern, vl);
187 void vsendto_one(struct Client *to, const char* pattern, va_list vl)
189 vsprintf_irc(sendbuf, pattern, vl);
194 static void send_to_god(struct Client* to, const char* buf)
196 if (!sdbflag && !IsUser(to)) {
197 char sbuf2[BUFSIZE + 1];
198 unsigned int len = strlen(buf) - 2; /* Remove "\r\n" */
201 len = IRCD_MIN(len, BUFSIZE);
202 ircd_strncpy(sbuf2, buf, len);
208 sendto_ops("SND:%-8.8s(%.4d): \"%s...%s\"",
209 to->name, len, sbuf2, &sbuf2[len - 200]);
212 sendto_ops("SND:%-8.8s(%.4d): \"%s\"", to->name, len, sbuf2);
218 void send_buffer(struct Client* to, char* buf)
229 * This socket has already been marked as dead
233 if (DBufLength(&to->sendQ) > get_sendq(to)) {
235 sendto_ops("Max SendQ limit exceeded for %s: " SIZE_T_FMT " > " SIZE_T_FMT,
236 to->name, DBufLength(&to->sendQ), get_sendq(to));
237 dead_link(to, "Max sendQ exceeded");
241 Debug((DEBUG_SEND, "Sending [%s] to %s", buf, to->name));
244 if (buf[len - 1] != '\n') {
252 if (0 == dbuf_put(&to->sendQ, buf, len)) {
253 dead_link(to, "Buffer allocation error");
258 send_to_god(to, buf);
261 * Update statistics. The following is slightly incorrect
262 * because it counts messages even if queued, but bytes
263 * only really sent. Queued bytes get updated in SendQueued.
268 * This little bit is to stop the sendQ from growing too large when
269 * there is no need for it to. Thus we call send_queued() every time
270 * 2k has been added to the queue since the last non-fatal write.
271 * Also stops us from deliberately building a large sendQ and then
272 * trying to flood that link with data (possible during the net
273 * relinking done by servers with a large load).
275 if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
279 void sendbufto_one(struct Client* to)
281 send_buffer(to, sendbuf);
284 static void vsendto_prefix_one(struct Client *to, struct Client *from,
285 const char* pattern, va_list vl)
287 if (to && from && MyUser(to) && IsUser(from))
289 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
292 struct User *user = from->user;
294 par = va_arg(vl, char *);
295 strcpy(sender, from->name);
301 strcat(sender, user->username);
303 if (*user->host && !MyConnect(from))
306 strcat(sender, user->host);
311 * Flag is used instead of strchr(sender, '@') for speed and
312 * also since username/nick may have had a '@' in them. -avalon
314 if (!flag && MyConnect(from) && *user->host)
317 strcat(sender, from->sockhost);
320 strcpy(&sendbuf[1], sender);
321 /* Assuming 'pattern' always starts with ":%s ..." */
322 vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
325 vsprintf_irc(sendbuf, pattern, vl);
329 void sendto_channel_butone(struct Client *one, struct Client *from, struct Channel *chptr,
330 const char* pattern, ...)
333 struct Membership* member;
334 struct Client *acptr;
337 va_start(vl, pattern);
340 for (member = chptr->members; member; member = member->next_member)
342 acptr = member->user;
343 /* ...was the one I should skip */
344 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
346 if (MyConnect(acptr)) /* (It is always a client) */
347 vsendto_prefix_one(acptr, from, pattern, vl);
348 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
350 sentalong[i] = sentalong_marker;
352 * Don't send channel messages to links that are still eating
353 * the net.burst: -- Run 2/1/1997
355 if (!IsBurstOrBurstAck(acptr->from))
356 vsendto_prefix_one(acptr, from, pattern, vl);
363 void sendmsgto_channel_butone(struct Client *one, struct Client *from,
364 struct Channel *chptr, const char *sender,
365 const char *cmd, const char *chname, const char *msg)
368 * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
369 * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
371 struct Membership* member;
372 struct Client *acptr;
380 * Precalculate the buffers we sent to the clients instead of doing an
381 * expensive sprintf() per member that we send to. We still have to
382 * use strcpy() which is evil.
384 if (IsServer(from)) {
385 sprintf(userbuf,":%s %s %s :%s",
386 from->name, ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
387 sprintf(servbuf,"%s %s %s :%s", NumServ(from), cmd, chname, msg);
390 sprintf(userbuf,":%s!%s@%s %s %s :%s",
391 from->name, from->username, from->user->host,
392 ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
393 sprintf(servbuf,"%s%s %s %s :%s", NumNick(from), cmd, chname, msg);
397 for (member = chptr->members; member; member = member->next_member)
399 acptr = member->user;
400 /* ...was the one I should skip */
401 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
403 if (MyConnect(acptr)) { /* (It is always a client) */
405 strcpy(sendbuf,userbuf);
407 sendbufto_one(acptr);
409 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
411 sentalong[i] = sentalong_marker;
412 if (!IsBurstOrBurstAck(acptr->from)) {
414 strcpy(sendbuf,servbuf);
416 sendbufto_one(acptr);
418 } /* of if MyConnect() */
419 } /* of for(members) */
422 void sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
423 const char* pattern, ...)
426 struct Membership* member;
427 struct Client *acptr;
429 va_start(vl, pattern);
431 for (member = chptr->members; member; member = member->next_member)
433 acptr = member->user;
434 /* ...was the one I should skip */
435 if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
437 if (MyConnect(acptr)) /* (It is always a client) */
438 vsendto_prefix_one(acptr, from, pattern, vl);
444 void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
445 const char* pattern, ...)
448 struct Membership* member;
449 struct Client *acptr;
458 va_start(vl, pattern);
461 for (member = chptr->members; member; member = member->next_member)
463 acptr = member->user;
464 if (acptr->from == acptr || /* Skip local clients */
466 Protocol(acptr->from) < 10 || /* Skip P09 links */
468 acptr->from == one || /* ...was the one I should skip */
469 !IsChanOp(member) || /* Skip non chanops */
470 IsZombie(member) || IsDeaf(acptr))
472 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
474 sentalong[i] = sentalong_marker;
475 /* Don't send channel messages to links that are
476 still eating the net.burst: -- Run 2/1/1997 */
477 if (!IsBurstOrBurstAck(acptr->from))
478 vsendto_prefix_one(acptr, from, pattern, vl);
483 /* Send message to all 2.9 servers */
484 /* This is a hack, because it assumes that we know how `vl' is build up */
485 source = va_arg(vl, char *);
486 tp = va_arg(vl, char *); /* Channel */
487 msg = va_arg(vl, char *);
488 for (member = chptr->members; member; member = member->next_member)
490 acptr = member->user;
491 if (acptr->from == acptr || /* Skip local clients */
492 Protocol(acptr->from) > 9 || /* Skip P10 servers */
493 acptr->from == one || /* ...was the one I should skip */
494 !IsChanOp(member) || /* Skip non chanops */
495 IsZombie(member) || IsDeaf(acptr))
497 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
499 sentalong[i] = sentalong_marker;
500 /* Don't send channel messages to links that are
501 still eating the net.burst: -- Run 2/1/1997 */
502 if (!IsBurstOrBurstAck(acptr->from))
504 struct Membership* other_member;
505 struct Client* acptr2;
508 /* Find all chanops in this direction: */
509 for (other_member = chptr->members; other_member; other_member = other_member->next_member)
511 acptr2 = other_member->user;
512 if (acptr2->from == acptr->from && acptr2->from != one &&
513 IsChanOp(other_member) && !IsZombie(other_member) &&
516 int len = strlen(acptr2->name);
517 if (tp + len + 2 > target + sizeof(target))
519 sendto_prefix_one(acptr, from,
520 ":%s NOTICE %s :%s", source, target, msg);
526 strcpy(tp, acptr2->name);
530 sendto_prefix_one(acptr, from,
531 ":%s NOTICE %s :%s", source, target, msg);
544 * Send a message to all connected servers except the client 'one'.
546 void sendto_serv_butone(struct Client *one, const char* pattern, ...)
551 va_start(vl, pattern);
552 vsprintf_irc(sendbuf, pattern, vl);
555 for (lp = me.serv->down; lp; lp = lp->next)
557 if (one && lp->value.cptr == one->from)
559 sendbufto_one(lp->value.cptr);
565 * sendbufto_serv_butone()
567 * Send prepared sendbuf to all connected servers except the client 'one'
568 * -Ghostwolf 18-May-97
570 void sendbufto_serv_butone(struct Client *one)
574 for (lp = me.serv->down; lp; lp = lp->next)
576 if (one && lp->value.cptr == one->from)
578 sendbufto_one(lp->value.cptr);
584 * sendto_common_channels()
586 * Sends a message to all people (inclusing `acptr') on local server
587 * who are in same channel with client `acptr'.
589 void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
592 struct Membership* chan;
593 struct Membership* member;
596 assert(0 != acptr->from);
597 assert(0 != pattern);
599 va_start(vl, pattern);
602 if (-1 < acptr->from->fd)
603 sentalong[acptr->from->fd] = sentalong_marker;
605 * loop through acptr's channels, and the members on their channels
608 for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
609 for (member = chan->channel->members; member; member = member->next_member) {
610 struct Client *cptr = member->user;
612 if (MyConnect(cptr) &&
613 -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
614 sentalong[i] = sentalong_marker;
615 vsendto_prefix_one(cptr, acptr, pattern, vl);
620 if (MyConnect(acptr))
621 vsendto_prefix_one(acptr, acptr, pattern, vl);
627 * sendto_channel_butserv
629 * Send a message to all members of a channel that
630 * are connected to this server.
632 void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
635 struct Membership* member;
636 struct Client *acptr;
638 va_start(vl, pattern);
640 for (member = chptr->members; member; member = member->next_member) {
641 acptr = member->user;
642 if (MyConnect(acptr) && !IsZombie(member))
643 vsendto_prefix_one(acptr, from, pattern, vl);
650 * Send a msg to all ppl on servers/hosts that match a specified mask
651 * (used for enhanced PRIVMSGs)
653 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
656 static int match_it(struct Client *one, const char *mask, int what)
661 return (match(mask, one->user->host) == 0);
664 return (match(mask, one->user->server->name) == 0);
669 * sendto_match_butone
671 * Send to all clients which match the mask in a way defined on 'what';
672 * either by user hostname or user servername.
674 void sendto_match_butone(struct Client *one, struct Client *from,
675 const char *mask, int what, const char* pattern, ...)
679 struct Client *cptr, *acptr;
681 va_start(vl, pattern);
682 for (i = 0; i <= HighestFd; i++)
684 if (!(cptr = LocalClientArray[i]))
685 continue; /* that clients are not mine */
686 if (cptr == one) /* must skip the origin !! */
690 for (acptr = GlobalClientList; acptr; acptr = acptr->next)
691 if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
693 /* a person on that server matches the mask, so we
694 * send *one* msg to that server ...
698 /* ... but only if there *IS* a matching person */
700 /* my client, does he match ? */
701 else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
703 vsendto_prefix_one(cptr, from, pattern, vl);
713 * Send to *local* ops but one.
715 void sendto_lops_butone(struct Client* one, const char* pattern, ...)
719 struct Client** clients = me.serv->client_list;
723 assert(0 != clients);
725 sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
726 va_start(vl, pattern);
727 vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
730 for (i = 0; i <= me.serv->nn_mask; ++i) {
731 if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
732 sprintf_irc(sendbuf, nbuf, cptr->name);
741 * Sends message to the list indicated by the bitmask field.
742 * Don't try to send to more than one list! That is not supported.
745 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
747 static char fmt[1024];
749 int i = 0; /* so that 1 points to opsarray[0] */
750 struct SLink *opslist;
754 if (!(opslist = opsarray[i]))
757 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
760 strcpy(fmt_target, opslist->value.cptr->name);
761 strcat(fmt_target, " :*** Notice -- ");
762 strcat(fmt_target, pattern);
763 vsendto_one(opslist->value.cptr, fmt, vl);
764 opslist = opslist->next;
772 * Send a prepared sendbuf to the list indicated by the bitmask field.
773 * Ghostwolf 16-May-97
775 void sendbufto_op_mask(unsigned int mask)
777 int i = 0; /* so that 1 points to opsarray[0] */
778 struct SLink *opslist;
781 if (!(opslist = opsarray[i]))
785 sendbufto_one(opslist->value.cptr);
786 opslist = opslist->next;
795 * Send to *local* ops only.
797 void vsendto_ops(const char *pattern, va_list vl)
804 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
806 for (i = 0; i <= HighestFd; i++)
807 if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
808 SendServNotice(cptr))
810 strcpy(fmt_target, cptr->name);
811 strcat(fmt_target, " :*** Notice -- ");
812 strcat(fmt_target, pattern);
813 vsendto_one(cptr, fmt, vl);
817 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
820 va_start(vl, pattern);
821 vsendto_op_mask(mask, pattern, vl);
825 void sendto_ops(const char *pattern, ...)
828 va_start(vl, pattern);
829 vsendto_op_mask(SNO_OLDSNO, pattern, vl);
836 * Send message to all operators.
837 * one - client not to send message to
838 * from- client which message is from *NEVER* NULL!!
840 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
846 va_start(vl, pattern);
848 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
850 if (!SendWallops(cptr))
852 i = cptr->from->fd; /* find connection oper is on */
853 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
855 if (cptr->from == one)
856 continue; /* ...was the one I should skip */
857 sentalong[i] = sentalong_marker;
858 vsendto_prefix_one(cptr->from, from, pattern, vl);
866 * sendto_g_serv_butone
868 * Send message to all remote +g users (server links).
870 * one - server not to send message to.
872 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
878 va_start(vl, pattern);
880 vsprintf_irc(sendbuf, pattern, vl);
881 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
883 if (!SendDebug(cptr))
885 i = cptr->from->fd; /* find connection user is on */
886 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
890 sentalong[i] = sentalong_marker;
891 if (cptr->from == one)
903 * to - destination client
904 * from - client which message is from
906 * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
909 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
912 va_start(vl, pattern);
913 vsendto_prefix_one(to, from, pattern, vl);
920 * Send to *local* ops only but NOT +s nonopers.
922 void sendto_realops(const char *pattern, ...)
926 va_start(vl, pattern);
927 vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
934 * Send message to all servers of protocol 'p' and lower.
936 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
940 va_start(vl, pattern);
941 for (lp = me.serv->down; lp; lp = lp->next)
942 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
943 vsendto_one(lp->value.cptr, pattern, vl);
948 * Send message to all servers of protocol 'p' and higher.
950 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
954 va_start(vl, pattern);
955 for (lp = me.serv->down; lp; lp = lp->next)
956 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
957 vsendto_one(lp->value.cptr, pattern, vl);