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