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 cli_flags(to) |= FLAGS_DEADSOCKET;
71 * If because of BUFFERPOOL problem then clean dbuf's now so that
72 * notices don't hurt operators below.
74 DBufClear(&(cli_recvQ(to)));
75 MsgQClear(&(cli_sendQ(to)));
78 * Keep a copy of the last comment, for later use...
80 ircd_strncpy(cli_info(to), notice, REALLEN);
82 if (!IsUser(to) && !IsUnknown(to) && !(cli_flags(to) & FLAGS_CLOSING))
83 sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
84 Debug((DEBUG_ERROR, cli_info(to)));
87 static int can_send(struct Client* to)
90 return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 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 != cli_local(to));
146 if (IsBlocked(to) || !can_send(to))
147 return; /* Don't bother */
149 while (MsgQLength(&(cli_sendQ(to))) > 0) {
152 if ((len = deliver_it(to, &(cli_sendQ(to))))) {
153 msgq_delete(&(cli_sendQ(to)), len);
154 to->lastsq = MsgQLength(&(cli_sendQ(to))) / 1024;
161 sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "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(&(cli_sendQ(to))) > get_sendq(to)) {
185 sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
186 "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
188 dead_link(to, "Max sendQ exceeded");
192 Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
194 msgq_add(&(cli_sendQ(to)), 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(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
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, cli_user(one)->host) == 0);
230 return (match(mask, cli_name(cli_user(one)->server)) == 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 == cli_from(one))
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 != cli_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 < cli_fd(cli_from(from)))
355 sentalong[cli_fd(cli_from(from))] = sentalong_marker;
357 * loop through from's channels, and the members on their channels
359 for (chan = cli_user(from)->channel; chan; chan = chan->next_channel)
360 for (member = chan->channel->members; member;
361 member = member->next_member)
362 if (MyConnect(member->user) && -1 < cli_fd(cli_from(member->user)) &&
363 sentalong[cli_fd(cli_from(member->user))] != sentalong_marker) {
364 sentalong[cli_fd(cli_from(member->user))] = 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 (cli_from(member->user) == one || IsZombie(member) ||
447 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
448 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
449 (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
450 cli_fd(cli_from(member->user)) < 0 ||
451 sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
453 sentalong[cli_fd(cli_from(member->user))] = 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 = cli_next(cptr)) {
503 if (cli_from(cptr) == one || IsServer(cptr) || !(cli_flags(cptr) & flag) ||
504 cli_fd(cli_from(cptr)) < 0 ||
505 sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
506 continue; /* skip it */
507 sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
509 if (MyConnect(cptr)) /* send right buffer */
510 send_buffer(cptr, user_mb, 1);
512 send_buffer(cptr, serv_mb, 1);
520 * Send a (prefixed) command to all users who match <to>, under control
523 /* XXX sentalong_marker used XXX
525 * This is also a difficult one to solve. The basic approach would be
526 * to walk the client list of each connected server until we find a
527 * match--but then, we also have to walk the client list of all the
528 * servers behind that one. We could implement this recursively--or we
529 * could add (yet another) field to the connection struct that would be
530 * a linked list of clients introduced through that link, and just walk
531 * that, making this into an iterative implementation. Unfortunately,
532 * we probably would not be able to use tail recursion for the recursive
533 * solution, so a deep network could exhaust our stack space; therefore
534 * I favor the extra linked list, even though that increases the
535 * complexity of the database. -Kev
537 void sendcmdto_match_butone(struct Client *from, const char *cmd,
538 const char *tok, const char *to,
539 struct Client *one, unsigned int who,
540 const char *pattern, ...)
544 struct MsgBuf *user_mb;
545 struct MsgBuf *serv_mb;
547 vd.vd_format = pattern;
549 /* Build buffer to send to users */
550 va_start(vd.vd_args, pattern);
551 user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
554 /* Build buffer to send to servers */
555 va_start(vd.vd_args, pattern);
556 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
559 /* send buffer along */
561 for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
562 if (cli_from(cptr) == one || IsServer(cptr) || IsMe(cptr) ||
563 !match_it(cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
564 sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
565 continue; /* skip it */
566 sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
568 if (MyConnect(cptr)) /* send right buffer */
569 send_buffer(cptr, user_mb, 0);
571 send_buffer(cptr, serv_mb, 0);
579 * Send a server notice to all users subscribing to the indicated <mask>
582 void sendto_opmask_butone(struct Client *one, unsigned int mask,
583 const char *pattern, ...)
587 va_start(vl, pattern);
588 vsendto_opmask_butone(one, mask, pattern, vl);
593 * Same as above, except called with a variable argument list
595 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
596 const char *pattern, va_list vl)
600 int i = 0; /* so that 1 points to opsarray[0] */
601 struct SLink *opslist;
606 if (!(opslist = opsarray[i]))
610 * build string; I don't want to bother with client nicknames, so I hope
613 vd.vd_format = pattern;
615 mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", me.name, &vd);
617 for (; opslist; opslist = opslist->next)
618 send_buffer(opslist->value.cptr, mb, 0);