From Aqua Owl, 4 Years ago, written in Lua.
Embed
  1. -- mod_http_upload_external
  2. --
  3. -- Copyright (C) 2016 Sebastian Luksch
  4. --
  5. -- This file is MIT/X11 licensed.
  6. --
  7. -- Implementation of HTTP Upload file transfer mechanism used by Conversations
  8. --
  9. -- Query external HTTP server to retrieve URLs
  10. --
  11.  
  12. -- configuration
  13. local external_url = "https://snippets.thedevstack.de/httpupload/index.php";
  14. local xmpp_server_key = "1323978hjkgh12";
  15.  
  16. -- imports
  17. local st = require"util.stanza";
  18. local http = require"socket.http";
  19. local json = require"util.json";
  20.  
  21. -- depends
  22. module:depends("disco");
  23.  
  24. -- namespace
  25. local xmlns_http_upload = "urn:xmpp:http:upload";
  26.  
  27. module:add_feature(xmlns_http_upload);
  28.  
  29. -- hooks
  30. module:hook("iq/host/"..xmlns_http_upload..":request", function (event)
  31.    local stanza, origin = event.stanza, event.origin;
  32.    local orig_from = stanza.attr.from;
  33.    local request = stanza.tags[1];
  34.    -- local clients only
  35.    if origin.type ~= "c2s" then
  36.       origin.send(st.error_reply(stanza, "cancel", "not-authorized"));
  37.       return true;
  38.    end
  39.    -- validate
  40.    local filename = request:get_child_text("filename");
  41.    if not filename then
  42.       origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid filename"));
  43.       return true;
  44.    end
  45.    local filesize = tonumber(request:get_child_text("size"));
  46.    if not filesize then
  47.       origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing or invalid file size"));
  48.       return true;
  49.    end
  50.  
  51.    local content_type = request:get_child_text("content-type");
  52.    
  53.    -- build the body
  54.    local reqbody = "xmpp_server_key=" .. xmpp_server_key .. "&size=" .. filesize .. "&filename=" .. filename .. "&user_jid=" .. orig_from;
  55.    if content_type then
  56.       reqbody = reqbody .. "&content_type=" .. content_type;
  57.    end
  58.  
  59.    -- the request
  60.    local respbody, statuscode = http.request(external_url, reqbody);
  61.    respbody = string.gsub(respbody, "\\/", "/")
  62.  
  63.    local get_url = nil;
  64.    local put_url = nil;
  65.  
  66.    -- check the response
  67.    if statuscode == 500 then
  68.       origin.send(st.error_reply(stanza, "cancel", "service-unavailable", respbody));
  69.    elseif statuscode == 406 or statuscode == 400 or statuscode == 403 then
  70.       local errobj, pos, err = json.decode(respbody);
  71.       if err then
  72.          origin.send(st.error_reply(stanza, "wait", "internal-server-error", err));
  73.          return true;
  74.       else
  75.          if errobj["err_code"] ~= nil and errobj["msg"] ~= nil then
  76.             if errobj.err_code == 1 then
  77.                origin.send(st.error_reply(stanza, "modify", "not-acceptable", errobj.msg));
  78.                return true;
  79.             elseif errobj.err_code == 2 then
  80.                origin.send(st.error_reply(stanza, "modify", "not-acceptable", errobj.msg,
  81.                   st.stanza("file-too-large", {xmlns=xmlns_http_upload})
  82.                      :tag("max-size"):text(errobj.parameters.max_file_size)));
  83.                return true;
  84.             elseif errobj.err_code == 3 then
  85.                origin.send(st.error_reply(stanza, "modify", "not-acceptable", errobj.msg,
  86.                   st.stanza("invalid-character", {xmlns=xmlns_http_upload})
  87.                      :text(errobj.parameters.invalid_character)));
  88.                return true;
  89.             elseif errobj.err_code == 4 then
  90.                origin.send(st.error_reply(stanza, "cancel", "internal-server-error", errobj.msg,
  91.                   st.stanza("missing-parameter", {xmlns=xmlns_http_upload})
  92.                      :text(errobj.parameters.missing_parameter)));
  93.                return true;
  94.             else
  95.                origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "unknown err_code"));
  96.                return true;
  97.             end
  98.          elseif statuscode == 403 and errobj["msg"] ~= nil then
  99.             origin.send(st.error_reply(stanza, "cancel", "internal-server-error", errobj.msg));
  100.          else
  101.             origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "msg or err_code not found"));
  102.             return true;
  103.          end
  104.       end
  105.    elseif statuscode == 200 then
  106.       local respobj, pos, err = json.decode(respbody);
  107.       if err then
  108.          origin.send(st.error_reply(stanza, "wait", "internal-server-error", err));
  109.          return true;
  110.       else
  111.          if respobj["get"] ~= nil and respobj["put"] ~= nil then
  112.             get_url = respobj.get;
  113.             put_url = respobj.put;
  114.          else
  115.             origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "get or put not found"));
  116.             return true;
  117.          end
  118.       end
  119.    else
  120.       origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "status code: " .. statuscode .. " response: " ..respbody));
  121.       return true;
  122.    end
  123.  
  124.    local reply = st.reply(stanza);
  125.    reply:tag("slot", { xmlns = xmlns_http_upload });
  126.    reply:tag("get"):text(get_url):up();
  127.    reply:tag("put"):text(put_url):up();
  128.    origin.send(reply);
  129.    return true;
  130. end);
  131.  

Replies to Untitled rss

Title Name Language When
Re: Untitled Wet Plover lua 4 Years ago.
Re: Untitled Cream Mockingjay lua 4 Years ago.
Re: Untitled Innocent Giraffe lua 4 Years ago.
Re: Untitled Cobalt Partdridge lua 4 Years ago.