Feature added: prosody module extended to request delete token

This commit is contained in:
steckbrief 2016-08-20 21:42:50 +02:00
parent dd33a02422
commit ccf99fc8d0

View file

@ -22,7 +22,7 @@ local json = require"util.json";
module:depends("disco");
-- namespace
local xmlns_http_upload = "urn:xmpp:http:upload";
local xmlns_http_upload = "urn:xmpp:filetransfer:http";
module:add_feature(xmlns_http_upload);
@ -42,6 +42,8 @@ module:hook("iq/host/"..xmlns_http_upload..":request", function (event)
origin.send(st.error_reply(stanza, "cancel", "internal-server-error"));
return true;
end
local slot_type = request.attr.type;
if not slot_type or slot_type ~= "upload" then
-- validate
local filename = request:get_child_text("filename");
if not filename then
@ -132,5 +134,71 @@ module:hook("iq/host/"..xmlns_http_upload..":request", function (event)
reply:tag("get"):text(get_url):up();
reply:tag("put"):text(put_url):up();
origin.send(reply);
elseif slot_type ~= "delete" then
-- validate
local fileurl = request:get_child_text("fileurl");
if not fileurl then
origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid fileurl"));
return true;
end
-- build the body
local reqbody = "xmpp_server_key=" .. xmpp_server_key .. "&size=" .. filesize .. "&filename=" .. filename .. "&user_jid=" .. orig_from;
-- the request
local respbody, statuscode = http.request(external_url, reqbody);
respbody = string.gsub(respbody, "\\/", "/")
local delete_token = nil;
-- check the response
if statuscode == 500 then
origin.send(st.error_reply(stanza, "cancel", "service-unavailable", respbody));
elseif statuscode == 406 or statuscode == 400 or statuscode == 403 then
local errobj, pos, err = json.decode(respbody);
if err then
origin.send(st.error_reply(stanza, "wait", "internal-server-error", err));
return true;
else
if errobj["err_code"] ~= nil and errobj["msg"] ~= nil then
if errobj.err_code == 4 then
origin.send(st.error_reply(stanza, "cancel", "internal-server-error", errobj.msg,
st.stanza("missing-parameter", {xmlns=xmlns_http_upload})
:text(errobj.parameters.missing_parameter)));
return true;
else
origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "unknown err_code"));
return true;
end
elseif statuscode == 403 and errobj["msg"] ~= nil then
origin.send(st.error_reply(stanza, "cancel", "internal-server-error", errobj.msg));
else
origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "msg or err_code not found"));
return true;
end
end
elseif statuscode == 200 then
local respobj, pos, err = json.decode(respbody);
if err then
origin.send(st.error_reply(stanza, "wait", "internal-server-error", err));
return true;
else
if respobj["deletetoken"] ~= nil then
delete_token = respobj.deletetoken;
else
origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "deletetoken not found"));
return true;
end
end
else
origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "status code: " .. statuscode .. " response: " ..respbody));
return true;
end
local reply = st.reply(stanza);
reply:tag("slot", { xmlns = xmlns_http_upload });
reply:tag("deletetoken"):text(deletetoken):up();
origin.send(reply);
else
origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "status code: " .. statuscode .. " response: " ..respbody));
return true;
end
return true;
end);