annotate mod_privacy/mod_privacy.lua @ 8:10502594a49b

adds mod_privacy; lists creating, editing and deletion working.
author Thilo Cestonaro <thilo@cestona.ro>
date Fri, 25 Sep 2009 16:56:50 +0200
parents
children 7d70faba234c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
1 -- Prosody IM
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
4 -- Copyright (C) 2009 Thilo Cestonaro
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
5 --
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
8 --
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
9
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
10
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
11 local prosody = prosody;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
12 local helpers = require "util/helpers";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
13 local st = require "util.stanza";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
14 local datamanager = require "util.datamanager";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
15 local privacy_lists = nil
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
16 local bare_sessions = bare_sessions;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
17
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
18
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
19 function findNamedList (name)
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
20 local ret = nil
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
21 if privacy_lists.lists == nil then return nil; end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
22
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
23 for i=1, #privacy_lists.lists do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
24 if privacy_lists.lists[i].name == name then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
25 ret = i;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
26 break;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
27 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
28 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
29 return ret;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
30 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
31
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
32 function declineList (origin, stanza, which)
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
33 module:log("info", "User requests to decline the use of privacy list: %s", which);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
34 privacy_lists[which] = nil;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
35 origin.send(st.reply(stanza));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
36 return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
37 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
38
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
39 function activateList (origin, stanza, which, name)
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
40 module:log("info", "User requests to change the privacy list: %s, to be list named %s", which, name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
41 local ret = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
42 local idx = findNamedList(name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
43
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
44 if privacy_lists[which] == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
45 privacy_lists[which] = "";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
46 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
47
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
48 if privacy_lists[which] ~= name and idx ~= nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
49 privacy_lists[which] = name;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
50 origin.send(st.reply(stanza));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
51 ret = true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
52 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
53 return ret;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
54 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
55
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
56 function deleteList (origin, stanza, name)
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
57 module:log("info", "User requests to delete privacy list: %s", name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
58 local ret = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
59 local idx = findNamedList(name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
60
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
61 if idx ~= nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
62 table.remove(privacy_lists.lists, idx);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
63 origin.send(st.reply(stanza));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
64 ret = true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
65 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
66 return ret;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
67 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
68
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
69 function createOrReplaceList (origin, stanza, name, entries)
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
70 module:log("info", "User requests to create / replace list named %s, item count: %d", name, #entries);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
71 local ret = true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
72 local idx = findNamedList(name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
73 local bare_jid = origin.username.."@"..origin.host;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
74
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
75 if privacy_lists.lists == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
76 privacy_lists.lists = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
77 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
78
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
79 if idx == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
80 idx = #privacy_lists.lists + 1;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
81 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
82
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
83 local list = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
84 list.name = name;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
85 list.items = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
86
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
87 for _,item in ipairs(entries) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
88 tmp = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
89 tmp["type"] = item.attr.type;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
90 tmp["value"] = item.attr.value;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
91 tmp["action"] = item.attr.action;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
92 tmp["order"] = item.attr.order;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
93 tmp["presence-in"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
94 tmp["presence-out"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
95 tmp["message"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
96 tmp["iq"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
97
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
98 if #item.tags > 0 then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
99 for _,tag in ipairs(item.tags) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
100 tmp[tag.name] = true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
101 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
102 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
103 list.items[#list.items + 1] = tmp;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
104 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
105
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
106 privacy_lists.lists[idx] = list;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
107 origin.send(st.reply(stanza));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
108 if bare_sessions[bare_jid] ~= nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
109 iq = st.iq ( { type = "set", id="push1" } );
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
110 iq:tag ("query", { xmlns = "jabber:iq:privacy" } );
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
111 iq:tag ("list", { name = list.name } ):up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
112 iq:up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
113 for resource, session in pairs(bare_sessions[bare_jid].sessions) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
114 iq.attr.to = bare_jid.."/"..resource
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
115 session.send(iq);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
116 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
117 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
118 return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
119 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
120
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
121 function getList(origin, stanza, name)
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
122 module:log("info", "User requests list named: %s", name or "nil");
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
123 local ret = true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
124 local idx = findNamedList(name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
125 local reply = st.reply(stanza);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
126 reply = reply:tag("query", {xmlns="jabber:iq:privacy"});
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
127
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
128 if idx == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
129 reply:tag("active", {name=privacy_lists.active or ""}):up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
130 reply:tag("default", {name=privacy_lists.default or ""}):up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
131 if privacy_lists.lists then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
132 for _,list in ipairs(privacy_lists.lists) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
133 reply:tag("list", {name=list.name}):up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
134 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
135 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
136 else
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
137 list = privacy_lists.lists[idx];
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
138 reply = reply:tag("list", {name=list.name});
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
139 for _,item in ipairs(list.items) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
140 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
141 if item["message"] then reply:tag("message"):up(); end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
142 if item["iq"] then reply:tag("iq"):up(); end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
143 if item["presence-in"] then reply:tag("presence-in"):up(); end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
144 if item["presence-out"] then reply:tag("presence-out"):up(); end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
145 reply:up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
146 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
147 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
148
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
149 origin.send(reply);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
150 return ret;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
151 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
152
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
153 -- "[tagname]/[target-type]/[payload-namespace]:[payload-tagname]"
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
154 module:hook("iq/bare/jabber:iq:privacy:query", function(data)
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
155 local origin, stanza = data.origin, data.stanza;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
156
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
157 if stanza.attr.to == nil then -- only service requests to own bare JID
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
158 local query = stanza.tags[1]; -- the query element
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
159 local valid = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
160 privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
161
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
162 if stanza.attr.type == "set" then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
163 if #query.tags >= 1 then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
164 for _,tag in ipairs(query.tags) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
165 if tag.name == "active" or tag.name == "default" then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
166 if tag.attr.name == nil then -- Client declines the use of active / default list
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
167 valid = declineList(origin, stanza, tag.name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
168 else -- Client requests change of active / default list
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
169 valid = activateList(origin, stanza, tag.name, tag.attr.name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
170 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
171 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
172 if #tag.tags == 0 then -- Client removes a privacy list
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
173 valid = deleteList(origin, stanza, tag.attr.name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
174 else -- Client edits a privacy list
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
175 valid = createOrReplaceList(origin, stanza, tag.attr.name, tag.tags)
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
176 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
177 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
178 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
179 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
180 elseif stanza.attr.type == "get" then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
181 local name = nil;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
182 if #query.tags >= 1 then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
183 for _,tag in ipairs(query.tags) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
184 if tag.name == "list" then -- Client requests a privacy list from server
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
185 name = tag.attr.name;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
186 break;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
187 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
188 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
189 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
190 valid = getList(origin, stanza, name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
191 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
192
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
193 if valid == false then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
194 origin.send(st.error_reply(stanza, "modify", "bad-request"));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
195 else
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
196 datamanager.store(origin.username, origin.host, "privacy", privacy_lists);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
197 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
198 return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
199 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
200 return false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
201 end, 500);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
202
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
203 function checkIfNeedToBeBlocked(e)
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
204 return false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
205 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
206
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
207 module:hook("pre-message/full", checkIfNeedToBeBlocked, 500);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
208 module:hook("pre-iq/bare", checkIfNeedToBeBlocked, 500);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
209 module:hook("pre-presence/bare", checkIfNeedToBeBlocked, 500);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
210
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
211 -- helpers.log_events(hosts["albastru.de"].events, "albastru.de");
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
212 -- helpers.log_events(prosody.events, "*");
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
213
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
214 module:log("info", "mod_privacy loaded ...");