unrealircd

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

log.c (3193B)

      1 /* log.* RPC calls
      2  * (C) Copyright 2023-.. 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/log",
     11 	"1.0.0",
     12 	"log.* RPC calls",
     13 	"UnrealIRCd Team",
     14 	"unrealircd-6",
     15 };
     16 
     17 /* Forward declarations */
     18 void rpc_log_hook_subscribe(Client *client, json_t *request, json_t *params);
     19 void rpc_log_hook_unsubscribe(Client *client, json_t *request, json_t *params);
     20 int rpc_log_hook(LogLevel loglevel, const char *subsystem, const char *event_id, MultiLine *msg, json_t *json, const char *json_serialized, const char *timebuf);
     21 
     22 MOD_INIT()
     23 {
     24 	RPCHandlerInfo r;
     25 
     26 	MARK_AS_OFFICIAL_MODULE(modinfo);
     27 
     28 	memset(&r, 0, sizeof(r));
     29 	r.method = "log.subscribe";
     30 	r.loglevel = ULOG_DEBUG;
     31 	r.call = rpc_log_hook_subscribe;
     32 	if (!RPCHandlerAdd(modinfo->handle, &r))
     33 	{
     34 		config_error("[rpc/log] Could not register RPC handler");
     35 		return MOD_FAILED;
     36 	}
     37 
     38 	memset(&r, 0, sizeof(r));
     39 	r.method = "log.unsubscribe";
     40 	r.loglevel = ULOG_DEBUG;
     41 	r.call = rpc_log_hook_unsubscribe;
     42 	if (!RPCHandlerAdd(modinfo->handle, &r))
     43 	{
     44 		config_error("[rpc/log] Could not register RPC handler");
     45 		return MOD_FAILED;
     46 	}
     47 
     48 	HookAdd(modinfo->handle, HOOKTYPE_LOG, 0, rpc_log_hook);
     49 
     50 	return MOD_SUCCESS;
     51 }
     52 
     53 MOD_LOAD()
     54 {
     55 	return MOD_SUCCESS;
     56 }
     57 
     58 MOD_UNLOAD()
     59 {
     60 	return MOD_SUCCESS;
     61 }
     62 
     63 void rpc_log_hook_subscribe(Client *client, json_t *request, json_t *params)
     64 {
     65 	json_t *result;
     66 	json_t *sources;
     67 	size_t index;
     68 	json_t *value;
     69 	const char *str;
     70 	LogSource *s;
     71 
     72 	sources = json_object_get(params, "sources");
     73 	if (!sources || !json_is_array(sources))
     74 	{
     75 		rpc_error_fmt(client, request, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: '%s'", "sources");
     76 		return;
     77 	}
     78 
     79 	/* Erase the old subscriptions first */
     80 	free_log_sources(client->rpc->log_sources);
     81 	client->rpc->log_sources = NULL;
     82 
     83 	/* Add subscriptions... */
     84 	json_array_foreach(sources, index, value)
     85 	{
     86 		str = json_get_value(value);
     87 		if (!str)
     88 			continue;
     89 
     90 		s = add_log_source(str);
     91 		AddListItem(s, client->rpc->log_sources);
     92 	}
     93 
     94 	result = json_boolean(1);
     95 
     96 	rpc_response(client, request, result);
     97 	json_decref(result);
     98 }
     99 
    100 /** log.unsubscribe: unsubscribe from all log messages */
    101 void rpc_log_hook_unsubscribe(Client *client, json_t *request, json_t *params)
    102 {
    103 	json_t *result;
    104 
    105 	free_log_sources(client->rpc->log_sources);
    106 	client->rpc->log_sources = NULL;
    107 	result = json_boolean(1);
    108 	rpc_response(client, request, result);
    109 	json_decref(result);
    110 }
    111 
    112 int rpc_log_hook(LogLevel loglevel, const char *subsystem, const char *event_id, MultiLine *msg, json_t *json, const char *json_serialized, const char *timebuf)
    113 {
    114 	Client *client;
    115 	json_t *request = NULL;
    116 
    117 	if (!strcmp(subsystem, "rawtraffic"))
    118 		return 0;
    119 
    120 	list_for_each_entry(client, &unknown_list, lclient_node)
    121 	{
    122 		if (IsRPC(client) && client->rpc->log_sources &&
    123 		    log_sources_match(client->rpc->log_sources, loglevel, subsystem, event_id, 0))
    124 		{
    125 			if (request == NULL)
    126 			{
    127 				/* Lazy initalization */
    128 				request = json_object();
    129 				json_object_set_new(request, "method", json_string_unreal("log.event"));
    130 			}
    131 			rpc_response(client, request, json);
    132 		}
    133 	}
    134 
    135 	if (request)
    136 		json_decref(request);
    137 
    138 	return 0;
    139 }