mod_pubsub_text_interface/mod_pubsub_text_interface.lua
changeset 5137 d7652471ae3e
parent 4649 25ce28711fac
child 5138 e0d0ef564095
equal deleted inserted replaced
5136:36b5677b9648 5137:d7652471ae3e
    16 - `subscribe node` - subscribe to a node
    16 - `subscribe node` - subscribe to a node
    17 - `unsubscribe node` - unsubscribe from a node]];
    17 - `unsubscribe node` - unsubscribe from a node]];
    18 if pubsub.get_last_item then -- COMPAT not available in 0.10
    18 if pubsub.get_last_item then -- COMPAT not available in 0.10
    19 	help = help ..  "\n- `last node` - send the last item (again)"
    19 	help = help ..  "\n- `last node` - send the last item (again)"
    20 end
    20 end
       
    21 -- FIXME better word for "node"
       
    22 
       
    23 local friendly_pubsub_errors = {
       
    24 	["forbidden"] = "You are not allowed to do that";
       
    25 	["item-not-found"] = "That node does not exist";
       
    26 	["internal-server-error"] = "Something went wrong (see server logs)";
       
    27 	["not-subscribed"] = "You were not subscribed";
       
    28 };
    21 
    29 
    22 module:hook("message/host", function (event)
    30 module:hook("message/host", function (event)
    23 	local stanza = event.stanza;
    31 	local stanza = event.stanza;
    24 	local body = stanza:get_child_text("body");
    32 	local body = stanza:get_child_text("body");
    25 	if not body then return end -- bail out
    33 	if not body then return end -- bail out
    43 			for node, node_obj in pairs(nodes) do
    51 			for node, node_obj in pairs(nodes) do
    44 				table.insert(list, ("- `%s` %s"):format(node, node_obj.config.title or ""));
    52 				table.insert(list, ("- `%s` %s"):format(node, node_obj.config.title or ""));
    45 			end
    53 			end
    46 			reply:body(table.concat(list, "\n"));
    54 			reply:body(table.concat(list, "\n"));
    47 		else
    55 		else
    48 			reply:body(nodes);
    56 			reply:body(friendly_pubsub_errors[nodes] or nodes);
    49 		end
    57 		end
    50 	elseif command == "subscriptions" then
    58 	elseif command == "subscriptions" then
    51 		local ok, subs = pubsub:get_subscriptions(nil, from, from);
    59 		local ok, subs = pubsub:get_subscriptions(nil, from, from);
    52 		if not ok then
    60 		if not ok then
    53 			reply:body(subs);
    61 			reply:body(friendly_pubsub_errors[subs] or subs);
    54 		elseif #subs == 0 then
    62 		elseif #subs == 0 then
    55 			reply:body("You are not subscribed to anything from this pubsub service");
    63 			reply:body("You are not subscribed to anything from this pubsub service");
    56 		else
    64 		else
    57 			local response = {};
    65 			local response = {};
    58 			for i = 1, #subs do
    66 			for i = 1, #subs do
    62 			end
    70 			end
    63 			reply:body(table.concat(response, "\n"));
    71 			reply:body(table.concat(response, "\n"));
    64 		end
    72 		end
    65 	elseif command == "subscribe" then
    73 	elseif command == "subscribe" then
    66 		local ok, err = pubsub:add_subscription(node_arg, from, jid.bare(from), { ["pubsub#include_body"] = true });
    74 		local ok, err = pubsub:add_subscription(node_arg, from, jid.bare(from), { ["pubsub#include_body"] = true });
    67 		reply:body(ok and "OK" or err);
    75 		reply:body(ok and "OK" or friendly_pubsub_errors[err] or err);
    68 	elseif command == "unsubscribe" then
    76 	elseif command == "unsubscribe" then
    69 		local ok, err = pubsub:remove_subscription(node_arg, from, jid.bare(from));
    77 		local ok, err = pubsub:remove_subscription(node_arg, from, jid.bare(from));
    70 		reply:body(ok and "OK" or err);
    78 		reply:body(ok and "OK" or friendly_pubsub_errors[err] or err);
    71 	elseif command == "last" and pubsub.get_last_item then
    79 	elseif command == "last" and pubsub.get_last_item then
    72 		local ok, item_id, item = pubsub:get_last_item(node_arg, from);
    80 		local ok, item_id, item = pubsub:get_last_item(node_arg, from);
    73 		if not ok then
    81 		if not ok then
    74 			reply:body(item_id); -- err message
    82 			reply:body(friendly_pubsub_errors[item_id] or item_id);
    75 		elseif not item_id then
    83 		elseif not item_id then
    76 			reply:body("node is empty");
    84 			reply:body("That node does not contain any items");
    77 		else
    85 		else
    78 			pubsub.config.broadcaster("items", node_arg, {
    86 			pubsub.config.broadcaster("items", node_arg, {
    79 				[from] = { ["pubsub#include_body"] = true }
    87 				[from] = { ["pubsub#include_body"] = true }
    80 			}, item, nil, pubsub.nodes[node_arg]);
    88 			}, item, nil, pubsub.nodes[node_arg]);
    81 			reply:body("OK");
    89 			reply:body("OK");