Files
vnctalk-prosody/vnctalk/mod_vnc_timestamp.lua
T

71 lines
2.5 KiB
Lua

-- vnc timestamp module
-- based on https://modules.prosody.im/mod_delay.html
-- Copyright (C) 2016-2017 Thilo Molitor
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
local add_filter = require "util.filters".add_filter;
local remove_filter = require "util.filters".remove_filter;
local datetime = require "util.datetime";
local st = require "util.stanza";
local uuid = require "util.uuid";
local xmlns_timestamp = "xmpp:vnctalk:stamp";
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
-- Raise an error if the modules has been loaded as a component in prosody's config
if module:get_host_type() == "component" then
error(module.name.." should NOT be loaded as a component, check out http://prosody.im/doc/components", 0);
end
local add_delay = function(stanza, session)
if stanza and stanza.name == "message" and stanza:get_child("stamp", xmlns_timestamp) == nil then
-- only add delay tag to chat or groupchat messages (should we add a delay to anything else, too???)
if stanza.attr.type == "chat" or stanza.attr.type == "groupchat" then
if stanza:get_child("body") then
-- session.log("debug", "adding delay to message %s", tostring(stanza));
local stamp = os.time();
if (stanza.attr.from and stanza.attr.id) then
-- module:log("info", "vnc-stamp added to stanza %s", dumpTable(stanza));
local iqid = uuid.generate();
module:send(st.iq({ id=iqid, to=stanza.attr.from, from=module.host, type="result", t = tostring(stamp) , f=stanza.attr.id}));
-- session:send(st.stanza("t", { xmlns =xmlns_timestamp, t = stamp }));
end
stanza = stanza:tag("stamp", { xmlns = xmlns_timestamp, from = session.host, stamp = tostring(stamp) });
end;
end
end
return stanza;
end
module:hook("resource-bind", function(event)
add_filter(event.session, "stanzas/in", add_delay, 1);
end);
module:hook("smacks-hibernation-end", function(event)
-- older smacks module versions send only the "intermediate" session in event.session and no session.resumed one
if event.resumed then
add_filter(event.resumed, "stanzas/in", add_delay, 1);
end
end);
module:hook("pre-resource-unbind", function (event)
remove_filter(event.session, "stanzas/in", add_delay);
end);