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_snprintf.h"
28 #include "ircd_string.h"
37 #include "sprintf_irc.h"
47 static int sentalong[MAXCONNECTIONS];
48 static int sentalong_marker;
49 struct SLink *opsarray[32]; /* don't use highest bit unless you change
50 atoi to strtoul in sendto_op_mask() */
59 * An error has been detected. The link *must* be closed,
60 * but *cannot* call ExitClient (m_bye) from here.
61 * Instead, mark it with FLAGS_DEADSOCKET. This should
62 * generate ExitClient from the main loop.
64 * If 'notice' is not NULL, it is assumed to be a format
65 * for a message to local opers. It can contain only one
66 * '%s', which will be replaced by the sockhost field of
69 * Also, the notice is skipped for "uninteresting" cases,
70 * like Persons and yet unknown connections...
73 static void dead_link(struct Client *to, char *notice)
75 to->flags |= FLAGS_DEADSOCKET;
77 * If because of BUFFERPOOL problem then clean dbuf's now so that
78 * notices don't hurt operators below.
80 DBufClear(&to->recvQ);
81 DBufClear(&to->sendQ);
84 * Keep a copy of the last comment, for later use...
86 ircd_strncpy(to->info, notice, REALLEN);
88 if (!IsUser(to) && !IsUnknown(to) && !(to->flags & FLAGS_CLOSING))
89 sendto_ops("%s for %s", to->info, to->name);
90 Debug((DEBUG_ERROR, to->info));
93 static int can_send(struct Client* to)
96 return (IsDead(to) || IsMe(to) || -1 == to->fd) ? 0 : 1;
102 * Used to empty all output buffers for all connections. Should only
103 * be called once per scan of connections. There should be a select in
104 * here perhaps but that means either forcing a timeout or doing a poll.
105 * When flushing, all we do is empty the obuffer array for each local
106 * client and try to send it. if we cant send it, it goes into the sendQ
109 void flush_connections(struct Client* cptr)
116 for (i = HighestFd; i >= 0; i--) {
117 if ((cptr = LocalClientArray[i]))
124 * flush_sendq_except - run through local client array and flush
125 * the sendq for each client, if the address of the client sendq
126 * is the same as the one specified, it is skipped. This is used
127 * by dbuf_put to try to get some more memory before bailing and
128 * causing the client to be disconnected.
130 void flush_sendq_except(const struct DBuf* one)
134 for (i = HighestFd; i >= 0; i--) {
135 if ( (cptr = LocalClientArray[i]) && one != &cptr->sendQ)
143 * This function is called from the main select-loop (or whatever)
144 * when there is a chance that some output would be possible. This
145 * attempts to empty the send queue as far as possible...
147 void send_queued(struct Client *to)
150 assert(0 != to->local);
152 if (IsBlocked(to) || !can_send(to))
153 return; /* Don't bother */
155 while (DBufLength(&to->sendQ) > 0) {
157 const char* msg = dbuf_map(&to->sendQ, &len);
159 if ((len = deliver_it(to, msg, len))) {
160 dbuf_delete(&to->sendQ, len);
161 to->lastsq = DBufLength(&to->sendQ) / 1024;
168 sprintf(tmp,"Write error: %s",(strerror(to->error)) ? (strerror(to->error)) : "Unknown error" );
177 * send message to single client
179 /* See sendcmdto_one, below */
180 void sendto_one(struct Client *to, const char* pattern, ...)
183 va_start(vl, pattern);
184 vsendto_one(to, pattern, vl);
188 /* See vsendcmdto_one, below */
189 void vsendto_one(struct Client *to, const char* pattern, va_list vl)
191 vsprintf_irc(sendbuf, pattern, vl);
196 static void send_to_god(struct Client* to, const char* buf)
198 if (!sdbflag && !IsUser(to)) {
199 char sbuf2[BUFSIZE + 1];
200 unsigned int len = strlen(buf) - 2; /* Remove "\r\n" */
203 len = IRCD_MIN(len, BUFSIZE);
204 ircd_strncpy(sbuf2, buf, len);
210 sendto_ops("SND:%-8.8s(%.4d): \"%s...%s\"",
211 to->name, len, sbuf2, &sbuf2[len - 200]);
214 sendto_ops("SND:%-8.8s(%.4d): \"%s\"", to->name, len, sbuf2);
220 void send_buffer(struct Client* to, char* buf)
231 * This socket has already been marked as dead
235 if (DBufLength(&to->sendQ) > get_sendq(to)) {
237 sendto_ops("Max SendQ limit exceeded for %s: " SIZE_T_FMT " > " SIZE_T_FMT,
238 to->name, DBufLength(&to->sendQ), get_sendq(to));
239 dead_link(to, "Max sendQ exceeded");
243 Debug((DEBUG_SEND, "Sending [%s] to %s", buf, to->name));
246 if (buf[len - 1] != '\n') {
254 if (0 == dbuf_put(&to->sendQ, buf, len)) {
255 dead_link(to, "Buffer allocation error");
260 send_to_god(to, buf);
263 * Update statistics. The following is slightly incorrect
264 * because it counts messages even if queued, but bytes
265 * only really sent. Queued bytes get updated in SendQueued.
270 * This little bit is to stop the sendQ from growing too large when
271 * there is no need for it to. Thus we call send_queued() every time
272 * 2k has been added to the queue since the last non-fatal write.
273 * Also stops us from deliberately building a large sendQ and then
274 * trying to flood that link with data (possible during the net
275 * relinking done by servers with a large load).
277 if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
281 void sendbufto_one(struct Client* to)
283 send_buffer(to, sendbuf);
286 /* See vsendcmdto_one, below */
287 static void vsendto_prefix_one(struct Client *to, struct Client *from,
288 const char* pattern, va_list vl)
290 if (to && from && MyUser(to) && IsUser(from))
292 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
295 struct User *user = from->user;
297 par = va_arg(vl, char *);
298 strcpy(sender, from->name);
304 strcat(sender, user->username);
306 if (*user->host && !MyConnect(from))
309 strcat(sender, user->host);
314 * Flag is used instead of strchr(sender, '@') for speed and
315 * also since username/nick may have had a '@' in them. -avalon
317 if (!flag && MyConnect(from) && *user->host)
320 strcat(sender, from->sockhost);
323 strcpy(&sendbuf[1], sender);
324 /* Assuming 'pattern' always starts with ":%s ..." */
325 vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
328 vsprintf_irc(sendbuf, pattern, vl);
332 /* See sendcmdto_channel_butone, below */
333 void sendto_channel_butone(struct Client *one, struct Client *from, struct Channel *chptr,
334 const char* pattern, ...)
337 struct Membership* member;
338 struct Client *acptr;
341 va_start(vl, pattern);
344 for (member = chptr->members; member; member = member->next_member)
346 acptr = member->user;
347 /* ...was the one I should skip */
348 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
350 if (MyConnect(acptr)) /* (It is always a client) */
351 vsendto_prefix_one(acptr, from, pattern, vl);
352 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
354 sentalong[i] = sentalong_marker;
356 * Don't send channel messages to links that are still eating
357 * the net.burst: -- Run 2/1/1997
359 if (!IsBurstOrBurstAck(acptr->from))
360 vsendto_prefix_one(acptr, from, pattern, vl);
366 /* See sendcmdto_channel_butone, below */
367 void sendmsgto_channel_butone(struct Client *one, struct Client *from,
368 struct Channel *chptr, const char *sender,
369 const char *cmd, const char *chname, const char *msg)
372 * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
373 * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
375 struct Membership* member;
376 struct Client *acptr;
384 * Precalculate the buffers we sent to the clients instead of doing an
385 * expensive sprintf() per member that we send to. We still have to
386 * use strcpy() which is evil.
388 if (IsServer(from)) {
389 sprintf(userbuf,":%s %s %s :%s",
390 from->name, ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
391 sprintf(servbuf,"%s %s %s :%s", NumServ(from), cmd, chname, msg);
394 sprintf(userbuf,":%s!%s@%s %s %s :%s",
395 from->name, from->user->username, from->user->host,
396 ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
397 sprintf(servbuf,"%s%s %s %s :%s", NumNick(from), cmd, chname, msg);
401 for (member = chptr->members; member; member = member->next_member)
403 acptr = member->user;
404 /* ...was the one I should skip */
405 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
407 if (MyConnect(acptr)) { /* (It is always a client) */
409 strcpy(sendbuf,userbuf);
411 sendbufto_one(acptr);
413 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
415 sentalong[i] = sentalong_marker;
416 if (!IsBurstOrBurstAck(acptr->from)) {
418 strcpy(sendbuf,servbuf);
420 sendbufto_one(acptr);
422 } /* of if MyConnect() */
423 } /* of for(members) */
426 /* See sendcmdto_channel_butone, below */
427 void sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
428 const char* pattern, ...)
431 struct Membership* member;
432 struct Client *acptr;
434 va_start(vl, pattern);
436 for (member = chptr->members; member; member = member->next_member)
438 acptr = member->user;
439 /* ...was the one I should skip */
440 if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
442 if (MyConnect(acptr)) /* (It is always a client) */
443 vsendto_prefix_one(acptr, from, pattern, vl);
449 /* See sendcmdto_channel_butone, below */
450 void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
451 const char* pattern, ...)
454 struct Membership* member;
455 struct Client *acptr;
464 va_start(vl, pattern);
467 for (member = chptr->members; member; member = member->next_member)
469 acptr = member->user;
470 if (acptr->from == acptr || /* Skip local clients */
472 Protocol(acptr->from) < 10 || /* Skip P09 links */
474 acptr->from == one || /* ...was the one I should skip */
475 !IsChanOp(member) || /* Skip non chanops */
476 IsZombie(member) || IsDeaf(acptr))
478 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
480 sentalong[i] = sentalong_marker;
481 /* Don't send channel messages to links that are
482 still eating the net.burst: -- Run 2/1/1997 */
483 if (!IsBurstOrBurstAck(acptr->from))
484 vsendto_prefix_one(acptr, from, pattern, vl);
489 /* Send message to all 2.9 servers */
490 /* This is a hack, because it assumes that we know how `vl' is build up */
491 source = va_arg(vl, char *);
492 tp = va_arg(vl, char *); /* Channel */
493 msg = va_arg(vl, char *);
494 for (member = chptr->members; member; member = member->next_member)
496 acptr = member->user;
497 if (acptr->from == acptr || /* Skip local clients */
498 Protocol(acptr->from) > 9 || /* Skip P10 servers */
499 acptr->from == one || /* ...was the one I should skip */
500 !IsChanOp(member) || /* Skip non chanops */
501 IsZombie(member) || IsDeaf(acptr))
503 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
505 sentalong[i] = sentalong_marker;
506 /* Don't send channel messages to links that are
507 still eating the net.burst: -- Run 2/1/1997 */
508 if (!IsBurstOrBurstAck(acptr->from))
510 struct Membership* other_member;
511 struct Client* acptr2;
514 /* Find all chanops in this direction: */
515 for (other_member = chptr->members; other_member; other_member = other_member->next_member)
517 acptr2 = other_member->user;
518 if (acptr2->from == acptr->from && acptr2->from != one &&
519 IsChanOp(other_member) && !IsZombie(other_member) &&
522 int len = strlen(acptr2->name);
523 if (tp + len + 2 > target + sizeof(target))
525 sendto_prefix_one(acptr, from,
526 ":%s NOTICE %s :%s", source, target, msg);
532 strcpy(tp, acptr2->name);
536 sendto_prefix_one(acptr, from,
537 ":%s NOTICE %s :%s", source, target, msg);
550 * Send a message to all connected servers except the client 'one'.
552 /* See sendcmdto_serv_butone, below */
553 void sendto_serv_butone(struct Client *one, const char* pattern, ...)
558 va_start(vl, pattern);
559 vsprintf_irc(sendbuf, pattern, vl);
562 for (lp = me.serv->down; lp; lp = lp->next)
564 if (one && lp->value.cptr == one->from)
566 sendbufto_one(lp->value.cptr);
572 * sendbufto_serv_butone()
574 * Send prepared sendbuf to all connected servers except the client 'one'
575 * -Ghostwolf 18-May-97
577 void sendbufto_serv_butone(struct Client *one)
581 for (lp = me.serv->down; lp; lp = lp->next)
583 if (one && lp->value.cptr == one->from)
585 sendbufto_one(lp->value.cptr);
591 * sendto_common_channels()
593 * Sends a message to all people (inclusing `acptr') on local server
594 * who are in same channel with client `acptr'.
596 /* See sendcmdto_common_channels, below */
597 void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
600 struct Membership* chan;
601 struct Membership* member;
604 assert(0 != acptr->from);
605 assert(0 != pattern);
607 va_start(vl, pattern);
610 if (-1 < acptr->from->fd)
611 sentalong[acptr->from->fd] = sentalong_marker;
613 * loop through acptr's channels, and the members on their channels
616 for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
617 for (member = chan->channel->members; member; member = member->next_member) {
618 struct Client *cptr = member->user;
620 if (MyConnect(cptr) &&
621 -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
622 sentalong[i] = sentalong_marker;
623 vsendto_prefix_one(cptr, acptr, pattern, vl);
628 if (MyConnect(acptr))
629 vsendto_prefix_one(acptr, acptr, pattern, vl);
635 * sendto_channel_butserv
637 * Send a message to all members of a channel that
638 * are connected to this server.
640 * This contains a subtle bug; after the first call to vsendto_prefix_one()
641 * below, vl is in an indeterminate state, according to ANSI; we'd have to
642 * move va_start() and va_end() into the loop to correct the problem. It's
643 * easier, however, just to use sendcmdto_channel_butserv(), which builds a
644 * buffer and sends that prepared buffer to each channel member.
646 /* See sendcmdto_channel_butserv, below */
647 void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
650 struct Membership* member;
651 struct Client *acptr;
653 va_start(vl, pattern);
655 for (member = chptr->members; member; member = member->next_member) {
656 acptr = member->user;
657 if (MyConnect(acptr) && !IsZombie(member))
658 vsendto_prefix_one(acptr, from, pattern, vl);
665 * Send a msg to all ppl on servers/hosts that match a specified mask
666 * (used for enhanced PRIVMSGs)
668 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
671 static int match_it(struct Client *one, const char *mask, int what)
676 return (match(mask, one->user->host) == 0);
679 return (match(mask, one->user->server->name) == 0);
684 * sendto_match_butone
686 * Send to all clients which match the mask in a way defined on 'what';
687 * either by user hostname or user servername.
689 /* See sendcmdto_match_butone, below */
690 void sendto_match_butone(struct Client *one, struct Client *from,
691 const char *mask, int what, const char* pattern, ...)
695 struct Client *cptr, *acptr;
697 va_start(vl, pattern);
698 for (i = 0; i <= HighestFd; i++)
700 if (!(cptr = LocalClientArray[i]))
701 continue; /* that clients are not mine */
702 if (cptr == one) /* must skip the origin !! */
706 for (acptr = GlobalClientList; acptr; acptr = acptr->next)
707 if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
709 /* a person on that server matches the mask, so we
710 * send *one* msg to that server ...
714 /* ... but only if there *IS* a matching person */
716 /* my client, does he match ? */
717 else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
719 vsendto_prefix_one(cptr, from, pattern, vl);
729 * Send to *local* ops but one.
731 /* See sendto_opmask_butone, below */
732 void sendto_lops_butone(struct Client* one, const char* pattern, ...)
736 struct Client** clients = me.serv->client_list;
740 assert(0 != clients);
742 sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
743 va_start(vl, pattern);
744 vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
747 for (i = 0; i <= me.serv->nn_mask; ++i) {
748 if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
749 sprintf_irc(sendbuf, nbuf, cptr->name);
758 * Sends message to the list indicated by the bitmask field.
759 * Don't try to send to more than one list! That is not supported.
762 #ifdef OLD_VSENDTO_OP_MASK
763 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
765 static char fmt[1024];
767 int i = 0; /* so that 1 points to opsarray[0] */
768 struct SLink *opslist;
772 if (!(opslist = opsarray[i]))
775 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
778 strcpy(fmt_target, opslist->value.cptr->name);
779 strcat(fmt_target, " :*** Notice -- ");
780 strcat(fmt_target, pattern);
781 vsendto_one(opslist->value.cptr, fmt, vl);
782 opslist = opslist->next;
786 #else /* !OLD_VSENDTO_OP_MASK */
787 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
789 vsendto_opmask_butone(0, mask, pattern, vl);
791 #endif /* OLD_VSENDTO_OP_MASK */
796 * Send a prepared sendbuf to the list indicated by the bitmask field.
797 * Ghostwolf 16-May-97
799 void sendbufto_op_mask(unsigned int mask)
801 int i = 0; /* so that 1 points to opsarray[0] */
802 struct SLink *opslist;
805 if (!(opslist = opsarray[i]))
809 sendbufto_one(opslist->value.cptr);
810 opslist = opslist->next;
819 * Send to *local* ops only.
821 /* See vsendto_opmask_butone, below */
822 void vsendto_ops(const char *pattern, va_list vl)
829 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
831 for (i = 0; i <= HighestFd; i++)
832 if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
833 SendServNotice(cptr))
835 strcpy(fmt_target, cptr->name);
836 strcat(fmt_target, " :*** Notice -- ");
837 strcat(fmt_target, pattern);
838 vsendto_one(cptr, fmt, vl);
842 /* See sendto_opmask_butone, below */
843 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
846 va_start(vl, pattern);
847 vsendto_op_mask(mask, pattern, vl);
851 /* See sendto_opmask_butone, below */
852 void sendto_ops(const char *pattern, ...)
855 va_start(vl, pattern);
856 vsendto_op_mask(SNO_OLDSNO, pattern, vl);
863 * Send message to all operators.
864 * one - client not to send message to
865 * from- client which message is from *NEVER* NULL!!
867 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
873 va_start(vl, pattern);
875 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
877 if (!SendWallops(cptr))
879 i = cptr->from->fd; /* find connection oper is on */
880 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
882 if (cptr->from == one)
883 continue; /* ...was the one I should skip */
884 sentalong[i] = sentalong_marker;
885 vsendto_prefix_one(cptr->from, from, pattern, vl);
893 * sendto_g_serv_butone
895 * Send message to all remote +g users (server links).
897 * one - server not to send message to.
899 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
905 va_start(vl, pattern);
907 vsprintf_irc(sendbuf, pattern, vl);
908 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
910 if (!SendDebug(cptr))
912 i = cptr->from->fd; /* find connection user is on */
913 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
917 sentalong[i] = sentalong_marker;
918 if (cptr->from == one)
930 * to - destination client
931 * from - client which message is from
933 * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
936 /* See sendcmdto_one, below */
937 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
940 va_start(vl, pattern);
941 vsendto_prefix_one(to, from, pattern, vl);
948 * Send to *local* ops only but NOT +s nonopers.
950 /* See sendto_opmask_butone, below */
951 void sendto_realops(const char *pattern, ...)
955 va_start(vl, pattern);
956 vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
963 * Send message to all servers of protocol 'p' and lower.
965 /* See sendcmdto_serv_butone, below */
966 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
970 va_start(vl, pattern);
971 for (lp = me.serv->down; lp; lp = lp->next)
972 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
973 vsendto_one(lp->value.cptr, pattern, vl);
978 * Send message to all servers of protocol 'p' and higher.
980 /* See sendcmdto_serv_butone, below */
981 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
985 va_start(vl, pattern);
986 for (lp = me.serv->down; lp; lp = lp->next)
987 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
988 vsendto_one(lp->value.cptr, pattern, vl);
993 * Send a raw command to a single client; use *ONLY* if you absolutely
994 * must send a command without a prefix.
996 void sendrawto_one(struct Client *to, const char *pattern, ...)
998 char sndbuf[IRC_BUFSIZE];
1001 va_start(vl, pattern);
1002 ircd_vsnprintf(to, sndbuf, sizeof(sndbuf) - 2, pattern, vl);
1005 send_buffer(to, sndbuf);
1009 * Send a (prefixed) command to a single client; select which of <cmd>
1010 * <tok> to use depending on if to is a server or not. <from> is the
1011 * originator of the command.
1013 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
1014 struct Client *to, const char *pattern, ...)
1018 va_start(vl, pattern);
1019 vsendcmdto_one(from, cmd, tok, to, pattern, vl);
1023 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
1024 struct Client *to, const char *pattern, va_list vl)
1027 char sndbuf[IRC_BUFSIZE];
1029 vd.vd_format = pattern; /* set up the struct VarData for %v */
1032 ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from,
1033 IsServer(to) || IsMe(to) ? tok : cmd, &vd);
1035 send_buffer(to, sndbuf);
1039 * Send a (prefixed) command to all servers but one, using tok; cmd
1040 * is ignored in this particular function.
1042 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
1043 const char *tok, struct Client *one,
1044 const char *pattern, ...)
1047 char sndbuf[IRC_BUFSIZE];
1050 vd.vd_format = pattern; /* set up the struct VarData for %v */
1051 va_start(vd.vd_args, pattern);
1054 ircd_snprintf(&me, sndbuf, sizeof(sndbuf) - 2, "%C %s %v", from, tok, &vd);
1057 /* send it to our downlinks */
1058 for (lp = me.serv->down; lp; lp = lp->next) {
1059 if (one && lp->value.cptr == one->from)
1061 send_buffer(lp->value.cptr, sndbuf);
1066 * Send a (prefix) command originating from <from> to all channels
1067 * <from> is locally on. <from> must be a user. <tok> is ignored in
1070 /* XXX sentalong_marker used XXX
1072 * There is not an easy way to revoke the need for sentalong_marker
1073 * from this function. Thoughts and ideas would be welcome... -Kev
1075 * One possibility would be to internalize the sentalong array; that
1076 * could be prohibitively big, though. We could get around that by
1077 * making one that's the number of connected servers or something...
1078 * or perhaps by adding a special flag to the servers we've sent a
1079 * message to, and then a final loop through the connected servers
1080 * to delete the flag. -Kev
1082 void sendcmdto_common_channels(struct Client *from, const char *cmd,
1083 const char *tok, const char *pattern, ...)
1086 char sndbuf[IRC_BUFSIZE];
1087 struct Membership *chan;
1088 struct Membership *member;
1091 assert(0 != from->from);
1092 assert(0 != pattern);
1093 assert(!IsServer(from) && !IsMe(from));
1095 vd.vd_format = pattern; /* set up the struct VarData for %v */
1097 va_start(vd.vd_args, pattern);
1099 /* build the buffer */
1100 ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1104 if (-1 < from->from->fd)
1105 sentalong[from->from->fd] = sentalong_marker;
1107 * loop through from's channels, and the members on their channels
1109 for (chan = from->user->channel; chan; chan = chan->next_channel)
1110 for (member = chan->channel->members; member;
1111 member = member->next_member)
1112 if (MyConnect(member->user) && -1 < from->fd &&
1113 sentalong[member->user->from->fd] != sentalong_marker) {
1114 sentalong[member->user->from->fd] = sentalong_marker;
1115 send_buffer(member->user, sndbuf);
1118 if (MyConnect(from))
1119 send_buffer(from, sndbuf);
1123 * Send a (prefixed) command to all local users on the channel specified
1124 * by <to>; <tok> is ignored by this function
1126 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
1127 const char *tok, struct Channel *to,
1128 const char *pattern, ...)
1131 char sndbuf[IRC_BUFSIZE];
1132 struct Membership *member;
1134 vd.vd_format = pattern; /* set up the struct VarData for %v */
1135 va_start(vd.vd_args, pattern);
1137 /* build the buffer */
1138 ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1141 /* send the buffer to each local channel member */
1142 for (member = to->members; member; member = member->next_member) {
1143 if (MyConnect(member->user) && !IsZombie(member))
1144 send_buffer(member->user, sndbuf);
1149 * Send a (prefixed) command to all users on this channel, including
1150 * remote users; users to skip may be specified by setting appropriate
1151 * flags in the <skip> argument. <one> will also be skipped.
1153 /* XXX sentalong_marker used XXX
1155 * We can drop sentalong_marker from this function by adding a field to
1156 * channels and to connections; what we do is make a user's connection
1157 * a "member" of the channel by adding it to the new list, and we use
1158 * the struct Membership status as a reference count. Then, to implement
1159 * this function, we just walk the list of connections. Unfortunately,
1160 * this doesn't account for sending only to channel ops, or for not
1161 * sending to +d users; we could account for that by splitting those
1162 * counts out, but that would imply adding two more fields (at least) to
1163 * the struct Membership... -Kev
1165 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
1166 const char *tok, struct Channel *to,
1167 struct Client *one, unsigned int skip,
1168 const char *pattern, ...)
1170 struct Membership *member;
1172 char userbuf[IRC_BUFSIZE];
1173 char servbuf[IRC_BUFSIZE];
1175 vd.vd_format = pattern;
1177 /* Build buffer to send to users */
1178 va_start(vd.vd_args, pattern);
1179 ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1182 /* Build buffer to send to servers */
1183 va_start(vd.vd_args, pattern);
1184 ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1187 /* send buffer along! */
1189 for (member = to->members; member; member = member->next_member) {
1190 /* skip one, zombies, and deaf users... */
1191 if (member->user->from == one || IsZombie(member) ||
1192 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
1193 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
1194 (skip & SKIP_BURST && IsBurstOrBurstAck(member->user->from)) ||
1195 member->user->from->fd < -1 ||
1196 sentalong[member->user->from->fd] == sentalong_marker)
1199 if (MyConnect(member->user)) /* pick right buffer to send */
1200 send_buffer(member->user, userbuf);
1202 send_buffer(member->user, servbuf);
1207 * Send a (prefixed) command to all users except <one> that have
1210 /* XXX sentalong_marker used XXX
1212 * Again, we can solve this use of sentalong_marker by adding a field
1213 * to connections--a count of the number of +w users, and another count
1214 * of +g users. Then, just walk through the local clients to send
1215 * those messages, and another walk through the connected servers list,
1216 * sending only if there's a non-zero count. No caveats here, either,
1217 * beyond remembering to decrement the count when a user /quit's or is
1218 * killed, or a server is squit. -Kev
1220 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
1221 const char *tok, struct Client *one,
1222 unsigned int flag, const char *pattern, ...)
1225 struct Client *cptr;
1226 char userbuf[IRC_BUFSIZE];
1227 char servbuf[IRC_BUFSIZE];
1229 vd.vd_format = pattern;
1231 /* Build buffer to send to users */
1232 va_start(vd.vd_args, pattern);
1233 ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C " MSG_WALLOPS " %v",
1237 /* Build buffer to send to servers */
1238 va_start(vd.vd_args, pattern);
1239 ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1242 /* send buffer along! */
1244 for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1245 if (cptr->from == one || IsServer(cptr) || !(cptr->flags & flag) ||
1246 cptr->from->fd < 0 || sentalong[cptr->from->fd] == sentalong_marker)
1247 continue; /* skip it */
1249 if (MyConnect(cptr)) /* send right buffer */
1250 send_buffer(cptr, userbuf);
1252 send_buffer(cptr, servbuf);
1257 * Send a (prefixed) command to all users who match <to>, under control
1260 /* XXX sentalong_marker used XXX
1262 * This is also a difficult one to solve. The basic approach would be
1263 * to walk the client list of each connected server until we find a
1264 * match--but then, we also have to walk the client list of all the
1265 * servers behind that one. We could implement this recursively--or we
1266 * could add (yet another) field to the connection struct that would be
1267 * a linked list of clients introduced through that link, and just walk
1268 * that, making this into an iterative implementation. Unfortunately,
1269 * we probably would not be able to use tail recursion for the recursive
1270 * solution, so a deep network could exhaust our stack space; therefore
1271 * I favor the extra linked list, even though that increases the
1272 * complexity of the database. -Kev
1274 void sendcmdto_match_butone(struct Client *from, const char *cmd,
1275 const char *tok, const char *to,
1276 struct Client *one, unsigned int who,
1277 const char *pattern, ...)
1280 struct Client *cptr;
1281 char userbuf[IRC_BUFSIZE];
1282 char servbuf[IRC_BUFSIZE];
1284 vd.vd_format = pattern;
1286 /* Build buffer to send to users */
1287 va_start(vd.vd_args, pattern);
1288 ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1291 /* Build buffer to send to servers */
1292 va_start(vd.vd_args, pattern);
1293 ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1296 /* send buffer along */
1298 for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1299 if (cptr->from == one || IsServer(cptr) || !match_it(cptr, to, who) ||
1300 cptr->from->fd < 0 || sentalong[cptr->from->fd] == sentalong_marker)
1301 continue; /* skip it */
1303 if (MyConnect(cptr)) /* send right buffer */
1304 send_buffer(cptr, userbuf);
1306 send_buffer(cptr, servbuf);
1311 * Send a server notice to all users subscribing to the indicated <mask>
1314 void sendto_opmask_butone(struct Client *one, unsigned int mask,
1315 const char *pattern, ...)
1319 va_start(vl, pattern);
1320 vsendto_opmask_butone(one, mask, pattern, vl);
1325 * Same as above, except called with a variable argument list
1327 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
1328 const char *pattern, va_list vl)
1331 char sndbuf[IRC_BUFSIZE];
1332 int i = 0; /* so that 1 points to opsarray[0] */
1333 struct SLink *opslist;
1335 while ((mask >>= 1))
1338 if (!(opslist = opsarray[i]))
1342 * build string; I don't want to bother with client nicknames, so I hope
1345 vd.vd_format = pattern;
1347 ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s " MSG_NOTICE
1348 " * :*** Notice -- %v", me.name, &vd);
1350 for (; opslist; opslist = opslist->next)
1351 send_buffer(opslist->value.cptr, sndbuf);