Diff

mod_pubsub_text_interface/mod_pubsub_text_interface.lua @ 3243:ca856a892719

mod_pubsub_text_interface: A chat interface to PubSub This module lets you manage subscriptions to pubsub nodes via simple chat messages. Subscriptions are always added based on bare JID. The include_body flag is enabled so that a plain text body version of events can be included, where supported.
author Kim Alvefur <zash@zash.se>
date Mon, 20 Aug 2018 17:09:20 +0200
child 3247:ada7a0c7221c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_pubsub_text_interface/mod_pubsub_text_interface.lua	Mon Aug 20 17:09:20 2018 +0200
@@ -0,0 +1,53 @@
+local st = require "util.stanza";
+local jid = require "util.jid";
+
+local pubsub = module:depends "pubsub".service;
+
+local name = module:get_option_string("name", "PubSub Service on "..module.host);
+local help = name..[[
+
+Commands:
+
+- `help` - this help message
+- `list` - list available nodes
+- `subscribe node` - subscribe to a node
+- `unsubscribe node` - unsubscribe from a node
+]];
+
+module:hook("message/host", function (event)
+	local origin, stanza = event.origin, event.stanza;
+	local body = stanza:get_child_text("body");
+	if not body then return end -- bail out
+	body = body:lower();
+
+	local from = stanza.attr.from;
+
+	local reply = st.reply(stanza);
+
+	if body == "help" then
+		reply:body(help);
+	elseif body == "list" then
+		local ok, nodes = pubsub:get_nodes(from);
+		if ok then
+			local list = {};
+			for node, node_obj in pairs(nodes) do
+				table.insert(list, ("- `%s` %s"):format(node, node_obj.config.title or ""));
+			end
+			reply:body(table.concat(list, "\n"));
+		else
+			reply:body(nodes);
+		end
+	end
+	local command, node = body:match("^(%a+)%s+(.*)");
+	if command == "subscribe" then
+		local ok, err = pubsub:add_subscription(node, from, jid.bare(from), { ["pubsub#include_body"] = true });
+		reply:body(ok and "OK" or err);
+	elseif command == "unsubscribe" then
+		local ok, err = pubsub:remove_subscription(node, from, jid.bare(from));
+		reply:body(ok and "OK" or err);
+	else
+		reply:body("Unknown command. `help` to list commands.");
+	end
+	origin.send(reply);
+	return true;
+end);