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