1-- Prosody IM
2-- Copyright (C) 2008-2009 Matthew Wild
3-- Copyright (C) 2008-2009 Waqas Hussain
4-- Copyright (C) 2009 Jeff Mitchell
5--
6-- This project is MIT/X11 licensed. Please see the
7-- COPYING file in the source package for more information.
8--
9
10local datamanager = require "util.datamanager";
11local jid_bare = require "util.jid".bare;
12local jid_split = require "util.jid".split;
13local st = require "util.stanza";
14local datetime = require "util.datetime";
15local ipairs = ipairs;
16local onhold_jids = module:get_option("onhold_jids") or {};
17for _, jid in ipairs(onhold_jids) do onhold_jids[jid] = true; end
18
19function process_message(event)
20	local session, stanza = event.origin, event.stanza;
21	local to = stanza.attr.to;
22	local from = jid_bare(stanza.attr.from);
23	local node, host;
24	local onhold_node, onhold_host;
25
26	if to then
27		node, host = jid_split(to)
28	else
29		node, host = session.username, session.host;
30	end
31
32	if onhold_jids[from] then
33		stanza.attr.stamp, stanza.attr.stamp_legacy = datetime.datetime(), datetime.legacy();
34		local result = datamanager.list_append(node, host, "onhold", st.preserialize(stanza));
35		stanza.attr.stamp, stanza.attr.stamp_legacy = nil, nil;
36		return true;
37	end
38	return nil;
39end
40
41module:hook("message/bare", process_message, 5);
42
43module:hook("message/full", process_message, 5);
44
45module:hook("presence/bare", function(event)
46	if event.origin.presence then return nil; end
47	local session = event.origin;
48	local node, host = session.username, session.host;
49	local from;
50	local de_stanza;
51
52	local data = datamanager.list_load(node, host, "onhold");
53	local newdata = {};
54	if not data then return nil; end
55	for _, stanza in ipairs(data) do
56		de_stanza = st.deserialize(stanza);
57		from = jid_bare(de_stanza.attr.from);
58		if not onhold_jids[from] then
59			de_stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = host, stamp = de_stanza.attr.stamp}):up(); -- XEP-0203
60			de_stanza:tag("x", {xmlns = "jabber:x:delay", from = host, stamp = de_stanza.attr.stamp_legacy}):up(); -- XEP-0091 (deprecated)
61			de_stanza.attr.stamp, de_stanza.attr.stamp_legacy = nil, nil;
62			session.send(de_stanza);
63		else
64			table.insert(newdata, stanza);
65		end
66	end
67	datamanager.list_store(node, host, "onhold", newdata);
68	return nil;
69end, 5);
70
71