2 * IRC - Internet Relay Chat, ircd/m_stats.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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 1, or (at your option)
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * m_functions execute protocol messages on this server:
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
95 #include "ircd_alloc.h"
96 #include "ircd_chattr.h"
97 #include "ircd_features.h"
98 #include "ircd_policy.h"
99 #include "ircd_reply.h"
100 #include "ircd_string.h"
102 #include "listener.h"
107 #include "numnicks.h"
108 #include "opercmds.h"
117 #include "userload.h"
124 int report_klines(struct Client* sptr, char* mask, int limit_query)
130 const struct DenyConf* conf;
132 if (EmptyString(mask)) {
134 return need_more_params(sptr, "STATS K");
136 report_deny_list(sptr);
141 wilds = string_has_wildcards(mask);
145 if ((host = strchr(mask, '@'))) {
153 for (conf = conf_get_deny_list(); conf; conf = conf->next) {
154 if ((!wilds && ((user || conf->hostmask) &&
155 !match(conf->hostmask, host) &&
156 (!user || !match(conf->usermask, user)))) ||
157 (wilds && !mmatch(host, conf->hostmask) &&
158 (!user || !mmatch(user, conf->usermask))))
160 send_reply(sptr, RPL_STATSKLINE,
161 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
162 conf->hostmask, conf->message, conf->usermask);
167 /* send_reply(sptr, RPL_ENDOFSTATS, stat); */
173 * m_stats - generic message handler
175 * parv[0] = sender prefix
176 * parv[1] = statistics selector (defaults to Message frequency)
177 * parv[2] = target server (current server defaulted, if omitted)
178 * And 'stats l' and 'stats' L:
179 * parv[3] = server mask ("*" defaulted, if omitted)
181 * parv[3] = port mask (returns p-lines when its port is matched by this)
182 * Or for stats k,K,i and I:
183 * parv[3] = [user@]host.name (returns which K/I-lines match this)
184 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
185 * (defaults to old reply if ommitted, when local or Oper)
186 * A remote mask (something containing wildcards) is only
187 * allowed for IRC Operators.
189 * parv[3] = time param
190 * parv[4] = time param
191 * (see report_memleak_stats() in runmalloc.c for details)
193 * This function is getting really ugly. -Ghostwolf
195 int m_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
197 char stat = parc > 1 ? parv[1][0] : '\0';
198 const char **infotext = statsinfo;
200 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
207 #ifdef HEAD_IN_SAND_STATS_L
208 return m_not_oper(sptr,cptr,parc,parv);
211 struct Client *acptr;
217 if (parc > 3 && !EmptyString(parv[3])) {
219 wilds = string_has_wildcards(name);
224 * Send info about connections which match, or all if the
225 * mask matches me.name. Only restrictions are on those who
226 * are invisible not being visible to 'foreigners' who use
227 * a wild card based search to list it.
229 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
230 "SendM SendKBytes RcveM RcveKBytes :Open since");
231 for (i = 0; i <= HighestFd; i++)
233 if (!(acptr = LocalClientArray[i]))
235 /* Don't return clients when this is a request for `all' */
236 if (doall && IsUser(acptr))
238 /* Don't show invisible people to non opers unless they know the nick */
239 if (IsInvisible(acptr) && (doall || wilds) && !IsAnOper(acptr) && (acptr != sptr))
241 /* Only show the ones that match the given mask - if any */
242 if (!doall && wilds && match(name, cli_name(acptr)))
244 /* Skip all that do not match the specific query */
245 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
247 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
248 "%s %u %u %u %u %u :%Tu", (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
249 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
250 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
251 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
259 #ifdef HEAD_IN_SAND_STATS_C
260 return m_not_oper(sptr,cptr,parc,parv);
262 report_configured_links(sptr, CONF_SERVER);
267 case 'g': /* send glines */
268 #ifdef HEAD_IN_SAND_STATS_G
269 return m_not_oper(sptr,cptr,parc,parv);
277 #ifdef HEAD_IN_SAND_STATS_H
278 return m_not_oper(sptr,cptr,parc,parv);
280 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
285 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
286 #ifdef HEAD_IN_SAND_STATS_K
287 /* Simple version - if you want to fix it - send in a patch */
288 return m_not_oper(sptr,cptr,parc,parv);
290 if (0 == report_klines(sptr, (parc == 4) ? parv[3] : 0, 0))
296 #ifdef HEAD_IN_SAND_STATS_F
297 return m_not_oper(sptr,cptr,parc,parv);
299 feature_report(sptr);
305 #ifdef HEAD_IN_SAND_STATS_I
306 /* Simple version - if you want to fix it - send in a patch */
307 return m_not_oper(sptr,cptr,parc,parv);
310 struct ConfItem *aconf;
316 report_configured_links(sptr, CONF_CLIENT);
319 if (EmptyString(parv[3]))
320 return need_more_params(sptr, "STATS I");
323 wilds = string_has_wildcards(host);
325 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
326 if (CONF_CLIENT == aconf->status) {
327 if ((!wilds && (!match(aconf->host, host) ||
328 !match(aconf->name, host))) ||
329 (wilds && (!mmatch(host, aconf->host) ||
330 !mmatch(host, aconf->name))))
332 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
333 aconf->port, get_conf_class(aconf));
344 #ifdef HEAD_IN_SAND_STATS_M
345 return m_not_oper(sptr,cptr,parc,parv);
348 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
349 fda_get_block_count());
355 #ifdef HEAD_IN_SAND_STATS_m
356 return m_not_oper(sptr,cptr,parc,parv);
359 struct Message *mptr;
361 for (mptr = msgtab; mptr->cmd; mptr++)
363 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
371 #ifdef HEAD_IN_SAND_STATS_O
372 return m_not_oper(sptr,cptr,parc,parv);
374 report_configured_links(sptr, CONF_OPS);
381 * show listener ports
382 * show hidden ports to opers, if there are more than 3 parameters,
383 * interpret the fourth parameter as the port number.
385 #ifdef HEAD_IN_SAND_STATS_P
386 return m_not_oper(sptr,cptr,parc,parv)
388 show_ports(sptr, 0, (parc > 3) ? atoi(parv[3]) : 0, 100);
394 #ifdef HEAD_IN_SAND_STATS_R
395 return m_not_oper(sptr,cptr,parc,parv);
398 send_usage(sptr, parv[0]);
404 #ifdef HEAD_IN_SAND_STATS_D
405 return m_not_oper(sptr,cptr,parc,parv);
407 report_crule_list(sptr, CRULE_ALL);
412 #ifdef HEAD_IN_SAND_STATS_d
413 return m_not_oper(sptr,cptr,parc,parv);
415 report_crule_list(sptr, CRULE_MASK);
420 #ifdef HEAD_IN_SAND_STATS_t
421 return m_not_oper(sptr,cptr,parc,parv);
423 tstats(sptr, parv[0]);
428 #ifdef HEAD_IN_SAND_STATS_T
429 return m_not_oper(sptr,cptr,parc,parv);
436 #ifdef HEAD_IN_SAND_STATS_U
437 return m_not_oper(sptr,cptr,parc,parv);
439 report_configured_links(sptr, CONF_UWORLD);
444 #ifdef HEAD_IN_SAND_STATS_u
445 return m_not_oper(sptr,cptr,parc,parv);
450 nowr = CurrentTime - cli_since(&me);
451 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
452 (nowr / 60) % 60, nowr % 60);
453 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
460 #ifdef HEAD_IN_SAND_STATS_W
461 return m_not_oper(sptr,cptr,parc,parv);
469 #ifdef HEAD_IN_SAND_STATS_X
470 return m_not_oper(sptr,cptr,parc,parv);
473 class_send_meminfo(sptr);
474 send_listinfo(sptr, parv[0]);
481 #ifdef HEAD_IN_SAND_STATS_Y
482 return m_not_oper(sptr,cptr,parc,parv);
484 report_classes(sptr);
490 #ifdef HEAD_IN_SAND_STATS_Z
491 return m_not_oper(sptr,cptr,parc,parv);
493 count_memory(sptr, parv[0]);
500 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
503 send_reply(sptr, RPL_ENDOFSTATS, stat);
508 * ms_stats - server message handler
510 * parv[0] = sender prefix
511 * parv[1] = statistics selector (defaults to Message frequency)
512 * parv[2] = target server (current server defaulted, if omitted)
513 * And 'stats l' and 'stats' L:
514 * parv[3] = server mask ("*" defaulted, if omitted)
516 * parv[3] = port mask (returns p-lines when its port is matched by this)
517 * Or for stats k,K,i and I:
518 * parv[3] = [user@]host.name (returns which K/I-lines match this)
519 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
520 * (defaults to old reply if ommitted, when local or Oper)
521 * A remote mask (something containing wildcards) is only
522 * allowed for IRC Operators.
524 * parv[3] = time param
525 * parv[4] = time param
526 * (see report_memleak_stats() in runmalloc.c for details)
528 * This function is getting really ugly. -Ghostwolf
530 int ms_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
532 struct Message *mptr;
533 struct Client *acptr;
534 struct ConfItem *aconf;
535 char stat = parc > 1 ? parv[1][0] : '\0';
538 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
550 if (parc > 3 && !EmptyString(parv[3])) {
552 wilds = string_has_wildcards(name);
557 * Send info about connections which match, or all if the
558 * mask matches me.name. Only restrictions are on those who
559 * are invisible not being visible to 'foreigners' who use
560 * a wild card based search to list it.
562 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
563 "SendM SendKBytes RcveM RcveKBytes :Open since");
564 for (i = 0; i <= HighestFd; i++)
566 if (!(acptr = LocalClientArray[i]))
568 /* Don't return clients when this is a request for `all' */
569 if (doall && IsUser(acptr))
571 /* Don't show invisible people to unauthorized people when using
572 * wildcards -- Is this still needed now /stats is oper only ?
573 * Not here, because ms_stats is specifically a remote command,
574 * thus the check was removed. -Ghostwolf */
575 /* Only show the ones that match the given mask - if any */
576 if (!doall && wilds && match(name, cli_name(acptr)))
578 /* Skip all that do not match the specific query */
579 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
581 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
582 "%s %u %u %u %u %u :%Tu", cli_name(acptr),
583 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
584 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
585 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
591 report_configured_links(sptr, CONF_SERVER);
594 case 'g': /* send glines */
599 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
602 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
603 if (0 == report_klines(sptr, (parc > 3) ? parv[3] : 0, !IsOper(sptr)))
608 feature_report(sptr);
617 if (parc < 4 && IsOper(sptr)) {
618 report_configured_links(sptr, CONF_CLIENT);
621 if (parc < 4 || EmptyString(parv[3]))
622 return need_more_params(sptr, "STATS I");
625 wilds = string_has_wildcards(parv[3]);
631 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
632 if (CONF_CLIENT == aconf->status) {
633 if ((!wilds && (!match(aconf->host, host) ||
634 !match(aconf->name, host))) ||
635 (wilds && (!mmatch(host, aconf->host) ||
636 !mmatch(host, aconf->name))))
638 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
639 aconf->port, get_conf_class(aconf));
649 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
650 fda_get_block_count());
654 for (mptr = msgtab; mptr->cmd; mptr++)
656 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
661 report_configured_links(sptr, CONF_OPS);
666 * show listener ports
667 * show hidden ports to opers, if there are more than 3 parameters,
668 * interpret the fourth parameter as the port number, limit non-local
669 * or non-oper results to 8 ports.
671 show_ports(sptr, IsOper(sptr), (parc > 3) ? atoi(parv[3]) : 0, IsOper(sptr) ? 100 : 8);
676 send_usage(sptr, parv[0]);
680 report_crule_list(sptr, CRULE_ALL);
683 report_crule_list(sptr, CRULE_MASK);
686 tstats(sptr, parv[0]);
692 report_configured_links(sptr, CONF_UWORLD);
698 nowr = CurrentTime - cli_since(&me);
699 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
700 (nowr / 60) % 60, nowr % 60);
701 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
711 class_send_meminfo(sptr);
712 send_listinfo(sptr, parv[0]);
717 report_classes(sptr);
721 count_memory(sptr, parv[0]);
727 send_reply(sptr, RPL_ENDOFSTATS, stat);
732 * mo_stats - oper message handler
734 * parv[0] = sender prefix
735 * parv[1] = statistics selector (defaults to Message frequency)
736 * parv[2] = target server (current server defaulted, if omitted)
737 * And 'stats l' and 'stats' L:
738 * parv[3] = server mask ("*" defaulted, if omitted)
740 * parv[3] = port mask (returns p-lines when its port is matched by this)
741 * Or for stats k,K,i and I:
742 * parv[3] = [user@]host.name (returns which K/I-lines match this)
743 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
744 * (defaults to old reply if ommitted, when local or Oper)
745 * A remote mask (something containing wildcards) is only
746 * allowed for IRC Operators.
748 * parv[3] = time param
749 * parv[4] = time param
750 * (see report_memleak_stats() in runmalloc.c for details)
752 * This function is getting really ugly. -Ghostwolf
754 int mo_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
756 struct Message* mptr;
757 struct Client* acptr;
758 struct ConfItem* aconf;
759 char stat = parc > 1 ? parv[1][0] : '\0';
760 const char** infotext = statsinfo;
763 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
771 int doall = 0, wilds = 0;
773 if (parc > 3 && !EmptyString(parv[3])) {
775 wilds = string_has_wildcards(name);
780 * Send info about connections which match, or all if the
781 * mask matches me.name. Only restrictions are on those who
782 * are invisible not being visible to 'foreigners' who use
783 * a wild card based search to list it.
785 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
786 "SendM SendKBytes RcveM RcveKBytes :Open since");
787 for (i = 0; i <= HighestFd; i++)
789 if (!(acptr = LocalClientArray[i]))
791 /* Don't return clients when this is a request for `all' */
792 if (doall && IsUser(acptr))
794 /* Only show the ones that match the given mask - if any */
795 if (!doall && wilds && match(name, cli_name(acptr)))
797 /* Skip all that do not match the specific query */
798 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
800 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
801 "%s %u %u %u %u %u :%Tu", cli_name(acptr),
802 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
803 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
804 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
810 report_configured_links(sptr, CONF_SERVER);
813 case 'g': /* send glines */
818 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
821 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
822 if (0 == report_klines(sptr, (parc > 3) ? parv[3] : 0, 0))
827 feature_report(sptr);
837 report_configured_links(sptr, CONF_CLIENT);
840 if (EmptyString(parv[3]))
841 return need_more_params(sptr, "STATS I");
844 wilds = string_has_wildcards(host);
846 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
847 if (CONF_CLIENT == aconf->status) {
848 if ((!wilds && (!match(aconf->host, host) ||
849 !match(aconf->name, host))) ||
850 (wilds && (!mmatch(host, aconf->host) ||
851 !mmatch(host, aconf->name))))
853 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
854 aconf->port, get_conf_class(aconf));
864 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
865 fda_get_block_count());
869 for (mptr = msgtab; mptr->cmd; mptr++)
871 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
876 report_configured_links(sptr, CONF_OPS);
881 * show listener ports
882 * show hidden ports to opers, if there are more than 3 parameters,
883 * interpret the fourth parameter as the port number, limit non-local
884 * or non-oper results to 8 ports.
886 show_ports(sptr, 1, (parc > 3) ? atoi(parv[3]) : 0, 100);
891 send_usage(sptr, parv[0]);
895 report_crule_list(sptr, CRULE_ALL);
898 report_crule_list(sptr, CRULE_MASK);
901 tstats(sptr, parv[0]);
907 report_configured_links(sptr, CONF_UWORLD);
913 nowr = CurrentTime - cli_since(&me);
914 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
915 (nowr / 60) % 60, nowr % 60);
916 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
926 class_send_meminfo(sptr);
927 send_listinfo(sptr, parv[0]);
932 report_classes(sptr);
936 count_memory(sptr, parv[0]);
941 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
944 send_reply(sptr, RPL_ENDOFSTATS, stat);