# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1574711976 -3600
# Node ID 5c2d1b13537c5d6e696a64a7d8255059c3744d37
# Parent  f53c03ab4357f81973f1ef7ae8ef3d01f63028d5
util.stanza: Support the 'by' attribute on errors

This is to be used when the entity generating the error is not the same
as the one the stanza was directed to, e.g. an intermediate server.

diff -r f53c03ab4357 -r 5c2d1b13537c spec/util_stanza_spec.lua
--- a/spec/util_stanza_spec.lua	Mon Nov 25 20:52:03 2019 +0100
+++ b/spec/util_stanza_spec.lua	Mon Nov 25 20:59:36 2019 +0100
@@ -191,13 +191,14 @@
 			local s = st.stanza("s", { to = "touser", from = "fromuser", id = "123" })
 				:tag("child1");
 			-- Make reply stanza
-			local r = st.error_reply(s, "cancel", "service-unavailable");
+			local r = st.error_reply(s, "cancel", "service-unavailable", nil, "host");
 			assert.are.equal(r.name, s.name);
 			assert.are.equal(r.id, s.id);
 			assert.are.equal(r.attr.to, s.attr.from);
 			assert.are.equal(r.attr.from, s.attr.to);
 			assert.are.equal(#r.tags, 1);
 			assert.are.equal(r.tags[1].tags[1].name, "service-unavailable");
+			assert.are.equal(r.tags[1].attr.by, "host");
 		end);
 
 		it("should work for <iq get>", function()
diff -r f53c03ab4357 -r 5c2d1b13537c util/stanza.lua
--- a/util/stanza.lua	Mon Nov 25 20:52:03 2019 +0100
+++ b/util/stanza.lua	Mon Nov 25 20:59:36 2019 +0100
@@ -447,7 +447,7 @@
 end
 
 local xmpp_stanzas_attr = { xmlns = xmlns_stanzas };
-local function error_reply(orig, error_type, condition, error_message)
+local function error_reply(orig, error_type, condition, error_message, error_by)
 	if not is_stanza(orig) then
 		error("bad argument to error_reply: expected stanza, got "..type(orig));
 	elseif orig.attr.type == "error" then
@@ -455,7 +455,10 @@
 	end
 	local t = reply(orig);
 	t.attr.type = "error";
-	t:tag("error", {type = error_type}) --COMPAT: Some day xmlns:stanzas goes here
+	if t.attr.from == error_by then
+		error_by = nil;
+	end
+	t:tag("error", {type = error_type, by = error_by}) --COMPAT: Some day xmlns:stanzas goes here
 	:tag(condition, xmpp_stanzas_attr):up();
 	if error_message then t:text_tag("text", error_message, xmpp_stanzas_attr); end
 	return t; -- stanza ready for adding app-specific errors