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.
10 local st = require "util.stanza"
12 local private_storage = module:open_store();
14 module:add_feature("jabber:iq:private");
16 module:hook("iq/self/jabber:iq:private:query", function(event)
17 local origin, stanza = event.origin, event.stanza;
18 local type = stanza.attr.type;
19 local query = stanza.tags[1];
20 if #query.tags == 1 then
21 local tag = query.tags[1];
22 local key = tag.name..":"..tag.attr.xmlns;
23 local data, err = private_storage:get(origin.username);
25 origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
28 if stanza.attr.type == "get" then
29 if data and data[key] then
30 origin.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"}):add_child(st.deserialize(data[key])));
32 origin.send(st.reply(stanza):add_child(stanza.tags[1]));
35 if not data then data = {}; end;
39 data[key] = st.preserialize(tag);
41 -- TODO delete datastore if empty
42 if private_storage:set(origin.username, data) then
43 origin.send(st.reply(stanza));
45 origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
49 origin.send(st.error_reply(stanza, "modify", "bad-format"));