bd6e59082f2c5cce388e4cddf34966506ce8ee91
[ircu2.10.12-pk.git] / ircd / m_stats.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_stats.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
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 1, or (at your option)
12  * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
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...).
34  *
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.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
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 ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
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*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #include "handlers.h"
85 /*
86  * XXX - ack!!!
87  */
88 #include "s_stats.h"
89 #include "channel.h"
90 #include "class.h"
91 #include "client.h"
92 #include "gline.h"
93 #include "hash.h"
94 #include "ircd.h"
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"
102 #include "list.h"
103 #include "listener.h"
104 #include "match.h"
105 #include "motd.h"
106 #include "msg.h"
107 #include "numeric.h"
108 #include "numnicks.h"
109 #include "opercmds.h"
110 #include "s_bsd.h"
111 #include "s_conf.h"
112 #include "s_debug.h"
113 #include "s_misc.h"
114 #include "s_serv.h"
115 #include "s_user.h"
116 #include "send.h"
117 #include "struct.h"
118 #include "userload.h"
119
120 #include <assert.h>
121 #include <stdlib.h>
122 #include <string.h>
123
124
125 int report_klines(struct Client* sptr, char* mask, int limit_query)
126 {
127   int   wilds = 0;
128   int   count = 3;
129   char* user  = 0;
130   char* host;
131   const struct DenyConf* conf;
132
133   if (EmptyString(mask)) {
134     if (limit_query)
135       return need_more_params(sptr, "STATS K");
136     else
137       report_deny_list(sptr);
138     return 1;
139   }
140
141   if (!limit_query) {
142     wilds = string_has_wildcards(mask);
143     count = 1000;
144   }
145
146   if ((host = strchr(mask, '@'))) {
147     user = mask;
148     *host++ = '\0';
149   }
150   else {
151     host = mask;
152   }
153
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))))
160     {
161       send_reply(sptr, RPL_STATSKLINE,
162                  (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
163                  conf->hostmask, conf->message, conf->usermask);
164       if (--count == 0)
165         return 1;
166     }
167   }
168   /* send_reply(sptr, RPL_ENDOFSTATS, stat); */
169   return 1;
170 }
171
172
173 /*
174  * m_stats - generic message handler
175  *
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)
181  * Or for stats p,P:
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.
189  * Or for stats M:
190  *    parv[3] = time param
191  *    parv[4] = time param 
192  *    (see report_memleak_stats() in runmalloc.c for details)
193  *
194  * This function is getting really ugly. -Ghostwolf
195  */
196 int m_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
197 {
198   char stat = parc > 1 ? parv[1][0] : '\0';
199   const char **infotext = statsinfo;
200
201   if (hunt_stats(cptr, sptr, parc, parv, stat, HEAD_IN_SAND_REMOTE)
202       != HUNTED_ISME)
203     return 0;
204
205   switch (stat)
206   {
207     case 'L':
208     case 'l':
209 #ifdef HEAD_IN_SAND_STATS_L
210         return m_not_oper(sptr,cptr,parc,parv);
211 #else
212     {
213       struct Client *acptr;
214       int i;
215       int doall = 0;
216       int wilds = 0;
217       char *name = "*";
218
219       if (parc > 3 && !EmptyString(parv[3])) {
220         name = parv[3];
221         wilds = string_has_wildcards(name);
222       }
223       else
224         doall = 1;
225       /*
226        * Send info about connections which match, or all if the
227        * mask matches me.name.  Only restrictions are on those who
228        * are invisible not being visible to 'foreigners' who use
229        * a wild card based search to list it.
230        */
231       send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
232                  "SendM SendKBytes RcveM RcveKBytes :Open since");
233       for (i = 0; i <= HighestFd; i++)
234       {
235         if (!(acptr = LocalClientArray[i]))
236           continue;
237         /* Don't return clients when this is a request for `all' */
238         if (doall && IsUser(acptr))
239           continue;
240         /* Don't show invisible people to non opers unless they know the nick */
241         if (IsInvisible(acptr) && (doall || wilds) && !IsAnOper(acptr) && (acptr != sptr))
242           continue;
243         /* Only show the ones that match the given mask - if any */
244         if (!doall && wilds && match(name, cli_name(acptr)))
245           continue;
246         /* Skip all that do not match the specific query */
247         if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
248           continue;
249         send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
250                    "%s %u %u %u %u %u :%Tu", (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
251                    (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
252                    (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
253                    (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
254       }
255     }
256 #endif
257       break;
258
259     case 'C':
260     case 'c':
261 #ifdef HEAD_IN_SAND_STATS_C
262       return m_not_oper(sptr,cptr,parc,parv);
263 #else
264       report_configured_links(sptr, CONF_SERVER);
265 #endif
266       break;
267
268     case 'E':
269     case 'e': /* report engine name */
270 #ifdef HEAD_IN_SAND_STATS_E
271       return m_not_oper(sptr,cptr,parc,parv);
272 #else
273       send_reply(sptr, RPL_STATSENGINE, engine_name());
274 #endif
275       break;
276
277     case 'G':
278     case 'g': /* send glines */
279 #ifdef HEAD_IN_SAND_STATS_G
280       return m_not_oper(sptr,cptr,parc,parv);
281 #else
282       gline_stats(sptr);
283 #endif
284       break;
285
286     case 'H':
287     case 'h':
288 #ifdef HEAD_IN_SAND_STATS_H
289       return m_not_oper(sptr,cptr,parc,parv);
290 #else
291       report_configured_links(sptr, CONF_HUB | CONF_LEAF);
292 #endif
293       break;
294
295     case 'K':
296     case 'k':    /* display CONF_IPKILL as well as CONF_KILL -Kev */
297 #ifdef HEAD_IN_SAND_STATS_K
298     /* Simple version - if you want to fix it - send in a patch */
299     return m_not_oper(sptr,cptr,parc,parv);
300 #else
301       if (0 == report_klines(sptr, (parc == 4) ? parv[3] : 0, 0))
302         return 0;
303 #endif
304       break;
305     case 'F':
306     case 'f':
307 #ifdef HEAD_IN_SAND_STATS_F
308       return m_not_oper(sptr,cptr,parc,parv);
309 #else
310       feature_report(sptr);
311 #endif
312       break;
313
314     case 'I':
315     case 'i':
316 #ifdef HEAD_IN_SAND_STATS_I
317     /* Simple version - if you want to fix it - send in a patch */
318     return m_not_oper(sptr,cptr,parc,parv);
319 #else
320     {
321       struct ConfItem *aconf;
322       int wilds = 0;
323       int count = 1000;
324       char* host;
325
326       if (parc < 4) {
327         report_configured_links(sptr, CONF_CLIENT);
328         break;
329       }
330       if (EmptyString(parv[3]))
331         return need_more_params(sptr, "STATS I");
332
333       host = parv[3];
334       wilds = string_has_wildcards(host);
335
336       for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
337         if (CONF_CLIENT == aconf->status) {
338           if ((!wilds && (!match(aconf->host, host) ||
339               !match(aconf->name, host))) ||
340               (wilds && (!mmatch(host, aconf->host) ||
341               !mmatch(host, aconf->name))))
342           {
343             send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
344                        aconf->port, get_conf_class(aconf));
345             if (--count == 0)
346               break;
347           }
348         }
349       }
350     }
351 #endif
352       break;
353
354     case 'M':
355 #ifdef HEAD_IN_SAND_STATS_M
356       return m_not_oper(sptr,cptr,parc,parv);
357 #else
358 #if !defined(NDEBUG)
359       send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
360                  fda_get_block_count());
361 #endif
362 #endif
363       break;
364
365     case 'm':
366 #ifdef HEAD_IN_SAND_STATS_m
367       return m_not_oper(sptr,cptr,parc,parv);
368 #else
369     {
370       struct Message *mptr;
371
372       for (mptr = msgtab; mptr->cmd; mptr++)
373         if (mptr->count)
374           send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
375                      mptr->bytes);
376     }
377 #endif
378       break;
379
380     case 'o':
381     case 'O':
382 #ifdef HEAD_IN_SAND_STATS_O
383       return m_not_oper(sptr,cptr,parc,parv);
384 #else
385       report_configured_links(sptr, CONF_OPS);
386 #endif
387       break;
388
389     case 'p':
390     case 'P':
391       /*
392        * show listener ports
393        * show hidden ports to opers, if there are more than 3 parameters,
394        * interpret the fourth parameter as the port number.
395        */ 
396 #ifdef HEAD_IN_SAND_STATS_P
397       return m_not_oper(sptr,cptr,parc,parv);
398 #else
399       show_ports(sptr, 0, (parc > 3) ? atoi(parv[3]) : 0, 100);
400 #endif
401       break;
402
403     case 'R':
404     case 'r':
405 #ifdef HEAD_IN_SAND_STATS_R
406       return m_not_oper(sptr,cptr,parc,parv);
407 #else
408 #ifdef DEBUGMODE
409       send_usage(sptr, parv[0]);
410 #endif
411 #endif
412       break;
413
414     case 'D':
415 #ifdef HEAD_IN_SAND_STATS_D
416       return m_not_oper(sptr,cptr,parc,parv);
417 #else
418       report_crule_list(sptr, CRULE_ALL);
419 #endif
420       break;
421
422     case 'd':
423 #ifdef HEAD_IN_SAND_STATS_d
424       return m_not_oper(sptr,cptr,parc,parv);
425 #else
426       report_crule_list(sptr, CRULE_MASK);
427 #endif
428       break;
429
430     case 't':
431 #ifdef HEAD_IN_SAND_STATS_t
432       return m_not_oper(sptr,cptr,parc,parv);
433 #else
434       tstats(sptr, parv[0]);
435 #endif
436       break;
437
438     case 'T':
439 #ifdef HEAD_IN_SAND_STATS_T
440       return m_not_oper(sptr,cptr,parc,parv);
441 #else
442       motd_report(sptr);
443 #endif
444       break;
445
446     case 'U':
447 #ifdef HEAD_IN_SAND_STATS_U
448       return m_not_oper(sptr,cptr,parc,parv);
449 #else
450       report_configured_links(sptr, CONF_UWORLD);
451 #endif
452       break;
453
454     case 'u':
455 #ifdef HEAD_IN_SAND_STATS_u
456       return m_not_oper(sptr,cptr,parc,parv);
457 #else
458     {
459       time_t nowr;
460
461       nowr = CurrentTime - cli_since(&me);
462       send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
463                  (nowr / 60) % 60, nowr % 60);
464       send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
465     }
466 #endif
467       break;
468
469     case 'W':
470     case 'w':
471 #ifdef HEAD_IN_SAND_STATS_W
472       return m_not_oper(sptr,cptr,parc,parv);
473 #else
474       calc_load(sptr);
475 #endif
476       break;
477
478     case 'X':
479     case 'x':
480 #ifdef HEAD_IN_SAND_STATS_X
481       return m_not_oper(sptr,cptr,parc,parv);
482 #else
483 #ifdef  DEBUGMODE
484       class_send_meminfo(sptr);
485       send_listinfo(sptr, parv[0]);
486 #endif
487 #endif
488       break;
489
490     case 'Y':
491     case 'y':
492 #ifdef HEAD_IN_SAND_STATS_Y
493       return m_not_oper(sptr,cptr,parc,parv);
494 #else
495       report_classes(sptr);
496 #endif
497       break;
498
499     case 'Z':
500     case 'z':
501 #ifdef HEAD_IN_SAND_STATS_Z
502       return m_not_oper(sptr,cptr,parc,parv);
503 #else
504       count_memory(sptr, parv[0]);
505 #endif
506       break;
507
508     default:
509       stat = '*';
510       while (*infotext)
511         sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
512       break;
513   }
514   send_reply(sptr, RPL_ENDOFSTATS, stat);
515   return 0;
516 }
517
518 /*
519  * ms_stats - server message handler
520  *
521  *    parv[0] = sender prefix
522  *    parv[1] = statistics selector (defaults to Message frequency)
523  *    parv[2] = target server (current server defaulted, if omitted)
524  * And 'stats l' and 'stats' L:
525  *    parv[3] = server mask ("*" defaulted, if omitted)
526  * Or for stats p,P:
527  *    parv[3] = port mask (returns p-lines when its port is matched by this)
528  * Or for stats k,K,i and I:
529  *    parv[3] = [user@]host.name  (returns which K/I-lines match this)
530  *           or [user@]host.mask  (returns which K/I-lines are mmatched by this)
531  *              (defaults to old reply if ommitted, when local or Oper)
532  *              A remote mask (something containing wildcards) is only
533  *              allowed for IRC Operators.
534  * Or for stats M:
535  *    parv[3] = time param
536  *    parv[4] = time param 
537  *    (see report_memleak_stats() in runmalloc.c for details)
538  *
539  * This function is getting really ugly. -Ghostwolf
540  */
541 int ms_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
542 {
543   struct Message *mptr;
544   struct Client *acptr;
545   struct ConfItem *aconf;
546   char stat = parc > 1 ? parv[1][0] : '\0';
547   int i;
548
549   if (hunt_stats(cptr, sptr, parc, parv, stat, 0) != HUNTED_ISME)
550     return 0;
551
552   switch (stat)
553   {
554     case 'L':
555     case 'l':
556     {
557       int doall = 0;
558       int wilds = 0;
559       char *name = "*";
560
561       if (parc > 3 && !EmptyString(parv[3])) {
562         name = parv[3];
563         wilds = string_has_wildcards(name);
564       }
565       else
566         doall = 1;
567       /*
568        * Send info about connections which match, or all if the
569        * mask matches me.name.  Only restrictions are on those who
570        * are invisible not being visible to 'foreigners' who use
571        * a wild card based search to list it.
572        */
573       send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
574                  "SendM SendKBytes RcveM RcveKBytes :Open since");
575       for (i = 0; i <= HighestFd; i++)
576       {
577         if (!(acptr = LocalClientArray[i]))
578           continue;
579         /* Don't return clients when this is a request for `all' */
580         if (doall && IsUser(acptr))
581           continue;
582         /* Don't show invisible people to unauthorized people when using
583          * wildcards  -- Is this still needed now /stats is oper only ? 
584          * Not here, because ms_stats is specifically a remote command, 
585          * thus the check was removed. -Ghostwolf */
586         /* Only show the ones that match the given mask - if any */
587         if (!doall && wilds && match(name, cli_name(acptr)))
588           continue;
589         /* Skip all that do not match the specific query */
590         if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
591           continue;
592         send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
593                    "%s %u %u %u %u %u :%Tu", cli_name(acptr),
594                    (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
595                    (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
596                    (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
597       }
598       break;
599     }
600     case 'C':
601     case 'c':
602       report_configured_links(sptr, CONF_SERVER);
603       break;
604     case 'E':
605     case 'e': /* report engine name */
606       send_reply(sptr, RPL_STATSENGINE, engine_name());
607       break;
608     case 'G':
609     case 'g': /* send glines */
610       gline_stats(sptr);
611       break;
612     case 'H':
613     case 'h':
614       report_configured_links(sptr, CONF_HUB | CONF_LEAF);
615       break;
616     case 'K':
617     case 'k':    /* display CONF_IPKILL as well as CONF_KILL -Kev */
618       if (0 == report_klines(sptr, (parc > 3) ? parv[3] : 0, !IsOper(sptr)))
619         return 0;
620       break;
621     case 'F':
622     case 'f':
623       feature_report(sptr);
624       break;
625     case 'I':
626     case 'i':
627     {
628       int   wilds = 0;
629       int   count = 3;
630       char* host;
631
632       if (parc < 4 && IsOper(sptr)) {
633         report_configured_links(sptr, CONF_CLIENT);
634         break;
635       }
636       if (parc < 4 || EmptyString(parv[3]))
637         return need_more_params(sptr, "STATS I");
638
639       if (IsOper(sptr)) {
640         wilds = string_has_wildcards(parv[3]);
641         count = 1000;
642       }
643
644       host = parv[3];
645
646       for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
647         if (CONF_CLIENT == aconf->status) {
648           if ((!wilds && (!match(aconf->host, host) ||
649               !match(aconf->name, host))) ||
650               (wilds && (!mmatch(host, aconf->host) ||
651               !mmatch(host, aconf->name))))
652           {
653             send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
654                        aconf->port, get_conf_class(aconf));
655             if (--count == 0)
656               break;
657           }
658         }
659       }
660       break;
661     }
662     case 'M':
663 #if !defined(NDEBUG)
664       send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
665                  fda_get_block_count());
666 #endif
667       break;
668     case 'm':
669       for (mptr = msgtab; mptr->cmd; mptr++)
670         if (mptr->count)
671           send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
672                      mptr->bytes);
673       break;
674     case 'o':
675     case 'O':
676       report_configured_links(sptr, CONF_OPS);
677       break;
678     case 'p':
679     case 'P':
680       /*
681        * show listener ports
682        * show hidden ports to opers, if there are more than 3 parameters,
683        * interpret the fourth parameter as the port number, limit non-local
684        * or non-oper results to 8 ports.
685        */ 
686       show_ports(sptr, IsOper(sptr), (parc > 3) ? atoi(parv[3]) : 0, IsOper(sptr) ? 100 : 8);
687       break;
688     case 'R':
689     case 'r':
690 #ifdef DEBUGMODE
691       send_usage(sptr, parv[0]);
692 #endif
693       break;
694     case 'D':
695       report_crule_list(sptr, CRULE_ALL);
696       break;
697     case 'd':
698       report_crule_list(sptr, CRULE_MASK);
699       break;
700     case 't':
701       tstats(sptr, parv[0]);
702       break;
703     case 'T':
704       motd_report(sptr);
705       break;
706     case 'U':
707       report_configured_links(sptr, CONF_UWORLD);
708       break;
709     case 'u':
710     {
711       time_t nowr;
712
713       nowr = CurrentTime - cli_since(&me);
714       send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
715                  (nowr / 60) % 60, nowr % 60);
716       send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
717       break;
718     }
719     case 'W':
720     case 'w':
721       calc_load(sptr);
722       break;
723     case 'X':
724     case 'x':
725 #ifdef  DEBUGMODE
726       class_send_meminfo(sptr);
727       send_listinfo(sptr, parv[0]);
728 #endif
729       break;
730     case 'Y':
731     case 'y':
732       report_classes(sptr);
733       break;
734     case 'Z':
735     case 'z':
736       count_memory(sptr, parv[0]);
737       break;
738     default:
739       stat = '*';
740       break;
741   }
742   send_reply(sptr, RPL_ENDOFSTATS, stat);
743   return 0;
744 }
745
746 /*
747  * mo_stats - oper message handler
748  *
749  *    parv[0] = sender prefix
750  *    parv[1] = statistics selector (defaults to Message frequency)
751  *    parv[2] = target server (current server defaulted, if omitted)
752  * And 'stats l' and 'stats' L:
753  *    parv[3] = server mask ("*" defaulted, if omitted)
754  * Or for stats p,P:
755  *    parv[3] = port mask (returns p-lines when its port is matched by this)
756  * Or for stats k,K,i and I:
757  *    parv[3] = [user@]host.name  (returns which K/I-lines match this)
758  *           or [user@]host.mask  (returns which K/I-lines are mmatched by this)
759  *              (defaults to old reply if ommitted, when local or Oper)
760  *              A remote mask (something containing wildcards) is only
761  *              allowed for IRC Operators.
762  * Or for stats M:
763  *    parv[3] = time param
764  *    parv[4] = time param 
765  *    (see report_memleak_stats() in runmalloc.c for details)
766  *
767  * This function is getting really ugly. -Ghostwolf
768  */
769 int mo_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
770 {
771   struct Message*  mptr;
772   struct Client*   acptr;
773   struct ConfItem* aconf;
774   char             stat = parc > 1 ? parv[1][0] : '\0';
775   const char**     infotext = statsinfo;
776   int              i;
777
778   if (hunt_stats(cptr, sptr, parc, parv, stat, HEAD_IN_SAND_REMOTE)
779       != HUNTED_ISME)
780     return 0;
781
782   switch (stat)
783   {
784     case 'L':
785     case 'l':
786     {
787       int doall = 0, wilds = 0;
788       char* name = "*";
789       if (parc > 3 && !EmptyString(parv[3])) {
790         name = parv[3];
791         wilds = string_has_wildcards(name);
792       }
793       else
794         doall = 1;
795       /*
796        * Send info about connections which match, or all if the
797        * mask matches me.name.  Only restrictions are on those who
798        * are invisible not being visible to 'foreigners' who use
799        * a wild card based search to list it.
800        */
801       send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
802                  "SendM SendKBytes RcveM RcveKBytes :Open since");
803       for (i = 0; i <= HighestFd; i++)
804       {
805         if (!(acptr = LocalClientArray[i]))
806           continue;
807         /* Don't return clients when this is a request for `all' */
808         if (doall && IsUser(acptr))
809           continue;
810         /* Only show the ones that match the given mask - if any */
811         if (!doall && wilds && match(name, cli_name(acptr)))
812           continue;
813         /* Skip all that do not match the specific query */
814         if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
815           continue;
816         send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
817                    "%s %u %u %u %u %u :%Tu", cli_name(acptr),
818                    (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
819                    (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
820                    (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
821       }
822       break;
823     }
824     case 'C':
825     case 'c':
826       report_configured_links(sptr, CONF_SERVER);
827       break;
828     case 'E':
829     case 'e': /* report engine name */
830       send_reply(sptr, RPL_STATSENGINE, engine_name());
831       break;
832     case 'G':
833     case 'g': /* send glines */
834       gline_stats(sptr);
835       break;
836     case 'H':
837     case 'h':
838       report_configured_links(sptr, CONF_HUB | CONF_LEAF);
839       break;
840     case 'K':
841     case 'k':    /* display CONF_IPKILL as well as CONF_KILL -Kev */
842       if (0 == report_klines(sptr, (parc > 3) ? parv[3] : 0, 0))
843         return 0;
844       break;
845     case 'F':
846     case 'f':
847       feature_report(sptr);
848       break;
849     case 'I':
850     case 'i':
851       {
852         int   wilds = 0;
853         int   count = 1000;
854         char* host;
855
856         if (parc < 4) {
857           report_configured_links(sptr, CONF_CLIENT);
858           break;
859         }
860         if (EmptyString(parv[3]))
861           return need_more_params(sptr, "STATS I");
862
863         host = parv[3];
864         wilds = string_has_wildcards(host);
865
866         for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
867           if (CONF_CLIENT == aconf->status) {
868             if ((!wilds && (!match(aconf->host, host) ||
869                 !match(aconf->name, host))) ||
870                 (wilds && (!mmatch(host, aconf->host) ||
871                 !mmatch(host, aconf->name))))
872             {
873               send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
874                          aconf->port, get_conf_class(aconf));
875               if (--count == 0)
876                 break;
877             }
878           }
879         }
880       }
881       break;
882     case 'M':
883 #if !defined(NDEBUG)
884       send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
885                  fda_get_block_count());
886 #endif
887       break;
888     case 'm':
889       for (mptr = msgtab; mptr->cmd; mptr++)
890         if (mptr->count)
891           send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
892                      mptr->bytes);
893       break;
894     case 'o':
895     case 'O':
896       report_configured_links(sptr, CONF_OPS);
897       break;
898     case 'p':
899     case 'P':
900       /*
901        * show listener ports
902        * show hidden ports to opers, if there are more than 3 parameters,
903        * interpret the fourth parameter as the port number, limit non-local
904        * or non-oper results to 8 ports.
905        */ 
906       show_ports(sptr, 1, (parc > 3) ? atoi(parv[3]) : 0, 100);
907       break;
908     case 'R':
909     case 'r':
910 #ifdef DEBUGMODE
911       send_usage(sptr, parv[0]);
912 #endif
913       break;
914     case 'D':
915       report_crule_list(sptr, CRULE_ALL);
916       break;
917     case 'd':
918       report_crule_list(sptr, CRULE_MASK);
919       break;
920     case 't':
921       tstats(sptr, parv[0]);
922       break;
923     case 'T':
924       motd_report(sptr);
925       break;
926     case 'U':
927       report_configured_links(sptr, CONF_UWORLD);
928       break;
929     case 'u':
930     {
931       time_t nowr;
932
933       nowr = CurrentTime - cli_since(&me);
934       send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
935                  (nowr / 60) % 60, nowr % 60);
936       send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
937       break;
938     }
939     case 'W':
940     case 'w':
941       calc_load(sptr);
942       break;
943     case 'X':
944     case 'x':
945 #ifdef  DEBUGMODE
946       class_send_meminfo(sptr);
947       send_listinfo(sptr, parv[0]);
948 #endif
949       break;
950     case 'Y':
951     case 'y':
952       report_classes(sptr);
953       break;
954     case 'Z':
955     case 'z':
956       count_memory(sptr, parv[0]);
957       break;
958     default:
959       stat = '*';
960       while (*infotext)
961         sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
962       break;
963   }
964   send_reply(sptr, RPL_ENDOFSTATS, stat);
965   return 0;
966 }
967