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