Annotate

mod_pubsub_text_interface/mod_pubsub_text_interface.lua @ 3489:33b3f02a9e7d

mod_pubsub_text_interface: Remove unused variable [luacheck]
author Kim Alvefur <zash@zash.se>
date Fri, 15 Mar 2019 04:27:52 +0100
parent 3488:4ddb034a8a03
child 3644:9980ea72ff91
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";
3247
ada7a0c7221c mod_pubsub_text_interface: Generate a stanza id for replies
Kim Alvefur <zash@zash.se>
parents: 3243
diff changeset
3 local id = require "util.id";
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local pubsub = module:depends "pubsub".service;
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 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
8 local help = name..[[
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 Commands:
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 - `help` - this help message
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 - `list` - list available nodes
3488
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
14 - `subscriptions` - list nodes you are subscribed to
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 - `subscribe node` - subscribe to a node
3328
7d39ffd058d5 mod_pubsub_text_interface: Handle lack of `pubsub:get_last_item` in Prosody 0.10
Kim Alvefur <zash@zash.se>
parents: 3293
diff changeset
16 - `unsubscribe node` - unsubscribe from a node]];
7d39ffd058d5 mod_pubsub_text_interface: Handle lack of `pubsub:get_last_item` in Prosody 0.10
Kim Alvefur <zash@zash.se>
parents: 3293
diff changeset
17 if pubsub.get_last_item then -- COMPAT not available in 0.10
7d39ffd058d5 mod_pubsub_text_interface: Handle lack of `pubsub:get_last_item` in Prosody 0.10
Kim Alvefur <zash@zash.se>
parents: 3293
diff changeset
18 help = help .. "\n- `last node` - send the last item (again)"
7d39ffd058d5 mod_pubsub_text_interface: Handle lack of `pubsub:get_last_item` in Prosody 0.10
Kim Alvefur <zash@zash.se>
parents: 3293
diff changeset
19 end
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 module:hook("message/host", function (event)
3489
33b3f02a9e7d mod_pubsub_text_interface: Remove unused variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3488
diff changeset
22 local stanza = event.stanza;
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 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
24 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
25
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local from = stanza.attr.from;
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local reply = st.reply(stanza);
3247
ada7a0c7221c mod_pubsub_text_interface: Generate a stanza id for replies
Kim Alvefur <zash@zash.se>
parents: 3243
diff changeset
29 reply.attr.id = id.medium();
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
3249
8f4a7084c466 mod_pubsub_text_interface: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3248
diff changeset
31 local command, node_arg = body:match("^(%a+)%s+(.*)");
3250
5801b5cf8f54 mod_pubsub_text_interface: Ignore whitespace after all commands
Kim Alvefur <zash@zash.se>
parents: 3249
diff changeset
32 command = (command or body):lower();
3248
ecec46f7d020 mod_pubsub_text_interface: Fix chain that accidentally produced nested <body>
Kim Alvefur <zash@zash.se>
parents: 3247
diff changeset
33
3250
5801b5cf8f54 mod_pubsub_text_interface: Ignore whitespace after all commands
Kim Alvefur <zash@zash.se>
parents: 3249
diff changeset
34 if command == "help" then
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 reply:body(help);
3250
5801b5cf8f54 mod_pubsub_text_interface: Ignore whitespace after all commands
Kim Alvefur <zash@zash.se>
parents: 3249
diff changeset
36 elseif command == "list" then
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 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
38 if ok then
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 local list = {};
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 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
41 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
42 end
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 reply:body(table.concat(list, "\n"));
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 else
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 reply:body(nodes);
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end
3488
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
47 elseif command == "subscriptions" then
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
48 local ok, subs = pubsub:get_subscriptions(nil, from, from);
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
49 if not ok then
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
50 reply:body(subs);
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
51 elseif #subs == 0 then
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
52 reply:body("You are not subscribed to anything from this pubsub service");
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
53 else
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
54 local response = {};
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
55 for i = 1, #subs do
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
56 response[i] = string.format("- `%s`", subs[i].node);
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
57 end
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
58 reply:body(table.concat(response, "\n"));
4ddb034a8a03 mod_pubsub_text_interface: Add command for listing current subscriptions
Kim Alvefur <zash@zash.se>
parents: 3429
diff changeset
59 end
3248
ecec46f7d020 mod_pubsub_text_interface: Fix chain that accidentally produced nested <body>
Kim Alvefur <zash@zash.se>
parents: 3247
diff changeset
60 elseif command == "subscribe" then
3249
8f4a7084c466 mod_pubsub_text_interface: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3248
diff changeset
61 local ok, err = pubsub:add_subscription(node_arg, from, jid.bare(from), { ["pubsub#include_body"] = true });
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 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
63 elseif command == "unsubscribe" then
3249
8f4a7084c466 mod_pubsub_text_interface: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3248
diff changeset
64 local ok, err = pubsub:remove_subscription(node_arg, from, jid.bare(from));
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 reply:body(ok and "OK" or err);
3328
7d39ffd058d5 mod_pubsub_text_interface: Handle lack of `pubsub:get_last_item` in Prosody 0.10
Kim Alvefur <zash@zash.se>
parents: 3293
diff changeset
66 elseif command == "last" and pubsub.get_last_item then
3293
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
67 local ok, item_id, item = pubsub:get_last_item(node_arg, from);
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
68 if not ok then
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
69 reply:body(item_id); -- err message
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
70 elseif not item_id then
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
71 reply:body("node is empty");
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
72 else
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
73 pubsub.config.broadcaster("items", node_arg, {
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
74 [from] = { ["pubsub#include_body"] = true }
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
75 }, item);
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
76 reply:body("OK");
f728c8c42860 mod_pubsub_text_interface: Add a 'last' command for sending the last item
Kim Alvefur <zash@zash.se>
parents: 3250
diff changeset
77 end
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 else
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 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
80 end
3429
1f0c290bd28a mod_pubsub_text_interface: Send replies trough normal stanza routing
Kim Alvefur <zash@zash.se>
parents: 3328
diff changeset
81 module:send(reply);
3243
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 return true;
ca856a892719 mod_pubsub_text_interface: A chat interface to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 end);