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) {
151 const char* msg = msgq_map(&to->sendQ, &len);
153 if ((len = deliver_it(to, msg, len))) {
154 msgq_delete(&to->sendQ, len);
155 to->lastsq = MsgQLength(&to->sendQ) / 1024;
162 sprintf(tmp,"Write error: %s",(strerror(to->error)) ? (strerror(to->error)) : "Unknown error" );
170 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
180 * This socket has already been marked as dead
184 if (MsgQLength(&to->sendQ) > get_sendq(to)) {
186 sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
187 "%zu > %zu", to, MsgQLength(&to->sendQ),
189 dead_link(to, "Max sendQ exceeded");
193 Debug((DEBUG_SEND, "Sending [%p] to %s", buf, to->name));
195 msgq_add(&to->sendQ, buf, prio);
198 * Update statistics. The following is slightly incorrect
199 * because it counts messages even if queued, but bytes
200 * only really sent. Queued bytes get updated in SendQueued.
205 * This little bit is to stop the sendQ from growing too large when
206 * there is no need for it to. Thus we call send_queued() every time
207 * 2k has been added to the queue since the last non-fatal write.
208 * Also stops us from deliberately building a large sendQ and then
209 * trying to flood that link with data (possible during the net
210 * relinking done by servers with a large load).
212 if (MsgQLength(&to->sendQ) / 1024 > to->lastsq)
217 * Send a msg to all ppl on servers/hosts that match a specified mask
218 * (used for enhanced PRIVMSGs)
220 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
223 static int match_it(struct Client *one, const char *mask, int what)
228 return (match(mask, one->user->host) == 0);
231 return (match(mask, one->user->server->name) == 0);
236 * Send a raw command to a single client; use *ONLY* if you absolutely
237 * must send a command without a prefix.
239 void sendrawto_one(struct Client *to, const char *pattern, ...)
244 va_start(vl, pattern);
245 mb = msgq_vmake(to, pattern, vl);
248 send_buffer(to, mb, 0);
254 * Send a (prefixed) command to a single client; select which of <cmd>
255 * <tok> to use depending on if to is a server or not. <from> is the
256 * originator of the command.
258 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
259 struct Client *to, const char *pattern, ...)
263 va_start(vl, pattern);
264 vsendcmdto_one(from, cmd, tok, to, pattern, vl);
268 void vsendcmdto_one(struct Client *from, const char *cmd, const char *tok,
269 struct Client *to, const char *pattern, va_list vl)
276 vd.vd_format = pattern; /* set up the struct VarData for %v */
279 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
282 send_buffer(to, mb, 0);
288 * Send a (prefixed) command to all servers but one, using tok; cmd
289 * is ignored in this particular function.
291 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
292 const char *tok, struct Client *one,
293 const char *pattern, ...)
299 vd.vd_format = pattern; /* set up the struct VarData for %v */
300 va_start(vd.vd_args, pattern);
303 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
306 /* send it to our downlinks */
307 for (lp = me.serv->down; lp; lp = lp->next) {
308 if (one && lp->value.cptr == one->from)
310 send_buffer(lp->value.cptr, mb, 0);
317 * Send a (prefix) command originating from <from> to all channels
318 * <from> is locally on. <from> must be a user. <tok> is ignored in
321 /* XXX sentalong_marker used XXX
323 * There is not an easy way to revoke the need for sentalong_marker
324 * from this function. Thoughts and ideas would be welcome... -Kev
326 * One possibility would be to internalize the sentalong array; that
327 * could be prohibitively big, though. We could get around that by
328 * making one that's the number of connected servers or something...
329 * or perhaps by adding a special flag to the servers we've sent a
330 * message to, and then a final loop through the connected servers
331 * to delete the flag. -Kev
333 void sendcmdto_common_channels(struct Client *from, const char *cmd,
334 const char *tok, const char *pattern, ...)
338 struct Membership *chan;
339 struct Membership *member;
342 assert(0 != from->from);
343 assert(0 != pattern);
344 assert(!IsServer(from) && !IsMe(from));
346 vd.vd_format = pattern; /* set up the struct VarData for %v */
348 va_start(vd.vd_args, pattern);
350 /* build the buffer */
351 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
355 if (-1 < from->from->fd)
356 sentalong[from->from->fd] = sentalong_marker;
358 * loop through from's channels, and the members on their channels
360 for (chan = from->user->channel; chan; chan = chan->next_channel)
361 for (member = chan->channel->members; member;
362 member = member->next_member)
363 if (MyConnect(member->user) && -1 < member->user->from->fd &&
364 sentalong[member->user->from->fd] != sentalong_marker) {
365 sentalong[member->user->from->fd] = sentalong_marker;
366 send_buffer(member->user, mb, 0);
370 send_buffer(from, mb, 0);
376 * Send a (prefixed) command to all local users on the channel specified
377 * by <to>; <tok> is ignored by this function
379 void sendcmdto_channel_butserv(struct Client *from, const char *cmd,
380 const char *tok, struct Channel *to,
381 const char *pattern, ...)
385 struct Membership *member;
387 vd.vd_format = pattern; /* set up the struct VarData for %v */
388 va_start(vd.vd_args, pattern);
390 /* build the buffer */
391 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
394 /* send the buffer to each local channel member */
395 for (member = to->members; member; member = member->next_member) {
396 if (MyConnect(member->user) && !IsZombie(member))
397 send_buffer(member->user, mb, 0);
404 * Send a (prefixed) command to all users on this channel, including
405 * remote users; users to skip may be specified by setting appropriate
406 * flags in the <skip> argument. <one> will also be skipped.
408 /* XXX sentalong_marker used XXX
410 * We can drop sentalong_marker from this function by adding a field to
411 * channels and to connections; what we do is make a user's connection
412 * a "member" of the channel by adding it to the new list, and we use
413 * the struct Membership status as a reference count. Then, to implement
414 * this function, we just walk the list of connections. Unfortunately,
415 * this doesn't account for sending only to channel ops, or for not
416 * sending to +d users; we could account for that by splitting those
417 * counts out, but that would imply adding two more fields (at least) to
418 * the struct Membership... -Kev
420 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
421 const char *tok, struct Channel *to,
422 struct Client *one, unsigned int skip,
423 const char *pattern, ...)
425 struct Membership *member;
427 struct MsgBuf *user_mb;
428 struct MsgBuf *serv_mb;
430 vd.vd_format = pattern;
432 /* Build buffer to send to users */
433 va_start(vd.vd_args, pattern);
434 user_mb = msgq_make(0, skip & SKIP_NONOPS ? "%:#C %s @%v" : "%:#C %s %v",
435 from, skip & SKIP_NONOPS ? MSG_NOTICE : cmd, &vd);
438 /* Build buffer to send to servers */
439 va_start(vd.vd_args, pattern);
440 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
443 /* send buffer along! */
445 for (member = to->members; member; member = member->next_member) {
446 /* skip one, zombies, and deaf users... */
447 if (member->user->from == one || IsZombie(member) ||
448 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
449 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
450 (skip & SKIP_BURST && IsBurstOrBurstAck(member->user->from)) ||
451 member->user->from->fd < 0 ||
452 sentalong[member->user->from->fd] == sentalong_marker)
454 sentalong[member->user->from->fd] = sentalong_marker;
456 if (MyConnect(member->user)) /* pick right buffer to send */
457 send_buffer(member->user, user_mb, 0);
459 send_buffer(member->user, serv_mb, 0);
467 * Send a (prefixed) command to all users except <one> that have
470 /* XXX sentalong_marker used XXX
472 * Again, we can solve this use of sentalong_marker by adding a field
473 * to connections--a count of the number of +w users, and another count
474 * of +g users. Then, just walk through the local clients to send
475 * those messages, and another walk through the connected servers list,
476 * sending only if there's a non-zero count. No caveats here, either,
477 * beyond remembering to decrement the count when a user /quit's or is
478 * killed, or a server is squit. -Kev
480 void sendcmdto_flag_butone(struct Client *from, const char *cmd,
481 const char *tok, struct Client *one,
482 unsigned int flag, const char *pattern, ...)
486 struct MsgBuf *user_mb;
487 struct MsgBuf *serv_mb;
489 vd.vd_format = pattern;
491 /* Build buffer to send to users */
492 va_start(vd.vd_args, pattern);
493 user_mb = msgq_make(0, "%:#C " MSG_WALLOPS " %v", from, &vd);
496 /* Build buffer to send to servers */
497 va_start(vd.vd_args, pattern);
498 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
501 /* send buffer along! */
503 for (cptr = GlobalClientList; cptr; cptr = cptr->next) {
504 if (cptr->from == one || IsServer(cptr) || !(cptr->flags & flag) ||
505 cptr->from->fd < 0 || sentalong[cptr->from->fd] == sentalong_marker)
506 continue; /* skip it */
507 sentalong[cptr->from->fd] = 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 (cptr->from == one || IsServer(cptr) || IsMe(cptr) ||
563 !match_it(cptr, to, who) || cptr->from->fd < 0 ||
564 sentalong[cptr->from->fd] == sentalong_marker)
565 continue; /* skip it */
566 sentalong[cptr->from->fd] = 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);