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