]> jfr.im git - solanum.git/blob - modules/m_stats.c
Add /stats g to show propagated (global) klines.
[solanum.git] / modules / m_stats.c
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"
28 #include "class.h" /* report_classes */
29 #include "client.h" /* Client */
30 #include "common.h" /* TRUE/FALSE */
31 #include "match.h"
32 #include "ircd.h" /* me */
33 #include "listener.h" /* show_ports */
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 */
39 #include "s_conf.h" /* ConfItem */
40 #include "s_serv.h" /* hunt_server */
41 #include "s_stats.h"
42 #include "s_user.h" /* show_opers */
43 #include "blacklist.h" /* dnsbl stuff */
44 #include "parse.h"
45 #include "modules.h"
46 #include "hook.h"
47 #include "s_newconf.h"
48 #include "hash.h"
49 #include "reject.h"
50 #include "whowas.h"
51
52 static int m_stats (struct Client *, struct Client *, int, const char **);
53
54 struct Message stats_msgtab = {
55 "STATS", 0, 0, 0, MFLG_SLOW,
56 {mg_unreg, {m_stats, 2}, {m_stats, 3}, mg_ignore, mg_ignore, {m_stats, 2}}
57 };
58
59 int doing_stats_hook;
60 int doing_stats_p_hook;
61
62 mapi_clist_av1 stats_clist[] = { &stats_msgtab, NULL };
63 mapi_hlist_av1 stats_hlist[] = {
64 { "doing_stats", &doing_stats_hook },
65 { "doing_stats_p", &doing_stats_p_hook },
66 { NULL, NULL }
67 };
68
69 DECLARE_MODULE_AV1(stats, NULL, NULL, stats_clist, stats_hlist, NULL, "$Revision: 1608 $");
70
71 const char *Lformat = "%s %u %u %u %u %u :%u %u %s";
72
73 static void stats_l_list(struct Client *s, const char *, int, int, rb_dlink_list *, char);
74 static void stats_l_client(struct Client *source_p, struct Client *target_p,
75 char statchar);
76
77 static void stats_spy(struct Client *, char, const char *);
78 static void stats_p_spy(struct Client *);
79
80 /* Heres our struct for the stats table */
81 struct StatsStruct
82 {
83 char letter;
84 void (*handler) (struct Client *source_p);
85 int need_oper;
86 int need_admin;
87 };
88
89 static void stats_dns_servers(struct Client *);
90 static void stats_delay(struct Client *);
91 static void stats_hash(struct Client *);
92 static void stats_connect(struct Client *);
93 static void stats_tdeny(struct Client *);
94 static void stats_deny(struct Client *);
95 static void stats_exempt(struct Client *);
96 static void stats_events(struct Client *);
97 static void stats_prop_klines(struct Client *);
98 static void stats_hubleaf(struct Client *);
99 static void stats_auth(struct Client *);
100 static void stats_tklines(struct Client *);
101 static void stats_klines(struct Client *);
102 static void stats_messages(struct Client *);
103 static void stats_dnsbl(struct Client *);
104 static void stats_oper(struct Client *);
105 static void stats_privset(struct Client *);
106 static void stats_operedup(struct Client *);
107 static void stats_ports(struct Client *);
108 static void stats_tresv(struct Client *);
109 static void stats_resv(struct Client *);
110 static void stats_usage(struct Client *);
111 static void stats_tstats(struct Client *);
112 static void stats_uptime(struct Client *);
113 static void stats_shared(struct Client *);
114 static void stats_servers(struct Client *);
115 static void stats_tgecos(struct Client *);
116 static void stats_gecos(struct Client *);
117 static void stats_class(struct Client *);
118 static void stats_memory(struct Client *);
119 static void stats_servlinks(struct Client *);
120 static void stats_ltrace(struct Client *, int, const char **);
121 static void stats_ziplinks(struct Client *);
122 static void stats_comm(struct Client *);
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 */
127 static 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, },
139 {'f', stats_comm, 1, 1, },
140 {'F', stats_comm, 1, 1, },
141 {'g', stats_prop_klines, 1, 0, },
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, },
148 {'l', NULL /* special */, 0, 0, },
149 {'L', NULL /* special */, 0, 0, },
150 {'m', stats_messages, 0, 0, },
151 {'M', stats_messages, 0, 0, },
152 {'n', stats_dnsbl, 0, 0, },
153 {'o', stats_oper, 0, 0, },
154 {'O', stats_privset, 1, 0, },
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_
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 */
185 static int
186 m_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 */
197 if((last_used + ConfigFileEntry.pace_wait) > rb_current_time())
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
207 last_used = rb_current_time();
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
216 for (i = 0; stats_cmd_table[i].letter; i++)
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 */
224 if(stats_cmd_table[i].need_oper && !IsOper(source_p))
225 {
226 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
227 form_str (ERR_NOPRIVILEGES));
228 break;
229 }
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 }
236
237 /* Blah, stats L needs the parameters, none of the others do.. */
238 if(statchar == 'L' || statchar == 'l')
239 stats_ltrace (source_p, parc, parv);
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
252 static void
253 stats_dns_servers (struct Client *source_p)
254 {
255 report_dns_servers (source_p);
256 }
257
258 static void
259 stats_delay(struct Client *source_p)
260 {
261 struct nd_entry *nd;
262 struct DictionaryIter iter;
263
264 DICTIONARY_FOREACH(nd, &iter, nd_dict)
265 {
266 sendto_one_notice(source_p, ":Delaying: %s for %ld",
267 nd->name, (long) nd->expire);
268 }
269 }
270
271 static void
272 stats_hash(struct Client *source_p)
273 {
274 hash_stats(source_p);
275 }
276
277 static void
278 stats_connect(struct Client *source_p)
279 {
280 static char buf[5];
281 struct server_conf *server_p;
282 char *s;
283 rb_dlink_node *ptr;
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
294 RB_DLINK_FOREACH(ptr, server_conf_list.head)
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';
308 if(ServerConfSSL(server_p))
309 *s++ = 'S';
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),
323 "*@127.0.0.1",
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 */
335 static void
336 stats_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 */
372 static void
373 stats_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 */
410 static void
411 stats_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 {
420 sendto_one_numeric(source_p, ERR_DISABLED,
421 form_str(ERR_DISABLED), "STATS e");
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
443 static void
444 stats_events_cb(char *str, void *ptr)
445 {
446 sendto_one_numeric(ptr, RPL_STATSDEBUG, "E :%s", str);
447 }
448
449 static void
450 stats_events (struct Client *source_p)
451 {
452 rb_dump_events(stats_events_cb, source_p);
453 }
454
455 static void
456 stats_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
481 static void
482 stats_hubleaf(struct Client *source_p)
483 {
484 struct remote_conf *hub_p;
485 rb_dlink_node *ptr;
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
496 RB_DLINK_FOREACH(ptr, hubleaf_conf_list.head)
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
512 static void
513 stats_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;
524 char *name, *host, *pass = "*", *user, *classname;
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,
532 source_p->username, NULL);
533 else
534 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_CLIENT,
535 0, source_p->username, NULL);
536
537 if(aconf == NULL)
538 return;
539
540 get_printable_conf (aconf, &name, &host, &pass, &user, &port, &classname);
541 if(!EmptyString(aconf->spasswd))
542 pass = aconf->spasswd;
543
544 sendto_one_numeric(source_p, RPL_STATSILINE, form_str(RPL_STATSILINE),
545 name, pass, show_iline_prefix(source_p, aconf, user),
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
555 static void
556 stats_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,
574 source_p->username, NULL);
575 else
576 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
577 0, source_p->username, NULL);
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,
589 form_str(RPL_STATSKLINE), aconf->flags & CONF_FLAGS_TEMPORARY ? 'k' : 'K',
590 host, user, pass, oper_reason ? "|" : "",
591 oper_reason ? oper_reason : "");
592 }
593 /* Theyre opered, or allowed to see all klines */
594 else
595 {
596 struct ConfItem *aconf;
597 rb_dlink_node *ptr;
598 int i;
599 char *user, *host, *pass, *oper_reason;
600
601 for(i = 0; i < LAST_TEMP_TYPE; i++)
602 {
603 RB_DLINK_FOREACH(ptr, temp_klines[i].head)
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
620 static void
621 stats_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,
640 source_p->username, NULL);
641 else
642 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
643 0, source_p->username, NULL);
644
645 if(aconf == NULL)
646 return;
647
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),
651 aconf->flags & CONF_FLAGS_TEMPORARY ? 'k' : 'K',
652 host, user, pass, oper_reason ? "|" : "",
653 oper_reason ? oper_reason : "");
654 }
655 /* Theyre opered, or allowed to see all klines */
656 else
657 report_Klines (source_p);
658 }
659
660 static void
661 stats_messages(struct Client *source_p)
662 {
663 report_messages(source_p);
664 }
665
666 static void
667 stats_dnsbl(struct Client *source_p)
668 {
669 rb_dlink_node *ptr;
670 struct Blacklist *blptr;
671
672 RB_DLINK_FOREACH(ptr, blacklist_list.head)
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
685 static void
686 stats_oper(struct Client *source_p)
687 {
688 struct oper_conf *oper_p;
689 rb_dlink_node *ptr;
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
698 RB_DLINK_FOREACH(ptr, oper_conf_list.head)
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,
705 IsOper(source_p) ? oper_p->privset->name : "0", "-1");
706 }
707 }
708
709 static void
710 stats_privset(struct Client *source_p)
711 {
712 privilegeset_report(source_p);
713 }
714
715 /* stats_operedup()
716 *
717 * input - client pointer
718 * output - none
719 * side effects - client is shown a list of active opers
720 */
721 static void
722 stats_operedup (struct Client *source_p)
723 {
724 struct Client *target_p;
725 rb_dlink_node *oper_ptr;
726 unsigned int count = 0;
727
728 RB_DLINK_FOREACH (oper_ptr, oper_list.head)
729 {
730 target_p = oper_ptr->data;
731
732 if(IsOperInvis(target_p) && !IsOper(source_p))
733 continue;
734
735 if(target_p->user->away)
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
752 static void
753 stats_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
762 static void
763 stats_tresv(struct Client *source_p)
764 {
765 struct ConfItem *aconf;
766 rb_dlink_node *ptr;
767 int i;
768
769 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
770 {
771 aconf = ptr->data;
772 if(aconf->hold)
773 sendto_one_numeric(source_p, RPL_STATSQLINE,
774 form_str(RPL_STATSQLINE),
775 'q', aconf->port, aconf->host, aconf->passwd);
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),
784 'q', aconf->port, aconf->host, aconf->passwd);
785 }
786 HASH_WALK_END
787 }
788
789
790 static void
791 stats_resv(struct Client *source_p)
792 {
793 struct ConfItem *aconf;
794 rb_dlink_node *ptr;
795 int i;
796
797 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
798 {
799 aconf = ptr->data;
800 if(!aconf->hold)
801 sendto_one_numeric(source_p, RPL_STATSQLINE,
802 form_str(RPL_STATSQLINE),
803 'Q', aconf->port, aconf->host, aconf->passwd);
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),
812 'Q', aconf->port, aconf->host, aconf->passwd);
813 }
814 HASH_WALK_END
815 }
816
817 static void
818 stats_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
843 rup = (rb_current_time() - startup_time) * hzz;
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
873 static void
874 stats_tstats (struct Client *source_p)
875 {
876 struct Client *target_p;
877 struct ServerStatistics sp;
878 rb_dlink_node *ptr;
879
880 memcpy(&sp, &ServerStats, sizeof(struct ServerStatistics));
881
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;
888 sp.is_sti += (unsigned long long)(rb_current_time() - target_p->localClient->firsttime);
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;
898 sp.is_cti += (unsigned long long)(rb_current_time() - target_p->localClient->firsttime);
899 sp.is_cl++;
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",
911 sp.is_rej, delay_exit_length());
912 sendto_one_numeric(source_p, RPL_STATSDEBUG,
913 "T :throttled refused %u throttle list size %lu", sp.is_thr, throttle_size());
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);
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,
947 sp.is_sbr / 1024);
948 sendto_one_numeric(source_p, RPL_STATSDEBUG,
949 "T :time connected %llu %llu",
950 sp.is_cti, sp.is_sti);
951 }
952
953 static void
954 stats_uptime (struct Client *source_p)
955 {
956 time_t now;
957
958 now = rb_current_time() - startup_time;
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
969 struct shared_flags
970 {
971 int flag;
972 char letter;
973 };
974 static 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' },
987 { SHARED_TDLINE, 'd' },
988 { SHARED_PDLINE, 'D' },
989 { SHARED_UNDLINE, 'E' },
990 { 0, '\0'}
991 };
992
993
994 static void
995 stats_shared (struct Client *source_p)
996 {
997 struct remote_conf *shared_p;
998 rb_dlink_node *ptr;
999 char buf[sizeof(shared_flagtable)/sizeof(shared_flagtable[0])];
1000 char *p;
1001 int i;
1002
1003 RB_DLINK_FOREACH(ptr, shared_conf_list.head)
1004 {
1005 shared_p = ptr->data;
1006
1007 p = buf;
1008
1009 *p++ = 'c';
1010
1011 for(i = 0; shared_flagtable[i].flag != 0; i++)
1012 {
1013 if(shared_p->flags & shared_flagtable[i].flag)
1014 *p++ = shared_flagtable[i].letter;
1015 }
1016
1017 *p = '\0';
1018
1019 sendto_one_numeric(source_p, RPL_STATSULINE,
1020 form_str(RPL_STATSULINE),
1021 shared_p->server, shared_p->username,
1022 shared_p->host, buf);
1023 }
1024
1025 RB_DLINK_FOREACH(ptr, cluster_conf_list.head)
1026 {
1027 shared_p = ptr->data;
1028
1029 p = buf;
1030
1031 *p++ = 'C';
1032
1033 for(i = 0; shared_flagtable[i].flag != 0; i++)
1034 {
1035 if(shared_p->flags & shared_flagtable[i].flag)
1036 *p++ = shared_flagtable[i].letter;
1037 }
1038
1039 *p = '\0';
1040
1041 sendto_one_numeric(source_p, RPL_STATSULINE,
1042 form_str(RPL_STATSULINE),
1043 shared_p->server, "*", "*", buf);
1044 }
1045 }
1046
1047 /* stats_servers()
1048 *
1049 * input - client pointer
1050 * output - none
1051 * side effects - client is shown lists of who connected servers
1052 */
1053 static void
1054 stats_servers (struct Client *source_p)
1055 {
1056 struct Client *target_p;
1057 rb_dlink_node *ptr;
1058 time_t seconds;
1059 int days, hours, minutes;
1060 int j = 0;
1061
1062 if(ConfigServerHide.flatten_links && !IsOper(source_p) &&
1063 !IsExemptShide(source_p))
1064 {
1065 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1066 form_str (ERR_NOPRIVILEGES));
1067 return;
1068 }
1069
1070 RB_DLINK_FOREACH (ptr, serv_list.head)
1071 {
1072 target_p = ptr->data;
1073
1074 j++;
1075 seconds = rb_current_time() - target_p->localClient->firsttime;
1076
1077 days = (int) (seconds / 86400);
1078 seconds %= 86400;
1079 hours = (int) (seconds / 3600);
1080 seconds %= 3600;
1081 minutes = (int) (seconds / 60);
1082 seconds %= 60;
1083
1084 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1085 "V :%s (%s!*@*) Idle: %d SendQ: %d "
1086 "Connected: %d day%s, %d:%02d:%02d",
1087 target_p->name,
1088 (target_p->serv->by[0] ? target_p->serv->by : "Remote."),
1089 (int) (rb_current_time() - target_p->localClient->lasttime),
1090 (int) rb_linebuf_len (&target_p->localClient->buf_sendq),
1091 days, (days == 1) ? "" : "s", hours, minutes,
1092 (int) seconds);
1093 }
1094
1095 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1096 "V :%d Server(s)", j);
1097 }
1098
1099 static void
1100 stats_tgecos(struct Client *source_p)
1101 {
1102 struct ConfItem *aconf;
1103 rb_dlink_node *ptr;
1104
1105 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
1106 {
1107 aconf = ptr->data;
1108
1109 if(aconf->hold)
1110 sendto_one_numeric(source_p, RPL_STATSXLINE,
1111 form_str(RPL_STATSXLINE),
1112 'x', aconf->port, aconf->host,
1113 aconf->passwd);
1114 }
1115 }
1116
1117 static void
1118 stats_gecos(struct Client *source_p)
1119 {
1120 struct ConfItem *aconf;
1121 rb_dlink_node *ptr;
1122
1123 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
1124 {
1125 aconf = ptr->data;
1126
1127 if(!aconf->hold)
1128 sendto_one_numeric(source_p, RPL_STATSXLINE,
1129 form_str(RPL_STATSXLINE),
1130 'X', aconf->port, aconf->host,
1131 aconf->passwd);
1132 }
1133 }
1134
1135 static void
1136 stats_class(struct Client *source_p)
1137 {
1138 if(ConfigFileEntry.stats_y_oper_only && !IsOper(source_p))
1139 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1140 form_str (ERR_NOPRIVILEGES));
1141 else
1142 report_classes(source_p);
1143 }
1144
1145 static void
1146 stats_memory (struct Client *source_p)
1147 {
1148 struct Client *target_p;
1149 struct Channel *chptr;
1150 struct Ban *actualBan;
1151 rb_dlink_node *rb_dlink;
1152 rb_dlink_node *ptr;
1153 int channel_count = 0;
1154 int local_client_conf_count = 0; /* local client conf links */
1155 int users_counted = 0; /* user structs */
1156
1157 int channel_users = 0;
1158 int channel_invites = 0;
1159 int channel_bans = 0;
1160 int channel_except = 0;
1161 int channel_invex = 0;
1162 int channel_quiets = 0;
1163
1164 int class_count = 0; /* classes */
1165 int conf_count = 0; /* conf lines */
1166 int users_invited_count = 0; /* users invited */
1167 int user_channels = 0; /* users in channels */
1168 int aways_counted = 0;
1169 size_t number_servers_cached; /* number of servers cached by scache */
1170
1171 size_t channel_memory = 0;
1172 size_t channel_ban_memory = 0;
1173 size_t channel_except_memory = 0;
1174 size_t channel_invex_memory = 0;
1175 size_t channel_quiet_memory = 0;
1176
1177 size_t away_memory = 0; /* memory used by aways */
1178 size_t ww = 0; /* whowas array count */
1179 size_t wwm = 0; /* whowas array memory used */
1180 size_t conf_memory = 0; /* memory used by conf lines */
1181 size_t mem_servers_cached; /* memory used by scache */
1182
1183 size_t linebuf_count = 0;
1184 size_t linebuf_memory_used = 0;
1185
1186 size_t total_channel_memory = 0;
1187 size_t totww = 0;
1188
1189 size_t local_client_count = 0;
1190 size_t local_client_memory_used = 0;
1191
1192 size_t remote_client_count = 0;
1193 size_t remote_client_memory_used = 0;
1194
1195 size_t total_memory = 0;
1196
1197 count_whowas_memory(&ww, &wwm);
1198
1199 RB_DLINK_FOREACH(ptr, global_client_list.head)
1200 {
1201 target_p = ptr->data;
1202 if(MyConnect(target_p))
1203 {
1204 local_client_conf_count++;
1205 }
1206
1207 if(target_p->user)
1208 {
1209 users_counted++;
1210 users_invited_count += rb_dlink_list_length(&target_p->user->invited);
1211 user_channels += rb_dlink_list_length(&target_p->user->channel);
1212 if(target_p->user->away)
1213 {
1214 aways_counted++;
1215 away_memory += (strlen(target_p->user->away) + 1);
1216 }
1217 }
1218 }
1219
1220 /* Count up all channels, ban lists, except lists, Invex lists */
1221 RB_DLINK_FOREACH(ptr, global_channel_list.head)
1222 {
1223 chptr = ptr->data;
1224 channel_count++;
1225 channel_memory += (strlen(chptr->chname) + sizeof(struct Channel));
1226
1227 channel_users += rb_dlink_list_length(&chptr->members);
1228 channel_invites += rb_dlink_list_length(&chptr->invites);
1229
1230 RB_DLINK_FOREACH(rb_dlink, chptr->banlist.head)
1231 {
1232 actualBan = rb_dlink->data;
1233 channel_bans++;
1234
1235 channel_ban_memory += sizeof(rb_dlink_node) + sizeof(struct Ban);
1236 }
1237
1238 RB_DLINK_FOREACH(rb_dlink, chptr->exceptlist.head)
1239 {
1240 actualBan = rb_dlink->data;
1241 channel_except++;
1242
1243 channel_except_memory += (sizeof(rb_dlink_node) + sizeof(struct Ban));
1244 }
1245
1246 RB_DLINK_FOREACH(rb_dlink, chptr->invexlist.head)
1247 {
1248 actualBan = rb_dlink->data;
1249 channel_invex++;
1250
1251 channel_invex_memory += (sizeof(rb_dlink_node) + sizeof(struct Ban));
1252 }
1253
1254 RB_DLINK_FOREACH(rb_dlink, chptr->quietlist.head)
1255 {
1256 actualBan = rb_dlink->data;
1257 channel_quiets++;
1258
1259 channel_quiet_memory += (sizeof(rb_dlink_node) + sizeof(struct Ban));
1260 }
1261 }
1262
1263 /* count up all classes */
1264
1265 class_count = rb_dlink_list_length(&class_list) + 1;
1266
1267 rb_count_rb_linebuf_memory(&linebuf_count, &linebuf_memory_used);
1268
1269 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1270 "z :Users %u(%lu) Invites %u(%lu)",
1271 users_counted,
1272 (unsigned long) users_counted * sizeof(struct User),
1273 users_invited_count,
1274 (unsigned long) users_invited_count * sizeof(rb_dlink_node));
1275
1276 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1277 "z :User channels %u(%lu) Aways %u(%d)",
1278 user_channels,
1279 (unsigned long) user_channels * sizeof(rb_dlink_node),
1280 aways_counted, (int) away_memory);
1281
1282 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1283 "z :Attached confs %u(%lu)",
1284 local_client_conf_count,
1285 (unsigned long) local_client_conf_count * sizeof(rb_dlink_node));
1286
1287 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1288 "z :Conflines %u(%d)", conf_count, (int) conf_memory);
1289
1290 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1291 "z :Classes %u(%lu)",
1292 class_count,
1293 (unsigned long) class_count * sizeof(struct Class));
1294
1295 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1296 "z :Channels %u(%d)",
1297 channel_count, (int) channel_memory);
1298
1299 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1300 "z :Bans %u(%d) Exceptions %u(%d) Invex %u(%d) Quiets %u(%d)",
1301 channel_bans, (int) channel_ban_memory,
1302 channel_except, (int) channel_except_memory,
1303 channel_invex, (int) channel_invex_memory,
1304 channel_quiets, (int) channel_quiet_memory);
1305
1306 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1307 "z :Channel members %u(%lu) invite %u(%lu)",
1308 channel_users,
1309 (unsigned long) channel_users * sizeof(rb_dlink_node),
1310 channel_invites,
1311 (unsigned long) channel_invites * sizeof(rb_dlink_node));
1312
1313 total_channel_memory = channel_memory +
1314 channel_ban_memory +
1315 channel_users * sizeof(rb_dlink_node) + channel_invites * sizeof(rb_dlink_node);
1316
1317 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1318 "z :Whowas array %ld(%ld)",
1319 (long)ww, (long)wwm);
1320
1321 totww = wwm;
1322
1323 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1324 "z :Hash: client %u(%ld) chan %u(%ld)",
1325 U_MAX, (long)(U_MAX * sizeof(rb_dlink_list)),
1326 CH_MAX, (long)(CH_MAX * sizeof(rb_dlink_list)));
1327
1328 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1329 "z :linebuf %ld(%ld)",
1330 (long)linebuf_count, (long)linebuf_memory_used);
1331
1332 count_scache(&number_servers_cached, &mem_servers_cached);
1333
1334 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1335 "z :scache %ld(%ld)",
1336 (long)number_servers_cached, (long)mem_servers_cached);
1337
1338 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1339 "z :hostname hash %d(%ld)",
1340 HOST_MAX, (long)HOST_MAX * sizeof(rb_dlink_list));
1341
1342 total_memory = totww + total_channel_memory + conf_memory +
1343 class_count * sizeof(struct Class);
1344
1345 total_memory += mem_servers_cached;
1346 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1347 "z :Total: whowas %d channel %d conf %d",
1348 (int) totww, (int) total_channel_memory,
1349 (int) conf_memory);
1350
1351 count_local_client_memory(&local_client_count, &local_client_memory_used);
1352 total_memory += local_client_memory_used;
1353
1354 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1355 "z :Local client Memory in use: %ld(%ld)",
1356 (long)local_client_count, (long)local_client_memory_used);
1357
1358
1359 count_remote_client_memory(&remote_client_count, &remote_client_memory_used);
1360 total_memory += remote_client_memory_used;
1361
1362 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1363 "z :Remote client Memory in use: %ld(%ld)",
1364 (long)remote_client_count,
1365 (long)remote_client_memory_used);
1366 }
1367
1368 static void
1369 stats_ziplinks (struct Client *source_p)
1370 {
1371 rb_dlink_node *ptr;
1372 struct Client *target_p;
1373 struct ZipStats *zipstats;
1374 int sent_data = 0;
1375 char buf[128], buf1[128];
1376 RB_DLINK_FOREACH (ptr, serv_list.head)
1377 {
1378 target_p = ptr->data;
1379 if(IsCapable (target_p, CAP_ZIP))
1380 {
1381 zipstats = target_p->localClient->zipstats;
1382 sprintf(buf, "%.2f%%", zipstats->out_ratio);
1383 sprintf(buf1, "%.2f%%", zipstats->in_ratio);
1384 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1385 "Z :ZipLinks stats for %s send[%s compression "
1386 "(%llu kB data/%llu kB wire)] recv[%s compression "
1387 "(%llu kB data/%llu kB wire)]",
1388 target_p->name,
1389 buf, zipstats->out >> 10,
1390 zipstats->out_wire >> 10, buf1,
1391 zipstats->in >> 10, zipstats->in_wire >> 10);
1392 sent_data++;
1393 }
1394 }
1395
1396 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1397 "Z :%u ziplink(s)", sent_data);
1398 }
1399
1400 static void
1401 stats_servlinks (struct Client *source_p)
1402 {
1403 static char Sformat[] = ":%s %d %s %s %u %u %u %u %u :%u %u %s";
1404 long uptime, sendK, receiveK;
1405 struct Client *target_p;
1406 rb_dlink_node *ptr;
1407 int j = 0;
1408 char buf[128];
1409
1410 if(ConfigServerHide.flatten_links && !IsOper (source_p) &&
1411 !IsExemptShide(source_p))
1412 {
1413 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1414 form_str (ERR_NOPRIVILEGES));
1415 return;
1416 }
1417
1418 sendK = receiveK = 0;
1419
1420 RB_DLINK_FOREACH (ptr, serv_list.head)
1421 {
1422 target_p = ptr->data;
1423
1424 j++;
1425 sendK += target_p->localClient->sendK;
1426 receiveK += target_p->localClient->receiveK;
1427
1428 sendto_one(source_p, Sformat,
1429 get_id(&me, source_p), RPL_STATSLINKINFO, get_id(source_p, source_p),
1430 target_p->name,
1431 (int) rb_linebuf_len (&target_p->localClient->buf_sendq),
1432 (int) target_p->localClient->sendM,
1433 (int) target_p->localClient->sendK,
1434 (int) target_p->localClient->receiveM,
1435 (int) target_p->localClient->receiveK,
1436 rb_current_time() - target_p->localClient->firsttime,
1437 (rb_current_time() > target_p->localClient->lasttime) ?
1438 (rb_current_time() - target_p->localClient->lasttime) : 0,
1439 IsOper (source_p) ? show_capabilities (target_p) : "TS");
1440 }
1441
1442 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1443 "? :%u total server(s)", j);
1444
1445 snprintf(buf, sizeof buf, "%7.2f", _GMKv ((sendK)));
1446 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1447 "? :Sent total : %s %s",
1448 buf, _GMKs (sendK));
1449 snprintf(buf, sizeof buf, "%7.2f", _GMKv ((receiveK)));
1450 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1451 "? :Recv total : %s %s",
1452 buf, _GMKs (receiveK));
1453
1454 uptime = (rb_current_time() - startup_time);
1455 snprintf(buf, sizeof buf, "%7.2f %s (%4.1f K/s)",
1456 _GMKv (me.localClient->sendK),
1457 _GMKs (me.localClient->sendK),
1458 (float) ((float) me.localClient->sendK / (float) uptime));
1459 sendto_one_numeric(source_p, RPL_STATSDEBUG, "? :Server send: %s", buf);
1460 snprintf(buf, sizeof buf, "%7.2f %s (%4.1f K/s)",
1461 _GMKv (me.localClient->receiveK),
1462 _GMKs (me.localClient->receiveK),
1463 (float) ((float) me.localClient->receiveK / (float) uptime));
1464 sendto_one_numeric(source_p, RPL_STATSDEBUG, "? :Server recv: %s", buf);
1465 }
1466
1467 static void
1468 stats_ltrace(struct Client *source_p, int parc, const char *parv[])
1469 {
1470 int doall = 0;
1471 int wilds = 0;
1472 const char *name;
1473 char statchar = parv[1][0];
1474
1475 /* this is def targeted at us somehow.. */
1476 if(parc > 2 && !EmptyString(parv[2]))
1477 {
1478 /* directed at us generically? */
1479 if(match(parv[2], me.name) ||
1480 (!MyClient(source_p) && !irccmp(parv[2], me.id)))
1481 {
1482 name = me.name;
1483 doall = 1;
1484 }
1485 else
1486 {
1487 name = parv[2];
1488 wilds = strchr(name, '*') || strchr(name, '?');
1489 }
1490
1491 /* must be directed at a specific person thats not us */
1492 if(!doall && !wilds)
1493 {
1494 struct Client *target_p;
1495
1496 if(MyClient(source_p))
1497 target_p = find_named_person(name);
1498 else
1499 target_p = find_person(name);
1500
1501 if(target_p != NULL)
1502 {
1503 stats_spy(source_p, statchar, target_p->name);
1504 stats_l_client(source_p, target_p, statchar);
1505 }
1506 else
1507 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
1508 form_str(ERR_NOSUCHSERVER),
1509 name);
1510
1511 return;
1512 }
1513 }
1514 else
1515 {
1516 name = me.name;
1517 doall = 1;
1518 }
1519
1520 stats_spy(source_p, statchar, name);
1521
1522 if(doall)
1523 {
1524 /* local opers get everyone */
1525 if(MyOper(source_p))
1526 {
1527 stats_l_list(source_p, name, doall, wilds, &unknown_list, statchar);
1528 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1529 }
1530 else
1531 {
1532 /* they still need themselves if theyre local.. */
1533 if(MyClient(source_p))
1534 stats_l_client(source_p, source_p, statchar);
1535
1536 stats_l_list(source_p, name, doall, wilds, &local_oper_list, statchar);
1537 }
1538
1539 if (!ConfigServerHide.flatten_links || IsOper(source_p) ||
1540 IsExemptShide(source_p))
1541 stats_l_list(source_p, name, doall, wilds, &serv_list, statchar);
1542
1543 return;
1544 }
1545
1546 /* ok, at this point theyre looking for a specific client whos on
1547 * our server.. but it contains a wildcard. --fl
1548 */
1549 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1550
1551 return;
1552 }
1553
1554
1555 static void
1556 stats_l_list(struct Client *source_p, const char *name, int doall, int wilds,
1557 rb_dlink_list * list, char statchar)
1558 {
1559 rb_dlink_node *ptr;
1560 struct Client *target_p;
1561
1562 /* send information about connections which match. note, we
1563 * dont need tests for IsInvisible(), because non-opers will
1564 * never get here for normal clients --fl
1565 */
1566 RB_DLINK_FOREACH(ptr, list->head)
1567 {
1568 target_p = ptr->data;
1569
1570 if(!doall && wilds && !match(name, target_p->name))
1571 continue;
1572
1573 stats_l_client(source_p, target_p, statchar);
1574 }
1575 }
1576
1577 void
1578 stats_l_client(struct Client *source_p, struct Client *target_p,
1579 char statchar)
1580 {
1581 if(IsAnyServer(target_p))
1582 {
1583 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1584 target_p->name,
1585 (int) rb_linebuf_len(&target_p->localClient->buf_sendq),
1586 (int) target_p->localClient->sendM,
1587 (int) target_p->localClient->sendK,
1588 (int) target_p->localClient->receiveM,
1589 (int) target_p->localClient->receiveK,
1590 rb_current_time() - target_p->localClient->firsttime,
1591 (rb_current_time() > target_p->localClient->lasttime) ?
1592 (rb_current_time() - target_p->localClient->lasttime) : 0,
1593 IsOper(source_p) ? show_capabilities(target_p) : "-");
1594 }
1595
1596 else
1597 {
1598 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1599 show_ip(source_p, target_p) ?
1600 (IsUpper(statchar) ?
1601 get_client_name(target_p, SHOW_IP) :
1602 get_client_name(target_p, HIDE_IP)) :
1603 get_client_name(target_p, MASK_IP),
1604 (int) rb_linebuf_len(&target_p->localClient->buf_sendq),
1605 (int) target_p->localClient->sendM,
1606 (int) target_p->localClient->sendK,
1607 (int) target_p->localClient->receiveM,
1608 (int) target_p->localClient->receiveK,
1609 rb_current_time() - target_p->localClient->firsttime,
1610 (rb_current_time() > target_p->localClient->lasttime) ?
1611 (rb_current_time() - target_p->localClient->lasttime) : 0,
1612 "-");
1613 }
1614 }
1615
1616 static void
1617 rb_dump_fd_callback(int fd, const char *desc, void *data)
1618 {
1619 struct Client *source_p = data;
1620 sendto_one_numeric(source_p, RPL_STATSDEBUG, "F :fd %-3d desc '%s'", fd, desc);
1621 }
1622
1623 static void
1624 stats_comm(struct Client *source_p)
1625 {
1626 rb_dump_fd(rb_dump_fd_callback, source_p);
1627 }
1628
1629 /*
1630 * stats_spy
1631 *
1632 * inputs - pointer to client doing the /stats
1633 * - char letter they are doing /stats on
1634 * output - none
1635 * side effects -
1636 * This little helper function reports to opers if configured.
1637 * personally, I don't see why opers need to see stats requests
1638 * at all. They are just "noise" to an oper, and users can't do
1639 * any damage with stats requests now anyway. So, why show them?
1640 * -Dianora
1641 */
1642 static void
1643 stats_spy(struct Client *source_p, char statchar, const char *name)
1644 {
1645 hook_data_int data;
1646
1647 data.client = source_p;
1648 data.arg1 = name;
1649 data.arg2 = (int) statchar;
1650
1651 call_hook(doing_stats_hook, &data);
1652 }
1653
1654 /* stats_p_spy()
1655 *
1656 * input - pointer to client doing stats
1657 * ouput -
1658 * side effects - call hook doing_stats_p
1659 */
1660 static void
1661 stats_p_spy (struct Client *source_p)
1662 {
1663 hook_data data;
1664
1665 data.client = source_p;
1666 data.arg1 = data.arg2 = NULL;
1667
1668 call_hook(doing_stats_p_hook, &data);
1669 }
1670