diff options
author | Kim Alvefur <zash@zash.se> | 2018-06-02 19:57:46 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2018-06-02 19:57:46 +0200 |
commit | a7448e43e0daa2772511d7744727f91db4962346 (patch) | |
tree | 810fd6136e2a41a3b4316066eaccace728e7e185 | |
parent | 5b1a2360296bfec2e8ff7242a6c12cda54521f6a (diff) | |
download | prosody-a7448e43e0daa2772511d7744727f91db4962346.tar.gz prosody-a7448e43e0daa2772511d7744727f91db4962346.zip |
util.dataforms: Add a simple function for identifying form types
This is meant to allow identifying forms without parsing them
completely.
-rw-r--r-- | spec/util_dataforms_spec.lua | 9 | ||||
-rw-r--r-- | util/dataforms.lua | 16 |
2 files changed, 25 insertions, 0 deletions
diff --git a/spec/util_dataforms_spec.lua b/spec/util_dataforms_spec.lua index 4b85444f..56751041 100644 --- a/spec/util_dataforms_spec.lua +++ b/spec/util_dataforms_spec.lua @@ -301,5 +301,14 @@ describe("util.dataforms", function () 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 --git a/util/dataforms.lua b/util/dataforms.lua index 8c5b8ee2..7b832f78 100644 --- a/util/dataforms.lua +++ b/util/dataforms.lua @@ -249,8 +249,24 @@ field_readers["hidden"] = 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; }; |