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