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