Import from http://svn.freenode.net/ircd-seven/private/beu/seven (r196).
[seven-1.x.git] / modules / m_stats.c
blob792f6dc02989532668b27dfe47bf0bed3248fa46
1 /*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * m_stats.c: Sends the user statistics or config information.
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
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.
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.
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
24 * $Id: m_stats.c 147 2006-11-13 12:35:45Z spb $
27 #include "stdinc.h"
28 #include "tools.h" /* dlink_node/dlink_list */
29 #include "class.h" /* report_classes */
30 #include "client.h" /* Client */
31 #include "common.h" /* TRUE/FALSE */
32 #include "irc_string.h"
33 #include "ircd.h" /* me */
34 #include "listener.h" /* show_ports */
35 #include "msg.h" /* Message */
36 #include "hostmask.h" /* report_mtrie_conf_links */
37 #include "numeric.h" /* ERR_xxx */
38 #include "scache.h" /* list_scache */
39 #include "send.h" /* sendto_one */
40 #include "commio.h" /* highest_fd */
41 #include "s_conf.h" /* ConfItem */
42 #include "s_serv.h" /* hunt_server */
43 #include "s_stats.h" /* tstats */
44 #include "s_user.h" /* show_opers */
45 #include "event.h" /* events */
46 #include "blacklist.h" /* dnsbl stuff */
47 #include "linebuf.h"
48 #include "parse.h"
49 #include "modules.h"
50 #include "hook.h"
51 #include "s_newconf.h"
52 #include "hash.h"
54 static int m_stats (struct Client *, struct Client *, int, const char **);
56 struct Message stats_msgtab = {
57 "STATS", 0, 0, 0, MFLG_SLOW,
58 {mg_unreg, {m_stats, 2}, {m_stats, 3}, mg_ignore, mg_ignore, {m_stats, 2}}
61 int doing_stats_hook;
62 int doing_stats_p_hook;
64 mapi_clist_av1 stats_clist[] = { &stats_msgtab, NULL };
65 mapi_hlist_av1 stats_hlist[] = {
66 { "doing_stats", &doing_stats_hook },
67 { "doing_stats_p", &doing_stats_p_hook },
68 { NULL, NULL }
71 DECLARE_MODULE_AV1(stats, NULL, NULL, stats_clist, stats_hlist, NULL, "$Revision: 147 $");
73 const char *Lformat = "%s %u %u %u %u %u :%u %u %s";
75 static void stats_l_list(struct Client *s, const char *, int, int, dlink_list *, char);
76 static void stats_l_client(struct Client *source_p, struct Client *target_p,
77 char statchar);
79 static void stats_spy(struct Client *, char, const char *);
80 static void stats_p_spy(struct Client *);
82 /* Heres our struct for the stats table */
83 struct StatsStruct
85 char letter;
86 void (*handler) ();
87 int need_oper;
88 int need_admin;
91 static void stats_dns_servers (struct Client *);
92 static void stats_delay(struct Client *);
93 static void stats_hash(struct Client *);
94 static void stats_connect(struct Client *);
95 static void stats_tdeny(struct Client *);
96 static void stats_deny(struct Client *);
97 static void stats_exempt(struct Client *);
98 static void stats_events(struct Client *);
99 static void stats_hubleaf(struct Client *);
100 static void stats_auth(struct Client *);
101 static void stats_tklines(struct Client *);
102 static void stats_klines(struct Client *);
103 static void stats_messages(struct Client *);
104 static void stats_dnsbl(struct Client *);
105 static void stats_oper(struct Client *);
106 static void stats_operedup(struct Client *);
107 static void stats_ports(struct Client *);
108 static void stats_tresv(struct Client *);
109 static void stats_resv(struct Client *);
110 static void stats_usage(struct Client *);
111 static void stats_tstats(struct Client *);
112 static void stats_uptime(struct Client *);
113 static void stats_shared(struct Client *);
114 static void stats_servers(struct Client *);
115 static void stats_tgecos(struct Client *);
116 static void stats_gecos(struct Client *);
117 static void stats_class(struct Client *);
118 static void stats_memory(struct Client *);
119 static void stats_servlinks(struct Client *);
120 static void stats_ltrace(struct Client *, int, const char **);
121 static void stats_ziplinks(struct Client *);
123 /* This table contains the possible stats items, in order:
124 * stats letter, function to call, operonly? adminonly?
125 * case only matters in the stats letter column.. -- fl_
127 static struct StatsStruct stats_cmd_table[] = {
128 /* letter function need_oper need_admin */
129 {'a', stats_dns_servers, 1, 1, },
130 {'A', stats_dns_servers, 1, 1, },
131 {'b', stats_delay, 1, 1, },
132 {'B', stats_hash, 1, 1, },
133 {'c', stats_connect, 0, 0, },
134 {'C', stats_connect, 0, 0, },
135 {'d', stats_tdeny, 1, 0, },
136 {'D', stats_deny, 1, 0, },
137 {'e', stats_exempt, 1, 0, },
138 {'E', stats_events, 1, 1, },
139 {'f', comm_dump, 1, 1, },
140 {'F', comm_dump, 1, 1, },
141 {'h', stats_hubleaf, 0, 0, },
142 {'H', stats_hubleaf, 0, 0, },
143 {'i', stats_auth, 0, 0, },
144 {'I', stats_auth, 0, 0, },
145 {'k', stats_tklines, 0, 0, },
146 {'K', stats_klines, 0, 0, },
147 {'l', stats_ltrace, 0, 0, },
148 {'L', stats_ltrace, 0, 0, },
149 {'m', stats_messages, 0, 0, },
150 {'M', stats_messages, 0, 0, },
151 {'n', stats_dnsbl, 0, 0, },
152 {'o', stats_oper, 0, 0, },
153 {'O', stats_oper, 0, 0, },
154 {'p', stats_operedup, 0, 0, },
155 {'P', stats_ports, 0, 0, },
156 {'q', stats_tresv, 1, 0, },
157 {'Q', stats_resv, 1, 0, },
158 {'r', stats_usage, 1, 0, },
159 {'R', stats_usage, 1, 0, },
160 {'t', stats_tstats, 1, 0, },
161 {'T', stats_tstats, 1, 0, },
162 {'u', stats_uptime, 0, 0, },
163 {'U', stats_shared, 1, 0, },
164 {'v', stats_servers, 0, 0, },
165 {'V', stats_servers, 0, 0, },
166 {'x', stats_tgecos, 1, 0, },
167 {'X', stats_gecos, 1, 0, },
168 {'y', stats_class, 0, 0, },
169 {'Y', stats_class, 0, 0, },
170 {'z', stats_memory, 1, 0, },
171 {'Z', stats_ziplinks, 1, 0, },
172 {'?', stats_servlinks, 0, 0, },
173 {(char) 0, (void (*)()) 0, 0, 0, }
177 * m_stats by fl_
178 * parv[0] = sender prefix
179 * parv[1] = stat letter/command
180 * parv[2] = (if present) server/mask in stats L, or target
182 * This will search the tables for the appropriate stats letter,
183 * if found execute it.
185 static int
186 m_stats(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
188 static time_t last_used = 0;
189 int i;
190 char statchar;
192 statchar = parv[1][0];
194 if(MyClient(source_p) && !IsOperStaffer(source_p))
196 /* Check the user is actually allowed to do /stats, and isnt flooding */
197 if((last_used + ConfigFileEntry.pace_wait) > CurrentTime)
199 /* safe enough to give this on a local connect only */
200 sendto_one(source_p, form_str(RPL_LOAD2HI),
201 me.name, source_p->name, "STATS");
202 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
203 form_str(RPL_ENDOFSTATS), statchar);
204 return 0;
206 else
207 last_used = CurrentTime;
210 if(hunt_server (client_p, source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
211 return 0;
213 if((statchar != 'L') && (statchar != 'l'))
214 stats_spy(source_p, statchar, NULL);
216 for (i = 0; stats_cmd_table[i].handler; i++)
218 if(stats_cmd_table[i].letter == statchar)
220 /* The stats table says what privs are needed, so check --fl_ */
221 /* Called for remote clients and for local opers, so check need_admin
222 * and need_oper
224 if((stats_cmd_table[i].need_admin && !IsOperAdmin (source_p)) ||
225 (stats_cmd_table[i].need_oper && !IsOper (source_p)))
227 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
228 form_str (ERR_NOPRIVILEGES));
229 break;
232 /* Blah, stats L needs the parameters, none of the others do.. */
233 if(statchar == 'L' || statchar == 'l')
234 stats_cmd_table[i].handler (source_p, parc, parv);
235 else
236 stats_cmd_table[i].handler (source_p);
240 /* Send the end of stats notice, and the stats_spy */
241 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
242 form_str(RPL_ENDOFSTATS), statchar);
244 return 0;
247 static void
248 stats_dns_servers (struct Client *source_p)
250 report_dns_servers (source_p);
253 static void
254 stats_delay(struct Client *source_p)
256 struct nd_entry *nd;
257 dlink_node *ptr;
258 int i;
260 HASH_WALK(i, U_MAX, ptr, ndTable)
262 nd = ptr->data;
263 sendto_one_notice(source_p, "Delaying: %s for %ld",
264 nd->name, (long) nd->expire);
266 HASH_WALK_END
269 static void
270 stats_hash(struct Client *source_p)
272 hash_stats(source_p);
275 static void
276 stats_connect(struct Client *source_p)
278 static char buf[5];
279 struct server_conf *server_p;
280 char *s;
281 dlink_node *ptr;
283 if((ConfigFileEntry.stats_c_oper_only ||
284 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
285 !IsOperRouting(source_p))
287 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
288 form_str(ERR_NOPRIVILEGES));
289 return;
292 DLINK_FOREACH(ptr, server_conf_list.head)
294 server_p = ptr->data;
296 if(ServerConfIllegal(server_p))
297 continue;
299 buf[0] = '\0';
300 s = buf;
302 if(IsOperRouting(source_p))
304 if(ServerConfAutoconn(server_p))
305 *s++ = 'A';
306 if(ServerConfTb(server_p))
307 *s++ = 'T';
308 if(ServerConfCompressed(server_p))
309 *s++ = 'Z';
312 if(!buf[0])
313 *s++ = '*';
315 *s = '\0';
317 sendto_one_numeric(source_p, RPL_STATSCLINE,
318 form_str(RPL_STATSCLINE),
319 #ifndef HIDE_SERVERS_IPS
320 server_p->host,
321 #else
322 "*@127.0.0.1",
323 #endif
324 buf, server_p->name,
325 server_p->port, server_p->class_name);
329 /* stats_tdeny()
331 * input - client to report to
332 * output - none
333 * side effects - client is given temp dline list.
335 static void
336 stats_tdeny (struct Client *source_p)
338 char *host, *pass, *user, *oper_reason;
339 struct AddressRec *arec;
340 struct ConfItem *aconf;
341 int i;
343 for (i = 0; i < ATABLE_SIZE; i++)
345 for (arec = atable[i]; arec; arec = arec->next)
347 if(arec->type == CONF_DLINE)
349 aconf = arec->aconf;
351 if(!(aconf->flags & CONF_FLAGS_TEMPORARY))
352 continue;
354 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
356 sendto_one_numeric(source_p, RPL_STATSDLINE,
357 form_str (RPL_STATSDLINE),
358 'd', host, pass,
359 oper_reason ? "|" : "",
360 oper_reason ? oper_reason : "");
366 /* stats_deny()
368 * input - client to report to
369 * output - none
370 * side effects - client is given dline list.
372 static void
373 stats_deny (struct Client *source_p)
375 char *host, *pass, *user, *oper_reason;
376 struct AddressRec *arec;
377 struct ConfItem *aconf;
378 int i;
380 for (i = 0; i < ATABLE_SIZE; i++)
382 for (arec = atable[i]; arec; arec = arec->next)
384 if(arec->type == CONF_DLINE)
386 aconf = arec->aconf;
388 if(aconf->flags & CONF_FLAGS_TEMPORARY)
389 continue;
391 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
393 sendto_one_numeric(source_p, RPL_STATSDLINE,
394 form_str (RPL_STATSDLINE),
395 'D', host, pass,
396 oper_reason ? "|" : "",
397 oper_reason ? oper_reason : "");
404 /* stats_exempt()
406 * input - client to report to
407 * output - none
408 * side effects - client is given list of exempt blocks
410 static void
411 stats_exempt(struct Client *source_p)
413 char *name, *host, *pass, *user, *classname;
414 struct AddressRec *arec;
415 struct ConfItem *aconf;
416 int i, port;
418 if(ConfigFileEntry.stats_e_disabled)
420 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
421 form_str (ERR_NOPRIVILEGES));
422 return;
425 for (i = 0; i < ATABLE_SIZE; i++)
427 for (arec = atable[i]; arec; arec = arec->next)
429 if(arec->type == CONF_EXEMPTDLINE)
431 aconf = arec->aconf;
432 get_printable_conf (aconf, &name, &host, &pass,
433 &user, &port, &classname);
435 sendto_one_numeric(source_p, RPL_STATSDLINE,
436 form_str(RPL_STATSDLINE),
437 'e', host, pass, "", "");
443 static void
444 stats_events (struct Client *source_p)
446 show_events (source_p);
449 static void
450 stats_hubleaf(struct Client *source_p)
452 struct remote_conf *hub_p;
453 dlink_node *ptr;
455 if((ConfigFileEntry.stats_h_oper_only ||
456 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
457 !IsOperRouting(source_p))
459 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
460 form_str (ERR_NOPRIVILEGES));
461 return;
464 DLINK_FOREACH(ptr, hubleaf_conf_list.head)
466 hub_p = ptr->data;
468 if(hub_p->flags & CONF_HUB)
469 sendto_one_numeric(source_p, RPL_STATSHLINE,
470 form_str(RPL_STATSHLINE),
471 hub_p->host, hub_p->server);
472 else
473 sendto_one_numeric(source_p, RPL_STATSLLINE,
474 form_str(RPL_STATSLLINE),
475 hub_p->host, hub_p->server);
480 static void
481 stats_auth (struct Client *source_p)
483 /* Oper only, if unopered, return ERR_NOPRIVS */
484 if((ConfigFileEntry.stats_i_oper_only == 2) && !IsOper (source_p))
485 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
486 form_str (ERR_NOPRIVILEGES));
488 /* If unopered, Only return matching auth blocks */
489 else if((ConfigFileEntry.stats_i_oper_only == 1) && !IsOper (source_p))
491 struct ConfItem *aconf;
492 char *name, *host, *pass, *user, *classname;
493 int port;
495 if(MyConnect (source_p))
496 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
497 (struct sockaddr *)&source_p->localClient->ip,
498 CONF_CLIENT,
499 source_p->localClient->ip.ss_family,
500 source_p->username);
501 else
502 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_CLIENT,
503 0, source_p->username);
505 if(aconf == NULL)
506 return;
508 get_printable_conf (aconf, &name, &host, &pass, &user, &port, &classname);
510 sendto_one_numeric(source_p, RPL_STATSILINE, form_str(RPL_STATSILINE),
511 name, show_iline_prefix(source_p, aconf, user),
512 host, port, classname);
515 /* Theyre opered, or allowed to see all auth blocks */
516 else
517 report_auth (source_p);
521 static void
522 stats_tklines(struct Client *source_p)
524 /* Oper only, if unopered, return ERR_NOPRIVS */
525 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
526 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
527 form_str (ERR_NOPRIVILEGES));
529 /* If unopered, Only return matching klines */
530 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
532 struct ConfItem *aconf;
533 char *host, *pass, *user, *oper_reason;
535 if(MyConnect (source_p))
536 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
537 (struct sockaddr *)&source_p->localClient->ip,
538 CONF_KILL,
539 source_p->localClient->ip.ss_family,
540 source_p->username);
541 else
542 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
543 0, source_p->username);
545 if(aconf == NULL)
546 return;
548 /* dont report a permanent kline as a tkline */
549 if((aconf->flags & CONF_FLAGS_TEMPORARY) == 0)
550 return;
552 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
554 sendto_one_numeric(source_p, RPL_STATSKLINE,
555 form_str(RPL_STATSKLINE), 'k',
556 user, pass, oper_reason ? "|" : "",
557 oper_reason ? oper_reason : "");
559 /* Theyre opered, or allowed to see all klines */
560 else
562 struct ConfItem *aconf;
563 dlink_node *ptr;
564 int i;
565 char *user, *host, *pass, *oper_reason;
567 for(i = 0; i < LAST_TEMP_TYPE; i++)
569 DLINK_FOREACH(ptr, temp_klines[i].head)
571 aconf = ptr->data;
573 get_printable_kline(source_p, aconf, &host, &pass,
574 &user, &oper_reason);
576 sendto_one_numeric(source_p, RPL_STATSKLINE,
577 form_str(RPL_STATSKLINE),
578 'k', host, user, pass,
579 oper_reason ? "|" : "",
580 oper_reason ? oper_reason : "");
586 static void
587 stats_klines(struct Client *source_p)
589 /* Oper only, if unopered, return ERR_NOPRIVS */
590 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
591 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
592 form_str (ERR_NOPRIVILEGES));
594 /* If unopered, Only return matching klines */
595 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
597 struct ConfItem *aconf;
598 char *host, *pass, *user, *oper_reason;
600 /* search for a kline */
601 if(MyConnect (source_p))
602 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
603 (struct sockaddr *)&source_p->localClient->ip,
604 CONF_KILL,
605 source_p->localClient->ip.ss_family,
606 source_p->username);
607 else
608 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
609 0, source_p->username);
611 if(aconf == NULL)
612 return;
614 /* dont report a tkline as a kline */
615 if(aconf->flags & CONF_FLAGS_TEMPORARY)
616 return;
618 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
620 sendto_one_numeric(source_p, RPL_STATSKLINE, form_str(RPL_STATSKLINE),
621 'K', host, user, pass, oper_reason ? "|" : "",
622 oper_reason ? oper_reason : "");
624 /* Theyre opered, or allowed to see all klines */
625 else
626 report_Klines (source_p);
629 static void
630 stats_messages(struct Client *source_p)
632 report_messages(source_p);
635 static void
636 stats_dnsbl(struct Client *source_p)
638 dlink_node *ptr;
639 struct Blacklist *blptr;
641 DLINK_FOREACH(ptr, blacklist_list.head)
643 blptr = ptr->data;
645 /* use RPL_STATSDEBUG for now -- jilles */
646 sendto_one_numeric(source_p, RPL_STATSDEBUG, "n :%d %s %s (%d)",
647 blptr->hits,
648 blptr->host,
649 blptr->status & CONF_ILLEGAL ? "disabled" : "active",
650 blptr->refcount);
654 static void
655 stats_oper(struct Client *source_p)
657 struct oper_conf *oper_p;
658 dlink_node *ptr;
660 if(!IsOperStaffer(source_p) && ConfigFileEntry.stats_o_oper_only)
662 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
663 form_str (ERR_NOPRIVILEGES));
664 return;
667 DLINK_FOREACH(ptr, oper_conf_list.head)
669 oper_p = ptr->data;
671 sendto_one_numeric(source_p, RPL_STATSOLINE,
672 form_str(RPL_STATSOLINE),
673 oper_p->username, oper_p->host, oper_p->name,
674 IsOperStaffer(source_p) ? get_oper_privs(oper_p->flags) : "0", "-1");
679 /* stats_operedup()
681 * input - client pointer
682 * output - none
683 * side effects - client is shown a list of active opers
685 static void
686 stats_operedup (struct Client *source_p)
688 struct Client *target_p;
689 dlink_node *oper_ptr;
690 unsigned int count = 0;
692 DLINK_FOREACH (oper_ptr, oper_list.head)
694 target_p = oper_ptr->data;
696 if(!IsHelper(target_p))
697 continue;
699 if(target_p->user->away)
700 continue;
702 count++;
704 sendto_one_numeric(source_p, RPL_STATSDEBUG,
705 "p :%s (%s@%s)",
706 target_p->name, target_p->username,
707 target_p->host);
710 sendto_one_numeric(source_p, RPL_STATSDEBUG,
711 "p :%u staff members", count);
713 stats_p_spy (source_p);
716 static void
717 stats_ports (struct Client *source_p)
719 if(!IsOper (source_p) && ConfigFileEntry.stats_P_oper_only)
720 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
721 form_str (ERR_NOPRIVILEGES));
722 else
723 show_ports (source_p);
726 static void
727 stats_tresv(struct Client *source_p)
729 struct ConfItem *aconf;
730 dlink_node *ptr;
731 int i;
733 DLINK_FOREACH(ptr, resv_conf_list.head)
735 aconf = ptr->data;
736 if(aconf->hold)
737 sendto_one_numeric(source_p, RPL_STATSQLINE,
738 form_str(RPL_STATSQLINE),
739 'q', aconf->port, aconf->name, aconf->passwd);
742 HASH_WALK(i, R_MAX, ptr, resvTable)
744 aconf = ptr->data;
745 if(aconf->hold)
746 sendto_one_numeric(source_p, RPL_STATSQLINE,
747 form_str(RPL_STATSQLINE),
748 'q', aconf->port, aconf->name, aconf->passwd);
750 HASH_WALK_END
754 static void
755 stats_resv(struct Client *source_p)
757 struct ConfItem *aconf;
758 dlink_node *ptr;
759 int i;
761 DLINK_FOREACH(ptr, resv_conf_list.head)
763 aconf = ptr->data;
764 if(!aconf->hold)
765 sendto_one_numeric(source_p, RPL_STATSQLINE,
766 form_str(RPL_STATSQLINE),
767 'Q', aconf->port, aconf->name, aconf->passwd);
770 HASH_WALK(i, R_MAX, ptr, resvTable)
772 aconf = ptr->data;
773 if(!aconf->hold)
774 sendto_one_numeric(source_p, RPL_STATSQLINE,
775 form_str(RPL_STATSQLINE),
776 'Q', aconf->port, aconf->name, aconf->passwd);
778 HASH_WALK_END
781 static void
782 stats_usage (struct Client *source_p)
784 struct rusage rus;
785 time_t secs;
786 time_t rup;
787 #ifdef hz
788 # define hzz hz
789 #else
790 # ifdef HZ
791 # define hzz HZ
792 # else
793 int hzz = 1;
794 # endif
795 #endif
797 if(getrusage(RUSAGE_SELF, &rus) == -1)
799 sendto_one_notice(source_p, ":Getruseage error: %s.",
800 strerror(errno));
801 return;
803 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
804 if(0 == secs)
805 secs = 1;
807 rup = (CurrentTime - startup_time) * hzz;
808 if(0 == rup)
809 rup = 1;
811 sendto_one_numeric(source_p, RPL_STATSDEBUG,
812 "R :CPU Secs %d:%d User %d:%d System %d:%d",
813 (int) (secs / 60), (int) (secs % 60),
814 (int) (rus.ru_utime.tv_sec / 60),
815 (int) (rus.ru_utime.tv_sec % 60),
816 (int) (rus.ru_stime.tv_sec / 60),
817 (int) (rus.ru_stime.tv_sec % 60));
818 sendto_one_numeric(source_p, RPL_STATSDEBUG,
819 "R :RSS %ld ShMem %ld Data %ld Stack %ld",
820 rus.ru_maxrss, (rus.ru_ixrss / rup),
821 (rus.ru_idrss / rup), (rus.ru_isrss / rup));
822 sendto_one_numeric(source_p, RPL_STATSDEBUG,
823 "R :Swaps %d Reclaims %d Faults %d",
824 (int) rus.ru_nswap, (int) rus.ru_minflt, (int) rus.ru_majflt);
825 sendto_one_numeric(source_p, RPL_STATSDEBUG,
826 "R :Block in %d out %d",
827 (int) rus.ru_inblock, (int) rus.ru_oublock);
828 sendto_one_numeric(source_p, RPL_STATSDEBUG,
829 "R :Msg Rcv %d Send %d",
830 (int) rus.ru_msgrcv, (int) rus.ru_msgsnd);
831 sendto_one_numeric(source_p, RPL_STATSDEBUG,
832 "R :Signals %d Context Vol. %d Invol %d",
833 (int) rus.ru_nsignals, (int) rus.ru_nvcsw,
834 (int) rus.ru_nivcsw);
837 static void
838 stats_tstats (struct Client *source_p)
840 tstats (source_p);
843 static void
844 stats_uptime (struct Client *source_p)
846 time_t now;
848 now = CurrentTime - startup_time;
849 sendto_one_numeric(source_p, RPL_STATSUPTIME,
850 form_str (RPL_STATSUPTIME),
851 now / 86400, (now / 3600) % 24,
852 (now / 60) % 60, now % 60);
853 sendto_one_numeric(source_p, RPL_STATSCONN,
854 form_str (RPL_STATSCONN),
855 MaxConnectionCount, MaxClientCount,
856 Count.totalrestartcount);
859 struct shared_flags
861 int flag;
862 char letter;
864 static struct shared_flags shared_flagtable[] =
866 { SHARED_PKLINE, 'K' },
867 { SHARED_TKLINE, 'k' },
868 { SHARED_UNKLINE, 'U' },
869 { SHARED_PXLINE, 'X' },
870 { SHARED_TXLINE, 'x' },
871 { SHARED_UNXLINE, 'Y' },
872 { SHARED_PRESV, 'Q' },
873 { SHARED_TRESV, 'q' },
874 { SHARED_UNRESV, 'R' },
875 { SHARED_PDLINE, 'D' },
876 { SHARED_TDLINE, 'd' },
877 { SHARED_UNDLINE, 'E' },
878 { SHARED_REHASH, 'H' },
879 { 0, '\0'}
883 static void
884 stats_shared (struct Client *source_p)
886 struct remote_conf *shared_p;
887 dlink_node *ptr;
888 char buf[15];
889 char *p;
890 int i;
892 DLINK_FOREACH(ptr, shared_conf_list.head)
894 shared_p = ptr->data;
896 p = buf;
898 *p++ = 'c';
900 for(i = 0; shared_flagtable[i].flag != 0; i++)
902 if(shared_p->flags & shared_flagtable[i].flag)
903 *p++ = shared_flagtable[i].letter;
906 *p = '\0';
908 sendto_one_numeric(source_p, RPL_STATSULINE,
909 form_str(RPL_STATSULINE),
910 shared_p->server, shared_p->username,
911 shared_p->host, buf);
914 DLINK_FOREACH(ptr, cluster_conf_list.head)
916 shared_p = ptr->data;
918 p = buf;
920 *p++ = 'C';
922 for(i = 0; shared_flagtable[i].flag != 0; i++)
924 if(shared_p->flags & shared_flagtable[i].flag)
925 *p++ = shared_flagtable[i].letter;
928 *p = '\0';
930 sendto_one_numeric(source_p, RPL_STATSULINE,
931 form_str(RPL_STATSULINE),
932 shared_p->server, "*", "*", buf);
936 /* stats_servers()
938 * input - client pointer
939 * output - none
940 * side effects - client is shown lists of who connected servers
942 static void
943 stats_servers (struct Client *source_p)
945 struct Client *target_p;
946 dlink_node *ptr;
947 time_t seconds;
948 int days, hours, minutes;
949 int j = 0;
951 if(ConfigServerHide.flatten_links && !IsOperRouting(source_p) &&
952 !IsExemptShide(source_p))
954 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
955 form_str (ERR_NOPRIVILEGES));
956 return;
959 DLINK_FOREACH (ptr, serv_list.head)
961 target_p = ptr->data;
963 j++;
964 seconds = CurrentTime - target_p->localClient->firsttime;
966 days = (int) (seconds / 86400);
967 seconds %= 86400;
968 hours = (int) (seconds / 3600);
969 seconds %= 3600;
970 minutes = (int) (seconds / 60);
971 seconds %= 60;
973 sendto_one_numeric(source_p, RPL_STATSDEBUG,
974 "V :%s (%s!*@*) Idle: %d SendQ: %d "
975 "Connected: %d day%s, %d:%02d:%02d",
976 target_p->name,
977 (target_p->serv->by[0] ? target_p->serv->by : "Remote."),
978 (int) (CurrentTime - target_p->localClient->lasttime),
979 (int) linebuf_len (&target_p->localClient->buf_sendq),
980 days, (days == 1) ? "" : "s", hours, minutes,
981 (int) seconds);
984 sendto_one_numeric(source_p, RPL_STATSDEBUG,
985 "V :%d Server(s)", j);
988 static void
989 stats_tgecos(struct Client *source_p)
991 struct ConfItem *aconf;
992 dlink_node *ptr;
994 DLINK_FOREACH(ptr, xline_conf_list.head)
996 aconf = ptr->data;
998 if(aconf->hold)
999 sendto_one_numeric(source_p, RPL_STATSXLINE,
1000 form_str(RPL_STATSXLINE),
1001 'x', aconf->port, aconf->name,
1002 aconf->passwd);
1006 static void
1007 stats_gecos(struct Client *source_p)
1009 struct ConfItem *aconf;
1010 dlink_node *ptr;
1012 DLINK_FOREACH(ptr, xline_conf_list.head)
1014 aconf = ptr->data;
1016 if(!aconf->hold)
1017 sendto_one_numeric(source_p, RPL_STATSXLINE,
1018 form_str(RPL_STATSXLINE),
1019 'X', aconf->port, aconf->name,
1020 aconf->passwd);
1024 static void
1025 stats_class(struct Client *source_p)
1027 if(ConfigFileEntry.stats_y_oper_only && !IsOperStaffer(source_p))
1028 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1029 form_str (ERR_NOPRIVILEGES));
1030 else
1031 report_classes(source_p);
1034 static void
1035 stats_memory (struct Client *source_p)
1037 count_memory (source_p);
1040 static void
1041 stats_ziplinks (struct Client *source_p)
1043 dlink_node *ptr;
1044 struct Client *target_p;
1045 int sent_data = 0;
1047 DLINK_FOREACH (ptr, serv_list.head)
1049 target_p = ptr->data;
1050 if(IsCapable (target_p, CAP_ZIP))
1052 /* we use memcpy(3) and a local copy of the structure to
1053 * work around a register use bug on GCC on the SPARC.
1054 * -jmallett, 04/27/2002
1056 struct ZipStats zipstats;
1057 memcpy (&zipstats, &target_p->localClient->zipstats,
1058 sizeof (struct ZipStats));
1059 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1060 "Z :ZipLinks stats for %s send[%.2f%% compression "
1061 "(%lu kB data/%lu kB wire)] recv[%.2f%% compression "
1062 "(%lu kB data/%lu kB wire)]",
1063 target_p->name,
1064 zipstats.out_ratio, zipstats.outK, zipstats.outK_wire,
1065 zipstats.in_ratio, zipstats.inK, zipstats.inK_wire);
1066 sent_data++;
1070 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1071 "Z :%u ziplink(s)", sent_data);
1074 static void
1075 stats_servlinks (struct Client *source_p)
1077 static char Sformat[] = ":%s %d %s %s %u %u %u %u %u :%u %u %s";
1078 long uptime, sendK, receiveK;
1079 struct Client *target_p;
1080 dlink_node *ptr;
1081 int j = 0;
1083 if(ConfigServerHide.flatten_links && !IsOper (source_p) &&
1084 !IsExemptShide(source_p))
1086 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1087 form_str (ERR_NOPRIVILEGES));
1088 return;
1091 sendK = receiveK = 0;
1093 DLINK_FOREACH (ptr, serv_list.head)
1095 target_p = ptr->data;
1097 j++;
1098 sendK += target_p->localClient->sendK;
1099 receiveK += target_p->localClient->receiveK;
1101 sendto_one(source_p, Sformat,
1102 get_id(&me, source_p), RPL_STATSLINKINFO, get_id(source_p, source_p),
1103 get_server_name(target_p, SHOW_IP),
1104 (int) linebuf_len (&target_p->localClient->buf_sendq),
1105 (int) target_p->localClient->sendM,
1106 (int) target_p->localClient->sendK,
1107 (int) target_p->localClient->receiveM,
1108 (int) target_p->localClient->receiveK,
1109 CurrentTime - target_p->localClient->firsttime,
1110 (CurrentTime > target_p->localClient->lasttime) ?
1111 (CurrentTime - target_p->localClient->lasttime) : 0,
1112 IsOper (source_p) ? show_capabilities (target_p) : "TS");
1115 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1116 "? :%u total server(s)", j);
1118 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1119 "? :Sent total : %7.2f %s",
1120 _GMKv (sendK), _GMKs (sendK));
1121 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1122 "? :Recv total : %7.2f %s",
1123 _GMKv (receiveK), _GMKs (receiveK));
1125 uptime = (CurrentTime - startup_time);
1127 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1128 "? :Server send: %7.2f %s (%4.1f K/s)",
1129 _GMKv (me.localClient->sendK),
1130 _GMKs (me.localClient->sendK),
1131 (float) ((float) me.localClient->sendK / (float) uptime));
1132 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1133 "? :Server recv: %7.2f %s (%4.1f K/s)",
1134 _GMKv (me.localClient->receiveK),
1135 _GMKs (me.localClient->receiveK),
1136 (float) ((float) me.localClient->receiveK / (float) uptime));
1139 static void
1140 stats_ltrace(struct Client *source_p, int parc, const char *parv[])
1142 int doall = 0;
1143 int wilds = 0;
1144 const char *name;
1145 char statchar = parv[1][0];
1147 /* this is def targeted at us somehow.. */
1148 if(parc > 2 && !EmptyString(parv[2]))
1150 /* directed at us generically? */
1151 if(match(parv[2], me.name) ||
1152 (!MyClient(source_p) && !irccmp(parv[2], me.id)))
1154 name = me.name;
1155 doall = 1;
1157 else
1159 name = parv[2];
1160 wilds = strchr(name, '*') || strchr(name, '?');
1163 /* must be directed at a specific person thats not us */
1164 if(!doall && !wilds)
1166 struct Client *target_p;
1168 if(MyClient(source_p))
1169 target_p = find_named_person(name);
1170 else
1171 target_p = find_person(name);
1173 if(target_p != NULL)
1175 stats_spy(source_p, statchar, target_p->name);
1176 stats_l_client(source_p, target_p, statchar);
1178 else
1179 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
1180 form_str(ERR_NOSUCHSERVER),
1181 name);
1183 return;
1186 else
1188 name = me.name;
1189 doall = 1;
1192 stats_spy(source_p, statchar, name);
1194 if(doall)
1196 /* local opers get everyone */
1197 if(MyOper(source_p))
1199 stats_l_list(source_p, name, doall, wilds, &unknown_list, statchar);
1200 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1202 else
1204 /* they still need themselves if theyre local.. */
1205 if(MyClient(source_p))
1206 stats_l_client(source_p, source_p, statchar);
1208 stats_l_list(source_p, name, doall, wilds, &local_oper_list, statchar);
1211 if (!ConfigServerHide.flatten_links || IsOperRouting(source_p) ||
1212 IsExemptShide(source_p))
1213 stats_l_list(source_p, name, doall, wilds, &serv_list, statchar);
1215 return;
1218 /* ok, at this point theyre looking for a specific client whos on
1219 * our server.. but it contains a wildcard. --fl
1221 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1223 return;
1227 static void
1228 stats_l_list(struct Client *source_p, const char *name, int doall, int wilds,
1229 dlink_list * list, char statchar)
1231 dlink_node *ptr;
1232 struct Client *target_p;
1234 /* send information about connections which match. note, we
1235 * dont need tests for IsInvisible(), because non-opers will
1236 * never get here for normal clients --fl
1238 DLINK_FOREACH(ptr, list->head)
1240 target_p = ptr->data;
1242 if(!doall && wilds && !match(name, target_p->name))
1243 continue;
1245 stats_l_client(source_p, target_p, statchar);
1249 void
1250 stats_l_client(struct Client *source_p, struct Client *target_p,
1251 char statchar)
1253 if(IsAnyServer(target_p))
1255 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1256 get_server_name(target_p, SHOW_IP),
1257 (int) linebuf_len(&target_p->localClient->buf_sendq),
1258 (int) target_p->localClient->sendM,
1259 (int) target_p->localClient->sendK,
1260 (int) target_p->localClient->receiveM,
1261 (int) target_p->localClient->receiveK,
1262 CurrentTime - target_p->localClient->firsttime,
1263 (CurrentTime > target_p->localClient->lasttime) ?
1264 (CurrentTime - target_p->localClient->lasttime) : 0,
1265 IsOperStaffer(source_p) ? show_capabilities(target_p) : "-");
1268 else
1270 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1271 show_ip(source_p, target_p) ?
1272 (IsUpper(statchar) ?
1273 get_client_name(target_p, SHOW_IP) :
1274 get_client_name(target_p, HIDE_IP)) :
1275 get_client_name(target_p, MASK_IP),
1276 (int) linebuf_len(&target_p->localClient->buf_sendq),
1277 (int) target_p->localClient->sendM,
1278 (int) target_p->localClient->sendK,
1279 (int) target_p->localClient->receiveM,
1280 (int) target_p->localClient->receiveK,
1281 CurrentTime - target_p->localClient->firsttime,
1282 (CurrentTime > target_p->localClient->lasttime) ?
1283 (CurrentTime - target_p->localClient->lasttime) : 0,
1284 "-");
1289 * stats_spy
1291 * inputs - pointer to client doing the /stats
1292 * - char letter they are doing /stats on
1293 * output - none
1294 * side effects -
1295 * This little helper function reports to opers if configured.
1296 * personally, I don't see why opers need to see stats requests
1297 * at all. They are just "noise" to an oper, and users can't do
1298 * any damage with stats requests now anyway. So, why show them?
1299 * -Dianora
1301 static void
1302 stats_spy(struct Client *source_p, char statchar, const char *name)
1304 hook_data_int data;
1306 data.client = source_p;
1307 data.arg1 = name;
1308 data.arg2 = (int) statchar;
1310 call_hook(doing_stats_hook, &data);
1313 /* stats_p_spy()
1315 * input - pointer to client doing stats
1316 * ouput -
1317 * side effects - call hook doing_stats_p
1319 static void
1320 stats_p_spy (struct Client *source_p)
1322 hook_data data;
1324 data.client = source_p;
1325 data.arg1 = data.arg2 = NULL;
1327 call_hook(doing_stats_p_hook, &data);