# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1527962266 -7200
# Node ID cf2f66b233d180d23a61d49bc5f9da52fec67226
# Parent  64fa8d80c09f851919be1ccfaeb1df05304000cc
util.dataforms: Add a simple function for identifying form types

This is meant to allow identifying forms without parsing them
completely.

diff -r 64fa8d80c09f -r cf2f66b233d1 spec/util_dataforms_spec.lua
--- a/spec/util_dataforms_spec.lua	Sat Jun 02 19:49:15 2018 +0200
+++ b/spec/util_dataforms_spec.lua	Sat Jun 02 19:57:46 2018 +0200
@@ -301,5 +301,14 @@
 		assert.equal("text-single-value", f:get_child_text("value"));
 	end);
 
+	describe("get_type()", function ()
+		it("identifes dataforms", function ()
+			assert.equal(nil, dataforms.get_type(nil));
+			assert.equal(nil, dataforms.get_type(""));
+			assert.equal(nil, dataforms.get_type({}));
+			assert.equal(nil, dataforms.get_type(st.stanza("no-a-form")));
+			assert.equal("xmpp:prosody.im/spec/util.dataforms#1", dataforms.get_type(xform));
+		end);
+	end);
 end);
 
diff -r 64fa8d80c09f -r cf2f66b233d1 util/dataforms.lua
--- a/util/dataforms.lua	Sat Jun 02 19:49:15 2018 +0200
+++ b/util/dataforms.lua	Sat Jun 02 19:57:46 2018 +0200
@@ -249,8 +249,24 @@
 		return field_tag:get_child_text("value");
 	end
 
+
+local function get_form_type(form)
+	if not st.is_stanza(form) then
+		return nil, "not a stanza object";
+	elseif form.attr.xmlns ~= "jabber:x:data" or form.name ~= "x" then
+		return nil, "not a dataform element";
+	end
+	for field in form:childtags("field") do
+		if field.attr.var == "FORM_TYPE" then
+			return field:get_child_text("value");
+		end
+	end
+	return "";
+end
+
 return {
 	new = new;
+	get_type = get_form_type;
 };