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 sendmsgto_channel_butone(struct Client *one, struct Client *from,
335 struct Channel *chptr, const char *sender,
336 const char *cmd, const char *chname, const char *msg)
339 * Sends a PRIVMSG/NOTICE to all members on a channel but 'one', translating
340 * TOKENS to full messages when sent to local clients. --Gte (12/12/99)
342 struct Membership* member;
343 struct Client *acptr;
351 * Precalculate the buffers we sent to the clients instead of doing an
352 * expensive sprintf() per member that we send to. We still have to
353 * use strcpy() which is evil.
355 if (IsServer(from)) {
356 sprintf(userbuf,":%s %s %s :%s",
357 from->name, ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
358 sprintf(servbuf,"%s %s %s :%s", NumServ(from), cmd, chname, msg);
361 sprintf(userbuf,":%s!%s@%s %s %s :%s",
362 from->name, from->user->username, from->user->host,
363 ('P' == *cmd) ? MSG_PRIVATE : MSG_NOTICE, chname, msg);
364 sprintf(servbuf,"%s%s %s %s :%s", NumNick(from), cmd, chname, msg);
368 for (member = chptr->members; member; member = member->next_member)
370 acptr = member->user;
371 /* ...was the one I should skip */
372 if (acptr->from == one || IsZombie(member) || IsDeaf(acptr))
374 if (MyConnect(acptr)) { /* (It is always a client) */
376 strcpy(sendbuf,userbuf);
378 sendbufto_one(acptr);
380 else if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
382 sentalong[i] = sentalong_marker;
383 if (!IsBurstOrBurstAck(acptr->from)) {
385 strcpy(sendbuf,servbuf);
387 sendbufto_one(acptr);
389 } /* of if MyConnect() */
390 } /* of for(members) */
393 /* See sendcmdto_channel_butone, below */
394 void sendto_lchanops_butone(struct Client *one, struct Client *from, struct Channel *chptr,
395 const char* pattern, ...)
398 struct Membership* member;
399 struct Client *acptr;
401 va_start(vl, pattern);
403 for (member = chptr->members; member; member = member->next_member)
405 acptr = member->user;
406 /* ...was the one I should skip */
407 if (acptr == one || !IsChanOp(member) || IsZombie(member) || IsDeaf(acptr))
409 if (MyConnect(acptr)) /* (It is always a client) */
410 vsendto_prefix_one(acptr, from, pattern, vl);
416 /* See sendcmdto_channel_butone, below */
417 void sendto_chanopsserv_butone(struct Client *one, struct Client *from, struct Channel *chptr,
418 const char* pattern, ...)
421 struct Membership* member;
422 struct Client *acptr;
431 va_start(vl, pattern);
434 for (member = chptr->members; member; member = member->next_member)
436 acptr = member->user;
437 if (acptr->from == acptr || /* Skip local clients */
439 Protocol(acptr->from) < 10 || /* Skip P09 links */
441 acptr->from == one || /* ...was the one I should skip */
442 !IsChanOp(member) || /* Skip non chanops */
443 IsZombie(member) || IsDeaf(acptr))
445 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
447 sentalong[i] = sentalong_marker;
448 /* Don't send channel messages to links that are
449 still eating the net.burst: -- Run 2/1/1997 */
450 if (!IsBurstOrBurstAck(acptr->from))
451 vsendto_prefix_one(acptr, from, pattern, vl);
456 /* Send message to all 2.9 servers */
457 /* This is a hack, because it assumes that we know how `vl' is build up */
458 source = va_arg(vl, char *);
459 tp = va_arg(vl, char *); /* Channel */
460 msg = va_arg(vl, char *);
461 for (member = chptr->members; member; member = member->next_member)
463 acptr = member->user;
464 if (acptr->from == acptr || /* Skip local clients */
465 Protocol(acptr->from) > 9 || /* Skip P10 servers */
466 acptr->from == one || /* ...was the one I should skip */
467 !IsChanOp(member) || /* Skip non chanops */
468 IsZombie(member) || IsDeaf(acptr))
470 if (-1 < (i = acptr->from->fd) && sentalong[i] != sentalong_marker)
472 sentalong[i] = sentalong_marker;
473 /* Don't send channel messages to links that are
474 still eating the net.burst: -- Run 2/1/1997 */
475 if (!IsBurstOrBurstAck(acptr->from))
477 struct Membership* other_member;
478 struct Client* acptr2;
481 /* Find all chanops in this direction: */
482 for (other_member = chptr->members; other_member; other_member = other_member->next_member)
484 acptr2 = other_member->user;
485 if (acptr2->from == acptr->from && acptr2->from != one &&
486 IsChanOp(other_member) && !IsZombie(other_member) &&
489 int len = strlen(acptr2->name);
490 if (tp + len + 2 > target + sizeof(target))
492 sendto_prefix_one(acptr, from,
493 ":%s NOTICE %s :%s", source, target, msg);
499 strcpy(tp, acptr2->name);
503 sendto_prefix_one(acptr, from,
504 ":%s NOTICE %s :%s", source, target, msg);
517 * Send a message to all connected servers except the client 'one'.
519 /* See sendcmdto_serv_butone, below */
520 void sendto_serv_butone(struct Client *one, const char* pattern, ...)
525 va_start(vl, pattern);
526 vsprintf_irc(sendbuf, pattern, vl);
529 for (lp = me.serv->down; lp; lp = lp->next)
531 if (one && lp->value.cptr == one->from)
533 sendbufto_one(lp->value.cptr);
539 * sendbufto_serv_butone()
541 * Send prepared sendbuf to all connected servers except the client 'one'
542 * -Ghostwolf 18-May-97
544 void sendbufto_serv_butone(struct Client *one)
548 for (lp = me.serv->down; lp; lp = lp->next)
550 if (one && lp->value.cptr == one->from)
552 sendbufto_one(lp->value.cptr);
558 * sendto_common_channels()
560 * Sends a message to all people (inclusing `acptr') on local server
561 * who are in same channel with client `acptr'.
563 /* See sendcmdto_common_channels, below */
564 void sendto_common_channels(struct Client *acptr, const char* pattern, ...)
567 struct Membership* chan;
568 struct Membership* member;
571 assert(0 != acptr->from);
572 assert(0 != pattern);
574 va_start(vl, pattern);
577 if (-1 < acptr->from->fd)
578 sentalong[acptr->from->fd] = sentalong_marker;
580 * loop through acptr's channels, and the members on their channels
583 for (chan = acptr->user->channel; chan; chan = chan->next_channel) {
584 for (member = chan->channel->members; member; member = member->next_member) {
585 struct Client *cptr = member->user;
587 if (MyConnect(cptr) &&
588 -1 < (i = cptr->fd) && sentalong[i] != sentalong_marker) {
589 sentalong[i] = sentalong_marker;
590 vsendto_prefix_one(cptr, acptr, pattern, vl);
595 if (MyConnect(acptr))
596 vsendto_prefix_one(acptr, acptr, pattern, vl);
602 * sendto_channel_butserv
604 * Send a message to all members of a channel that
605 * are connected to this server.
607 * This contains a subtle bug; after the first call to vsendto_prefix_one()
608 * below, vl is in an indeterminate state, according to ANSI; we'd have to
609 * move va_start() and va_end() into the loop to correct the problem. It's
610 * easier, however, just to use sendcmdto_channel_butserv(), which builds a
611 * buffer and sends that prepared buffer to each channel member.
613 /* See sendcmdto_channel_butserv, below */
614 void sendto_channel_butserv(struct Channel *chptr, struct Client *from, const char* pattern, ...)
617 struct Membership* member;
618 struct Client *acptr;
620 va_start(vl, pattern);
622 for (member = chptr->members; member; member = member->next_member) {
623 acptr = member->user;
624 if (MyConnect(acptr) && !IsZombie(member))
625 vsendto_prefix_one(acptr, from, pattern, vl);
632 * Send a msg to all ppl on servers/hosts that match a specified mask
633 * (used for enhanced PRIVMSGs)
635 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
638 static int match_it(struct Client *one, const char *mask, int what)
643 return (match(mask, one->user->host) == 0);
646 return (match(mask, one->user->server->name) == 0);
651 * sendto_match_butone
653 * Send to all clients which match the mask in a way defined on 'what';
654 * either by user hostname or user servername.
656 /* See sendcmdto_match_butone, below */
657 void sendto_match_butone(struct Client *one, struct Client *from,
658 const char *mask, int what, const char* pattern, ...)
662 struct Client *cptr, *acptr;
664 va_start(vl, pattern);
665 for (i = 0; i <= HighestFd; i++)
667 if (!(cptr = LocalClientArray[i]))
668 continue; /* that clients are not mine */
669 if (cptr == one) /* must skip the origin !! */
673 for (acptr = GlobalClientList; acptr; acptr = acptr->next)
674 if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
676 /* a person on that server matches the mask, so we
677 * send *one* msg to that server ...
681 /* ... but only if there *IS* a matching person */
683 /* my client, does he match ? */
684 else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
686 vsendto_prefix_one(cptr, from, pattern, vl);
696 * Sends message to the list indicated by the bitmask field.
697 * Don't try to send to more than one list! That is not supported.
700 #ifdef OLD_VSENDTO_OP_MASK
701 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
703 static char fmt[1024];
705 int i = 0; /* so that 1 points to opsarray[0] */
706 struct SLink *opslist;
710 if (!(opslist = opsarray[i]))
713 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
716 strcpy(fmt_target, opslist->value.cptr->name);
717 strcat(fmt_target, " :*** Notice -- ");
718 strcat(fmt_target, pattern);
719 vsendto_one(opslist->value.cptr, fmt, vl);
720 opslist = opslist->next;
724 #else /* !OLD_VSENDTO_OP_MASK */
725 void vsendto_op_mask(unsigned int mask, const char *pattern, va_list vl)
727 vsendto_opmask_butone(0, mask, pattern, vl);
729 #endif /* OLD_VSENDTO_OP_MASK */
734 * Send a prepared sendbuf to the list indicated by the bitmask field.
735 * Ghostwolf 16-May-97
737 void sendbufto_op_mask(unsigned int mask)
739 int i = 0; /* so that 1 points to opsarray[0] */
740 struct SLink *opslist;
743 if (!(opslist = opsarray[i]))
747 sendbufto_one(opslist->value.cptr);
748 opslist = opslist->next;
757 * Send to *local* ops only.
759 /* See vsendto_opmask_butone, below */
760 void vsendto_ops(const char *pattern, va_list vl)
767 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
769 for (i = 0; i <= HighestFd; i++)
770 if ((cptr = LocalClientArray[i]) && !IsServer(cptr) &&
771 SendServNotice(cptr))
773 strcpy(fmt_target, cptr->name);
774 strcat(fmt_target, " :*** Notice -- ");
775 strcat(fmt_target, pattern);
776 vsendto_one(cptr, fmt, vl);
780 /* See sendto_opmask_butone, below */
781 void sendto_op_mask(unsigned int mask, const char *pattern, ...)
784 va_start(vl, pattern);
785 vsendto_op_mask(mask, pattern, vl);
789 /* See sendto_opmask_butone, below */
790 void sendto_ops(const char *pattern, ...)
793 va_start(vl, pattern);
794 vsendto_op_mask(SNO_OLDSNO, pattern, vl);
801 * Send message to all operators.
802 * one - client not to send message to
803 * from- client which message is from *NEVER* NULL!!
805 void sendto_ops_butone(struct Client *one, struct Client *from, const char *pattern, ...)
811 va_start(vl, pattern);
813 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
815 if (!SendWallops(cptr))
817 i = cptr->from->fd; /* find connection oper is on */
818 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
820 if (cptr->from == one)
821 continue; /* ...was the one I should skip */
822 sentalong[i] = sentalong_marker;
823 vsendto_prefix_one(cptr->from, from, pattern, vl);
831 * sendto_g_serv_butone
833 * Send message to all remote +g users (server links).
835 * one - server not to send message to.
837 void sendto_g_serv_butone(struct Client *one, const char *pattern, ...)
843 va_start(vl, pattern);
845 vsprintf_irc(sendbuf, pattern, vl);
846 for (cptr = GlobalClientList; cptr; cptr = cptr->next)
848 if (!SendDebug(cptr))
850 i = cptr->from->fd; /* find connection user is on */
851 if (i < 0 || sentalong[i] == sentalong_marker) /* sent message along it already ? */
855 sentalong[i] = sentalong_marker;
856 if (cptr->from == one)
868 * to - destination client
869 * from - client which message is from
871 * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
874 /* See sendcmdto_one, below */
875 void sendto_prefix_one(struct Client *to, struct Client *from, const char *pattern, ...)
878 va_start(vl, pattern);
879 vsendto_prefix_one(to, from, pattern, vl);
886 * Send to *local* ops only but NOT +s nonopers.
888 /* See sendto_opmask_butone, below */
889 void sendto_realops(const char *pattern, ...)
893 va_start(vl, pattern);
894 vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
901 * Send message to all servers of protocol 'p' and lower.
903 /* See sendcmdto_serv_butone, below */
904 void sendto_lowprot_butone(struct Client *cptr, int p, const char *pattern, ...)
908 va_start(vl, pattern);
909 for (lp = me.serv->down; lp; lp = lp->next)
910 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
911 vsendto_one(lp->value.cptr, pattern, vl);
916 * Send message to all servers of protocol 'p' and higher.
918 /* See sendcmdto_serv_butone, below */
919 void sendto_highprot_butone(struct Client *cptr, int p, const char *pattern, ...)
923 va_start(vl, pattern);
924 for (lp = me.serv->down; lp; lp = lp->next)
925 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
926 vsendto_one(lp->value.cptr, pattern, vl);
931 * Send a raw command to a single client; use *ONLY* if you absolutely
932 * must send a command without a prefix.
934 void sendrawto_one(struct Client *to, const char *pattern, ...)
936 char sndbuf[IRC_BUFSIZE];
939 va_start(vl, pattern);
940 ircd_vsnprintf(to, sndbuf, sizeof(sndbuf) - 2, pattern, vl);
943 send_buffer(to, sndbuf);
947 * Send a (prefixed) command to a single client; select which of <cmd>
948 * <tok> to use depending on if to is a server or not. <from> is the
949 * originator of the command.
951 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
952 struct Client *to, const char *pattern, ...)
956 va_start(vl, pattern);
957 vsendcmdto_one(from, cmd, tok, to, pattern, vl);
961 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
962 struct Client *to, const char *pattern, va_list vl)
965 char sndbuf[IRC_BUFSIZE];
969 vd.vd_format = pattern; /* set up the struct VarData for %v */
972 ircd_snprintf(to, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from,
973 IsServer(to) || IsMe(to) ? tok : cmd, &vd);
975 send_buffer(to, sndbuf);
979 * Send a (prefixed) command to all servers but one, using tok; cmd
980 * is ignored in this particular function.
982 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
983 const char *tok, struct Client *one,
984 const char *pattern, ...)
987 char sndbuf[IRC_BUFSIZE];
990 vd.vd_format = pattern; /* set up the struct VarData for %v */
991 va_start(vd.vd_args, pattern);
994 ircd_snprintf(&me, sndbuf, sizeof(sndbuf) - 2, "%C %s %v", from, tok, &vd);
997 /* send it to our downlinks */
998 for (lp = me.serv->down; lp; lp = lp->next) {
999 if (one && lp->value.cptr == one->from)
1001 send_buffer(lp->value.cptr, sndbuf);
1006 * Send a (prefix) command originating from <from> to all channels
1007 * <from> is locally on. <from> must be a user. <tok> is ignored in
1010 /* XXX sentalong_marker used XXX
1012 * There is not an easy way to revoke the need for sentalong_marker
1013 * from this function. Thoughts and ideas would be welcome... -Kev
1015 * One possibility would be to internalize the sentalong array; that
1016 * could be prohibitively big, though. We could get around that by
1017 * making one that's the number of connected servers or something...
1018 * or perhaps by adding a special flag to the servers we've sent a
1019 * message to, and then a final loop through the connected servers
1020 * to delete the flag. -Kev
1022 void sendcmdto_common_channels(struct Client *from, const char *cmd,
1023 const char *tok, const char *pattern, ...)
1026 char sndbuf[IRC_BUFSIZE];
1027 struct Membership *chan;
1028 struct Membership *member;
1031 assert(0 != from->from);
1032 assert(0 != pattern);
1033 assert(!IsServer(from) && !IsMe(from));
1035 vd.vd_format = pattern; /* set up the struct VarData for %v */
1037 va_start(vd.vd_args, pattern);
1039 /* build the buffer */
1040 ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1044 if (-1 < from->from->fd)
1045 sentalong[from->from->fd] = sentalong_marker;
1047 * loop through from's channels, and the members on their channels
1049 for (chan = from->user->channel; chan; chan = chan->next_channel)
1050 for (member = chan->channel->members; member;
1051 member = member->next_member)
1052 if (MyConnect(member->user) && -1 < member->user->from->fd &&
1053 sentalong[member->user->from->fd] != sentalong_marker) {
1054 sentalong[member->user->from->fd] = sentalong_marker;
1055 send_buffer(member->user, sndbuf);
1058 if (MyConnect(from))
1059 send_buffer(from, sndbuf);
1063 * Send a (prefixed) command to all local users on the channel specified
1064 * by <to>; <tok> is ignored by this function
1066 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
1067 const char *tok, struct Channel *to,
1068 const char *pattern, ...)
1071 char sndbuf[IRC_BUFSIZE];
1072 struct Membership *member;
1074 vd.vd_format = pattern; /* set up the struct VarData for %v */
1075 va_start(vd.vd_args, pattern);
1077 /* build the buffer */
1078 ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1081 /* send the buffer to each local channel member */
1082 for (member = to->members; member; member = member->next_member) {
1083 if (MyConnect(member->user) && !IsZombie(member))
1084 send_buffer(member->user, sndbuf);
1089 * Send a (prefixed) command to all users on this channel, including
1090 * remote users; users to skip may be specified by setting appropriate
1091 * flags in the <skip> argument. <one> will also be skipped.
1093 /* XXX sentalong_marker used XXX
1095 * We can drop sentalong_marker from this function by adding a field to
1096 * channels and to connections; what we do is make a user's connection
1097 * a "member" of the channel by adding it to the new list, and we use
1098 * the struct Membership status as a reference count. Then, to implement
1099 * this function, we just walk the list of connections. Unfortunately,
1100 * this doesn't account for sending only to channel ops, or for not
1101 * sending to +d users; we could account for that by splitting those
1102 * counts out, but that would imply adding two more fields (at least) to
1103 * the struct Membership... -Kev
1105 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
1106 const char *tok, struct Channel *to,
1107 struct Client *one, unsigned int skip,
1108 const char *pattern, ...)
1110 struct Membership *member;
1112 char userbuf[IRC_BUFSIZE];
1113 char servbuf[IRC_BUFSIZE];
1115 vd.vd_format = pattern;
1117 /* Build buffer to send to users */
1118 va_start(vd.vd_args, pattern);
1119 ircd_snprintf(0, userbuf, sizeof(userbuf) - 2,
1120 skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v", from,
1121 skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
1124 /* Build buffer to send to servers */
1125 va_start(vd.vd_args, pattern);
1126 ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1129 /* send buffer along! */
1131 for (member = to->members; member; member = member->next_member) {
1132 /* skip one, zombies, and deaf users... */
1133 if (member->user->from == one || IsZombie(member) ||
1134 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
1135 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
1136 (skip & SKIP_BURST && IsBurstOrBurstAck(member->user->from)) ||
1137 member->user->from->fd < 0 ||
1138 sentalong[member->user->from->fd] == sentalong_marker)
1140 sentalong[member->user->from->fd] = sentalong_marker;
1142 if (MyConnect(member->user)) /* pick right buffer to send */
1143 send_buffer(member->user, userbuf);
1145 send_buffer(member->user, servbuf);
1150 * Send a (prefixed) command to all users except <one> that have
1153 /* XXX sentalong_marker used XXX
1155 * Again, we can solve this use of sentalong_marker by adding a field
1156 * to connections--a count of the number of +w users, and another count
1157 * of +g users. Then, just walk through the local clients to send
1158 * those messages, and another walk through the connected servers list,
1159 * sending only if there's a non-zero count. No caveats here, either,
1160 * beyond remembering to decrement the count when a user /quit's or is
1161 * killed, or a server is squit. -Kev
1163 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
1164 const char *tok, struct Client *one,
1165 unsigned int flag, const char *pattern, ...)
1168 struct Client *cptr;
1169 char userbuf[IRC_BUFSIZE];
1170 char servbuf[IRC_BUFSIZE];
1172 vd.vd_format = pattern;
1174 /* Build buffer to send to users */
1175 va_start(vd.vd_args, pattern);
1176 ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C " MSG_WALLOPS " %v",
1180 /* Build buffer to send to servers */
1181 va_start(vd.vd_args, pattern);
1182 ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1185 /* send buffer along! */
1187 for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1188 if (cptr->from == one || IsServer(cptr) || !(cptr->flags & flag) ||
1189 cptr->from->fd < 0 || sentalong[cptr->from->fd] == sentalong_marker)
1190 continue; /* skip it */
1191 sentalong[cptr->from->fd] = sentalong_marker;
1193 if (MyConnect(cptr)) /* send right buffer */
1194 send_buffer(cptr, userbuf);
1196 send_buffer(cptr, servbuf);
1201 * Send a (prefixed) command to all users who match <to>, under control
1204 /* XXX sentalong_marker used XXX
1206 * This is also a difficult one to solve. The basic approach would be
1207 * to walk the client list of each connected server until we find a
1208 * match--but then, we also have to walk the client list of all the
1209 * servers behind that one. We could implement this recursively--or we
1210 * could add (yet another) field to the connection struct that would be
1211 * a linked list of clients introduced through that link, and just walk
1212 * that, making this into an iterative implementation. Unfortunately,
1213 * we probably would not be able to use tail recursion for the recursive
1214 * solution, so a deep network could exhaust our stack space; therefore
1215 * I favor the extra linked list, even though that increases the
1216 * complexity of the database. -Kev
1218 void sendcmdto_match_butone(struct Client *from, const char *cmd,
1219 const char *tok, const char *to,
1220 struct Client *one, unsigned int who,
1221 const char *pattern, ...)
1224 struct Client *cptr;
1225 char userbuf[IRC_BUFSIZE];
1226 char servbuf[IRC_BUFSIZE];
1228 vd.vd_format = pattern;
1230 /* Build buffer to send to users */
1231 va_start(vd.vd_args, pattern);
1232 ircd_snprintf(0, userbuf, sizeof(userbuf) - 2, "%:#C %s %v", from, cmd, &vd);
1235 /* Build buffer to send to servers */
1236 va_start(vd.vd_args, pattern);
1237 ircd_snprintf(&me, servbuf, sizeof(servbuf) - 2, "%C %s %v", from, tok, &vd);
1240 /* send buffer along */
1242 for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
1243 if (cptr->from == one || IsServer(cptr) || IsMe(cptr) ||
1244 !match_it(cptr, to, who) || cptr->from->fd < 0 ||
1245 sentalong[cptr->from->fd] == sentalong_marker)
1246 continue; /* skip it */
1247 sentalong[cptr->from->fd] = sentalong_marker;
1249 if (MyConnect(cptr)) /* send right buffer */
1250 send_buffer(cptr, userbuf);
1252 send_buffer(cptr, servbuf);
1257 * Send a server notice to all users subscribing to the indicated <mask>
1260 void sendto_opmask_butone(struct Client *one, unsigned int mask,
1261 const char *pattern, ...)
1265 va_start(vl, pattern);
1266 vsendto_opmask_butone(one, mask, pattern, vl);
1271 * Same as above, except called with a variable argument list
1273 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
1274 const char *pattern, va_list vl)
1277 char sndbuf[IRC_BUFSIZE];
1278 int i = 0; /* so that 1 points to opsarray[0] */
1279 struct SLink *opslist;
1281 while ((mask >>= 1))
1284 if (!(opslist = opsarray[i]))
1288 * build string; I don't want to bother with client nicknames, so I hope
1291 vd.vd_format = pattern;
1293 ircd_snprintf(0, sndbuf, sizeof(sndbuf) - 2, ":%s " MSG_NOTICE
1294 " * :*** Notice -- %v", me.name, &vd);
1296 for (; opslist; opslist = opslist->next)
1297 send_buffer(opslist->value.cptr, sndbuf);