Comparison

plugins/mod_private.lua @ 6343:e6839156bb82

mod_private: Use map store
author Kim Alvefur <zash@zash.se>
date Sat, 09 Aug 2014 21:52:32 +0200
parent 6341:ab9a1af80632
child 6354:bbb4a82db32e
comparison
equal deleted inserted replaced
6342:379faad8ce9f 6343:e6839156bb82
7 -- 7 --
8 8
9 9
10 local st = require "util.stanza" 10 local st = require "util.stanza"
11 11
12 local private_storage = module:open_store(); 12 local private_storage = module:open_store("private", "map");
13 13
14 module:add_feature("jabber:iq:private"); 14 module:add_feature("jabber:iq:private");
15 15
16 module:hook("iq/self/jabber:iq:private:query", function(event) 16 module:hook("iq/self/jabber:iq:private:query", function(event)
17 local origin, stanza = event.origin, event.stanza; 17 local origin, stanza = event.origin, event.stanza;
19 if #query.tags ~= 1 then 19 if #query.tags ~= 1 then
20 return origin.send(st.error_reply(stanza, "modify", "bad-format")); 20 return origin.send(st.error_reply(stanza, "modify", "bad-format"));
21 end 21 end
22 local tag = query.tags[1]; 22 local tag = query.tags[1];
23 local key = tag.name..":"..tag.attr.xmlns; 23 local key = tag.name..":"..tag.attr.xmlns;
24 local data, err = private_storage:get(origin.username);
25 if err then
26 return origin.send(st.error_reply(stanza, "wait", "internal-server-error", err));
27 end
28 if stanza.attr.type == "get" then 24 if stanza.attr.type == "get" then
29 if data and data[key] then 25 local data, err = private_storage:get(origin.username, key);
26 if data then
30 return origin.send(st.reply(stanza):query("jabber:iq:private"):add_child(st.deserialize(data))); 27 return origin.send(st.reply(stanza):query("jabber:iq:private"):add_child(st.deserialize(data)));
28 elseif err then
29 return origin.send(st.error_reply(stanza, "wait", "internal-server-error", err));
31 else 30 else
32 return origin.send(st.reply(stanza):add_child(query)); 31 return origin.send(st.reply(stanza):add_child(query));
33 end 32 end
34 else -- type == set 33 else -- type == set
35 if not data then data = {}; end; 34 local data;
36 if #tag == 0 then 35 if #tag ~= 0 then
37 data[key] = nil; 36 data = st.preserialize(tag);
38 else
39 data[key] = st.preserialize(tag);
40 end 37 end
41 -- TODO delete datastore if empty 38 -- TODO delete datastore if empty
42 local ok, err = private_storage:set(origin.username, data); 39 local ok, err = private_storage:set(origin.username, key, data);
43 if not ok then 40 if not ok then
44 return origin.send(st.error_reply(stanza, "wait", "internal-server-error", err)); 41 return origin.send(st.error_reply(stanza, "wait", "internal-server-error", err));
45 end 42 end
46 return origin.send(st.reply(stanza)); 43 return origin.send(st.reply(stanza));
47 end 44 end