annotate mod_privacy/mod_privacy.lua @ 20:2675dc25445b

mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
author Thilo Cestonaro <thilo@cestona.ro>
date Wed, 30 Sep 2009 21:35:29 +0200
parents e400ee8471b0
children d91cb13ef0ee
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 local prosody = prosody;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
11 local st = require "util.stanza";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
12 local datamanager = require "util.datamanager";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
13 local bare_sessions = bare_sessions;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
14 local util_Jid = require "util.jid";
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
15 local jid_bare = util_Jid.bare;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
16 local jid_split = util_Jid.split;
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
17 local load_roster = require "core.rostermanager".load_roster;
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
18 local to_number = _G.tonumber;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
19
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
20 function findNamedList (privacy_lists, name)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
21 local ret = nil
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
22 if privacy_lists.lists == nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
23 module:log("debug", "no lists loaded.")
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
24 return nil;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
25 end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
26
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
27 module:log("debug", "searching for list: %s", name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
28 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
29 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
30 ret = i;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
31 break;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
32 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
33 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
34 return ret;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
35 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
36
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
37 function isListUsed(origin, name, privacy_lists)
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
38 if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
39 for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
40 if resource ~= origin.resource then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
41 if session.activePrivacyList == name then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
42 module:log("debug", "List {0} is in use.", name);
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
43 return true;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
44 elseif session.activePrivacyList == nil and privacy_lists.default == name then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
45 module:log("debug", "List {0} is in use.", name);
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
46 return true;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
47 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
48 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
49 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
50 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
51 module:log("debug", "List {0} is in NOT use.", name);
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
52 return false;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
53 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
54
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
55 function isAnotherSessionUsingDefaultList(origin)
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
56 local ret = false
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
57 if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
58 for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
59 if resource ~= origin.resource and session.activePrivacyList == nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
60 module:log("debug", "Default list is used by another resource.");
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
61 ret = true;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
62 break;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
63 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
64 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
65 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
66 return ret;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
67 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
68
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
69 function declineList (privacy_lists, origin, stanza, which)
8
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 decline the use of privacy list: %s", which);
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
71 if which == "default" then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
72 if isAnotherSessionUsingDefaultList(origin) then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
73 return { "cancel", "conflict", "Another session is online and using the default list."};
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
74 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
75 privacy_lists.default = nil;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
76 origin.send(st.reply(stanza));
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
77 elseif which == "active" then
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
78 origin.activePrivacyList = nil;
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
79 origin.send(st.reply(stanza));
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
80 else
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
81 return {"modify", "bad-request", "Neither default nor active list specifed to decline."};
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
82 end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
83 return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
84 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
85
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
86 function activateList (privacy_lists, origin, stanza, which, name)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
87 module:log("info", "User requests to change the privacy list: %s, to be list named %s", which, name);
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
88 local idx = findNamedList(privacy_lists, name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
89
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
90 if privacy_lists.default == nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
91 privacy_lists.default = "";
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
92 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
93 if origin.activePrivacyList == nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
94 origin.activePrivacyList = "";
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
95 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
96
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
97 if which == "default" and idx ~= nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
98 if isAnotherSessionUsingDefaultList(origin) then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
99 return {"cancel", "conflict", "Another session is online and using the default list."};
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
100 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
101 privacy_lists.default = name;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
102 origin.send(st.reply(stanza));
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
103 elseif which == "active" and idx ~= nil then
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
104 origin.activePrivacyList = name;
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
105 origin.send(st.reply(stanza));
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
106 else
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
107 return {"modify", "bad-request", "Either not active or default given or unknown list name specified."};
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
108 end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
109 return true;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
110 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
111
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
112 function deleteList (privacy_lists, origin, stanza, name)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
113 module:log("info", "User requests to delete privacy list: %s", name);
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
114 local idx = findNamedList(privacy_lists, name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
115
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
116 if idx ~= nil then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
117 if isListUsed(origin, name, privacy_lists) then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
118 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
119 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
120 table.remove(privacy_lists.lists, idx);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
121 origin.send(st.reply(stanza));
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
122 return true;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
123 end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
124 return {"modify", "bad-request", "Not existing list specifed to be deleted."};
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
125 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
126
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
127 local function sortByOrder(a, b)
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
128 if a.order < b.order then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
129 return true;
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
130 end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
131 return false;
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
132 end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
133
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
134 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
135 module:log("info", "User requests to create / replace list named %s, item count: %d", name, #entries);
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
136 local idx = findNamedList(privacy_lists, name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
137 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
138
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
139 if privacy_lists.lists == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
140 privacy_lists.lists = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
141 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
142
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
143 if idx == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
144 idx = #privacy_lists.lists + 1;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
145 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
146
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
147 local orderCheck = {};
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
148 local list = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
149 list.name = name;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
150 list.items = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
151
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
152 for _,item in ipairs(entries) do
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
153 if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
154 return {"modify", "bad-request", "Order attribute not valid."};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
155 end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
156
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
157 if item.attr.type ~= nil and item.attr.type ~= "jid" and item.attr.type ~= "subscription" and item.attr.type ~= "group" then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
158 return {"modify", "bad-request", "Type attribute not valid."};
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
159 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
160
14
0892941186f2 mod_privacy: Make tmp variable a local
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
161 local tmp = {};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
162 orderCheck[item.attr.order] = true;
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
163
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
164 tmp["type"] = item.attr.type;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
165 tmp["value"] = item.attr.value;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
166 tmp["action"] = item.attr.action;
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
167 tmp["order"] = to_number(item.attr.order);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
168 tmp["presence-in"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
169 tmp["presence-out"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
170 tmp["message"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
171 tmp["iq"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
172
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
173 if #item.tags > 0 then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
174 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
175 tmp[tag.name] = true;
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
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
178
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
179 if tmp.type == "group" then
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
180 local found = false;
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
181 local roster = load_roster(origin.username, origin.host);
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
182 local groups = roster.groups;
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
183 if groups == nil then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
184 return {"cancel", "item-not-found", "Specifed roster group not existing."};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
185 end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
186 for _,group in ipairs(groups) do
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
187 if group == tmp.value then
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
188 found = true;
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
189 end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
190 end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
191 if found == false then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
192 return {"cancel", "item-not-found", "Specifed roster group not existing."};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
193 end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
194 elseif tmp.type == "subscription" then
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
195 if tmp.value ~= "both" and
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
196 tmp.value ~= "to" and
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
197 tmp.value ~= "from" and
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
198 tmp.value ~= "none" then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
199 return {"cancel", "bad-request", "Subscription value must be both, to, from or none."};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
200 end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
201 end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
202
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
203 if tmp.action ~= "deny" and tmp.action ~= "allow" then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
204 return {"cancel", "bad-request", "Action must be either deny or allow."};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
205 end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
206
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
207 list.items[#list.items + 1] = tmp;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
208 end
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
209
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
210 table.sort(list, sortByOrder);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
211
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
212 privacy_lists.lists[idx] = list;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
213 origin.send(st.reply(stanza));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
214 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
215 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
216 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
217 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
218 iq:up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
219 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
220 iq.attr.to = bare_jid.."/"..resource
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
221 session.send(iq);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
222 end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
223 else
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
224 return {"cancel", "bad-request", "internal error."};
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
225 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
226 return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
227 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
228
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
229 function getList(privacy_lists, origin, stanza, name)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
230 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
231 local reply = st.reply(stanza);
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
232 reply:tag("query", {xmlns="jabber:iq:privacy"});
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
233
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
234 if name == nil then
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
235 reply:tag("active", {name=origin.activePrivacyList or ""}):up();
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
236 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
237 if privacy_lists.lists then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
238 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
239 reply:tag("list", {name=list.name}):up();
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
240 end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
241 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
242 else
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
243 local idx = findNamedList(privacy_lists, name);
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
244 module:log("debug", "list idx: %d", idx or -1);
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
245 if idx ~= nil then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
246 list = privacy_lists.lists[idx];
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
247 reply = reply:tag("list", {name=list.name});
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
248 for _,item in ipairs(list.items) do
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
249 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
250 if item["message"] then reply:tag("message"):up(); end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
251 if item["iq"] then reply:tag("iq"):up(); end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
252 if item["presence-in"] then reply:tag("presence-in"):up(); end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
253 if item["presence-out"] then reply:tag("presence-out"):up(); end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
254 reply:up();
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
255 end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
256 else
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
257 return {"cancel", "item-not-found", "Unknown list specified."};
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
258 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
259 end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
260
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
261 origin.send(reply);
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
262 return true;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
263 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
264
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
265 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
266 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
267
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
268 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
269 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
270 local valid = false;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
271 local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or {};
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
272
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
273 if stanza.attr.type == "set" then
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
274 if #query.tags == 1 then -- the <query/> element MUST NOT include more than one child element
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
275 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
276 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
277 if tag.attr.name == nil then -- Client declines the use of active / default list
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
278 valid = declineList(privacy_lists, origin, stanza, tag.name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
279 else -- Client requests change of active / default list
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
280 valid = activateList(privacy_lists, origin, stanza, tag.name, tag.attr.name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
281 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
282 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
283 if #tag.tags == 0 then -- Client removes a privacy list
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
284 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
285 else -- Client edits a privacy list
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
286 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags); -- TODO check if used!
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
287 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
288 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
289 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
290 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
291 elseif stanza.attr.type == "get" then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
292 local name = nil;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
293 local listsToRetrieve = 0;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
294 if #query.tags >= 1 then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
295 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
296 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
297 name = tag.attr.name;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
298 listsToRetrieve = listsToRetrieve + 1;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
299 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
300 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
301 end
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
302 if listsToRetrieve == 0 or listsToRetrieve == 1 then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
303 valid = getList(privacy_lists, origin, stanza, name);
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
304 end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
305 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
306
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
307 if valid ~= true then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
308 if valid[0] == nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
309 valid[0] = "cancel";
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
310 end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
311 if valid[1] == nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
312 valid[1] = "bad-request";
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
313 end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
314 origin.send(st.error_reply(stanza, valid[0], valid[1], valid[2]));
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
315 else
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
316 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
317 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
318 return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
319 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
320 return false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
321 end, 500);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
322
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
323 function checkIfNeedToBeBlocked(e, session)
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
324 local origin, stanza = e.origin, e.stanza;
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
325 local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {};
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
326 local bare_jid = session.username.."@"..session.host;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
327
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
328 module:log("debug", "checkIfNeedToBeBlocked: username: %s, host: %s", session.username, session.host);
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
329 module:log("debug", "stanza: %s, to: %s, form: %s", stanza.name, stanza.attr.to or "nil", stanza.attr.from or "nil");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
330
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
331 if privacy_lists.lists ~= nil and stanza.attr.to ~= nil and stanza.attr.from ~= nil then
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
332 if session.activePrivacyList == nil and privacy_lists.default == nil then
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
333 return; -- Nothing to block, default is Allow all
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
334 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
335
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
336 local idx;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
337 local list;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
338 local item;
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
339 local listname = session.activePrivacyList;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
340 if listname == nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
341 listname = privacy_lists.default; -- no active list selected, use default list
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
342 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
343 idx = findNamedList(privacy_lists, listname);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
344 if idx == nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
345 module:log("info", "given privacy listname not found.");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
346 return;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
347 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
348 list = privacy_lists.lists[idx];
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
349 if list == nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
350 module:log("info", "privacy list index wrong.");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
351 return;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
352 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
353 for _,item in ipairs(list.items) do
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
354 local apply = false;
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
355 local block = false;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
356 if (stanza.name == "message" and item.message) or
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
357 (stanza.name == "iq" and item.iq) or
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
358 (stanza.name == "presence" and jid_bare(stanza.attr.to) == bare_jid and item["presence-in"]) or
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
359 (stanza.name == "presence" and jid_bare(stanza.attr.from) == bare_jid and item["presence-out"]) or
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
360 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-in"] == false) then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
361 module:log("debug", "stanza type matched.");
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
362 apply = true;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
363 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
364 if apply then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
365 local evilJid = {};
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
366 apply = false;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
367 if jid_bare(stanza.attr.to) == bare_jid then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
368 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.from);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
369 else
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
370 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.to);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
371 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
372 if item.type == "jid" and
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
373 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
374 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
375 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
376 (evilJid.host and item.value == evilJid.host) then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
377 module:log("debug", "jid matched.");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
378 apply = true;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
379 block = (item.action == "deny");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
380 elseif item.type == "group" then
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
381 local roster = load_roster(session.username, session.host);
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
382 local groups = roster.groups;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
383 for _,group in ipairs(groups) do
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
384 if group == item.value then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
385 module:log("debug", "group matched.");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
386 apply = true;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
387 block = (item.action == "deny");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
388 break;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
389 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
390 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
391 elseif item.type == "subscription" then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
392 if origin.roster[jid_bare(stanza.from)].subscription == item.value then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
393 module:log("debug", "subscription matched.");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
394 apply = true;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
395 block = (item.action == "deny");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
396 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
397 elseif item.type == nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
398 module:log("debug", "no item.type, so matched.");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
399 apply = true;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
400 block = (item.action == "deny");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
401 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
402 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
403 if apply then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
404 if block then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
405 module:log("info", "stanza blocked: %s, to: %s, from: %s", stanza.name, stanza.attr.to or "nil", stanza.attr.from or "nil");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
406 if stanza.name == "message" then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
407 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
408 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
409 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
410 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
411 return true; -- stanza blocked !
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
412 else
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
413 module:log("info", "stanza explicit allowed!")
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
414 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
415 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
416 end
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
417 end
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
418 return;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
419 end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
420
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
421 function preCheckIncoming(e)
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
422 local session;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
423 if e.stanza.attr.to ~= nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
424 local node, host, resource = jid_split(e.stanza.attr.to);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
425 if node == nil or host == nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
426 return;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
427 end
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
428 if resource == nil then
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
429 local prio = 0;
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
430 local session_;
18
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
431 if bare_sessions[node.."@"..host] ~= nil then
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
432 for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
433 if session_.priority > prio then
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
434 session = session_;
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
435 prio = session_.priority;
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
436 end
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
437 end
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
438 end
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
439 else
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
440 session = full_sessions[node.."@"..host.."/"..resource];
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
441 end
18
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
442 if session ~= nil then
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
443 return checkIfNeedToBeBlocked(e, session);
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
444 else
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
445 module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", node or "nil", host or "nil", resource or "nil")
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
446 end
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
447 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
448 return;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
449 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
450
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
451 function preCheckOutgoing(e)
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
452 local session;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
453 if e.stanza.attr.from ~= nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
454 local node, host, resource = jid_split(e.stanza.attr.from);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
455 if node == nil or host == nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
456 return;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
457 end
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
458 if resource == nil then
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
459 local prio = 0;
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
460 local session_;
18
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
461 if bare_sessions[node.."@"..host] ~= nil then
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
462 for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
463 if session_.priority > prio then
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
464 session = session_;
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
465 prio = session_.priority;
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
466 end
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
467 end
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
468 end
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
469 else
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
470 session = full_sessions[node.."@"..host.."/"..resource];
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
471 end
18
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
472 if session ~= nil then
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
473 return checkIfNeedToBeBlocked(e, session);
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
474 else
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
475 module:log("debug", "preCheckOutgoing: Couldn't get session for jid: %s@%s/%s", node or "nil", host or "nil", resource or "nil")
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
476 end
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
477 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
478 return;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
479 end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
480
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
481
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
482 module:hook("pre-message/full", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
483 module:hook("pre-message/bare", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
484 module:hook("pre-message/host", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
485 module:hook("pre-iq/full", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
486 module:hook("pre-iq/bare", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
487 module:hook("pre-iq/host", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
488 module:hook("pre-presence/full", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
489 module:hook("pre-presence/bare", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
490 module:hook("pre-presence/host", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
491
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
492 module:hook("message/full", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
493 module:hook("message/bare", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
494 module:hook("message/host", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
495 module:hook("iq/full", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
496 module:hook("iq/bare", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
497 module:hook("iq/host", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
498 module:hook("presence/full", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
499 module:hook("presence/bare", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
500 module:hook("presence/host", preCheckIncoming, 500);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
501
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
502 module:log("info", "mod_privacy loaded ...");