From 32cbf316230aa72e32c5d32a3ea0a5fb7ccadb0b Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Fri, 19 Feb 2016 11:03:46 +0100 Subject: [PATCH] MT#18055 mod_sipwise_vjud: use dataforms for searching e164 values * support using field as e164 for backwards compatibility Change-Id: If12445facee1e77f57283ddf7401b06b87a9782b --- plugins/mod_sipwise_vjud.lua | 64 +++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/plugins/mod_sipwise_vjud.lua b/plugins/mod_sipwise_vjud.lua index f264db6..0351fac 100644 --- a/plugins/mod_sipwise_vjud.lua +++ b/plugins/mod_sipwise_vjud.lua @@ -12,10 +12,32 @@ local st = require "util.stanza"; local template = require "util.template"; local rex = require "rex_pcre"; local prosodyctl = require "util.prosodyctl" +local dataforms_new = require "util.dataforms".new; + +local form_layout = dataforms_new{ + title= 'User Directory Search'; + instructions = 'Please provide the following information to search for subscribers'; + { + type = 'text-single', + label = 'e164 Phone number', + name = 'e164', + required = false, + }, +}; local get_reply = template[[ - Please enter a phone number + Use the enclosed form to search + + User Directory Search + Please provide the following information to search for subscribers + + jabber:iq:search + + + ]].apply({}); @@ -148,6 +170,7 @@ if module:get_host_type() ~= "component" then end module:add_feature("jabber:iq:search"); +module:add_feature("jabber:x:data"); -- dataforms module:hook("iq/host/jabber:iq:search:query", function(event) local origin, stanza = event.origin, event.stanza; module:log("debug", "stanza[%s]", tostring(stanza)); @@ -156,20 +179,39 @@ module:hook("iq/host/jabber:iq:search:query", function(event) else local user, host = ut_jid.split(stanza.attr.from); - local number = stanza.tags[1]:get_child_text("nick"); - - -- Reconnect to DB if necessary - if not engine.conn:ping() then - engine.conn = nil; - engine:connect(); + local query = stanza:get_child("query",'jabber:iq:search'); + local form_stanza = query:get_child("x",'jabber:x:data'); + local reply; + local search_number = stanza.tags[1]:get_child_text("nick"); + + if form_stanza then + local form_data, form_errors = form_layout:data(form_stanza); + if form_errors and form_errors['e164'] then + reply = st.error_reply(stanza, "modify", + "bad-request", "e164: "..form_errors['e164']); + return origin.send(reply); + else + search_number = form_data['e164'] or search_number; + end end - number = normalize_number(user, host, number); + if search_number then + -- Reconnect to DB if necessary + if not engine.conn:ping() then + engine.conn = nil; + engine:connect(); + end + + local number = normalize_number(user, host, search_number); + reply = st.reply(stanza):query("jabber:iq:search"); - local reply = st.reply(stanza):query("jabber:iq:search"); - for _, jid in ipairs(search_by_number(number)) do - reply:tag("item", { jid = jid }):up(); + for _, jid in ipairs(search_by_number(number)) do + reply:tag("item", { jid = jid }):up(); + end + else + reply = st.error_reply(stanza, "modify", + "bad-request", "no nick or e164 field found"); end return origin.send(reply);