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