Software /
code /
prosody
Comparison
plugins/mod_private.lua @ 3522:4646b5b039ca
mod_private: Updated to use the new events API. Smaller, more robust.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 16 Oct 2010 04:11:48 +0500 |
parent | 3114:75ea1aff69da |
child | 5371:706206e191e8 |
comparison
equal
deleted
inserted
replaced
3521:896ffec79f57 | 3522:4646b5b039ca |
---|---|
5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
7 -- | 7 -- |
8 | 8 |
9 | 9 |
10 | |
11 local st = require "util.stanza" | 10 local st = require "util.stanza" |
12 | 11 |
13 local jid_split = require "util.jid".split; | 12 local jid_split = require "util.jid".split; |
14 local datamanager = require "util.datamanager" | 13 local datamanager = require "util.datamanager" |
15 | 14 |
16 module:add_feature("jabber:iq:private"); | 15 module:add_feature("jabber:iq:private"); |
17 | 16 |
18 module:add_iq_handler("c2s", "jabber:iq:private", | 17 module:hook("iq/self/jabber:iq:private:query", function(event) |
19 function (session, stanza) | 18 local origin, stanza = event.origin, event.stanza; |
20 local type = stanza.attr.type; | 19 local type = stanza.attr.type; |
21 local query = stanza.tags[1]; | 20 local query = stanza.tags[1]; |
22 if (type == "get" or type == "set") and query.name == "query" then | 21 if #query.tags == 1 then |
23 local node, host = jid_split(stanza.attr.to); | 22 local tag = query.tags[1]; |
24 if not(node or host) or (node == session.username and host == session.host) then | 23 local key = tag.name..":"..tag.attr.xmlns; |
25 node, host = session.username, session.host; | 24 local data, err = datamanager.load(origin.username, origin.host, "private"); |
26 if #query.tags == 1 then | 25 if err then |
27 local tag = query.tags[1]; | 26 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); |
28 local key = tag.name..":"..tag.attr.xmlns; | 27 return true; |
29 local data, err = datamanager.load(node, host, "private"); | 28 end |
30 if err then | 29 if stanza.attr.type == "get" then |
31 session.send(st.error_reply(stanza, "wait", "internal-server-error")); | 30 if data and data[key] then |
32 return true; | 31 origin.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"}):add_child(st.deserialize(data[key]))); |
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 | 32 else |
58 session.send(st.error_reply(stanza, "cancel", "forbidden")); | 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")); | |
59 end | 47 end |
60 end | 48 end |
61 end); | 49 else |
50 origin.send(st.error_reply(stanza, "modify", "bad-format")); | |
51 end | |
52 return true; | |
53 end); |