Files

124 lines
3.0 KiB
Lua

--require "socket"
--rawset(_G, "PROXY", false); -- socket module accesses global variable PROXY
local http = require "net.http"
local json = require "util.json"
-- local ltn12 = require "ltn12"
local jid_bare = require "util.jid".bare;
local jid_split = require "util.jid".split;
local timer = require "util.timer";
local st = require "util.stanza";
local time_now = os.time;
local delAPIURL = module:get_option_string("del_api_url");
local moduleIsActive = delAPIURL;
local function contains(list, x)
if (list) then
for _, v in pairs(list) do
if (v == x) then return true end
end
end
return false
end
local function starts_with(str, start)
return str:sub(1, #start) == start
end
local function dumpTable(t, depth)
local result = "";
if (not depth) then depth = 0 end;
local prefix = string.rep(" ", depth);
if (type(t) ~= "table") then
result = result.." "..tostring(t);
else
for key,value in pairs(t) do
result = result..prefix..tostring(key).." => "..dumpTable(value, depth+1).."\n"
end
end
return result;
end
local function trim(s)
if s and (type(s) == "string") then
return (s:gsub("^%s*(.-)%s*$", "%1"))
else
return s
end
end
local function getPostCallback()
return function(body, code, response)
if code ~= 200 then
module:log("error", "DEL result: HTTP %s", tostring(code))
-- if body then module:log("error", body); end
end
return false -- close request
end
end
local function postDEL(_from, _replace)
if (not moduleIsActive) then return end;
local delRequest = {
from = _from,
msgid = _replace
};
local data = json.encode(delRequest);
local httpRequestOptions = {
method = "POST",
body = data,
headers = {
["Connection"] = 'close',
["Content-Type"] = "application/json; charset=utf-8"
}
}
module:log("info", "sending delete for message id %s from %s", _replace, _from);
--module:log("info", "sendingHTTP request to %s", delAPIURL);
http.request(delAPIURL, httpRequestOptions, getPostCallback())
end
local function message_handler(event, fromLocal)
local origin, stanza = event.origin, event.stanza;
local message_id = event.stanza.attr.id;
local type = stanza.attr.type or "normal";
local from = jid_bare(stanza.attr.from);
local to = jid_bare(stanza.attr.to) or from;
if (to ~= from)
then
local content = trim(stanza:find("body#"));
local isMessageCorrection = stanza:find("{urn:xmpp:message-correct:0}replace@id");
-- module:log("info", "got replaceid: %s", isMessageCorrection);
if (isMessageCorrection) then
postDEL(from, isMessageCorrection);
end
end
return;
end
local function message_handler_from_local(event)
return message_handler(event, true);
end
if (moduleIsActive)
then
module:hook("pre-message/bare", message_handler_from_local, 5);
module:hook("pre-message/full", message_handler_from_local, 5);
module:log("info", "DEL HTTP upload files ACTIVATED");
end