mod_delay/mod_delay.lua
changeset 3474 c0fc739a1b81
parent 3473 85b849d5ec88
child 3475 b4bcb84997e7
equal deleted inserted replaced
3473:85b849d5ec88 3474:c0fc739a1b81
     1 -- Copyright (C) 2016-2017 Thilo Molitor
       
     2 --
       
     3 -- This project is MIT/X11 licensed. Please see the
       
     4 -- COPYING file in the source package for more information.
       
     5 --
       
     6 
       
     7 local add_filter = require "util.filters".add_filter;
       
     8 local remove_filter = require "util.filters".remove_filter;
       
     9 local datetime = require "util.datetime";
       
    10 
       
    11 local xmlns_delay = "urn:xmpp:delay";
       
    12 
       
    13 -- Raise an error if the modules has been loaded as a component in prosody's config
       
    14 if module:get_host_type() == "component" then
       
    15 	error(module.name.." should NOT be loaded as a component, check out http://prosody.im/doc/components", 0);
       
    16 end
       
    17 
       
    18 local add_delay = function(stanza, session)
       
    19 	if stanza and stanza.name == "message" and stanza:get_child("delay", xmlns_delay) == nil then
       
    20 		-- only add delay tag to chat or groupchat messages (should we add a delay to anything else, too???)
       
    21 		if stanza.attr.type == "chat" or stanza.attr.type == "groupchat" then
       
    22 			-- session.log("debug", "adding delay to message %s", tostring(stanza));
       
    23 			stanza = stanza:tag("delay", { xmlns = xmlns_delay, from = session.host, stamp = datetime.datetime()});
       
    24 		end
       
    25 	end
       
    26 	return stanza;
       
    27 end
       
    28 
       
    29 module:hook("resource-bind", function(event)
       
    30 	add_filter(event.session, "stanzas/in", add_delay, 1);
       
    31 end);
       
    32 module:hook("smacks-hibernation-end", function(event)
       
    33 	-- older smacks module versions send only the "intermediate" session in event.session and no session.resumed one
       
    34 	if event.resumed then
       
    35 		add_filter(event.resumed, "stanzas/in", add_delay, 1);
       
    36 	end
       
    37 end);
       
    38 module:hook("pre-resource-unbind", function (event)
       
    39 	remove_filter(event.session, "stanzas/in", add_delay);
       
    40 end);