]>
Commit | Line | Data |
---|---|---|
212380e3 | 1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * send.c: Functions for sending messages. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 | * USA | |
23 | * | |
24 | * $Id: send.c 1379 2006-05-20 14:11:07Z jilles $ | |
25 | */ | |
26 | ||
27 | #include "stdinc.h" | |
28 | #include "tools.h" | |
29 | #include "send.h" | |
30 | #include "channel.h" | |
31 | #include "class.h" | |
32 | #include "client.h" | |
33 | #include "common.h" | |
34 | #include "irc_string.h" | |
35 | #include "ircd.h" | |
36 | #include "numeric.h" | |
37 | #include "commio.h" | |
38 | #include "s_serv.h" | |
39 | #include "sprintf_irc.h" | |
40 | #include "s_conf.h" | |
41 | #include "s_newconf.h" | |
42 | #include "linebuf.h" | |
43 | #include "s_log.h" | |
44 | #include "memory.h" | |
45 | #include "hook.h" | |
46 | ||
47 | #define LOG_BUFSIZE 2048 | |
48 | ||
49 | /* send the message to the link the target is attached to */ | |
50 | #define send_linebuf(a,b) _send_linebuf((a->from ? a->from : a) ,b) | |
51 | ||
52 | unsigned long current_serial = 0L; | |
53 | ||
54 | /* send_linebuf() | |
55 | * | |
56 | * inputs - client to send to, linebuf to attach | |
57 | * outputs - | |
58 | * side effects - linebuf is attached to client | |
59 | */ | |
60 | static int | |
61 | _send_linebuf(struct Client *to, buf_head_t *linebuf) | |
62 | { | |
63 | if(IsMe(to)) | |
64 | { | |
65 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send message to myself!"); | |
66 | return 0; | |
67 | } | |
68 | ||
69 | if(!MyConnect(to) || IsIOError(to)) | |
70 | return 0; | |
71 | ||
72 | if(linebuf_len(&to->localClient->buf_sendq) > get_sendq(to)) | |
73 | { | |
74 | if(IsServer(to)) | |
75 | { | |
76 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
77 | "Max SendQ limit exceeded for %s: %u > %lu", | |
78 | get_server_name(to, HIDE_IP), | |
79 | linebuf_len(&to->localClient->buf_sendq), | |
80 | get_sendq(to)); | |
81 | ||
82 | ilog(L_SERVER, "Max SendQ limit exceeded for %s: %u > %lu", | |
83 | log_client_name(to, SHOW_IP), | |
84 | linebuf_len(&to->localClient->buf_sendq), | |
85 | get_sendq(to)); | |
86 | } | |
87 | ||
88 | if(IsClient(to)) | |
89 | to->flags |= FLAGS_SENDQEX; | |
90 | ||
91 | dead_link(to); | |
92 | return -1; | |
93 | } | |
94 | else | |
95 | { | |
96 | /* just attach the linebuf to the sendq instead of | |
97 | * generating a new one | |
98 | */ | |
99 | linebuf_attach(&to->localClient->buf_sendq, linebuf); | |
100 | } | |
101 | ||
102 | /* | |
103 | ** Update statistics. The following is slightly incorrect | |
104 | ** because it counts messages even if queued, but bytes | |
105 | ** only really sent. Queued bytes get updated in SendQueued. | |
106 | */ | |
107 | to->localClient->sendM += 1; | |
108 | me.localClient->sendM += 1; | |
109 | if(linebuf_len(&to->localClient->buf_sendq) > 0) | |
110 | send_queued_write(to->localClient->fd, to); | |
111 | return 0; | |
112 | } | |
113 | ||
114 | /* send_linebuf_remote() | |
115 | * | |
116 | * inputs - client to attach to, sender, linebuf | |
117 | * outputs - | |
118 | * side effects - client has linebuf attached | |
119 | */ | |
120 | static void | |
121 | send_linebuf_remote(struct Client *to, struct Client *from, buf_head_t *linebuf) | |
122 | { | |
123 | if(to->from) | |
124 | to = to->from; | |
125 | ||
126 | /* test for fake direction */ | |
127 | if(!MyClient(from) && IsPerson(to) && (to == from->from)) | |
128 | { | |
129 | if(IsServer(from)) | |
130 | { | |
131 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
132 | "Send message to %s[%s] dropped from %s(Fake Dir)", | |
133 | to->name, to->from->name, from->name); | |
134 | return; | |
135 | } | |
136 | ||
137 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
138 | "Ghosted: %s[%s@%s] from %s[%s@%s] (%s)", | |
139 | to->name, to->username, to->host, | |
140 | from->name, from->username, from->host, to->from->name); | |
141 | kill_client_serv_butone(NULL, to, "%s (%s[%s@%s] Ghosted %s)", | |
142 | me.name, to->name, to->username, | |
143 | to->host, to->from->name); | |
144 | ||
145 | to->flags |= FLAGS_KILLED; | |
146 | ||
147 | exit_client(NULL, to, &me, "Ghosted client"); | |
148 | return; | |
149 | } | |
150 | ||
151 | _send_linebuf(to, linebuf); | |
152 | return; | |
153 | } | |
154 | ||
155 | /* send_queued_write() | |
156 | * | |
157 | * inputs - fd to have queue sent, client we're sending to | |
158 | * outputs - contents of queue | |
159 | * side effects - write is rescheduled if queue isnt emptied | |
160 | */ | |
161 | void | |
162 | send_queued_write(int fd, void *data) | |
163 | { | |
164 | struct Client *to = data; | |
165 | int retlen; | |
166 | int flags; | |
167 | #ifdef USE_IODEBUG_HOOKS | |
168 | hook_data_int hd; | |
169 | #endif | |
170 | /* cant write anything to a dead socket. */ | |
171 | if(IsIOError(to)) | |
172 | return; | |
173 | ||
174 | #ifdef USE_IODEBUG_HOOKS | |
175 | hd.client = to; | |
176 | if(to->localClient->buf_sendq.list.head) | |
177 | hd.arg1 = ((buf_line_t *) to->localClient->buf_sendq.list.head->data)->buf + | |
178 | to->localClient->buf_sendq.writeofs; | |
179 | #endif | |
180 | ||
181 | if(linebuf_len(&to->localClient->buf_sendq)) | |
182 | { | |
183 | while ((retlen = | |
184 | linebuf_flush(to->localClient->fd, &to->localClient->buf_sendq)) > 0) | |
185 | { | |
186 | /* We have some data written .. update counters */ | |
187 | #ifdef USE_IODEBUG_HOOKS | |
188 | hd.arg2 = retlen; | |
189 | call_hook(h_iosend_id, &hd); | |
190 | ||
191 | if(to->localClient->buf_sendq.list.head) | |
192 | hd.arg1 = | |
193 | ((buf_line_t *) to->localClient->buf_sendq.list.head-> | |
194 | data)->buf + to->localClient->buf_sendq.writeofs; | |
195 | #endif | |
196 | ||
197 | ||
198 | to->localClient->sendB += retlen; | |
199 | me.localClient->sendB += retlen; | |
200 | if(to->localClient->sendB > 1023) | |
201 | { | |
202 | to->localClient->sendK += (to->localClient->sendB >> 10); | |
203 | to->localClient->sendB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */ | |
204 | } | |
205 | else if(me.localClient->sendB > 1023) | |
206 | { | |
207 | me.localClient->sendK += (me.localClient->sendB >> 10); | |
208 | me.localClient->sendB &= 0x03ff; | |
209 | } | |
210 | } | |
211 | ||
212 | if(retlen == 0 || (retlen < 0 && !ignoreErrno(errno))) | |
213 | { | |
214 | dead_link(to); | |
215 | return; | |
216 | } | |
217 | } | |
218 | if(ignoreErrno(errno)) | |
219 | flags = COMM_SELECT_WRITE|COMM_SELECT_RETRY; | |
220 | else | |
221 | flags = COMM_SELECT_WRITE; | |
222 | if(linebuf_len(&to->localClient->buf_sendq)) | |
223 | comm_setselect(fd, FDLIST_IDLECLIENT, flags, | |
224 | send_queued_write, to, 0); | |
225 | } | |
226 | ||
227 | /* send_queued_slink_write() | |
228 | * | |
229 | * inputs - fd to have queue sent, client we're sending to | |
230 | * outputs - contents of queue | |
231 | * side effects - write is rescheduled if queue isnt emptied | |
232 | */ | |
233 | void | |
234 | send_queued_slink_write(int fd, void *data) | |
235 | { | |
236 | struct Client *to = data; | |
237 | int retlen; | |
238 | ||
239 | /* | |
240 | ** Once socket is marked dead, we cannot start writing to it, | |
241 | ** even if the error is removed... | |
242 | */ | |
243 | if(IsIOError(to)) | |
244 | return; | |
245 | ||
246 | /* Next, lets try to write some data */ | |
247 | if(to->localClient->slinkq) | |
248 | { | |
249 | retlen = write(to->localClient->ctrlfd, | |
250 | to->localClient->slinkq + to->localClient->slinkq_ofs, | |
251 | to->localClient->slinkq_len); | |
252 | ||
253 | if(retlen < 0) | |
254 | { | |
255 | /* If we have a fatal error */ | |
256 | if(!ignoreErrno(errno)) | |
257 | { | |
258 | dead_link(to); | |
259 | return; | |
260 | } | |
261 | } | |
262 | /* 0 bytes is an EOF .. */ | |
263 | else if(retlen == 0) | |
264 | { | |
265 | dead_link(to); | |
266 | return; | |
267 | } | |
268 | else | |
269 | { | |
270 | to->localClient->slinkq_len -= retlen; | |
271 | ||
272 | s_assert(to->localClient->slinkq_len >= 0); | |
273 | if(to->localClient->slinkq_len) | |
274 | to->localClient->slinkq_ofs += retlen; | |
275 | else | |
276 | { | |
277 | to->localClient->slinkq_ofs = 0; | |
278 | MyFree(to->localClient->slinkq); | |
279 | to->localClient->slinkq = NULL; | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
284 | /* if we have any more data, reschedule a write */ | |
285 | if(to->localClient->slinkq_len) | |
286 | comm_setselect(to->localClient->ctrlfd, FDLIST_IDLECLIENT, | |
287 | COMM_SELECT_WRITE|COMM_SELECT_RETRY, send_queued_slink_write, to, 0); | |
288 | } | |
289 | ||
290 | /* sendto_one() | |
291 | * | |
292 | * inputs - client to send to, va_args | |
293 | * outputs - client has message put into its queue | |
294 | * side effects - | |
295 | */ | |
296 | void | |
297 | sendto_one(struct Client *target_p, const char *pattern, ...) | |
298 | { | |
299 | va_list args; | |
300 | buf_head_t linebuf; | |
301 | ||
302 | /* send remote if to->from non NULL */ | |
303 | if(target_p->from != NULL) | |
304 | target_p = target_p->from; | |
305 | ||
306 | if(IsIOError(target_p)) | |
307 | return; | |
308 | ||
309 | linebuf_newbuf(&linebuf); | |
310 | ||
311 | va_start(args, pattern); | |
312 | linebuf_putmsg(&linebuf, pattern, &args, NULL); | |
313 | va_end(args); | |
314 | ||
315 | _send_linebuf(target_p, &linebuf); | |
316 | ||
317 | linebuf_donebuf(&linebuf); | |
318 | ||
319 | } | |
320 | ||
321 | /* sendto_one_prefix() | |
322 | * | |
323 | * inputs - client to send to, va_args | |
324 | * outputs - client has message put into its queue | |
325 | * side effects - source(us)/target is chosen based on TS6 capability | |
326 | */ | |
327 | void | |
328 | sendto_one_prefix(struct Client *target_p, struct Client *source_p, | |
329 | const char *command, const char *pattern, ...) | |
330 | { | |
331 | struct Client *dest_p; | |
332 | va_list args; | |
333 | buf_head_t linebuf; | |
334 | ||
335 | /* send remote if to->from non NULL */ | |
336 | if(target_p->from != NULL) | |
337 | dest_p = target_p->from; | |
338 | else | |
339 | dest_p = target_p; | |
340 | ||
341 | if(IsIOError(dest_p)) | |
342 | return; | |
343 | ||
344 | if(IsMe(dest_p)) | |
345 | { | |
346 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!"); | |
347 | return; | |
348 | } | |
349 | ||
350 | linebuf_newbuf(&linebuf); | |
351 | va_start(args, pattern); | |
352 | linebuf_putmsg(&linebuf, pattern, &args, | |
353 | ":%s %s %s ", | |
354 | get_id(source_p, target_p), | |
355 | command, get_id(target_p, target_p)); | |
356 | va_end(args); | |
357 | ||
358 | _send_linebuf(dest_p, &linebuf); | |
359 | linebuf_donebuf(&linebuf); | |
360 | } | |
361 | ||
362 | /* sendto_one_notice() | |
363 | * | |
364 | * inputs - client to send to, va_args | |
365 | * outputs - client has a NOTICE put into its queue | |
366 | * side effects - source(us)/target is chosen based on TS6 capability | |
367 | */ | |
368 | void | |
369 | sendto_one_notice(struct Client *target_p, const char *pattern, ...) | |
370 | { | |
371 | struct Client *dest_p; | |
372 | va_list args; | |
373 | buf_head_t linebuf; | |
374 | ||
375 | /* send remote if to->from non NULL */ | |
376 | if(target_p->from != NULL) | |
377 | dest_p = target_p->from; | |
378 | else | |
379 | dest_p = target_p; | |
380 | ||
381 | if(IsIOError(dest_p)) | |
382 | return; | |
383 | ||
384 | if(IsMe(dest_p)) | |
385 | { | |
386 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!"); | |
387 | return; | |
388 | } | |
389 | ||
390 | linebuf_newbuf(&linebuf); | |
391 | va_start(args, pattern); | |
392 | linebuf_putmsg(&linebuf, pattern, &args, | |
393 | ":%s NOTICE %s ", | |
394 | get_id(&me, target_p), get_id(target_p, target_p)); | |
395 | va_end(args); | |
396 | ||
397 | _send_linebuf(dest_p, &linebuf); | |
398 | linebuf_donebuf(&linebuf); | |
399 | } | |
400 | ||
401 | ||
402 | /* sendto_one_numeric() | |
403 | * | |
404 | * inputs - client to send to, va_args | |
405 | * outputs - client has message put into its queue | |
406 | * side effects - source/target is chosen based on TS6 capability | |
407 | */ | |
408 | void | |
409 | sendto_one_numeric(struct Client *target_p, int numeric, const char *pattern, ...) | |
410 | { | |
411 | struct Client *dest_p; | |
412 | va_list args; | |
413 | buf_head_t linebuf; | |
414 | ||
415 | /* send remote if to->from non NULL */ | |
416 | if(target_p->from != NULL) | |
417 | dest_p = target_p->from; | |
418 | else | |
419 | dest_p = target_p; | |
420 | ||
421 | if(IsIOError(dest_p)) | |
422 | return; | |
423 | ||
424 | if(IsMe(dest_p)) | |
425 | { | |
426 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Trying to send to myself!"); | |
427 | return; | |
428 | } | |
429 | ||
430 | linebuf_newbuf(&linebuf); | |
431 | va_start(args, pattern); | |
432 | linebuf_putmsg(&linebuf, pattern, &args, | |
433 | ":%s %03d %s ", | |
434 | get_id(&me, target_p), | |
435 | numeric, get_id(target_p, target_p)); | |
436 | va_end(args); | |
437 | ||
438 | _send_linebuf(dest_p, &linebuf); | |
439 | linebuf_donebuf(&linebuf); | |
440 | } | |
441 | ||
442 | /* | |
443 | * sendto_server | |
444 | * | |
445 | * inputs - pointer to client to NOT send to | |
446 | * - caps or'd together which must ALL be present | |
447 | * - caps or'd together which must ALL NOT be present | |
448 | * - printf style format string | |
449 | * - args to format string | |
450 | * output - NONE | |
451 | * side effects - Send a message to all connected servers, except the | |
452 | * client 'one' (if non-NULL), as long as the servers | |
453 | * support ALL capabs in 'caps', and NO capabs in 'nocaps'. | |
454 | * | |
455 | * This function was written in an attempt to merge together the other | |
456 | * billion sendto_*serv*() functions, which sprung up with capabs, uids etc | |
457 | * -davidt | |
458 | */ | |
459 | void | |
460 | sendto_server(struct Client *one, struct Channel *chptr, unsigned long caps, | |
461 | unsigned long nocaps, const char *format, ...) | |
462 | { | |
463 | va_list args; | |
464 | struct Client *target_p; | |
465 | dlink_node *ptr; | |
466 | dlink_node *next_ptr; | |
467 | buf_head_t linebuf; | |
468 | ||
469 | /* noone to send to.. */ | |
470 | if(dlink_list_length(&serv_list) == 0) | |
471 | return; | |
472 | ||
473 | if(chptr != NULL && *chptr->chname != '#') | |
474 | return; | |
475 | ||
476 | linebuf_newbuf(&linebuf); | |
477 | va_start(args, format); | |
478 | linebuf_putmsg(&linebuf, format, &args, NULL); | |
479 | va_end(args); | |
480 | ||
481 | DLINK_FOREACH_SAFE(ptr, next_ptr, serv_list.head) | |
482 | { | |
483 | target_p = ptr->data; | |
484 | ||
485 | /* check against 'one' */ | |
486 | if(one != NULL && (target_p == one->from)) | |
487 | continue; | |
488 | ||
489 | /* check we have required capabs */ | |
490 | if(!IsCapable(target_p, caps)) | |
491 | continue; | |
492 | ||
493 | /* check we don't have any forbidden capabs */ | |
494 | if(!NotCapable(target_p, nocaps)) | |
495 | continue; | |
496 | ||
497 | _send_linebuf(target_p, &linebuf); | |
498 | } | |
499 | ||
500 | linebuf_donebuf(&linebuf); | |
501 | ||
502 | } | |
503 | ||
504 | /* sendto_channel_flags() | |
505 | * | |
506 | * inputs - server not to send to, flags needed, source, channel, va_args | |
507 | * outputs - message is sent to channel members | |
508 | * side effects - | |
509 | */ | |
510 | void | |
511 | sendto_channel_flags(struct Client *one, int type, struct Client *source_p, | |
512 | struct Channel *chptr, const char *pattern, ...) | |
513 | { | |
514 | static char buf[BUFSIZE]; | |
515 | va_list args; | |
516 | buf_head_t linebuf_local; | |
517 | buf_head_t linebuf_name; | |
518 | buf_head_t linebuf_id; | |
519 | struct Client *target_p; | |
520 | struct membership *msptr; | |
521 | dlink_node *ptr; | |
522 | dlink_node *next_ptr; | |
523 | ||
524 | linebuf_newbuf(&linebuf_local); | |
525 | linebuf_newbuf(&linebuf_name); | |
526 | linebuf_newbuf(&linebuf_id); | |
527 | ||
528 | current_serial++; | |
529 | ||
530 | va_start(args, pattern); | |
531 | ircvsnprintf(buf, sizeof(buf), pattern, args); | |
532 | va_end(args); | |
533 | ||
534 | if(IsServer(source_p)) | |
535 | linebuf_putmsg(&linebuf_local, NULL, NULL, | |
536 | ":%s %s", source_p->name, buf); | |
537 | else | |
538 | linebuf_putmsg(&linebuf_local, NULL, NULL, | |
539 | ":%s!%s@%s %s", | |
540 | source_p->name, source_p->username, | |
541 | source_p->host, buf); | |
542 | ||
543 | linebuf_putmsg(&linebuf_name, NULL, NULL, ":%s %s", source_p->name, buf); | |
544 | linebuf_putmsg(&linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf); | |
545 | ||
546 | DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->members.head) | |
547 | { | |
548 | msptr = ptr->data; | |
549 | target_p = msptr->client_p; | |
550 | ||
551 | if(IsIOError(target_p->from) || target_p->from == one) | |
552 | continue; | |
553 | ||
554 | if(type && ((msptr->flags & type) == 0)) | |
555 | continue; | |
556 | ||
557 | if(IsDeaf(target_p)) | |
558 | continue; | |
559 | ||
560 | if(!MyClient(target_p)) | |
561 | { | |
562 | /* if we've got a specific type, target must support | |
563 | * CHW.. --fl | |
564 | */ | |
565 | if(type && NotCapable(target_p->from, CAP_CHW)) | |
566 | continue; | |
567 | ||
568 | if(target_p->from->serial != current_serial) | |
569 | { | |
570 | if(has_id(target_p->from)) | |
571 | send_linebuf_remote(target_p, source_p, &linebuf_id); | |
572 | else | |
573 | send_linebuf_remote(target_p, source_p, &linebuf_name); | |
574 | ||
575 | target_p->from->serial = current_serial; | |
576 | } | |
577 | } | |
578 | else | |
579 | _send_linebuf(target_p, &linebuf_local); | |
580 | } | |
581 | ||
582 | linebuf_donebuf(&linebuf_local); | |
583 | linebuf_donebuf(&linebuf_name); | |
584 | linebuf_donebuf(&linebuf_id); | |
585 | } | |
586 | ||
587 | ||
588 | /* sendto_channel_local() | |
589 | * | |
590 | * inputs - flags to send to, channel to send to, va_args | |
591 | * outputs - message to local channel members | |
592 | * side effects - | |
593 | */ | |
594 | void | |
595 | sendto_channel_local(int type, struct Channel *chptr, const char *pattern, ...) | |
596 | { | |
597 | va_list args; | |
598 | buf_head_t linebuf; | |
599 | struct membership *msptr; | |
600 | struct Client *target_p; | |
601 | dlink_node *ptr; | |
602 | dlink_node *next_ptr; | |
603 | ||
604 | linebuf_newbuf(&linebuf); | |
605 | ||
606 | va_start(args, pattern); | |
607 | linebuf_putmsg(&linebuf, pattern, &args, NULL); | |
608 | va_end(args); | |
609 | ||
610 | DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head) | |
611 | { | |
612 | msptr = ptr->data; | |
613 | target_p = msptr->client_p; | |
614 | ||
615 | if(IsIOError(target_p)) | |
616 | continue; | |
617 | ||
618 | if(type && ((msptr->flags & type) == 0)) | |
619 | continue; | |
620 | ||
621 | _send_linebuf(target_p, &linebuf); | |
622 | } | |
623 | ||
624 | linebuf_donebuf(&linebuf); | |
625 | } | |
626 | ||
627 | /* sendto_channel_local_butone() | |
628 | * | |
629 | * inputs - flags to send to, channel to send to, va_args | |
630 | * - user to ignore when sending | |
631 | * outputs - message to local channel members | |
632 | * side effects - | |
633 | */ | |
634 | void | |
635 | sendto_channel_local_butone(struct Client *one, int type, struct Channel *chptr, const char *pattern, ...) | |
636 | { | |
637 | va_list args; | |
638 | buf_head_t linebuf; | |
639 | struct membership *msptr; | |
640 | struct Client *target_p; | |
641 | dlink_node *ptr; | |
642 | dlink_node *next_ptr; | |
643 | ||
644 | linebuf_newbuf(&linebuf); | |
645 | ||
646 | va_start(args, pattern); | |
647 | linebuf_putmsg(&linebuf, pattern, &args, NULL); | |
648 | va_end(args); | |
649 | ||
650 | DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head) | |
651 | { | |
652 | msptr = ptr->data; | |
653 | target_p = msptr->client_p; | |
654 | ||
655 | if(target_p == one) | |
656 | continue; | |
657 | ||
658 | if(IsIOError(target_p)) | |
659 | continue; | |
660 | ||
661 | if(type && ((msptr->flags & type) == 0)) | |
662 | continue; | |
663 | ||
664 | _send_linebuf(target_p, &linebuf); | |
665 | } | |
666 | ||
667 | linebuf_donebuf(&linebuf); | |
668 | } | |
669 | ||
670 | /* | |
671 | * sendto_common_channels_local() | |
672 | * | |
673 | * inputs - pointer to client | |
674 | * - pattern to send | |
675 | * output - NONE | |
676 | * side effects - Sends a message to all people on local server who are | |
677 | * in same channel with user. | |
678 | * used by m_nick.c and exit_one_client. | |
679 | */ | |
680 | void | |
681 | sendto_common_channels_local(struct Client *user, const char *pattern, ...) | |
682 | { | |
683 | va_list args; | |
684 | dlink_node *ptr; | |
685 | dlink_node *next_ptr; | |
686 | dlink_node *uptr; | |
687 | dlink_node *next_uptr; | |
688 | struct Channel *chptr; | |
689 | struct Client *target_p; | |
690 | struct membership *msptr; | |
691 | struct membership *mscptr; | |
692 | buf_head_t linebuf; | |
693 | ||
694 | linebuf_newbuf(&linebuf); | |
695 | va_start(args, pattern); | |
696 | linebuf_putmsg(&linebuf, pattern, &args, NULL); | |
697 | va_end(args); | |
698 | ||
699 | ++current_serial; | |
700 | ||
701 | DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head) | |
702 | { | |
703 | mscptr = ptr->data; | |
704 | chptr = mscptr->chptr; | |
705 | ||
706 | DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head) | |
707 | { | |
708 | msptr = uptr->data; | |
709 | target_p = msptr->client_p; | |
710 | ||
711 | if(IsIOError(target_p) || | |
712 | target_p->serial == current_serial) | |
713 | continue; | |
714 | ||
715 | target_p->serial = current_serial; | |
716 | send_linebuf(target_p, &linebuf); | |
717 | } | |
718 | } | |
719 | ||
720 | /* this can happen when the user isnt in any channels, but we still | |
721 | * need to send them the data, ie a nick change | |
722 | */ | |
723 | if(MyConnect(user) && (user->serial != current_serial)) | |
724 | send_linebuf(user, &linebuf); | |
725 | ||
726 | linebuf_donebuf(&linebuf); | |
727 | } | |
728 | ||
729 | /* | |
730 | * sendto_common_channels_local_butone() | |
731 | * | |
732 | * inputs - pointer to client | |
733 | * - pattern to send | |
734 | * output - NONE | |
735 | * side effects - Sends a message to all people on local server who are | |
736 | * in same channel with user, except for user itself. | |
737 | */ | |
738 | void | |
739 | sendto_common_channels_local_butone(struct Client *user, const char *pattern, ...) | |
740 | { | |
741 | va_list args; | |
742 | dlink_node *ptr; | |
743 | dlink_node *next_ptr; | |
744 | dlink_node *uptr; | |
745 | dlink_node *next_uptr; | |
746 | struct Channel *chptr; | |
747 | struct Client *target_p; | |
748 | struct membership *msptr; | |
749 | struct membership *mscptr; | |
750 | buf_head_t linebuf; | |
751 | ||
752 | linebuf_newbuf(&linebuf); | |
753 | va_start(args, pattern); | |
754 | linebuf_putmsg(&linebuf, pattern, &args, NULL); | |
755 | va_end(args); | |
756 | ||
757 | ++current_serial; | |
758 | /* Skip them -- jilles */ | |
759 | user->serial = current_serial; | |
760 | ||
761 | DLINK_FOREACH_SAFE(ptr, next_ptr, user->user->channel.head) | |
762 | { | |
763 | mscptr = ptr->data; | |
764 | chptr = mscptr->chptr; | |
765 | ||
766 | DLINK_FOREACH_SAFE(uptr, next_uptr, chptr->locmembers.head) | |
767 | { | |
768 | msptr = uptr->data; | |
769 | target_p = msptr->client_p; | |
770 | ||
771 | if(IsIOError(target_p) || | |
772 | target_p->serial == current_serial) | |
773 | continue; | |
774 | ||
775 | target_p->serial = current_serial; | |
776 | send_linebuf(target_p, &linebuf); | |
777 | } | |
778 | } | |
779 | ||
780 | linebuf_donebuf(&linebuf); | |
781 | } | |
782 | ||
783 | /* sendto_match_butone() | |
784 | * | |
785 | * inputs - server not to send to, source, mask, type of mask, va_args | |
786 | * output - | |
787 | * side effects - message is sent to matching clients | |
788 | */ | |
789 | void | |
790 | sendto_match_butone(struct Client *one, struct Client *source_p, | |
791 | const char *mask, int what, const char *pattern, ...) | |
792 | { | |
793 | static char buf[BUFSIZE]; | |
794 | va_list args; | |
795 | struct Client *target_p; | |
796 | dlink_node *ptr; | |
797 | dlink_node *next_ptr; | |
798 | buf_head_t linebuf_local; | |
799 | buf_head_t linebuf_name; | |
800 | buf_head_t linebuf_id; | |
801 | ||
802 | linebuf_newbuf(&linebuf_local); | |
803 | linebuf_newbuf(&linebuf_name); | |
804 | linebuf_newbuf(&linebuf_id); | |
805 | ||
806 | va_start(args, pattern); | |
807 | ircvsnprintf(buf, sizeof(buf), pattern, args); | |
808 | va_end(args); | |
809 | ||
810 | if(IsServer(source_p)) | |
811 | linebuf_putmsg(&linebuf_local, NULL, NULL, | |
812 | ":%s %s", source_p->name, buf); | |
813 | else | |
814 | linebuf_putmsg(&linebuf_local, NULL, NULL, | |
815 | ":%s!%s@%s %s", | |
816 | source_p->name, source_p->username, | |
817 | source_p->host, buf); | |
818 | ||
819 | linebuf_putmsg(&linebuf_name, NULL, NULL, ":%s %s", source_p->name, buf); | |
820 | linebuf_putmsg(&linebuf_id, NULL, NULL, ":%s %s", use_id(source_p), buf); | |
821 | ||
822 | if(what == MATCH_HOST) | |
823 | { | |
824 | DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) | |
825 | { | |
826 | target_p = ptr->data; | |
827 | ||
828 | if(match(mask, target_p->host)) | |
829 | _send_linebuf(target_p, &linebuf_local); | |
830 | } | |
831 | } | |
832 | /* what = MATCH_SERVER, if it doesnt match us, just send remote */ | |
833 | else if(match(mask, me.name)) | |
834 | { | |
835 | DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) | |
836 | { | |
837 | target_p = ptr->data; | |
838 | _send_linebuf(target_p, &linebuf_local); | |
839 | } | |
840 | } | |
841 | ||
842 | DLINK_FOREACH(ptr, serv_list.head) | |
843 | { | |
844 | target_p = ptr->data; | |
845 | ||
846 | if(target_p == one) | |
847 | continue; | |
848 | ||
849 | if(has_id(target_p)) | |
850 | send_linebuf_remote(target_p, source_p, &linebuf_id); | |
851 | else | |
852 | send_linebuf_remote(target_p, source_p, &linebuf_name); | |
853 | } | |
854 | ||
855 | linebuf_donebuf(&linebuf_local); | |
856 | linebuf_donebuf(&linebuf_id); | |
857 | linebuf_donebuf(&linebuf_name); | |
858 | } | |
859 | ||
860 | /* sendto_match_servs() | |
861 | * | |
862 | * inputs - source, mask to send to, caps needed, va_args | |
863 | * outputs - | |
864 | * side effects - message is sent to matching servers with caps. | |
865 | */ | |
866 | void | |
867 | sendto_match_servs(struct Client *source_p, const char *mask, int cap, | |
868 | int nocap, const char *pattern, ...) | |
869 | { | |
870 | static char buf[BUFSIZE]; | |
871 | va_list args; | |
872 | dlink_node *ptr; | |
873 | struct Client *target_p; | |
874 | buf_head_t linebuf_id; | |
875 | buf_head_t linebuf_name; | |
876 | ||
877 | if(EmptyString(mask)) | |
878 | return; | |
879 | ||
880 | linebuf_newbuf(&linebuf_id); | |
881 | linebuf_newbuf(&linebuf_name); | |
882 | ||
883 | va_start(args, pattern); | |
884 | ircvsnprintf(buf, sizeof(buf), pattern, args); | |
885 | va_end(args); | |
886 | ||
887 | linebuf_putmsg(&linebuf_id, NULL, NULL, | |
888 | ":%s %s", use_id(source_p), buf); | |
889 | linebuf_putmsg(&linebuf_name, NULL, NULL, | |
890 | ":%s %s", source_p->name, buf); | |
891 | ||
892 | current_serial++; | |
893 | ||
894 | DLINK_FOREACH(ptr, global_serv_list.head) | |
895 | { | |
896 | target_p = ptr->data; | |
897 | ||
898 | /* dont send to ourselves, or back to where it came from.. */ | |
899 | if(IsMe(target_p) || target_p->from == source_p->from) | |
900 | continue; | |
901 | ||
902 | if(target_p->from->serial == current_serial) | |
903 | continue; | |
904 | ||
905 | if(match(mask, target_p->name)) | |
906 | { | |
907 | /* if we set the serial here, then we'll never do | |
908 | * a match() again if !IsCapable() | |
909 | */ | |
910 | target_p->from->serial = current_serial; | |
911 | ||
912 | if(cap && !IsCapable(target_p->from, cap)) | |
913 | continue; | |
914 | ||
915 | if(nocap && !NotCapable(target_p->from, nocap)) | |
916 | continue; | |
917 | ||
918 | if(has_id(target_p->from)) | |
919 | _send_linebuf(target_p->from, &linebuf_id); | |
920 | else | |
921 | _send_linebuf(target_p->from, &linebuf_name); | |
922 | } | |
923 | } | |
924 | ||
925 | linebuf_donebuf(&linebuf_id); | |
926 | linebuf_donebuf(&linebuf_name); | |
927 | } | |
928 | ||
929 | /* sendto_anywhere() | |
930 | * | |
931 | * inputs - target, source, va_args | |
932 | * outputs - | |
933 | * side effects - client is sent message with correct prefix. | |
934 | */ | |
935 | void | |
936 | sendto_anywhere(struct Client *target_p, struct Client *source_p, | |
937 | const char *command, const char *pattern, ...) | |
938 | { | |
939 | va_list args; | |
940 | buf_head_t linebuf; | |
941 | ||
942 | linebuf_newbuf(&linebuf); | |
943 | ||
944 | va_start(args, pattern); | |
945 | ||
946 | if(MyClient(target_p)) | |
947 | { | |
948 | if(IsServer(source_p)) | |
949 | linebuf_putmsg(&linebuf, pattern, &args, ":%s %s %s ", | |
950 | source_p->name, command, | |
951 | target_p->name); | |
952 | else | |
953 | linebuf_putmsg(&linebuf, pattern, &args, | |
954 | ":%s!%s@%s %s %s ", | |
955 | source_p->name, source_p->username, | |
956 | source_p->host, command, | |
957 | target_p->name); | |
958 | } | |
959 | else | |
960 | linebuf_putmsg(&linebuf, pattern, &args, ":%s %s %s ", | |
961 | get_id(source_p, target_p), command, | |
962 | get_id(target_p, target_p)); | |
963 | va_end(args); | |
964 | ||
965 | if(MyClient(target_p)) | |
966 | _send_linebuf(target_p, &linebuf); | |
967 | else | |
968 | send_linebuf_remote(target_p, source_p, &linebuf); | |
969 | ||
970 | linebuf_donebuf(&linebuf); | |
971 | } | |
972 | ||
973 | /* sendto_realops_flags() | |
974 | * | |
975 | * inputs - umode needed, level (opers/admin), va_args | |
976 | * output - | |
977 | * side effects - message is sent to opers with matching umodes | |
978 | */ | |
979 | void | |
980 | sendto_realops_flags(int flags, int level, const char *pattern, ...) | |
981 | { | |
982 | struct Client *client_p; | |
983 | dlink_node *ptr; | |
984 | dlink_node *next_ptr; | |
985 | va_list args; | |
986 | buf_head_t linebuf; | |
987 | ||
988 | linebuf_newbuf(&linebuf); | |
989 | ||
990 | va_start(args, pattern); | |
991 | linebuf_putmsg(&linebuf, pattern, &args, | |
992 | ":%s NOTICE * :*** Notice -- ", me.name); | |
993 | va_end(args); | |
994 | ||
995 | DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head) | |
996 | { | |
997 | client_p = ptr->data; | |
998 | ||
999 | /* If we're sending it to opers and theyre an admin, skip. | |
1000 | * If we're sending it to admins, and theyre not, skip. | |
1001 | */ | |
1002 | if(((level == L_ADMIN) && !IsOperAdmin(client_p)) || | |
1003 | ((level == L_OPER) && IsOperAdmin(client_p))) | |
1004 | continue; | |
1005 | ||
1006 | if(client_p->umodes & flags) | |
1007 | _send_linebuf(client_p, &linebuf); | |
1008 | } | |
1009 | ||
1010 | linebuf_donebuf(&linebuf); | |
1011 | } | |
1012 | ||
1013 | /* sendto_realops_snomask() | |
1014 | * | |
1015 | * inputs - snomask needed, level (opers/admin), va_args | |
1016 | * output - | |
1017 | * side effects - message is sent to opers with matching snomasks | |
1018 | */ | |
1019 | void | |
1020 | sendto_realops_snomask(int flags, int level, const char *pattern, ...) | |
1021 | { | |
1022 | static char buf[BUFSIZE]; | |
1023 | char *snobuf; | |
1024 | struct Client *client_p; | |
1025 | dlink_node *ptr; | |
1026 | dlink_node *next_ptr; | |
1027 | va_list args; | |
1028 | buf_head_t linebuf; | |
1029 | ||
1030 | linebuf_newbuf(&linebuf); | |
1031 | ||
1032 | /* Be very sure not to do things like "Trying to send to myself" | |
1033 | * L_NETWIDE, otherwise infinite recursion may result! -- jilles */ | |
1034 | if (level & L_NETWIDE && ConfigFileEntry.global_snotices) | |
1035 | { | |
1036 | /* rather a lot of copying around, oh well -- jilles */ | |
1037 | va_start(args, pattern); | |
1038 | ircvsnprintf(buf, sizeof(buf), pattern, args); | |
1039 | va_end(args); | |
1040 | linebuf_putmsg(&linebuf, pattern, NULL, | |
1041 | ":%s NOTICE * :*** Notice -- %s", me.name, buf); | |
1042 | snobuf = construct_snobuf(flags); | |
1043 | if (snobuf[1] != '\0') | |
1044 | { | |
1045 | sendto_server(NULL, NULL, CAP_ENCAP|CAP_TS6, NOCAPS, | |
1046 | ":%s ENCAP * SNOTE %c :%s", | |
1047 | me.id, snobuf[1], buf); | |
1048 | sendto_server(NULL, NULL, CAP_ENCAP, CAP_TS6, | |
1049 | ":%s ENCAP * SNOTE %c :%s", | |
1050 | me.name, snobuf[1], buf); | |
1051 | } | |
1052 | } | |
1053 | else | |
1054 | { | |
1055 | va_start(args, pattern); | |
1056 | linebuf_putmsg(&linebuf, pattern, &args, | |
1057 | ":%s NOTICE * :*** Notice -- ", me.name); | |
1058 | va_end(args); | |
1059 | } | |
1060 | level &= ~L_NETWIDE; | |
1061 | ||
1062 | DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head) | |
1063 | { | |
1064 | client_p = ptr->data; | |
1065 | ||
1066 | /* If we're sending it to opers and theyre an admin, skip. | |
1067 | * If we're sending it to admins, and theyre not, skip. | |
1068 | */ | |
1069 | if(((level == L_ADMIN) && !IsOperAdmin(client_p)) || | |
1070 | ((level == L_OPER) && IsOperAdmin(client_p))) | |
1071 | continue; | |
1072 | ||
1073 | if(client_p->snomask & flags) | |
1074 | _send_linebuf(client_p, &linebuf); | |
1075 | } | |
1076 | ||
1077 | linebuf_donebuf(&linebuf); | |
1078 | } | |
1079 | /* sendto_realops_snomask_from() | |
1080 | * | |
1081 | * inputs - snomask needed, level (opers/admin), source server, va_args | |
1082 | * output - | |
1083 | * side effects - message is sent to opers with matching snomask | |
1084 | */ | |
1085 | void | |
1086 | sendto_realops_snomask_from(int flags, int level, struct Client *source_p, | |
1087 | const char *pattern, ...) | |
1088 | { | |
1089 | struct Client *client_p; | |
1090 | dlink_node *ptr; | |
1091 | dlink_node *next_ptr; | |
1092 | va_list args; | |
1093 | buf_head_t linebuf; | |
1094 | ||
1095 | linebuf_newbuf(&linebuf); | |
1096 | ||
1097 | va_start(args, pattern); | |
1098 | linebuf_putmsg(&linebuf, pattern, &args, | |
1099 | ":%s NOTICE * :*** Notice -- ", source_p->name); | |
1100 | va_end(args); | |
1101 | ||
1102 | DLINK_FOREACH_SAFE(ptr, next_ptr, local_oper_list.head) | |
1103 | { | |
1104 | client_p = ptr->data; | |
1105 | ||
1106 | /* If we're sending it to opers and theyre an admin, skip. | |
1107 | * If we're sending it to admins, and theyre not, skip. | |
1108 | */ | |
1109 | if(((level == L_ADMIN) && !IsOperAdmin(client_p)) || | |
1110 | ((level == L_OPER) && IsOperAdmin(client_p))) | |
1111 | continue; | |
1112 | ||
1113 | if(client_p->snomask & flags) | |
1114 | _send_linebuf(client_p, &linebuf); | |
1115 | } | |
1116 | ||
1117 | linebuf_donebuf(&linebuf); | |
1118 | } | |
1119 | ||
1120 | /* | |
1121 | * sendto_wallops_flags | |
1122 | * | |
1123 | * inputs - flag types of messages to show to real opers | |
1124 | * - client sending request | |
1125 | * - var args input message | |
1126 | * output - NONE | |
1127 | * side effects - Send a wallops to local opers | |
1128 | */ | |
1129 | void | |
1130 | sendto_wallops_flags(int flags, struct Client *source_p, const char *pattern, ...) | |
1131 | { | |
1132 | struct Client *client_p; | |
1133 | dlink_node *ptr; | |
1134 | dlink_node *next_ptr; | |
1135 | va_list args; | |
1136 | buf_head_t linebuf; | |
1137 | ||
1138 | linebuf_newbuf(&linebuf); | |
1139 | ||
1140 | va_start(args, pattern); | |
1141 | ||
1142 | if(IsPerson(source_p)) | |
1143 | linebuf_putmsg(&linebuf, pattern, &args, | |
1144 | ":%s!%s@%s WALLOPS :", source_p->name, | |
1145 | source_p->username, source_p->host); | |
1146 | else | |
1147 | linebuf_putmsg(&linebuf, pattern, &args, ":%s WALLOPS :", source_p->name); | |
1148 | ||
1149 | va_end(args); | |
1150 | ||
1151 | DLINK_FOREACH_SAFE(ptr, next_ptr, IsPerson(source_p) && flags == UMODE_WALLOP ? lclient_list.head : local_oper_list.head) | |
1152 | { | |
1153 | client_p = ptr->data; | |
1154 | ||
1155 | if(client_p->umodes & flags) | |
1156 | _send_linebuf(client_p, &linebuf); | |
1157 | } | |
1158 | ||
1159 | linebuf_donebuf(&linebuf); | |
1160 | } | |
1161 | ||
1162 | /* kill_client() | |
1163 | * | |
1164 | * input - client to send kill to, client to kill, va_args | |
1165 | * output - | |
1166 | * side effects - we issue a kill for the client | |
1167 | */ | |
1168 | void | |
1169 | kill_client(struct Client *target_p, struct Client *diedie, const char *pattern, ...) | |
1170 | { | |
1171 | va_list args; | |
1172 | buf_head_t linebuf; | |
1173 | ||
1174 | linebuf_newbuf(&linebuf); | |
1175 | ||
1176 | va_start(args, pattern); | |
1177 | linebuf_putmsg(&linebuf, pattern, &args, ":%s KILL %s :", | |
1178 | get_id(&me, target_p), get_id(diedie, target_p)); | |
1179 | va_end(args); | |
1180 | ||
1181 | send_linebuf(target_p, &linebuf); | |
1182 | linebuf_donebuf(&linebuf); | |
1183 | } | |
1184 | ||
1185 | ||
1186 | /* | |
1187 | * kill_client_serv_butone | |
1188 | * | |
1189 | * inputs - pointer to client to not send to | |
1190 | * - pointer to client to kill | |
1191 | * output - NONE | |
1192 | * side effects - Send a KILL for the given client | |
1193 | * message to all connected servers | |
1194 | * except the client 'one'. Also deal with | |
1195 | * client being unknown to leaf, as in lazylink... | |
1196 | */ | |
1197 | void | |
1198 | kill_client_serv_butone(struct Client *one, struct Client *target_p, const char *pattern, ...) | |
1199 | { | |
1200 | static char buf[BUFSIZE]; | |
1201 | va_list args; | |
1202 | struct Client *client_p; | |
1203 | dlink_node *ptr; | |
1204 | dlink_node *next_ptr; | |
1205 | buf_head_t linebuf_id; | |
1206 | buf_head_t linebuf_name; | |
1207 | ||
1208 | linebuf_newbuf(&linebuf_name); | |
1209 | linebuf_newbuf(&linebuf_id); | |
1210 | ||
1211 | va_start(args, pattern); | |
1212 | ircvsnprintf(buf, sizeof(buf), pattern, args); | |
1213 | va_end(args); | |
1214 | ||
1215 | linebuf_putmsg(&linebuf_name, NULL, NULL, ":%s KILL %s :%s", | |
1216 | me.name, target_p->name, buf); | |
1217 | linebuf_putmsg(&linebuf_id, NULL, NULL, ":%s KILL %s :%s", | |
1218 | use_id(&me), use_id(target_p), buf); | |
1219 | ||
1220 | DLINK_FOREACH_SAFE(ptr, next_ptr, serv_list.head) | |
1221 | { | |
1222 | client_p = ptr->data; | |
1223 | ||
1224 | /* ok, if the client we're supposed to not send to has an | |
1225 | * ID, then we still want to issue the kill there.. | |
1226 | */ | |
1227 | if(one != NULL && (client_p == one->from) && | |
1228 | (!has_id(client_p) || !has_id(target_p))) | |
1229 | continue; | |
1230 | ||
1231 | if(has_id(client_p)) | |
1232 | _send_linebuf(client_p, &linebuf_id); | |
1233 | else | |
1234 | _send_linebuf(client_p, &linebuf_name); | |
1235 | } | |
1236 | ||
1237 | linebuf_donebuf(&linebuf_id); | |
1238 | linebuf_donebuf(&linebuf_name); | |
1239 | } |