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
local st = require "util.stanza"
11
 
12
local jid_split = require "util.jid".split;
13
local datamanager = require "util.datamanager"
14
 
15
module:add_feature("jabber:iq:private");
16
 
17
module:hook("iq/self/jabber:iq:private:query", function(event)
18
	local origin, stanza = event.origin, event.stanza;
19
	local type = stanza.attr.type;
20
	local query = stanza.tags[1];
21
	if #query.tags == 1 then
22
		local tag = query.tags[1];
23
		local key = tag.name..":"..tag.attr.xmlns;
24
		local data, err = datamanager.load(origin.username, origin.host, "private");
25
		if err then
26
			origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
27
			return true;
28
		end
29
		if stanza.attr.type == "get" then
30
			if data and data[key] then
31
				origin.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"}):add_child(st.deserialize(data[key])));
32
			else
33
				origin.send(st.reply(stanza):add_child(stanza.tags[1]));
34
			end
35
		else -- set
36
			if not data then data = {}; end;
37
			if #tag == 0 then
38
				data[key] = nil;
39
			else
40
				data[key] = st.preserialize(tag);
41
			end
42
			-- TODO delete datastore if empty
43
			if datamanager.store(origin.username, origin.host, "private", data) then
44
				origin.send(st.reply(stanza));
45
			else
46
				origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
47
			end
48
		end
49
	else
50
		origin.send(st.error_reply(stanza, "modify", "bad-format"));
51
	end
52
	return true;
53
end);