From fbe3d6124bc2d69859be92d0c624b008577fe041 Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Sat, 7 Aug 2021 17:30:04 +0200 Subject: [PATCH] Add unrealircd.org/json-log CAP, which sends JSON logs to IRCOps. This basically enhances the regular snomask/ircop notices with JSON logs, the same logs that are logged to disk (with type 'json'). This allows bots/machines to much more easily parse server notices such as connect notices or.. anything. Note that JSON logs are quite large, so make sure the ircop has a BIG class::sendq! Also, everyone can set the cap but it is only effective for IRCOps. --- src/log.c | 15 ++++++++++++++- src/modules/json-log-tag.c | 15 ++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/log.c b/src/log.c index 519216c54..5a9adb2b8 100644 --- a/src/log.c +++ b/src/log.c @@ -1106,6 +1106,7 @@ void do_unreal_log_ircops(LogLevel loglevel, char *subsystem, char *event_id, ch char *client_snomasks; char *p; char found; + MessageTag *mtags = NULL; /* If not fully booted then we don't have a logging to snomask mapping so can't do much.. */ if (!loop.ircd_booted) @@ -1125,6 +1126,14 @@ void do_unreal_log_ircops(LogLevel loglevel, char *subsystem, char *event_id, ch if (snomask_destinations && !strcmp(snomask_destinations, "*")) snomask_destinations = NULL; + /* Prepare message tag for those who have CAP unrealircd.org/json-log */ + if (json_serialized) + { + mtags = safe_alloc(sizeof(MessageTag)); + safe_strdup(mtags->name, "unrealircd.org/json-log"); + safe_strdup(mtags->value, json_serialized); + } + /* To specific snomasks... */ list_for_each_entry(client, &oper_list, special_node) { @@ -1148,11 +1157,15 @@ void do_unreal_log_ircops(LogLevel loglevel, char *subsystem, char *event_id, ch log_level_color(loglevel), log_level_valtostring(loglevel), COLOR_NONE, COLOR_DARKGREY, subsystem, event_id, COLOR_NONE, msg);*/ - sendnotice(client, "%s%s.%s%s %s[%s]%s %s", + sendto_one(client, mtags, ":%s NOTICE %s :%s%s.%s%s %s[%s]%s %s", + me.name, client->name, COLOR_DARKGREY, subsystem, event_id, COLOR_NONE, log_level_color(loglevel), log_level_valtostring(loglevel), COLOR_NONE, msg); } + + if (mtags) + free_message_tags(mtags); } void do_unreal_log_remote(LogLevel loglevel, char *subsystem, char *event_id, char *msg, char *json_serialized) diff --git a/src/modules/json-log-tag.c b/src/modules/json-log-tag.c index 1064cdd7b..5412b9e15 100644 --- a/src/modules/json-log-tag.c +++ b/src/modules/json-log-tag.c @@ -31,21 +31,30 @@ ModuleHeader MOD_HEADER "unrealircd-5", }; +/* Variables */ +long CAP_JSON_LOG = 0L; + /* Forward declarations */ int json_log_mtag_is_ok(Client *client, char *name, char *value); int json_log_mtag_can_send(Client *target); MOD_INIT() -{ +{ + ClientCapabilityInfo cap; + ClientCapability *c; MessageTagHandlerInfo mtag; MARK_AS_OFFICIAL_MODULE(modinfo); + memset(&cap, 0, sizeof(cap)); + cap.name = "unrealircd.org/json-log"; + c = ClientCapabilityAdd(modinfo->handle, &cap, &CAP_JSON_LOG); + memset(&mtag, 0, sizeof(mtag)); mtag.name = "unrealircd.org/json-log"; mtag.is_ok = json_log_mtag_is_ok; mtag.can_send = json_log_mtag_can_send; - mtag.flags = MTAG_HANDLER_FLAGS_NO_CAP_NEEDED; + mtag.clicap_handler = c; MessageTagHandlerAdd(modinfo->handle, &mtag); return MOD_SUCCESS; @@ -75,7 +84,7 @@ int json_log_mtag_is_ok(Client *client, char *name, char *value) /** Outgoing filter for this message tag */ int json_log_mtag_can_send(Client *target) { - if (IsServer(target) || IsOper(target)) + if (IsServer(target) || (target->local && IsOper(target) && HasCapabilityFast(target, CAP_JSON_LOG))) return 1; return 0; }