mod_delay/mod_delay.lua
author Kim Alvefur <zash@zash.se>
Sat, 02 Jun 2018 15:39:37 +0200
changeset 3095 8e5da12205b5
parent 3058 5e94061c1aa7
permissions -rw-r--r--
mod_storage_memory: Fix removal of data in keyvalue (thanks jonasw)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3058
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
     1
-- Copyright (C) 2016-2017 Thilo Molitor
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
     2
--
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
     3
-- This project is MIT/X11 licensed. Please see the
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
     4
-- COPYING file in the source package for more information.
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
     5
--
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
     6
2397
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
     7
local add_filter = require "util.filters".add_filter;
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
     8
local remove_filter = require "util.filters".remove_filter;
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
     9
local datetime = require "util.datetime";
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    10
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    11
local xmlns_delay = "urn:xmpp:delay";
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    12
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    13
-- Raise an error if the modules has been loaded as a component in prosody's config
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    14
if module:get_host_type() == "component" then
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    15
	error(module.name.." should NOT be loaded as a component, check out http://prosody.im/doc/components", 0);
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    16
end
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    17
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    18
local add_delay = function(stanza, session)
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    19
	if stanza and stanza.name == "message" and stanza:get_child("delay", xmlns_delay) == nil then
2439
05248d5a7166 mod_delay: Only add delay to messages of type chat or groupchat (fixes #811)
tmolitor <thilo@eightysoft.de>
parents: 2397
diff changeset
    20
		-- only add delay tag to chat or groupchat messages (should we add a delay to anything else, too???)
05248d5a7166 mod_delay: Only add delay to messages of type chat or groupchat (fixes #811)
tmolitor <thilo@eightysoft.de>
parents: 2397
diff changeset
    21
		if stanza.attr.type == "chat" or stanza.attr.type == "groupchat" then
05248d5a7166 mod_delay: Only add delay to messages of type chat or groupchat (fixes #811)
tmolitor <thilo@eightysoft.de>
parents: 2397
diff changeset
    22
			-- session.log("debug", "adding delay to message %s", tostring(stanza));
05248d5a7166 mod_delay: Only add delay to messages of type chat or groupchat (fixes #811)
tmolitor <thilo@eightysoft.de>
parents: 2397
diff changeset
    23
			stanza = stanza:tag("delay", { xmlns = xmlns_delay, from = session.host, stamp = datetime.datetime()});
05248d5a7166 mod_delay: Only add delay to messages of type chat or groupchat (fixes #811)
tmolitor <thilo@eightysoft.de>
parents: 2397
diff changeset
    24
		end
2397
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    25
	end
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    26
	return stanza;
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    27
end
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    28
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    29
module:hook("resource-bind", function(event)
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    30
	add_filter(event.session, "stanzas/in", add_delay, 1);
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    31
end);
3058
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
    32
module:hook("smacks-hibernation-end", function(event)
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
    33
	-- older smacks module versions send only the "intermediate" session in event.session and no session.resumed one
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
    34
	if event.resumed then
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
    35
		add_filter(event.resumed, "stanzas/in", add_delay, 1);
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
    36
	end
5e94061c1aa7 mod_delay: Don't break on smacks resume
tmolitor <thilo@eightysoft.de>
parents: 2439
diff changeset
    37
end);
2397
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    38
module:hook("pre-resource-unbind", function (event)
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    39
	remove_filter(event.session, "stanzas/in", add_delay);
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
    40
end);