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 "common.h" |
31 |
#include "dbuf.h" |
32 |
#include "irc_string.h" |
33 |
#include "ircd.h" |
34 |
#include "handlers.h" |
35 |
#include "numeric.h" |
36 |
#include "fdlist.h" |
37 |
#include "s_bsd.h" |
38 |
#include "s_serv.h" |
39 |
#include "sprintf_irc.h" |
40 |
#include "s_conf.h" |
41 |
#include "s_log.h" |
42 |
#include "memory.h" |
43 |
#include "hook.h" |
44 |
#include "irc_getnameinfo.h" |
45 |
#include "packet.h" |
46 |
|
47 |
#define LOG_BUFSIZE 2048 |
48 |
|
49 |
struct Callback *iosend_cb = NULL; |
50 |
struct Callback *iosendctrl_cb = NULL; |
51 |
|
52 |
static void send_message(struct Client *, char *, int); |
53 |
static void send_message_remote(struct Client *, struct Client *, char *, int); |
54 |
|
55 |
static unsigned int current_serial = 0; |
56 |
|
57 |
/* send_format() |
58 |
* |
59 |
* inputs - buffer to format into |
60 |
* - size of the buffer |
61 |
* - format pattern to use |
62 |
* - var args |
63 |
* output - number of bytes formatted output |
64 |
* side effects - modifies sendbuf |
65 |
*/ |
66 |
static inline int |
67 |
send_format(char *lsendbuf, int bufsize, const char *pattern, va_list args) |
68 |
{ |
69 |
int len; |
70 |
|
71 |
/* |
72 |
* from rfc1459 |
73 |
* |
74 |
* IRC messages are always lines of characters terminated with a CR-LF |
75 |
* (Carriage Return - Line Feed) pair, and these messages shall not |
76 |
* exceed 512 characters in length, counting all characters |
77 |
* including the trailing CR-LF. |
78 |
* Thus, there are 510 characters maximum allowed |
79 |
* for the command and its parameters. There is no provision for |
80 |
* continuation message lines. See section 7 for more details about |
81 |
* current implementations. |
82 |
*/ |
83 |
len = vsnprintf(lsendbuf, bufsize - 1, pattern, args); |
84 |
if (len > bufsize - 2) |
85 |
len = bufsize - 2; /* required by some versions of vsnprintf */ |
86 |
|
87 |
lsendbuf[len++] = '\r'; |
88 |
lsendbuf[len++] = '\n'; |
89 |
return (len); |
90 |
} |
91 |
|
92 |
/* |
93 |
* iosend_default - append a packet to the client's sendq. |
94 |
*/ |
95 |
void * |
96 |
iosend_default(va_list args) |
97 |
{ |
98 |
struct Client *to = va_arg(args, struct Client *); |
99 |
int length = va_arg(args, int); |
100 |
char *buf = va_arg(args, char *); |
101 |
|
102 |
dbuf_put(&to->localClient->buf_sendq, buf, length); |
103 |
return NULL; |
104 |
} |
105 |
|
106 |
/* |
107 |
** send_message |
108 |
** Internal utility which appends given buffer to the sockets |
109 |
** sendq. |
110 |
*/ |
111 |
static void |
112 |
send_message(struct Client *to, char *buf, int len) |
113 |
{ |
114 |
assert(!IsMe(to)); |
115 |
assert(to != &me); |
116 |
|
117 |
if (dbuf_length(&to->localClient->buf_sendq) + len > get_sendq(to)) |
118 |
{ |
119 |
if (IsServer(to)) |
120 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
121 |
"Max SendQ limit exceeded for %s: %lu > %lu", |
122 |
get_client_name(to, HIDE_IP), |
123 |
(unsigned long)(dbuf_length(&to->localClient->buf_sendq) + len), |
124 |
get_sendq(to)); |
125 |
if (IsClient(to)) |
126 |
SetSendQExceeded(to); |
127 |
dead_link_on_write(to, 0); |
128 |
return; |
129 |
} |
130 |
|
131 |
execute_callback(iosend_cb, to, len, buf); |
132 |
|
133 |
/* |
134 |
** Update statistics. The following is slightly incorrect |
135 |
** because it counts messages even if queued, but bytes |
136 |
** only really sent. Queued bytes get updated in SendQueued. |
137 |
*/ |
138 |
++to->localClient->send.messages; |
139 |
++me.localClient->send.messages; |
140 |
|
141 |
if (dbuf_length(&to->localClient->buf_sendq) > |
142 |
(IsServer(to) ? (unsigned int) 1024 : (unsigned int) 4096)) |
143 |
send_queued_write(to); |
144 |
} |
145 |
|
146 |
/* send_message_remote() |
147 |
* |
148 |
* inputs - pointer to client from message is being sent |
149 |
* - pointer to client to send to |
150 |
* - pointer to preformatted buffer |
151 |
* - length of input buffer |
152 |
* output - none |
153 |
* side effects - Despite the function name, this only sends to directly |
154 |
* connected clients. |
155 |
* |
156 |
*/ |
157 |
static void |
158 |
send_message_remote(struct Client *to, struct Client *from, |
159 |
char *buf, int len) |
160 |
{ |
161 |
if (!MyConnect(to)) |
162 |
{ |
163 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
164 |
"server send message to %s [%s] dropped from %s(Not local server)", |
165 |
to->name, to->from->name, from->name); |
166 |
return; |
167 |
} |
168 |
|
169 |
/* Optimize by checking if (from && to) before everything */ |
170 |
/* we set to->from up there.. */ |
171 |
|
172 |
if (!MyClient(from) && IsClient(to) && (to == from->from)) |
173 |
{ |
174 |
if (IsServer(from)) |
175 |
{ |
176 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
177 |
"Send message to %s [%s] dropped from %s(Fake Dir)", |
178 |
to->name, to->from->name, from->name); |
179 |
return; |
180 |
} |
181 |
|
182 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
183 |
"Ghosted: %s[%s@%s] from %s[%s@%s] (%s)", |
184 |
to->name, to->username, to->host, |
185 |
from->name, from->username, from->host, |
186 |
to->from->name); |
187 |
|
188 |
sendto_server(NULL, NULL, CAP_TS6, NOCAPS, |
189 |
":%s KILL %s :%s (%s[%s@%s] Ghosted %s)", |
190 |
me.id, to->name, me.name, to->name, |
191 |
to->username, to->host, to->from->name); |
192 |
sendto_server(NULL, NULL, NOCAPS, CAP_TS6, |
193 |
":%s KILL %s :%s (%s[%s@%s] Ghosted %s)", |
194 |
me.name, to->name, me.name, to->name, |
195 |
to->username, to->host, to->from->name); |
196 |
|
197 |
SetKilled(to); |
198 |
|
199 |
if (IsClient(from)) |
200 |
sendto_one(from, form_str(ERR_GHOSTEDCLIENT), |
201 |
me.name, from->name, to->name, to->username, |
202 |
to->host, to->from); |
203 |
|
204 |
exit_client(to, &me, "Ghosted client"); |
205 |
|
206 |
return; |
207 |
} |
208 |
|
209 |
send_message(to, buf, len); |
210 |
} |
211 |
|
212 |
/* |
213 |
** sendq_unblocked |
214 |
** Called when a socket is ready for writing. |
215 |
*/ |
216 |
void |
217 |
sendq_unblocked(fde_t *fd, struct Client *client_p) |
218 |
{ |
219 |
ClearSendqBlocked(client_p); |
220 |
/* let send_queued_write be executed by send_queued_all */ |
221 |
|
222 |
#ifdef HAVE_LIBCRYPTO |
223 |
if (fd->flags.pending_read) |
224 |
{ |
225 |
fd->flags.pending_read = 0; |
226 |
read_packet(fd, client_p); |
227 |
} |
228 |
#endif |
229 |
} |
230 |
|
231 |
/* |
232 |
** slinkq_unblocked |
233 |
** Called when a server control socket is ready for writing. |
234 |
*/ |
235 |
static void |
236 |
slinkq_unblocked(fde_t *fd, struct Client *client_p) |
237 |
{ |
238 |
ClearSlinkqBlocked(client_p); |
239 |
send_queued_slink_write(client_p); |
240 |
} |
241 |
|
242 |
/* |
243 |
** send_queued_write |
244 |
** This is called when there is a chance that some output would |
245 |
** be possible. This attempts to empty the send queue as far as |
246 |
** possible, and then if any data is left, a write is rescheduled. |
247 |
*/ |
248 |
void |
249 |
send_queued_write(struct Client *to) |
250 |
{ |
251 |
int retlen; |
252 |
struct dbuf_block *first; |
253 |
|
254 |
/* |
255 |
** Once socket is marked dead, we cannot start writing to it, |
256 |
** even if the error is removed... |
257 |
*/ |
258 |
if (IsDead(to) || IsSendqBlocked(to)) |
259 |
return; /* no use calling send() now */ |
260 |
|
261 |
/* Next, lets try to write some data */ |
262 |
|
263 |
if (dbuf_length(&to->localClient->buf_sendq)) |
264 |
{ |
265 |
do { |
266 |
first = to->localClient->buf_sendq.blocks.head->data; |
267 |
|
268 |
#ifdef HAVE_LIBCRYPTO |
269 |
if (to->localClient->fd.ssl) |
270 |
{ |
271 |
retlen = SSL_write(to->localClient->fd.ssl, first->data, first->size); |
272 |
|
273 |
/* translate openssl error codes, sigh */ |
274 |
if (retlen < 0) |
275 |
switch (SSL_get_error(to->localClient->fd.ssl, retlen)) |
276 |
{ |
277 |
case SSL_ERROR_WANT_READ: |
278 |
return; /* retry later, don't register for write events */ |
279 |
|
280 |
case SSL_ERROR_WANT_WRITE: |
281 |
errno = EWOULDBLOCK; |
282 |
case SSL_ERROR_SYSCALL: |
283 |
break; |
284 |
case SSL_ERROR_SSL: |
285 |
if (errno == EAGAIN) |
286 |
break; |
287 |
default: |
288 |
retlen = errno = 0; /* either an SSL-specific error or EOF */ |
289 |
} |
290 |
} |
291 |
else |
292 |
#endif |
293 |
retlen = send(to->localClient->fd.fd, first->data, first->size, 0); |
294 |
|
295 |
if (retlen <= 0) |
296 |
break; |
297 |
|
298 |
dbuf_delete(&to->localClient->buf_sendq, retlen); |
299 |
|
300 |
/* We have some data written .. update counters */ |
301 |
to->localClient->send.bytes += retlen; |
302 |
me.localClient->send.bytes += retlen; |
303 |
} while (dbuf_length(&to->localClient->buf_sendq)); |
304 |
|
305 |
if ((retlen < 0) && (ignoreErrno(errno))) |
306 |
{ |
307 |
/* we have a non-fatal error, reschedule a write */ |
308 |
SetSendqBlocked(to); |
309 |
comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE, |
310 |
(PF *)sendq_unblocked, (void *)to, 0); |
311 |
} |
312 |
else if (retlen <= 0) |
313 |
{ |
314 |
dead_link_on_write(to, errno); |
315 |
return; |
316 |
} |
317 |
} |
318 |
} |
319 |
|
320 |
/* |
321 |
** send_queued_slink_write |
322 |
** This is called when there is a chance the some output would |
323 |
** be possible. This attempts to empty the send queue as far as |
324 |
** possible, and then if any data is left, a write is rescheduled. |
325 |
*/ |
326 |
void |
327 |
send_queued_slink_write(struct Client *to) |
328 |
{ |
329 |
int retlen; |
330 |
|
331 |
/* |
332 |
** Once socket is marked dead, we cannot start writing to it, |
333 |
** even if the error is removed... |
334 |
*/ |
335 |
if (IsDead(to) || IsSlinkqBlocked(to)) |
336 |
return; /* no use calling send() now */ |
337 |
|
338 |
/* Next, lets try to write some data */ |
339 |
if (to->localClient->slinkq != NULL) |
340 |
{ |
341 |
retlen = send(to->localClient->ctrlfd.fd, |
342 |
to->localClient->slinkq + to->localClient->slinkq_ofs, |
343 |
to->localClient->slinkq_len, 0); |
344 |
if (retlen < 0) |
345 |
{ |
346 |
/* If we have a fatal error */ |
347 |
if (!ignoreErrno(errno)) |
348 |
{ |
349 |
dead_link_on_write(to, errno); |
350 |
return; |
351 |
} |
352 |
} |
353 |
else if (retlen == 0) |
354 |
{ |
355 |
/* 0 bytes is an EOF .. */ |
356 |
dead_link_on_write(to, 0); |
357 |
return; |
358 |
} |
359 |
else |
360 |
{ |
361 |
execute_callback(iosendctrl_cb, to, retlen, |
362 |
to->localClient->slinkq + to->localClient->slinkq_ofs); |
363 |
to->localClient->slinkq_len -= retlen; |
364 |
|
365 |
assert(to->localClient->slinkq_len >= 0); |
366 |
if (to->localClient->slinkq_len) |
367 |
to->localClient->slinkq_ofs += retlen; |
368 |
else |
369 |
{ |
370 |
to->localClient->slinkq_ofs = 0; |
371 |
MyFree(to->localClient->slinkq); |
372 |
to->localClient->slinkq = NULL; |
373 |
} |
374 |
} |
375 |
|
376 |
/* Finally, if we have any more data, reschedule a write */ |
377 |
if (to->localClient->slinkq_len) |
378 |
{ |
379 |
SetSlinkqBlocked(to); |
380 |
comm_setselect(&to->localClient->ctrlfd, COMM_SELECT_WRITE, |
381 |
(PF *)slinkq_unblocked, (void *)to, 0); |
382 |
} |
383 |
} |
384 |
} |
385 |
|
386 |
/* send_queued_all() |
387 |
* |
388 |
* input - NONE |
389 |
* output - NONE |
390 |
* side effects - try to flush sendq of each client |
391 |
*/ |
392 |
void |
393 |
send_queued_all(void) |
394 |
{ |
395 |
dlink_node *ptr; |
396 |
|
397 |
/* Servers are processed first, mainly because this can generate |
398 |
* a notice to opers, which is to be delivered by this function. |
399 |
*/ |
400 |
DLINK_FOREACH(ptr, serv_list.head) |
401 |
send_queued_write((struct Client *) ptr->data); |
402 |
|
403 |
DLINK_FOREACH(ptr, unknown_list.head) |
404 |
send_queued_write((struct Client *) ptr->data); |
405 |
|
406 |
DLINK_FOREACH(ptr, local_client_list.head) |
407 |
send_queued_write((struct Client *) ptr->data); |
408 |
|
409 |
/* NOTE: This can still put clients on aborted_list; unfortunately, |
410 |
* exit_aborted_clients takes precedence over send_queued_all, |
411 |
* because exiting clients can generate server/oper traffic. |
412 |
* The easiest approach is dealing with aborted clients in the next I/O lap. |
413 |
* -adx |
414 |
*/ |
415 |
} |
416 |
|
417 |
/* sendto_one() |
418 |
* |
419 |
* inputs - pointer to destination client |
420 |
* - var args message |
421 |
* output - NONE |
422 |
* side effects - send message to single client |
423 |
*/ |
424 |
void |
425 |
sendto_one(struct Client *to, const char *pattern, ...) |
426 |
{ |
427 |
va_list args; |
428 |
char buffer[IRCD_BUFSIZE]; |
429 |
int len; |
430 |
|
431 |
if (to->from != NULL) |
432 |
to = to->from; |
433 |
if (IsDead(to)) |
434 |
return; /* This socket has already been marked as dead */ |
435 |
|
436 |
va_start(args, pattern); |
437 |
len = send_format(buffer, IRCD_BUFSIZE, pattern, args); |
438 |
va_end(args); |
439 |
|
440 |
send_message(to, buffer, len); |
441 |
} |
442 |
|
443 |
/* sendto_channel_butone() |
444 |
* |
445 |
* inputs - pointer to client(server) to NOT send message to |
446 |
* - pointer to client that is sending this message |
447 |
* - pointer to channel being sent to |
448 |
* - vargs message |
449 |
* output - NONE |
450 |
* side effects - message as given is sent to given channel members. |
451 |
* |
452 |
* WARNING - +D clients are ignored |
453 |
*/ |
454 |
void |
455 |
sendto_channel_butone(struct Client *one, struct Client *from, |
456 |
struct Channel *chptr, const char *command, |
457 |
const char *pattern, ...) |
458 |
{ |
459 |
va_list alocal, aremote, auid; |
460 |
char local_buf[IRCD_BUFSIZE]; |
461 |
char remote_buf[IRCD_BUFSIZE]; |
462 |
char uid_buf[IRCD_BUFSIZE]; |
463 |
int local_len, remote_len, uid_len; |
464 |
dlink_node *ptr; |
465 |
dlink_node *ptr_next; |
466 |
struct Client *target_p; |
467 |
|
468 |
if (IsServer(from)) |
469 |
local_len = ircsprintf(local_buf, ":%s %s %s ", |
470 |
from->name, command, chptr->chname); |
471 |
else |
472 |
local_len = ircsprintf(local_buf, ":%s!%s@%s %s %s ", |
473 |
from->name, from->username, from->host, |
474 |
command, chptr->chname); |
475 |
remote_len = ircsprintf(remote_buf, ":%s %s %s ", |
476 |
from->name, command, chptr->chname); |
477 |
uid_len = ircsprintf(uid_buf, ":%s %s %s ", |
478 |
ID(from), command, chptr->chname); |
479 |
|
480 |
va_start(alocal, pattern); |
481 |
va_start(aremote, pattern); |
482 |
va_start(auid, pattern); |
483 |
local_len += send_format(&local_buf[local_len], IRCD_BUFSIZE - local_len, |
484 |
pattern, alocal); |
485 |
remote_len += send_format(&remote_buf[remote_len], IRCD_BUFSIZE - remote_len, |
486 |
pattern, aremote); |
487 |
uid_len += send_format(&uid_buf[uid_len], IRCD_BUFSIZE - uid_len, pattern, |
488 |
auid); |
489 |
va_end(auid); |
490 |
va_end(aremote); |
491 |
va_end(alocal); |
492 |
|
493 |
++current_serial; |
494 |
|
495 |
DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->members.head) |
496 |
{ |
497 |
target_p = ((struct Membership *)ptr->data)->client_p; |
498 |
assert(target_p != NULL); |
499 |
|
500 |
if (IsDefunct(target_p) || IsDeaf(target_p) || target_p->from == one) |
501 |
continue; |
502 |
|
503 |
if (MyClient(target_p)) |
504 |
{ |
505 |
if (target_p->serial != current_serial) |
506 |
{ |
507 |
send_message(target_p, local_buf, local_len); |
508 |
target_p->serial = current_serial; |
509 |
} |
510 |
} |
511 |
else |
512 |
{ |
513 |
/* Now check whether a message has been sent to this |
514 |
* remote link already |
515 |
*/ |
516 |
if (target_p->from->serial != current_serial) |
517 |
{ |
518 |
if (IsCapable(target_p->from, CAP_TS6)) |
519 |
send_message_remote(target_p->from, from, uid_buf, uid_len); |
520 |
else |
521 |
send_message_remote(target_p->from, from, remote_buf, remote_len); |
522 |
target_p->from->serial = current_serial; |
523 |
} |
524 |
} |
525 |
} |
526 |
} |
527 |
|
528 |
/* sendto_server() |
529 |
* |
530 |
* inputs - pointer to client to NOT send to |
531 |
* - pointer to channel |
532 |
* - caps or'd together which must ALL be present |
533 |
* - caps or'd together which must ALL NOT be present |
534 |
* - printf style format string |
535 |
* - args to format string |
536 |
* output - NONE |
537 |
* side effects - Send a message to all connected servers, except the |
538 |
* client 'one' (if non-NULL), as long as the servers |
539 |
* support ALL capabs in 'caps', and NO capabs in 'nocaps'. |
540 |
* |
541 |
* This function was written in an attempt to merge together the other |
542 |
* billion sendto_*serv*() functions, which sprung up with capabs, |
543 |
* lazylinks, uids, etc. |
544 |
* -davidt |
545 |
*/ |
546 |
void |
547 |
sendto_server(struct Client *one, const struct Channel *chptr, |
548 |
const unsigned int caps, |
549 |
const unsigned int nocaps, |
550 |
const char *format, ...) |
551 |
{ |
552 |
va_list args; |
553 |
dlink_node *ptr = NULL; |
554 |
char buffer[IRCD_BUFSIZE]; |
555 |
int len = 0; |
556 |
|
557 |
if (chptr && chptr->chname[0] != '#') |
558 |
return; |
559 |
|
560 |
va_start(args, format); |
561 |
len = send_format(buffer, IRCD_BUFSIZE, format, args); |
562 |
va_end(args); |
563 |
|
564 |
DLINK_FOREACH(ptr, serv_list.head) |
565 |
{ |
566 |
struct Client *client_p = ptr->data; |
567 |
|
568 |
/* If dead already skip */ |
569 |
if (IsDead(client_p)) |
570 |
continue; |
571 |
/* check against 'one' */ |
572 |
if (one != NULL && (client_p == one->from)) |
573 |
continue; |
574 |
/* check we have required capabs */ |
575 |
if ((client_p->localClient->caps & caps) != caps) |
576 |
continue; |
577 |
/* check we don't have any forbidden capabs */ |
578 |
if ((client_p->localClient->caps & nocaps) != 0) |
579 |
continue; |
580 |
|
581 |
send_message(client_p, buffer, len); |
582 |
} |
583 |
} |
584 |
|
585 |
/* sendto_common_channels_local() |
586 |
* |
587 |
* inputs - pointer to client |
588 |
* - pattern to send |
589 |
* output - NONE |
590 |
* side effects - Sends a message to all people on local server who are |
591 |
* in same channel with user. |
592 |
* used by m_nick.c and exit_one_client. |
593 |
*/ |
594 |
void |
595 |
sendto_common_channels_local(struct Client *user, int touser, |
596 |
const char *pattern, ...) |
597 |
{ |
598 |
va_list args; |
599 |
dlink_node *uptr; |
600 |
dlink_node *cptr; |
601 |
struct Channel *chptr; |
602 |
struct Membership *ms; |
603 |
struct Client *target_p; |
604 |
char buffer[IRCD_BUFSIZE]; |
605 |
int len; |
606 |
|
607 |
va_start(args, pattern); |
608 |
len = send_format(buffer, IRCD_BUFSIZE, pattern, args); |
609 |
va_end(args); |
610 |
|
611 |
++current_serial; |
612 |
|
613 |
DLINK_FOREACH(cptr, user->channel.head) |
614 |
{ |
615 |
chptr = ((struct Membership *) cptr->data)->chptr; |
616 |
assert(chptr != NULL); |
617 |
|
618 |
DLINK_FOREACH(uptr, chptr->members.head) |
619 |
{ |
620 |
ms = uptr->data; |
621 |
target_p = ms->client_p; |
622 |
assert(target_p != NULL); |
623 |
|
624 |
if (!MyConnect(target_p) || target_p == user || IsDefunct(target_p) || |
625 |
target_p->serial == current_serial) |
626 |
continue; |
627 |
|
628 |
target_p->serial = current_serial; |
629 |
send_message(target_p, buffer, len); |
630 |
} |
631 |
} |
632 |
|
633 |
if (touser && MyConnect(user) && !IsDead(user) && |
634 |
user->serial != current_serial) |
635 |
send_message(user, buffer, len); |
636 |
} |
637 |
|
638 |
/* sendto_channel_local() |
639 |
* |
640 |
* inputs - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE |
641 |
* - whether to ignore +D clients (YES/NO) |
642 |
* - pointer to channel to send to |
643 |
* - var args pattern |
644 |
* output - NONE |
645 |
* side effects - Send a message to all members of a channel that are |
646 |
* locally connected to this server. |
647 |
*/ |
648 |
void |
649 |
sendto_channel_local(int type, int nodeaf, struct Channel *chptr, |
650 |
const char *pattern, ...) |
651 |
{ |
652 |
va_list args; |
653 |
char buffer[IRCD_BUFSIZE]; |
654 |
int len; |
655 |
dlink_node *ptr; |
656 |
struct Membership *ms; |
657 |
struct Client *target_p; |
658 |
|
659 |
va_start(args, pattern); |
660 |
len = send_format(buffer, IRCD_BUFSIZE, pattern, args); |
661 |
va_end(args); |
662 |
|
663 |
DLINK_FOREACH(ptr, chptr->members.head) |
664 |
{ |
665 |
ms = ptr->data; |
666 |
target_p = ms->client_p; |
667 |
|
668 |
if (type != 0 && (ms->flags & type) == 0) |
669 |
continue; |
670 |
|
671 |
if (!MyConnect(target_p) || IsDefunct(target_p) || |
672 |
(nodeaf && IsDeaf(target_p))) |
673 |
continue; |
674 |
|
675 |
send_message(target_p, buffer, len); |
676 |
} |
677 |
} |
678 |
|
679 |
/* sendto_channel_local_butone() |
680 |
* |
681 |
* inputs - pointer to client to NOT send message to |
682 |
* - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE |
683 |
* - pointer to channel to send to |
684 |
* - var args pattern |
685 |
* output - NONE |
686 |
* side effects - Send a message to all members of a channel that are |
687 |
* locally connected to this server except one. |
688 |
* |
689 |
* WARNING - +D clients are omitted |
690 |
*/ |
691 |
void |
692 |
sendto_channel_local_butone(struct Client *one, int type, |
693 |
struct Channel *chptr, const char *pattern, ...) |
694 |
{ |
695 |
va_list args; |
696 |
char buffer[IRCD_BUFSIZE]; |
697 |
int len; |
698 |
struct Client *target_p; |
699 |
struct Membership *ms; |
700 |
dlink_node *ptr; |
701 |
|
702 |
va_start(args, pattern); |
703 |
len = send_format(buffer, IRCD_BUFSIZE, pattern, args); |
704 |
va_end(args); |
705 |
|
706 |
DLINK_FOREACH(ptr, chptr->members.head) |
707 |
{ |
708 |
ms = ptr->data; |
709 |
target_p = ms->client_p; |
710 |
|
711 |
if (type != 0 && (ms->flags & type) == 0) |
712 |
continue; |
713 |
|
714 |
if (!MyConnect(target_p) || target_p == one || |
715 |
IsDefunct(target_p) || IsDeaf(target_p)) |
716 |
continue; |
717 |
send_message(target_p, buffer, len); |
718 |
} |
719 |
} |
720 |
|
721 |
|
722 |
/* sendto_channel_remote() |
723 |
* |
724 |
* inputs - Client not to send towards |
725 |
* - Client from whom message is from |
726 |
* - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE |
727 |
* - pointer to channel to send to |
728 |
* - var args pattern |
729 |
* output - NONE |
730 |
* side effects - Send a message to all members of a channel that are |
731 |
* remote to this server. |
732 |
*/ |
733 |
void |
734 |
sendto_channel_remote(struct Client *one, struct Client *from, int type, |
735 |
const unsigned int caps, const unsigned int nocaps, |
736 |
struct Channel *chptr, const char *pattern, ...) |
737 |
{ |
738 |
va_list args; |
739 |
char buffer[IRCD_BUFSIZE]; |
740 |
int len; |
741 |
dlink_node *ptr; |
742 |
struct Client *target_p; |
743 |
struct Membership *ms; |
744 |
|
745 |
va_start(args, pattern); |
746 |
len = send_format(buffer, IRCD_BUFSIZE, pattern, args); |
747 |
va_end(args); |
748 |
|
749 |
++current_serial; |
750 |
|
751 |
DLINK_FOREACH(ptr, chptr->members.head) |
752 |
{ |
753 |
ms = ptr->data; |
754 |
target_p = ms->client_p; |
755 |
|
756 |
if (type != 0 && (ms->flags & type) == 0) |
757 |
continue; |
758 |
|
759 |
if (MyConnect(target_p)) |
760 |
continue; |
761 |
target_p = target_p->from; |
762 |
|
763 |
if (target_p == one->from || |
764 |
((target_p->from->localClient->caps & caps) != caps) || |
765 |
((target_p->from->localClient->caps & nocaps) != 0)) |
766 |
continue; |
767 |
if (target_p->from->serial != current_serial) |
768 |
{ |
769 |
send_message(target_p, buffer, len); |
770 |
target_p->from->serial = current_serial; |
771 |
} |
772 |
} |
773 |
} |
774 |
|
775 |
/* |
776 |
** match_it() and sendto_match_butone() ARE only used |
777 |
** to send a msg to all ppl on servers/hosts that match a specified mask |
778 |
** (used for enhanced PRIVMSGs) for opers |
779 |
** |
780 |
** addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de) |
781 |
** |
782 |
*/ |
783 |
|
784 |
/* match_it() |
785 |
* |
786 |
* inputs - client pointer to match on |
787 |
* - actual mask to match |
788 |
* - what to match on, HOST or SERVER |
789 |
* output - 1 or 0 if match or not |
790 |
* side effects - NONE |
791 |
*/ |
792 |
static int |
793 |
match_it(const struct Client *one, const char *mask, int what) |
794 |
{ |
795 |
if (what == MATCH_HOST) |
796 |
return(match(mask, one->host)); |
797 |
|
798 |
return(match(mask, one->servptr->name)); |
799 |
} |
800 |
|
801 |
/* sendto_match_butone() |
802 |
* |
803 |
* Send to all clients which match the mask in a way defined on 'what'; |
804 |
* either by user hostname or user servername. |
805 |
* |
806 |
* ugh. ONLY used by m_message.c to send an "oper magic" message. ugh. |
807 |
*/ |
808 |
void |
809 |
sendto_match_butone(struct Client *one, struct Client *from, char *mask, |
810 |
int what, const char *pattern, ...) |
811 |
{ |
812 |
va_list alocal, aremote; |
813 |
struct Client *client_p; |
814 |
dlink_node *ptr, *ptr_next; |
815 |
char local_buf[IRCD_BUFSIZE], remote_buf[IRCD_BUFSIZE]; |
816 |
int local_len = ircsprintf(local_buf, ":%s!%s@%s ", from->name, |
817 |
from->username, from->host); |
818 |
int remote_len = ircsprintf(remote_buf, ":%s ", from->name); |
819 |
|
820 |
va_start(alocal, pattern); |
821 |
va_start(aremote, pattern); |
822 |
local_len += send_format(&local_buf[local_len], IRCD_BUFSIZE - local_len, |
823 |
pattern, alocal); |
824 |
remote_len += send_format(&remote_buf[remote_len], IRCD_BUFSIZE - remote_len, |
825 |
pattern, aremote); |
826 |
va_end(aremote); |
827 |
va_end(alocal); |
828 |
|
829 |
/* scan the local clients */ |
830 |
DLINK_FOREACH(ptr, local_client_list.head) |
831 |
{ |
832 |
client_p = ptr->data; |
833 |
|
834 |
if (client_p != one && !IsDefunct(client_p) && |
835 |
match_it(client_p, mask, what)) |
836 |
send_message(client_p, local_buf, local_len); |
837 |
} |
838 |
|
839 |
/* Now scan servers */ |
840 |
DLINK_FOREACH_SAFE(ptr, ptr_next, serv_list.head) |
841 |
{ |
842 |
client_p = ptr->data; |
843 |
|
844 |
/* |
845 |
* The old code looped through every client on the |
846 |
* network for each server to check if the |
847 |
* server (client_p) has at least 1 client matching |
848 |
* the mask, using something like: |
849 |
* |
850 |
* for (target_p = GlobalClientList; target_p; target_p = target_p->next) |
851 |
* if (IsRegisteredUser(target_p) && |
852 |
* match_it(target_p, mask, what) && |
853 |
* (target_p->from == client_p)) |
854 |
* vsendto_prefix_one(client_p, from, pattern, args); |
855 |
* |
856 |
* That way, we wouldn't send the message to |
857 |
* a server who didn't have a matching client. |
858 |
* However, on a network such as EFNet, that |
859 |
* code would have looped through about 50 |
860 |
* servers, and in each loop, loop through |
861 |
* about 50k clients as well, calling match() |
862 |
* in each nested loop. That is a very bad |
863 |
* thing cpu wise - just send the message |
864 |
* to every connected server and let that |
865 |
* server deal with it. |
866 |
* -wnder |
867 |
*/ |
868 |
if (client_p != one && !IsDefunct(client_p)) |
869 |
send_message_remote(client_p, from, remote_buf, remote_len); |
870 |
} |
871 |
} |
872 |
|
873 |
/* sendto_match_servs() |
874 |
* |
875 |
* inputs - source client |
876 |
* - mask to send to |
877 |
* - capab needed |
878 |
* - data |
879 |
* outputs - none |
880 |
* side effects - data sent to servers matching with capab |
881 |
*/ |
882 |
void |
883 |
sendto_match_servs(struct Client *source_p, const char *mask, int cap, |
884 |
const char *pattern, ...) |
885 |
{ |
886 |
va_list args; |
887 |
struct Client *target_p; |
888 |
dlink_node *ptr; |
889 |
char buffer[IRCD_BUFSIZE]; |
890 |
int found = 0; |
891 |
|
892 |
va_start(args, pattern); |
893 |
vsnprintf(buffer, sizeof(buffer), pattern, args); |
894 |
va_end(args); |
895 |
|
896 |
++current_serial; |
897 |
|
898 |
DLINK_FOREACH(ptr, global_serv_list.head) |
899 |
{ |
900 |
target_p = ptr->data; |
901 |
|
902 |
/* Do not attempt to send to ourselves, or the source */ |
903 |
if (IsMe(target_p) || target_p->from == source_p->from) |
904 |
continue; |
905 |
|
906 |
if (target_p->from->serial == current_serial) |
907 |
continue; |
908 |
|
909 |
if (match(mask, target_p->name)) |
910 |
{ |
911 |
/* |
912 |
* if we set the serial here, then we'll never do a |
913 |
* match() again, if !IsCapable() |
914 |
*/ |
915 |
target_p->from->serial = current_serial; |
916 |
found++; |
917 |
|
918 |
if (!IsCapable(target_p->from, cap)) |
919 |
continue; |
920 |
|
921 |
sendto_anywhere(target_p, source_p, "%s", buffer); |
922 |
} |
923 |
} |
924 |
} |
925 |
|
926 |
/* sendto_anywhere() |
927 |
* |
928 |
* inputs - pointer to dest client |
929 |
* - pointer to from client |
930 |
* - varags |
931 |
* output - NONE |
932 |
* side effects - less efficient than sendto_remote and sendto_one |
933 |
* but useful when one does not know where target "lives" |
934 |
*/ |
935 |
void |
936 |
sendto_anywhere(struct Client *to, struct Client *from, |
937 |
const char *pattern, ...) |
938 |
{ |
939 |
va_list args; |
940 |
char buffer[IRCD_BUFSIZE]; |
941 |
int len; |
942 |
struct Client *send_to = (to->from != NULL ? to->from : to); |
943 |
|
944 |
if (IsDead(send_to)) |
945 |
return; |
946 |
|
947 |
if (MyClient(to)) |
948 |
{ |
949 |
if (IsServer(from)) |
950 |
{ |
951 |
if (IsCapable(to, CAP_TS6) && HasID(from)) |
952 |
len = ircsprintf(buffer, ":%s ", from->id); |
953 |
else |
954 |
len = ircsprintf(buffer, ":%s ", from->name); |
955 |
} |
956 |
else |
957 |
len = ircsprintf(buffer, ":%s!%s@%s ", |
958 |
from->name, from->username, from->host); |
959 |
} |
960 |
else len = ircsprintf(buffer, ":%s ", ID_or_name(from, send_to)); |
961 |
|
962 |
va_start(args, pattern); |
963 |
len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args); |
964 |
va_end(args); |
965 |
|
966 |
if(MyClient(to)) |
967 |
send_message(send_to, buffer, len); |
968 |
else |
969 |
send_message_remote(send_to, from, buffer, len); |
970 |
} |
971 |
|
972 |
/* sendto_realops_flags() |
973 |
* |
974 |
* inputs - flag types of messages to show to real opers |
975 |
* - flag indicating opers/admins |
976 |
* - var args input message |
977 |
* output - NONE |
978 |
* side effects - Send to *local* ops only but NOT +s nonopers. |
979 |
*/ |
980 |
void |
981 |
sendto_realops_flags(unsigned int flags, int level, const char *pattern, ...) |
982 |
{ |
983 |
struct Client *client_p; |
984 |
char nbuf[IRCD_BUFSIZE]; |
985 |
dlink_node *ptr; |
986 |
va_list args; |
987 |
|
988 |
va_start(args, pattern); |
989 |
vsnprintf(nbuf, IRCD_BUFSIZE, pattern, args); |
990 |
va_end(args); |
991 |
|
992 |
DLINK_FOREACH(ptr, oper_list.head) |
993 |
{ |
994 |
client_p = ptr->data; |
995 |
assert(client_p->umodes & UMODE_OPER); |
996 |
|
997 |
/* If we're sending it to opers and theyre an admin, skip. |
998 |
* If we're sending it to admins, and theyre not, skip. |
999 |
*/ |
1000 |
if (((level == L_ADMIN) && !IsAdmin(client_p)) || |
1001 |
((level == L_OPER) && IsAdmin(client_p))) |
1002 |
continue; |
1003 |
|
1004 |
if (client_p->umodes & flags) |
1005 |
sendto_one(client_p, ":%s NOTICE %s :*** Notice -- %s", |
1006 |
me.name, client_p->name, nbuf); |
1007 |
} |
1008 |
} |
1009 |
|
1010 |
/* sendto_wallops_flags() |
1011 |
* |
1012 |
* inputs - flag types of messages to show to real opers |
1013 |
* - client sending request |
1014 |
* - var args input message |
1015 |
* output - NONE |
1016 |
* side effects - Send a wallops to local opers |
1017 |
*/ |
1018 |
void |
1019 |
sendto_wallops_flags(unsigned int flags, struct Client *source_p, |
1020 |
const char *pattern, ...) |
1021 |
{ |
1022 |
struct Client *client_p; |
1023 |
dlink_node *ptr; |
1024 |
va_list args; |
1025 |
char buffer[IRCD_BUFSIZE]; |
1026 |
int len; |
1027 |
|
1028 |
if (IsClient(source_p)) |
1029 |
len = ircsprintf(buffer, ":%s!%s@%s WALLOPS :", |
1030 |
source_p->name, source_p->username, source_p->host); |
1031 |
else |
1032 |
len = ircsprintf(buffer, ":%s WALLOPS :", source_p->name); |
1033 |
|
1034 |
va_start(args, pattern); |
1035 |
len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args); |
1036 |
va_end(args); |
1037 |
|
1038 |
DLINK_FOREACH(ptr, oper_list.head) |
1039 |
{ |
1040 |
client_p = ptr->data; |
1041 |
assert(client_p->umodes & UMODE_OPER); |
1042 |
|
1043 |
if ((client_p->umodes & flags) && !IsDefunct(client_p)) |
1044 |
send_message(client_p, buffer, len); |
1045 |
} |
1046 |
} |
1047 |
|
1048 |
/* ts_warn() |
1049 |
* |
1050 |
* inputs - var args message |
1051 |
* output - NONE |
1052 |
* side effects - Call sendto_realops_flags, with some flood checking |
1053 |
* (at most 5 warnings every 5 seconds) |
1054 |
*/ |
1055 |
void |
1056 |
ts_warn(const char *pattern, ...) |
1057 |
{ |
1058 |
va_list args; |
1059 |
char buffer[LOG_BUFSIZE]; |
1060 |
static time_t last = 0; |
1061 |
static int warnings = 0; |
1062 |
|
1063 |
/* |
1064 |
** if we're running with TS_WARNINGS enabled and someone does |
1065 |
** something silly like (remotely) connecting a nonTS server, |
1066 |
** we'll get a ton of warnings, so we make sure we don't send |
1067 |
** more than 5 every 5 seconds. -orabidoo |
1068 |
*/ |
1069 |
|
1070 |
if (CurrentTime - last < 5) |
1071 |
{ |
1072 |
if (++warnings > 5) |
1073 |
return; |
1074 |
} |
1075 |
else |
1076 |
{ |
1077 |
last = CurrentTime; |
1078 |
warnings = 0; |
1079 |
} |
1080 |
|
1081 |
va_start(args, pattern); |
1082 |
vsprintf_irc(buffer, pattern, args); |
1083 |
va_end(args); |
1084 |
|
1085 |
sendto_realops_flags(UMODE_ALL, L_ALL, "%s", buffer); |
1086 |
ilog(L_CRIT, "%s", buffer); |
1087 |
} |
1088 |
|
1089 |
/* kill_client() |
1090 |
* |
1091 |
* inputs - client to send kill towards |
1092 |
* - pointer to client to kill |
1093 |
* - reason for kill |
1094 |
* output - NONE |
1095 |
* side effects - NONE |
1096 |
*/ |
1097 |
void |
1098 |
kill_client(struct Client *client_p, struct Client *diedie, |
1099 |
const char *pattern, ...) |
1100 |
{ |
1101 |
va_list args; |
1102 |
char buffer[IRCD_BUFSIZE]; |
1103 |
int len; |
1104 |
|
1105 |
if (client_p->from != NULL) |
1106 |
client_p = client_p->from; |
1107 |
if (IsDead(client_p)) |
1108 |
return; |
1109 |
|
1110 |
len = ircsprintf(buffer, ":%s KILL %s :", ID_or_name(&me, client_p->from), |
1111 |
ID_or_name(diedie, client_p)); |
1112 |
|
1113 |
va_start(args, pattern); |
1114 |
len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args); |
1115 |
va_end(args); |
1116 |
|
1117 |
send_message(client_p, buffer, len); |
1118 |
} |
1119 |
|
1120 |
/* kill_client_ll_serv_butone() |
1121 |
* |
1122 |
* inputs - pointer to client to not send to |
1123 |
* - pointer to client to kill |
1124 |
* output - NONE |
1125 |
* side effects - Send a KILL for the given client |
1126 |
* message to all connected servers |
1127 |
* except the client 'one'. Also deal with |
1128 |
* client being unknown to leaf, as in lazylink... |
1129 |
*/ |
1130 |
void |
1131 |
kill_client_ll_serv_butone(struct Client *one, struct Client *source_p, |
1132 |
const char *pattern, ...) |
1133 |
{ |
1134 |
va_list args; |
1135 |
int have_uid = 0; |
1136 |
dlink_node *ptr = NULL; |
1137 |
char buf_uid[IRCD_BUFSIZE], buf_nick[IRCD_BUFSIZE]; |
1138 |
int len_uid = 0, len_nick = 0; |
1139 |
|
1140 |
if (HasID(source_p) && (me.id[0] != '\0')) |
1141 |
{ |
1142 |
have_uid = 1; |
1143 |
va_start(args, pattern); |
1144 |
len_uid = ircsprintf(buf_uid, ":%s KILL %s :", me.id, ID(source_p)); |
1145 |
len_uid += send_format(&buf_uid[len_uid], IRCD_BUFSIZE - len_uid, pattern, |
1146 |
args); |
1147 |
va_end(args); |
1148 |
} |
1149 |
|
1150 |
va_start(args, pattern); |
1151 |
len_nick = ircsprintf(buf_nick, ":%s KILL %s :", me.name, source_p->name); |
1152 |
len_nick += send_format(&buf_nick[len_nick], IRCD_BUFSIZE - len_nick, pattern, |
1153 |
args); |
1154 |
va_end(args); |
1155 |
|
1156 |
DLINK_FOREACH(ptr, serv_list.head) |
1157 |
{ |
1158 |
struct Client *client_p = ptr->data; |
1159 |
|
1160 |
if (one != NULL && (client_p == one->from)) |
1161 |
continue; |
1162 |
if (IsDefunct(client_p)) |
1163 |
continue; |
1164 |
|
1165 |
if (have_uid && IsCapable(client_p, CAP_TS6)) |
1166 |
send_message(client_p, buf_uid, len_uid); |
1167 |
else |
1168 |
send_message(client_p, buf_nick, len_nick); |
1169 |
} |
1170 |
} |