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