2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local st = require "util.stanza"
13 local jid_split = require "util.jid".split;
14 local datamanager = require "util.datamanager"
16 module:add_feature("jabber:iq:private");
18 module:add_iq_handler("c2s", "jabber:iq:private",
19 function (session, stanza)
20 local type = stanza.attr.type;
21 local query = stanza.tags[1];
22 if (type == "get" or type == "set") and query.name == "query" then
23 local node, host = jid_split(stanza.attr.to);
24 if not(node or host) or (node == session.username and host == session.host) then
25 node, host = session.username, session.host;
26 if #query.tags == 1 then
27 local tag = query.tags[1];
28 local key = tag.name..":"..tag.attr.xmlns;
29 local data = datamanager.load(node, host, "private");
30 if stanza.attr.type == "get" then
31 if data and data[key] then
32 session.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"}):add_child(st.deserialize(data[key])));
34 session.send(st.reply(stanza):add_child(stanza.tags[1]));
37 if not data then data = {}; end;
41 data[key] = st.preserialize(tag);
43 -- TODO delete datastore if empty
44 if datamanager.store(node, host, "private", data) then
45 session.send(st.reply(stanza));
47 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
51 session.send(st.error_reply(stanza, "modify", "bad-format"));
54 session.send(st.error_reply(stanza, "cancel", "forbidden"));