Software /
code /
prosody
Comparison
plugins/mod_private.lua @ 6341:ab9a1af80632
mod_private: Cleanup and more tailcalls
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 09 Aug 2014 21:48:11 +0200 |
parent | 5776:bd0ff8ae98a8 |
child | 6343:e6839156bb82 |
child | 6353:a868d5d6a83f |
comparison
equal
deleted
inserted
replaced
6340:7e820979fd9b | 6341:ab9a1af80632 |
---|---|
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; |
18 local type = stanza.attr.type; | |
19 local query = stanza.tags[1]; | 18 local query = stanza.tags[1]; |
20 if #query.tags == 1 then | 19 if #query.tags ~= 1 then |
21 local tag = query.tags[1]; | 20 return origin.send(st.error_reply(stanza, "modify", "bad-format")); |
22 local key = tag.name..":"..tag.attr.xmlns; | 21 end |
23 local data, err = private_storage:get(origin.username); | 22 local tag = query.tags[1]; |
24 if err then | 23 local key = tag.name..":"..tag.attr.xmlns; |
25 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); | 24 local data, err = private_storage:get(origin.username); |
26 return true; | 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 | |
29 if data and data[key] then | |
30 return origin.send(st.reply(stanza):query("jabber:iq:private"):add_child(st.deserialize(data))); | |
31 else | |
32 return origin.send(st.reply(stanza):add_child(query)); | |
27 end | 33 end |
28 if stanza.attr.type == "get" then | 34 else -- type == set |
29 if data and data[key] then | 35 if not data then data = {}; end; |
30 origin.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"}):add_child(st.deserialize(data[key]))); | 36 if #tag == 0 then |
31 else | 37 data[key] = nil; |
32 origin.send(st.reply(stanza):add_child(stanza.tags[1])); | 38 else |
33 end | 39 data[key] = st.preserialize(tag); |
34 else -- set | |
35 if not data then data = {}; end; | |
36 if #tag == 0 then | |
37 data[key] = nil; | |
38 else | |
39 data[key] = st.preserialize(tag); | |
40 end | |
41 -- TODO delete datastore if empty | |
42 if private_storage:set(origin.username, data) then | |
43 origin.send(st.reply(stanza)); | |
44 else | |
45 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); | |
46 end | |
47 end | 40 end |
48 else | 41 -- TODO delete datastore if empty |
49 origin.send(st.error_reply(stanza, "modify", "bad-format")); | 42 local ok, err = private_storage:set(origin.username, data); |
43 if not ok then | |
44 return origin.send(st.error_reply(stanza, "wait", "internal-server-error", err)); | |
45 end | |
46 return origin.send(st.reply(stanza)); | |
50 end | 47 end |
51 return true; | |
52 end); | 48 end); |