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