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"
46 static int sentalong[MAXCONNECTIONS];
47 static int sentalong_marker;
48 struct SLink *opsarray[32]; /* don't use highest bit unless you change
49 atoi to strtoul in sendto_op_mask() */
53 * An error has been detected. The link *must* be closed,
54 * but *cannot* call ExitClient (m_bye) from here.
55 * Instead, mark it with FLAGS_DEADSOCKET. This should
56 * generate ExitClient from the main loop.
58 * If 'notice' is not NULL, it is assumed to be a format
59 * for a message to local opers. It can contain only one
60 * '%s', which will be replaced by the sockhost field of
63 * Also, the notice is skipped for "uninteresting" cases,
64 * like Persons and yet unknown connections...
67 static void dead_link(struct Client *to, char *notice)
69 to->flags |= FLAGS_DEADSOCKET;
71 * If because of BUFFERPOOL problem then clean dbuf's now so that
72 * notices don't hurt operators below.
74 DBufClear(&to->recvQ);
75 MsgQClear(&to->sendQ);
78 * Keep a copy of the last comment, for later use...
80 ircd_strncpy(to->info, notice, REALLEN);
82 if (!IsUser(to) && !IsUnknown(to) && !(to->flags & FLAGS_CLOSING))
83 sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", to->info, to->name);
84 Debug((DEBUG_ERROR, to->info));
87 static int can_send(struct Client* to)
90 return (IsDead(to) || IsMe(to) || -1 == to->fd) ? 0 : 1;
96 * Used to empty all output buffers for all connections. Should only
97 * be called once per scan of connections. There should be a select in
98 * here perhaps but that means either forcing a timeout or doing a poll.
99 * When flushing, all we do is empty the obuffer array for each local
100 * client and try to send it. if we cant send it, it goes into the sendQ
103 void flush_connections(struct Client* cptr)
110 for (i = HighestFd; i >= 0; i--) {
111 if ((cptr = LocalClientArray[i]))
118 * flush_sendq_except - run through local client array and flush
119 * the sendq for each client, if the address of the client sendq
120 * is the same as the one specified, it is skipped. This is used
121 * by dbuf_put to try to get some more memory before bailing and
122 * causing the client to be disconnected.
124 void flush_sendq_except(void)
128 for (i = HighestFd; i >= 0; i--) {
129 if ( (cptr = LocalClientArray[i]))
137 * This function is called from the main select-loop (or whatever)
138 * when there is a chance that some output would be possible. This
139 * attempts to empty the send queue as far as possible...
141 void send_queued(struct Client *to)
144 assert(0 != to->local);
146 if (IsBlocked(to) || !can_send(to))
147 return; /* Don't bother */
149 while (MsgQLength(&to->sendQ) > 0) {
152 if ((len = deliver_it(to, &to->sendQ))) {
153 msgq_delete(&to->sendQ, len);
154 to->lastsq = MsgQLength(&to->sendQ) / 1024;
161 sprintf(tmp,"Write error: %s",(strerror(to->error)) ? (strerror(to->error)) : "Unknown error" );
169 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
179 * This socket has already been marked as dead
183 if (MsgQLength(&to->sendQ) > get_sendq(to)) {
185 sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
186 "%zu > %zu", to, MsgQLength(&to->sendQ),
188 dead_link(to, "Max sendQ exceeded");
192 Debug((DEBUG_SEND, "Sending [%p] to %s", buf, to->name));
194 msgq_add(&to->sendQ, buf, prio);
197 * Update statistics. The following is slightly incorrect
198 * because it counts messages even if queued, but bytes
199 * only really sent. Queued bytes get updated in SendQueued.
204 * This little bit is to stop the sendQ from growing too large when
205 * there is no need for it to. Thus we call send_queued() every time
206 * 2k has been added to the queue since the last non-fatal write.
207 * Also stops us from deliberately building a large sendQ and then
208 * trying to flood that link with data (possible during the net
209 * relinking done by servers with a large load).
211 if (MsgQLength(&to->sendQ) / 1024 > to->lastsq)
216 * Send a msg to all ppl on servers/hosts that match a specified mask
217 * (used for enhanced PRIVMSGs)
219 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
222 static int match_it(struct Client *one, const char *mask, int what)
227 return (match(mask, one->user->host) == 0);
230 return (match(mask, one->user->server->name) == 0);
235 * Send a raw command to a single client; use *ONLY* if you absolutely
236 * must send a command without a prefix.
238 void sendrawto_one(struct Client *to, const char *pattern, ...)
243 va_start(vl, pattern);
244 mb = msgq_vmake(to, pattern, vl);
247 send_buffer(to, mb, 0);
253 * Send a (prefixed) command to a single client; select which of <cmd>
254 * <tok> to use depending on if to is a server or not. <from> is the
255 * originator of the command.
257 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
258 struct Client *to, const char *pattern, ...)
262 va_start(vl, pattern);
263 vsendcmdto_one(from, cmd, tok, to, pattern, vl);
267 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
268 struct Client *to, const char *pattern, va_list vl)
275 vd.vd_format = pattern; /* set up the struct VarData for %v */
278 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
281 send_buffer(to, mb, 0);
287 * Send a (prefixed) command to all servers but one, using tok; cmd
288 * is ignored in this particular function.
290 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
291 const char *tok, struct Client *one,
292 const char *pattern, ...)
298 vd.vd_format = pattern; /* set up the struct VarData for %v */
299 va_start(vd.vd_args, pattern);
302 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
305 /* send it to our downlinks */
306 for (lp = me.serv->down; lp; lp = lp->next) {
307 if (one && lp->value.cptr == one->from)
309 send_buffer(lp->value.cptr, mb, 0);
316 * Send a (prefix) command originating from <from> to all channels
317 * <from> is locally on. <from> must be a user. <tok> is ignored in
320 /* XXX sentalong_marker used XXX
322 * There is not an easy way to revoke the need for sentalong_marker
323 * from this function. Thoughts and ideas would be welcome... -Kev
325 * One possibility would be to internalize the sentalong array; that
326 * could be prohibitively big, though. We could get around that by
327 * making one that's the number of connected servers or something...
328 * or perhaps by adding a special flag to the servers we've sent a
329 * message to, and then a final loop through the connected servers
330 * to delete the flag. -Kev
332 void sendcmdto_common_channels(struct Client *from, const char *cmd,
333 const char *tok, const char *pattern, ...)
337 struct Membership *chan;
338 struct Membership *member;
341 assert(0 != from->from);
342 assert(0 != pattern);
343 assert(!IsServer(from) && !IsMe(from));
345 vd.vd_format = pattern; /* set up the struct VarData for %v */
347 va_start(vd.vd_args, pattern);
349 /* build the buffer */
350 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
354 if (-1 < from->from->fd)
355 sentalong[from->from->fd] = sentalong_marker;
357 * loop through from's channels, and the members on their channels
359 for (chan = from->user->channel; chan; chan = chan->next_channel)
360 for (member = chan->channel->members; member;
361 member = member->next_member)
362 if (MyConnect(member->user) && -1 < member->user->from->fd &&
363 sentalong[member->user->from->fd] != sentalong_marker) {
364 sentalong[member->user->from->fd] = sentalong_marker;
365 send_buffer(member->user, mb, 0);
369 send_buffer(from, mb, 0);
375 * Send a (prefixed) command to all local users on the channel specified
376 * by <to>; <tok> is ignored by this function
378 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
379 const char *tok, struct Channel *to,
380 const char *pattern, ...)
384 struct Membership *member;
386 vd.vd_format = pattern; /* set up the struct VarData for %v */
387 va_start(vd.vd_args, pattern);
389 /* build the buffer */
390 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
393 /* send the buffer to each local channel member */
394 for (member = to->members; member; member = member->next_member) {
395 if (MyConnect(member->user) && !IsZombie(member))
396 send_buffer(member->user, mb, 0);
403 * Send a (prefixed) command to all users on this channel, including
404 * remote users; users to skip may be specified by setting appropriate
405 * flags in the <skip> argument. <one> will also be skipped.
407 /* XXX sentalong_marker used XXX
409 * We can drop sentalong_marker from this function by adding a field to
410 * channels and to connections; what we do is make a user's connection
411 * a "member" of the channel by adding it to the new list, and we use
412 * the struct Membership status as a reference count. Then, to implement
413 * this function, we just walk the list of connections. Unfortunately,
414 * this doesn't account for sending only to channel ops, or for not
415 * sending to +d users; we could account for that by splitting those
416 * counts out, but that would imply adding two more fields (at least) to
417 * the struct Membership... -Kev
419 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
420 const char *tok, struct Channel *to,
421 struct Client *one, unsigned int skip,
422 const char *pattern, ...)
424 struct Membership *member;
426 struct MsgBuf *user_mb;
427 struct MsgBuf *serv_mb;
429 vd.vd_format = pattern;
431 /* Build buffer to send to users */
432 va_start(vd.vd_args, pattern);
433 user_mb = msgq_make(0, skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v",
434 from, skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
437 /* Build buffer to send to servers */
438 va_start(vd.vd_args, pattern);
439 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
442 /* send buffer along! */
444 for (member = to->members; member; member = member->next_member) {
445 /* skip one, zombies, and deaf users... */
446 if (member->user->from == one || IsZombie(member) ||
447 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
448 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
449 (skip & SKIP_BURST && IsBurstOrBurstAck(member->user->from)) ||
450 member->user->from->fd < 0 ||
451 sentalong[member->user->from->fd] == sentalong_marker)
453 sentalong[member->user->from->fd] = sentalong_marker;
455 if (MyConnect(member->user)) /* pick right buffer to send */
456 send_buffer(member->user, user_mb, 0);
458 send_buffer(member->user, serv_mb, 0);
466 * Send a (prefixed) command to all users except <one> that have
469 /* XXX sentalong_marker used XXX
471 * Again, we can solve this use of sentalong_marker by adding a field
472 * to connections--a count of the number of +w users, and another count
473 * of +g users. Then, just walk through the local clients to send
474 * those messages, and another walk through the connected servers list,
475 * sending only if there's a non-zero count. No caveats here, either,
476 * beyond remembering to decrement the count when a user /quit's or is
477 * killed, or a server is squit. -Kev
479 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
480 const char *tok, struct Client *one,
481 unsigned int flag, const char *pattern, ...)
485 struct MsgBuf *user_mb;
486 struct MsgBuf *serv_mb;
488 vd.vd_format = pattern;
490 /* Build buffer to send to users */
491 va_start(vd.vd_args, pattern);
492 user_mb = msgq_make(0, "%:#C " MSG_WALLOPS " %v", from, &vd);
495 /* Build buffer to send to servers */
496 va_start(vd.vd_args, pattern);
497 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
500 /* send buffer along! */
502 for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
503 if (cptr->from == one || IsServer(cptr) || !(cptr->flags & flag) ||
504 cptr->from->fd < 0 || sentalong[cptr->from->fd] == sentalong_marker)
505 continue; /* skip it */
506 sentalong[cptr->from->fd] = sentalong_marker;
508 if (MyConnect(cptr)) /* send right buffer */
509 send_buffer(cptr, user_mb, 1);
511 send_buffer(cptr, serv_mb, 1);
519 * Send a (prefixed) command to all users who match <to>, under control
522 /* XXX sentalong_marker used XXX
524 * This is also a difficult one to solve. The basic approach would be
525 * to walk the client list of each connected server until we find a
526 * match--but then, we also have to walk the client list of all the
527 * servers behind that one. We could implement this recursively--or we
528 * could add (yet another) field to the connection struct that would be
529 * a linked list of clients introduced through that link, and just walk
530 * that, making this into an iterative implementation. Unfortunately,
531 * we probably would not be able to use tail recursion for the recursive
532 * solution, so a deep network could exhaust our stack space; therefore
533 * I favor the extra linked list, even though that increases the
534 * complexity of the database. -Kev
536 void sendcmdto_match_butone(struct Client *from, const char *cmd,
537 const char *tok, const char *to,
538 struct Client *one, unsigned int who,
539 const char *pattern, ...)
543 struct MsgBuf *user_mb;
544 struct MsgBuf *serv_mb;
546 vd.vd_format = pattern;
548 /* Build buffer to send to users */
549 va_start(vd.vd_args, pattern);
550 user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
553 /* Build buffer to send to servers */
554 va_start(vd.vd_args, pattern);
555 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
558 /* send buffer along */
560 for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
561 if (cptr->from == one || IsServer(cptr) || IsMe(cptr) ||
562 !match_it(cptr, to, who) || cptr->from->fd < 0 ||
563 sentalong[cptr->from->fd] == sentalong_marker)
564 continue; /* skip it */
565 sentalong[cptr->from->fd] = sentalong_marker;
567 if (MyConnect(cptr)) /* send right buffer */
568 send_buffer(cptr, user_mb, 0);
570 send_buffer(cptr, serv_mb, 0);
578 * Send a server notice to all users subscribing to the indicated <mask>
581 void sendto_opmask_butone(struct Client *one, unsigned int mask,
582 const char *pattern, ...)
586 va_start(vl, pattern);
587 vsendto_opmask_butone(one, mask, pattern, vl);
592 * Same as above, except called with a variable argument list
594 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
595 const char *pattern, va_list vl)
599 int i = 0; /* so that 1 points to opsarray[0] */
600 struct SLink *opslist;
605 if (!(opslist = opsarray[i]))
609 * build string; I don't want to bother with client nicknames, so I hope
612 vd.vd_format = pattern;
614 mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", me.name, &vd);
616 for (; opslist; opslist = opslist->next)
617 send_buffer(opslist->value.cptr, mb, 0);