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