]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/m_stats.c
BAN: Avoid fake direction.
[irc/rqf/shadowircd.git] / modules / m_stats.c
1 /*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * m_stats.c: Sends the user statistics or config information.
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: m_stats.c 1608 2006-06-04 02:11:40Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "class.h" /* report_classes */
29 #include "client.h" /* Client */
30 #include "common.h" /* TRUE/FALSE */
31 #include "match.h"
32 #include "ircd.h" /* me */
33 #include "listener.h" /* show_ports */
34 #include "msg.h" /* Message */
35 #include "hostmask.h" /* report_mtrie_conf_links */
36 #include "numeric.h" /* ERR_xxx */
37 #include "scache.h" /* list_scache */
38 #include "send.h" /* sendto_one */
39 #include "s_conf.h" /* ConfItem */
40 #include "s_serv.h" /* hunt_server */
41 #include "s_stats.h"
42 #include "s_user.h" /* show_opers */
43 #include "blacklist.h" /* dnsbl stuff */
44 #include "parse.h"
45 #include "modules.h"
46 #include "hook.h"
47 #include "s_newconf.h"
48 #include "hash.h"
49 #include "reject.h"
50 #include "whowas.h"
51
52 static int m_stats (struct Client *, struct Client *, int, const char **);
53
54 struct Message stats_msgtab = {
55 "STATS", 0, 0, 0, MFLG_SLOW,
56 {mg_unreg, {m_stats, 2}, {m_stats, 3}, mg_ignore, mg_ignore, {m_stats, 2}}
57 };
58
59 int doing_stats_hook;
60 int doing_stats_p_hook;
61
62 mapi_clist_av1 stats_clist[] = { &stats_msgtab, NULL };
63 mapi_hlist_av1 stats_hlist[] = {
64 { "doing_stats", &doing_stats_hook },
65 { "doing_stats_p", &doing_stats_p_hook },
66 { NULL, NULL }
67 };
68
69 DECLARE_MODULE_AV1(stats, NULL, NULL, stats_clist, stats_hlist, NULL, "$Revision: 1608 $");
70
71 const char *Lformat = "%s %u %u %u %u %u :%u %u %s";
72
73 static void stats_l_list(struct Client *s, const char *, int, int, rb_dlink_list *, char);
74 static void stats_l_client(struct Client *source_p, struct Client *target_p,
75 char statchar);
76
77 static void stats_spy(struct Client *, char, const char *);
78 static void stats_p_spy(struct Client *);
79
80 /* Heres our struct for the stats table */
81 struct StatsStruct
82 {
83 char letter;
84 void (*handler) (struct Client *source_p);
85 int need_oper;
86 int need_admin;
87 };
88
89 static void stats_dns_servers(struct Client *);
90 static void stats_delay(struct Client *);
91 static void stats_hash(struct Client *);
92 static void stats_connect(struct Client *);
93 static void stats_tdeny(struct Client *);
94 static void stats_deny(struct Client *);
95 static void stats_exempt(struct Client *);
96 static void stats_events(struct Client *);
97 static void stats_hubleaf(struct Client *);
98 static void stats_auth(struct Client *);
99 static void stats_tklines(struct Client *);
100 static void stats_klines(struct Client *);
101 static void stats_messages(struct Client *);
102 static void stats_dnsbl(struct Client *);
103 static void stats_oper(struct Client *);
104 static void stats_privset(struct Client *);
105 static void stats_operedup(struct Client *);
106 static void stats_ports(struct Client *);
107 static void stats_tresv(struct Client *);
108 static void stats_resv(struct Client *);
109 static void stats_usage(struct Client *);
110 static void stats_tstats(struct Client *);
111 static void stats_uptime(struct Client *);
112 static void stats_shared(struct Client *);
113 static void stats_servers(struct Client *);
114 static void stats_tgecos(struct Client *);
115 static void stats_gecos(struct Client *);
116 static void stats_class(struct Client *);
117 static void stats_memory(struct Client *);
118 static void stats_servlinks(struct Client *);
119 static void stats_ltrace(struct Client *, int, const char **);
120 static void stats_ziplinks(struct Client *);
121 static void stats_comm(struct Client *);
122 /* This table contains the possible stats items, in order:
123 * stats letter, function to call, operonly? adminonly?
124 * case only matters in the stats letter column.. -- fl_
125 */
126 static struct StatsStruct stats_cmd_table[] = {
127 /* letter function need_oper need_admin */
128 {'a', stats_dns_servers, 1, 1, },
129 {'A', stats_dns_servers, 1, 1, },
130 {'b', stats_delay, 1, 1, },
131 {'B', stats_hash, 1, 1, },
132 {'c', stats_connect, 0, 0, },
133 {'C', stats_connect, 0, 0, },
134 {'d', stats_tdeny, 1, 0, },
135 {'D', stats_deny, 1, 0, },
136 {'e', stats_exempt, 1, 0, },
137 {'E', stats_events, 1, 1, },
138 {'f', stats_comm, 1, 1, },
139 {'F', stats_comm, 1, 1, },
140 {'h', stats_hubleaf, 0, 0, },
141 {'H', stats_hubleaf, 0, 0, },
142 {'i', stats_auth, 0, 0, },
143 {'I', stats_auth, 0, 0, },
144 {'k', stats_tklines, 0, 0, },
145 {'K', stats_klines, 0, 0, },
146 {'l', NULL /* special */, 0, 0, },
147 {'L', NULL /* special */, 0, 0, },
148 {'m', stats_messages, 0, 0, },
149 {'M', stats_messages, 0, 0, },
150 {'n', stats_dnsbl, 0, 0, },
151 {'o', stats_oper, 0, 0, },
152 {'O', stats_privset, 1, 0, },
153 {'p', stats_operedup, 0, 0, },
154 {'P', stats_ports, 0, 0, },
155 {'q', stats_tresv, 1, 0, },
156 {'Q', stats_resv, 1, 0, },
157 {'r', stats_usage, 1, 0, },
158 {'R', stats_usage, 1, 0, },
159 {'t', stats_tstats, 1, 0, },
160 {'T', stats_tstats, 1, 0, },
161 {'u', stats_uptime, 0, 0, },
162 {'U', stats_shared, 1, 0, },
163 {'v', stats_servers, 0, 0, },
164 {'V', stats_servers, 0, 0, },
165 {'x', stats_tgecos, 1, 0, },
166 {'X', stats_gecos, 1, 0, },
167 {'y', stats_class, 0, 0, },
168 {'Y', stats_class, 0, 0, },
169 {'z', stats_memory, 1, 0, },
170 {'Z', stats_ziplinks, 1, 0, },
171 {'?', stats_servlinks, 0, 0, },
172 {(char) 0, (void (*)()) 0, 0, 0, }
173 };
174
175 /*
176 * m_stats by fl_
177 * parv[1] = stat letter/command
178 * parv[2] = (if present) server/mask in stats L, or target
179 *
180 * This will search the tables for the appropriate stats letter,
181 * if found execute it.
182 */
183 static int
184 m_stats(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
185 {
186 static time_t last_used = 0;
187 int i;
188 char statchar;
189
190 statchar = parv[1][0];
191
192 if(MyClient(source_p) && !IsOper(source_p))
193 {
194 /* Check the user is actually allowed to do /stats, and isnt flooding */
195 if((last_used + ConfigFileEntry.pace_wait) > rb_current_time())
196 {
197 /* safe enough to give this on a local connect only */
198 sendto_one(source_p, form_str(RPL_LOAD2HI),
199 me.name, source_p->name, "STATS");
200 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
201 form_str(RPL_ENDOFSTATS), statchar);
202 return 0;
203 }
204 else
205 last_used = rb_current_time();
206 }
207
208 if(hunt_server (client_p, source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
209 return 0;
210
211 if((statchar != 'L') && (statchar != 'l'))
212 stats_spy(source_p, statchar, NULL);
213
214 for (i = 0; stats_cmd_table[i].letter; i++)
215 {
216 if(stats_cmd_table[i].letter == statchar)
217 {
218 /* The stats table says what privs are needed, so check --fl_ */
219 /* Called for remote clients and for local opers, so check need_admin
220 * and need_oper
221 */
222 if(stats_cmd_table[i].need_oper && !IsOper(source_p))
223 {
224 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
225 form_str (ERR_NOPRIVILEGES));
226 break;
227 }
228 if(stats_cmd_table[i].need_admin && !IsOperAdmin(source_p))
229 {
230 sendto_one(source_p, form_str(ERR_NOPRIVS),
231 me.name, source_p->name, "admin");
232 break;
233 }
234
235 /* Blah, stats L needs the parameters, none of the others do.. */
236 if(statchar == 'L' || statchar == 'l')
237 stats_ltrace (source_p, parc, parv);
238 else
239 stats_cmd_table[i].handler (source_p);
240 }
241 }
242
243 /* Send the end of stats notice, and the stats_spy */
244 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
245 form_str(RPL_ENDOFSTATS), statchar);
246
247 return 0;
248 }
249
250 static void
251 stats_dns_servers (struct Client *source_p)
252 {
253 report_dns_servers (source_p);
254 }
255
256 static void
257 stats_delay(struct Client *source_p)
258 {
259 struct nd_entry *nd;
260 struct DictionaryIter iter;
261
262 DICTIONARY_FOREACH(nd, &iter, nd_dict)
263 {
264 sendto_one_notice(source_p, ":Delaying: %s for %ld",
265 nd->name, (long) nd->expire);
266 }
267 }
268
269 static void
270 stats_hash(struct Client *source_p)
271 {
272 hash_stats(source_p);
273 }
274
275 static void
276 stats_connect(struct Client *source_p)
277 {
278 static char buf[5];
279 struct server_conf *server_p;
280 char *s;
281 rb_dlink_node *ptr;
282
283 if((ConfigFileEntry.stats_c_oper_only ||
284 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
285 !IsOper(source_p))
286 {
287 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
288 form_str(ERR_NOPRIVILEGES));
289 return;
290 }
291
292 RB_DLINK_FOREACH(ptr, server_conf_list.head)
293 {
294 server_p = ptr->data;
295
296 if(ServerConfIllegal(server_p))
297 continue;
298
299 buf[0] = '\0';
300 s = buf;
301
302 if(IsOper(source_p))
303 {
304 if(ServerConfAutoconn(server_p))
305 *s++ = 'A';
306 if(ServerConfSSL(server_p))
307 *s++ = 'S';
308 if(ServerConfTb(server_p))
309 *s++ = 'T';
310 if(ServerConfCompressed(server_p))
311 *s++ = 'Z';
312 }
313
314 if(!buf[0])
315 *s++ = '*';
316
317 *s = '\0';
318
319 sendto_one_numeric(source_p, RPL_STATSCLINE,
320 form_str(RPL_STATSCLINE),
321 "*@127.0.0.1",
322 buf, server_p->name,
323 server_p->port, server_p->class_name);
324 }
325 }
326
327 /* stats_tdeny()
328 *
329 * input - client to report to
330 * output - none
331 * side effects - client is given temp dline list.
332 */
333 static void
334 stats_tdeny (struct Client *source_p)
335 {
336 char *host, *pass, *user, *oper_reason;
337 struct AddressRec *arec;
338 struct ConfItem *aconf;
339 int i;
340
341 for (i = 0; i < ATABLE_SIZE; i++)
342 {
343 for (arec = atable[i]; arec; arec = arec->next)
344 {
345 if(arec->type == CONF_DLINE)
346 {
347 aconf = arec->aconf;
348
349 if(!(aconf->flags & CONF_FLAGS_TEMPORARY))
350 continue;
351
352 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
353
354 sendto_one_numeric(source_p, RPL_STATSDLINE,
355 form_str (RPL_STATSDLINE),
356 'd', host, pass,
357 oper_reason ? "|" : "",
358 oper_reason ? oper_reason : "");
359 }
360 }
361 }
362 }
363
364 /* stats_deny()
365 *
366 * input - client to report to
367 * output - none
368 * side effects - client is given dline list.
369 */
370 static void
371 stats_deny (struct Client *source_p)
372 {
373 char *host, *pass, *user, *oper_reason;
374 struct AddressRec *arec;
375 struct ConfItem *aconf;
376 int i;
377
378 for (i = 0; i < ATABLE_SIZE; i++)
379 {
380 for (arec = atable[i]; arec; arec = arec->next)
381 {
382 if(arec->type == CONF_DLINE)
383 {
384 aconf = arec->aconf;
385
386 if(aconf->flags & CONF_FLAGS_TEMPORARY)
387 continue;
388
389 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
390
391 sendto_one_numeric(source_p, RPL_STATSDLINE,
392 form_str (RPL_STATSDLINE),
393 'D', host, pass,
394 oper_reason ? "|" : "",
395 oper_reason ? oper_reason : "");
396 }
397 }
398 }
399 }
400
401
402 /* stats_exempt()
403 *
404 * input - client to report to
405 * output - none
406 * side effects - client is given list of exempt blocks
407 */
408 static void
409 stats_exempt(struct Client *source_p)
410 {
411 char *name, *host, *pass, *user, *classname;
412 struct AddressRec *arec;
413 struct ConfItem *aconf;
414 int i, port;
415
416 if(ConfigFileEntry.stats_e_disabled)
417 {
418 sendto_one_numeric(source_p, ERR_DISABLED,
419 form_str(ERR_DISABLED), "STATS e");
420 return;
421 }
422
423 for (i = 0; i < ATABLE_SIZE; i++)
424 {
425 for (arec = atable[i]; arec; arec = arec->next)
426 {
427 if(arec->type == CONF_EXEMPTDLINE)
428 {
429 aconf = arec->aconf;
430 get_printable_conf (aconf, &name, &host, &pass,
431 &user, &port, &classname);
432
433 sendto_one_numeric(source_p, RPL_STATSDLINE,
434 form_str(RPL_STATSDLINE),
435 'e', host, pass, "", "");
436 }
437 }
438 }}
439
440
441 static void
442 stats_events_cb(char *str, void *ptr)
443 {
444 sendto_one_numeric(ptr, RPL_STATSDEBUG, "E :%s", str);
445 }
446
447 static void
448 stats_events (struct Client *source_p)
449 {
450 rb_dump_events(stats_events_cb, source_p);
451 }
452
453 static void
454 stats_hubleaf(struct Client *source_p)
455 {
456 struct remote_conf *hub_p;
457 rb_dlink_node *ptr;
458
459 if((ConfigFileEntry.stats_h_oper_only ||
460 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
461 !IsOper(source_p))
462 {
463 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
464 form_str (ERR_NOPRIVILEGES));
465 return;
466 }
467
468 RB_DLINK_FOREACH(ptr, hubleaf_conf_list.head)
469 {
470 hub_p = ptr->data;
471
472 if(hub_p->flags & CONF_HUB)
473 sendto_one_numeric(source_p, RPL_STATSHLINE,
474 form_str(RPL_STATSHLINE),
475 hub_p->host, hub_p->server);
476 else
477 sendto_one_numeric(source_p, RPL_STATSLLINE,
478 form_str(RPL_STATSLLINE),
479 hub_p->host, hub_p->server);
480 }
481 }
482
483
484 static void
485 stats_auth (struct Client *source_p)
486 {
487 /* Oper only, if unopered, return ERR_NOPRIVS */
488 if((ConfigFileEntry.stats_i_oper_only == 2) && !IsOper (source_p))
489 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
490 form_str (ERR_NOPRIVILEGES));
491
492 /* If unopered, Only return matching auth blocks */
493 else if((ConfigFileEntry.stats_i_oper_only == 1) && !IsOper (source_p))
494 {
495 struct ConfItem *aconf;
496 char *name, *host, *pass = "*", *user, *classname;
497 int port;
498
499 if(MyConnect (source_p))
500 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
501 (struct sockaddr *)&source_p->localClient->ip,
502 CONF_CLIENT,
503 source_p->localClient->ip.ss_family,
504 source_p->username, NULL);
505 else
506 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_CLIENT,
507 0, source_p->username, NULL);
508
509 if(aconf == NULL)
510 return;
511
512 get_printable_conf (aconf, &name, &host, &pass, &user, &port, &classname);
513 if(!EmptyString(aconf->spasswd))
514 pass = aconf->spasswd;
515
516 sendto_one_numeric(source_p, RPL_STATSILINE, form_str(RPL_STATSILINE),
517 name, pass, show_iline_prefix(source_p, aconf, user),
518 host, port, classname);
519 }
520
521 /* Theyre opered, or allowed to see all auth blocks */
522 else
523 report_auth (source_p);
524 }
525
526
527 static void
528 stats_tklines(struct Client *source_p)
529 {
530 /* Oper only, if unopered, return ERR_NOPRIVS */
531 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
532 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
533 form_str (ERR_NOPRIVILEGES));
534
535 /* If unopered, Only return matching klines */
536 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
537 {
538 struct ConfItem *aconf;
539 char *host, *pass, *user, *oper_reason;
540
541 if(MyConnect (source_p))
542 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
543 (struct sockaddr *)&source_p->localClient->ip,
544 CONF_KILL,
545 source_p->localClient->ip.ss_family,
546 source_p->username, NULL);
547 else
548 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
549 0, source_p->username, NULL);
550
551 if(aconf == NULL)
552 return;
553
554 /* dont report a permanent kline as a tkline */
555 if((aconf->flags & CONF_FLAGS_TEMPORARY) == 0)
556 return;
557
558 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
559
560 sendto_one_numeric(source_p, RPL_STATSKLINE,
561 form_str(RPL_STATSKLINE), aconf->flags & CONF_FLAGS_TEMPORARY ? 'k' : 'K',
562 host, user, pass, oper_reason ? "|" : "",
563 oper_reason ? oper_reason : "");
564 }
565 /* Theyre opered, or allowed to see all klines */
566 else
567 {
568 struct ConfItem *aconf;
569 rb_dlink_node *ptr;
570 int i;
571 char *user, *host, *pass, *oper_reason;
572
573 for(i = 0; i < LAST_TEMP_TYPE; i++)
574 {
575 RB_DLINK_FOREACH(ptr, temp_klines[i].head)
576 {
577 aconf = ptr->data;
578
579 get_printable_kline(source_p, aconf, &host, &pass,
580 &user, &oper_reason);
581
582 sendto_one_numeric(source_p, RPL_STATSKLINE,
583 form_str(RPL_STATSKLINE),
584 'k', host, user, pass,
585 oper_reason ? "|" : "",
586 oper_reason ? oper_reason : "");
587 }
588 }
589 }
590 }
591
592 static void
593 stats_klines(struct Client *source_p)
594 {
595 /* Oper only, if unopered, return ERR_NOPRIVS */
596 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
597 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
598 form_str (ERR_NOPRIVILEGES));
599
600 /* If unopered, Only return matching klines */
601 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
602 {
603 struct ConfItem *aconf;
604 char *host, *pass, *user, *oper_reason;
605
606 /* search for a kline */
607 if(MyConnect (source_p))
608 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
609 (struct sockaddr *)&source_p->localClient->ip,
610 CONF_KILL,
611 source_p->localClient->ip.ss_family,
612 source_p->username, NULL);
613 else
614 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
615 0, source_p->username, NULL);
616
617 if(aconf == NULL)
618 return;
619
620 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
621
622 sendto_one_numeric(source_p, RPL_STATSKLINE, form_str(RPL_STATSKLINE),
623 aconf->flags & CONF_FLAGS_TEMPORARY ? 'k' : 'K',
624 host, user, pass, oper_reason ? "|" : "",
625 oper_reason ? oper_reason : "");
626 }
627 /* Theyre opered, or allowed to see all klines */
628 else
629 report_Klines (source_p);
630 }
631
632 static void
633 stats_messages(struct Client *source_p)
634 {
635 report_messages(source_p);
636 }
637
638 static void
639 stats_dnsbl(struct Client *source_p)
640 {
641 rb_dlink_node *ptr;
642 struct Blacklist *blptr;
643
644 RB_DLINK_FOREACH(ptr, blacklist_list.head)
645 {
646 blptr = ptr->data;
647
648 /* use RPL_STATSDEBUG for now -- jilles */
649 sendto_one_numeric(source_p, RPL_STATSDEBUG, "n :%d %s %s (%d)",
650 blptr->hits,
651 blptr->host,
652 blptr->status & CONF_ILLEGAL ? "disabled" : "active",
653 blptr->refcount);
654 }
655 }
656
657 static void
658 stats_oper(struct Client *source_p)
659 {
660 struct oper_conf *oper_p;
661 rb_dlink_node *ptr;
662
663 if(!IsOper(source_p) && ConfigFileEntry.stats_o_oper_only)
664 {
665 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
666 form_str (ERR_NOPRIVILEGES));
667 return;
668 }
669
670 RB_DLINK_FOREACH(ptr, oper_conf_list.head)
671 {
672 oper_p = ptr->data;
673
674 sendto_one_numeric(source_p, RPL_STATSOLINE,
675 form_str(RPL_STATSOLINE),
676 oper_p->username, oper_p->host, oper_p->name,
677 IsOper(source_p) ? oper_p->privset->name : "0", "-1");
678 }
679 }
680
681 static void
682 stats_privset(struct Client *source_p)
683 {
684 privilegeset_report(source_p);
685 }
686
687 /* stats_operedup()
688 *
689 * input - client pointer
690 * output - none
691 * side effects - client is shown a list of active opers
692 */
693 static void
694 stats_operedup (struct Client *source_p)
695 {
696 struct Client *target_p;
697 rb_dlink_node *oper_ptr;
698 unsigned int count = 0;
699
700 RB_DLINK_FOREACH (oper_ptr, oper_list.head)
701 {
702 target_p = oper_ptr->data;
703
704 if(IsOperInvis(target_p) && !IsOper(source_p))
705 continue;
706
707 if(target_p->user->away)
708 continue;
709
710 count++;
711
712 sendto_one_numeric(source_p, RPL_STATSDEBUG,
713 "p :%s (%s@%s)",
714 target_p->name, target_p->username,
715 target_p->host);
716 }
717
718 sendto_one_numeric(source_p, RPL_STATSDEBUG,
719 "p :%u staff members", count);
720
721 stats_p_spy (source_p);
722 }
723
724 static void
725 stats_ports (struct Client *source_p)
726 {
727 if(!IsOper (source_p) && ConfigFileEntry.stats_P_oper_only)
728 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
729 form_str (ERR_NOPRIVILEGES));
730 else
731 show_ports (source_p);
732 }
733
734 static void
735 stats_tresv(struct Client *source_p)
736 {
737 struct ConfItem *aconf;
738 rb_dlink_node *ptr;
739 int i;
740
741 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
742 {
743 aconf = ptr->data;
744 if(aconf->hold)
745 sendto_one_numeric(source_p, RPL_STATSQLINE,
746 form_str(RPL_STATSQLINE),
747 'q', aconf->port, aconf->host, aconf->passwd);
748 }
749
750 HASH_WALK(i, R_MAX, ptr, resvTable)
751 {
752 aconf = ptr->data;
753 if(aconf->hold)
754 sendto_one_numeric(source_p, RPL_STATSQLINE,
755 form_str(RPL_STATSQLINE),
756 'q', aconf->port, aconf->host, aconf->passwd);
757 }
758 HASH_WALK_END
759 }
760
761
762 static void
763 stats_resv(struct Client *source_p)
764 {
765 struct ConfItem *aconf;
766 rb_dlink_node *ptr;
767 int i;
768
769 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
770 {
771 aconf = ptr->data;
772 if(!aconf->hold)
773 sendto_one_numeric(source_p, RPL_STATSQLINE,
774 form_str(RPL_STATSQLINE),
775 'Q', aconf->port, aconf->host, aconf->passwd);
776 }
777
778 HASH_WALK(i, R_MAX, ptr, resvTable)
779 {
780 aconf = ptr->data;
781 if(!aconf->hold)
782 sendto_one_numeric(source_p, RPL_STATSQLINE,
783 form_str(RPL_STATSQLINE),
784 'Q', aconf->port, aconf->host, aconf->passwd);
785 }
786 HASH_WALK_END
787 }
788
789 static void
790 stats_usage (struct Client *source_p)
791 {
792 struct rusage rus;
793 time_t secs;
794 time_t rup;
795 #ifdef hz
796 # define hzz hz
797 #else
798 # ifdef HZ
799 # define hzz HZ
800 # else
801 int hzz = 1;
802 # endif
803 #endif
804
805 if(getrusage(RUSAGE_SELF, &rus) == -1)
806 {
807 sendto_one_notice(source_p, ":Getruseage error: %s.",
808 strerror(errno));
809 return;
810 }
811 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
812 if(0 == secs)
813 secs = 1;
814
815 rup = (rb_current_time() - startup_time) * hzz;
816 if(0 == rup)
817 rup = 1;
818
819 sendto_one_numeric(source_p, RPL_STATSDEBUG,
820 "R :CPU Secs %d:%d User %d:%d System %d:%d",
821 (int) (secs / 60), (int) (secs % 60),
822 (int) (rus.ru_utime.tv_sec / 60),
823 (int) (rus.ru_utime.tv_sec % 60),
824 (int) (rus.ru_stime.tv_sec / 60),
825 (int) (rus.ru_stime.tv_sec % 60));
826 sendto_one_numeric(source_p, RPL_STATSDEBUG,
827 "R :RSS %ld ShMem %ld Data %ld Stack %ld",
828 rus.ru_maxrss, (rus.ru_ixrss / rup),
829 (rus.ru_idrss / rup), (rus.ru_isrss / rup));
830 sendto_one_numeric(source_p, RPL_STATSDEBUG,
831 "R :Swaps %d Reclaims %d Faults %d",
832 (int) rus.ru_nswap, (int) rus.ru_minflt, (int) rus.ru_majflt);
833 sendto_one_numeric(source_p, RPL_STATSDEBUG,
834 "R :Block in %d out %d",
835 (int) rus.ru_inblock, (int) rus.ru_oublock);
836 sendto_one_numeric(source_p, RPL_STATSDEBUG,
837 "R :Msg Rcv %d Send %d",
838 (int) rus.ru_msgrcv, (int) rus.ru_msgsnd);
839 sendto_one_numeric(source_p, RPL_STATSDEBUG,
840 "R :Signals %d Context Vol. %d Invol %d",
841 (int) rus.ru_nsignals, (int) rus.ru_nvcsw,
842 (int) rus.ru_nivcsw);
843 }
844
845 static void
846 stats_tstats (struct Client *source_p)
847 {
848 struct Client *target_p;
849 struct ServerStatistics sp;
850 rb_dlink_node *ptr;
851
852 memcpy(&sp, &ServerStats, sizeof(struct ServerStatistics));
853
854 RB_DLINK_FOREACH(ptr, serv_list.head)
855 {
856 target_p = ptr->data;
857
858 sp.is_sbs += target_p->localClient->sendB;
859 sp.is_sbr += target_p->localClient->receiveB;
860 sp.is_sti += (unsigned long long)(rb_current_time() - target_p->localClient->firsttime);
861 sp.is_sv++;
862 }
863
864 RB_DLINK_FOREACH(ptr, lclient_list.head)
865 {
866 target_p = ptr->data;
867
868 sp.is_cbs += target_p->localClient->sendB;
869 sp.is_cbr += target_p->localClient->receiveB;
870 sp.is_cti += (unsigned long long)(rb_current_time() - target_p->localClient->firsttime);
871 sp.is_cl++;
872 }
873
874 RB_DLINK_FOREACH(ptr, unknown_list.head)
875 {
876 sp.is_ni++;
877 }
878
879 sendto_one_numeric(source_p, RPL_STATSDEBUG,
880 "T :accepts %u refused %u", sp.is_ac, sp.is_ref);
881 sendto_one_numeric(source_p, RPL_STATSDEBUG,
882 "T :rejected %u delaying %lu",
883 sp.is_rej, delay_exit_length());
884 sendto_one_numeric(source_p, RPL_STATSDEBUG,
885 "T :throttled refused %u throttle list size %lu", sp.is_thr, throttle_size());
886 sendto_one_numeric(source_p, RPL_STATSDEBUG,
887 "T :nicks being delayed %lu",
888 get_nd_count());
889 sendto_one_numeric(source_p, RPL_STATSDEBUG,
890 "T :unknown commands %u prefixes %u",
891 sp.is_unco, sp.is_unpf);
892 sendto_one_numeric(source_p, RPL_STATSDEBUG,
893 "T :nick collisions %u saves %u unknown closes %u",
894 sp.is_kill, sp.is_save, sp.is_ni);
895 sendto_one_numeric(source_p, RPL_STATSDEBUG,
896 "T :wrong direction %u empty %u",
897 sp.is_wrdi, sp.is_empt);
898 sendto_one_numeric(source_p, RPL_STATSDEBUG,
899 "T :numerics seen %u", sp.is_num);
900 sendto_one_numeric(source_p, RPL_STATSDEBUG,
901 "T :tgchange blocked msgs %u restricted addrs %lu",
902 sp.is_tgch, rb_dlink_list_length(&tgchange_list));
903 sendto_one_numeric(source_p, RPL_STATSDEBUG,
904 "T :auth successes %u fails %u",
905 sp.is_asuc, sp.is_abad);
906 sendto_one_numeric(source_p, RPL_STATSDEBUG,
907 "T :sasl successes %u fails %u",
908 sp.is_ssuc, sp.is_sbad);
909 sendto_one_numeric(source_p, RPL_STATSDEBUG, "T :Client Server");
910 sendto_one_numeric(source_p, RPL_STATSDEBUG,
911 "T :connected %u %u", sp.is_cl, sp.is_sv);
912 sendto_one_numeric(source_p, RPL_STATSDEBUG,
913 "T :bytes sent %lluK %lluK",
914 sp.is_cbs / 1024,
915 sp.is_sbs / 1024);
916 sendto_one_numeric(source_p, RPL_STATSDEBUG,
917 "T :bytes recv %lluK %lluK",
918 sp.is_cbr / 1024,
919 sp.is_sbr / 1024);
920 sendto_one_numeric(source_p, RPL_STATSDEBUG,
921 "T :time connected %llu %llu",
922 sp.is_cti, sp.is_sti);
923 }
924
925 static void
926 stats_uptime (struct Client *source_p)
927 {
928 time_t now;
929
930 now = rb_current_time() - startup_time;
931 sendto_one_numeric(source_p, RPL_STATSUPTIME,
932 form_str (RPL_STATSUPTIME),
933 now / 86400, (now / 3600) % 24,
934 (now / 60) % 60, now % 60);
935 sendto_one_numeric(source_p, RPL_STATSCONN,
936 form_str (RPL_STATSCONN),
937 MaxConnectionCount, MaxClientCount,
938 Count.totalrestartcount);
939 }
940
941 struct shared_flags
942 {
943 int flag;
944 char letter;
945 };
946 static struct shared_flags shared_flagtable[] =
947 {
948 { SHARED_PKLINE, 'K' },
949 { SHARED_TKLINE, 'k' },
950 { SHARED_UNKLINE, 'U' },
951 { SHARED_PXLINE, 'X' },
952 { SHARED_TXLINE, 'x' },
953 { SHARED_UNXLINE, 'Y' },
954 { SHARED_PRESV, 'Q' },
955 { SHARED_TRESV, 'q' },
956 { SHARED_UNRESV, 'R' },
957 { SHARED_LOCOPS, 'L' },
958 { SHARED_REHASH, 'H' },
959 { SHARED_TDLINE, 'd' },
960 { SHARED_PDLINE, 'D' },
961 { SHARED_UNDLINE, 'E' },
962 { SHARED_DIE, 'I' },
963 { SHARED_MODULE, 'M' },
964 { 0, '\0'}
965 };
966
967
968 static void
969 stats_shared (struct Client *source_p)
970 {
971 struct remote_conf *shared_p;
972 rb_dlink_node *ptr;
973 char buf[sizeof(shared_flagtable)/sizeof(shared_flagtable[0])];
974 char *p;
975 int i;
976
977 RB_DLINK_FOREACH(ptr, shared_conf_list.head)
978 {
979 shared_p = ptr->data;
980
981 p = buf;
982
983 *p++ = 'c';
984
985 for(i = 0; shared_flagtable[i].flag != 0; i++)
986 {
987 if(shared_p->flags & shared_flagtable[i].flag)
988 *p++ = shared_flagtable[i].letter;
989 }
990
991 *p = '\0';
992
993 sendto_one_numeric(source_p, RPL_STATSULINE,
994 form_str(RPL_STATSULINE),
995 shared_p->server, shared_p->username,
996 shared_p->host, buf);
997 }
998
999 RB_DLINK_FOREACH(ptr, cluster_conf_list.head)
1000 {
1001 shared_p = ptr->data;
1002
1003 p = buf;
1004
1005 *p++ = 'C';
1006
1007 for(i = 0; shared_flagtable[i].flag != 0; i++)
1008 {
1009 if(shared_p->flags & shared_flagtable[i].flag)
1010 *p++ = shared_flagtable[i].letter;
1011 }
1012
1013 *p = '\0';
1014
1015 sendto_one_numeric(source_p, RPL_STATSULINE,
1016 form_str(RPL_STATSULINE),
1017 shared_p->server, "*", "*", buf);
1018 }
1019 }
1020
1021 /* stats_servers()
1022 *
1023 * input - client pointer
1024 * output - none
1025 * side effects - client is shown lists of who connected servers
1026 */
1027 static void
1028 stats_servers (struct Client *source_p)
1029 {
1030 struct Client *target_p;
1031 rb_dlink_node *ptr;
1032 time_t seconds;
1033 int days, hours, minutes;
1034 int j = 0;
1035
1036 if(ConfigServerHide.flatten_links && !IsOper(source_p) &&
1037 !IsExemptShide(source_p))
1038 {
1039 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1040 form_str (ERR_NOPRIVILEGES));
1041 return;
1042 }
1043
1044 RB_DLINK_FOREACH (ptr, serv_list.head)
1045 {
1046 target_p = ptr->data;
1047
1048 j++;
1049 seconds = rb_current_time() - target_p->localClient->firsttime;
1050
1051 days = (int) (seconds / 86400);
1052 seconds %= 86400;
1053 hours = (int) (seconds / 3600);
1054 seconds %= 3600;
1055 minutes = (int) (seconds / 60);
1056 seconds %= 60;
1057
1058 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1059 "V :%s (%s!*@*) Idle: %d SendQ: %d "
1060 "Connected: %d day%s, %d:%02d:%02d",
1061 target_p->name,
1062 (target_p->serv->by[0] ? target_p->serv->by : "Remote."),
1063 (int) (rb_current_time() - target_p->localClient->lasttime),
1064 (int) rb_linebuf_len (&target_p->localClient->buf_sendq),
1065 days, (days == 1) ? "" : "s", hours, minutes,
1066 (int) seconds);
1067 }
1068
1069 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1070 "V :%d Server(s)", j);
1071 }
1072
1073 static void
1074 stats_tgecos(struct Client *source_p)
1075 {
1076 struct ConfItem *aconf;
1077 rb_dlink_node *ptr;
1078
1079 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
1080 {
1081 aconf = ptr->data;
1082
1083 if(aconf->hold)
1084 sendto_one_numeric(source_p, RPL_STATSXLINE,
1085 form_str(RPL_STATSXLINE),
1086 'x', aconf->port, aconf->host,
1087 aconf->passwd);
1088 }
1089 }
1090
1091 static void
1092 stats_gecos(struct Client *source_p)
1093 {
1094 struct ConfItem *aconf;
1095 rb_dlink_node *ptr;
1096
1097 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
1098 {
1099 aconf = ptr->data;
1100
1101 if(!aconf->hold)
1102 sendto_one_numeric(source_p, RPL_STATSXLINE,
1103 form_str(RPL_STATSXLINE),
1104 'X', aconf->port, aconf->host,
1105 aconf->passwd);
1106 }
1107 }
1108
1109 static void
1110 stats_class(struct Client *source_p)
1111 {
1112 if(ConfigFileEntry.stats_y_oper_only && !IsOper(source_p))
1113 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1114 form_str (ERR_NOPRIVILEGES));
1115 else
1116 report_classes(source_p);
1117 }
1118
1119 static void
1120 stats_memory (struct Client *source_p)
1121 {
1122 struct Client *target_p;
1123 struct Channel *chptr;
1124 struct Ban *actualBan;
1125 rb_dlink_node *rb_dlink;
1126 rb_dlink_node *ptr;
1127 int channel_count = 0;
1128 int local_client_conf_count = 0; /* local client conf links */
1129 int users_counted = 0; /* user structs */
1130
1131 int channel_users = 0;
1132 int channel_invites = 0;
1133 int channel_bans = 0;
1134 int channel_except = 0;
1135 int channel_invex = 0;
1136 int channel_quiets = 0;
1137
1138 int class_count = 0; /* classes */
1139 int conf_count = 0; /* conf lines */
1140 int users_invited_count = 0; /* users invited */
1141 int user_channels = 0; /* users in channels */
1142 int aways_counted = 0;
1143 size_t number_servers_cached; /* number of servers cached by scache */
1144
1145 size_t channel_memory = 0;
1146 size_t channel_ban_memory = 0;
1147 size_t channel_except_memory = 0;
1148 size_t channel_invex_memory = 0;
1149 size_t channel_quiet_memory = 0;
1150
1151 size_t away_memory = 0; /* memory used by aways */
1152 size_t ww = 0; /* whowas array count */
1153 size_t wwm = 0; /* whowas array memory used */
1154 size_t conf_memory = 0; /* memory used by conf lines */
1155 size_t mem_servers_cached; /* memory used by scache */
1156
1157 size_t linebuf_count = 0;
1158 size_t linebuf_memory_used = 0;
1159
1160 size_t total_channel_memory = 0;
1161 size_t totww = 0;
1162
1163 size_t local_client_count = 0;
1164 size_t local_client_memory_used = 0;
1165
1166 size_t remote_client_count = 0;
1167 size_t remote_client_memory_used = 0;
1168
1169 size_t total_memory = 0;
1170
1171 count_whowas_memory(&ww, &wwm);
1172
1173 RB_DLINK_FOREACH(ptr, global_client_list.head)
1174 {
1175 target_p = ptr->data;
1176 if(MyConnect(target_p))
1177 {
1178 local_client_conf_count++;
1179 }
1180
1181 if(target_p->user)
1182 {
1183 users_counted++;
1184 users_invited_count += rb_dlink_list_length(&target_p->user->invited);
1185 user_channels += rb_dlink_list_length(&target_p->user->channel);
1186 if(target_p->user->away)
1187 {
1188 aways_counted++;
1189 away_memory += (strlen(target_p->user->away) + 1);
1190 }
1191 }
1192 }
1193
1194 /* Count up all channels, ban lists, except lists, Invex lists */
1195 RB_DLINK_FOREACH(ptr, global_channel_list.head)
1196 {
1197 chptr = ptr->data;
1198 channel_count++;
1199 channel_memory += (strlen(chptr->chname) + sizeof(struct Channel));
1200
1201 channel_users += rb_dlink_list_length(&chptr->members);
1202 channel_invites += rb_dlink_list_length(&chptr->invites);
1203
1204 RB_DLINK_FOREACH(rb_dlink, chptr->banlist.head)
1205 {
1206 actualBan = rb_dlink->data;
1207 channel_bans++;
1208
1209 channel_ban_memory += sizeof(rb_dlink_node) + sizeof(struct Ban);
1210 }
1211
1212 RB_DLINK_FOREACH(rb_dlink, chptr->exceptlist.head)
1213 {
1214 actualBan = rb_dlink->data;
1215 channel_except++;
1216
1217 channel_except_memory += (sizeof(rb_dlink_node) + sizeof(struct Ban));
1218 }
1219
1220 RB_DLINK_FOREACH(rb_dlink, chptr->invexlist.head)
1221 {
1222 actualBan = rb_dlink->data;
1223 channel_invex++;
1224
1225 channel_invex_memory += (sizeof(rb_dlink_node) + sizeof(struct Ban));
1226 }
1227
1228 RB_DLINK_FOREACH(rb_dlink, chptr->quietlist.head)
1229 {
1230 actualBan = rb_dlink->data;
1231 channel_quiets++;
1232
1233 channel_quiet_memory += (sizeof(rb_dlink_node) + sizeof(struct Ban));
1234 }
1235 }
1236
1237 /* count up all classes */
1238
1239 class_count = rb_dlink_list_length(&class_list) + 1;
1240
1241 rb_count_rb_linebuf_memory(&linebuf_count, &linebuf_memory_used);
1242
1243 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1244 "z :Users %u(%lu) Invites %u(%lu)",
1245 users_counted,
1246 (unsigned long) users_counted * sizeof(struct User),
1247 users_invited_count,
1248 (unsigned long) users_invited_count * sizeof(rb_dlink_node));
1249
1250 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1251 "z :User channels %u(%lu) Aways %u(%d)",
1252 user_channels,
1253 (unsigned long) user_channels * sizeof(rb_dlink_node),
1254 aways_counted, (int) away_memory);
1255
1256 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1257 "z :Attached confs %u(%lu)",
1258 local_client_conf_count,
1259 (unsigned long) local_client_conf_count * sizeof(rb_dlink_node));
1260
1261 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1262 "z :Conflines %u(%d)", conf_count, (int) conf_memory);
1263
1264 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1265 "z :Classes %u(%lu)",
1266 class_count,
1267 (unsigned long) class_count * sizeof(struct Class));
1268
1269 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1270 "z :Channels %u(%d)",
1271 channel_count, (int) channel_memory);
1272
1273 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1274 "z :Bans %u(%d) Exceptions %u(%d) Invex %u(%d) Quiets %u(%d)",
1275 channel_bans, (int) channel_ban_memory,
1276 channel_except, (int) channel_except_memory,
1277 channel_invex, (int) channel_invex_memory,
1278 channel_quiets, (int) channel_quiet_memory);
1279
1280 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1281 "z :Channel members %u(%lu) invite %u(%lu)",
1282 channel_users,
1283 (unsigned long) channel_users * sizeof(rb_dlink_node),
1284 channel_invites,
1285 (unsigned long) channel_invites * sizeof(rb_dlink_node));
1286
1287 total_channel_memory = channel_memory +
1288 channel_ban_memory +
1289 channel_users * sizeof(rb_dlink_node) + channel_invites * sizeof(rb_dlink_node);
1290
1291 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1292 "z :Whowas array %ld(%ld)",
1293 (long)ww, (long)wwm);
1294
1295 totww = wwm;
1296
1297 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1298 "z :Hash: client %u(%ld) chan %u(%ld)",
1299 U_MAX, (long)(U_MAX * sizeof(rb_dlink_list)),
1300 CH_MAX, (long)(CH_MAX * sizeof(rb_dlink_list)));
1301
1302 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1303 "z :linebuf %ld(%ld)",
1304 (long)linebuf_count, (long)linebuf_memory_used);
1305
1306 count_scache(&number_servers_cached, &mem_servers_cached);
1307
1308 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1309 "z :scache %ld(%ld)",
1310 (long)number_servers_cached, (long)mem_servers_cached);
1311
1312 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1313 "z :hostname hash %d(%ld)",
1314 HOST_MAX, (long)HOST_MAX * sizeof(rb_dlink_list));
1315
1316 total_memory = totww + total_channel_memory + conf_memory +
1317 class_count * sizeof(struct Class);
1318
1319 total_memory += mem_servers_cached;
1320 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1321 "z :Total: whowas %d channel %d conf %d",
1322 (int) totww, (int) total_channel_memory,
1323 (int) conf_memory);
1324
1325 count_local_client_memory(&local_client_count, &local_client_memory_used);
1326 total_memory += local_client_memory_used;
1327
1328 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1329 "z :Local client Memory in use: %ld(%ld)",
1330 (long)local_client_count, (long)local_client_memory_used);
1331
1332
1333 count_remote_client_memory(&remote_client_count, &remote_client_memory_used);
1334 total_memory += remote_client_memory_used;
1335
1336 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1337 "z :Remote client Memory in use: %ld(%ld)",
1338 (long)remote_client_count,
1339 (long)remote_client_memory_used);
1340 }
1341
1342 static void
1343 stats_ziplinks (struct Client *source_p)
1344 {
1345 rb_dlink_node *ptr;
1346 struct Client *target_p;
1347 struct ZipStats *zipstats;
1348 int sent_data = 0;
1349 char buf[128], buf1[128];
1350 RB_DLINK_FOREACH (ptr, serv_list.head)
1351 {
1352 target_p = ptr->data;
1353 if(IsCapable (target_p, CAP_ZIP))
1354 {
1355 zipstats = target_p->localClient->zipstats;
1356 sprintf(buf, "%.2f%%", zipstats->out_ratio);
1357 sprintf(buf1, "%.2f%%", zipstats->in_ratio);
1358 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1359 "Z :ZipLinks stats for %s send[%s compression "
1360 "(%llu kB data/%llu kB wire)] recv[%s compression "
1361 "(%llu kB data/%llu kB wire)]",
1362 target_p->name,
1363 buf, zipstats->out >> 10,
1364 zipstats->out_wire >> 10, buf1,
1365 zipstats->in >> 10, zipstats->in_wire >> 10);
1366 sent_data++;
1367 }
1368 }
1369
1370 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1371 "Z :%u ziplink(s)", sent_data);
1372 }
1373
1374 static void
1375 stats_servlinks (struct Client *source_p)
1376 {
1377 static char Sformat[] = ":%s %d %s %s %u %u %u %u %u :%u %u %s";
1378 long uptime, sendK, receiveK;
1379 struct Client *target_p;
1380 rb_dlink_node *ptr;
1381 int j = 0;
1382 char buf[128];
1383
1384 if(ConfigServerHide.flatten_links && !IsOper (source_p) &&
1385 !IsExemptShide(source_p))
1386 {
1387 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1388 form_str (ERR_NOPRIVILEGES));
1389 return;
1390 }
1391
1392 sendK = receiveK = 0;
1393
1394 RB_DLINK_FOREACH (ptr, serv_list.head)
1395 {
1396 target_p = ptr->data;
1397
1398 j++;
1399 sendK += target_p->localClient->sendK;
1400 receiveK += target_p->localClient->receiveK;
1401
1402 sendto_one(source_p, Sformat,
1403 get_id(&me, source_p), RPL_STATSLINKINFO, get_id(source_p, source_p),
1404 target_p->name,
1405 (int) rb_linebuf_len (&target_p->localClient->buf_sendq),
1406 (int) target_p->localClient->sendM,
1407 (int) target_p->localClient->sendK,
1408 (int) target_p->localClient->receiveM,
1409 (int) target_p->localClient->receiveK,
1410 rb_current_time() - target_p->localClient->firsttime,
1411 (rb_current_time() > target_p->localClient->lasttime) ?
1412 (rb_current_time() - target_p->localClient->lasttime) : 0,
1413 IsOper (source_p) ? show_capabilities (target_p) : "TS");
1414 }
1415
1416 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1417 "? :%u total server(s)", j);
1418
1419 snprintf(buf, sizeof buf, "%7.2f", _GMKv ((sendK)));
1420 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1421 "? :Sent total : %s %s",
1422 buf, _GMKs (sendK));
1423 snprintf(buf, sizeof buf, "%7.2f", _GMKv ((receiveK)));
1424 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1425 "? :Recv total : %s %s",
1426 buf, _GMKs (receiveK));
1427
1428 uptime = (rb_current_time() - startup_time);
1429 snprintf(buf, sizeof buf, "%7.2f %s (%4.1f K/s)",
1430 _GMKv (me.localClient->sendK),
1431 _GMKs (me.localClient->sendK),
1432 (float) ((float) me.localClient->sendK / (float) uptime));
1433 sendto_one_numeric(source_p, RPL_STATSDEBUG, "? :Server send: %s", buf);
1434 snprintf(buf, sizeof buf, "%7.2f %s (%4.1f K/s)",
1435 _GMKv (me.localClient->receiveK),
1436 _GMKs (me.localClient->receiveK),
1437 (float) ((float) me.localClient->receiveK / (float) uptime));
1438 sendto_one_numeric(source_p, RPL_STATSDEBUG, "? :Server recv: %s", buf);
1439 }
1440
1441 static void
1442 stats_ltrace(struct Client *source_p, int parc, const char *parv[])
1443 {
1444 int doall = 0;
1445 int wilds = 0;
1446 const char *name;
1447 char statchar = parv[1][0];
1448
1449 /* this is def targeted at us somehow.. */
1450 if(parc > 2 && !EmptyString(parv[2]))
1451 {
1452 /* directed at us generically? */
1453 if(match(parv[2], me.name) ||
1454 (!MyClient(source_p) && !irccmp(parv[2], me.id)))
1455 {
1456 name = me.name;
1457 doall = 1;
1458 }
1459 else
1460 {
1461 name = parv[2];
1462 wilds = strchr(name, '*') || strchr(name, '?');
1463 }
1464
1465 /* must be directed at a specific person thats not us */
1466 if(!doall && !wilds)
1467 {
1468 struct Client *target_p;
1469
1470 if(MyClient(source_p))
1471 target_p = find_named_person(name);
1472 else
1473 target_p = find_person(name);
1474
1475 if(target_p != NULL)
1476 {
1477 stats_spy(source_p, statchar, target_p->name);
1478 stats_l_client(source_p, target_p, statchar);
1479 }
1480 else
1481 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
1482 form_str(ERR_NOSUCHSERVER),
1483 name);
1484
1485 return;
1486 }
1487 }
1488 else
1489 {
1490 name = me.name;
1491 doall = 1;
1492 }
1493
1494 stats_spy(source_p, statchar, name);
1495
1496 if(doall)
1497 {
1498 /* local opers get everyone */
1499 if(MyOper(source_p))
1500 {
1501 stats_l_list(source_p, name, doall, wilds, &unknown_list, statchar);
1502 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1503 }
1504 else
1505 {
1506 /* they still need themselves if theyre local.. */
1507 if(MyClient(source_p))
1508 stats_l_client(source_p, source_p, statchar);
1509
1510 stats_l_list(source_p, name, doall, wilds, &local_oper_list, statchar);
1511 }
1512
1513 if (!ConfigServerHide.flatten_links || IsOper(source_p) ||
1514 IsExemptShide(source_p))
1515 stats_l_list(source_p, name, doall, wilds, &serv_list, statchar);
1516
1517 return;
1518 }
1519
1520 /* ok, at this point theyre looking for a specific client whos on
1521 * our server.. but it contains a wildcard. --fl
1522 */
1523 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1524
1525 return;
1526 }
1527
1528
1529 static void
1530 stats_l_list(struct Client *source_p, const char *name, int doall, int wilds,
1531 rb_dlink_list * list, char statchar)
1532 {
1533 rb_dlink_node *ptr;
1534 struct Client *target_p;
1535
1536 /* send information about connections which match. note, we
1537 * dont need tests for IsInvisible(), because non-opers will
1538 * never get here for normal clients --fl
1539 */
1540 RB_DLINK_FOREACH(ptr, list->head)
1541 {
1542 target_p = ptr->data;
1543
1544 if(!doall && wilds && !match(name, target_p->name))
1545 continue;
1546
1547 stats_l_client(source_p, target_p, statchar);
1548 }
1549 }
1550
1551 void
1552 stats_l_client(struct Client *source_p, struct Client *target_p,
1553 char statchar)
1554 {
1555 if(IsAnyServer(target_p))
1556 {
1557 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1558 target_p->name,
1559 (int) rb_linebuf_len(&target_p->localClient->buf_sendq),
1560 (int) target_p->localClient->sendM,
1561 (int) target_p->localClient->sendK,
1562 (int) target_p->localClient->receiveM,
1563 (int) target_p->localClient->receiveK,
1564 rb_current_time() - target_p->localClient->firsttime,
1565 (rb_current_time() > target_p->localClient->lasttime) ?
1566 (rb_current_time() - target_p->localClient->lasttime) : 0,
1567 IsOper(source_p) ? show_capabilities(target_p) : "-");
1568 }
1569
1570 else
1571 {
1572 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1573 show_ip(source_p, target_p) ?
1574 (IsUpper(statchar) ?
1575 get_client_name(target_p, SHOW_IP) :
1576 get_client_name(target_p, HIDE_IP)) :
1577 get_client_name(target_p, MASK_IP),
1578 (int) rb_linebuf_len(&target_p->localClient->buf_sendq),
1579 (int) target_p->localClient->sendM,
1580 (int) target_p->localClient->sendK,
1581 (int) target_p->localClient->receiveM,
1582 (int) target_p->localClient->receiveK,
1583 rb_current_time() - target_p->localClient->firsttime,
1584 (rb_current_time() > target_p->localClient->lasttime) ?
1585 (rb_current_time() - target_p->localClient->lasttime) : 0,
1586 "-");
1587 }
1588 }
1589
1590 static void
1591 rb_dump_fd_callback(int fd, const char *desc, void *data)
1592 {
1593 struct Client *source_p = data;
1594 sendto_one_numeric(source_p, RPL_STATSDEBUG, "F :fd %-3d desc '%s'", fd, desc);
1595 }
1596
1597 static void
1598 stats_comm(struct Client *source_p)
1599 {
1600 rb_dump_fd(rb_dump_fd_callback, source_p);
1601 }
1602
1603 /*
1604 * stats_spy
1605 *
1606 * inputs - pointer to client doing the /stats
1607 * - char letter they are doing /stats on
1608 * output - none
1609 * side effects -
1610 * This little helper function reports to opers if configured.
1611 * personally, I don't see why opers need to see stats requests
1612 * at all. They are just "noise" to an oper, and users can't do
1613 * any damage with stats requests now anyway. So, why show them?
1614 * -Dianora
1615 */
1616 static void
1617 stats_spy(struct Client *source_p, char statchar, const char *name)
1618 {
1619 hook_data_int data;
1620
1621 data.client = source_p;
1622 data.arg1 = name;
1623 data.arg2 = (int) statchar;
1624
1625 call_hook(doing_stats_hook, &data);
1626 }
1627
1628 /* stats_p_spy()
1629 *
1630 * input - pointer to client doing stats
1631 * ouput -
1632 * side effects - call hook doing_stats_p
1633 */
1634 static void
1635 stats_p_spy (struct Client *source_p)
1636 {
1637 hook_data data;
1638
1639 data.client = source_p;
1640 data.arg1 = data.arg2 = NULL;
1641
1642 call_hook(doing_stats_p_hook, &data);
1643 }
1644