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_opmask_butone(0, SNO_OLDSNO, "%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_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
238 "%zu > %zu", to, DBufLength(&to->sendQ),
240 dead_link(to, "Max sendQ exceeded");
244 Debug((DEBUG_SEND, "Sending [%s] to %s", buf, to->name));
247 if (buf[len - 1] != '\n') {
255 if (0 == dbuf_put(&to->sendQ, buf, len)) {
256 dead_link(to, "Buffer allocation error");
261 send_to_god(to, buf);
264 * Update statistics. The following is slightly incorrect
265 * because it counts messages even if queued, but bytes
266 * only really sent. Queued bytes get updated in SendQueued.
271 * This little bit is to stop the sendQ from growing too large when
272 * there is no need for it to. Thus we call send_queued() every time
273 * 2k has been added to the queue since the last non-fatal write.
274 * Also stops us from deliberately building a large sendQ and then
275 * trying to flood that link with data (possible during the net
276 * relinking done by servers with a large load).
278 if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
282 void sendbufto_one(struct Client* to)
284 send_buffer(to, sendbuf);
287 /* See vsendcmdto_one, below */
288 static void vsendto_prefix_one(struct Client *to, struct Client *from,
289 const char* pattern, va_list vl)
291 if (to && from && MyUser(to) && IsUser(from))
293 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
296 struct User *user = from->user;
298 par = va_arg(vl, char *);
299 strcpy(sender, from->name);
305 strcat(sender, user->username);
307 if (*user->host && !MyConnect(from))
310 strcat(sender, user->host);
315 * Flag is used instead of strchr(sender, '@') for speed and
316 * also since username/nick may have had a '@' in them. -avalon
318 if (!flag && MyConnect(from) && *user->host)
321 strcat(sender, from->sockhost);
324 strcpy(&sendbuf[1], sender);
325 /* Assuming 'pattern' always starts with ":%s ..." */
326 vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
329 vsprintf_irc(sendbuf, pattern, vl);
333 /* See sendcmdto_channel_butone, below */
334 void sendto_channel_butone(struct Client *one, struct Client *from, struct Channel *chptr,
335 const char* pattern, ...)
338 struct Membership* member;
339 struct Client *acptr;
342 va_start(vl, pattern);
345 for (member = chptr->members; member; member = member->next_member)
347 acptr = member->user;
348 /* ...was the one I should skip */
349 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
351 if (MyConnect(acptr)) /* (It is always a client) */
352 vsendto_prefix_one(acptr, from, pattern, vl);
353 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
355 sentalong[i] = sentalong_marker;
357 * Don't send channel messages to links that are still eating
358 * the net.burst: -- Run 2/1/1997
360 if (!IsBurstOrBurstAck(acptr->from))
361 vsendto_prefix_one(acptr, from, pattern, vl);
367 /* See sendcmdto_channel_butone, below */
368 void sendmsgto_channel_butone(struct Client *one, struct Client *from,
369 struct Channel *chptr, const char *sender,
370 const char *cmd, const char *chname, const char *msg)
373 * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
374 * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
376 struct Membership* member;
377 struct Client *acptr;
385 * Precalculate the buffers we sent to the clients instead of doing an
386 * expensive sprintf() per member that we send to. We still have to
387 * use strcpy() which is evil.
389 if (IsServer(from)) {
390 sprintf(userbuf,":%s %s %s :%s",
391 from->name, ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
392 sprintf(servbuf,"%s %s %s :%s", NumServ(from), cmd, chname, msg);
395 sprintf(userbuf,":%s!%s@%s %s %s :%s",
396 from->name, from->user->username, from->user->host,
397 ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
398 sprintf(servbuf,"%s%s %s %s :%s", NumNick(from), cmd, chname, msg);
402 for (member = chptr->members; member; member = member->next_member)
404 acptr = member->user;
405 /* ...was the one I should skip */
406 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
408 if (MyConnect(acptr)) { /* (It is always a client) */
410 strcpy(sendbuf,userbuf);
412 sendbufto_one(acptr);
414 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
416 sentalong[i] = sentalong_marker;
417 if (!IsBurstOrBurstAck(acptr->from)) {
419 strcpy(sendbuf,servbuf);
421 sendbufto_one(acptr);
423 } /* of if MyConnect() */
424 } /* of for(members) */
427 /* See sendcmdto_channel_butone, below */
428 void sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
429 const char* pattern, ...)
432 struct Membership* member;
433 struct Client *acptr;
435 va_start(vl, pattern);
437 for (member = chptr->members; member; member = member->next_member)
439 acptr = member->user;
440 /* ...was the one I should skip */
441 if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
443 if (MyConnect(acptr)) /* (It is always a client) */
444 vsendto_prefix_one(acptr, from, pattern, vl);
450 /* See sendcmdto_channel_butone, below */
451 void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
452 const char* pattern, ...)
455 struct Membership* member;
456 struct Client *acptr;
465 va_start(vl, pattern);
468 for (member = chptr->members; member; member = member->next_member)
470 acptr = member->user;
471 if (acptr->from == acptr || /* Skip local clients */
473 Protocol(acptr->from) < 10 || /* Skip P09 links */
475 acptr->from == one || /* ...was the one I should skip */
476 !IsChanOp(member) || /* Skip non chanops */
477 IsZombie(member) || IsDeaf(acptr))
479 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
481 sentalong[i] = sentalong_marker;
482 /* Don't send channel messages to links that are
483 still eating the net.burst: -- Run 2/1/1997 */
484 if (!IsBurstOrBurstAck(acptr->from))
485 vsendto_prefix_one(acptr, from, pattern, vl);
490 /* Send message to all 2.9 servers */
491 /* This is a hack, because it assumes that we know how `vl' is build up */
492 source = va_arg(vl, char *);
493 tp = va_arg(vl, char *); /* Channel */
494 msg = va_arg(vl, char *);
495 for (member = chptr->members; member; member = member->next_member)
497 acptr = member->user;
498 if (acptr->from == acptr || /* Skip local clients */
499 Protocol(acptr->from) > 9 || /* Skip P10 servers */
500 acptr->from == one || /* ...was the one I should skip */
501 !IsChanOp(member) || /* Skip non chanops */
502 IsZombie(member) || IsDeaf(acptr))
504 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
506 sentalong[i] = sentalong_marker;
507 /* Don't send channel messages to links that are
508 still eating the net.burst: -- Run 2/1/1997 */
509 if (!IsBurstOrBurstAck(acptr->from))
511 struct Membership* other_member;
512 struct Client* acptr2;
515 /* Find all chanops in this direction: */
516 for (other_member = chptr->members; other_member; other_member = other_member->next_member)
518 acptr2 = other_member->user;
519 if (acptr2->from == acptr->from && acptr2->from != one &&
520 IsChanOp(other_member) && !IsZombie(other_member) &&
523 int len = strlen(acptr2->name);
524 if (tp + len + 2 > target + sizeof(target))
526 sendto_prefix_one(acptr, from,
527 ":%s NOTICE %s :%s", source, target, msg);
533 strcpy(tp, acptr2->name);
537 sendto_prefix_one(acptr, from,
538 ":%s NOTICE %s :%s", source, target, msg);
551 * Send a message to all connected servers except the client 'one'.
553 /* See sendcmdto_serv_butone, below */
554 void sendto_serv_butone(struct Client *one, const char* pattern, ...)
559 va_start(vl, pattern);
560 vsprintf_irc(sendbuf, pattern, vl);
563 for (lp = me.serv->down; lp; lp = lp->next)
565 if (one && lp->value.cptr == one->from)
567 sendbufto_one(lp->value.cptr);
573 * sendbufto_serv_butone()
575 * Send prepared sendbuf to all connected servers except the client 'one'
576 * -Ghostwolf 18-May-97
578 void sendbufto_serv_butone(struct Client *one)
582 for (lp = me.serv->down; lp; lp = lp->next)
584 if (one && lp->value.cptr == one->from)
586 sendbufto_one(lp->value.cptr);
592 * sendto_common_channels()
594 * Sends a message to all people (inclusing `acptr') on local server
595 * who are in same channel with client `acptr'.
597 /* See sendcmdto_common_channels, below */
598 void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
601 struct Membership* chan;
602 struct Membership* member;
605 assert(0 != acptr->from);
606 assert(0 != pattern);
608 va_start(vl, pattern);
611 if (-1 < acptr->from->fd)
612 sentalong[acptr->from->fd] = sentalong_marker;
614 * loop through acptr's channels, and the members on their channels
617 for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
618 for (member = chan->channel->members; member; member = member->next_member) {
619 struct Client *cptr = member->user;
621 if (MyConnect(cptr) &&
622 -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
623 sentalong[i] = sentalong_marker;
624 vsendto_prefix_one(cptr, acptr, pattern, vl);
629 if (MyConnect(acptr))
630 vsendto_prefix_one(acptr, acptr, pattern, vl);
636 * sendto_channel_butserv
638 * Send a message to all members of a channel that
639 * are connected to this server.
641 * This contains a subtle bug; after the first call to vsendto_prefix_one()
642 * below, vl is in an indeterminate state, according to ANSI; we'd have to
643 * move va_start() and va_end() into the loop to correct the problem. It's
644 * easier, however, just to use sendcmdto_channel_butserv(), which builds a
645 * buffer and sends that prepared buffer to each channel member.
647 /* See sendcmdto_channel_butserv, below */
648 void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
651 struct Membership* member;
652 struct Client *acptr;
654 va_start(vl, pattern);
656 for (member = chptr->members; member; member = member->next_member) {
657 acptr = member->user;
658 if (MyConnect(acptr) && !IsZombie(member))
659 vsendto_prefix_one(acptr, from, pattern, vl);
666 * Send a msg to all ppl on servers/hosts that match a specified mask
667 * (used for enhanced PRIVMSGs)
669 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
672 static int match_it(struct Client *one, const char *mask, int what)
677 return (match(mask, one->user->host) == 0);
680 return (match(mask, one->user->server->name) == 0);
685 * sendto_match_butone
687 * Send to all clients which match the mask in a way defined on 'what';
688 * either by user hostname or user servername.
690 /* See sendcmdto_match_butone, below */
691 void sendto_match_butone(struct Client *one, struct Client *from,
692 const char *mask, int what, const char* pattern, ...)
696 struct Client *cptr, *acptr;
698 va_start(vl, pattern);
699 for (i = 0; i <= HighestFd; i++)
701 if (!(cptr = LocalClientArray[i]))
702 continue; /* that clients are not mine */
703 if (cptr == one) /* must skip the origin !! */
707 for (acptr = GlobalClientList; acptr; acptr = acptr->next)
708 if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
710 /* a person on that server matches the mask, so we
711 * send *one* msg to that server ...
715 /* ... but only if there *IS* a matching person */
717 /* my client, does he match ? */
718 else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
720 vsendto_prefix_one(cptr, from, pattern, vl);
730 * Send to *local* ops but one.
732 /* See sendto_opmask_butone, below */
733 void sendto_lops_butone(struct Client* one, const char* pattern, ...)
737 struct Client** clients = me.serv->client_list;
741 assert(0 != clients);
743 sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
744 va_start(vl, pattern);
745 vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
748 for (i = 0; i <= me.serv->nn_mask; ++i) {
749 if ((cptr = clients[i]) && cptr != one && SendServNotice(cptr)) {
750 sprintf_irc(sendbuf, nbuf, cptr->name);
759 * Sends message to the list indicated by the bitmask field.
760 * Don't try to send to more than one list! That is not supported.
763 #ifdef OLD_VSENDTO_OP_MASK
764 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
766 static char fmt[1024];
768 int i = 0; /* so that 1 points to opsarray[0] */
769 struct SLink *opslist;
773 if (!(opslist = opsarray[i]))
776 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
779 strcpy(fmt_target, opslist->value.cptr->name);
780 strcat(fmt_target, " :*** Notice -- ");
781 strcat(fmt_target, pattern);
782 vsendto_one(opslist->value.cptr, fmt, vl);
783 opslist = opslist->next;
787 #else /* !OLD_VSENDTO_OP_MASK */
788 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
790 vsendto_opmask_butone(0, mask, pattern, vl);
792 #endif /* OLD_VSENDTO_OP_MASK */
797 * Send a prepared sendbuf to the list indicated by the bitmask field.
798 * Ghostwolf 16-May-97
800 void sendbufto_op_mask(unsigned int mask)
802 int i = 0; /* so that 1 points to opsarray[0] */
803 struct SLink *opslist;
806 if (!(opslist = opsarray[i]))
810 sendbufto_one(opslist->value.cptr);
811 opslist = opslist->next;
820 * Send to *local* ops only.
822 /* See vsendto_opmask_butone, below */
823 void vsendto_ops(const char *pattern, va_list vl)
830 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
832 for (i = 0; i <= HighestFd; i++)
833 if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
834 SendServNotice(cptr))
836 strcpy(fmt_target, cptr->name);
837 strcat(fmt_target, " :*** Notice -- ");
838 strcat(fmt_target, pattern);
839 vsendto_one(cptr, fmt, vl);
843 /* See sendto_opmask_butone, below */
844 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
847 va_start(vl, pattern);
848 vsendto_op_mask(mask, pattern, vl);
852 /* See sendto_opmask_butone, below */
853 void sendto_ops(const char *pattern, ...)
856 va_start(vl, pattern);
857 vsendto_op_mask(SNO_OLDSNO, pattern, vl);
864 * Send message to all operators.
865 * one - client not to send message to
866 * from- client which message is from *NEVER* NULL!!
868 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
874 va_start(vl, pattern);
876 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
878 if (!SendWallops(cptr))
880 i = cptr->from->fd; /* find connection oper is on */
881 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
883 if (cptr->from == one)
884 continue; /* ...was the one I should skip */
885 sentalong[i] = sentalong_marker;
886 vsendto_prefix_one(cptr->from, from, pattern, vl);
894 * sendto_g_serv_butone
896 * Send message to all remote +g users (server links).
898 * one - server not to send message to.
900 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
906 va_start(vl, pattern);
908 vsprintf_irc(sendbuf, pattern, vl);
909 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
911 if (!SendDebug(cptr))
913 i = cptr->from->fd; /* find connection user is on */
914 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
918 sentalong[i] = sentalong_marker;
919 if (cptr->from == one)
931 * to - destination client
932 * from - client which message is from
934 * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
937 /* See sendcmdto_one, below */
938 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
941 va_start(vl, pattern);
942 vsendto_prefix_one(to, from, pattern, vl);
949 * Send to *local* ops only but NOT +s nonopers.
951 /* See sendto_opmask_butone, below */
952 void sendto_realops(const char *pattern, ...)
956 va_start(vl, pattern);
957 vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
964 * Send message to all servers of protocol 'p' and lower.
966 /* See sendcmdto_serv_butone, below */
967 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
971 va_start(vl, pattern);
972 for (lp = me.serv->down; lp; lp = lp->next)
973 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
974 vsendto_one(lp->value.cptr, pattern, vl);
979 * Send message to all servers of protocol 'p' and higher.
981 /* See sendcmdto_serv_butone, below */
982 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
986 va_start(vl, pattern);
987 for (lp = me.serv->down; lp; lp = lp->next)
988 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
989 vsendto_one(lp->value.cptr, pattern, vl);
994 * Send a raw command to a single client; use *ONLY* if you absolutely
995 * must send a command without a prefix.
997 void sendrawto_one(struct Client *to, const char *pattern, ...)
999 char sndbuf[IRC_BUFSIZE];
1002 va_start(vl, pattern);
1003 ircd_vsnprintf(to, sndbuf, sizeof(sndbuf) - 2, pattern, vl);
1006 send_buffer(to, sndbuf);
1010 * Send a (prefixed) command to a single client; select which of <cmd>
1011 * <tok> to use depending on if to is a server or not. <from> is the
1012 * originator of the command.
1014 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
1015 struct Client *to, const char *pattern, ...)
1019 va_start(vl, pattern);
1020 vsendcmdto_one(from, cmd, tok, to, pattern, vl);
1024 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
1025 struct Client *to, const char *pattern, va_list vl)
1028 char sndbuf[IRC_BUFSIZE];
1032 vd.vd_format = pattern; /* set up the struct VarData for %v */
1035 ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from,
1036 IsServer(to) || IsMe(to) ? tok : cmd, &vd);
1038 send_buffer(to, sndbuf);
1042 * Send a (prefixed) command to all servers but one, using tok; cmd
1043 * is ignored in this particular function.
1045 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
1046 const char *tok, struct Client *one,
1047 const char *pattern, ...)
1050 char sndbuf[IRC_BUFSIZE];
1053 vd.vd_format = pattern; /* set up the struct VarData for %v */
1054 va_start(vd.vd_args, pattern);
1057 ircd_snprintf(&me, sndbuf, sizeof(sndbuf) - 2, "%C %s %v", from, tok, &vd);
1060 /* send it to our downlinks */
1061 for (lp = me.serv->down; lp; lp = lp->next) {
1062 if (one && lp->value.cptr == one->from)
1064 send_buffer(lp->value.cptr, sndbuf);
1069 * Send a (prefix) command originating from <from> to all channels
1070 * <from> is locally on. <from> must be a user. <tok> is ignored in
1073 /* XXX sentalong_marker used XXX
1075 * There is not an easy way to revoke the need for sentalong_marker
1076 * from this function. Thoughts and ideas would be welcome... -Kev
1078 * One possibility would be to internalize the sentalong array; that
1079 * could be prohibitively big, though. We could get around that by
1080 * making one that's the number of connected servers or something...
1081 * or perhaps by adding a special flag to the servers we've sent a
1082 * message to, and then a final loop through the connected servers
1083 * to delete the flag. -Kev
1085 void sendcmdto_common_channels(struct Client *from, const char *cmd,
1086 const char *tok, const char *pattern, ...)
1089 char sndbuf[IRC_BUFSIZE];
1090 struct Membership *chan;
1091 struct Membership *member;
1094 assert(0 != from->from);
1095 assert(0 != pattern);
1096 assert(!IsServer(from) && !IsMe(from));
1098 vd.vd_format = pattern; /* set up the struct VarData for %v */
1100 va_start(vd.vd_args, pattern);
1102 /* build the buffer */
1103 ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1107 if (-1 < from->from->fd)
1108 sentalong[from->from->fd] = sentalong_marker;
1110 * loop through from's channels, and the members on their channels
1112 for (chan = from->user->channel; chan; chan = chan->next_channel)
1113 for (member = chan->channel->members; member;
1114 member = member->next_member)
1115 if (MyConnect(member->user) && -1 < member->user->from->fd &&
1116 sentalong[member->user->from->fd] != sentalong_marker) {
1117 sentalong[member->user->from->fd] = sentalong_marker;
1118 send_buffer(member->user, sndbuf);
1121 if (MyConnect(from))
1122 send_buffer(from, sndbuf);
1126 * Send a (prefixed) command to all local users on the channel specified
1127 * by <to>; <tok> is ignored by this function
1129 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
1130 const char *tok, struct Channel *to,
1131 const char *pattern, ...)
1134 char sndbuf[IRC_BUFSIZE];
1135 struct Membership *member;
1137 vd.vd_format = pattern; /* set up the struct VarData for %v */
1138 va_start(vd.vd_args, pattern);
1140 /* build the buffer */
1141 ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1144 /* send the buffer to each local channel member */
1145 for (member = to->members; member; member = member->next_member) {
1146 if (MyConnect(member->user) && !IsZombie(member))
1147 send_buffer(member->user, sndbuf);
1152 * Send a (prefixed) command to all users on this channel, including
1153 * remote users; users to skip may be specified by setting appropriate
1154 * flags in the <skip> argument. <one> will also be skipped.
1156 /* XXX sentalong_marker used XXX
1158 * We can drop sentalong_marker from this function by adding a field to
1159 * channels and to connections; what we do is make a user's connection
1160 * a "member" of the channel by adding it to the new list, and we use
1161 * the struct Membership status as a reference count. Then, to implement
1162 * this function, we just walk the list of connections. Unfortunately,
1163 * this doesn't account for sending only to channel ops, or for not
1164 * sending to +d users; we could account for that by splitting those
1165 * counts out, but that would imply adding two more fields (at least) to
1166 * the struct Membership... -Kev
1168 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
1169 const char *tok, struct Channel *to,
1170 struct Client *one, unsigned int skip,
1171 const char *pattern, ...)
1173 struct Membership *member;
1175 char userbuf[IRC_BUFSIZE];
1176 char servbuf[IRC_BUFSIZE];
1178 vd.vd_format = pattern;
1180 /* Build buffer to send to users */
1181 va_start(vd.vd_args, pattern);
1182 ircd_snprintf(0, userbuf, sizeof(userbuf) - 2,
1183 skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v", from,
1184 skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
1187 /* Build buffer to send to servers */
1188 va_start(vd.vd_args, pattern);
1189 ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1192 /* send buffer along! */
1194 for (member = to->members; member; member = member->next_member) {
1195 /* skip one, zombies, and deaf users... */
1196 if (member->user->from == one || IsZombie(member) ||
1197 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
1198 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
1199 (skip & SKIP_BURST && IsBurstOrBurstAck(member->user->from)) ||
1200 member->user->from->fd < 0 ||
1201 sentalong[member->user->from->fd] == sentalong_marker)
1203 sentalong[member->user->from->fd] = sentalong_marker;
1205 if (MyConnect(member->user)) /* pick right buffer to send */
1206 send_buffer(member->user, userbuf);
1208 send_buffer(member->user, servbuf);
1213 * Send a (prefixed) command to all users except <one> that have
1216 /* XXX sentalong_marker used XXX
1218 * Again, we can solve this use of sentalong_marker by adding a field
1219 * to connections--a count of the number of +w users, and another count
1220 * of +g users. Then, just walk through the local clients to send
1221 * those messages, and another walk through the connected servers list,
1222 * sending only if there's a non-zero count. No caveats here, either,
1223 * beyond remembering to decrement the count when a user /quit's or is
1224 * killed, or a server is squit. -Kev
1226 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
1227 const char *tok, struct Client *one,
1228 unsigned int flag, const char *pattern, ...)
1231 struct Client *cptr;
1232 char userbuf[IRC_BUFSIZE];
1233 char servbuf[IRC_BUFSIZE];
1235 vd.vd_format = pattern;
1237 /* Build buffer to send to users */
1238 va_start(vd.vd_args, pattern);
1239 ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C " MSG_WALLOPS " %v",
1243 /* Build buffer to send to servers */
1244 va_start(vd.vd_args, pattern);
1245 ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1248 /* send buffer along! */
1250 for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1251 if (cptr->from == one || IsServer(cptr) || !(cptr->flags & flag) ||
1252 cptr->from->fd < 0 || sentalong[cptr->from->fd] == sentalong_marker)
1253 continue; /* skip it */
1254 sentalong[cptr->from->fd] = sentalong_marker;
1256 if (MyConnect(cptr)) /* send right buffer */
1257 send_buffer(cptr, userbuf);
1259 send_buffer(cptr, servbuf);
1264 * Send a (prefixed) command to all users who match <to>, under control
1267 /* XXX sentalong_marker used XXX
1269 * This is also a difficult one to solve. The basic approach would be
1270 * to walk the client list of each connected server until we find a
1271 * match--but then, we also have to walk the client list of all the
1272 * servers behind that one. We could implement this recursively--or we
1273 * could add (yet another) field to the connection struct that would be
1274 * a linked list of clients introduced through that link, and just walk
1275 * that, making this into an iterative implementation. Unfortunately,
1276 * we probably would not be able to use tail recursion for the recursive
1277 * solution, so a deep network could exhaust our stack space; therefore
1278 * I favor the extra linked list, even though that increases the
1279 * complexity of the database. -Kev
1281 void sendcmdto_match_butone(struct Client *from, const char *cmd,
1282 const char *tok, const char *to,
1283 struct Client *one, unsigned int who,
1284 const char *pattern, ...)
1287 struct Client *cptr;
1288 char userbuf[IRC_BUFSIZE];
1289 char servbuf[IRC_BUFSIZE];
1291 vd.vd_format = pattern;
1293 /* Build buffer to send to users */
1294 va_start(vd.vd_args, pattern);
1295 ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1298 /* Build buffer to send to servers */
1299 va_start(vd.vd_args, pattern);
1300 ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1303 /* send buffer along */
1305 for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1306 if (cptr->from == one || IsServer(cptr) || IsMe(cptr) ||
1307 !match_it(cptr, to, who) || cptr->from->fd < 0 ||
1308 sentalong[cptr->from->fd] == sentalong_marker)
1309 continue; /* skip it */
1310 sentalong[cptr->from->fd] = sentalong_marker;
1312 if (MyConnect(cptr)) /* send right buffer */
1313 send_buffer(cptr, userbuf);
1315 send_buffer(cptr, servbuf);
1320 * Send a server notice to all users subscribing to the indicated <mask>
1323 void sendto_opmask_butone(struct Client *one, unsigned int mask,
1324 const char *pattern, ...)
1328 va_start(vl, pattern);
1329 vsendto_opmask_butone(one, mask, pattern, vl);
1334 * Same as above, except called with a variable argument list
1336 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
1337 const char *pattern, va_list vl)
1340 char sndbuf[IRC_BUFSIZE];
1341 int i = 0; /* so that 1 points to opsarray[0] */
1342 struct SLink *opslist;
1344 while ((mask >>= 1))
1347 if (!(opslist = opsarray[i]))
1351 * build string; I don't want to bother with client nicknames, so I hope
1354 vd.vd_format = pattern;
1356 ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s " MSG_NOTICE
1357 " * :*** Notice -- %v", me.name, &vd);
1359 for (; opslist; opslist = opslist->next)
1360 send_buffer(opslist->value.cptr, sndbuf);