changeset 4238:e97c509fdbe3

mod_admin_notify: New module providing an API to notify host admins
author Matthew Wild <mwild1@gmail.com>
date Mon, 09 Nov 2020 16:53:13 +0000
parents a0ab7be0538d
children 313e293f44b3
files mod_admin_notify/README.markdown mod_admin_notify/mod_admin_notify.lua
diffstat 2 files changed, 55 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_admin_notify/README.markdown	Mon Nov 09 16:53:13 2020 +0000
@@ -0,0 +1,28 @@
+---
+labels:
+- 'Stage-Alpha'
+summary: API to notify server admins
+---
+
+# Introduction
+
+This module provides an API for other module developers to send
+notification messages to host admins.
+
+# Configuration
+
+None required.
+
+# Developers
+
+Example:
+
+```
+local notify_admins = module:depends("admin_notify").notify;
+
+notify("This is an important message for you, admins")
+```
+
+# Compatibility
+
+Prosody trunk or later. Incompatible with 0.11 or lower.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_admin_notify/mod_admin_notify.lua	Mon Nov 09 16:53:13 2020 +0000
@@ -0,0 +1,27 @@
+local it = require "util.iterators";
+local jid = require "util.jid";
+local set = require "util.set";
+local st = require "util.stanza";
+
+local roles_store = module:open_store("roles", "map");
+local config_admins = module:get_option_inherited_set("admins") / jid.prep;
+
+local function append_host(username)
+	return username.."@"..module.host;
+end
+
+local function get_admins()
+	local role_admins = roles_store:get_all("prosody:admin") or {};
+	local admins = config_admins + (set.new(it.to_array(it.keys(role_admins))) / append_host);
+	return admins;
+end
+
+function notify(text) --luacheck: ignore 131/notify
+	local base_msg = st.message({ from = module.host })
+		:text_tag("body", text);
+	for admin_jid in get_admins() do
+		local msg = st.clone(base_msg);
+		msg.attr.to = admin_jid;
+		module:send(msg);
+	end
+end