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