Providing some human readable error messages and some fixes.
[prosody.git] / plugins / mod_private.lua
1
2 local st = require "util.stanza"
3 local send = require "core.sessionmanager".send_to_session
4
5 local jid_split = require "util.jid".split;
6 local datamanager = require "util.datamanager"
7
8 add_iq_handler("c2s", "jabber:iq:private",
9         function (session, stanza)
10                 local type = stanza.attr.type;
11                 local query = stanza.tags[1];
12                 if (type == "get" or type == "set") and query.name == "query" then
13                         local node, host = jid_split(stanza.attr.to);
14                         if not(node or host) or (node == session.username and host == session.host) then
15                                 node, host = session.username, session.host;
16                                 if #query.tags == 1 then
17                                         local tag = query.tags[1];
18                                         local key = tag.name..":"..tag.attr.xmlns;
19                                         local data = datamanager.load(node, host, "private");
20                                         if stanza.attr.type == "get" then
21                                                 if data and data[key] then
22                                                         send(session, st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"}):add_child(st.deserialize(data[key])));
23                                                 else
24                                                         send(session, st.reply(stanza):add_child(stanza.tags[1]));
25                                                 end
26                                         else -- set
27                                                 if not data then data = {}; end;
28                                                 if #tag == 0 then
29                                                         data[key] = nil;
30                                                 else
31                                                         data[key] = st.preserialize(tag);
32                                                 end
33                                                 -- TODO delete datastore if empty
34                                                 if datamanager.store(node, host, "private", data) then
35                                                         send(session, st.reply(stanza));
36                                                 else
37                                                         send(session, st.error_reply(stanza, "wait", "internal-server-error"));
38                                                 end
39                                         end
40                                 else
41                                         send(session, st.error_reply(stanza, "modify", "bad-format"));
42                                 end
43                         else
44                                 send(session, st.error_reply(stanza, "cancel", "forbidden"));
45                         end
46                 end
47         end);