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