mod_blocklist: Clear second level cache when user is deleted
[prosody.git] / plugins / mod_blocklist.lua
1 -- Prosody IM
2 -- Copyright (C) 2009-2010 Matthew Wild
3 -- Copyright (C) 2009-2010 Waqas Hussain
4 -- Copyright (C) 2014 Kim Alvefur
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9 -- This module implements XEP-0191: Blocking Command
10 --
11
12 local user_exists = require"core.usermanager".user_exists;
13 local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed;
14 local st = require"util.stanza";
15 local st_error_reply = st.error_reply;
16 local jid_prep = require"util.jid".prep;
17 local jid_split = require"util.jid".split;
18
19 local storage = module:open_store();
20 local sessions = prosody.hosts[module.host].sessions;
21
22 -- Cache of blocklists by username may randomly expire at any time
23 local cache = setmetatable({}, { __mode = "v" });
24
25 -- Second level of caching, keeps a fixed number of items,
26 -- also anchors items in the above cache
27 local cache_size = module:get_option_number("blocklist_cache_size", 64);
28 local cache2 = require"util.cache".new(cache_size);
29
30 local null_blocklist = {};
31
32 module:add_feature("urn:xmpp:blocking");
33
34 local function set_blocklist(username, blocklist)
35         local ok, err = storage:set(username, blocklist);
36         if not ok then
37                 return ok, err;
38         end
39         -- Successful save, update the cache
40         cache2:set(username, blocklist);
41         cache[username] = blocklist;
42         return true;
43 end
44
45 -- Migrates from the old mod_privacy storage
46 local function migrate_privacy_list(username)
47         local migrated_data = { [false] = "not empty" };
48         local legacy_data = module:open_store("privacy"):get(username);
49         if legacy_data and legacy_data.lists and legacy_data.default then
50                 legacy_data = legacy_data.lists[legacy_data.default];
51                 legacy_data = legacy_data and legacy_data.items;
52         else
53                 return migrated_data;
54         end
55         if legacy_data then
56                 module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username);
57                 local item, jid;
58                 for i = 1, #legacy_data do
59                         item = legacy_data[i];
60                         if item.type == "jid" and item.action == "deny" then
61                                 jid = jid_prep(item.value);
62                                 if not jid then
63                                         module:log("warn", "Invalid JID in privacy store for user '%s' not migrated: %s", username, tostring(item.value));
64                                 else
65                                         migrated_data[jid] = true;
66                                 end
67                         end
68                 end
69         end
70         set_blocklist(username, migrated_data);
71         return migrated_data;
72 end
73
74 local function get_blocklist(username)
75         local blocklist = cache[username];
76         if not blocklist then
77                 blocklist = cache2:get(username);
78         end
79         if not blocklist then
80                 if not user_exists(username, module.host) then
81                         return null_blocklist;
82                 end
83                 blocklist = storage:get(username);
84                 if not blocklist then
85                         blocklist = migrate_privacy_list(username);
86                 end
87                 cache2:set(username, blocklist);
88         end
89         cache[username] = blocklist;
90         return blocklist;
91 end
92
93 module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event)
94         local origin, stanza = event.origin, event.stanza;
95         local username = origin.username;
96         local reply = st.reply(stanza):tag("blocklist", { xmlns = "urn:xmpp:blocking" });
97         local blocklist = get_blocklist(username);
98         for jid in pairs(blocklist) do
99                 if jid then
100                         reply:tag("item", { jid = jid }):up();
101                 end
102         end
103         origin.interested_blocklist = true; -- Gets notified about changes
104         origin.send(reply);
105         return true;
106 end);
107
108 -- Add or remove some jid(s) from the blocklist
109 -- We want this to be atomic and not do a partial update
110 local function edit_blocklist(event)
111         local origin, stanza = event.origin, event.stanza;
112         local username = origin.username;
113         local action = stanza.tags[1];
114         local new = {};
115
116         for item in action:childtags("item") do
117                 local jid = jid_prep(item.attr.jid);
118                 if not jid then
119                         origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
120                         return true;
121                 end
122                 item.attr.jid = jid; -- echo back prepped
123                 new[jid] = is_contact_subscribed(username, module.host, jid) or false;
124         end
125
126         local mode = action.name == "block" or nil;
127
128         if mode and not next(new) then
129                 -- <block/> element does not contain at least one <item/> child element
130                 origin.send(st_error_reply(stanza, "modify", "bad-request"));
131                 return true;
132         end
133
134         local blocklist = get_blocklist(username);
135
136         local new_blocklist = {};
137
138         if mode or next(new) then
139                 for jid in pairs(blocklist) do
140                         new_blocklist[jid] = true;
141                 end
142                 for jid in pairs(new) do
143                         new_blocklist[jid] = mode;
144                 end
145                 -- else empty the blocklist
146         end
147         new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice
148
149         local ok, err = set_blocklist(username, new_blocklist);
150         if ok then
151                 origin.send(st.reply(stanza));
152         else
153                 origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
154                 return true;
155         end
156
157         if mode then
158                 for jid, in_roster in pairs(new) do
159                         if not blocklist[jid] and in_roster and sessions[username] then
160                                 for _, session in pairs(sessions[username].sessions) do
161                                         if session.presence then
162                                                 module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
163                                         end
164                                 end
165                         end
166                 end
167         end
168         if sessions[username] then
169                 local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
170                         :add_child(action); -- I am lazy
171
172                 for _, session in pairs(sessions[username].sessions) do
173                         if session.interested_blocklist then
174                                 blocklist_push.attr.to = session.full_jid;
175                                 session.send(blocklist_push);
176                         end
177                 end
178         end
179
180         return true;
181 end
182
183 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist);
184 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
185
186 -- Cache invalidation, solved!
187 module:hook_global("user-deleted", function (event)
188         if event.host == module.host then
189                 cache:set(event.username, nil);
190                 cache[event.username] = nil;
191         end
192 end);
193
194 -- Buggy clients
195 module:hook("iq-error/self/blocklist-push", function (event)
196         local _, condition, text = event.stanza:get_error();
197         (event.origin.log or module._log)("warn", "Client returned an error in response to notification from mod_%s: %s%s%s", module.name, condition, text and ": " or "", text or "");
198         return true;
199 end);
200
201 local function is_blocked(user, jid)
202         local blocklist = cache[user] or get_blocklist(user);
203         if blocklist[jid] then return true; end
204         local node, host = jid_split(jid);
205         return blocklist[host] or node and blocklist[node..'@'..host];
206 end
207
208 -- Event handlers for bouncing or dropping stanzas
209 local function drop_stanza(event)
210         local stanza = event.stanza;
211         local attr = stanza.attr;
212         local to, from = attr.to, attr.from;
213         to = to and jid_split(to);
214         if to and from then
215                 return is_blocked(to, from);
216         end
217 end
218
219 local function bounce_stanza(event)
220         local origin, stanza = event.origin, event.stanza;
221         if drop_stanza(event) then
222                 origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
223                 return true;
224         end
225 end
226
227 local function bounce_iq(event)
228         local type = event.stanza.attr.type;
229         if type == "set" or type == "get" then
230                 return bounce_stanza(event);
231         end
232         return drop_stanza(event); -- result or error
233 end
234
235 local function bounce_message(event)
236         local type = event.stanza.attr.type;
237         if type == "chat" or not type or type == "normal" then
238                 return bounce_stanza(event);
239         end
240         return drop_stanza(event); -- drop headlines, groupchats etc
241 end
242
243 local function drop_outgoing(event)
244         local origin, stanza = event.origin, event.stanza;
245         local username = origin.username or jid_split(stanza.attr.from);
246         if not username then return end
247         local to = stanza.attr.to;
248         if to then return is_blocked(username, to); end
249         -- nil 'to' means a self event, don't bock those
250 end
251
252 local function bounce_outgoing(event)
253         local origin, stanza = event.origin, event.stanza;
254         local type = stanza.attr.type;
255         if type == "error" or stanza.name == "iq" and type == "result" then
256                 return drop_outgoing(event);
257         end
258         if drop_outgoing(event) then
259                 origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
260                         :tag("blocked", { xmlns = "urn:xmpp:blocking:errors" }));
261                 return true;
262         end
263 end
264
265 -- Hook all the events!
266 local prio_in, prio_out = 100, 100;
267 module:hook("presence/bare", drop_stanza, prio_in);
268 module:hook("presence/full", drop_stanza, prio_in);
269
270 module:hook("message/bare", bounce_message, prio_in);
271 module:hook("message/full", bounce_message, prio_in);
272
273 module:hook("iq/bare", bounce_iq, prio_in);
274 module:hook("iq/full", bounce_iq, prio_in);
275
276 module:hook("pre-message/bare", bounce_outgoing, prio_out);
277 module:hook("pre-message/full", bounce_outgoing, prio_out);
278 module:hook("pre-message/host", bounce_outgoing, prio_out);
279
280 module:hook("pre-presence/bare", drop_outgoing, prio_out);
281 module:hook("pre-presence/full", drop_outgoing, prio_out);
282 module:hook("pre-presence/host", drop_outgoing, prio_out);
283
284 module:hook("pre-iq/bare", bounce_outgoing, prio_out);
285 module:hook("pre-iq/full", bounce_outgoing, prio_out);
286 module:hook("pre-iq/host", bounce_outgoing, prio_out);
287