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