Changeset

9247:26854d7a4947

mod_pubsub: Fix dataforms error handling The :data method returns the table holding parsed values always. The second return value is a table in case some fields had problems.
author Kim Alvefur <zash@zash.se>
date Sat, 01 Sep 2018 21:18:30 +0200
parents 9246:397e8e5a2f1f
children 9248:1d6a2cc389eb
files plugins/mod_pubsub/pubsub.lib.lua
diffstat 1 files changed, 27 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_pubsub/pubsub.lib.lua	Mon Sep 03 19:44:28 2018 +0200
+++ b/plugins/mod_pubsub/pubsub.lib.lua	Sat Sep 01 21:18:30 2018 +0200
@@ -42,6 +42,14 @@
 end
 _M.pubsub_error_reply = pubsub_error_reply;
 
+local function dataform_error_message(err) -- ({ string : string }) -> string?
+	local out = {};
+	for field, errmsg in pairs(err) do
+		table.insert(out, ("%s: %s"):format(field, errmsg))
+	end
+	return table.concat(out, "; ");
+end
+
 -- Note: If any config options are added that are of complex types,
 -- (not simply strings/numbers) then the publish-options code will
 -- need to be revisited
@@ -400,8 +408,8 @@
 			return true;
 		end
 		local form_data, err = node_config_form:data(config_form);
-		if not form_data then
-			origin.send(st.error_reply(stanza, "modify", "bad-request", err));
+		if err then
+			origin.send(st.error_reply(stanza, "modify", "bad-request", dataform_error_message(err)));
 			return true;
 		end
 		config = form_data;
@@ -457,7 +465,13 @@
 	end
 	local options_tag, options = stanza.tags[1]:get_child("options"), nil;
 	if options_tag then
-		options = subscribe_options_form:data(options_tag.tags[1]);
+		-- FIXME form parsing errors ignored here, why?
+		local err
+		options, err = subscribe_options_form:data(options_tag.tags[1]);
+		if err then
+			origin.send(st.error_reply(stanza, "modify", "bad-request", dataform_error_message(err)));
+			return true
+		end
 	end
 	local ok, ret = service:add_subscription(node, stanza.attr.from, jid, options);
 	local reply;
@@ -533,8 +547,8 @@
 	end
 	local old_subopts = ret;
 	local new_subopts, err = subscribe_options_form:data(options.tags[1], old_subopts);
-	if not new_subopts then
-		origin.send(pubsub_error_reply(stanza, ret));
+	if err then
+		origin.send(st.error_reply(stanza, "modify", "bad-request", dataform_error_message(err)));
 		return true;
 	end
 	local ok, err = service:add_subscription(node, stanza.attr.from, jid, new_subopts);
@@ -557,7 +571,12 @@
 	if publish_options then
 		-- Ensure that the node configuration matches the values in publish-options
 		local publish_options_form = publish_options:get_child("x", "jabber:x:data");
-		required_config = node_config_form:data(publish_options_form);
+		local err;
+		required_config, err = node_config_form:data(publish_options_form);
+		if err then
+			origin.send(st.error_reply(stanza, "modify", "bad-request", dataform_error_message(err)));
+			return true
+		end
 	end
 	local item = publish:get_child("item");
 	local id = (item and item.attr.id);
@@ -667,8 +686,8 @@
 		return true;
 	end
 	local new_config, err = node_config_form:data(config_form, old_config);
-	if not new_config then
-		origin.send(st.error_reply(stanza, "modify", "bad-request", err));
+	if err then
+		origin.send(st.error_reply(stanza, "modify", "bad-request", dataform_error_message(err)));
 		return true;
 	end
 	local ok, err = service:set_node_config(node, stanza.attr.from, new_config);