Doxyfy s_stats.h and s_stats.c.
[ircu2.10.12-pk.git] / ircd / s_stats.c
1 /*
2  * IRC - Internet Relay Chat, ircd/s_stats.c
3  * Copyright (C) 2000 Joseph Bongaarts
4  *
5  * See file AUTHORS in IRC package for additional names of
6  * the programmers.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 1, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 #include "config.h"
23
24 #include "class.h"
25 #include "client.h"
26 #include "gline.h"
27 #include "ircd.h"
28 #include "ircd_chattr.h"
29 #include "ircd_events.h"
30 #include "ircd_features.h"
31 #include "ircd_crypt.h"
32 #include "ircd_log.h"
33 #include "ircd_reply.h"
34 #include "ircd_string.h"
35 #include "listener.h"
36 #include "list.h"
37 #include "match.h"
38 #include "motd.h"
39 #include "msg.h"
40 #include "msgq.h"
41 #include "numeric.h"
42 #include "numnicks.h"
43 #include "res.h"
44 #include "s_bsd.h"
45 #include "s_conf.h"
46 #include "s_debug.h"
47 #include "s_misc.h"
48 #include "s_serv.h"
49 #include "s_stats.h"
50 #include "s_user.h"
51 #include "send.h"
52 #include "struct.h"
53 #include "userload.h"
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <sys/time.h>
59
60 /** @file
61  * @brief Report configuration lines and other statistics from this
62  * server.
63  * @version $Id$
64  *
65  * Note: The info is reported in the order the server uses
66  *       it--not reversed as in ircd.conf!
67  */
68
69 /** Stats response to use for various ConfItem types. */
70 static unsigned int report_array[][3] = {
71   {CONF_SERVER, RPL_STATSCLINE, 'C'},
72   {CONF_CLIENT, RPL_STATSILINE, 'I'},
73   {CONF_LEAF, RPL_STATSLLINE, 'L'},
74   {CONF_OPERATOR, RPL_STATSOLINE, 'O'},
75   {CONF_HUB, RPL_STATSHLINE, 'H'},
76   {CONF_UWORLD, RPL_STATSULINE, 'U'},
77   {0, 0}
78 };
79
80 /* The statsinfo array should only be used in this file, but just TRY
81  * telling the compiler that you want to forward declare a static
82  * array without specifying a length, and see how it responds.  So we
83  * forward declare it "extern".
84  */
85 extern struct StatDesc statsinfo[];
86
87 /** Report items from #GlobalConfList.
88  * Uses sd->sd_funcdata as a filter for ConfItem::status.
89  * @param[in] sptr Client requesting statistics.
90  * @param[in] sd Stats descriptor for request.
91  * @param[in] param Extra parameter from user (ignored).
92  */
93 static void
94 stats_configured_links(struct Client *sptr, const struct StatDesc* sd,
95                        char* param)
96 {
97   static char null[] = "<NULL>";
98   struct ConfItem *tmp;
99   int mask;
100   unsigned int *p;
101   unsigned short int port;
102   char c, *host, *pass, *name;
103
104   mask = sd->sd_funcdata;
105
106   for (tmp = GlobalConfList; tmp; tmp = tmp->next) 
107   {
108     if ((tmp->status & mask))
109     {
110       for (p = &report_array[0][0]; *p; p += 3)
111         if (*p == tmp->status)
112           break;
113       if (!*p)
114         continue;
115       c = (char)*(p + 2);
116       host = BadPtr(tmp->host) ? null : tmp->host;
117       pass = BadPtr(tmp->passwd) ? null : tmp->passwd;
118       name = BadPtr(tmp->name) ? null : tmp->name;
119       port = tmp->address.port;
120       /*
121        * On K line the passwd contents can be
122        * displayed on STATS reply.    -Vesa
123        */
124       /* Special-case 'k' or 'K' lines as appropriate... -Kev */
125       if ((tmp->status & CONF_UWORLD))
126         send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
127       else if ((tmp->status & (CONF_SERVER | CONF_HUB)))
128         send_reply(sptr, p[1], c, "*", name, port, get_conf_class(tmp));
129       else if ((tmp->status & CONF_CLIENT))
130       {
131         if(tmp->passwd && IsDigit(*tmp->passwd) && (!tmp->passwd[1] ||
132             (IsDigit(tmp->passwd[1]) && !tmp->passwd[2])))
133           send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
134         else
135           send_reply(sptr, p[1], c, host, "*", name, port, get_conf_class(tmp));
136       }
137       else
138         send_reply(sptr, p[1], c, host, name, port, get_conf_class(tmp));
139     }
140   }
141 }
142
143 /** Report connection rules from conf_get_crule_list().
144  * Uses sd->sd_funcdata as a filter for CRuleConf::type.
145  * @param[in] to Client requesting statistics.
146  * @param[in] sd Stats descriptor for request.
147  * @param[in] param Extra parameter from user (ignored).
148  */
149 static void
150 stats_crule_list(struct Client* to, const struct StatDesc *sd,
151                  char *param)
152 {
153   const struct CRuleConf* p = conf_get_crule_list();
154
155   for ( ; p; p = p->next)
156   {
157     if (p->type & sd->sd_funcdata)
158       send_reply(to, RPL_STATSDLINE, sd->sd_c, p->hostmask, p->rule);
159   }
160 }
161
162 /** Report active event engine name.
163  * @param[in] to Client requesting statistics.
164  * @param[in] sd Stats descriptor for request (ignored).
165  * @param[in] param Extra parameter from user (ignored).
166  */
167 static void
168 stats_engine(struct Client *to, const struct StatDesc *sd, char *param)
169 {
170   send_reply(to, RPL_STATSENGINE, engine_name());
171 }
172
173 /** Report client access lists.
174  * @param[in] to Client requesting statistics.
175  * @param[in] sd Stats descriptor for request.
176  * @param[in] param Filter for hostname or IP (NULL to show all).
177  */
178 static void
179 stats_access(struct Client *to, const struct StatDesc *sd, char *param)
180 {
181   struct ConfItem *aconf;
182   int wilds = 0;
183   int count = 1000;
184
185   if (!param)
186   {
187     stats_configured_links(to, sd, param);
188     return;
189   }
190
191   wilds = string_has_wildcards(param);
192
193   for (aconf = GlobalConfList; aconf; aconf = aconf->next)
194   {
195     if (aconf->status != CONF_CLIENT)
196       continue;
197     if ((!wilds && (!match(aconf->host, param) ||
198                     !match(aconf->name, param))) ||
199         (wilds && (!mmatch(param, aconf->host) ||
200                    !mmatch(param, aconf->name))))
201     {
202       send_reply(to, RPL_STATSILINE, 'I', aconf->host, aconf->name,
203                  aconf->address.port, get_conf_class(aconf));
204       if (--count == 0)
205         break;
206     }
207   }
208 }
209
210
211 /** Report DenyConf entries.
212  * @param[in] to Client requesting list.
213  */
214 static void
215 report_deny_list(struct Client* to)
216 {
217   const struct DenyConf* p = conf_get_deny_list();
218   for ( ; p; p = p->next)
219     send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
220                p->hostmask, p->message, p->usermask);
221 }
222
223 /** Report K/k-lines to a user.
224  * @param[in] sptr Client requesting statistics.
225  * @param[in] sd Stats descriptor for request (ignored).
226  * @param[in] mask Filter for hostmasks to show.
227  */
228 static void
229 stats_klines(struct Client *sptr, const struct StatDesc *sd, char *mask)
230 {
231   int wilds = 0;
232   int count = 3;
233   int limit_query = 0;
234   char *user  = 0;
235   char *host;
236   const struct DenyConf* conf;
237
238   if (!IsAnOper(sptr))
239     limit_query = 1;
240
241   if (!mask)
242   {
243     if (limit_query)
244       need_more_params(sptr, "STATS K");
245     else
246       report_deny_list(sptr);
247     return;
248   }
249
250   if (!limit_query)
251   {
252     wilds = string_has_wildcards(mask);
253     count = 1000;
254   }
255   if ((host = strchr(mask, '@')))
256   {
257     user = mask;
258     *host++ = '\0';
259   }
260   else
261     host = mask;
262
263   for (conf = conf_get_deny_list(); conf; conf = conf->next)
264   {
265     if ((!wilds && ((user || conf->hostmask) &&
266                     !match(conf->hostmask, host) &&
267                     (!user || !match(conf->usermask, user)))) ||
268         (wilds && !mmatch(host, conf->hostmask) &&
269          (!user || !mmatch(user, conf->usermask))))
270     {
271       send_reply(sptr, RPL_STATSKLINE,
272                  (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
273                  conf->hostmask, conf->message, conf->usermask);
274       if (--count == 0)
275         return;
276     }
277   }
278 }
279
280 /** Report on servers and/or clients connected to the network.
281  * @param[in] sptr Client requesting statistics.
282  * @param[in] sd Stats descriptor for request (ignored).
283  * @param[in] name Filter for client names to show.
284  */
285 static void
286 stats_links(struct Client* sptr, const struct StatDesc* sd, char* name)
287 {
288   struct Client *acptr;
289   int i;
290   int wilds = 0;
291
292   if (name)
293     wilds = string_has_wildcards(name);
294
295   /*
296    * Send info about connections which match, or all if the
297    * mask matches me.name.  Only restrictions are on those who
298    * are invisible not being visible to 'foreigners' who use
299    * a wild card based search to list it.
300    */
301   send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
302              "SendM SendKBytes RcveM RcveKBytes :Open since");
303     for (i = 0; i <= HighestFd; i++)
304     {
305       if (!(acptr = LocalClientArray[i]))
306         continue;
307       /* Don't return clients when this is a request for `all' */
308       if (!name && IsUser(acptr))
309         continue;
310       /* Don't show invisible people to non opers unless they know the nick */
311       if (IsInvisible(acptr) && (!name || wilds) && !IsAnOper(acptr) &&
312           (acptr != sptr))
313         continue;
314       /* Only show the ones that match the given mask - if any */
315       if (name && wilds && match(name, cli_name(acptr)))
316         continue;
317       /* Skip all that do not match the specific query */
318       if (!(!name || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
319         continue;
320       send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
321                  "%s %u %u %u %u %u :%Tu",
322                  (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
323                  (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
324                  (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
325                  (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
326     }
327 }
328
329 /** Report on loaded modules.
330  * @param[in] to Client requesting statistics.
331  * @param[in] sd Stats descriptor for request (ignored).
332  * @param[in] param Extra parameter from user (ignored).
333  */
334 static void
335 stats_modules(struct Client* to, const struct StatDesc* sd, char* param)
336 {
337 crypt_mechs_t* mechs;
338
339   send_reply(to, SND_EXPLICIT | RPL_STATSLLINE, 
340    "Module  Description      Entry Point");
341
342  /* atm the only "modules" we have are the crypto mechanisms,
343     eventualy they'll be part of a global dl module list, for now
344     i'll just output data about them -- hikari */
345
346  if(crypt_mechs_root == NULL)
347   return;
348
349  mechs = crypt_mechs_root->next;
350
351  for(;;)
352  {
353   if(mechs == NULL)
354    return;
355
356   send_reply(to, SND_EXPLICIT | RPL_STATSLLINE, 
357    "%s  %s     0x%X", 
358    mechs->mech->shortname, mechs->mech->description, 
359    mechs->mech->crypt_function);
360
361   mechs = mechs->next;
362  }
363
364 }
365
366 /** Report how many times each command has been used.
367  * @param[in] to Client requesting statistics.
368  * @param[in] sd Stats descriptor for request (ignored).
369  * @param[in] param Extra parameter from user (ignored).
370  */
371 static void
372 stats_commands(struct Client* to, const struct StatDesc* sd, char* param)
373 {
374   struct Message *mptr;
375
376   for (mptr = msgtab; mptr->cmd; mptr++)
377     if (mptr->count)
378       send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
379 }
380
381 /** List channel quarantines.
382  * @param[in] to Client requesting statistics.
383  * @param[in] sd Stats descriptor for request (ignored).
384  * @param[in] param Filter for quarantined channel names.
385  */
386 static void
387 stats_quarantine(struct Client* to, const struct StatDesc* sd, char* param)
388 {
389   struct qline *qline;
390
391   for (qline = GlobalQuarantineList; qline; qline = qline->next)
392   {
393     if (param && match(param, qline->chname)) /* narrow search */
394       continue;
395     send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
396   }
397 }
398
399 /** List service pseudo-command mappings.
400  * @param[in] to Client requesting statistics.
401  * @param[in] sd Stats descriptor for request (ignored).
402  * @param[in] param Extra parameter from user (ignored).
403  */
404 static void
405 stats_mapping(struct Client *to, const struct StatDesc* sd, char* param)
406 {
407   struct s_map *map;
408
409   send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
410   for (map = GlobalServiceMapList; map; map = map->next) {
411     struct nick_host *nh;
412     for (nh = map->services; nh; nh = nh->next) {
413       send_reply(to, RPL_STATSRLINE, map->command, map->name,
414                  (map->prepend ? map->prepend : "*"), nh->nick);
415     }
416   }
417 }
418
419 /** Report server uptime and maximum connection/client counts.
420  * @param[in] to Client requesting statistics.
421  * @param[in] sd Stats descriptor for request (ignored).
422  * @param[in] param Extra parameter from user (ignored).
423  */
424 static void
425 stats_uptime(struct Client* to, const struct StatDesc* sd, char* param)
426 {
427   time_t nowr;
428
429   nowr = CurrentTime - cli_since(&me);
430   send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
431              (nowr / 60) % 60, nowr % 60);
432   send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
433 }
434
435 /** Verbosely report on servers connected to the network.
436  * If sd->sd_funcdata != 0, then display in a more human-friendly format.
437  * @param[in] sptr Client requesting statistics.
438  * @param[in] sd Stats descriptor for request.
439  * @param[in] param Filter for server names to display.
440  */
441 static void
442 stats_servers_verbose(struct Client* sptr, const struct StatDesc* sd,
443                       char* param)
444 {
445   struct Client *acptr;
446   const char *fmt;
447
448   /*
449    * lowercase 'v' is for human-readable,
450    * uppercase 'V' is for machine-readable
451    */
452   if (sd->sd_funcdata) {
453     send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
454                "%-20s %-20s Flags Hops Numeric   Lag  RTT   Up Down "
455                "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
456                "LinkTS");
457     fmt = "%-20s %-20s %c%c%c%c  %4i %s %-4i %5i %4i %4i %4i %5i %5i P%-2i   %Tu :%s";
458   } else {
459     fmt = "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s";
460   }
461
462   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
463   {
464     if (!IsServer(acptr) && !IsMe(acptr))
465       continue;
466     /* narrow search */
467     if (param && match(param, cli_name(acptr)))
468       continue;
469     send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, fmt,
470                cli_name(acptr),
471                cli_name(cli_serv(acptr)->up),
472                IsBurst(acptr) ? 'B' : '-',
473                IsBurstAck(acptr) ? 'A' : '-',
474                IsHub(acptr) ? 'H' : '-',
475                IsService(acptr) ? 'S' : '-',
476                cli_hopcount(acptr),
477                NumServ(acptr),
478                base64toint(cli_yxx(acptr)),
479                cli_serv(acptr)->lag,
480                cli_serv(acptr)->asll_rtt,
481                cli_serv(acptr)->asll_to,
482                cli_serv(acptr)->asll_from,
483                cli_serv(acptr)->clients,
484                cli_serv(acptr)->nn_mask,
485                cli_serv(acptr)->prot,
486                cli_serv(acptr)->timestamp,
487                cli_info(acptr));
488   }
489 }
490
491 #ifdef DEBUGMODE
492 /** Display objects allocated (and total memory used by them) for
493  * several types of structures.
494  * @param[in] to Client requesting statistics.
495  * @param[in] sd Stats descriptor for request (ignored).
496  * @param[in] param Extra parameter from user (ignored).
497  */
498 static void
499 stats_meminfo(struct Client* to, const struct StatDesc* sd, char* param)
500 {
501   class_send_meminfo(to);
502   send_listinfo(to, 0);
503 }
504 #endif
505
506 /** Send a list of available statistics.
507  * @param[in] to Client requesting statistics.
508  * @param[in] sd Stats descriptor for request.
509  * @param[in] param Extra parameter from user (ignored).
510  */
511 static void
512 stats_help(struct Client* to, const struct StatDesc* sd, char* param)
513 {
514   struct StatDesc *asd;
515
516   /* only if it's my user */
517   if (MyUser(to))
518     for (asd = statsinfo; asd->sd_name; asd++)
519       if (asd != sd) /* don't send the help for us */
520         sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c (%s) - %s", to, asd->sd_c,
521                       asd->sd_name, asd->sd_desc);
522 }
523
524 /** Contains information about all statistics. */
525 struct StatDesc statsinfo[] = {
526   { 'a', "nameservers", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_a,
527     report_dns_servers, 0,
528     "DNS servers." },
529   { 'c', "connect", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
530     stats_configured_links, CONF_SERVER,
531     "Remote server connection lines." },
532   { 'd', "maskrules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
533     stats_crule_list, CRULE_MASK,
534     "Dynamic routing configuration." },
535   { 'D', "crules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
536     stats_crule_list, CRULE_ALL,
537     "Dynamic routing configuration." },
538   { 'e', "engine", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
539     stats_engine, 0,
540     "Report server event loop engine." },
541   { 'f', "features", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
542     feature_report, 0,
543     "Feature settings." },
544   { 'g', "glines", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
545     gline_stats, 0,
546     "Global bans (G-lines)." },
547   { 'h', "hubs", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_h,
548     stats_configured_links, (CONF_HUB | CONF_LEAF),
549     "Hubs information." },
550   { 'i', "access", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
551     stats_access, CONF_CLIENT,
552     "Connection authorization lines." },
553   { 'j', "histogram", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
554     msgq_histogram, 0,
555     "Message length histogram." },
556   { 'k', "klines", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
557     stats_klines, 0,
558     "Local bans (K-Lines)." },
559   { 'l', "links", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
560     FEAT_HIS_STATS_l,
561     stats_links, 0,
562     "Current connections information." },
563   { 'L', "modules", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
564     FEAT_HIS_STATS_L,
565     stats_modules, 0,
566     "Dynamically loaded modules." },
567   { 'm', "commands", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
568     stats_commands, 0,
569     "Message usage information." },
570   { 'o', "operators", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
571     stats_configured_links, CONF_OPERATOR,
572     "Operator information." },
573   { 'p', "ports", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
574     show_ports, 0,
575     "Listening ports." },
576   { 'q', "quarantines", (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
577     stats_quarantine, 0,
578     "Quarantined channels list." },
579   { 'R', "mappings", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
580     stats_mapping, 0,
581     "Service mappings." },
582 #ifdef DEBUGMODE
583   { 'r', "usage", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
584     send_usage, 0,
585     "System resource usage (Debug only)." },
586 #endif
587   { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
588     motd_report, 0,
589     "Configured Message Of The Day files." },
590   { 't', "locals", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
591     tstats, 0,
592     "Local connection statistics (Total SND/RCV, etc)." },
593   { 'U', "uworld", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
594     stats_configured_links, CONF_UWORLD,
595     "Service server & nick jupes information." },
596   { 'u', "uptime", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
597     stats_uptime, 0,
598     "Current uptime & highest connection count." },
599   { 'v', "vservers", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
600     stats_servers_verbose, 1,
601     "Verbose server information." },
602   { 'V', "vserversmach", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
603     stats_servers_verbose, 0,
604     "Verbose server information." },
605   { 'w', "userload", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
606     calc_load, 0,
607     "Userload statistics." },
608 #ifdef DEBUGMODE
609   { 'x', "memusage", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
610     stats_meminfo, 0,
611     "List usage information (Debug only)." },
612 #endif
613   { 'y', "classes", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
614     report_classes, 0,
615     "Connection classes." },
616   { 'z', "memory", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
617     count_memory, 0,
618     "Memory/Structure allocation information." },
619   { '*', "help", (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
620     stats_help, 0,
621     "Send help for stats." },
622   { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
623 };
624
625 /** Maps from characters to statistics descriptors.
626  * Statistics descriptors with no single-character alias are not included.
627  */
628 static struct StatDesc *statsmap[256];
629 /** Number of statistics descriptors. */
630 static int statscount;
631
632 /** Compare two StatDesc structures by long name (StatDesc::sd_name).
633  * @param[in] a_ Pointer to a StatDesc.
634  * @param[in] b_ Pointer to a StatDesc.
635  * @return Less than, equal to, or greater than zero if \a a_ is
636  * lexicographically less than, equal to, or greater than \a b_.
637  */
638 static int
639 stats_cmp(const void *a_, const void *b_)
640 {
641   const struct StatDesc *a = a_;
642   const struct StatDesc *b = b_;
643   return ircd_strcmp(a->sd_name, b->sd_name);
644 }
645
646 /** Compare a StatDesc's name against a string.
647  * @param[in] key Pointer to a null-terminated string.
648  * @param[in] sd_ Pointer to a StatDesc.
649  * @return Less than,e qual to, or greater than zero if \a key is
650  * lexicographically less than, equal to, or greater than \a
651  * sd_->sd_name.
652  */
653 static int
654 stats_search(const void *key, const void *sd_)
655 {
656   const struct StatDesc *sd = sd_;
657   return ircd_strcmp(key, sd->sd_name);
658 }
659
660 /** Look up a stats handler.  If name_or_char is just one character
661  * long, use that as a character index; otherwise, look it up by name
662  * in #statsinfo.
663  * @param[in] name_or_char Null-terminated string to look up.
664  * @return The statistics descriptor for \a name_or_char (NULL if none).
665  */
666 const struct StatDesc *
667 stats_find(const char *name_or_char)
668 {
669   if (!name_or_char[1])
670     return statsmap[(int)name_or_char[0]];
671   else
672     return bsearch(name_or_char, statsinfo, statscount, sizeof(statsinfo[0]), stats_search);
673 }
674
675 /** Build statsmap from the statsinfo array. */
676 void
677 stats_init(void)
678 {
679   struct StatDesc *sd;
680   int i;
681
682   /* Make darn sure the statsmap array is initialized to all zeros */
683   for (i = 0; i < 256; i++)
684     statsmap[i] = 0;
685
686   /* Count number of stats entries and sort them. */
687   for (statscount = 0, sd = statsinfo; sd->sd_name; sd++, statscount++) {}
688   qsort(statsinfo, statscount, sizeof(statsinfo[0]), stats_cmp);
689
690   /* Build the mapping */
691   for (sd = statsinfo; sd->sd_name; sd++)
692   {
693     if (!sd->sd_c)
694       continue;
695     else if (sd->sd_flags & STAT_FLAG_CASESENS)
696       /* case sensitive character... */
697       statsmap[(int)sd->sd_c] = sd;
698     else
699     {
700       /* case insensitive--make sure to put in two entries */
701       statsmap[(int)ToLower((int)sd->sd_c)] = sd;
702       statsmap[(int)ToUpper((int)sd->sd_c)] = sd;
703     }
704   }
705 }