mod_posix: Some (perhaps temporary) changes to re-lock the pidfile after truncating...
[prosody.git] / plugins / mod_private.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11 local st = require "util.stanza"
12
13 local jid_split = require "util.jid".split;
14 local datamanager = require "util.datamanager"
15
16 module:add_feature("jabber:iq:private");
17
18 module:add_iq_handler("c2s", "jabber:iq:private",
19         function (session, stanza)
20                 local type = stanza.attr.type;
21                 local query = stanza.tags[1];
22                 if (type == "get" or type == "set") and query.name == "query" then
23                         local node, host = jid_split(stanza.attr.to);
24                         if not(node or host) or (node == session.username and host == session.host) then
25                                 node, host = session.username, session.host;
26                                 if #query.tags == 1 then
27                                         local tag = query.tags[1];
28                                         local key = tag.name..":"..tag.attr.xmlns;
29                                         local data, err = datamanager.load(node, host, "private");
30                                         if err then
31                                                 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
32                                                 return true;
33                                         end
34                                         if stanza.attr.type == "get" then
35                                                 if data and data[key] then
36                                                         session.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"}):add_child(st.deserialize(data[key])));
37                                                 else
38                                                         session.send(st.reply(stanza):add_child(stanza.tags[1]));
39                                                 end
40                                         else -- set
41                                                 if not data then data = {}; end;
42                                                 if #tag == 0 then
43                                                         data[key] = nil;
44                                                 else
45                                                         data[key] = st.preserialize(tag);
46                                                 end
47                                                 -- TODO delete datastore if empty
48                                                 if datamanager.store(node, host, "private", data) then
49                                                         session.send(st.reply(stanza));
50                                                 else
51                                                         session.send(st.error_reply(stanza, "wait", "internal-server-error"));
52                                                 end
53                                         end
54                                 else
55                                         session.send(st.error_reply(stanza, "modify", "bad-format"));
56                                 end
57                         else
58                                 session.send(st.error_reply(stanza, "cancel", "forbidden"));
59                         end
60                 end
61         end);