unrealircd

- supernets unrealircd source & configuration
git clone git://git.acid.vegas/unrealircd.git
Log | Files | Refs | Archive | README | LICENSE

stats.c (4853B)

      1 /* stats.* RPC calls
      2  * (C) Copyright 2022-.. Bram Matthys (Syzop) and the UnrealIRCd team
      3  * License: GPLv2 or later
      4  */
      5 
      6 #include "unrealircd.h"
      7 
      8 ModuleHeader MOD_HEADER
      9 = {
     10 	"rpc/stats",
     11 	"1.0.2",
     12 	"stats.* RPC calls",
     13 	"UnrealIRCd Team",
     14 	"unrealircd-6",
     15 };
     16 
     17 /* Forward declarations */
     18 void rpc_stats_get(Client *client, json_t *request, json_t *params);
     19 
     20 MOD_INIT()
     21 {
     22 	RPCHandlerInfo r;
     23 
     24 	MARK_AS_OFFICIAL_MODULE(modinfo);
     25 
     26 	memset(&r, 0, sizeof(r));
     27 	r.method = "stats.get";
     28 	r.loglevel = ULOG_DEBUG;
     29 	r.call = rpc_stats_get;
     30 	if (!RPCHandlerAdd(modinfo->handle, &r))
     31 	{
     32 		config_error("[rpc/stats] Could not register RPC handler");
     33 		return MOD_FAILED;
     34 	}
     35 
     36 	return MOD_SUCCESS;
     37 }
     38 
     39 MOD_LOAD()
     40 {
     41 	return MOD_SUCCESS;
     42 }
     43 
     44 MOD_UNLOAD()
     45 {
     46 	return MOD_SUCCESS;
     47 }
     48 
     49 void json_expand_countries(json_t *main, const char *name, NameValuePrioList *geo)
     50 {
     51 	json_t *list = json_array();
     52 	json_t *item;
     53 
     54 	json_object_set_new(main, name, list);
     55 
     56 	for (; geo; geo = geo->next)
     57 	{
     58 		item = json_object();
     59 		json_object_set_new(item, "country", json_string_unreal(geo->name));
     60 		json_object_set_new(item, "count", json_integer(0 - geo->priority));
     61 		json_array_append_new(list, item);
     62 	}
     63 }
     64 
     65 void rpc_stats_user(json_t *main, int detail)
     66 {
     67 	Client *client;
     68 	int total = 0, ulined = 0, oper = 0;
     69 	json_t *child;
     70 	GeoIPResult *geo;
     71 	NameValuePrioList *countries = NULL;
     72 
     73 	child = json_object();
     74 	json_object_set_new(main, "user", child);
     75 
     76 	list_for_each_entry(client, &client_list, client_node)
     77 	{
     78 		if (IsUser(client))
     79 		{
     80 			total++;
     81 			if (IsULine(client))
     82 				ulined++;
     83 			else if (IsOper(client))
     84 				oper++;
     85 			if (detail >= 1)
     86 			{
     87 				geo = geoip_client(client);
     88 				if (geo && geo->country_code)
     89 				{
     90 					NameValuePrioList *e = find_nvplist(countries, geo->country_code);
     91 					if (e)
     92 					{
     93 						DelListItem(e, countries);
     94 						e->priority--;
     95 						AddListItemPrio(e, countries, e->priority);
     96 					} else {
     97 						add_nvplist(&countries, -1, geo->country_code, NULL);
     98 					}
     99 				}
    100 			}
    101 		}
    102 	}
    103 
    104 	json_object_set_new(child, "total", json_integer(total));
    105 	json_object_set_new(child, "ulined", json_integer(ulined));
    106 	json_object_set_new(child, "oper", json_integer(oper));
    107 	json_object_set_new(child, "record", json_integer(irccounts.global_max));
    108 	if (detail >= 1)
    109 		json_expand_countries(child, "countries", countries);
    110 }
    111 
    112 void rpc_stats_channel(json_t *main)
    113 {
    114 	json_t *child = json_object();
    115 	json_object_set_new(main, "channel", child);
    116 	json_object_set_new(child, "total", json_integer(irccounts.channels));
    117 }
    118 
    119 void rpc_stats_server(json_t *main)
    120 {
    121 	Client *client;
    122 	int total = 0, ulined = 0, oper = 0;
    123 	json_t *child = json_object();
    124 	json_object_set_new(main, "server", child);
    125 
    126 	total++; /* ourselves */
    127 	list_for_each_entry(client, &global_server_list, client_node)
    128 	{
    129 		if (IsServer(client))
    130 		{
    131 			total++;
    132 			if (IsULine(client))
    133 				ulined++;
    134 		}
    135 	}
    136 
    137 	json_object_set_new(child, "total", json_integer(total));
    138 	json_object_set_new(child, "ulined", json_integer(ulined));
    139 }
    140 
    141 void rpc_stats_server_ban(json_t *main)
    142 {
    143 	Client *client;
    144 	int index, index2;
    145 	TKL *tkl;
    146 	int total = 0;
    147 	int server_ban = 0;
    148 	int server_ban_exception = 0;
    149 	int spamfilter = 0;
    150 	int name_ban = 0;
    151 	json_t *child = json_object();
    152 	json_object_set_new(main, "server_ban", child);
    153 
    154 	/* First, hashed entries.. */
    155 	for (index = 0; index < TKLIPHASHLEN1; index++)
    156 	{
    157 		for (index2 = 0; index2 < TKLIPHASHLEN2; index2++)
    158 		{
    159 			for (tkl = tklines_ip_hash[index][index2]; tkl; tkl = tkl->next)
    160 			{
    161 				total++;
    162 				if (TKLIsServerBan(tkl))
    163 					server_ban++;
    164 				else if (TKLIsBanException(tkl))
    165 					server_ban_exception++;
    166 				else if (TKLIsNameBan(tkl))
    167 					name_ban++;
    168 				else if (TKLIsSpamfilter(tkl))
    169 					spamfilter++;
    170 			}
    171 		}
    172 	}
    173 
    174 	/* Now normal entries.. */
    175 	for (index = 0; index < TKLISTLEN; index++)
    176 	{
    177 		for (tkl = tklines[index]; tkl; tkl = tkl->next)
    178 		{
    179 			total++;
    180 			if (TKLIsServerBan(tkl))
    181 				server_ban++;
    182 			else if (TKLIsBanException(tkl))
    183 				server_ban_exception++;
    184 			else if (TKLIsNameBan(tkl))
    185 				name_ban++;
    186 			else if (TKLIsSpamfilter(tkl))
    187 				spamfilter++;
    188 		}
    189 	}
    190 
    191 	json_object_set_new(child, "total", json_integer(total));
    192 	json_object_set_new(child, "server_ban", json_integer(server_ban));
    193 	json_object_set_new(child, "spamfilter", json_integer(spamfilter));
    194 	json_object_set_new(child, "name_ban", json_integer(name_ban));
    195 	json_object_set_new(child, "server_ban_exception", json_integer(server_ban_exception));
    196 }
    197 
    198 void rpc_stats_get(Client *client, json_t *request, json_t *params)
    199 {
    200 	json_t *result, *item;
    201 	const char *statsname;
    202 	Channel *stats;
    203 	int details;
    204 
    205 	OPTIONAL_PARAM_INTEGER("object_detail_level", details, 1);
    206 
    207 	result = json_object();
    208 	rpc_stats_server(result);
    209 	rpc_stats_user(result, details);
    210 	rpc_stats_channel(result);
    211 	rpc_stats_server_ban(result);
    212 	rpc_response(client, request, result);
    213 	json_decref(result);
    214 }