Remove cruft left behind by removed stats_p_hook.
[seven-1.x.git] / modules / m_stats.c
blob90d39c9d34cc9b691f3c429d5a0ad75c6635c941
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;
61 mapi_clist_av1 stats_clist[] = { &stats_msgtab, NULL };
62 mapi_hlist_av1 stats_hlist[] = {
63 { "doing_stats", &doing_stats_hook },
64 { NULL, NULL }
67 DECLARE_MODULE_AV1(stats, NULL, NULL, stats_clist, stats_hlist, NULL, "$Revision: 147 $");
69 const char *Lformat = "%s %u %u %u %u %u :%u %u %s";
71 static void stats_l_list(struct Client *s, const char *, int, int, dlink_list *, char);
72 static void stats_l_client(struct Client *source_p, struct Client *target_p,
73 char statchar);
75 static void stats_spy(struct Client *, char, const char *);
77 /* Heres our struct for the stats table */
78 struct StatsStruct
80 char letter;
81 void (*handler) ();
82 int need_oper;
83 int need_admin;
86 static void stats_dns_servers (struct Client *);
87 static void stats_delay(struct Client *);
88 static void stats_hash(struct Client *);
89 static void stats_connect(struct Client *);
90 static void stats_tdeny(struct Client *);
91 static void stats_deny(struct Client *);
92 static void stats_exempt(struct Client *);
93 static void stats_events(struct Client *);
94 static void stats_hubleaf(struct Client *);
95 static void stats_auth(struct Client *);
96 static void stats_tklines(struct Client *);
97 static void stats_klines(struct Client *);
98 static void stats_messages(struct Client *);
99 static void stats_dnsbl(struct Client *);
100 static void stats_oper(struct Client *);
101 static void stats_operedup(struct Client *);
102 static void stats_ports(struct Client *);
103 static void stats_tresv(struct Client *);
104 static void stats_resv(struct Client *);
105 static void stats_usage(struct Client *);
106 static void stats_tstats(struct Client *);
107 static void stats_uptime(struct Client *);
108 static void stats_shared(struct Client *);
109 static void stats_servers(struct Client *);
110 static void stats_tgecos(struct Client *);
111 static void stats_gecos(struct Client *);
112 static void stats_class(struct Client *);
113 static void stats_memory(struct Client *);
114 static void stats_servlinks(struct Client *);
115 static void stats_ltrace(struct Client *, int, const char **);
116 static void stats_ziplinks(struct Client *);
118 /* This table contains the possible stats items, in order:
119 * stats letter, function to call, operonly? adminonly?
120 * case only matters in the stats letter column.. -- fl_
122 static struct StatsStruct stats_cmd_table[] = {
123 /* letter function need_oper need_admin */
124 {'a', stats_dns_servers, 1, 1, },
125 {'A', stats_dns_servers, 1, 1, },
126 {'b', stats_delay, 1, 1, },
127 {'B', stats_hash, 1, 1, },
128 {'c', stats_connect, 0, 0, },
129 {'C', stats_connect, 0, 0, },
130 {'d', stats_tdeny, 1, 0, },
131 {'D', stats_deny, 1, 0, },
132 {'e', stats_exempt, 1, 0, },
133 {'E', stats_events, 1, 1, },
134 {'f', comm_dump, 1, 1, },
135 {'F', comm_dump, 1, 1, },
136 {'h', stats_hubleaf, 0, 0, },
137 {'H', stats_hubleaf, 0, 0, },
138 {'i', stats_auth, 0, 0, },
139 {'I', stats_auth, 0, 0, },
140 {'k', stats_tklines, 0, 0, },
141 {'K', stats_klines, 0, 0, },
142 {'l', stats_ltrace, 0, 0, },
143 {'L', stats_ltrace, 0, 0, },
144 {'m', stats_messages, 0, 0, },
145 {'M', stats_messages, 0, 0, },
146 {'n', stats_dnsbl, 0, 0, },
147 {'o', stats_oper, 0, 0, },
148 {'O', stats_oper, 0, 0, },
149 {'p', stats_operedup, 0, 0, },
150 {'P', stats_ports, 0, 0, },
151 {'q', stats_tresv, 1, 0, },
152 {'Q', stats_resv, 1, 0, },
153 {'r', stats_usage, 1, 0, },
154 {'R', stats_usage, 1, 0, },
155 {'t', stats_tstats, 1, 0, },
156 {'T', stats_tstats, 1, 0, },
157 {'u', stats_uptime, 0, 0, },
158 {'U', stats_shared, 1, 0, },
159 {'v', stats_servers, 0, 0, },
160 {'V', stats_servers, 0, 0, },
161 {'x', stats_tgecos, 1, 0, },
162 {'X', stats_gecos, 1, 0, },
163 {'y', stats_class, 0, 0, },
164 {'Y', stats_class, 0, 0, },
165 {'z', stats_memory, 1, 0, },
166 {'Z', stats_ziplinks, 1, 0, },
167 {'?', stats_servlinks, 0, 0, },
168 {(char) 0, (void (*)()) 0, 0, 0, }
172 * m_stats by fl_
173 * parv[0] = sender prefix
174 * parv[1] = stat letter/command
175 * parv[2] = (if present) server/mask in stats L, or target
177 * This will search the tables for the appropriate stats letter,
178 * if found execute it.
180 static int
181 m_stats(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
183 static time_t last_used = 0;
184 int i;
185 char statchar;
187 statchar = parv[1][0];
189 if(MyClient(source_p) && !IsOperStaffer(source_p))
191 /* Check the user is actually allowed to do /stats, and isnt flooding */
192 if((last_used + ConfigFileEntry.pace_wait) > CurrentTime)
194 /* safe enough to give this on a local connect only */
195 sendto_one(source_p, form_str(RPL_LOAD2HI),
196 me.name, source_p->name, "STATS");
197 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
198 form_str(RPL_ENDOFSTATS), statchar);
199 return 0;
201 else
202 last_used = CurrentTime;
205 if(hunt_server (client_p, source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
206 return 0;
208 if((statchar != 'L') && (statchar != 'l'))
209 stats_spy(source_p, statchar, NULL);
211 for (i = 0; stats_cmd_table[i].handler; i++)
213 if(stats_cmd_table[i].letter == statchar)
215 /* The stats table says what privs are needed, so check --fl_ */
216 /* Called for remote clients and for local opers, so check need_admin
217 * and need_oper
219 if((stats_cmd_table[i].need_admin && !IsOperAdmin (source_p)) ||
220 (stats_cmd_table[i].need_oper && !IsOper (source_p)))
222 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
223 form_str (ERR_NOPRIVILEGES));
224 break;
227 /* Blah, stats L needs the parameters, none of the others do.. */
228 if(statchar == 'L' || statchar == 'l')
229 stats_cmd_table[i].handler (source_p, parc, parv);
230 else
231 stats_cmd_table[i].handler (source_p);
235 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
236 form_str(RPL_ENDOFSTATS), statchar);
238 return 0;
241 static void
242 stats_dns_servers (struct Client *source_p)
244 report_dns_servers (source_p);
247 static void
248 stats_delay(struct Client *source_p)
250 struct nd_entry *nd;
251 dlink_node *ptr;
252 int i;
254 HASH_WALK(i, U_MAX, ptr, ndTable)
256 nd = ptr->data;
257 sendto_one_notice(source_p, "Delaying: %s for %ld",
258 nd->name, (long) nd->expire);
260 HASH_WALK_END
263 static void
264 stats_hash(struct Client *source_p)
266 hash_stats(source_p);
269 static void
270 stats_connect(struct Client *source_p)
272 static char buf[5];
273 struct server_conf *server_p;
274 char *s;
275 dlink_node *ptr;
277 if((ConfigFileEntry.stats_c_oper_only ||
278 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
279 !IsOperRouting(source_p))
281 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
282 form_str(ERR_NOPRIVILEGES));
283 return;
286 DLINK_FOREACH(ptr, server_conf_list.head)
288 server_p = ptr->data;
290 if(ServerConfIllegal(server_p))
291 continue;
293 buf[0] = '\0';
294 s = buf;
296 if(IsOperRouting(source_p))
298 if(ServerConfAutoconn(server_p))
299 *s++ = 'A';
300 if(ServerConfTb(server_p))
301 *s++ = 'T';
302 if(ServerConfCompressed(server_p))
303 *s++ = 'Z';
306 if(!buf[0])
307 *s++ = '*';
309 *s = '\0';
311 sendto_one_numeric(source_p, RPL_STATSCLINE,
312 form_str(RPL_STATSCLINE),
313 #ifndef HIDE_SERVERS_IPS
314 server_p->host,
315 #else
316 "*@127.0.0.1",
317 #endif
318 buf, server_p->name,
319 server_p->port, server_p->class_name);
323 /* stats_tdeny()
325 * input - client to report to
326 * output - none
327 * side effects - client is given temp dline list.
329 static void
330 stats_tdeny (struct Client *source_p)
332 char *host, *pass, *user, *oper_reason;
333 struct AddressRec *arec;
334 struct ConfItem *aconf;
335 int i;
337 for (i = 0; i < ATABLE_SIZE; i++)
339 for (arec = atable[i]; arec; arec = arec->next)
341 if(arec->type == CONF_DLINE)
343 aconf = arec->aconf;
345 if(!(aconf->flags & CONF_FLAGS_TEMPORARY))
346 continue;
348 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
350 sendto_one_numeric(source_p, RPL_STATSDLINE,
351 form_str (RPL_STATSDLINE),
352 'd', host, pass,
353 oper_reason ? "|" : "",
354 oper_reason ? oper_reason : "");
360 /* stats_deny()
362 * input - client to report to
363 * output - none
364 * side effects - client is given dline list.
366 static void
367 stats_deny (struct Client *source_p)
369 char *host, *pass, *user, *oper_reason;
370 struct AddressRec *arec;
371 struct ConfItem *aconf;
372 int i;
374 for (i = 0; i < ATABLE_SIZE; i++)
376 for (arec = atable[i]; arec; arec = arec->next)
378 if(arec->type == CONF_DLINE)
380 aconf = arec->aconf;
382 if(aconf->flags & CONF_FLAGS_TEMPORARY)
383 continue;
385 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
387 sendto_one_numeric(source_p, RPL_STATSDLINE,
388 form_str (RPL_STATSDLINE),
389 'D', host, pass,
390 oper_reason ? "|" : "",
391 oper_reason ? oper_reason : "");
398 /* stats_exempt()
400 * input - client to report to
401 * output - none
402 * side effects - client is given list of exempt blocks
404 static void
405 stats_exempt(struct Client *source_p)
407 char *name, *host, *pass, *user, *classname;
408 struct AddressRec *arec;
409 struct ConfItem *aconf;
410 int i, port;
412 if(ConfigFileEntry.stats_e_disabled)
414 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
415 form_str (ERR_NOPRIVILEGES));
416 return;
419 for (i = 0; i < ATABLE_SIZE; i++)
421 for (arec = atable[i]; arec; arec = arec->next)
423 if(arec->type == CONF_EXEMPTDLINE)
425 aconf = arec->aconf;
426 get_printable_conf (aconf, &name, &host, &pass,
427 &user, &port, &classname);
429 sendto_one_numeric(source_p, RPL_STATSDLINE,
430 form_str(RPL_STATSDLINE),
431 'e', host, pass, "", "");
437 static void
438 stats_events (struct Client *source_p)
440 show_events (source_p);
443 static void
444 stats_hubleaf(struct Client *source_p)
446 struct remote_conf *hub_p;
447 dlink_node *ptr;
449 if((ConfigFileEntry.stats_h_oper_only ||
450 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
451 !IsOperRouting(source_p))
453 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
454 form_str (ERR_NOPRIVILEGES));
455 return;
458 DLINK_FOREACH(ptr, hubleaf_conf_list.head)
460 hub_p = ptr->data;
462 if(hub_p->flags & CONF_HUB)
463 sendto_one_numeric(source_p, RPL_STATSHLINE,
464 form_str(RPL_STATSHLINE),
465 hub_p->host, hub_p->server);
466 else
467 sendto_one_numeric(source_p, RPL_STATSLLINE,
468 form_str(RPL_STATSLLINE),
469 hub_p->host, hub_p->server);
474 static void
475 stats_auth (struct Client *source_p)
477 /* Oper only, if unopered, return ERR_NOPRIVS */
478 if((ConfigFileEntry.stats_i_oper_only == 2) && !IsOper (source_p))
479 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
480 form_str (ERR_NOPRIVILEGES));
482 /* If unopered, Only return matching auth blocks */
483 else if((ConfigFileEntry.stats_i_oper_only == 1) && !IsOper (source_p))
485 struct ConfItem *aconf;
486 char *name, *host, *pass, *user, *classname;
487 int port;
489 if(MyConnect (source_p))
490 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
491 (struct sockaddr *)&source_p->localClient->ip,
492 CONF_CLIENT,
493 source_p->localClient->ip.ss_family,
494 source_p->username);
495 else
496 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_CLIENT,
497 0, source_p->username);
499 if(aconf == NULL)
500 return;
502 get_printable_conf (aconf, &name, &host, &pass, &user, &port, &classname);
504 sendto_one_numeric(source_p, RPL_STATSILINE, form_str(RPL_STATSILINE),
505 name, show_iline_prefix(source_p, aconf, user),
506 host, port, classname);
509 /* Theyre opered, or allowed to see all auth blocks */
510 else
511 report_auth (source_p);
515 static void
516 stats_tklines(struct Client *source_p)
518 /* Oper only, if unopered, return ERR_NOPRIVS */
519 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
520 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
521 form_str (ERR_NOPRIVILEGES));
523 /* If unopered, Only return matching klines */
524 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
526 struct ConfItem *aconf;
527 char *host, *pass, *user, *oper_reason;
529 if(MyConnect (source_p))
530 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
531 (struct sockaddr *)&source_p->localClient->ip,
532 CONF_KILL,
533 source_p->localClient->ip.ss_family,
534 source_p->username);
535 else
536 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
537 0, source_p->username);
539 if(aconf == NULL)
540 return;
542 /* dont report a permanent kline as a tkline */
543 if((aconf->flags & CONF_FLAGS_TEMPORARY) == 0)
544 return;
546 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
548 sendto_one_numeric(source_p, RPL_STATSKLINE,
549 form_str(RPL_STATSKLINE), 'k',
550 user, pass, oper_reason ? "|" : "",
551 oper_reason ? oper_reason : "");
553 /* Theyre opered, or allowed to see all klines */
554 else
556 struct ConfItem *aconf;
557 dlink_node *ptr;
558 int i;
559 char *user, *host, *pass, *oper_reason;
561 for(i = 0; i < LAST_TEMP_TYPE; i++)
563 DLINK_FOREACH(ptr, temp_klines[i].head)
565 aconf = ptr->data;
567 get_printable_kline(source_p, aconf, &host, &pass,
568 &user, &oper_reason);
570 sendto_one_numeric(source_p, RPL_STATSKLINE,
571 form_str(RPL_STATSKLINE),
572 'k', host, user, pass,
573 oper_reason ? "|" : "",
574 oper_reason ? oper_reason : "");
580 static void
581 stats_klines(struct Client *source_p)
583 /* Oper only, if unopered, return ERR_NOPRIVS */
584 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
585 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
586 form_str (ERR_NOPRIVILEGES));
588 /* If unopered, Only return matching klines */
589 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
591 struct ConfItem *aconf;
592 char *host, *pass, *user, *oper_reason;
594 /* search for a kline */
595 if(MyConnect (source_p))
596 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
597 (struct sockaddr *)&source_p->localClient->ip,
598 CONF_KILL,
599 source_p->localClient->ip.ss_family,
600 source_p->username);
601 else
602 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
603 0, source_p->username);
605 if(aconf == NULL)
606 return;
608 /* dont report a tkline as a kline */
609 if(aconf->flags & CONF_FLAGS_TEMPORARY)
610 return;
612 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
614 sendto_one_numeric(source_p, RPL_STATSKLINE, form_str(RPL_STATSKLINE),
615 'K', host, user, pass, oper_reason ? "|" : "",
616 oper_reason ? oper_reason : "");
618 /* Theyre opered, or allowed to see all klines */
619 else
620 report_Klines (source_p);
623 static void
624 stats_messages(struct Client *source_p)
626 report_messages(source_p);
629 static void
630 stats_dnsbl(struct Client *source_p)
632 dlink_node *ptr;
633 struct Blacklist *blptr;
635 DLINK_FOREACH(ptr, blacklist_list.head)
637 blptr = ptr->data;
639 /* use RPL_STATSDEBUG for now -- jilles */
640 sendto_one_numeric(source_p, RPL_STATSDEBUG, "n :%d %s %s (%d)",
641 blptr->hits,
642 blptr->host,
643 blptr->status & CONF_ILLEGAL ? "disabled" : "active",
644 blptr->refcount);
648 static void
649 stats_oper(struct Client *source_p)
651 struct oper_conf *oper_p;
652 dlink_node *ptr;
654 if(!IsOperStaffer(source_p) && ConfigFileEntry.stats_o_oper_only)
656 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
657 form_str (ERR_NOPRIVILEGES));
658 return;
661 DLINK_FOREACH(ptr, oper_conf_list.head)
663 oper_p = ptr->data;
665 sendto_one_numeric(source_p, RPL_STATSOLINE,
666 form_str(RPL_STATSOLINE),
667 oper_p->username, oper_p->host, oper_p->name,
668 IsOperStaffer(source_p) ? get_oper_privs(oper_p->flags) : "0", "-1");
673 /* stats_operedup()
675 * input - client pointer
676 * output - none
677 * side effects - client is shown a list of active opers
679 static void
680 stats_operedup (struct Client *source_p)
682 struct Client *target_p;
683 dlink_node *oper_ptr;
684 unsigned int count = 0;
686 DLINK_FOREACH (oper_ptr, oper_list.head)
688 target_p = oper_ptr->data;
690 if(!IsHelper(target_p))
691 continue;
693 if(target_p->user->away)
694 continue;
696 count++;
698 sendto_one_numeric(source_p, RPL_STATSDEBUG,
699 "p :%s (%s@%s)",
700 target_p->name, target_p->username,
701 target_p->host);
704 sendto_one_numeric(source_p, RPL_STATSDEBUG,
705 "p :%u staff members", count);
708 static void
709 stats_ports (struct Client *source_p)
711 if(!IsOper (source_p) && ConfigFileEntry.stats_P_oper_only)
712 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
713 form_str (ERR_NOPRIVILEGES));
714 else
715 show_ports (source_p);
718 static void
719 stats_tresv(struct Client *source_p)
721 struct ConfItem *aconf;
722 dlink_node *ptr;
723 int i;
725 DLINK_FOREACH(ptr, resv_conf_list.head)
727 aconf = ptr->data;
728 if(aconf->hold)
729 sendto_one_numeric(source_p, RPL_STATSQLINE,
730 form_str(RPL_STATSQLINE),
731 'q', aconf->port, aconf->name, aconf->passwd);
734 HASH_WALK(i, R_MAX, ptr, resvTable)
736 aconf = ptr->data;
737 if(aconf->hold)
738 sendto_one_numeric(source_p, RPL_STATSQLINE,
739 form_str(RPL_STATSQLINE),
740 'q', aconf->port, aconf->name, aconf->passwd);
742 HASH_WALK_END
746 static void
747 stats_resv(struct Client *source_p)
749 struct ConfItem *aconf;
750 dlink_node *ptr;
751 int i;
753 DLINK_FOREACH(ptr, resv_conf_list.head)
755 aconf = ptr->data;
756 if(!aconf->hold)
757 sendto_one_numeric(source_p, RPL_STATSQLINE,
758 form_str(RPL_STATSQLINE),
759 'Q', aconf->port, aconf->name, aconf->passwd);
762 HASH_WALK(i, R_MAX, ptr, resvTable)
764 aconf = ptr->data;
765 if(!aconf->hold)
766 sendto_one_numeric(source_p, RPL_STATSQLINE,
767 form_str(RPL_STATSQLINE),
768 'Q', aconf->port, aconf->name, aconf->passwd);
770 HASH_WALK_END
773 static void
774 stats_usage (struct Client *source_p)
776 struct rusage rus;
777 time_t secs;
778 time_t rup;
779 #ifdef hz
780 # define hzz hz
781 #else
782 # ifdef HZ
783 # define hzz HZ
784 # else
785 int hzz = 1;
786 # endif
787 #endif
789 if(getrusage(RUSAGE_SELF, &rus) == -1)
791 sendto_one_notice(source_p, ":Getruseage error: %s.",
792 strerror(errno));
793 return;
795 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
796 if(0 == secs)
797 secs = 1;
799 rup = (CurrentTime - startup_time) * hzz;
800 if(0 == rup)
801 rup = 1;
803 sendto_one_numeric(source_p, RPL_STATSDEBUG,
804 "R :CPU Secs %d:%d User %d:%d System %d:%d",
805 (int) (secs / 60), (int) (secs % 60),
806 (int) (rus.ru_utime.tv_sec / 60),
807 (int) (rus.ru_utime.tv_sec % 60),
808 (int) (rus.ru_stime.tv_sec / 60),
809 (int) (rus.ru_stime.tv_sec % 60));
810 sendto_one_numeric(source_p, RPL_STATSDEBUG,
811 "R :RSS %ld ShMem %ld Data %ld Stack %ld",
812 rus.ru_maxrss, (rus.ru_ixrss / rup),
813 (rus.ru_idrss / rup), (rus.ru_isrss / rup));
814 sendto_one_numeric(source_p, RPL_STATSDEBUG,
815 "R :Swaps %d Reclaims %d Faults %d",
816 (int) rus.ru_nswap, (int) rus.ru_minflt, (int) rus.ru_majflt);
817 sendto_one_numeric(source_p, RPL_STATSDEBUG,
818 "R :Block in %d out %d",
819 (int) rus.ru_inblock, (int) rus.ru_oublock);
820 sendto_one_numeric(source_p, RPL_STATSDEBUG,
821 "R :Msg Rcv %d Send %d",
822 (int) rus.ru_msgrcv, (int) rus.ru_msgsnd);
823 sendto_one_numeric(source_p, RPL_STATSDEBUG,
824 "R :Signals %d Context Vol. %d Invol %d",
825 (int) rus.ru_nsignals, (int) rus.ru_nvcsw,
826 (int) rus.ru_nivcsw);
829 static void
830 stats_tstats (struct Client *source_p)
832 tstats (source_p);
835 static void
836 stats_uptime (struct Client *source_p)
838 time_t now;
840 now = CurrentTime - startup_time;
841 sendto_one_numeric(source_p, RPL_STATSUPTIME,
842 form_str (RPL_STATSUPTIME),
843 now / 86400, (now / 3600) % 24,
844 (now / 60) % 60, now % 60);
845 sendto_one_numeric(source_p, RPL_STATSCONN,
846 form_str (RPL_STATSCONN),
847 MaxConnectionCount, MaxClientCount,
848 Count.totalrestartcount);
851 struct shared_flags
853 int flag;
854 char letter;
856 static struct shared_flags shared_flagtable[] =
858 { SHARED_PKLINE, 'K' },
859 { SHARED_TKLINE, 'k' },
860 { SHARED_UNKLINE, 'U' },
861 { SHARED_PXLINE, 'X' },
862 { SHARED_TXLINE, 'x' },
863 { SHARED_UNXLINE, 'Y' },
864 { SHARED_PRESV, 'Q' },
865 { SHARED_TRESV, 'q' },
866 { SHARED_UNRESV, 'R' },
867 { SHARED_PDLINE, 'D' },
868 { SHARED_TDLINE, 'd' },
869 { SHARED_UNDLINE, 'E' },
870 { SHARED_REHASH, 'H' },
871 { 0, '\0'}
875 static void
876 stats_shared (struct Client *source_p)
878 struct remote_conf *shared_p;
879 dlink_node *ptr;
880 char buf[15];
881 char *p;
882 int i;
884 DLINK_FOREACH(ptr, shared_conf_list.head)
886 shared_p = ptr->data;
888 p = buf;
890 *p++ = 'c';
892 for(i = 0; shared_flagtable[i].flag != 0; i++)
894 if(shared_p->flags & shared_flagtable[i].flag)
895 *p++ = shared_flagtable[i].letter;
898 *p = '\0';
900 sendto_one_numeric(source_p, RPL_STATSULINE,
901 form_str(RPL_STATSULINE),
902 shared_p->server, shared_p->username,
903 shared_p->host, buf);
906 DLINK_FOREACH(ptr, cluster_conf_list.head)
908 shared_p = ptr->data;
910 p = buf;
912 *p++ = 'C';
914 for(i = 0; shared_flagtable[i].flag != 0; i++)
916 if(shared_p->flags & shared_flagtable[i].flag)
917 *p++ = shared_flagtable[i].letter;
920 *p = '\0';
922 sendto_one_numeric(source_p, RPL_STATSULINE,
923 form_str(RPL_STATSULINE),
924 shared_p->server, "*", "*", buf);
928 /* stats_servers()
930 * input - client pointer
931 * output - none
932 * side effects - client is shown lists of who connected servers
934 static void
935 stats_servers (struct Client *source_p)
937 struct Client *target_p;
938 dlink_node *ptr;
939 time_t seconds;
940 int days, hours, minutes;
941 int j = 0;
943 if(ConfigServerHide.flatten_links && !IsOperRouting(source_p) &&
944 !IsExemptShide(source_p))
946 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
947 form_str (ERR_NOPRIVILEGES));
948 return;
951 DLINK_FOREACH (ptr, serv_list.head)
953 target_p = ptr->data;
955 j++;
956 seconds = CurrentTime - target_p->localClient->firsttime;
958 days = (int) (seconds / 86400);
959 seconds %= 86400;
960 hours = (int) (seconds / 3600);
961 seconds %= 3600;
962 minutes = (int) (seconds / 60);
963 seconds %= 60;
965 sendto_one_numeric(source_p, RPL_STATSDEBUG,
966 "V :%s (%s!*@*) Idle: %d SendQ: %d "
967 "Connected: %d day%s, %d:%02d:%02d",
968 target_p->name,
969 (target_p->serv->by[0] ? target_p->serv->by : "Remote."),
970 (int) (CurrentTime - target_p->localClient->lasttime),
971 (int) linebuf_len (&target_p->localClient->buf_sendq),
972 days, (days == 1) ? "" : "s", hours, minutes,
973 (int) seconds);
976 sendto_one_numeric(source_p, RPL_STATSDEBUG,
977 "V :%d Server(s)", j);
980 static void
981 stats_tgecos(struct Client *source_p)
983 struct ConfItem *aconf;
984 dlink_node *ptr;
986 DLINK_FOREACH(ptr, xline_conf_list.head)
988 aconf = ptr->data;
990 if(aconf->hold)
991 sendto_one_numeric(source_p, RPL_STATSXLINE,
992 form_str(RPL_STATSXLINE),
993 'x', aconf->port, aconf->name,
994 aconf->passwd);
998 static void
999 stats_gecos(struct Client *source_p)
1001 struct ConfItem *aconf;
1002 dlink_node *ptr;
1004 DLINK_FOREACH(ptr, xline_conf_list.head)
1006 aconf = ptr->data;
1008 if(!aconf->hold)
1009 sendto_one_numeric(source_p, RPL_STATSXLINE,
1010 form_str(RPL_STATSXLINE),
1011 'X', aconf->port, aconf->name,
1012 aconf->passwd);
1016 static void
1017 stats_class(struct Client *source_p)
1019 if(ConfigFileEntry.stats_y_oper_only && !IsOperStaffer(source_p))
1020 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1021 form_str (ERR_NOPRIVILEGES));
1022 else
1023 report_classes(source_p);
1026 static void
1027 stats_memory (struct Client *source_p)
1029 count_memory (source_p);
1032 static void
1033 stats_ziplinks (struct Client *source_p)
1035 dlink_node *ptr;
1036 struct Client *target_p;
1037 int sent_data = 0;
1039 DLINK_FOREACH (ptr, serv_list.head)
1041 target_p = ptr->data;
1042 if(IsCapable (target_p, CAP_ZIP))
1044 /* we use memcpy(3) and a local copy of the structure to
1045 * work around a register use bug on GCC on the SPARC.
1046 * -jmallett, 04/27/2002
1048 struct ZipStats zipstats;
1049 memcpy (&zipstats, &target_p->localClient->zipstats,
1050 sizeof (struct ZipStats));
1051 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1052 "Z :ZipLinks stats for %s send[%.2f%% compression "
1053 "(%lu kB data/%lu kB wire)] recv[%.2f%% compression "
1054 "(%lu kB data/%lu kB wire)]",
1055 target_p->name,
1056 zipstats.out_ratio, zipstats.outK, zipstats.outK_wire,
1057 zipstats.in_ratio, zipstats.inK, zipstats.inK_wire);
1058 sent_data++;
1062 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1063 "Z :%u ziplink(s)", sent_data);
1066 static void
1067 stats_servlinks (struct Client *source_p)
1069 static char Sformat[] = ":%s %d %s %s %u %u %u %u %u :%u %u %s";
1070 long uptime, sendK, receiveK;
1071 struct Client *target_p;
1072 dlink_node *ptr;
1073 int j = 0;
1075 if(ConfigServerHide.flatten_links && !IsOper (source_p) &&
1076 !IsExemptShide(source_p))
1078 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1079 form_str (ERR_NOPRIVILEGES));
1080 return;
1083 sendK = receiveK = 0;
1085 DLINK_FOREACH (ptr, serv_list.head)
1087 target_p = ptr->data;
1089 j++;
1090 sendK += target_p->localClient->sendK;
1091 receiveK += target_p->localClient->receiveK;
1093 sendto_one(source_p, Sformat,
1094 get_id(&me, source_p), RPL_STATSLINKINFO, get_id(source_p, source_p),
1095 get_server_name(target_p, SHOW_IP),
1096 (int) linebuf_len (&target_p->localClient->buf_sendq),
1097 (int) target_p->localClient->sendM,
1098 (int) target_p->localClient->sendK,
1099 (int) target_p->localClient->receiveM,
1100 (int) target_p->localClient->receiveK,
1101 CurrentTime - target_p->localClient->firsttime,
1102 (CurrentTime > target_p->localClient->lasttime) ?
1103 (CurrentTime - target_p->localClient->lasttime) : 0,
1104 IsOper (source_p) ? show_capabilities (target_p) : "TS");
1107 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1108 "? :%u total server(s)", j);
1110 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1111 "? :Sent total : %7.2f %s",
1112 _GMKv (sendK), _GMKs (sendK));
1113 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1114 "? :Recv total : %7.2f %s",
1115 _GMKv (receiveK), _GMKs (receiveK));
1117 uptime = (CurrentTime - startup_time);
1119 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1120 "? :Server send: %7.2f %s (%4.1f K/s)",
1121 _GMKv (me.localClient->sendK),
1122 _GMKs (me.localClient->sendK),
1123 (float) ((float) me.localClient->sendK / (float) uptime));
1124 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1125 "? :Server recv: %7.2f %s (%4.1f K/s)",
1126 _GMKv (me.localClient->receiveK),
1127 _GMKs (me.localClient->receiveK),
1128 (float) ((float) me.localClient->receiveK / (float) uptime));
1131 static void
1132 stats_ltrace(struct Client *source_p, int parc, const char *parv[])
1134 int doall = 0;
1135 int wilds = 0;
1136 const char *name;
1137 char statchar = parv[1][0];
1139 /* this is def targeted at us somehow.. */
1140 if(parc > 2 && !EmptyString(parv[2]))
1142 /* directed at us generically? */
1143 if(match(parv[2], me.name) ||
1144 (!MyClient(source_p) && !irccmp(parv[2], me.id)))
1146 name = me.name;
1147 doall = 1;
1149 else
1151 name = parv[2];
1152 wilds = strchr(name, '*') || strchr(name, '?');
1155 /* must be directed at a specific person thats not us */
1156 if(!doall && !wilds)
1158 struct Client *target_p;
1160 if(MyClient(source_p))
1161 target_p = find_named_person(name);
1162 else
1163 target_p = find_person(name);
1165 if(target_p != NULL)
1167 stats_spy(source_p, statchar, target_p->name);
1168 stats_l_client(source_p, target_p, statchar);
1170 else
1171 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
1172 form_str(ERR_NOSUCHSERVER),
1173 name);
1175 return;
1178 else
1180 name = me.name;
1181 doall = 1;
1184 stats_spy(source_p, statchar, name);
1186 if(doall)
1188 /* local opers get everyone */
1189 if(MyOper(source_p))
1191 stats_l_list(source_p, name, doall, wilds, &unknown_list, statchar);
1192 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1194 else
1196 /* they still need themselves if theyre local.. */
1197 if(MyClient(source_p))
1198 stats_l_client(source_p, source_p, statchar);
1200 stats_l_list(source_p, name, doall, wilds, &local_oper_list, statchar);
1203 if (!ConfigServerHide.flatten_links || IsOperRouting(source_p) ||
1204 IsExemptShide(source_p))
1205 stats_l_list(source_p, name, doall, wilds, &serv_list, statchar);
1207 return;
1210 /* ok, at this point theyre looking for a specific client whos on
1211 * our server.. but it contains a wildcard. --fl
1213 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1215 return;
1219 static void
1220 stats_l_list(struct Client *source_p, const char *name, int doall, int wilds,
1221 dlink_list * list, char statchar)
1223 dlink_node *ptr;
1224 struct Client *target_p;
1226 /* send information about connections which match. note, we
1227 * dont need tests for IsInvisible(), because non-opers will
1228 * never get here for normal clients --fl
1230 DLINK_FOREACH(ptr, list->head)
1232 target_p = ptr->data;
1234 if(!doall && wilds && !match(name, target_p->name))
1235 continue;
1237 stats_l_client(source_p, target_p, statchar);
1241 void
1242 stats_l_client(struct Client *source_p, struct Client *target_p,
1243 char statchar)
1245 if(IsAnyServer(target_p))
1247 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1248 get_server_name(target_p, SHOW_IP),
1249 (int) linebuf_len(&target_p->localClient->buf_sendq),
1250 (int) target_p->localClient->sendM,
1251 (int) target_p->localClient->sendK,
1252 (int) target_p->localClient->receiveM,
1253 (int) target_p->localClient->receiveK,
1254 CurrentTime - target_p->localClient->firsttime,
1255 (CurrentTime > target_p->localClient->lasttime) ?
1256 (CurrentTime - target_p->localClient->lasttime) : 0,
1257 IsOperStaffer(source_p) ? show_capabilities(target_p) : "-");
1260 else
1262 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1263 show_ip(source_p, target_p) ?
1264 (IsUpper(statchar) ?
1265 get_client_name(target_p, SHOW_IP) :
1266 get_client_name(target_p, HIDE_IP)) :
1267 get_client_name(target_p, MASK_IP),
1268 (int) linebuf_len(&target_p->localClient->buf_sendq),
1269 (int) target_p->localClient->sendM,
1270 (int) target_p->localClient->sendK,
1271 (int) target_p->localClient->receiveM,
1272 (int) target_p->localClient->receiveK,
1273 CurrentTime - target_p->localClient->firsttime,
1274 (CurrentTime > target_p->localClient->lasttime) ?
1275 (CurrentTime - target_p->localClient->lasttime) : 0,
1276 "-");
1281 * stats_spy
1283 * inputs - pointer to client doing the /stats
1284 * - char letter they are doing /stats on
1285 * output - none
1286 * side effects -
1287 * This little helper function reports to opers if configured.
1288 * personally, I don't see why opers need to see stats requests
1289 * at all. They are just "noise" to an oper, and users can't do
1290 * any damage with stats requests now anyway. So, why show them?
1291 * -Dianora
1293 static void
1294 stats_spy(struct Client *source_p, char statchar, const char *name)
1296 hook_data_int data;
1298 data.client = source_p;
1299 data.arg1 = name;
1300 data.arg2 = (int) statchar;
1302 call_hook(doing_stats_hook, &data);