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