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.
29 #include "ircd_snprintf.h"
30 #include "ircd_string.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() */
51 static struct Connection *send_queues = 0;
56 * An error has been detected. The link *must* be closed,
57 * but *cannot* call ExitClient (m_bye) from here.
58 * Instead, mark it with FLAGS_DEADSOCKET. This should
59 * generate ExitClient from the main loop.
61 * If 'notice' is not NULL, it is assumed to be a format
62 * for a message to local opers. It can contain only one
63 * '%s', which will be replaced by the sockhost field of
66 * Also, the notice is skipped for "uninteresting" cases,
67 * like Persons and yet unknown connections...
70 static void dead_link(struct Client *to, char *notice)
72 cli_flags(to) |= FLAGS_DEADSOCKET;
74 * If because of BUFFERPOOL problem then clean dbuf's now so that
75 * notices don't hurt operators below.
77 DBufClear(&(cli_recvQ(to)));
78 MsgQClear(&(cli_sendQ(to)));
79 client_drop_sendq(cli_connect(to));
82 * Keep a copy of the last comment, for later use...
84 ircd_strncpy(cli_info(to), notice, REALLEN);
86 if (!IsUser(to) && !IsUnknown(to) && !(cli_flags(to) & FLAGS_CLOSING))
87 sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
88 Debug((DEBUG_ERROR, cli_info(to)));
91 static int can_send(struct Client* to)
94 return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
100 * Used to empty all output buffers for all connections. Should only
101 * be called once per scan of connections. There should be a select in
102 * here perhaps but that means either forcing a timeout or doing a poll.
103 * When flushing, all we do is empty the obuffer array for each local
104 * client and try to send it. if we cant send it, it goes into the sendQ
107 void flush_connections(struct Client* cptr)
113 struct Connection* con;
114 for (con = send_queues; con; con = con_next(con)) {
115 assert(0 < MsgQLength(&(con_sendQ(con))));
116 send_queued(con_client(con));
124 * This function is called from the main select-loop (or whatever)
125 * when there is a chance that some output would be possible. This
126 * attempts to empty the send queue as far as possible...
128 void send_queued(struct Client *to)
131 assert(0 != cli_local(to));
133 if (IsBlocked(to) || !can_send(to))
134 return; /* Don't bother */
136 while (MsgQLength(&(cli_sendQ(to))) > 0) {
139 if ((len = deliver_it(to, &(cli_sendQ(to))))) {
140 msgq_delete(&(cli_sendQ(to)), len);
141 cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
150 sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
157 /* Ok, sendq is now empty... */
158 client_drop_sendq(cli_connect(to));
162 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
172 * This socket has already been marked as dead
176 if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
178 sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
179 "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
181 dead_link(to, "Max sendQ exceeded");
185 Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
187 msgq_add(&(cli_sendQ(to)), buf, prio);
188 client_add_sendq(cli_connect(to), &send_queues);
192 * Update statistics. The following is slightly incorrect
193 * because it counts messages even if queued, but bytes
194 * only really sent. Queued bytes get updated in SendQueued.
199 * This little bit is to stop the sendQ from growing too large when
200 * there is no need for it to. Thus we call send_queued() every time
201 * 2k has been added to the queue since the last non-fatal write.
202 * Also stops us from deliberately building a large sendQ and then
203 * trying to flood that link with data (possible during the net
204 * relinking done by servers with a large load).
206 if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
211 * Send a msg to all ppl on servers/hosts that match a specified mask
212 * (used for enhanced PRIVMSGs)
214 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
217 static int match_it(struct Client *one, const char *mask, int what)
222 return (match(mask, cli_user(one)->host) == 0);
225 return (match(mask, cli_name(cli_user(one)->server)) == 0);
230 * Send a raw command to a single client; use *ONLY* if you absolutely
231 * must send a command without a prefix.
233 void sendrawto_one(struct Client *to, const char *pattern, ...)
238 va_start(vl, pattern);
239 mb = msgq_vmake(to, pattern, vl);
242 send_buffer(to, mb, 0);
248 * Send a (prefixed) command to a single client; select which of <cmd>
249 * <tok> to use depending on if to is a server or not. <from> is the
250 * originator of the command.
252 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
253 struct Client *to, const char *pattern, ...)
260 vd.vd_format = pattern; /* set up the struct VarData for %v */
261 va_start(vd.vd_args, pattern);
263 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
268 send_buffer(to, mb, 0);
274 * Send a (prefixed) command to a single client in the priority queue;
275 * select which of <cmd> <tok> to use depending on if to is a server
276 *or not. <from> is the originator of the command.
278 void sendcmdto_prio_one(struct Client *from, const char *cmd, const char *tok,
279 struct Client *to, const char *pattern, ...)
286 vd.vd_format = pattern; /* set up the struct VarData for %v */
287 va_start(vd.vd_args, pattern);
289 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
294 send_buffer(to, mb, 1);
300 * Send a (prefixed) command to all servers but one, using tok; cmd
301 * is ignored in this particular function.
303 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
304 const char *tok, struct Client *one,
305 const char *pattern, ...)
311 vd.vd_format = pattern; /* set up the struct VarData for %v */
312 va_start(vd.vd_args, pattern);
315 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
318 /* send it to our downlinks */
319 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
320 if (one && lp->value.cptr == cli_from(one))
322 send_buffer(lp->value.cptr, mb, 0);
329 * Send a (prefix) command originating from <from> to all channels
330 * <from> is locally on. <from> must be a user. <tok> is ignored in
333 /* XXX sentalong_marker used XXX
335 * There is not an easy way to revoke the need for sentalong_marker
336 * from this function. Thoughts and ideas would be welcome... -Kev
338 * One possibility would be to internalize the sentalong array; that
339 * could be prohibitively big, though. We could get around that by
340 * making one that's the number of connected servers or something...
341 * or perhaps by adding a special flag to the servers we've sent a
342 * message to, and then a final loop through the connected servers
343 * to delete the flag. -Kev
345 void sendcmdto_common_channels(struct Client *from, const char *cmd,
346 const char *tok, const char *pattern, ...)
350 struct Membership *chan;
351 struct Membership *member;
354 assert(0 != cli_from(from));
355 assert(0 != pattern);
356 assert(!IsServer(from) && !IsMe(from));
358 vd.vd_format = pattern; /* set up the struct VarData for %v */
360 va_start(vd.vd_args, pattern);
362 /* build the buffer */
363 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
367 if (-1 < cli_fd(cli_from(from)))
368 sentalong[cli_fd(cli_from(from))] = sentalong_marker;
370 * loop through from's channels, and the members on their channels
372 for (chan = cli_user(from)->channel; chan; chan = chan->next_channel)
373 for (member = chan->channel->members; member;
374 member = member->next_member)
375 if (MyConnect(member->user) && -1 < cli_fd(cli_from(member->user)) &&
376 sentalong[cli_fd(cli_from(member->user))] != sentalong_marker) {
377 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
378 send_buffer(member->user, mb, 0);
382 send_buffer(from, mb, 0);
388 * Send a (prefixed) command to all local users on the channel specified
389 * by <to>; <tok> is ignored by this function
391 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
392 const char *tok, struct Channel *to,
393 const char *pattern, ...)
397 struct Membership *member;
399 vd.vd_format = pattern; /* set up the struct VarData for %v */
400 va_start(vd.vd_args, pattern);
402 /* build the buffer */
403 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
406 /* send the buffer to each local channel member */
407 for (member = to->members; member; member = member->next_member) {
408 if (MyConnect(member->user) && !IsZombie(member))
409 send_buffer(member->user, mb, 0);
416 * Send a (prefixed) command to all users on this channel, including
417 * remote users; users to skip may be specified by setting appropriate
418 * flags in the <skip> argument. <one> will also be skipped.
420 /* XXX sentalong_marker used XXX
422 * We can drop sentalong_marker from this function by adding a field to
423 * channels and to connections; what we do is make a user's connection
424 * a "member" of the channel by adding it to the new list, and we use
425 * the struct Membership status as a reference count. Then, to implement
426 * this function, we just walk the list of connections. Unfortunately,
427 * this doesn't account for sending only to channel ops, or for not
428 * sending to +d users; we could account for that by splitting those
429 * counts out, but that would imply adding two more fields (at least) to
430 * the struct Membership... -Kev
432 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
433 const char *tok, struct Channel *to,
434 struct Client *one, unsigned int skip,
435 const char *pattern, ...)
437 struct Membership *member;
439 struct MsgBuf *user_mb;
440 struct MsgBuf *serv_mb;
442 vd.vd_format = pattern;
444 /* Build buffer to send to users */
445 va_start(vd.vd_args, pattern);
446 user_mb = msgq_make(0, skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v",
447 from, skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
450 /* Build buffer to send to servers */
451 va_start(vd.vd_args, pattern);
452 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
455 /* send buffer along! */
457 for (member = to->members; member; member = member->next_member) {
458 /* skip one, zombies, and deaf users... */
459 if (cli_from(member->user) == one || IsZombie(member) ||
460 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
461 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
462 (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
463 cli_fd(cli_from(member->user)) < 0 ||
464 sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
466 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
468 if (MyConnect(member->user)) /* pick right buffer to send */
469 send_buffer(member->user, user_mb, 0);
471 send_buffer(member->user, serv_mb, 0);
479 * Send a (prefixed) command to all users except <one> that have
482 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
483 const char *tok, struct Client *one,
484 unsigned int flag, const char *pattern, ...)
490 unsigned int oper_fl = 0;
493 if (flag & FLAGS_OPER) {
498 vd.vd_format = pattern;
500 /* Build buffer to send to users */
501 va_start(vd.vd_args, pattern);
502 mb = msgq_make(0, "%:#C " MSG_WALLOPS " %v", from, &vd);
505 /* send buffer along! */
506 for (i = 0; i <= HighestFd; i++) {
507 if (!(cptr = LocalClientArray[i]) || !(cli_flags(cptr) & flag) ||
508 (oper_fl && !IsAnOper(cptr)) || cli_fd(cli_from(cptr)) < 0)
509 continue; /* skip it */
510 send_buffer(cptr, mb, 1);
515 /* Build buffer to send to servers */
516 va_start(vd.vd_args, pattern);
517 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
520 /* send buffer along! */
521 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
522 if (one && lp->value.cptr == cli_from(one))
524 send_buffer(lp->value.cptr, mb, 1);
531 * Send a (prefixed) command to all users who match <to>, under control
534 /* XXX sentalong_marker used XXX
536 * This is also a difficult one to solve. The basic approach would be
537 * to walk the client list of each connected server until we find a
538 * match--but then, we also have to walk the client list of all the
539 * servers behind that one. We could implement this recursively--or we
540 * could add (yet another) field to the connection struct that would be
541 * a linked list of clients introduced through that link, and just walk
542 * that, making this into an iterative implementation. Unfortunately,
543 * we probably would not be able to use tail recursion for the recursive
544 * solution, so a deep network could exhaust our stack space; therefore
545 * I favor the extra linked list, even though that increases the
546 * complexity of the database. -Kev
548 void sendcmdto_match_butone(struct Client *from, const char *cmd,
549 const char *tok, const char *to,
550 struct Client *one, unsigned int who,
551 const char *pattern, ...)
555 struct MsgBuf *user_mb;
556 struct MsgBuf *serv_mb;
558 vd.vd_format = pattern;
560 /* Build buffer to send to users */
561 va_start(vd.vd_args, pattern);
562 user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
565 /* Build buffer to send to servers */
566 va_start(vd.vd_args, pattern);
567 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
570 /* send buffer along */
572 for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
573 if (!IsRegistered(cptr) || cli_from(cptr) == one || IsServer(cptr) ||
574 IsMe(cptr) || !match_it(cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
575 sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
576 continue; /* skip it */
577 sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
579 if (MyConnect(cptr)) /* send right buffer */
580 send_buffer(cptr, user_mb, 0);
582 send_buffer(cptr, serv_mb, 0);
590 * Send a server notice to all users subscribing to the indicated <mask>
593 void sendto_opmask_butone(struct Client *one, unsigned int mask,
594 const char *pattern, ...)
598 va_start(vl, pattern);
599 vsendto_opmask_butone(one, mask, pattern, vl);
604 * Send a server notice to all users subscribing to the indicated <mask>
605 * except for <one> - Ratelimited 1 / 30sec
607 void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
608 time_t *rate, const char *pattern, ...)
612 if ((CurrentTime - *rate) < 30)
617 va_start(vl, pattern);
618 vsendto_opmask_butone(one, mask, pattern, vl);
625 * Same as above, except called with a variable argument list
627 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
628 const char *pattern, va_list vl)
632 int i = 0; /* so that 1 points to opsarray[0] */
633 struct SLink *opslist;
638 if (!(opslist = opsarray[i]))
642 * build string; I don't want to bother with client nicknames, so I hope
645 vd.vd_format = pattern;
647 mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
650 for (; opslist; opslist = opslist->next)
651 send_buffer(opslist->value.cptr, mb, 0);