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"
39 #include "sprintf_irc.h"
48 static int sentalong[MAXCONNECTIONS];
49 static int sentalong_marker;
50 struct SLink *opsarray[32]; /* don't use highest bit unless you change
51 atoi to strtoul in sendto_op_mask() */
52 static struct Connection *send_queues = 0;
57 * An error has been detected. The link *must* be closed,
58 * but *cannot* call ExitClient (m_bye) from here.
59 * Instead, mark it with FLAGS_DEADSOCKET. This should
60 * generate ExitClient from the main loop.
62 * If 'notice' is not NULL, it is assumed to be a format
63 * for a message to local opers. It can contain only one
64 * '%s', which will be replaced by the sockhost field of
67 * Also, the notice is skipped for "uninteresting" cases,
68 * like Persons and yet unknown connections...
71 static void dead_link(struct Client *to, char *notice)
73 cli_flags(to) |= FLAGS_DEADSOCKET;
75 * If because of BUFFERPOOL problem then clean dbuf's now so that
76 * notices don't hurt operators below.
78 DBufClear(&(cli_recvQ(to)));
79 MsgQClear(&(cli_sendQ(to)));
80 client_drop_sendq(cli_connect(to));
83 * Keep a copy of the last comment, for later use...
85 ircd_strncpy(cli_info(to), notice, REALLEN);
87 if (!IsUser(to) && !IsUnknown(to) && !(cli_flags(to) & FLAGS_CLOSING))
88 sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
89 Debug((DEBUG_ERROR, cli_info(to)));
92 static int can_send(struct Client* to)
95 return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
101 * Used to empty all output buffers for all connections. Should only
102 * be called once per scan of connections. There should be a select in
103 * here perhaps but that means either forcing a timeout or doing a poll.
104 * When flushing, all we do is empty the obuffer array for each local
105 * client and try to send it. if we cant send it, it goes into the sendQ
108 void flush_connections(struct Client* cptr)
114 struct Connection* con;
115 for (con = send_queues; con; con = con_next(con)) {
116 assert(0 < MsgQLength(&(con_sendQ(con))));
117 send_queued(con_client(con));
125 * This function is called from the main select-loop (or whatever)
126 * when there is a chance that some output would be possible. This
127 * attempts to empty the send queue as far as possible...
129 void send_queued(struct Client *to)
132 assert(0 != cli_local(to));
134 if (IsBlocked(to) || !can_send(to))
135 return; /* Don't bother */
137 while (MsgQLength(&(cli_sendQ(to))) > 0) {
140 if ((len = deliver_it(to, &(cli_sendQ(to))))) {
141 msgq_delete(&(cli_sendQ(to)), len);
142 cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
151 sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
158 /* Ok, sendq is now empty... */
159 client_drop_sendq(cli_connect(to));
163 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
173 * This socket has already been marked as dead
177 if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
179 sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
180 "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
182 dead_link(to, "Max sendQ exceeded");
186 Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
188 msgq_add(&(cli_sendQ(to)), buf, prio);
189 client_add_sendq(cli_connect(to), &send_queues);
193 * Update statistics. The following is slightly incorrect
194 * because it counts messages even if queued, but bytes
195 * only really sent. Queued bytes get updated in SendQueued.
200 * This little bit is to stop the sendQ from growing too large when
201 * there is no need for it to. Thus we call send_queued() every time
202 * 2k has been added to the queue since the last non-fatal write.
203 * Also stops us from deliberately building a large sendQ and then
204 * trying to flood that link with data (possible during the net
205 * relinking done by servers with a large load).
207 if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
212 * Send a msg to all ppl on servers/hosts that match a specified mask
213 * (used for enhanced PRIVMSGs)
215 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
218 static int match_it(struct Client *one, const char *mask, int what)
223 return (match(mask, cli_user(one)->host) == 0);
226 return (match(mask, cli_name(cli_user(one)->server)) == 0);
231 * Send a raw command to a single client; use *ONLY* if you absolutely
232 * must send a command without a prefix.
234 void sendrawto_one(struct Client *to, const char *pattern, ...)
239 va_start(vl, pattern);
240 mb = msgq_vmake(to, pattern, vl);
243 send_buffer(to, mb, 0);
249 * Send a (prefixed) command to a single client; select which of <cmd>
250 * <tok> to use depending on if to is a server or not. <from> is the
251 * originator of the command.
253 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
254 struct Client *to, const char *pattern, ...)
258 va_start(vl, pattern);
259 vsendcmdto_one(from, cmd, tok, to, pattern, vl);
263 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
264 struct Client *to, const char *pattern, va_list vl)
271 vd.vd_format = pattern; /* set up the struct VarData for %v */
274 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
277 send_buffer(to, mb, 0);
283 * Send a (prefixed) command to all servers but one, using tok; cmd
284 * is ignored in this particular function.
286 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
287 const char *tok, struct Client *one,
288 const char *pattern, ...)
294 vd.vd_format = pattern; /* set up the struct VarData for %v */
295 va_start(vd.vd_args, pattern);
298 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
301 /* send it to our downlinks */
302 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
303 if (one && lp->value.cptr == cli_from(one))
305 send_buffer(lp->value.cptr, mb, 0);
312 * Send a (prefix) command originating from <from> to all channels
313 * <from> is locally on. <from> must be a user. <tok> is ignored in
316 /* XXX sentalong_marker used XXX
318 * There is not an easy way to revoke the need for sentalong_marker
319 * from this function. Thoughts and ideas would be welcome... -Kev
321 * One possibility would be to internalize the sentalong array; that
322 * could be prohibitively big, though. We could get around that by
323 * making one that's the number of connected servers or something...
324 * or perhaps by adding a special flag to the servers we've sent a
325 * message to, and then a final loop through the connected servers
326 * to delete the flag. -Kev
328 void sendcmdto_common_channels(struct Client *from, const char *cmd,
329 const char *tok, const char *pattern, ...)
333 struct Membership *chan;
334 struct Membership *member;
337 assert(0 != cli_from(from));
338 assert(0 != pattern);
339 assert(!IsServer(from) && !IsMe(from));
341 vd.vd_format = pattern; /* set up the struct VarData for %v */
343 va_start(vd.vd_args, pattern);
345 /* build the buffer */
346 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
350 if (-1 < cli_fd(cli_from(from)))
351 sentalong[cli_fd(cli_from(from))] = sentalong_marker;
353 * loop through from's channels, and the members on their channels
355 for (chan = cli_user(from)->channel; chan; chan = chan->next_channel)
356 for (member = chan->channel->members; member;
357 member = member->next_member)
358 if (MyConnect(member->user) && -1 < cli_fd(cli_from(member->user)) &&
359 sentalong[cli_fd(cli_from(member->user))] != sentalong_marker) {
360 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
361 send_buffer(member->user, mb, 0);
365 send_buffer(from, mb, 0);
371 * Send a (prefixed) command to all local users on the channel specified
372 * by <to>; <tok> is ignored by this function
374 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
375 const char *tok, struct Channel *to,
376 const char *pattern, ...)
380 struct Membership *member;
382 vd.vd_format = pattern; /* set up the struct VarData for %v */
383 va_start(vd.vd_args, pattern);
385 /* build the buffer */
386 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
389 /* send the buffer to each local channel member */
390 for (member = to->members; member; member = member->next_member) {
391 if (MyConnect(member->user) && !IsZombie(member))
392 send_buffer(member->user, mb, 0);
399 * Send a (prefixed) command to all users on this channel, including
400 * remote users; users to skip may be specified by setting appropriate
401 * flags in the <skip> argument. <one> will also be skipped.
403 /* XXX sentalong_marker used XXX
405 * We can drop sentalong_marker from this function by adding a field to
406 * channels and to connections; what we do is make a user's connection
407 * a "member" of the channel by adding it to the new list, and we use
408 * the struct Membership status as a reference count. Then, to implement
409 * this function, we just walk the list of connections. Unfortunately,
410 * this doesn't account for sending only to channel ops, or for not
411 * sending to +d users; we could account for that by splitting those
412 * counts out, but that would imply adding two more fields (at least) to
413 * the struct Membership... -Kev
415 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
416 const char *tok, struct Channel *to,
417 struct Client *one, unsigned int skip,
418 const char *pattern, ...)
420 struct Membership *member;
422 struct MsgBuf *user_mb;
423 struct MsgBuf *serv_mb;
425 vd.vd_format = pattern;
427 /* Build buffer to send to users */
428 va_start(vd.vd_args, pattern);
429 user_mb = msgq_make(0, skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v",
430 from, skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
433 /* Build buffer to send to servers */
434 va_start(vd.vd_args, pattern);
435 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
438 /* send buffer along! */
440 for (member = to->members; member; member = member->next_member) {
441 /* skip one, zombies, and deaf users... */
442 if (cli_from(member->user) == one || IsZombie(member) ||
443 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
444 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
445 (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
446 cli_fd(cli_from(member->user)) < 0 ||
447 sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
449 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
451 if (MyConnect(member->user)) /* pick right buffer to send */
452 send_buffer(member->user, user_mb, 0);
454 send_buffer(member->user, serv_mb, 0);
462 * Send a (prefixed) command to all users except <one> that have
465 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
466 const char *tok, struct Client *one,
467 unsigned int flag, const char *pattern, ...)
473 unsigned int oper_fl = 0;
476 if (flag & FLAGS_OPER) {
481 vd.vd_format = pattern;
483 /* Build buffer to send to users */
484 va_start(vd.vd_args, pattern);
485 mb = msgq_make(0, "%:#C " MSG_WALLOPS " %v", from, &vd);
488 /* send buffer along! */
489 for (i = 0; i <= HighestFd; i++) {
490 if (!(cptr = LocalClientArray[i]) || !(cli_flags(cptr) & flag) ||
491 (oper_fl && !IsAnOper(cptr)) || cli_fd(cli_from(cptr)) < 0)
492 continue; /* skip it */
493 send_buffer(cptr, mb, 1);
498 /* Build buffer to send to servers */
499 va_start(vd.vd_args, pattern);
500 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
503 /* send buffer along! */
504 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
505 if (one && lp->value.cptr == cli_from(one))
507 send_buffer(lp->value.cptr, mb, 1);
514 * Send a (prefixed) command to all users who match <to>, under control
517 /* XXX sentalong_marker used XXX
519 * This is also a difficult one to solve. The basic approach would be
520 * to walk the client list of each connected server until we find a
521 * match--but then, we also have to walk the client list of all the
522 * servers behind that one. We could implement this recursively--or we
523 * could add (yet another) field to the connection struct that would be
524 * a linked list of clients introduced through that link, and just walk
525 * that, making this into an iterative implementation. Unfortunately,
526 * we probably would not be able to use tail recursion for the recursive
527 * solution, so a deep network could exhaust our stack space; therefore
528 * I favor the extra linked list, even though that increases the
529 * complexity of the database. -Kev
531 void sendcmdto_match_butone(struct Client *from, const char *cmd,
532 const char *tok, const char *to,
533 struct Client *one, unsigned int who,
534 const char *pattern, ...)
538 struct MsgBuf *user_mb;
539 struct MsgBuf *serv_mb;
541 vd.vd_format = pattern;
543 /* Build buffer to send to users */
544 va_start(vd.vd_args, pattern);
545 user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
548 /* Build buffer to send to servers */
549 va_start(vd.vd_args, pattern);
550 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
553 /* send buffer along */
555 for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
556 if (cli_from(cptr) == one || IsServer(cptr) || IsMe(cptr) ||
557 !match_it(cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
558 sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
559 continue; /* skip it */
560 sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
562 if (MyConnect(cptr)) /* send right buffer */
563 send_buffer(cptr, user_mb, 0);
565 send_buffer(cptr, serv_mb, 0);
573 * Send a server notice to all users subscribing to the indicated <mask>
576 void sendto_opmask_butone(struct Client *one, unsigned int mask,
577 const char *pattern, ...)
581 va_start(vl, pattern);
582 vsendto_opmask_butone(one, mask, pattern, vl);
587 * Same as above, except called with a variable argument list
589 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
590 const char *pattern, va_list vl)
594 int i = 0; /* so that 1 points to opsarray[0] */
595 struct SLink *opslist;
600 if (!(opslist = opsarray[i]))
604 * build string; I don't want to bother with client nicknames, so I hope
607 vd.vd_format = pattern;
609 mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
612 for (; opslist; opslist = opslist->next)
613 send_buffer(opslist->value.cptr, mb, 0);