| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
* send.c: Functions for sending messages. |
| 4 |
* |
| 5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 20 |
* USA |
| 21 |
* |
| 22 |
* $Id$ |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "stdinc.h" |
| 26 |
#include "list.h" |
| 27 |
#include "send.h" |
| 28 |
#include "channel.h" |
| 29 |
#include "client.h" |
| 30 |
#include "msgq.h" |
| 31 |
#include "irc_string.h" |
| 32 |
#include "ircd.h" |
| 33 |
#include "numeric.h" |
| 34 |
#include "s_bsd.h" |
| 35 |
#include "s_serv.h" |
| 36 |
#include "conf.h" |
| 37 |
#include "log.h" |
| 38 |
#include "memory.h" |
| 39 |
#include "packet.h" |
| 40 |
#include "xsnprintf.h" |
| 41 |
|
| 42 |
|
| 43 |
static uint64_t current_serial = 0; |
| 44 |
/** Linked list of all connections with data queued to send. */ |
| 45 |
static struct Connection *send_queues; |
| 46 |
|
| 47 |
|
| 48 |
/* |
| 49 |
* dead_link |
| 50 |
* |
| 51 |
* An error has been detected. The link *must* be closed, |
| 52 |
* but *cannot* call ExitClient (m_bye) from here. |
| 53 |
* Instead, mark it with FLAGS_DEADSOCKET. This should |
| 54 |
* generate ExitClient from the main loop. |
| 55 |
* |
| 56 |
* If 'notice' is not NULL, it is assumed to be a format |
| 57 |
* for a message to local opers. It can contain only one |
| 58 |
* '%s', which will be replaced by the sockhost field of |
| 59 |
* the failing link. |
| 60 |
* |
| 61 |
* Also, the notice is skipped for "uninteresting" cases, |
| 62 |
* like Persons and yet unknown connections... |
| 63 |
*/ |
| 64 |
/** Mark a client as dead, even if they are not the current message source. |
| 65 |
* This is done by setting the DEADSOCKET flag on the user and letting the |
| 66 |
* main loop perform the actual exit logic. |
| 67 |
* @param[in,out] to Client being killed. |
| 68 |
* @param[in] notice Message for local opers. |
| 69 |
*/ |
| 70 |
static void |
| 71 |
dead_link(struct Client *to, const char *notice) |
| 72 |
{ |
| 73 |
AddFlag(to, FLAGS_DEADSOCKET); |
| 74 |
|
| 75 |
/* |
| 76 |
* If because of BUFFERPOOL problem then clean dbuf's now so that |
| 77 |
* notices don't hurt operators below. |
| 78 |
*/ |
| 79 |
DBufClear(&to->localClient->recvQ); |
| 80 |
MsgQClear(&to->localClient->sendQ); |
| 81 |
client_drop_sendq(to->localClient); |
| 82 |
|
| 83 |
/* |
| 84 |
* Keep a copy of the last comment, for later use... |
| 85 |
*/ |
| 86 |
strlcpy(to->info, notice, sizeof(to->info)); |
| 87 |
|
| 88 |
if (!IsClient(to) && !IsUnknown(to) && !HasFlag(to, FLAGS_CLOSING)) |
| 89 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "%s for %s", |
| 90 |
to->info, to->name); |
| 91 |
} |
| 92 |
|
| 93 |
/** Test whether we can send to a client. |
| 94 |
* @param[in] to Client we want to send to. |
| 95 |
* @return Non-zero if we can send to the client. |
| 96 |
*/ |
| 97 |
static int |
| 98 |
can_send(struct Client *to) |
| 99 |
{ |
| 100 |
assert(to); |
| 101 |
return (IsDead(to) || IsMe(to) || s_fd(&to->localClient->socket) == -1) ? 0 : 1; |
| 102 |
} |
| 103 |
|
| 104 |
/* |
| 105 |
* flush_connections |
| 106 |
* |
| 107 |
* Used to empty all output buffers for all connections. Should only |
| 108 |
* be called once per scan of connections. There should be a select in |
| 109 |
* here perhaps but that means either forcing a timeout or doing a poll. |
| 110 |
* When flushing, all we do is empty the obuffer array for each local |
| 111 |
* client and try to send it. if we cant send it, it goes into the sendQ |
| 112 |
* -avalon |
| 113 |
*/ |
| 114 |
/** Flush data queued for one or all connections. |
| 115 |
* @param[in] client_p Client to flush (if NULL, do all). |
| 116 |
*/ |
| 117 |
void |
| 118 |
flush_connections(struct Client *client_p) |
| 119 |
{ |
| 120 |
if (client_p) |
| 121 |
send_queued(client_p); |
| 122 |
else |
| 123 |
{ |
| 124 |
struct Connection *con = send_queues; |
| 125 |
for (; con; con = con->con_next) |
| 126 |
{ |
| 127 |
assert(MsgQLength(&con->sendQ) > 0); |
| 128 |
send_queued(con->client_p); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* send_queued |
| 135 |
* |
| 136 |
* This function is called from the main select-loop (or whatever) |
| 137 |
* when there is a chance that some output would be possible. This |
| 138 |
* attempts to empty the send queue as far as possible... |
| 139 |
*/ |
| 140 |
/** Attempt to send data queued for a client. |
| 141 |
* @param[in] to Client to send data to. |
| 142 |
*/ |
| 143 |
void |
| 144 |
send_queued(struct Client *to) |
| 145 |
{ |
| 146 |
assert(to); |
| 147 |
assert(to->localClient); |
| 148 |
|
| 149 |
if (HasFlag(to, FLAGS_BLOCKED) || !can_send(to)) |
| 150 |
return; /* Don't bother */ |
| 151 |
|
| 152 |
while (MsgQLength(&to->localClient->sendQ) > 0) |
| 153 |
{ |
| 154 |
unsigned int len; |
| 155 |
|
| 156 |
if ((len = deliver_it(to, &to->localClient->sendQ))) |
| 157 |
{ |
| 158 |
msgq_delete(&to->localClient->sendQ, len); |
| 159 |
to->localClient->lastsq = MsgQLength(&to->localClient->sendQ) / 1024; |
| 160 |
|
| 161 |
if (HasFlag(to, FLAGS_BLOCKED)) |
| 162 |
{ |
| 163 |
update_write(to); |
| 164 |
return; |
| 165 |
} |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
if (IsDead(to)) |
| 170 |
{ |
| 171 |
char tmp[IRCD_BUFSIZE]; |
| 172 |
|
| 173 |
snprintf(tmp, sizeof(tmp), "Write error: %s", |
| 174 |
(strerror(to->localClient->error)) ? |
| 175 |
(strerror(to->localClient->error)) : |
| 176 |
"Unknown error" ); |
| 177 |
dead_link(to, tmp); |
| 178 |
} |
| 179 |
|
| 180 |
return; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/* Ok, sendq is now empty... */ |
| 185 |
client_drop_sendq(to->localClient); |
| 186 |
update_write(to); |
| 187 |
} |
| 188 |
|
| 189 |
/** Try to send a buffer to a client, queueing it if needed. |
| 190 |
* @param[in,out] to Client to send message to. |
| 191 |
* @param[in] buf Message to send. |
| 192 |
* @param[in] prio If non-zero, send as high priority. |
| 193 |
*/ |
| 194 |
void |
| 195 |
send_buffer(struct Client *to, struct MsgBuf *buf, const int prio) |
| 196 |
{ |
| 197 |
assert(to); |
| 198 |
assert(buf); |
| 199 |
|
| 200 |
if (to->from) |
| 201 |
to = to->from; |
| 202 |
|
| 203 |
if (!can_send(to)) |
| 204 |
/* |
| 205 |
* This socket has already been marked as dead |
| 206 |
*/ |
| 207 |
return; |
| 208 |
|
| 209 |
if (MsgQLength(&to->localClient->sendQ) > get_sendq(&to->localClient->confs)) |
| 210 |
{ |
| 211 |
if (IsServer(to)) |
| 212 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
| 213 |
"Max SendQ limit exceeded for %s: %u > %u", |
| 214 |
get_client_name(to, HIDE_IP), |
| 215 |
MsgQLength(&to->localClient->sendQ), |
| 216 |
get_sendq(&to->localClient->confs)); |
| 217 |
dead_link(to, "Max sendQ exceeded"); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
msgq_add(&to->localClient->sendQ, buf, prio); |
| 222 |
client_add_sendq(to->localClient, &send_queues); |
| 223 |
update_write(to); |
| 224 |
|
| 225 |
/* |
| 226 |
* Update statistics. The following is slightly incorrect |
| 227 |
* because it counts messages even if queued, but bytes |
| 228 |
* only really sent. Queued bytes get updated in SendQueued. |
| 229 |
*/ |
| 230 |
++to->localClient->send.messages; |
| 231 |
++me.localClient->send.messages; |
| 232 |
|
| 233 |
/* |
| 234 |
* This little bit is to stop the sendQ from growing too large when |
| 235 |
* there is no need for it to. Thus we call send_queued() every time |
| 236 |
* 2k has been added to the queue since the last non-fatal write. |
| 237 |
* Also stops us from deliberately building a large sendQ and then |
| 238 |
* trying to flood that link with data (possible during the net |
| 239 |
* relinking done by servers with a large load). |
| 240 |
*/ |
| 241 |
if (MsgQLength(&to->localClient->sendQ) / 1024 > to->localClient->lastsq) |
| 242 |
send_queued(to); |
| 243 |
} |
| 244 |
|
| 245 |
/* send_message_remote() |
| 246 |
* |
| 247 |
* inputs - pointer to client from message is being sent |
| 248 |
* - pointer to client to send to |
| 249 |
* - pointer to preformatted buffer |
| 250 |
* - length of input buffer |
| 251 |
* output - none |
| 252 |
* side effects - Despite the function name, this only sends to directly |
| 253 |
* connected clients. |
| 254 |
* |
| 255 |
*/ |
| 256 |
static void |
| 257 |
send_buffer_remote(struct Client *to, struct Client *from, |
| 258 |
struct MsgBuf *buf, const int prio) |
| 259 |
{ |
| 260 |
if (!MyConnect(to)) |
| 261 |
{ |
| 262 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
| 263 |
"server send message to %s [%s] dropped from %s(Not local server)", |
| 264 |
to->name, to->from->name, from->name); |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
/* Optimize by checking if (from && to) before everything */ |
| 269 |
/* we set to->from up there.. */ |
| 270 |
|
| 271 |
if (!MyClient(from) && IsClient(to) && (to == from->from)) |
| 272 |
{ |
| 273 |
if (IsServer(from)) |
| 274 |
{ |
| 275 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
| 276 |
"Send message to %s [%s] dropped from %s(Fake Dir)", |
| 277 |
to->name, to->from->name, from->name); |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
| 282 |
"Ghosted: %s[%s@%s] from %s[%s@%s] (%s)", |
| 283 |
to->name, to->username, to->host, |
| 284 |
from->name, from->username, from->host, |
| 285 |
to->from->name); |
| 286 |
|
| 287 |
sendto_server(NULL, CAP_TS6, NOCAPS, |
| 288 |
":%s KILL %s :%s (%s[%s@%s] Ghosted %s)", |
| 289 |
me.id, to->name, me.name, to->name, |
| 290 |
to->username, to->host, to->from->name); |
| 291 |
sendto_server(NULL, NOCAPS, CAP_TS6, |
| 292 |
":%s KILL %s :%s (%s[%s@%s] Ghosted %s)", |
| 293 |
me.name, to->name, me.name, to->name, |
| 294 |
to->username, to->host, to->from->name); |
| 295 |
|
| 296 |
AddFlag(to, FLAGS_KILLED); |
| 297 |
|
| 298 |
if (IsClient(from)) |
| 299 |
sendto_one(from, form_str(ERR_GHOSTEDCLIENT), |
| 300 |
me.name, from->name, to->name, to->username, |
| 301 |
to->host, to->from); |
| 302 |
|
| 303 |
exit_client(NULL, to, &me, "Ghosted client"); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
send_buffer(to, buf, prio); |
| 308 |
} |
| 309 |
|
| 310 |
/* sendto_one() |
| 311 |
* |
| 312 |
* inputs - pointer to destination client |
| 313 |
* - var args message |
| 314 |
* output - NONE |
| 315 |
* side effects - send message to single client |
| 316 |
*/ |
| 317 |
void |
| 318 |
sendto_one(struct Client *to, const char *pattern, ...) |
| 319 |
{ |
| 320 |
struct MsgBuf *mb; |
| 321 |
va_list vl; |
| 322 |
|
| 323 |
va_start(vl, pattern); |
| 324 |
mb = msgq_vmake(to, pattern, vl); |
| 325 |
va_end(vl); |
| 326 |
|
| 327 |
send_buffer(to, mb, 0); |
| 328 |
|
| 329 |
msgq_clean(mb); |
| 330 |
} |
| 331 |
|
| 332 |
/* sendto_channel_butone() |
| 333 |
* |
| 334 |
* inputs - pointer to client(server) to NOT send message to |
| 335 |
* - pointer to client that is sending this message |
| 336 |
* - pointer to channel being sent to |
| 337 |
* - vargs message |
| 338 |
* output - NONE |
| 339 |
* side effects - message as given is sent to given channel members. |
| 340 |
* |
| 341 |
* WARNING - +D clients are ignored |
| 342 |
*/ |
| 343 |
void |
| 344 |
sendto_channel_butone(struct Client *one, struct Client *from, |
| 345 |
struct Channel *chptr, unsigned int type, |
| 346 |
const char *pattern, ...) |
| 347 |
{ |
| 348 |
struct VarData vd; |
| 349 |
struct MsgBuf *remote_mb = NULL; |
| 350 |
struct MsgBuf *local_mb = NULL; |
| 351 |
struct MsgBuf *uid_mb = NULL; |
| 352 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
| 353 |
|
| 354 |
vd.vd_format = pattern; |
| 355 |
va_start(vd.vd_args, pattern); |
| 356 |
|
| 357 |
if (IsServer(from)) |
| 358 |
local_mb = msgq_make(NULL, ":%s %v",&vd); |
| 359 |
|
| 360 |
else |
| 361 |
local_mb = msgq_make(NULL, ":%s!%s@%s %v", |
| 362 |
from->name, |
| 363 |
from->username, from->host, &vd); |
| 364 |
va_end(vd.vd_args); |
| 365 |
|
| 366 |
va_start(vd.vd_args, pattern); |
| 367 |
remote_mb = msgq_make(NULL, ":%s %v", from->name, &vd); |
| 368 |
va_end(vd.vd_args); |
| 369 |
|
| 370 |
va_start(vd.vd_args, pattern); |
| 371 |
uid_mb = msgq_make(NULL, ":%s %v", ID(from), &vd); |
| 372 |
va_end(vd.vd_args); |
| 373 |
|
| 374 |
|
| 375 |
++current_serial; |
| 376 |
|
| 377 |
DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->members.head) |
| 378 |
{ |
| 379 |
struct Membership *ms = ptr->data; |
| 380 |
struct Client *target_p = ms->client_p; |
| 381 |
|
| 382 |
assert(IsClient(target_p)); |
| 383 |
|
| 384 |
if (HasUMode(target_p, UMODE_DEAF) || target_p->from == one) |
| 385 |
continue; |
| 386 |
|
| 387 |
if (type != 0 && (ms->flags & type) == 0) |
| 388 |
continue; |
| 389 |
|
| 390 |
if (MyConnect(target_p)) |
| 391 |
{ |
| 392 |
if (target_p->localClient->serial != current_serial) |
| 393 |
{ |
| 394 |
send_buffer(target_p, local_mb, 0); |
| 395 |
target_p->localClient->serial = current_serial; |
| 396 |
} |
| 397 |
} |
| 398 |
else |
| 399 |
{ |
| 400 |
/* Now check whether a message has been sent to this |
| 401 |
* remote link already |
| 402 |
*/ |
| 403 |
if (target_p->from->localClient->serial != current_serial) |
| 404 |
{ |
| 405 |
if (IsCapable(target_p->from, CAP_TS6)) |
| 406 |
send_buffer_remote(target_p->from, from, uid_mb, 0); |
| 407 |
else |
| 408 |
send_buffer_remote(target_p->from, from, remote_mb, 0); |
| 409 |
target_p->from->localClient->serial = current_serial; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
msgq_clean(local_mb); |
| 415 |
msgq_clean(remote_mb); |
| 416 |
msgq_clean(uid_mb); |
| 417 |
} |
| 418 |
|
| 419 |
/* sendto_server() |
| 420 |
* |
| 421 |
* inputs - pointer to client to NOT send to |
| 422 |
* - pointer to channel |
| 423 |
* - caps or'd together which must ALL be present |
| 424 |
* - caps or'd together which must ALL NOT be present |
| 425 |
* - printf style format string |
| 426 |
* - args to format string |
| 427 |
* output - NONE |
| 428 |
* side effects - Send a message to all connected servers, except the |
| 429 |
* client 'one' (if non-NULL), as long as the servers |
| 430 |
* support ALL capabs in 'caps', and NO capabs in 'nocaps'. |
| 431 |
* |
| 432 |
* This function was written in an attempt to merge together the other |
| 433 |
* billion sendto_*serv*() functions, which sprung up with capabs, |
| 434 |
* lazylinks, uids, etc. |
| 435 |
* -davidt |
| 436 |
*/ |
| 437 |
void |
| 438 |
sendto_server(struct Client *one, |
| 439 |
const unsigned int caps, |
| 440 |
const unsigned int nocaps, |
| 441 |
const char *pattern, ...) |
| 442 |
{ |
| 443 |
dlink_node *ptr = NULL; |
| 444 |
struct MsgBuf *mb = NULL; |
| 445 |
va_list vl; |
| 446 |
|
| 447 |
va_start(vl, pattern); |
| 448 |
mb = msgq_vmake(&me, pattern, vl); |
| 449 |
va_end(vl); |
| 450 |
|
| 451 |
DLINK_FOREACH(ptr, serv_list.head) |
| 452 |
{ |
| 453 |
struct Client *client_p = ptr->data; |
| 454 |
|
| 455 |
/* check against 'one' */ |
| 456 |
if (one != NULL && (client_p == one->from)) |
| 457 |
continue; |
| 458 |
/* check we have required capabs */ |
| 459 |
if ((client_p->localClient->caps & caps) != caps) |
| 460 |
continue; |
| 461 |
/* check we don't have any forbidden capabs */ |
| 462 |
if ((client_p->localClient->caps & nocaps) != 0) |
| 463 |
continue; |
| 464 |
|
| 465 |
send_buffer(client_p, mb, 0); |
| 466 |
} |
| 467 |
|
| 468 |
msgq_clean(mb); |
| 469 |
} |
| 470 |
|
| 471 |
/* sendto_common_channels_local() |
| 472 |
* |
| 473 |
* inputs - pointer to client |
| 474 |
* - pattern to send |
| 475 |
* output - NONE |
| 476 |
* side effects - Sends a message to all people on local server who are |
| 477 |
* in same channel with user. |
| 478 |
* used by m_nick.c and exit_one_client. |
| 479 |
*/ |
| 480 |
void |
| 481 |
sendto_common_channels_local(struct Client *user, int touser, unsigned int cap, |
| 482 |
const char *pattern, ...) |
| 483 |
{ |
| 484 |
dlink_node *uptr; |
| 485 |
dlink_node *cptr; |
| 486 |
struct Channel *chptr; |
| 487 |
struct Membership *ms; |
| 488 |
struct Client *target_p; |
| 489 |
struct MsgBuf *mb; |
| 490 |
va_list vl; |
| 491 |
|
| 492 |
va_start(vl, pattern); |
| 493 |
mb = msgq_vmake(NULL, pattern, vl); |
| 494 |
va_end(vl); |
| 495 |
|
| 496 |
++current_serial; |
| 497 |
|
| 498 |
DLINK_FOREACH(cptr, user->channel.head) |
| 499 |
{ |
| 500 |
chptr = ((struct Membership *) cptr->data)->chptr; |
| 501 |
assert(chptr != NULL); |
| 502 |
|
| 503 |
DLINK_FOREACH(uptr, chptr->members.head) |
| 504 |
{ |
| 505 |
ms = uptr->data; |
| 506 |
target_p = ms->client_p; |
| 507 |
assert(target_p != NULL); |
| 508 |
|
| 509 |
if (!MyConnect(target_p) || target_p == user || |
| 510 |
target_p->localClient->serial == current_serial) |
| 511 |
continue; |
| 512 |
|
| 513 |
if (HasCap(target_p, cap) != cap) |
| 514 |
continue; |
| 515 |
|
| 516 |
target_p->localClient->serial = current_serial; |
| 517 |
send_buffer(target_p, mb, 0); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
if (touser && MyConnect(user) && |
| 522 |
user->localClient->serial != current_serial) |
| 523 |
if (HasCap(user, cap) == cap) |
| 524 |
send_buffer(user, mb, 0); |
| 525 |
msgq_clean(mb); |
| 526 |
} |
| 527 |
|
| 528 |
/* sendto_channel_local() |
| 529 |
* |
| 530 |
* inputs - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE |
| 531 |
* - whether to ignore +D clients (YES/NO) |
| 532 |
* - pointer to channel to send to |
| 533 |
* - var args pattern |
| 534 |
* output - NONE |
| 535 |
* side effects - Send a message to all members of a channel that are |
| 536 |
* locally connected to this server. |
| 537 |
*/ |
| 538 |
void |
| 539 |
sendto_channel_local(int type, int nodeaf, struct Channel *chptr, |
| 540 |
const char *pattern, ...) |
| 541 |
{ |
| 542 |
dlink_node *ptr; |
| 543 |
struct MsgBuf *mb; |
| 544 |
va_list vl; |
| 545 |
|
| 546 |
va_start(vl, pattern); |
| 547 |
mb = msgq_vmake(NULL, pattern, vl); |
| 548 |
va_end(vl); |
| 549 |
|
| 550 |
DLINK_FOREACH(ptr, chptr->members.head) |
| 551 |
{ |
| 552 |
struct Membership *ms = ptr->data; |
| 553 |
|
| 554 |
if (type != 0 && (ms->flags & type) == 0) |
| 555 |
continue; |
| 556 |
|
| 557 |
if (!MyConnect(ms->client_p) || |
| 558 |
(nodeaf && HasUMode(ms->client_p, UMODE_DEAF))) |
| 559 |
continue; |
| 560 |
|
| 561 |
send_buffer(ms->client_p, mb, 0); |
| 562 |
} |
| 563 |
|
| 564 |
msgq_clean(mb); |
| 565 |
} |
| 566 |
|
| 567 |
/* sendto_channel_local_butone() |
| 568 |
* |
| 569 |
* inputs - pointer to client to NOT send message to |
| 570 |
* - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE |
| 571 |
* - pointer to channel to send to |
| 572 |
* - var args pattern |
| 573 |
* output - NONE |
| 574 |
* side effects - Send a message to all members of a channel that are |
| 575 |
* locally connected to this server except one. |
| 576 |
* |
| 577 |
* WARNING - +D clients are omitted |
| 578 |
*/ |
| 579 |
void |
| 580 |
sendto_channel_local_butone(struct Client *one, int type, unsigned int cap, |
| 581 |
struct Channel *chptr, const char *pattern, ...) |
| 582 |
{ |
| 583 |
struct Client *target_p; |
| 584 |
struct Membership *ms; |
| 585 |
dlink_node *ptr; |
| 586 |
struct MsgBuf *mb; |
| 587 |
va_list vl; |
| 588 |
|
| 589 |
va_start(vl, pattern); |
| 590 |
mb = msgq_vmake(NULL, pattern, vl); |
| 591 |
va_end(vl); |
| 592 |
|
| 593 |
DLINK_FOREACH(ptr, chptr->members.head) |
| 594 |
{ |
| 595 |
ms = ptr->data; |
| 596 |
target_p = ms->client_p; |
| 597 |
|
| 598 |
if (type != 0 && (ms->flags & type) == 0) |
| 599 |
continue; |
| 600 |
|
| 601 |
if (!MyConnect(target_p) || target_p == one || |
| 602 |
HasUMode(target_p, UMODE_DEAF)) |
| 603 |
continue; |
| 604 |
|
| 605 |
if (HasCap(target_p, cap) != cap) |
| 606 |
continue; |
| 607 |
send_buffer(target_p, mb, 0); |
| 608 |
} |
| 609 |
|
| 610 |
msgq_clean(mb); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
/* sendto_channel_remote() |
| 615 |
* |
| 616 |
* inputs - Client not to send towards |
| 617 |
* - Client from whom message is from |
| 618 |
* - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE |
| 619 |
* - pointer to channel to send to |
| 620 |
* - var args pattern |
| 621 |
* output - NONE |
| 622 |
* side effects - Send a message to all members of a channel that are |
| 623 |
* remote to this server. |
| 624 |
*/ |
| 625 |
void |
| 626 |
sendto_channel_remote(struct Client *one, struct Client *from, int type, |
| 627 |
const unsigned int caps, const unsigned int nocaps, |
| 628 |
struct Channel *chptr, const char *pattern, ...) |
| 629 |
{ |
| 630 |
dlink_node *ptr; |
| 631 |
struct Client *target_p; |
| 632 |
struct Membership *ms; |
| 633 |
|
| 634 |
struct MsgBuf *mb; |
| 635 |
va_list vl; |
| 636 |
|
| 637 |
va_start(vl, pattern); |
| 638 |
mb = msgq_vmake(&me, pattern, vl); |
| 639 |
va_end(vl); |
| 640 |
|
| 641 |
++current_serial; |
| 642 |
|
| 643 |
DLINK_FOREACH(ptr, chptr->members.head) |
| 644 |
{ |
| 645 |
ms = ptr->data; |
| 646 |
target_p = ms->client_p; |
| 647 |
|
| 648 |
if (type != 0 && (ms->flags & type) == 0) |
| 649 |
continue; |
| 650 |
|
| 651 |
if (MyConnect(target_p)) |
| 652 |
continue; |
| 653 |
target_p = target_p->from; |
| 654 |
|
| 655 |
if (target_p == one->from || |
| 656 |
((target_p->from->localClient->caps & caps) != caps) || |
| 657 |
((target_p->from->localClient->caps & nocaps) != 0)) |
| 658 |
continue; |
| 659 |
if (target_p->from->localClient->serial != current_serial) |
| 660 |
{ |
| 661 |
send_buffer(target_p, mb, 0); |
| 662 |
target_p->from->localClient->serial = current_serial; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
msgq_clean(mb); |
| 667 |
} |
| 668 |
|
| 669 |
/* |
| 670 |
** match_it() and sendto_match_butone() ARE only used |
| 671 |
** to send a msg to all ppl on servers/hosts that match a specified mask |
| 672 |
** (used for enhanced PRIVMSGs) for opers |
| 673 |
** |
| 674 |
** addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de) |
| 675 |
** |
| 676 |
*/ |
| 677 |
|
| 678 |
/* match_it() |
| 679 |
* |
| 680 |
* inputs - client pointer to match on |
| 681 |
* - actual mask to match |
| 682 |
* - what to match on, HOST or SERVER |
| 683 |
* output - 1 or 0 if match or not |
| 684 |
* side effects - NONE |
| 685 |
*/ |
| 686 |
static int |
| 687 |
match_it(const struct Client *one, const char *mask, const int what) |
| 688 |
{ |
| 689 |
if (what == MATCH_HOST) |
| 690 |
return !match(mask, one->host); |
| 691 |
|
| 692 |
return !match(mask, one->servptr->name); |
| 693 |
} |
| 694 |
|
| 695 |
/* sendto_match_butone() |
| 696 |
* |
| 697 |
* Send to all clients which match the mask in a way defined on 'what'; |
| 698 |
* either by user hostname or user servername. |
| 699 |
* |
| 700 |
* ugh. ONLY used by m_message.c to send an "oper magic" message. ugh. |
| 701 |
*/ |
| 702 |
void |
| 703 |
sendto_match_butone(struct Client *one, struct Client *from, const char *mask, |
| 704 |
const int what, const char *pattern, ...) |
| 705 |
{ |
| 706 |
struct VarData vd; |
| 707 |
struct MsgBuf *user_mb = NULL; |
| 708 |
struct MsgBuf *serv_mb = NULL; |
| 709 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
| 710 |
|
| 711 |
vd.vd_format = pattern; |
| 712 |
|
| 713 |
/* Build buffer to send to users */ |
| 714 |
va_start(vd.vd_args, pattern); |
| 715 |
user_mb = msgq_make(NULL, ":%s!%s@%s %v", |
| 716 |
from->name, |
| 717 |
from->username, from->host, &vd); |
| 718 |
va_end(vd.vd_args); |
| 719 |
|
| 720 |
/* Build buffer to send to servers */ |
| 721 |
va_start(vd.vd_args, pattern); |
| 722 |
serv_mb = msgq_make(&me, ":%s %v", from->name, &vd); |
| 723 |
va_end(vd.vd_args); |
| 724 |
|
| 725 |
/* scan the local clients */ |
| 726 |
DLINK_FOREACH(ptr, local_client_list.head) |
| 727 |
{ |
| 728 |
struct Client *client_p = ptr->data; |
| 729 |
|
| 730 |
if (client_p != one && match_it(client_p, mask, what)) |
| 731 |
send_buffer(client_p, user_mb, 0); |
| 732 |
} |
| 733 |
|
| 734 |
/* Now scan servers */ |
| 735 |
DLINK_FOREACH_SAFE(ptr, ptr_next, serv_list.head) |
| 736 |
{ |
| 737 |
struct Client *client_p = ptr->data; |
| 738 |
|
| 739 |
/* |
| 740 |
* The old code looped through every client on the |
| 741 |
* network for each server to check if the |
| 742 |
* server (client_p) has at least 1 client matching |
| 743 |
* the mask, using something like: |
| 744 |
* |
| 745 |
* for (target_p = GlobalClientList; target_p; target_p = target_p->next) |
| 746 |
* if (IsRegisteredUser(target_p) && |
| 747 |
* match_it(target_p, mask, what) && |
| 748 |
* (target_p->from == client_p)) |
| 749 |
* vsendto_prefix_one(client_p, from, pattern, args); |
| 750 |
* |
| 751 |
* That way, we wouldn't send the message to |
| 752 |
* a server who didn't have a matching client. |
| 753 |
* However, on a network such as EFNet, that |
| 754 |
* code would have looped through about 50 |
| 755 |
* servers, and in each loop, loop through |
| 756 |
* about 50k clients as well, calling match() |
| 757 |
* in each nested loop. That is a very bad |
| 758 |
* thing cpu wise - just send the message |
| 759 |
* to every connected server and let that |
| 760 |
* server deal with it. |
| 761 |
* -wnder |
| 762 |
*/ |
| 763 |
if (client_p != one) |
| 764 |
send_buffer_remote(client_p, from, serv_mb, 0); |
| 765 |
} |
| 766 |
|
| 767 |
msgq_clean(user_mb); |
| 768 |
msgq_clean(serv_mb); |
| 769 |
} |
| 770 |
|
| 771 |
/* sendto_match_servs() |
| 772 |
* |
| 773 |
* inputs - source client |
| 774 |
* - mask to send to |
| 775 |
* - capab needed |
| 776 |
* - data |
| 777 |
* outputs - none |
| 778 |
* side effects - data sent to servers matching with capab |
| 779 |
*/ |
| 780 |
void |
| 781 |
sendto_match_servs(struct Client *source_p, const char *mask, int cap, |
| 782 |
const char *pattern, ...) |
| 783 |
{ |
| 784 |
va_list args; |
| 785 |
struct Client *target_p; |
| 786 |
dlink_node *ptr; |
| 787 |
char buffer[IRCD_BUFSIZE]; |
| 788 |
int found = 0; |
| 789 |
|
| 790 |
va_start(args, pattern); |
| 791 |
vsnprintf(buffer, sizeof(buffer), pattern, args); |
| 792 |
va_end(args); |
| 793 |
|
| 794 |
++current_serial; |
| 795 |
|
| 796 |
DLINK_FOREACH(ptr, global_serv_list.head) |
| 797 |
{ |
| 798 |
target_p = ptr->data; |
| 799 |
|
| 800 |
/* Do not attempt to send to ourselves, or the source */ |
| 801 |
if (IsMe(target_p) || target_p->from == source_p->from) |
| 802 |
continue; |
| 803 |
|
| 804 |
if (target_p->from->localClient->serial == current_serial) |
| 805 |
continue; |
| 806 |
|
| 807 |
if (!match(mask, target_p->name)) |
| 808 |
{ |
| 809 |
/* |
| 810 |
* if we set the serial here, then we'll never do a |
| 811 |
* match() again, if !IsCapable() |
| 812 |
*/ |
| 813 |
target_p->from->localClient->serial = current_serial; |
| 814 |
found++; |
| 815 |
|
| 816 |
if (!IsCapable(target_p->from, cap)) |
| 817 |
continue; |
| 818 |
|
| 819 |
sendto_anywhere(target_p, source_p, "%s", buffer); |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
/* sendto_anywhere() |
| 825 |
* |
| 826 |
* inputs - pointer to dest client |
| 827 |
* - pointer to from client |
| 828 |
* - varags |
| 829 |
* output - NONE |
| 830 |
* side effects - less efficient than sendto_remote and sendto_one |
| 831 |
* but useful when one does not know where target "lives" |
| 832 |
*/ |
| 833 |
void |
| 834 |
sendto_anywhere(struct Client *to, struct Client *from, |
| 835 |
const char *pattern, ...) |
| 836 |
{ |
| 837 |
char buffer[IRCD_BUFSIZE]; |
| 838 |
struct VarData vd; |
| 839 |
struct MsgBuf *mb = NULL; |
| 840 |
struct Client *send_to = (to->from != NULL ? to->from : to); |
| 841 |
|
| 842 |
if (IsDead(send_to)) |
| 843 |
return; |
| 844 |
|
| 845 |
if (MyClient(to)) |
| 846 |
{ |
| 847 |
if (IsServer(from)) |
| 848 |
{ |
| 849 |
if (IsCapable(to, CAP_TS6) && HasID(from)) |
| 850 |
sprintf(buffer, ":%s", from->id); |
| 851 |
else |
| 852 |
sprintf(buffer, ":%s", from->name); |
| 853 |
} |
| 854 |
else |
| 855 |
sprintf(buffer, ":%s!%s@%s", from->name, from->username, from->host); |
| 856 |
} |
| 857 |
else |
| 858 |
sprintf(buffer, ":%s", ID_or_name(from, send_to)); |
| 859 |
|
| 860 |
vd.vd_format = pattern; |
| 861 |
va_start(vd.vd_args, pattern); |
| 862 |
|
| 863 |
mb = msgq_make(NULL, "%s %v", buffer, &vd); |
| 864 |
va_end(vd.vd_args); |
| 865 |
|
| 866 |
if (MyClient(to)) |
| 867 |
send_buffer(send_to, mb, 0); |
| 868 |
else |
| 869 |
send_buffer_remote(send_to, from, mb, 0); |
| 870 |
msgq_clean(mb); |
| 871 |
} |
| 872 |
|
| 873 |
/* sendto_realops_flags() |
| 874 |
* |
| 875 |
* inputs - flag types of messages to show to real opers |
| 876 |
* - flag indicating opers/admins |
| 877 |
* - var args input message |
| 878 |
* output - NONE |
| 879 |
* side effects - Send to *local* ops only but NOT +s nonopers. |
| 880 |
*/ |
| 881 |
void |
| 882 |
sendto_realops_flags(unsigned int flags, int level, int type, const char *pattern, ...) |
| 883 |
{ |
| 884 |
const char *ntype = NULL; |
| 885 |
dlink_node *ptr = NULL; |
| 886 |
char nbuf[IRCD_BUFSIZE]; |
| 887 |
va_list args; |
| 888 |
|
| 889 |
va_start(args, pattern); |
| 890 |
vsnprintf(nbuf, sizeof(nbuf), pattern, args); |
| 891 |
va_end(args); |
| 892 |
|
| 893 |
switch (type) |
| 894 |
{ |
| 895 |
case SEND_NOTICE: |
| 896 |
ntype = "Notice"; |
| 897 |
break; |
| 898 |
case SEND_GLOBAL: |
| 899 |
ntype = "Global"; |
| 900 |
break; |
| 901 |
case SEND_LOCOPS: |
| 902 |
ntype = "LocOps"; |
| 903 |
break; |
| 904 |
default: |
| 905 |
assert(0); |
| 906 |
} |
| 907 |
|
| 908 |
DLINK_FOREACH(ptr, oper_list.head) |
| 909 |
{ |
| 910 |
struct Client *client_p = ptr->data; |
| 911 |
assert(HasUMode(client_p, UMODE_OPER)); |
| 912 |
|
| 913 |
/* |
| 914 |
* If we're sending it to opers and they're an admin, skip. |
| 915 |
* If we're sending it to admins, and they're not, skip. |
| 916 |
*/ |
| 917 |
if (((level == L_ADMIN) && !HasUMode(client_p, UMODE_ADMIN)) || |
| 918 |
((level == L_OPER) && HasUMode(client_p, UMODE_ADMIN))) |
| 919 |
continue; |
| 920 |
|
| 921 |
if (HasUMode(client_p, flags)) |
| 922 |
sendto_one(client_p, ":%s NOTICE %s :*** %s -- %s", |
| 923 |
me.name, client_p->name, ntype, nbuf); |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
/* sendto_wallops_flags() |
| 928 |
* |
| 929 |
* inputs - flag types of messages to show to real opers |
| 930 |
* - client sending request |
| 931 |
* - var args input message |
| 932 |
* output - NONE |
| 933 |
* side effects - Send a wallops to local opers |
| 934 |
*/ |
| 935 |
void |
| 936 |
sendto_wallops_flags(unsigned int flags, struct Client *source_p, |
| 937 |
const char *pattern, ...) |
| 938 |
{ |
| 939 |
dlink_node *ptr = NULL; |
| 940 |
va_list args; |
| 941 |
char buffer[IRCD_BUFSIZE]; |
| 942 |
int len; |
| 943 |
|
| 944 |
if (IsClient(source_p)) |
| 945 |
len = sprintf(buffer, ":%s!%s@%s WALLOPS :", |
| 946 |
source_p->name, source_p->username, source_p->host); |
| 947 |
else |
| 948 |
len = sprintf(buffer, ":%s WALLOPS :", source_p->name); |
| 949 |
|
| 950 |
va_start(args, pattern); |
| 951 |
snprintf(&buffer[len], sizeof(buffer) - len, pattern, args); |
| 952 |
va_end(args); |
| 953 |
|
| 954 |
DLINK_FOREACH(ptr, oper_list.head) |
| 955 |
{ |
| 956 |
struct Client *client_p = ptr->data; |
| 957 |
assert(client_p->umodes & UMODE_OPER); |
| 958 |
|
| 959 |
if (HasUMode(client_p, flags)) |
| 960 |
sendto_one(client_p, "%s", buffer); |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
/* ts_warn() |
| 965 |
* |
| 966 |
* inputs - var args message |
| 967 |
* output - NONE |
| 968 |
* side effects - Call sendto_realops_flags, with some flood checking |
| 969 |
* (at most 5 warnings every 5 seconds) |
| 970 |
*/ |
| 971 |
void |
| 972 |
ts_warn(const char *pattern, ...) |
| 973 |
{ |
| 974 |
va_list args; |
| 975 |
char buffer[IRCD_BUFSIZE]; |
| 976 |
static time_t last = 0; |
| 977 |
static int warnings = 0; |
| 978 |
|
| 979 |
/* |
| 980 |
** if we're running with TS_WARNINGS enabled and someone does |
| 981 |
** something silly like (remotely) connecting a nonTS server, |
| 982 |
** we'll get a ton of warnings, so we make sure we don't send |
| 983 |
** more than 5 every 5 seconds. -orabidoo |
| 984 |
*/ |
| 985 |
|
| 986 |
if (CurrentTime - last < 5) |
| 987 |
{ |
| 988 |
if (++warnings > 5) |
| 989 |
return; |
| 990 |
} |
| 991 |
else |
| 992 |
{ |
| 993 |
last = CurrentTime; |
| 994 |
warnings = 0; |
| 995 |
} |
| 996 |
|
| 997 |
va_start(args, pattern); |
| 998 |
vsnprintf(buffer, sizeof(buffer), pattern, args); |
| 999 |
va_end(args); |
| 1000 |
|
| 1001 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "%s", buffer); |
| 1002 |
ilog(LOG_TYPE_IRCD, "%s", buffer); |
| 1003 |
} |
| 1004 |
|
| 1005 |
/* kill_client() |
| 1006 |
* |
| 1007 |
* inputs - client to send kill towards |
| 1008 |
* - pointer to client to kill |
| 1009 |
* - reason for kill |
| 1010 |
* output - NONE |
| 1011 |
* side effects - NONE |
| 1012 |
*/ |
| 1013 |
void |
| 1014 |
kill_client(struct Client *client_p, struct Client *diedie, |
| 1015 |
const char *pattern, ...) |
| 1016 |
{ |
| 1017 |
struct VarData vd; |
| 1018 |
struct MsgBuf *mb = NULL; |
| 1019 |
|
| 1020 |
if (client_p->from != NULL) |
| 1021 |
client_p = client_p->from; |
| 1022 |
if (IsDead(client_p)) |
| 1023 |
return; |
| 1024 |
|
| 1025 |
vd.vd_format = pattern; |
| 1026 |
va_start(vd.vd_args, pattern); |
| 1027 |
|
| 1028 |
mb = msgq_make(NULL, ":%s KILL %s :%v", |
| 1029 |
ID_or_name(&me, client_p->from), |
| 1030 |
ID_or_name(diedie, client_p), &vd); |
| 1031 |
va_end(vd.vd_args); |
| 1032 |
|
| 1033 |
send_buffer(client_p, mb, 0); |
| 1034 |
msgq_clean(mb); |
| 1035 |
} |
| 1036 |
|
| 1037 |
/* kill_client_ll_serv_butone() |
| 1038 |
* |
| 1039 |
* inputs - pointer to client to not send to |
| 1040 |
* - pointer to client to kill |
| 1041 |
* output - NONE |
| 1042 |
* side effects - Send a KILL for the given client |
| 1043 |
* message to all connected servers |
| 1044 |
* except the client 'one'. Also deal with |
| 1045 |
* client being unknown to leaf, as in lazylink... |
| 1046 |
*/ |
| 1047 |
void |
| 1048 |
kill_client_ll_serv_butone(struct Client *one, struct Client *source_p, |
| 1049 |
const char *pattern, ...) |
| 1050 |
{ |
| 1051 |
struct VarData vd; |
| 1052 |
struct MsgBuf *uid_mb = NULL; |
| 1053 |
struct MsgBuf *nick_mb = NULL; |
| 1054 |
dlink_node *ptr = NULL; |
| 1055 |
|
| 1056 |
vd.vd_format = pattern; |
| 1057 |
|
| 1058 |
if (HasID(source_p)) |
| 1059 |
{ |
| 1060 |
va_start(vd.vd_args, pattern); |
| 1061 |
uid_mb = msgq_make(NULL, ":%s KILL %s :%v", me.id, ID(source_p), &vd); |
| 1062 |
va_end(vd.vd_args); |
| 1063 |
} |
| 1064 |
|
| 1065 |
va_start(vd.vd_args, pattern); |
| 1066 |
nick_mb = msgq_make(NULL, ":%s KILL %s :%v", me.name, source_p->name, &vd); |
| 1067 |
va_end(vd.vd_args); |
| 1068 |
|
| 1069 |
DLINK_FOREACH(ptr, serv_list.head) |
| 1070 |
{ |
| 1071 |
struct Client *client_p = ptr->data; |
| 1072 |
|
| 1073 |
if (one != NULL && (client_p == one->from)) |
| 1074 |
continue; |
| 1075 |
if (IsDefunct(client_p)) |
| 1076 |
continue; |
| 1077 |
|
| 1078 |
if (uid_mb && IsCapable(client_p, CAP_TS6)) |
| 1079 |
send_buffer(client_p, uid_mb, 0); |
| 1080 |
else |
| 1081 |
send_buffer(client_p, nick_mb, 0); |
| 1082 |
} |
| 1083 |
|
| 1084 |
if (uid_mb) |
| 1085 |
msgq_clean(uid_mb); |
| 1086 |
msgq_clean(nick_mb); |
| 1087 |
} |