f1eee55ec3a0d771fc68425c6f5434c6ca8c522a
[prosody.git] / plugins / mod_private.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
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.
9 -- 
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.
14 -- 
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.
18 --
19
20
21
22 local st = require "util.stanza"
23
24 local jid_split = require "util.jid".split;
25 local datamanager = require "util.datamanager"
26
27 require "core.discomanager".set("private", "jabber:iq:private");
28
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])));
44                                                 else
45                                                         session.send(st.reply(stanza):add_child(stanza.tags[1]));
46                                                 end
47                                         else -- set
48                                                 if not data then data = {}; end;
49                                                 if #tag == 0 then
50                                                         data[key] = nil;
51                                                 else
52                                                         data[key] = st.preserialize(tag);
53                                                 end
54                                                 -- TODO delete datastore if empty
55                                                 if datamanager.store(node, host, "private", data) then
56                                                         session.send(st.reply(stanza));
57                                                 else
58                                                         session.send(st.error_reply(stanza, "wait", "internal-server-error"));
59                                                 end
60                                         end
61                                 else
62                                         session.send(st.error_reply(stanza, "modify", "bad-format"));
63                                 end
64                         else
65                                 session.send(st.error_reply(stanza, "cancel", "forbidden"));
66                         end
67                 end
68         end);