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