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