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