2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 local st = require "util.stanza"
24 local jid_split = require "util.jid".split;
25 local datamanager = require "util.datamanager"
27 module:add_feature("jabber:iq:private");
29 module:add_iq_handler("c2s", "jabber:iq:private",
30 function (session, stanza)
31 local type = stanza.attr.type;
32 local query = stanza.tags[1];
33 if (type == "get" or type == "set") and query.name == "query" then
34 local node, host = jid_split(stanza.attr.to);
35 if not(node or host) or (node == session.username and host == session.host) then
36 node, host = session.username, session.host;
37 if #query.tags == 1 then
38 local tag = query.tags[1];
39 local key = tag.name..":"..tag.attr.xmlns;
40 local data = datamanager.load(node, host, "private");
41 if stanza.attr.type == "get" then
42 if data and data[key] then
43 session.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"}):add_child(st.deserialize(data[key])));
45 session.send(st.reply(stanza):add_child(stanza.tags[1]));
48 if not data then data = {}; end;
52 data[key] = st.preserialize(tag);
54 -- TODO delete datastore if empty
55 if datamanager.store(node, host, "private", data) then
56 session.send(st.reply(stanza));
58 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
62 session.send(st.error_reply(stanza, "modify", "bad-format"));
65 session.send(st.error_reply(stanza, "cancel", "forbidden"));