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
93 #include "ircd_alloc.h"
94 #include "ircd_chattr.h"
95 #include "ircd_features.h"
96 #include "ircd_policy.h"
97 #include "ircd_reply.h"
98 #include "ircd_string.h"
100 #include "listener.h"
105 #include "numnicks.h"
106 #include "opercmds.h"
115 #include "userload.h"
122 int report_klines(struct Client* sptr, char* mask, int limit_query)
128 const struct DenyConf* conf;
130 if (EmptyString(mask)) {
132 return need_more_params(sptr, "STATS K");
134 report_deny_list(sptr);
139 wilds = string_has_wildcards(mask);
143 if ((host = strchr(mask, '@'))) {
151 for (conf = conf_get_deny_list(); conf; conf = conf->next) {
152 if ((!wilds && ((user || conf->hostmask) &&
153 !match(conf->hostmask, host) &&
154 (!user || !match(conf->usermask, user)))) ||
155 (wilds && !mmatch(host, conf->hostmask) &&
156 (!user || !mmatch(user, conf->usermask))))
158 send_reply(sptr, RPL_STATSKLINE,
159 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
160 conf->hostmask, conf->message, conf->usermask);
165 /* send_reply(sptr, RPL_ENDOFSTATS, stat); */
171 * m_stats - generic message handler
173 * parv[0] = sender prefix
174 * parv[1] = statistics selector (defaults to Message frequency)
175 * parv[2] = target server (current server defaulted, if omitted)
176 * And 'stats l' and 'stats' L:
177 * parv[3] = server mask ("*" defaulted, if omitted)
179 * parv[3] = port mask (returns p-lines when its port is matched by this)
180 * Or for stats k,K,i and I:
181 * parv[3] = [user@]host.name (returns which K/I-lines match this)
182 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
183 * (defaults to old reply if ommitted, when local or Oper)
184 * A remote mask (something containing wildcards) is only
185 * allowed for IRC Operators.
187 * parv[3] = time param
188 * parv[4] = time param
189 * (see report_memleak_stats() in runmalloc.c for details)
191 * This function is getting really ugly. -Ghostwolf
193 int m_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
195 char stat = parc > 1 ? parv[1][0] : '\0';
196 const char **infotext = statsinfo;
198 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
205 #ifdef HEAD_IN_SAND_STATS_L
206 return m_not_oper(sptr,cptr,parc,parv);
209 struct Client *acptr;
215 if (parc > 3 && !EmptyString(parv[3])) {
217 wilds = string_has_wildcards(name);
222 * Send info about connections which match, or all if the
223 * mask matches me.name. Only restrictions are on those who
224 * are invisible not being visible to 'foreigners' who use
225 * a wild card based search to list it.
227 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
228 "SendM SendKBytes RcveM RcveKBytes :Open since");
229 for (i = 0; i <= HighestFd; i++)
231 if (!(acptr = LocalClientArray[i]))
233 /* Don't return clients when this is a request for `all' */
234 if (doall && IsUser(acptr))
236 /* Don't show invisible people to non opers unless they know the nick */
237 if (IsInvisible(acptr) && (doall || wilds) && !IsAnOper(acptr) && (acptr != sptr))
239 /* Only show the ones that match the given mask - if any */
240 if (!doall && wilds && match(name, cli_name(acptr)))
242 /* Skip all that do not match the specific query */
243 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
245 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
246 "%s %u %u %u %u %u :%Tu", (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
247 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
248 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
249 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
257 #ifdef HEAD_IN_SAND_STATS_C
258 return m_not_oper(sptr,cptr,parc,parv);
260 report_configured_links(sptr, CONF_SERVER);
265 case 'g': /* send glines */
266 #ifdef HEAD_IN_SAND_STATS_G
267 return m_not_oper(sptr,cptr,parc,parv);
275 #ifdef HEAD_IN_SAND_STATS_H
276 return m_not_oper(sptr,cptr,parc,parv);
278 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
283 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
284 #ifdef HEAD_IN_SAND_STATS_K
285 /* Simple version - if you want to fix it - send in a patch */
286 return m_not_oper(sptr,cptr,parc,parv);
288 if (0 == report_klines(sptr, (parc == 4) ? parv[3] : 0, 0))
294 #ifdef HEAD_IN_SAND_STATS_F
295 return m_not_oper(sptr,cptr,parc,parv);
297 feature_report(sptr);
303 #ifdef HEAD_IN_SAND_STATS_I
304 /* Simple version - if you want to fix it - send in a patch */
305 return m_not_oper(sptr,cptr,parc,parv);
308 struct ConfItem *aconf;
314 report_configured_links(sptr, CONF_CLIENT);
317 if (EmptyString(parv[3]))
318 return need_more_params(sptr, "STATS I");
321 wilds = string_has_wildcards(host);
323 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
324 if (CONF_CLIENT == aconf->status) {
325 if ((!wilds && (!match(aconf->host, host) ||
326 !match(aconf->name, host))) ||
327 (wilds && (!mmatch(host, aconf->host) ||
328 !mmatch(host, aconf->name))))
330 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
331 aconf->port, get_conf_class(aconf));
342 #ifdef HEAD_IN_SAND_STATS_M
343 return m_not_oper(sptr,cptr,parc,parv);
346 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
347 fda_get_block_count());
353 #ifdef HEAD_IN_SAND_STATS_m
354 return m_not_oper(sptr,cptr,parc,parv);
357 struct Message *mptr;
359 for (mptr = msgtab; mptr->cmd; mptr++)
361 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
369 #ifdef HEAD_IN_SAND_STATS_O
370 return m_not_oper(sptr,cptr,parc,parv);
372 report_configured_links(sptr, CONF_OPS);
379 * show listener ports
380 * show hidden ports to opers, if there are more than 3 parameters,
381 * interpret the fourth parameter as the port number.
383 #ifdef HEAD_IN_SAND_STATS_P
384 return m_not_oper(sptr,cptr,parc,parv)
386 show_ports(sptr, 0, (parc > 3) ? atoi(parv[3]) : 0, 100);
392 #ifdef HEAD_IN_SAND_STATS_R
393 return m_not_oper(sptr,cptr,parc,parv);
396 send_usage(sptr, parv[0]);
402 #ifdef HEAD_IN_SAND_STATS_D
403 return m_not_oper(sptr,cptr,parc,parv);
405 report_crule_list(sptr, CRULE_ALL);
410 #ifdef HEAD_IN_SAND_STATS_d
411 return m_not_oper(sptr,cptr,parc,parv);
413 report_crule_list(sptr, CRULE_MASK);
418 #ifdef HEAD_IN_SAND_STATS_t
419 return m_not_oper(sptr,cptr,parc,parv);
421 tstats(sptr, parv[0]);
426 #ifdef HEAD_IN_SAND_STATS_T
427 return m_not_oper(sptr,cptr,parc,parv);
434 #ifdef HEAD_IN_SAND_STATS_U
435 return m_not_oper(sptr,cptr,parc,parv);
437 report_configured_links(sptr, CONF_UWORLD);
442 #ifdef HEAD_IN_SAND_STATS_u
443 return m_not_oper(sptr,cptr,parc,parv);
448 nowr = CurrentTime - cli_since(&me);
449 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
450 (nowr / 60) % 60, nowr % 60);
451 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
458 #ifdef HEAD_IN_SAND_STATS_W
459 return m_not_oper(sptr,cptr,parc,parv);
467 #ifdef HEAD_IN_SAND_STATS_X
468 return m_not_oper(sptr,cptr,parc,parv);
471 class_send_meminfo(sptr);
472 send_listinfo(sptr, parv[0]);
479 #ifdef HEAD_IN_SAND_STATS_Y
480 return m_not_oper(sptr,cptr,parc,parv);
482 report_classes(sptr);
488 #ifdef HEAD_IN_SAND_STATS_Z
489 return m_not_oper(sptr,cptr,parc,parv);
491 count_memory(sptr, parv[0]);
498 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
501 send_reply(sptr, RPL_ENDOFSTATS, stat);
506 * ms_stats - server message handler
508 * parv[0] = sender prefix
509 * parv[1] = statistics selector (defaults to Message frequency)
510 * parv[2] = target server (current server defaulted, if omitted)
511 * And 'stats l' and 'stats' L:
512 * parv[3] = server mask ("*" defaulted, if omitted)
514 * parv[3] = port mask (returns p-lines when its port is matched by this)
515 * Or for stats k,K,i and I:
516 * parv[3] = [user@]host.name (returns which K/I-lines match this)
517 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
518 * (defaults to old reply if ommitted, when local or Oper)
519 * A remote mask (something containing wildcards) is only
520 * allowed for IRC Operators.
522 * parv[3] = time param
523 * parv[4] = time param
524 * (see report_memleak_stats() in runmalloc.c for details)
526 * This function is getting really ugly. -Ghostwolf
528 int ms_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
530 struct Message *mptr;
531 struct Client *acptr;
532 struct ConfItem *aconf;
533 char stat = parc > 1 ? parv[1][0] : '\0';
536 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
548 if (parc > 3 && !EmptyString(parv[3])) {
550 wilds = string_has_wildcards(name);
555 * Send info about connections which match, or all if the
556 * mask matches me.name. Only restrictions are on those who
557 * are invisible not being visible to 'foreigners' who use
558 * a wild card based search to list it.
560 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
561 "SendM SendKBytes RcveM RcveKBytes :Open since");
562 for (i = 0; i <= HighestFd; i++)
564 if (!(acptr = LocalClientArray[i]))
566 /* Don't return clients when this is a request for `all' */
567 if (doall && IsUser(acptr))
569 /* Don't show invisible people to unauthorized people when using
570 * wildcards -- Is this still needed now /stats is oper only ?
571 * Not here, because ms_stats is specifically a remote command,
572 * thus the check was removed. -Ghostwolf */
573 /* Only show the ones that match the given mask - if any */
574 if (!doall && wilds && match(name, cli_name(acptr)))
576 /* Skip all that do not match the specific query */
577 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
579 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
580 "%s %u %u %u %u %u :%Tu", cli_name(acptr),
581 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
582 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
583 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
589 report_configured_links(sptr, CONF_SERVER);
592 case 'g': /* send glines */
597 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
600 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
601 if (0 == report_klines(sptr, (parc > 3) ? parv[3] : 0, !IsOper(sptr)))
606 feature_report(sptr);
615 if (parc < 4 && IsOper(sptr)) {
616 report_configured_links(sptr, CONF_CLIENT);
619 if (parc < 4 || EmptyString(parv[3]))
620 return need_more_params(sptr, "STATS I");
623 wilds = string_has_wildcards(parv[3]);
629 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
630 if (CONF_CLIENT == aconf->status) {
631 if ((!wilds && (!match(aconf->host, host) ||
632 !match(aconf->name, host))) ||
633 (wilds && (!mmatch(host, aconf->host) ||
634 !mmatch(host, aconf->name))))
636 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
637 aconf->port, get_conf_class(aconf));
647 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
648 fda_get_block_count());
652 for (mptr = msgtab; mptr->cmd; mptr++)
654 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
659 report_configured_links(sptr, CONF_OPS);
664 * show listener ports
665 * show hidden ports to opers, if there are more than 3 parameters,
666 * interpret the fourth parameter as the port number, limit non-local
667 * or non-oper results to 8 ports.
669 show_ports(sptr, IsOper(sptr), (parc > 3) ? atoi(parv[3]) : 0, IsOper(sptr) ? 100 : 8);
674 send_usage(sptr, parv[0]);
678 report_crule_list(sptr, CRULE_ALL);
681 report_crule_list(sptr, CRULE_MASK);
684 tstats(sptr, parv[0]);
690 report_configured_links(sptr, CONF_UWORLD);
696 nowr = CurrentTime - cli_since(&me);
697 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
698 (nowr / 60) % 60, nowr % 60);
699 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
709 class_send_meminfo(sptr);
710 send_listinfo(sptr, parv[0]);
715 report_classes(sptr);
719 count_memory(sptr, parv[0]);
725 send_reply(sptr, RPL_ENDOFSTATS, stat);
730 * mo_stats - oper message handler
732 * parv[0] = sender prefix
733 * parv[1] = statistics selector (defaults to Message frequency)
734 * parv[2] = target server (current server defaulted, if omitted)
735 * And 'stats l' and 'stats' L:
736 * parv[3] = server mask ("*" defaulted, if omitted)
738 * parv[3] = port mask (returns p-lines when its port is matched by this)
739 * Or for stats k,K,i and I:
740 * parv[3] = [user@]host.name (returns which K/I-lines match this)
741 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
742 * (defaults to old reply if ommitted, when local or Oper)
743 * A remote mask (something containing wildcards) is only
744 * allowed for IRC Operators.
746 * parv[3] = time param
747 * parv[4] = time param
748 * (see report_memleak_stats() in runmalloc.c for details)
750 * This function is getting really ugly. -Ghostwolf
752 int mo_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
754 struct Message* mptr;
755 struct Client* acptr;
756 struct ConfItem* aconf;
757 char stat = parc > 1 ? parv[1][0] : '\0';
758 const char** infotext = statsinfo;
761 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
769 int doall = 0, wilds = 0;
771 if (parc > 3 && !EmptyString(parv[3])) {
773 wilds = string_has_wildcards(name);
778 * Send info about connections which match, or all if the
779 * mask matches me.name. Only restrictions are on those who
780 * are invisible not being visible to 'foreigners' who use
781 * a wild card based search to list it.
783 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
784 "SendM SendKBytes RcveM RcveKBytes :Open since");
785 for (i = 0; i <= HighestFd; i++)
787 if (!(acptr = LocalClientArray[i]))
789 /* Don't return clients when this is a request for `all' */
790 if (doall && IsUser(acptr))
792 /* Only show the ones that match the given mask - if any */
793 if (!doall && wilds && match(name, cli_name(acptr)))
795 /* Skip all that do not match the specific query */
796 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
798 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
799 "%s %u %u %u %u %u :%Tu", cli_name(acptr),
800 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
801 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
802 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
808 report_configured_links(sptr, CONF_SERVER);
811 case 'g': /* send glines */
816 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
819 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
820 if (0 == report_klines(sptr, (parc > 3) ? parv[3] : 0, 0))
825 feature_report(sptr);
835 report_configured_links(sptr, CONF_CLIENT);
838 if (EmptyString(parv[3]))
839 return need_more_params(sptr, "STATS I");
842 wilds = string_has_wildcards(host);
844 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
845 if (CONF_CLIENT == aconf->status) {
846 if ((!wilds && (!match(aconf->host, host) ||
847 !match(aconf->name, host))) ||
848 (wilds && (!mmatch(host, aconf->host) ||
849 !mmatch(host, aconf->name))))
851 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
852 aconf->port, get_conf_class(aconf));
862 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
863 fda_get_block_count());
867 for (mptr = msgtab; mptr->cmd; mptr++)
869 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
874 report_configured_links(sptr, CONF_OPS);
879 * show listener ports
880 * show hidden ports to opers, if there are more than 3 parameters,
881 * interpret the fourth parameter as the port number, limit non-local
882 * or non-oper results to 8 ports.
884 show_ports(sptr, 1, (parc > 3) ? atoi(parv[3]) : 0, 100);
889 send_usage(sptr, parv[0]);
893 report_crule_list(sptr, CRULE_ALL);
896 report_crule_list(sptr, CRULE_MASK);
899 tstats(sptr, parv[0]);
905 report_configured_links(sptr, CONF_UWORLD);
911 nowr = CurrentTime - cli_since(&me);
912 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
913 (nowr / 60) % 60, nowr % 60);
914 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
924 class_send_meminfo(sptr);
925 send_listinfo(sptr, parv[0]);
930 report_classes(sptr);
934 count_memory(sptr, parv[0]);
939 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
942 send_reply(sptr, RPL_ENDOFSTATS, stat);