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.
38 #include "sprintf_irc.h"
43 static int sentalong[MAXCONNECTIONS];
44 static int sentalong_marker;
45 struct SLink *opsarray[32]; /* don't use highest bit unless you change
46 atoi to strtoul in sendto_op_mask() */
55 * An error has been detected. The link *must* be closed,
56 * but *cannot* call ExitClient (m_bye) from here.
57 * Instead, mark it with FLAGS_DEADSOCKET. This should
58 * generate ExitClient from the main loop.
60 * If 'notice' is not NULL, it is assumed to be a format
61 * for a message to local opers. I can contain only one
62 * '%s', which will be replaced by the sockhost field of
65 * Also, the notice is skipped for "uninteresting" cases,
66 * like Persons and yet unknown connections...
69 static void dead_link(aClient *to, char *notice)
71 to->flags |= FLAGS_DEADSOCKET;
73 * If because of BUFFERPOOL problem then clean dbuf's now so that
74 * notices don't hurt operators below.
76 DBufClear(&to->recvQ);
77 DBufClear(&to->sendQ);
79 /* Keep a copy of the last comment, for later use... */
80 strncpy(LastDeadComment(to), notice, sizeof(LastDeadComment(to)));
81 LastDeadComment(to)[sizeof(LastDeadComment(to)) - 1] = 0;
83 if (!IsUser(to) && !IsUnknown(to) && !(to->flags & FLAGS_CLOSING))
84 sendto_ops("%s for %s", LastDeadComment(to), to->name);
85 Debug((DEBUG_ERROR, LastDeadComment(to)));
91 * Used to empty all output buffers for all connections. Should only
92 * be called once per scan of connections. There should be a select in
93 * here perhaps but that means either forcing a timeout or doing a poll.
94 * When flushing, all we do is empty the obuffer array for each local
95 * client and try to send it. if we cant send it, it goes into the sendQ
98 void flush_connections(int fd)
105 for (i = highest_fd; i >= 0; i--)
106 if ((cptr = loc_clients[i]) && DBufLength(&cptr->sendQ) > 0)
109 else if (fd >= 0 && (cptr = loc_clients[fd]) && DBufLength(&cptr->sendQ) > 0)
116 * This function is called from the main select-loop (or whatever)
117 * when there is a chance that some output would be possible. This
118 * attempts to empty the send queue as far as possible...
120 void send_queued(aClient *to)
123 if (to->flags & FLAGS_BLOCKED)
124 return; /* Don't bother */
127 * Once socket is marked dead, we cannot start writing to it,
128 * even if the error is removed...
133 * Actually, we should *NEVER* get here--something is
134 * not working correct if send_queued is called for a
135 * dead socket... --msa
137 * But we DO get here since flush_connections() is called
138 * from the main loop when a server still had remaining data
139 * in its buffer (not ending on a new-line).
140 * I rather leave the test here then move it to the main loop
141 * though: It wouldn't save cpu and it might introduce a bug :/.
146 while (DBufLength(&to->sendQ) > 0)
152 msg = dbuf_map(&to->sendQ, &len);
153 /* Returns always len > 0 */
154 if ((tmp = deliver_it(to, msg, len)) < 0)
156 dead_link(to, "Write error, closing link");
160 dbuf_delete(&to->sendQ, rlen);
161 to->lastsq = DBufLength(&to->sendQ) / 1024;
164 to->flags |= FLAGS_BLOCKED; /* Wait till select() says
165 we can write again */
174 * send message to single client
176 void sendto_one(aClient *to, char *pattern, ...)
179 va_start(vl, pattern);
180 vsendto_one(to, pattern, vl);
184 void vsendto_one(aClient *to, char *pattern, va_list vl)
186 vsprintf_irc(sendbuf, pattern, vl);
190 void sendbufto_one(aClient *to)
194 Debug((DEBUG_SEND, "Sending [%s] to %s", sendbuf, to->name));
199 return; /* This socket has already
200 been marked as dead */
203 /* This is normal when 'to' was being closed (via exit_client
204 * and close_connection) --Run
205 * Print the debug message anyway...
207 Debug((DEBUG_ERROR, "Local socket %s with negative fd %d... AARGH!",
212 len = strlen(sendbuf);
213 if (sendbuf[len - 1] != '\n')
217 sendbuf[len++] = '\r';
218 sendbuf[len++] = '\n';
224 char tmp_sendbuf[sizeof(sendbuf)];
226 strcpy(tmp_sendbuf, sendbuf);
227 sendto_ops("Trying to send [%s] to myself!", tmp_sendbuf);
231 if (DBufLength(&to->sendQ) > get_sendq(to))
234 sendto_ops("Max SendQ limit exceeded for %s: "
235 SIZE_T_FMT " > " SIZE_T_FMT, to->name,
236 DBufLength(&to->sendQ), get_sendq(to));
237 dead_link(to, "Max sendQ exceeded");
241 else if (!dbuf_put(&to->sendQ, sendbuf, len))
243 dead_link(to, "Buffer allocation error");
248 if (!sdbflag && !IsUser(to))
250 size_t len = strlen(sendbuf) - 2; /* Remove "\r\n" */
252 strncpy(sendbuf2, sendbuf, len);
253 sendbuf2[len] = '\0';
256 char c = sendbuf2[200];
258 sendto_ops("SND:%-8.8s(%.4d): \"%s...%s\"",
259 to->name, len, sendbuf2, &sendbuf2[len - 200]);
263 sendto_ops("SND:%-8.8s(%.4d): \"%s\"", to->name, len, sendbuf2);
264 strcpy(sendbuf, sendbuf2);
265 strcat(sendbuf, "\r\n");
271 * Update statistics. The following is slightly incorrect
272 * because it counts messages even if queued, but bytes
273 * only really sent. Queued bytes get updated in SendQueued.
278 to->acpt->sendM += 1;
280 * This little bit is to stop the sendQ from growing too large when
281 * there is no need for it to. Thus we call send_queued() every time
282 * 2k has been added to the queue since the last non-fatal write.
283 * Also stops us from deliberately building a large sendQ and then
284 * trying to flood that link with data (possible during the net
285 * relinking done by servers with a large load).
287 if (DBufLength(&to->sendQ) / 1024 > to->lastsq)
291 static void vsendto_prefix_one(register aClient *to, register aClient *from,
292 char *pattern, va_list vl)
294 if (to && from && MyUser(to) && IsUser(from))
296 static char sender[HOSTLEN + NICKLEN + USERLEN + 5];
299 Reg3 anUser *user = from->user;
301 par = va_arg(vl, char *);
302 strcpy(sender, from->name);
308 strcat(sender, user->username);
310 if (*user->host && !MyConnect(from))
313 strcat(sender, user->host);
318 * Flag is used instead of strchr(sender, '@') for speed and
319 * also since username/nick may have had a '@' in them. -avalon
321 if (!flag && MyConnect(from) && *user->host)
324 if (IsUnixSocket(from))
325 strcat(sender, user->host);
327 strcat(sender, from->sockhost);
330 strcpy(&sendbuf[1], sender);
331 /* Assuming 'pattern' always starts with ":%s ..." */
332 vsprintf_irc(sendbuf + strlen(sendbuf), &pattern[3], vl);
335 vsprintf_irc(sendbuf, pattern, vl);
339 void sendto_channel_butone(aClient *one, aClient *from, aChannel *chptr,
347 va_start(vl, pattern);
350 for (lp = chptr->members; lp; lp = lp->next)
352 acptr = lp->value.cptr;
353 if (acptr->from == one || /* ...was the one I should skip */
354 (lp->flags & CHFL_ZOMBIE) || IsDeaf(acptr))
356 if (MyConnect(acptr)) /* (It is always a client) */
357 vsendto_prefix_one(acptr, from, pattern, vl);
358 else if (sentalong[(i = acptr->from->fd)] != sentalong_marker)
360 sentalong[i] = sentalong_marker;
361 /* Don't send channel messages to links that are still eating
362 the net.burst: -- Run 2/1/1997 */
363 if (!IsBurstOrBurstAck(acptr->from))
364 vsendto_prefix_one(acptr, from, pattern, vl);
371 void sendto_lchanops_butone(aClient *one, aClient *from, aChannel *chptr,
378 va_start(vl, pattern);
380 for (lp = chptr->members; lp; lp = lp->next)
382 acptr = lp->value.cptr;
383 if (acptr == one || /* ...was the one I should skip */
384 !(lp->flags & CHFL_CHANOP) || /* Skip non chanops */
385 (lp->flags & CHFL_ZOMBIE) || IsDeaf(acptr))
387 if (MyConnect(acptr)) /* (It is always a client) */
388 vsendto_prefix_one(acptr, from, pattern, vl);
394 void sendto_chanopsserv_butone(aClient *one, aClient *from, aChannel *chptr,
403 char *source, *tp, *msg;
406 va_start(vl, pattern);
409 for (lp = chptr->members; lp; lp = lp->next)
411 acptr = lp->value.cptr;
412 if (acptr->from == acptr || /* Skip local clients */
414 Protocol(acptr->from) < 10 || /* Skip P09 links */
416 acptr->from == one || /* ...was the one I should skip */
417 !(lp->flags & CHFL_CHANOP) || /* Skip non chanops */
418 (lp->flags & CHFL_ZOMBIE) || IsDeaf(acptr))
420 if (sentalong[(i = acptr->from->fd)] != sentalong_marker)
422 sentalong[i] = sentalong_marker;
423 /* Don't send channel messages to links that are
424 still eating the net.burst: -- Run 2/1/1997 */
425 if (!IsBurstOrBurstAck(acptr->from))
426 vsendto_prefix_one(acptr, from, pattern, vl);
431 /* Send message to all 2.9 servers */
432 /* This is a hack, because it assumes that we know how `vl' is build up */
433 source = va_arg(vl, char *);
434 tp = va_arg(vl, char *); /* Channel */
435 msg = va_arg(vl, char *);
436 for (lp = chptr->members; lp; lp = lp->next)
438 acptr = lp->value.cptr;
439 if (acptr->from == acptr || /* Skip local clients */
440 Protocol(acptr->from) > 9 || /* Skip P10 servers */
441 acptr->from == one || /* ...was the one I should skip */
442 !(lp->flags & CHFL_CHANOP) || /* Skip non chanops */
443 (lp->flags & CHFL_ZOMBIE) || IsDeaf(acptr))
445 if (sentalong[(i = acptr->from->fd)] != 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))
456 /* Find all chanops in this direction: */
457 for (lp2 = chptr->members; lp2; lp2 = lp2->next)
459 acptr2 = lp2->value.cptr;
460 if (acptr2->from == acptr->from && acptr2->from != one &&
461 (lp2->flags & CHFL_CHANOP) && !(lp2->flags & CHFL_ZOMBIE) &&
464 int len = strlen(acptr2->name);
465 if (tp + len + 2 > target + sizeof(target))
467 sendto_prefix_one(acptr, from,
468 ":%s NOTICE %s :%s", source, target, msg);
474 strcpy(tp, acptr2->name);
478 sendto_prefix_one(acptr, from,
479 ":%s NOTICE %s :%s", source, target, msg);
490 * sendto_server_butone
492 * Send a message to all connected servers except the client 'one'.
494 void sendto_serv_butone(aClient *one, char *pattern, ...)
499 va_start(vl, pattern);
500 vsprintf_irc(sendbuf, pattern, vl);
503 for (lp = me.serv->down; lp; lp = lp->next)
505 if (one && lp->value.cptr == one->from)
507 sendbufto_one(lp->value.cptr);
513 * sendbufto_serv_butone()
515 * Send prepared sendbuf to all connected servers except the client 'one'
516 * -Ghostwolf 18-May-97
518 void sendbufto_serv_butone(aClient *one)
522 for (lp = me.serv->down; lp; lp = lp->next)
524 if (one && lp->value.cptr == one->from)
526 sendbufto_one(lp->value.cptr);
531 * sendto_common_channels()
533 * Sends a message to all people (inclusing `acptr') on local server
534 * who are in same channel with client `acptr'.
536 void sendto_common_channels(aClient *acptr, char *pattern, ...)
542 va_start(vl, pattern);
546 sentalong[acptr->fd] = sentalong_marker;
547 /* loop through acptr's channels, and the members on their channels */
549 for (chan = acptr->user->channel; chan; chan = chan->next)
550 for (member = chan->value.chptr->members; member; member = member->next)
552 Reg3 aClient *cptr = member->value.cptr;
553 if (MyConnect(cptr) && sentalong[cptr->fd] != sentalong_marker)
555 sentalong[cptr->fd] = sentalong_marker;
556 vsendto_prefix_one(cptr, acptr, pattern, vl);
559 if (MyConnect(acptr))
560 vsendto_prefix_one(acptr, acptr, pattern, vl);
566 * sendto_channel_butserv
568 * Send a message to all members of a channel that
569 * are connected to this server.
571 void sendto_channel_butserv(aChannel *chptr, aClient *from, char *pattern, ...)
577 for (va_start(vl, pattern), lp = chptr->members; lp; lp = lp->next)
578 if (MyConnect(acptr = lp->value.cptr) && !(lp->flags & CHFL_ZOMBIE))
579 vsendto_prefix_one(acptr, from, pattern, vl);
585 * Send a msg to all ppl on servers/hosts that match a specified mask
586 * (used for enhanced PRIVMSGs)
588 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
591 static int match_it(aClient *one, char *mask, int what)
596 return (match(mask, one->user->host) == 0);
599 return (match(mask, one->user->server->name) == 0);
604 * sendto_match_butone
606 * Send to all clients which match the mask in a way defined on 'what';
607 * either by user hostname or user servername.
609 void sendto_match_butone(aClient *one, aClient *from,
610 char *mask, int what, char *pattern, ...)
614 Reg2 aClient *cptr, *acptr;
616 va_start(vl, pattern);
617 for (i = 0; i <= highest_fd; i++)
619 if (!(cptr = loc_clients[i]))
620 continue; /* that clients are not mine */
621 if (cptr == one) /* must skip the origin !! */
625 for (acptr = client; acptr; acptr = acptr->next)
626 if (IsUser(acptr) && match_it(acptr, mask, what) && acptr->from == cptr)
628 /* a person on that server matches the mask, so we
629 * send *one* msg to that server ...
633 /* ... but only if there *IS* a matching person */
635 /* my client, does he match ? */
636 else if (!(IsUser(cptr) && match_it(cptr, mask, what)))
638 vsendto_prefix_one(cptr, from, pattern, vl);
648 * Send to *local* ops but one.
650 void sendto_lops_butone(aClient *one, char *pattern, ...)
658 sprintf_irc(nbuf, ":%s NOTICE %%s :*** Notice -- ", me.name);
659 va_start(vl, pattern);
660 vsprintf_irc(nbuf + strlen(nbuf), pattern, vl);
662 for (cptrp = me.serv->client_list, i = 0; i <= me.serv->nn_mask; ++cptrp, ++i)
663 if ((cptr = *cptrp) && cptr != one && SendServNotice(cptr))
665 sprintf_irc(sendbuf, nbuf, cptr->name);
674 * Sends message to the list indicated by the bitmask field.
675 * Don't try to send to more than one list! That is not supported.
678 void vsendto_op_mask(register snomask_t mask, const char *pattern, va_list vl)
680 static char fmt[1024];
682 register int i = 0; /* so that 1 points to opsarray[0] */
687 if (!(opslist = opsarray[i]))
690 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
693 strcpy(fmt_target, opslist->value.cptr->name);
694 strcat(fmt_target, " :*** Notice -- ");
695 strcat(fmt_target, pattern);
696 vsendto_one(opslist->value.cptr, fmt, vl);
697 opslist = opslist->next;
705 * Send a prepared sendbuf to the list indicated by the bitmask field.
706 * Ghostwolf 16-May-97
708 void sendbufto_op_mask(snomask_t mask)
710 register int i = 0; /* so that 1 points to opsarray[0] */
714 if (!(opslist = opsarray[i]))
718 sendbufto_one(opslist->value.cptr);
719 opslist = opslist->next;
728 * Send to *local* ops only.
730 void vsendto_ops(const char *pattern, va_list vl)
737 fmt_target = sprintf_irc(fmt, ":%s NOTICE ", me.name);
739 for (i = 0; i <= highest_fd; i++)
740 if ((cptr = loc_clients[i]) && !IsServer(cptr) && !IsMe(cptr) &&
741 SendServNotice(cptr))
743 strcpy(fmt_target, cptr->name);
744 strcat(fmt_target, " :*** Notice -- ");
745 strcat(fmt_target, pattern);
746 vsendto_one(cptr, fmt, vl);
752 void sendto_op_mask(snomask_t mask, const char *pattern, ...)
755 va_start(vl, pattern);
756 vsendto_op_mask(mask, pattern, vl);
760 void sendto_ops(const char *pattern, ...)
763 va_start(vl, pattern);
764 vsendto_op_mask(SNO_OLDSNO, pattern, vl);
771 * Send message to all operators.
772 * one - client not to send message to
773 * from- client which message is from *NEVER* NULL!!
775 void sendto_ops_butone(aClient *one, aClient *from, char *pattern, ...)
781 va_start(vl, pattern);
783 for (cptr = client; cptr; cptr = cptr->next)
785 /*if (!IsAnOper(cptr)) */
786 if (!SendWallops(cptr))
788 i = cptr->from->fd; /* find connection oper is on */
789 if (sentalong[i] == sentalong_marker) /* sent message along it already ? */
791 if (cptr->from == one)
792 continue; /* ...was the one I should skip */
793 sentalong[i] = sentalong_marker;
794 vsendto_prefix_one(cptr->from, from, pattern, vl);
802 * sendto_g_serv_butone
804 * Send message to all remote +g users (server links).
806 * one - server not to send message to.
808 void sendto_g_serv_butone(aClient *one, char *pattern, ...)
814 va_start(vl, pattern);
816 vsprintf_irc(sendbuf, pattern, vl);
817 for (cptr = client; cptr; cptr = cptr->next)
819 if (!SendDebug(cptr))
821 i = cptr->from->fd; /* find connection user is on */
822 if (sentalong[i] == sentalong_marker) /* sent message along it already ? */
826 sentalong[i] = sentalong_marker;
827 if (cptr->from == one)
839 * to - destination client
840 * from - client which message is from
842 * NOTE: NEITHER OF THESE SHOULD *EVER* BE NULL!!
845 void sendto_prefix_one(Reg1 aClient *to, Reg2 aClient *from, char *pattern, ...)
848 va_start(vl, pattern);
849 vsendto_prefix_one(to, from, pattern, vl);
856 * Send to *local* ops only but NOT +s nonopers.
858 void sendto_realops(const char *pattern, ...)
862 va_start(vl, pattern);
863 vsendto_op_mask(SNO_OLDREALOP, pattern, vl);
870 * Send message to all servers of protocol 'p' and lower.
872 void sendto_lowprot_butone(aClient *cptr, int p, char *pattern, ...)
876 va_start(vl, pattern);
877 for (lp = me.serv->down; lp; lp = lp->next)
878 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) <= p)
879 vsendto_one(lp->value.cptr, pattern, vl);
884 * Send message to all servers of protocol 'p' and higher.
886 void sendto_highprot_butone(aClient *cptr, int p, char *pattern, ...)
890 va_start(vl, pattern);
891 for (lp = me.serv->down; lp; lp = lp->next)
892 if (lp->value.cptr != cptr && Protocol(lp->value.cptr) >= p)
893 vsendto_one(lp->value.cptr, pattern, vl);