Annotate

mod_delay/mod_delay.lua @ 2393:3b2c94ea0c2e

mod_delay: initial commit
author tmolitor <thilo@eightysoft.de>
date Tue, 22 Nov 2016 21:15:01 +0100
child 2435:05248d5a7166
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2393
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
1 local add_filter = require "util.filters".add_filter;
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
2 local remove_filter = require "util.filters".remove_filter;
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
3 local datetime = require "util.datetime";
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
4
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
5 local xmlns_delay = "urn:xmpp:delay";
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
6
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
7 -- 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
8 if module:get_host_type() == "component" then
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
9 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
10 end
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
11
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
12 local add_delay = function(stanza, session)
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
13 if stanza and stanza.name == "message" and stanza:get_child("delay", xmlns_delay) == nil then
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
14 -- session.log("debug", "adding delay to message %s", tostring(stanza));
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
15 stanza = stanza:tag("delay", { xmlns = xmlns_delay, from = session.host, stamp = datetime.datetime()});
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 return stanza;
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
18 end
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
19
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
20 module:hook("resource-bind", function(event)
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
21 add_filter(event.session, "stanzas/in", add_delay, 1);
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
22 end);
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
23
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
24 module:hook("pre-resource-unbind", function (event)
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
25 remove_filter(event.session, "stanzas/in", add_delay);
3b2c94ea0c2e mod_delay: initial commit
tmolitor <thilo@eightysoft.de>
parents:
diff changeset
26 end);