Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / modules / m_stats.c
blob4ab22335658ef553c47e9ae9ad9952b7e9e45cf7
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
25 #include "stdinc.h"
26 #include "tools.h" /* dlink_node/dlink_list */
27 #include "class.h" /* report_classes */
28 #include "client.h" /* Client */
29 #include "common.h" /* TRUE/FALSE */
30 #include "irc_string.h"
31 #include "ircd.h" /* me */
32 #include "listener.h" /* show_ports */
33 #include "msg.h" /* Message */
34 #include "hostmask.h" /* report_mtrie_conf_links */
35 #include "numeric.h" /* ERR_xxx */
36 #include "scache.h" /* list_scache */
37 #include "send.h" /* sendto_one */
38 #include "commio.h" /* highest_fd */
39 #include "s_conf.h" /* ConfItem */
40 #include "s_serv.h" /* hunt_server */
41 #include "s_stats.h" /* tstats */
42 #include "s_user.h" /* show_opers */
43 #include "event.h" /* events */
44 #include "blacklist.h" /* dnsbl stuff */
45 #include "linebuf.h"
46 #include "parse.h"
47 #include "modules.h"
48 #include "hook.h"
49 #include "s_newconf.h"
50 #include "hash.h"
52 static int m_stats (struct Client *, struct Client *, int, const char **);
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}}
59 int doing_stats_hook;
60 int doing_stats_p_hook;
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 }
69 DECLARE_MODULE_AV1(stats, NULL, NULL, stats_clist, stats_hlist, NULL, "$Revision: 147 $");
71 const char *Lformat = "%s %u %u %u %u %u :%u %u %s";
73 static void stats_l_list(struct Client *s, const char *, int, int, dlink_list *, char);
74 static void stats_l_client(struct Client *source_p, struct Client *target_p,
75 char statchar);
77 static void stats_spy(struct Client *, char, const char *);
78 static void stats_p_spy(struct Client *);
80 /* Heres our struct for the stats table */
81 struct StatsStruct
83 char letter;
84 void (*handler) ();
85 int need_oper;
86 int need_admin;
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_operedup(struct Client *);
105 static void stats_ports(struct Client *);
106 static void stats_tresv(struct Client *);
107 static void stats_resv(struct Client *);
108 static void stats_usage(struct Client *);
109 static void stats_tstats(struct Client *);
110 static void stats_uptime(struct Client *);
111 static void stats_shared(struct Client *);
112 static void stats_servers(struct Client *);
113 static void stats_tgecos(struct Client *);
114 static void stats_gecos(struct Client *);
115 static void stats_class(struct Client *);
116 static void stats_memory(struct Client *);
117 static void stats_servlinks(struct Client *);
118 static void stats_ltrace(struct Client *, int, const char **);
119 static void stats_ziplinks(struct Client *);
121 /* This table contains the possible stats items, in order:
122 * stats letter, function to call, operonly? adminonly?
123 * case only matters in the stats letter column.. -- fl_
125 static struct StatsStruct stats_cmd_table[] = {
126 /* letter function need_oper need_admin */
127 {'a', stats_dns_servers, 1, 1, },
128 {'A', stats_dns_servers, 1, 1, },
129 {'b', stats_delay, 1, 1, },
130 {'B', stats_hash, 1, 1, },
131 {'c', stats_connect, 0, 0, },
132 {'C', stats_connect, 0, 0, },
133 {'d', stats_tdeny, 1, 0, },
134 {'D', stats_deny, 1, 0, },
135 {'e', stats_exempt, 1, 0, },
136 {'E', stats_events, 1, 1, },
137 {'f', comm_dump, 1, 1, },
138 {'F', comm_dump, 1, 1, },
139 {'h', stats_hubleaf, 0, 0, },
140 {'H', stats_hubleaf, 0, 0, },
141 {'i', stats_auth, 0, 0, },
142 {'I', stats_auth, 0, 0, },
143 {'k', stats_tklines, 0, 0, },
144 {'K', stats_klines, 0, 0, },
145 {'l', stats_ltrace, 0, 0, },
146 {'L', stats_ltrace, 0, 0, },
147 {'m', stats_messages, 0, 0, },
148 {'M', stats_messages, 0, 0, },
149 {'n', stats_dnsbl, 0, 0, },
150 {'o', stats_oper, 0, 0, },
151 {'O', stats_oper, 0, 0, },
152 {'p', stats_operedup, 0, 0, },
153 {'P', stats_ports, 0, 0, },
154 {'q', stats_tresv, 1, 0, },
155 {'Q', stats_resv, 1, 0, },
156 {'r', stats_usage, 1, 0, },
157 {'R', stats_usage, 1, 0, },
158 {'t', stats_tstats, 1, 0, },
159 {'T', stats_tstats, 1, 0, },
160 {'u', stats_uptime, 0, 0, },
161 {'U', stats_shared, 1, 0, },
162 {'v', stats_servers, 0, 0, },
163 {'V', stats_servers, 0, 0, },
164 {'x', stats_tgecos, 1, 0, },
165 {'X', stats_gecos, 1, 0, },
166 {'y', stats_class, 0, 0, },
167 {'Y', stats_class, 0, 0, },
168 {'z', stats_memory, 1, 0, },
169 {'Z', stats_ziplinks, 1, 0, },
170 {'?', stats_servlinks, 0, 0, },
171 {(char) 0, (void (*)()) 0, 0, 0, }
175 * m_stats by fl_
176 * parv[0] = sender prefix
177 * parv[1] = stat letter/command
178 * parv[2] = (if present) server/mask in stats L, or target
180 * This will search the tables for the appropriate stats letter,
181 * if found execute it.
183 static int
184 m_stats(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
186 static time_t last_used = 0;
187 int i;
188 char statchar;
190 statchar = parv[1][0];
192 if(MyClient(source_p) && !IsOperStaffer(source_p))
194 /* Check the user is actually allowed to do /stats, and isnt flooding */
195 if((last_used + ConfigFileEntry.pace_wait) > CurrentTime)
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;
204 else
205 last_used = CurrentTime;
208 if(hunt_server (client_p, source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
209 return 0;
211 if((statchar != 'L') && (statchar != 'l'))
212 stats_spy(source_p, statchar, NULL);
214 for (i = 0; stats_cmd_table[i].handler; i++)
216 if(stats_cmd_table[i].letter == statchar)
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
222 if((stats_cmd_table[i].need_admin && !IsOperAdmin (source_p)) ||
223 (stats_cmd_table[i].need_oper && !IsOper (source_p)))
225 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
226 form_str (ERR_NOPRIVILEGES));
227 break;
230 /* Blah, stats L needs the parameters, none of the others do.. */
231 if(statchar == 'L' || statchar == 'l')
232 stats_cmd_table[i].handler (source_p, parc, parv);
233 else
234 stats_cmd_table[i].handler (source_p);
238 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
239 form_str(RPL_ENDOFSTATS), statchar);
241 return 0;
244 static void
245 stats_dns_servers (struct Client *source_p)
247 report_dns_servers (source_p);
250 static void
251 stats_delay(struct Client *source_p)
253 struct nd_entry *nd;
254 dlink_node *ptr;
255 int i;
257 HASH_WALK(i, U_MAX, ptr, ndTable)
259 nd = ptr->data;
260 sendto_one_notice(source_p, "Delaying: %s for %ld",
261 nd->name, (long) nd->expire);
263 HASH_WALK_END
266 static void
267 stats_hash(struct Client *source_p)
269 hash_stats(source_p);
272 static void
273 stats_connect(struct Client *source_p)
275 static char buf[5];
276 struct server_conf *server_p;
277 char *s;
278 dlink_node *ptr;
280 if((ConfigFileEntry.stats_c_oper_only ||
281 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
282 !IsOperRouting(source_p))
284 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
285 form_str(ERR_NOPRIVILEGES));
286 return;
289 DLINK_FOREACH(ptr, server_conf_list.head)
291 server_p = ptr->data;
293 if(ServerConfIllegal(server_p))
294 continue;
296 buf[0] = '\0';
297 s = buf;
299 if(IsOperRouting(source_p))
301 if(ServerConfAutoconn(server_p))
302 *s++ = 'A';
303 if(ServerConfTb(server_p))
304 *s++ = 'T';
305 if(ServerConfCompressed(server_p))
306 *s++ = 'Z';
309 if(!buf[0])
310 *s++ = '*';
312 *s = '\0';
314 sendto_one_numeric(source_p, RPL_STATSCLINE,
315 form_str(RPL_STATSCLINE),
316 #ifndef HIDE_SERVERS_IPS
317 server_p->host,
318 #else
319 "*@127.0.0.1",
320 #endif
321 buf, server_p->name,
322 server_p->port, server_p->class_name);
326 /* stats_tdeny()
328 * input - client to report to
329 * output - none
330 * side effects - client is given temp dline list.
332 static void
333 stats_tdeny (struct Client *source_p)
335 char *host, *pass, *user, *oper_reason;
336 struct AddressRec *arec;
337 struct ConfItem *aconf;
338 int i;
340 for (i = 0; i < ATABLE_SIZE; i++)
342 for (arec = atable[i]; arec; arec = arec->next)
344 if(arec->type == CONF_DLINE)
346 aconf = arec->aconf;
348 if(!(aconf->flags & CONF_FLAGS_TEMPORARY))
349 continue;
351 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
353 sendto_one_numeric(source_p, RPL_STATSDLINE,
354 form_str (RPL_STATSDLINE),
355 'd', host, pass,
356 oper_reason ? "|" : "",
357 oper_reason ? oper_reason : "");
363 /* stats_deny()
365 * input - client to report to
366 * output - none
367 * side effects - client is given dline list.
369 static void
370 stats_deny (struct Client *source_p)
372 char *host, *pass, *user, *oper_reason;
373 struct AddressRec *arec;
374 struct ConfItem *aconf;
375 int i;
377 for (i = 0; i < ATABLE_SIZE; i++)
379 for (arec = atable[i]; arec; arec = arec->next)
381 if(arec->type == CONF_DLINE)
383 aconf = arec->aconf;
385 if(aconf->flags & CONF_FLAGS_TEMPORARY)
386 continue;
388 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
390 sendto_one_numeric(source_p, RPL_STATSDLINE,
391 form_str (RPL_STATSDLINE),
392 'D', host, pass,
393 oper_reason ? "|" : "",
394 oper_reason ? oper_reason : "");
401 /* stats_exempt()
403 * input - client to report to
404 * output - none
405 * side effects - client is given list of exempt blocks
407 static void
408 stats_exempt(struct Client *source_p)
410 char *name, *host, *pass, *user, *classname;
411 struct AddressRec *arec;
412 struct ConfItem *aconf;
413 int i, port;
415 if(ConfigFileEntry.stats_e_disabled)
417 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
418 form_str (ERR_NOPRIVILEGES));
419 return;
422 for (i = 0; i < ATABLE_SIZE; i++)
424 for (arec = atable[i]; arec; arec = arec->next)
426 if(arec->type == CONF_EXEMPTDLINE)
428 aconf = arec->aconf;
429 get_printable_conf (aconf, &name, &host, &pass,
430 &user, &port, &classname);
432 sendto_one_numeric(source_p, RPL_STATSDLINE,
433 form_str(RPL_STATSDLINE),
434 'e', host, pass, "", "");
440 static void
441 stats_events (struct Client *source_p)
443 show_events (source_p);
446 static void
447 stats_hubleaf(struct Client *source_p)
449 struct remote_conf *hub_p;
450 dlink_node *ptr;
452 if((ConfigFileEntry.stats_h_oper_only ||
453 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
454 !IsOperRouting(source_p))
456 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
457 form_str (ERR_NOPRIVILEGES));
458 return;
461 DLINK_FOREACH(ptr, hubleaf_conf_list.head)
463 hub_p = ptr->data;
465 if(hub_p->flags & CONF_HUB)
466 sendto_one_numeric(source_p, RPL_STATSHLINE,
467 form_str(RPL_STATSHLINE),
468 hub_p->host, hub_p->server);
469 else
470 sendto_one_numeric(source_p, RPL_STATSLLINE,
471 form_str(RPL_STATSLLINE),
472 hub_p->host, hub_p->server);
477 static void
478 stats_auth (struct Client *source_p)
480 /* Oper only, if unopered, return ERR_NOPRIVS */
481 if((ConfigFileEntry.stats_i_oper_only == 2) && !IsOper (source_p))
482 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
483 form_str (ERR_NOPRIVILEGES));
485 /* If unopered, Only return matching auth blocks */
486 else if((ConfigFileEntry.stats_i_oper_only == 1) && !IsOper (source_p))
488 struct ConfItem *aconf;
489 char *name, *host, *pass, *user, *classname;
490 int port;
492 if(MyConnect (source_p))
493 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
494 (struct sockaddr *)&source_p->localClient->ip,
495 CONF_CLIENT,
496 source_p->localClient->ip.ss_family,
497 source_p->username);
498 else
499 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_CLIENT,
500 0, source_p->username);
502 if(aconf == NULL)
503 return;
505 get_printable_conf (aconf, &name, &host, &pass, &user, &port, &classname);
507 sendto_one_numeric(source_p, RPL_STATSILINE, form_str(RPL_STATSILINE),
508 name, show_iline_prefix(source_p, aconf, user),
509 host, port, classname);
512 /* Theyre opered, or allowed to see all auth blocks */
513 else
514 report_auth (source_p);
518 static void
519 stats_tklines(struct Client *source_p)
521 /* Oper only, if unopered, return ERR_NOPRIVS */
522 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
523 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
524 form_str (ERR_NOPRIVILEGES));
526 /* If unopered, Only return matching klines */
527 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
529 struct ConfItem *aconf;
530 char *host, *pass, *user, *oper_reason;
532 if(MyConnect (source_p))
533 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
534 (struct sockaddr *)&source_p->localClient->ip,
535 CONF_KILL,
536 source_p->localClient->ip.ss_family,
537 source_p->username);
538 else
539 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
540 0, source_p->username);
542 if(aconf == NULL)
543 return;
545 /* dont report a permanent kline as a tkline */
546 if((aconf->flags & CONF_FLAGS_TEMPORARY) == 0)
547 return;
549 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
551 sendto_one_numeric(source_p, RPL_STATSKLINE,
552 form_str(RPL_STATSKLINE), 'k',
553 user, pass, oper_reason ? "|" : "",
554 oper_reason ? oper_reason : "");
556 /* Theyre opered, or allowed to see all klines */
557 else
559 struct ConfItem *aconf;
560 dlink_node *ptr;
561 int i;
562 char *user, *host, *pass, *oper_reason;
564 for(i = 0; i < LAST_TEMP_TYPE; i++)
566 DLINK_FOREACH(ptr, temp_klines[i].head)
568 aconf = ptr->data;
570 get_printable_kline(source_p, aconf, &host, &pass,
571 &user, &oper_reason);
573 sendto_one_numeric(source_p, RPL_STATSKLINE,
574 form_str(RPL_STATSKLINE),
575 'k', host, user, pass,
576 oper_reason ? "|" : "",
577 oper_reason ? oper_reason : "");
583 static void
584 stats_klines(struct Client *source_p)
586 /* Oper only, if unopered, return ERR_NOPRIVS */
587 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
588 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
589 form_str (ERR_NOPRIVILEGES));
591 /* If unopered, Only return matching klines */
592 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
594 struct ConfItem *aconf;
595 char *host, *pass, *user, *oper_reason;
597 /* search for a kline */
598 if(MyConnect (source_p))
599 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
600 (struct sockaddr *)&source_p->localClient->ip,
601 CONF_KILL,
602 source_p->localClient->ip.ss_family,
603 source_p->username);
604 else
605 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
606 0, source_p->username);
608 if(aconf == NULL)
609 return;
611 /* dont report a tkline as a kline */
612 if(aconf->flags & CONF_FLAGS_TEMPORARY)
613 return;
615 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
617 sendto_one_numeric(source_p, RPL_STATSKLINE, form_str(RPL_STATSKLINE),
618 'K', host, user, pass, oper_reason ? "|" : "",
619 oper_reason ? oper_reason : "");
621 /* Theyre opered, or allowed to see all klines */
622 else
623 report_Klines (source_p);
626 static void
627 stats_messages(struct Client *source_p)
629 report_messages(source_p);
632 static void
633 stats_dnsbl(struct Client *source_p)
635 dlink_node *ptr;
636 struct Blacklist *blptr;
638 DLINK_FOREACH(ptr, blacklist_list.head)
640 blptr = ptr->data;
642 /* use RPL_STATSDEBUG for now -- jilles */
643 sendto_one_numeric(source_p, RPL_STATSDEBUG, "n :%d %s %s (%d)",
644 blptr->hits,
645 blptr->host,
646 blptr->status & CONF_ILLEGAL ? "disabled" : "active",
647 blptr->refcount);
651 static void
652 stats_oper(struct Client *source_p)
654 struct oper_conf *oper_p;
655 dlink_node *ptr;
657 if(!IsOperStaffer(source_p) && ConfigFileEntry.stats_o_oper_only)
659 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
660 form_str (ERR_NOPRIVILEGES));
661 return;
664 DLINK_FOREACH(ptr, oper_conf_list.head)
666 oper_p = ptr->data;
668 sendto_one_numeric(source_p, RPL_STATSOLINE,
669 form_str(RPL_STATSOLINE),
670 oper_p->username, oper_p->host, oper_p->name,
671 IsOperStaffer(source_p) ? get_oper_privs(oper_p->flags) : "0", "-1");
676 /* stats_operedup()
678 * input - client pointer
679 * output - none
680 * side effects - client is shown a list of active opers
682 static void
683 stats_operedup (struct Client *source_p)
685 struct Client *target_p;
686 dlink_node *oper_ptr;
687 unsigned int count = 0;
689 DLINK_FOREACH (oper_ptr, oper_list.head)
691 target_p = oper_ptr->data;
693 if(!IsHelper(target_p))
694 continue;
696 if(target_p->user->away)
697 continue;
699 count++;
701 sendto_one_numeric(source_p, RPL_STATSDEBUG,
702 "p :%s (%s@%s)",
703 target_p->name, target_p->username,
704 target_p->host);
707 sendto_one_numeric(source_p, RPL_STATSDEBUG,
708 "p :%u staff members", count);
711 static void
712 stats_ports (struct Client *source_p)
714 if(!IsOper (source_p) && ConfigFileEntry.stats_P_oper_only)
715 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
716 form_str (ERR_NOPRIVILEGES));
717 else
718 show_ports (source_p);
721 static void
722 stats_tresv(struct Client *source_p)
724 struct ConfItem *aconf;
725 dlink_node *ptr;
726 int i;
728 DLINK_FOREACH(ptr, resv_conf_list.head)
730 aconf = ptr->data;
731 if(aconf->hold)
732 sendto_one_numeric(source_p, RPL_STATSQLINE,
733 form_str(RPL_STATSQLINE),
734 'q', aconf->port, aconf->name, aconf->passwd);
737 HASH_WALK(i, R_MAX, ptr, resvTable)
739 aconf = ptr->data;
740 if(aconf->hold)
741 sendto_one_numeric(source_p, RPL_STATSQLINE,
742 form_str(RPL_STATSQLINE),
743 'q', aconf->port, aconf->name, aconf->passwd);
745 HASH_WALK_END
749 static void
750 stats_resv(struct Client *source_p)
752 struct ConfItem *aconf;
753 dlink_node *ptr;
754 int i;
756 DLINK_FOREACH(ptr, resv_conf_list.head)
758 aconf = ptr->data;
759 if(!aconf->hold)
760 sendto_one_numeric(source_p, RPL_STATSQLINE,
761 form_str(RPL_STATSQLINE),
762 'Q', aconf->port, aconf->name, aconf->passwd);
765 HASH_WALK(i, R_MAX, ptr, resvTable)
767 aconf = ptr->data;
768 if(!aconf->hold)
769 sendto_one_numeric(source_p, RPL_STATSQLINE,
770 form_str(RPL_STATSQLINE),
771 'Q', aconf->port, aconf->name, aconf->passwd);
773 HASH_WALK_END
776 static void
777 stats_usage (struct Client *source_p)
779 struct rusage rus;
780 time_t secs;
781 time_t rup;
782 #ifdef hz
783 # define hzz hz
784 #else
785 # ifdef HZ
786 # define hzz HZ
787 # else
788 int hzz = 1;
789 # endif
790 #endif
792 if(getrusage(RUSAGE_SELF, &rus) == -1)
794 sendto_one_notice(source_p, ":Getruseage error: %s.",
795 strerror(errno));
796 return;
798 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
799 if(0 == secs)
800 secs = 1;
802 rup = (CurrentTime - startup_time) * hzz;
803 if(0 == rup)
804 rup = 1;
806 sendto_one_numeric(source_p, RPL_STATSDEBUG,
807 "R :CPU Secs %d:%d User %d:%d System %d:%d",
808 (int) (secs / 60), (int) (secs % 60),
809 (int) (rus.ru_utime.tv_sec / 60),
810 (int) (rus.ru_utime.tv_sec % 60),
811 (int) (rus.ru_stime.tv_sec / 60),
812 (int) (rus.ru_stime.tv_sec % 60));
813 sendto_one_numeric(source_p, RPL_STATSDEBUG,
814 "R :RSS %ld ShMem %ld Data %ld Stack %ld",
815 rus.ru_maxrss, (rus.ru_ixrss / rup),
816 (rus.ru_idrss / rup), (rus.ru_isrss / rup));
817 sendto_one_numeric(source_p, RPL_STATSDEBUG,
818 "R :Swaps %d Reclaims %d Faults %d",
819 (int) rus.ru_nswap, (int) rus.ru_minflt, (int) rus.ru_majflt);
820 sendto_one_numeric(source_p, RPL_STATSDEBUG,
821 "R :Block in %d out %d",
822 (int) rus.ru_inblock, (int) rus.ru_oublock);
823 sendto_one_numeric(source_p, RPL_STATSDEBUG,
824 "R :Msg Rcv %d Send %d",
825 (int) rus.ru_msgrcv, (int) rus.ru_msgsnd);
826 sendto_one_numeric(source_p, RPL_STATSDEBUG,
827 "R :Signals %d Context Vol. %d Invol %d",
828 (int) rus.ru_nsignals, (int) rus.ru_nvcsw,
829 (int) rus.ru_nivcsw);
832 static void
833 stats_tstats (struct Client *source_p)
835 tstats (source_p);
838 static void
839 stats_uptime (struct Client *source_p)
841 time_t now;
843 now = CurrentTime - startup_time;
844 sendto_one_numeric(source_p, RPL_STATSUPTIME,
845 form_str (RPL_STATSUPTIME),
846 now / 86400, (now / 3600) % 24,
847 (now / 60) % 60, now % 60);
848 sendto_one_numeric(source_p, RPL_STATSCONN,
849 form_str (RPL_STATSCONN),
850 MaxConnectionCount, MaxClientCount,
851 Count.totalrestartcount);
854 struct shared_flags
856 int flag;
857 char letter;
859 static struct shared_flags shared_flagtable[] =
861 { SHARED_PKLINE, 'K' },
862 { SHARED_TKLINE, 'k' },
863 { SHARED_UNKLINE, 'U' },
864 { SHARED_PXLINE, 'X' },
865 { SHARED_TXLINE, 'x' },
866 { SHARED_UNXLINE, 'Y' },
867 { SHARED_PRESV, 'Q' },
868 { SHARED_TRESV, 'q' },
869 { SHARED_UNRESV, 'R' },
870 { SHARED_PDLINE, 'D' },
871 { SHARED_TDLINE, 'd' },
872 { SHARED_UNDLINE, 'E' },
873 { SHARED_REHASH, 'H' },
874 { 0, '\0'}
878 static void
879 stats_shared (struct Client *source_p)
881 struct remote_conf *shared_p;
882 dlink_node *ptr;
883 char buf[15];
884 char *p;
885 int i;
887 DLINK_FOREACH(ptr, shared_conf_list.head)
889 shared_p = ptr->data;
891 p = buf;
893 *p++ = 'c';
895 for(i = 0; shared_flagtable[i].flag != 0; i++)
897 if(shared_p->flags & shared_flagtable[i].flag)
898 *p++ = shared_flagtable[i].letter;
901 *p = '\0';
903 sendto_one_numeric(source_p, RPL_STATSULINE,
904 form_str(RPL_STATSULINE),
905 shared_p->server, shared_p->username,
906 shared_p->host, buf);
909 DLINK_FOREACH(ptr, cluster_conf_list.head)
911 shared_p = ptr->data;
913 p = buf;
915 *p++ = 'C';
917 for(i = 0; shared_flagtable[i].flag != 0; i++)
919 if(shared_p->flags & shared_flagtable[i].flag)
920 *p++ = shared_flagtable[i].letter;
923 *p = '\0';
925 sendto_one_numeric(source_p, RPL_STATSULINE,
926 form_str(RPL_STATSULINE),
927 shared_p->server, "*", "*", buf);
931 /* stats_servers()
933 * input - client pointer
934 * output - none
935 * side effects - client is shown lists of who connected servers
937 static void
938 stats_servers (struct Client *source_p)
940 struct Client *target_p;
941 dlink_node *ptr;
942 time_t seconds;
943 int days, hours, minutes;
944 int j = 0;
946 if(ConfigServerHide.flatten_links && !IsOperRouting(source_p) &&
947 !IsExemptShide(source_p))
949 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
950 form_str (ERR_NOPRIVILEGES));
951 return;
954 DLINK_FOREACH (ptr, serv_list.head)
956 target_p = ptr->data;
958 j++;
959 seconds = CurrentTime - target_p->localClient->firsttime;
961 days = (int) (seconds / 86400);
962 seconds %= 86400;
963 hours = (int) (seconds / 3600);
964 seconds %= 3600;
965 minutes = (int) (seconds / 60);
966 seconds %= 60;
968 sendto_one_numeric(source_p, RPL_STATSDEBUG,
969 "V :%s (%s!*@*) Idle: %d SendQ: %d "
970 "Connected: %d day%s, %d:%02d:%02d",
971 target_p->name,
972 (target_p->serv->by[0] ? target_p->serv->by : "Remote."),
973 (int) (CurrentTime - target_p->localClient->lasttime),
974 (int) linebuf_len (&target_p->localClient->buf_sendq),
975 days, (days == 1) ? "" : "s", hours, minutes,
976 (int) seconds);
979 sendto_one_numeric(source_p, RPL_STATSDEBUG,
980 "V :%d Server(s)", j);
983 static void
984 stats_tgecos(struct Client *source_p)
986 struct ConfItem *aconf;
987 dlink_node *ptr;
989 DLINK_FOREACH(ptr, xline_conf_list.head)
991 aconf = ptr->data;
993 if(aconf->hold)
994 sendto_one_numeric(source_p, RPL_STATSXLINE,
995 form_str(RPL_STATSXLINE),
996 'x', aconf->port, aconf->name,
997 aconf->passwd);
1001 static void
1002 stats_gecos(struct Client *source_p)
1004 struct ConfItem *aconf;
1005 dlink_node *ptr;
1007 DLINK_FOREACH(ptr, xline_conf_list.head)
1009 aconf = ptr->data;
1011 if(!aconf->hold)
1012 sendto_one_numeric(source_p, RPL_STATSXLINE,
1013 form_str(RPL_STATSXLINE),
1014 'X', aconf->port, aconf->name,
1015 aconf->passwd);
1019 static void
1020 stats_class(struct Client *source_p)
1022 if(ConfigFileEntry.stats_y_oper_only && !IsOperStaffer(source_p))
1023 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1024 form_str (ERR_NOPRIVILEGES));
1025 else
1026 report_classes(source_p);
1029 static void
1030 stats_memory (struct Client *source_p)
1032 count_memory (source_p);
1035 static void
1036 stats_ziplinks (struct Client *source_p)
1038 dlink_node *ptr;
1039 struct Client *target_p;
1040 int sent_data = 0;
1042 DLINK_FOREACH (ptr, serv_list.head)
1044 target_p = ptr->data;
1045 if(IsCapable (target_p, CAP_ZIP))
1047 /* we use memcpy(3) and a local copy of the structure to
1048 * work around a register use bug on GCC on the SPARC.
1049 * -jmallett, 04/27/2002
1051 struct ZipStats zipstats;
1052 memcpy (&zipstats, &target_p->localClient->zipstats,
1053 sizeof (struct ZipStats));
1054 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1055 "Z :ZipLinks stats for %s send[%.2f%% compression "
1056 "(%lu kB data/%lu kB wire)] recv[%.2f%% compression "
1057 "(%lu kB data/%lu kB wire)]",
1058 target_p->name,
1059 zipstats.out_ratio, zipstats.outK, zipstats.outK_wire,
1060 zipstats.in_ratio, zipstats.inK, zipstats.inK_wire);
1061 sent_data++;
1065 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1066 "Z :%u ziplink(s)", sent_data);
1069 static void
1070 stats_servlinks (struct Client *source_p)
1072 static char Sformat[] = ":%s %d %s %s %u %u %u %u %u :%u %u %s";
1073 long uptime, sendK, receiveK;
1074 struct Client *target_p;
1075 dlink_node *ptr;
1076 int j = 0;
1078 if(ConfigServerHide.flatten_links && !IsOper (source_p) &&
1079 !IsExemptShide(source_p))
1081 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1082 form_str (ERR_NOPRIVILEGES));
1083 return;
1086 sendK = receiveK = 0;
1088 DLINK_FOREACH (ptr, serv_list.head)
1090 target_p = ptr->data;
1092 j++;
1093 sendK += target_p->localClient->sendK;
1094 receiveK += target_p->localClient->receiveK;
1096 sendto_one(source_p, Sformat,
1097 get_id(&me, source_p), RPL_STATSLINKINFO, get_id(source_p, source_p),
1098 get_server_name(target_p, SHOW_IP),
1099 (int) linebuf_len (&target_p->localClient->buf_sendq),
1100 (int) target_p->localClient->sendM,
1101 (int) target_p->localClient->sendK,
1102 (int) target_p->localClient->receiveM,
1103 (int) target_p->localClient->receiveK,
1104 CurrentTime - target_p->localClient->firsttime,
1105 (CurrentTime > target_p->localClient->lasttime) ?
1106 (CurrentTime - target_p->localClient->lasttime) : 0,
1107 IsOper (source_p) ? show_capabilities (target_p) : "TS");
1110 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1111 "? :%u total server(s)", j);
1113 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1114 "? :Sent total : %7.2f %s",
1115 _GMKv (sendK), _GMKs (sendK));
1116 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1117 "? :Recv total : %7.2f %s",
1118 _GMKv (receiveK), _GMKs (receiveK));
1120 uptime = (CurrentTime - startup_time);
1122 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1123 "? :Server send: %7.2f %s (%4.1f K/s)",
1124 _GMKv (me.localClient->sendK),
1125 _GMKs (me.localClient->sendK),
1126 (float) ((float) me.localClient->sendK / (float) uptime));
1127 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1128 "? :Server recv: %7.2f %s (%4.1f K/s)",
1129 _GMKv (me.localClient->receiveK),
1130 _GMKs (me.localClient->receiveK),
1131 (float) ((float) me.localClient->receiveK / (float) uptime));
1134 static void
1135 stats_ltrace(struct Client *source_p, int parc, const char *parv[])
1137 int doall = 0;
1138 int wilds = 0;
1139 const char *name;
1140 char statchar = parv[1][0];
1142 /* this is def targeted at us somehow.. */
1143 if(parc > 2 && !EmptyString(parv[2]))
1145 /* directed at us generically? */
1146 if(match(parv[2], me.name) ||
1147 (!MyClient(source_p) && !irccmp(parv[2], me.id)))
1149 name = me.name;
1150 doall = 1;
1152 else
1154 name = parv[2];
1155 wilds = strchr(name, '*') || strchr(name, '?');
1158 /* must be directed at a specific person thats not us */
1159 if(!doall && !wilds)
1161 struct Client *target_p;
1163 if(MyClient(source_p))
1164 target_p = find_named_person(name);
1165 else
1166 target_p = find_person(name);
1168 if(target_p != NULL)
1170 stats_spy(source_p, statchar, target_p->name);
1171 stats_l_client(source_p, target_p, statchar);
1173 else
1174 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
1175 form_str(ERR_NOSUCHSERVER),
1176 name);
1178 return;
1181 else
1183 name = me.name;
1184 doall = 1;
1187 stats_spy(source_p, statchar, name);
1189 if(doall)
1191 /* local opers get everyone */
1192 if(MyOper(source_p))
1194 stats_l_list(source_p, name, doall, wilds, &unknown_list, statchar);
1195 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1197 else
1199 /* they still need themselves if theyre local.. */
1200 if(MyClient(source_p))
1201 stats_l_client(source_p, source_p, statchar);
1203 stats_l_list(source_p, name, doall, wilds, &local_oper_list, statchar);
1206 if (!ConfigServerHide.flatten_links || IsOperRouting(source_p) ||
1207 IsExemptShide(source_p))
1208 stats_l_list(source_p, name, doall, wilds, &serv_list, statchar);
1210 return;
1213 /* ok, at this point theyre looking for a specific client whos on
1214 * our server.. but it contains a wildcard. --fl
1216 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1218 return;
1222 static void
1223 stats_l_list(struct Client *source_p, const char *name, int doall, int wilds,
1224 dlink_list * list, char statchar)
1226 dlink_node *ptr;
1227 struct Client *target_p;
1229 /* send information about connections which match. note, we
1230 * dont need tests for IsInvisible(), because non-opers will
1231 * never get here for normal clients --fl
1233 DLINK_FOREACH(ptr, list->head)
1235 target_p = ptr->data;
1237 if(!doall && wilds && !match(name, target_p->name))
1238 continue;
1240 stats_l_client(source_p, target_p, statchar);
1244 void
1245 stats_l_client(struct Client *source_p, struct Client *target_p,
1246 char statchar)
1248 if(IsAnyServer(target_p))
1250 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1251 get_server_name(target_p, SHOW_IP),
1252 (int) linebuf_len(&target_p->localClient->buf_sendq),
1253 (int) target_p->localClient->sendM,
1254 (int) target_p->localClient->sendK,
1255 (int) target_p->localClient->receiveM,
1256 (int) target_p->localClient->receiveK,
1257 CurrentTime - target_p->localClient->firsttime,
1258 (CurrentTime > target_p->localClient->lasttime) ?
1259 (CurrentTime - target_p->localClient->lasttime) : 0,
1260 IsOperStaffer(source_p) ? show_capabilities(target_p) : "-");
1263 else
1265 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1266 show_ip(source_p, target_p) ?
1267 (IsUpper(statchar) ?
1268 get_client_name(target_p, SHOW_IP) :
1269 get_client_name(target_p, HIDE_IP)) :
1270 get_client_name(target_p, MASK_IP),
1271 (int) linebuf_len(&target_p->localClient->buf_sendq),
1272 (int) target_p->localClient->sendM,
1273 (int) target_p->localClient->sendK,
1274 (int) target_p->localClient->receiveM,
1275 (int) target_p->localClient->receiveK,
1276 CurrentTime - target_p->localClient->firsttime,
1277 (CurrentTime > target_p->localClient->lasttime) ?
1278 (CurrentTime - target_p->localClient->lasttime) : 0,
1279 "-");
1284 * stats_spy
1286 * inputs - pointer to client doing the /stats
1287 * - char letter they are doing /stats on
1288 * output - none
1289 * side effects -
1290 * This little helper function reports to opers if configured.
1291 * personally, I don't see why opers need to see stats requests
1292 * at all. They are just "noise" to an oper, and users can't do
1293 * any damage with stats requests now anyway. So, why show them?
1294 * -Dianora
1296 static void
1297 stats_spy(struct Client *source_p, char statchar, const char *name)
1299 hook_data_int data;
1301 data.client = source_p;
1302 data.arg1 = name;
1303 data.arg2 = (int) statchar;
1305 call_hook(doing_stats_hook, &data);