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_events.h"
98 #include "ircd_features.h"
99 #include "ircd_policy.h"
100 #include "ircd_reply.h"
101 #include "ircd_string.h"
103 #include "listener.h"
108 #include "numnicks.h"
109 #include "opercmds.h"
118 #include "userload.h"
125 int report_klines(struct Client* sptr, char* mask, int limit_query)
131 const struct DenyConf* conf;
133 if (EmptyString(mask)) {
135 return need_more_params(sptr, "STATS K");
137 report_deny_list(sptr);
142 wilds = string_has_wildcards(mask);
146 if ((host = strchr(mask, '@'))) {
154 for (conf = conf_get_deny_list(); conf; conf = conf->next) {
155 if ((!wilds && ((user || conf->hostmask) &&
156 !match(conf->hostmask, host) &&
157 (!user || !match(conf->usermask, user)))) ||
158 (wilds && !mmatch(host, conf->hostmask) &&
159 (!user || !mmatch(user, conf->usermask))))
161 send_reply(sptr, RPL_STATSKLINE,
162 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
163 conf->hostmask, conf->message, conf->usermask);
168 /* send_reply(sptr, RPL_ENDOFSTATS, stat); */
174 * m_stats - generic message handler
176 * parv[0] = sender prefix
177 * parv[1] = statistics selector (defaults to Message frequency)
178 * parv[2] = target server (current server defaulted, if omitted)
179 * And 'stats l' and 'stats' L:
180 * parv[3] = server mask ("*" defaulted, if omitted)
182 * parv[3] = port mask (returns p-lines when its port is matched by this)
183 * Or for stats k,K,i and I:
184 * parv[3] = [user@]host.name (returns which K/I-lines match this)
185 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
186 * (defaults to old reply if ommitted, when local or Oper)
187 * A remote mask (something containing wildcards) is only
188 * allowed for IRC Operators.
190 * parv[3] = time param
191 * parv[4] = time param
192 * (see report_memleak_stats() in runmalloc.c for details)
194 * This function is getting really ugly. -Ghostwolf
196 int m_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
198 char stat = parc > 1 ? parv[1][0] : '\0';
199 const char **infotext = statsinfo;
201 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
208 #ifdef HEAD_IN_SAND_STATS_L
209 return m_not_oper(sptr,cptr,parc,parv);
212 struct Client *acptr;
218 if (parc > 3 && !EmptyString(parv[3])) {
220 wilds = string_has_wildcards(name);
225 * Send info about connections which match, or all if the
226 * mask matches me.name. Only restrictions are on those who
227 * are invisible not being visible to 'foreigners' who use
228 * a wild card based search to list it.
230 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
231 "SendM SendKBytes RcveM RcveKBytes :Open since");
232 for (i = 0; i <= HighestFd; i++)
234 if (!(acptr = LocalClientArray[i]))
236 /* Don't return clients when this is a request for `all' */
237 if (doall && IsUser(acptr))
239 /* Don't show invisible people to non opers unless they know the nick */
240 if (IsInvisible(acptr) && (doall || wilds) && !IsAnOper(acptr) && (acptr != sptr))
242 /* Only show the ones that match the given mask - if any */
243 if (!doall && wilds && match(name, cli_name(acptr)))
245 /* Skip all that do not match the specific query */
246 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
248 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
249 "%s %u %u %u %u %u :%Tu", (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
250 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
251 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
252 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
260 #ifdef HEAD_IN_SAND_STATS_C
261 return m_not_oper(sptr,cptr,parc,parv);
263 report_configured_links(sptr, CONF_SERVER);
268 case 'e': /* report engine name */
269 #ifdef HEAD_IN_SAND_STATS_E
270 return m_not_oper(sptr,cptr,parc,parv);
272 send_reply(sptr, RPL_STATSENGINE, engine_name());
277 case 'g': /* send glines */
278 #ifdef HEAD_IN_SAND_STATS_G
279 return m_not_oper(sptr,cptr,parc,parv);
287 #ifdef HEAD_IN_SAND_STATS_H
288 return m_not_oper(sptr,cptr,parc,parv);
290 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
295 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
296 #ifdef HEAD_IN_SAND_STATS_K
297 /* Simple version - if you want to fix it - send in a patch */
298 return m_not_oper(sptr,cptr,parc,parv);
300 if (0 == report_klines(sptr, (parc == 4) ? parv[3] : 0, 0))
306 #ifdef HEAD_IN_SAND_STATS_F
307 return m_not_oper(sptr,cptr,parc,parv);
309 feature_report(sptr);
315 #ifdef HEAD_IN_SAND_STATS_I
316 /* Simple version - if you want to fix it - send in a patch */
317 return m_not_oper(sptr,cptr,parc,parv);
320 struct ConfItem *aconf;
326 report_configured_links(sptr, CONF_CLIENT);
329 if (EmptyString(parv[3]))
330 return need_more_params(sptr, "STATS I");
333 wilds = string_has_wildcards(host);
335 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
336 if (CONF_CLIENT == aconf->status) {
337 if ((!wilds && (!match(aconf->host, host) ||
338 !match(aconf->name, host))) ||
339 (wilds && (!mmatch(host, aconf->host) ||
340 !mmatch(host, aconf->name))))
342 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
343 aconf->port, get_conf_class(aconf));
354 #ifdef HEAD_IN_SAND_STATS_M
355 return m_not_oper(sptr,cptr,parc,parv);
358 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
359 fda_get_block_count());
365 #ifdef HEAD_IN_SAND_STATS_m
366 return m_not_oper(sptr,cptr,parc,parv);
369 struct Message *mptr;
371 for (mptr = msgtab; mptr->cmd; mptr++)
373 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
381 #ifdef HEAD_IN_SAND_STATS_O
382 return m_not_oper(sptr,cptr,parc,parv);
384 report_configured_links(sptr, CONF_OPS);
391 * show listener ports
392 * show hidden ports to opers, if there are more than 3 parameters,
393 * interpret the fourth parameter as the port number.
395 #ifdef HEAD_IN_SAND_STATS_P
396 return m_not_oper(sptr,cptr,parc,parv)
398 show_ports(sptr, 0, (parc > 3) ? atoi(parv[3]) : 0, 100);
404 #ifdef HEAD_IN_SAND_STATS_R
405 return m_not_oper(sptr,cptr,parc,parv);
408 send_usage(sptr, parv[0]);
414 #ifdef HEAD_IN_SAND_STATS_D
415 return m_not_oper(sptr,cptr,parc,parv);
417 report_crule_list(sptr, CRULE_ALL);
422 #ifdef HEAD_IN_SAND_STATS_d
423 return m_not_oper(sptr,cptr,parc,parv);
425 report_crule_list(sptr, CRULE_MASK);
430 #ifdef HEAD_IN_SAND_STATS_t
431 return m_not_oper(sptr,cptr,parc,parv);
433 tstats(sptr, parv[0]);
438 #ifdef HEAD_IN_SAND_STATS_T
439 return m_not_oper(sptr,cptr,parc,parv);
446 #ifdef HEAD_IN_SAND_STATS_U
447 return m_not_oper(sptr,cptr,parc,parv);
449 report_configured_links(sptr, CONF_UWORLD);
454 #ifdef HEAD_IN_SAND_STATS_u
455 return m_not_oper(sptr,cptr,parc,parv);
460 nowr = CurrentTime - cli_since(&me);
461 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
462 (nowr / 60) % 60, nowr % 60);
463 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
470 #ifdef HEAD_IN_SAND_STATS_W
471 return m_not_oper(sptr,cptr,parc,parv);
479 #ifdef HEAD_IN_SAND_STATS_X
480 return m_not_oper(sptr,cptr,parc,parv);
483 class_send_meminfo(sptr);
484 send_listinfo(sptr, parv[0]);
491 #ifdef HEAD_IN_SAND_STATS_Y
492 return m_not_oper(sptr,cptr,parc,parv);
494 report_classes(sptr);
500 #ifdef HEAD_IN_SAND_STATS_Z
501 return m_not_oper(sptr,cptr,parc,parv);
503 count_memory(sptr, parv[0]);
510 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
513 send_reply(sptr, RPL_ENDOFSTATS, stat);
518 * ms_stats - server message handler
520 * parv[0] = sender prefix
521 * parv[1] = statistics selector (defaults to Message frequency)
522 * parv[2] = target server (current server defaulted, if omitted)
523 * And 'stats l' and 'stats' L:
524 * parv[3] = server mask ("*" defaulted, if omitted)
526 * parv[3] = port mask (returns p-lines when its port is matched by this)
527 * Or for stats k,K,i and I:
528 * parv[3] = [user@]host.name (returns which K/I-lines match this)
529 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
530 * (defaults to old reply if ommitted, when local or Oper)
531 * A remote mask (something containing wildcards) is only
532 * allowed for IRC Operators.
534 * parv[3] = time param
535 * parv[4] = time param
536 * (see report_memleak_stats() in runmalloc.c for details)
538 * This function is getting really ugly. -Ghostwolf
540 int ms_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
542 struct Message *mptr;
543 struct Client *acptr;
544 struct ConfItem *aconf;
545 char stat = parc > 1 ? parv[1][0] : '\0';
548 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
560 if (parc > 3 && !EmptyString(parv[3])) {
562 wilds = string_has_wildcards(name);
567 * Send info about connections which match, or all if the
568 * mask matches me.name. Only restrictions are on those who
569 * are invisible not being visible to 'foreigners' who use
570 * a wild card based search to list it.
572 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
573 "SendM SendKBytes RcveM RcveKBytes :Open since");
574 for (i = 0; i <= HighestFd; i++)
576 if (!(acptr = LocalClientArray[i]))
578 /* Don't return clients when this is a request for `all' */
579 if (doall && IsUser(acptr))
581 /* Don't show invisible people to unauthorized people when using
582 * wildcards -- Is this still needed now /stats is oper only ?
583 * Not here, because ms_stats is specifically a remote command,
584 * thus the check was removed. -Ghostwolf */
585 /* Only show the ones that match the given mask - if any */
586 if (!doall && wilds && match(name, cli_name(acptr)))
588 /* Skip all that do not match the specific query */
589 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
591 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
592 "%s %u %u %u %u %u :%Tu", cli_name(acptr),
593 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
594 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
595 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
601 report_configured_links(sptr, CONF_SERVER);
604 case 'e': /* report engine name */
605 send_reply(sptr, RPL_STATSENGINE, engine_name());
608 case 'g': /* send glines */
613 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
616 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
617 if (0 == report_klines(sptr, (parc > 3) ? parv[3] : 0, !IsOper(sptr)))
622 feature_report(sptr);
631 if (parc < 4 && IsOper(sptr)) {
632 report_configured_links(sptr, CONF_CLIENT);
635 if (parc < 4 || EmptyString(parv[3]))
636 return need_more_params(sptr, "STATS I");
639 wilds = string_has_wildcards(parv[3]);
645 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
646 if (CONF_CLIENT == aconf->status) {
647 if ((!wilds && (!match(aconf->host, host) ||
648 !match(aconf->name, host))) ||
649 (wilds && (!mmatch(host, aconf->host) ||
650 !mmatch(host, aconf->name))))
652 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
653 aconf->port, get_conf_class(aconf));
663 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
664 fda_get_block_count());
668 for (mptr = msgtab; mptr->cmd; mptr++)
670 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
675 report_configured_links(sptr, CONF_OPS);
680 * show listener ports
681 * show hidden ports to opers, if there are more than 3 parameters,
682 * interpret the fourth parameter as the port number, limit non-local
683 * or non-oper results to 8 ports.
685 show_ports(sptr, IsOper(sptr), (parc > 3) ? atoi(parv[3]) : 0, IsOper(sptr) ? 100 : 8);
690 send_usage(sptr, parv[0]);
694 report_crule_list(sptr, CRULE_ALL);
697 report_crule_list(sptr, CRULE_MASK);
700 tstats(sptr, parv[0]);
706 report_configured_links(sptr, CONF_UWORLD);
712 nowr = CurrentTime - cli_since(&me);
713 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
714 (nowr / 60) % 60, nowr % 60);
715 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
725 class_send_meminfo(sptr);
726 send_listinfo(sptr, parv[0]);
731 report_classes(sptr);
735 count_memory(sptr, parv[0]);
741 send_reply(sptr, RPL_ENDOFSTATS, stat);
746 * mo_stats - oper message handler
748 * parv[0] = sender prefix
749 * parv[1] = statistics selector (defaults to Message frequency)
750 * parv[2] = target server (current server defaulted, if omitted)
751 * And 'stats l' and 'stats' L:
752 * parv[3] = server mask ("*" defaulted, if omitted)
754 * parv[3] = port mask (returns p-lines when its port is matched by this)
755 * Or for stats k,K,i and I:
756 * parv[3] = [user@]host.name (returns which K/I-lines match this)
757 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
758 * (defaults to old reply if ommitted, when local or Oper)
759 * A remote mask (something containing wildcards) is only
760 * allowed for IRC Operators.
762 * parv[3] = time param
763 * parv[4] = time param
764 * (see report_memleak_stats() in runmalloc.c for details)
766 * This function is getting really ugly. -Ghostwolf
768 int mo_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
770 struct Message* mptr;
771 struct Client* acptr;
772 struct ConfItem* aconf;
773 char stat = parc > 1 ? parv[1][0] : '\0';
774 const char** infotext = statsinfo;
777 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
785 int doall = 0, wilds = 0;
787 if (parc > 3 && !EmptyString(parv[3])) {
789 wilds = string_has_wildcards(name);
794 * Send info about connections which match, or all if the
795 * mask matches me.name. Only restrictions are on those who
796 * are invisible not being visible to 'foreigners' who use
797 * a wild card based search to list it.
799 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
800 "SendM SendKBytes RcveM RcveKBytes :Open since");
801 for (i = 0; i <= HighestFd; i++)
803 if (!(acptr = LocalClientArray[i]))
805 /* Don't return clients when this is a request for `all' */
806 if (doall && IsUser(acptr))
808 /* Only show the ones that match the given mask - if any */
809 if (!doall && wilds && match(name, cli_name(acptr)))
811 /* Skip all that do not match the specific query */
812 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
814 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
815 "%s %u %u %u %u %u :%Tu", cli_name(acptr),
816 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
817 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
818 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
824 report_configured_links(sptr, CONF_SERVER);
827 case 'e': /* report engine name */
828 send_reply(sptr, RPL_STATSENGINE, engine_name());
831 case 'g': /* send glines */
836 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
839 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
840 if (0 == report_klines(sptr, (parc > 3) ? parv[3] : 0, 0))
845 feature_report(sptr);
855 report_configured_links(sptr, CONF_CLIENT);
858 if (EmptyString(parv[3]))
859 return need_more_params(sptr, "STATS I");
862 wilds = string_has_wildcards(host);
864 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
865 if (CONF_CLIENT == aconf->status) {
866 if ((!wilds && (!match(aconf->host, host) ||
867 !match(aconf->name, host))) ||
868 (wilds && (!mmatch(host, aconf->host) ||
869 !mmatch(host, aconf->name))))
871 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
872 aconf->port, get_conf_class(aconf));
882 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
883 fda_get_block_count());
887 for (mptr = msgtab; mptr->cmd; mptr++)
889 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
894 report_configured_links(sptr, CONF_OPS);
899 * show listener ports
900 * show hidden ports to opers, if there are more than 3 parameters,
901 * interpret the fourth parameter as the port number, limit non-local
902 * or non-oper results to 8 ports.
904 show_ports(sptr, 1, (parc > 3) ? atoi(parv[3]) : 0, 100);
909 send_usage(sptr, parv[0]);
913 report_crule_list(sptr, CRULE_ALL);
916 report_crule_list(sptr, CRULE_MASK);
919 tstats(sptr, parv[0]);
925 report_configured_links(sptr, CONF_UWORLD);
931 nowr = CurrentTime - cli_since(&me);
932 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
933 (nowr / 60) % 60, nowr % 60);
934 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
944 class_send_meminfo(sptr);
945 send_listinfo(sptr, parv[0]);
950 report_classes(sptr);
954 count_memory(sptr, parv[0]);
959 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
962 send_reply(sptr, RPL_ENDOFSTATS, stat);