annotate mod_privacy_lists/mod_privacy_lists.lua @ 1578:a68ec7a2dc02

mod_http_muc_log: Show only messages with presentable content
author Kim Alvefur <zash@zash.se>
date Thu, 20 Nov 2014 20:01:07 +0100
parents 5410f5c30d63
children c2fc3516cca1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1474
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
1475
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
2 -- Copyright (C) 2009-2014 Matthew Wild
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
3 -- Copyright (C) 2009-2014 Waqas Hussain
1474
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- Copyright (C) 2009 Thilo Cestonaro
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 --
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 --
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 module:add_feature("jabber:iq:privacy");
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local st = require "util.stanza";
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local bare_sessions, full_sessions = prosody.bare_sessions, prosody.full_sessions;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local util_Jid = require "util.jid";
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local jid_bare = util_Jid.bare;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local jid_split, jid_join = util_Jid.split, util_Jid.join;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local load_roster = require "core.rostermanager".load_roster;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local to_number = tonumber;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
1480
92b930be261f mod_privacy_lists: Open 'privacy' storage (module:open_store() defaults to the name of the module)
Kim Alvefur <zash@zash.se>
parents: 1475
diff changeset
20 local privacy_storage = module:open_store("privacy");
1475
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
21 local user_sessions = hosts[module.host].sessions;
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
22
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
23 local function get_lists(username)
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
24 return user_sessions[username].privacy_lists;
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
25 end
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
26
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
27 local function save_lists(username)
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
28 local privacy_lists = user_sessions[username].privacy_lists;
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
29 if privacy_lists.default == nil and next(privacy_lists.lists) == nil then
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
30 privacy_lists = nil;
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
31 end
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
32 return privacy_storage:set(username, privacy_lists);
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
33 end
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
34
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
35 module:hook("resource-bind", function (event)
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
36 local username = event.session.username;
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
37 user_sessions[username].privacy_lists = privacy_storage:get(username) or { lists = {} };
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
38 end);
1474
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 function isListUsed(origin, name, privacy_lists)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local user = bare_sessions[origin.username.."@"..origin.host];
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if user then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 for resource, session in pairs(user.sessions) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if resource ~= origin.resource then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if session.activePrivacyList == name then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 return true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 elseif session.activePrivacyList == nil and privacy_lists.default == name then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 function isAnotherSessionUsingDefaultList(origin)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local user = bare_sessions[origin.username.."@"..origin.host];
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if user then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 for resource, session in pairs(user.sessions) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 if resource ~= origin.resource and session.activePrivacyList == nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 function declineList(privacy_lists, origin, stanza, which)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if which == "default" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 if isAnotherSessionUsingDefaultList(origin) then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 return { "cancel", "conflict", "Another session is online and using the default list."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 privacy_lists.default = nil;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 origin.send(st.reply(stanza));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 elseif which == "active" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 origin.activePrivacyList = nil;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 origin.send(st.reply(stanza));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 else
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 return {"modify", "bad-request", "Neither default nor active list specifed to decline."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 return true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 function activateList(privacy_lists, origin, stanza, which, name)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local list = privacy_lists.lists[name];
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 if which == "default" and list then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 if isAnotherSessionUsingDefaultList(origin) then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 return {"cancel", "conflict", "Another session is online and using the default list."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 privacy_lists.default = name;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 origin.send(st.reply(stanza));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 elseif which == "active" and list then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 origin.activePrivacyList = name;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 origin.send(st.reply(stanza));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 elseif not list then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 return {"cancel", "item-not-found", "No such list: "..name};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 else
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 return {"modify", "bad-request", "No list chosen to be active or default."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 return true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 function deleteList(privacy_lists, origin, stanza, name)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 local list = privacy_lists.lists[name];
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 if list then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 if isListUsed(origin, name, privacy_lists) then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 if privacy_lists.default == name then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 privacy_lists.default = nil;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 if origin.activePrivacyList == name then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 origin.activePrivacyList = nil;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 privacy_lists.lists[name] = nil;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 origin.send(st.reply(stanza));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 return true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 return {"modify", "bad-request", "Not existing list specifed to be deleted."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 function createOrReplaceList (privacy_lists, origin, stanza, name, entries)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 local bare_jid = origin.username.."@"..origin.host;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 if privacy_lists.lists == nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 privacy_lists.lists = {};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 local list = {};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 privacy_lists.lists[name] = list;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local orderCheck = {};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 list.name = name;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 list.items = {};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 for _,item in ipairs(entries) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 return {"modify", "bad-request", "Order attribute not valid."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 if item.attr.type ~= nil and item.attr.type ~= "jid" and item.attr.type ~= "subscription" and item.attr.type ~= "group" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 return {"modify", "bad-request", "Type attribute not valid."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 local tmp = {};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 orderCheck[item.attr.order] = true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 tmp["type"] = item.attr.type;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 tmp["value"] = item.attr.value;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 tmp["action"] = item.attr.action;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 tmp["order"] = to_number(item.attr.order);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 tmp["presence-in"] = false;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 tmp["presence-out"] = false;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 tmp["message"] = false;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 tmp["iq"] = false;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 if #item.tags > 0 then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 for _,tag in ipairs(item.tags) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 tmp[tag.name] = true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 if tmp.type == "subscription" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 if tmp.value ~= "both" and
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 tmp.value ~= "to" and
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 tmp.value ~= "from" and
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 tmp.value ~= "none" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 return {"cancel", "bad-request", "Subscription value must be both, to, from or none."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 if tmp.action ~= "deny" and tmp.action ~= "allow" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 return {"cancel", "bad-request", "Action must be either deny or allow."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 list.items[#list.items + 1] = tmp;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
1487
5410f5c30d63 mod_privacy_lists: Correctly sort the list rules by order (thanks Flow). Fixes issue #58
Matthew Wild <mwild1@gmail.com>
parents: 1480
diff changeset
178 table.sort(list.items, function(a, b) return a.order < b.order; end);
1474
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 origin.send(st.reply(stanza));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 if bare_sessions[bare_jid] ~= nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 local iq = st.iq ( { type = "set", id="push1" } );
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 iq:tag ("query", { xmlns = "jabber:iq:privacy" } );
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 iq:tag ("list", { name = list.name } ):up();
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 iq:up();
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 for resource, session in pairs(bare_sessions[bare_jid].sessions) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 iq.attr.to = bare_jid.."/"..resource
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 session.send(iq);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 else
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 return {"cancel", "bad-request", "internal error."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 return true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 function getList(privacy_lists, origin, stanza, name)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 local reply = st.reply(stanza);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 reply:tag("query", {xmlns="jabber:iq:privacy"});
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 if name == nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 if privacy_lists.lists then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 if origin.activePrivacyList then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 reply:tag("active", {name=origin.activePrivacyList}):up();
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 if privacy_lists.default then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 reply:tag("default", {name=privacy_lists.default}):up();
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 for name,list in pairs(privacy_lists.lists) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 reply:tag("list", {name=name}):up();
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 else
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 local list = privacy_lists.lists[name];
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 if list then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 reply = reply:tag("list", {name=list.name});
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 for _,item in ipairs(list.items) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 if item["message"] then reply:tag("message"):up(); end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 if item["iq"] then reply:tag("iq"):up(); end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 if item["presence-in"] then reply:tag("presence-in"):up(); end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 if item["presence-out"] then reply:tag("presence-out"):up(); end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 reply:up();
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 else
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 return {"cancel", "item-not-found", "Unknown list specified."};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 origin.send(reply);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 return true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 module:hook("iq/bare/jabber:iq:privacy:query", function(data)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 local origin, stanza = data.origin, data.stanza;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 if stanza.attr.to == nil then -- only service requests to own bare JID
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 local query = stanza.tags[1]; -- the query element
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 local valid = false;
1475
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
239 local privacy_lists = get_lists(origin.username);
1474
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 if privacy_lists.lists[1] then -- Code to migrate from old privacy lists format, remove in 0.8
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 module:log("info", "Upgrading format of stored privacy lists for %s@%s", origin.username, origin.host);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 local lists = privacy_lists.lists;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 for idx, list in ipairs(lists) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 lists[list.name] = list;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 lists[idx] = nil;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 if stanza.attr.type == "set" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 if #query.tags == 1 then -- the <query/> element MUST NOT include more than one child element
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 for _,tag in ipairs(query.tags) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 if tag.name == "active" or tag.name == "default" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 if tag.attr.name == nil then -- Client declines the use of active / default list
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 valid = declineList(privacy_lists, origin, stanza, tag.name);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 else -- Client requests change of active / default list
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 valid = activateList(privacy_lists, origin, stanza, tag.name, tag.attr.name);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 if #tag.tags == 0 then -- Client removes a privacy list
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 else -- Client edits a privacy list
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 elseif stanza.attr.type == "get" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 local name = nil;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 local listsToRetrieve = 0;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 if #query.tags >= 1 then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 for _,tag in ipairs(query.tags) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 if tag.name == "list" then -- Client requests a privacy list from server
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 name = tag.attr.name;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 listsToRetrieve = listsToRetrieve + 1;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 if listsToRetrieve == 0 or listsToRetrieve == 1 then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 valid = getList(privacy_lists, origin, stanza, name);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 if valid ~= true then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 valid = valid or { "cancel", "bad-request", "Couldn't understand request" };
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 if valid[1] == nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 valid[1] = "cancel";
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 if valid[2] == nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 valid[2] = "bad-request";
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 origin.send(st.error_reply(stanza, valid[1], valid[2], valid[3]));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 else
1475
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
294 save_lists(origin.username);
1474
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 return true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 end);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 function checkIfNeedToBeBlocked(e, session)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 local origin, stanza = e.origin, e.stanza;
1475
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
302 local user = user_sessions[session.username];
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
303 local privacy_lists = user and user.privacy_lists;
1474
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 local bare_jid = session.username.."@"..session.host;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 local to = stanza.attr.to or bare_jid;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 local from = stanza.attr.from;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 local is_to_user = bare_jid == jid_bare(to);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 local is_from_user = bare_jid == jid_bare(from);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 --module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312
1475
58d48afca54d mod_privacy_lists: Cache privacy lists for better performance with high traffic
Matthew Wild <mwild1@gmail.com>
parents: 1474
diff changeset
313 if not privacy_lists or privacy_lists.lists == nil or
1474
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 not (session.activePrivacyList or privacy_lists.default)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 return; -- Nothing to block, default is Allow all
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 if is_from_user and is_to_user then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 --module:log("debug", "Not blocking communications between user's resources");
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 return; -- from one of a user's resource to another => HANDS OFF!
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 local listname = session.activePrivacyList;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 if listname == nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 listname = privacy_lists.default; -- no active list selected, use default list
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 local list = privacy_lists.lists[listname];
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 if not list then -- should never happen
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 module:log("warn", "given privacy list not found. name: %s for user %s", listname, bare_jid);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 return;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 for _,item in ipairs(list.items) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 local apply = false;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 local block = false;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 if (
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 (stanza.name == "message" and item.message) or
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 (stanza.name == "iq" and item.iq) or
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 (stanza.name == "presence" and is_to_user and item["presence-in"]) or
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 (stanza.name == "presence" and is_from_user and item["presence-out"]) or
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-out"] == false)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 ) then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 apply = true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 if apply then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 local evilJid = {};
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 apply = false;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 if is_to_user then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 --module:log("debug", "evil jid is (from): %s", from);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 evilJid.node, evilJid.host, evilJid.resource = jid_split(from);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 else
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 --module:log("debug", "evil jid is (to): %s", to);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 evilJid.node, evilJid.host, evilJid.resource = jid_split(to);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 if item.type == "jid" and
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 (evilJid.host and item.value == evilJid.host) then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 apply = true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 block = (item.action == "deny");
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 elseif item.type == "group" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 local roster = load_roster(session.username, session.host);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 local roster_entry = roster[jid_join(evilJid.node, evilJid.host)];
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 if roster_entry then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 local groups = roster_entry.groups;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 for group in pairs(groups) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 if group == item.value then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 apply = true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 block = (item.action == "deny");
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 break;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374 elseif item.type == "subscription" then -- we need a valid bare evil jid
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375 local roster = load_roster(session.username, session.host);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 local roster_entry = roster[jid_join(evilJid.node, evilJid.host)];
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 if (not(roster_entry) and item.value == "none")
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 or (roster_entry and roster_entry.subscription == item.value) then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 apply = true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 block = (item.action == "deny");
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 elseif item.type == nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 apply = true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 block = (item.action == "deny");
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 if apply then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 if block then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389 -- drop and not bounce groupchat messages, otherwise users will get kicked
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390 if stanza.attr.type == "groupchat" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 return true;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393 module:log("debug", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 if stanza.name == "message" then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 return true; -- stanza blocked !
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 else
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401 --module:log("debug", "stanza explicitly allowed!")
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 return;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 function preCheckIncoming(e)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409 local session;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
410 if e.stanza.attr.to ~= nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
411 local node, host, resource = jid_split(e.stanza.attr.to);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
412 if node == nil or host == nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
413 return;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
414 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
415 if resource == nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416 local prio = 0;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 if bare_sessions[node.."@"..host] ~= nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
418 for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
419 if session_.priority ~= nil and session_.priority > prio then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
420 session = session_;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421 prio = session_.priority;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
422 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
424 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
425 else
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
426 session = full_sessions[node.."@"..host.."/"..resource];
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
427 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
428 if session ~= nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
429 return checkIfNeedToBeBlocked(e, session);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
430 else
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
431 --module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource));
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
432 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
433 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
434 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
435
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
436 function preCheckOutgoing(e)
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
437 local session = e.origin;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
438 if e.stanza.attr.from == nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
439 e.stanza.attr.from = session.username .. "@" .. session.host;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
440 if session.resource ~= nil then
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
441 e.stanza.attr.from = e.stanza.attr.from .. "/" .. session.resource;
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
442 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
443 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
444 if session.username then -- FIXME do properly
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
445 return checkIfNeedToBeBlocked(e, session);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
446 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
447 end
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
448
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
449 module:hook("pre-message/full", preCheckOutgoing, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450 module:hook("pre-message/bare", preCheckOutgoing, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
451 module:hook("pre-message/host", preCheckOutgoing, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 module:hook("pre-iq/full", preCheckOutgoing, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
453 module:hook("pre-iq/bare", preCheckOutgoing, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454 module:hook("pre-iq/host", preCheckOutgoing, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
455 module:hook("pre-presence/full", preCheckOutgoing, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
456 module:hook("pre-presence/bare", preCheckOutgoing, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457 module:hook("pre-presence/host", preCheckOutgoing, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
459 module:hook("message/full", preCheckIncoming, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460 module:hook("message/bare", preCheckIncoming, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461 module:hook("message/host", preCheckIncoming, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462 module:hook("iq/full", preCheckIncoming, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 module:hook("iq/bare", preCheckIncoming, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
464 module:hook("iq/host", preCheckIncoming, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
465 module:hook("presence/full", preCheckIncoming, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
466 module:hook("presence/bare", preCheckIncoming, 500);
d4233dce479f mod_privacy_lists: Import mod_privacy from 0.9
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
467 module:hook("presence/host", preCheckIncoming, 500);