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