mod_announce: Add shell commands and APIs for sending to all/online/roles
authorMatthew Wild <mwild1@gmail.com>
Wed, 24 Apr 2024 11:50:13 +0100
changeset 13488 e22609460975
parent 13487 7b070909bd15
child 13489 3bdbaba15c00
mod_announce: Add shell commands and APIs for sending to all/online/roles
plugins/mod_announce.lua
--- a/plugins/mod_announce.lua	Wed Apr 24 11:45:37 2024 +0100
+++ b/plugins/mod_announce.lua	Wed Apr 24 11:50:13 2024 +0100
@@ -6,11 +6,15 @@
 -- COPYING file in the source package for more information.
 --
 
-local st, jid = require "prosody.util.stanza", require "prosody.util.jid";
+local usermanager = require "prosody.core.usermanager";
+local id = require "prosody.util.id";
+local jid = require "prosody.util.jid";
+local st = require "prosody.util.stanza";
 
 local hosts = prosody.hosts;
 
 function send_to_online(message, host)
+	host = host or module.host;
 	local sessions;
 	if host then
 		sessions = { [host] = hosts[host] };
@@ -33,6 +37,28 @@
 	return c;
 end
 
+function send_to_all(message, host)
+	host = host or module.host;
+	local c = 0;
+	for username in usermanager.users(host) do
+		message.attr.to = username.."@"..host;
+		module:send(st.clone(message));
+		c = c + 1;
+	end
+	return c;
+end
+
+function send_to_role(message, role, host)
+	host = host or module.host;
+	local c = 0;
+	for _, recipient_jid in ipairs(usermanager.get_jids_with_role(role, host)) do
+		message.attr.to = recipient_jid;
+		module:send(st.clone(message));
+		c = c + 1;
+	end
+	return c;
+end
+
 module:default_permission("prosody:admin", ":send-announcement");
 
 -- Old <message>-based jabberd-style announcement sending
@@ -82,8 +108,10 @@
 		local fields = announce_layout:data(data.form);
 
 		module:log("info", "Sending server announcement to all online users");
-		local message = st.message({type = "headline"}, fields.announcement):up()
-			:tag("subject"):text(fields.subject or "Announcement");
+		local message = st.message({type = "headline"}, fields.announcement):up();
+		if fields.subject and fields.subject ~= "" then
+			message:text_tag("subject", fields.subject);
+		end
 
 		local count = send_to_online(message, data.to);
 
@@ -99,3 +127,57 @@
 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin");
 module:provides("adhoc", announce_desc);
 
+module:add_item("shell-command", {
+	section = "announce";
+	section_desc = "Broadcast announcements to users";
+	name = "all";
+	desc = "Send announcement to all users on the host";
+	args = {
+		{ name = "host", type = "string" };
+		{ name = "text", type = "string" };
+	};
+	host_selector = "host";
+	handler = function(self, host, text)
+		local msg = st.message({ from = host, id = id.short() })
+			:text_tag("body", text);
+		local count = send_to_all(msg, host);
+		return true, ("Announcement sent to %d users"):format(count);
+	end;
+});
+
+module:add_item("shell-command", {
+	section = "announce";
+	section_desc = "Broadcast announcements to users";
+	name = "online";
+	desc = "Send announcement to all online users on the host";
+	args = {
+		{ name = "host", type = "string" };
+		{ name = "text", type = "string" };
+	};
+	host_selector = "host";
+	handler = function(self, host, text)
+		local msg = st.message({ from = host, id = id.short(), type = "headline" })
+			:text_tag("body", text);
+		local count = send_to_online(msg, host);
+		return true, ("Announcement sent to %d users"):format(count);
+	end;
+});
+
+module:add_item("shell-command", {
+	section = "announce";
+	section_desc = "Broadcast announcements to users";
+	name = "role";
+	desc = "Send announcement to users with a specific role on the host";
+	args = {
+		{ name = "host", type = "string" };
+		{ name = "role", type = "string" };
+		{ name = "text", type = "string" };
+	};
+	host_selector = "host";
+	handler = function(self, host, role, text)
+		local msg = st.message({ from = host, id = id.short() })
+			:text_tag("body", text);
+		local count = send_to_role(msg, role, host);
+		return true, ("Announcement sent to %d users"):format(count);
+	end;
+});