Annotate

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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local st = require "util.stanza";
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local jid = require "util.jid";
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local pubsub = module:depends "pubsub".service;
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local name = module:get_option_string("name", "PubSub Service on "..module.host);
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local help = name..[[
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 Commands:
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 - `help` - this help message
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 - `list` - list available nodes
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 - `subscribe node` - subscribe to a node
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 - `unsubscribe node` - unsubscribe from a node
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 ]];
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 module:hook("message/host", function (event)
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local origin, stanza = event.origin, event.stanza;
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local body = stanza:get_child_text("body");
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 if not body then return end -- bail out
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 body = body:lower();
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local from = stanza.attr.from;
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local reply = st.reply(stanza);
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 if body == "help" then
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 reply:body(help);
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 elseif body == "list" then
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local ok, nodes = pubsub:get_nodes(from);
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 if ok then
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 local list = {};
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 for node, node_obj in pairs(nodes) do
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 table.insert(list, ("- `%s` %s"):format(node, node_obj.config.title or ""));
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 end
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 reply:body(table.concat(list, "\n"));
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 else
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 reply:body(nodes);
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 end
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local command, node = body:match("^(%a+)%s+(.*)");
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 if command == "subscribe" then
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 local ok, err = pubsub:add_subscription(node, from, jid.bare(from), { ["pubsub#include_body"] = true });
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 reply:body(ok and "OK" or err);
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 elseif command == "unsubscribe" then
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 local ok, err = pubsub:remove_subscription(node, from, jid.bare(from));
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 reply:body(ok and "OK" or err);
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 else
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 reply:body("Unknown command. `help` to list commands.");
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 end
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 origin.send(reply);
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 return true;
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end);