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