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() */
50 static struct Connection *send_queues = 0;
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. It 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(struct Client *to, char *notice)
71 cli_flags(to) |= FLAGS_DEADSOCKET;
73 * If because of BUFFERPOOL problem then clean dbuf's now so that
74 * notices don't hurt operators below.
76 DBufClear(&(cli_recvQ(to)));
77 MsgQClear(&(cli_sendQ(to)));
78 client_drop_sendq(cli_connect(to));
81 * Keep a copy of the last comment, for later use...
83 ircd_strncpy(cli_info(to), notice, REALLEN);
85 if (!IsUser(to) && !IsUnknown(to) && !(cli_flags(to) & FLAGS_CLOSING))
86 sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
87 Debug((DEBUG_ERROR, cli_info(to)));
90 static int can_send(struct Client* to)
93 return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
99 * Used to empty all output buffers for all connections. Should only
100 * be called once per scan of connections. There should be a select in
101 * here perhaps but that means either forcing a timeout or doing a poll.
102 * When flushing, all we do is empty the obuffer array for each local
103 * client and try to send it. if we cant send it, it goes into the sendQ
106 void flush_connections(struct Client* cptr)
112 struct Connection* con;
113 for (con = send_queues; con; con = con_next(con)) {
114 assert(0 < MsgQLength(&(con_sendQ(con))));
115 send_queued(con_client(con));
123 * This function is called from the main select-loop (or whatever)
124 * when there is a chance that some output would be possible. This
125 * attempts to empty the send queue as far as possible...
127 void send_queued(struct Client *to)
130 assert(0 != cli_local(to));
132 if (IsBlocked(to) || !can_send(to))
133 return; /* Don't bother */
135 while (MsgQLength(&(cli_sendQ(to))) > 0) {
138 if ((len = deliver_it(to, &(cli_sendQ(to))))) {
139 msgq_delete(&(cli_sendQ(to)), len);
140 cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
147 sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
154 /* Ok, sendq is now empty... */
155 client_drop_sendq(cli_connect(to));
158 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
168 * This socket has already been marked as dead
172 if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
174 sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
175 "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
177 dead_link(to, "Max sendQ exceeded");
181 Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
183 msgq_add(&(cli_sendQ(to)), buf, prio);
184 client_add_sendq(cli_connect(to), &send_queues);
187 * Update statistics. The following is slightly incorrect
188 * because it counts messages even if queued, but bytes
189 * only really sent. Queued bytes get updated in SendQueued.
194 * This little bit is to stop the sendQ from growing too large when
195 * there is no need for it to. Thus we call send_queued() every time
196 * 2k has been added to the queue since the last non-fatal write.
197 * Also stops us from deliberately building a large sendQ and then
198 * trying to flood that link with data (possible during the net
199 * relinking done by servers with a large load).
201 if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
206 * Send a msg to all ppl on servers/hosts that match a specified mask
207 * (used for enhanced PRIVMSGs)
209 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
212 static int match_it(struct Client *one, const char *mask, int what)
217 return (match(mask, cli_user(one)->host) == 0);
220 return (match(mask, cli_name(cli_user(one)->server)) == 0);
225 * Send a raw command to a single client; use *ONLY* if you absolutely
226 * must send a command without a prefix.
228 void sendrawto_one(struct Client *to, const char *pattern, ...)
233 va_start(vl, pattern);
234 mb = msgq_vmake(to, pattern, vl);
237 send_buffer(to, mb, 0);
243 * Send a (prefixed) command to a single client; select which of <cmd>
244 * <tok> to use depending on if to is a server or not. <from> is the
245 * originator of the command.
247 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
248 struct Client *to, const char *pattern, ...)
252 va_start(vl, pattern);
253 vsendcmdto_one(from, cmd, tok, to, pattern, vl);
257 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
258 struct Client *to, const char *pattern, va_list vl)
265 vd.vd_format = pattern; /* set up the struct VarData for %v */
268 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
271 send_buffer(to, mb, 0);
277 * Send a (prefixed) command to all servers but one, using tok; cmd
278 * is ignored in this particular function.
280 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
281 const char *tok, struct Client *one,
282 const char *pattern, ...)
288 vd.vd_format = pattern; /* set up the struct VarData for %v */
289 va_start(vd.vd_args, pattern);
292 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
295 /* send it to our downlinks */
296 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
297 if (one && lp->value.cptr == cli_from(one))
299 send_buffer(lp->value.cptr, mb, 0);
306 * Send a (prefix) command originating from <from> to all channels
307 * <from> is locally on. <from> must be a user. <tok> is ignored in
310 /* XXX sentalong_marker used XXX
312 * There is not an easy way to revoke the need for sentalong_marker
313 * from this function. Thoughts and ideas would be welcome... -Kev
315 * One possibility would be to internalize the sentalong array; that
316 * could be prohibitively big, though. We could get around that by
317 * making one that's the number of connected servers or something...
318 * or perhaps by adding a special flag to the servers we've sent a
319 * message to, and then a final loop through the connected servers
320 * to delete the flag. -Kev
322 void sendcmdto_common_channels(struct Client *from, const char *cmd,
323 const char *tok, const char *pattern, ...)
327 struct Membership *chan;
328 struct Membership *member;
331 assert(0 != cli_from(from));
332 assert(0 != pattern);
333 assert(!IsServer(from) && !IsMe(from));
335 vd.vd_format = pattern; /* set up the struct VarData for %v */
337 va_start(vd.vd_args, pattern);
339 /* build the buffer */
340 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
344 if (-1 < cli_fd(cli_from(from)))
345 sentalong[cli_fd(cli_from(from))] = sentalong_marker;
347 * loop through from's channels, and the members on their channels
349 for (chan = cli_user(from)->channel; chan; chan = chan->next_channel)
350 for (member = chan->channel->members; member;
351 member = member->next_member)
352 if (MyConnect(member->user) && -1 < cli_fd(cli_from(member->user)) &&
353 sentalong[cli_fd(cli_from(member->user))] != sentalong_marker) {
354 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
355 send_buffer(member->user, mb, 0);
359 send_buffer(from, mb, 0);
365 * Send a (prefixed) command to all local users on the channel specified
366 * by <to>; <tok> is ignored by this function
368 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
369 const char *tok, struct Channel *to,
370 const char *pattern, ...)
374 struct Membership *member;
376 vd.vd_format = pattern; /* set up the struct VarData for %v */
377 va_start(vd.vd_args, pattern);
379 /* build the buffer */
380 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
383 /* send the buffer to each local channel member */
384 for (member = to->members; member; member = member->next_member) {
385 if (MyConnect(member->user) && !IsZombie(member))
386 send_buffer(member->user, mb, 0);
393 * Send a (prefixed) command to all users on this channel, including
394 * remote users; users to skip may be specified by setting appropriate
395 * flags in the <skip> argument. <one> will also be skipped.
397 /* XXX sentalong_marker used XXX
399 * We can drop sentalong_marker from this function by adding a field to
400 * channels and to connections; what we do is make a user's connection
401 * a "member" of the channel by adding it to the new list, and we use
402 * the struct Membership status as a reference count. Then, to implement
403 * this function, we just walk the list of connections. Unfortunately,
404 * this doesn't account for sending only to channel ops, or for not
405 * sending to +d users; we could account for that by splitting those
406 * counts out, but that would imply adding two more fields (at least) to
407 * the struct Membership... -Kev
409 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
410 const char *tok, struct Channel *to,
411 struct Client *one, unsigned int skip,
412 const char *pattern, ...)
414 struct Membership *member;
416 struct MsgBuf *user_mb;
417 struct MsgBuf *serv_mb;
419 vd.vd_format = pattern;
421 /* Build buffer to send to users */
422 va_start(vd.vd_args, pattern);
423 user_mb = msgq_make(0, skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v",
424 from, skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
427 /* Build buffer to send to servers */
428 va_start(vd.vd_args, pattern);
429 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
432 /* send buffer along! */
434 for (member = to->members; member; member = member->next_member) {
435 /* skip one, zombies, and deaf users... */
436 if (cli_from(member->user) == one || IsZombie(member) ||
437 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
438 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
439 (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
440 cli_fd(cli_from(member->user)) < 0 ||
441 sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
443 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
445 if (MyConnect(member->user)) /* pick right buffer to send */
446 send_buffer(member->user, user_mb, 0);
448 send_buffer(member->user, serv_mb, 0);
456 * Send a (prefixed) command to all users except <one> that have
459 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
460 const char *tok, struct Client *one,
461 unsigned int flag, const char *pattern, ...)
467 unsigned int oper_fl = 0;
470 if (flag & FLAGS_OPER) {
475 vd.vd_format = pattern;
477 /* Build buffer to send to users */
478 va_start(vd.vd_args, pattern);
479 mb = msgq_make(0, "%:#C " MSG_WALLOPS " %v", from, &vd);
482 /* send buffer along! */
483 for (i = 0; i <= HighestFd; i++) {
484 if (!(cptr = LocalClientArray[i]) || !(cli_flags(cptr) & flag) ||
485 (oper_fl && !IsAnOper(cptr)) || cli_fd(cli_from(cptr)) < 0)
486 continue; /* skip it */
487 send_buffer(cptr, mb, 1);
492 /* Build buffer to send to servers */
493 va_start(vd.vd_args, pattern);
494 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
497 /* send buffer along! */
498 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
499 if (one && lp->value.cptr == cli_from(one))
501 send_buffer(lp->value.cptr, mb, 0);
508 * Send a (prefixed) command to all users who match <to>, under control
511 /* XXX sentalong_marker used XXX
513 * This is also a difficult one to solve. The basic approach would be
514 * to walk the client list of each connected server until we find a
515 * match--but then, we also have to walk the client list of all the
516 * servers behind that one. We could implement this recursively--or we
517 * could add (yet another) field to the connection struct that would be
518 * a linked list of clients introduced through that link, and just walk
519 * that, making this into an iterative implementation. Unfortunately,
520 * we probably would not be able to use tail recursion for the recursive
521 * solution, so a deep network could exhaust our stack space; therefore
522 * I favor the extra linked list, even though that increases the
523 * complexity of the database. -Kev
525 void sendcmdto_match_butone(struct Client *from, const char *cmd,
526 const char *tok, const char *to,
527 struct Client *one, unsigned int who,
528 const char *pattern, ...)
532 struct MsgBuf *user_mb;
533 struct MsgBuf *serv_mb;
535 vd.vd_format = pattern;
537 /* Build buffer to send to users */
538 va_start(vd.vd_args, pattern);
539 user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
542 /* Build buffer to send to servers */
543 va_start(vd.vd_args, pattern);
544 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
547 /* send buffer along */
549 for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
550 if (cli_from(cptr) == one || IsServer(cptr) || IsMe(cptr) ||
551 !match_it(cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
552 sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
553 continue; /* skip it */
554 sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
556 if (MyConnect(cptr)) /* send right buffer */
557 send_buffer(cptr, user_mb, 0);
559 send_buffer(cptr, serv_mb, 0);
567 * Send a server notice to all users subscribing to the indicated <mask>
570 void sendto_opmask_butone(struct Client *one, unsigned int mask,
571 const char *pattern, ...)
575 va_start(vl, pattern);
576 vsendto_opmask_butone(one, mask, pattern, vl);
581 * Same as above, except called with a variable argument list
583 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
584 const char *pattern, va_list vl)
588 int i = 0; /* so that 1 points to opsarray[0] */
589 struct SLink *opslist;
594 if (!(opslist = opsarray[i]))
598 * build string; I don't want to bother with client nicknames, so I hope
601 vd.vd_format = pattern;
603 mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
606 for (; opslist; opslist = opslist->next)
607 send_buffer(opslist->value.cptr, mb, 0);