mod_blocklist: When blocking someone who sent a subscription request, forget that...
[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 rostermanager = require"core.rostermanager";
14 local is_contact_subscribed = rostermanager.is_contact_subscribed;
15 local is_contact_pending_in = rostermanager.is_contact_pending_in;
16 local load_roster = rostermanager.load_roster;
17 local save_roster = rostermanager.save_roster;
18 local st = require"util.stanza";
19 local st_error_reply = st.error_reply;
20 local jid_prep = require"util.jid".prep;
21 local jid_split = require"util.jid".split;
22
23 local storage = module:open_store();
24 local sessions = prosody.hosts[module.host].sessions;
25
26 -- First level cache of blocklists by username.
27 -- Weak table so may randomly expire at any time.
28 local cache = setmetatable({}, { __mode = "v" });
29
30 -- Second level of caching, keeps a fixed number of items, also anchors
31 -- items in the above cache.
32 --
33 -- The size of this affects how often we will need to load a blocklist from
34 -- disk, which we want to avoid during routing. On the other hand, we don't
35 -- want to use too much memory either, so this can be tuned by advanced
36 -- users. TODO use science to figure out a better default, 64 is just a guess.
37 local cache_size = module:get_option_number("blocklist_cache_size", 64);
38 local cache2 = require"util.cache".new(cache_size);
39
40 local null_blocklist = {};
41
42 module:add_feature("urn:xmpp:blocking");
43
44 local function set_blocklist(username, blocklist)
45         local ok, err = storage:set(username, blocklist);
46         if not ok then
47                 return ok, err;
48         end
49         -- Successful save, update the cache
50         cache2:set(username, blocklist);
51         cache[username] = blocklist;
52         return true;
53 end
54
55 -- Migrates from the old mod_privacy storage
56 local function migrate_privacy_list(username)
57         local migrated_data = { [false] = "not empty" };
58         local legacy_data = module:open_store("privacy"):get(username);
59         if legacy_data and legacy_data.lists and legacy_data.default then
60                 legacy_data = legacy_data.lists[legacy_data.default];
61                 legacy_data = legacy_data and legacy_data.items;
62         else
63                 return migrated_data;
64         end
65         if legacy_data then
66                 module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username);
67                 local item, jid;
68                 for i = 1, #legacy_data do
69                         item = legacy_data[i];
70                         if item.type == "jid" and item.action == "deny" then
71                                 jid = jid_prep(item.value);
72                                 if not jid then
73                                         module:log("warn", "Invalid JID in privacy store for user '%s' not migrated: %s", username, tostring(item.value));
74                                 else
75                                         migrated_data[jid] = true;
76                                 end
77                         end
78                 end
79         end
80         set_blocklist(username, migrated_data);
81         return migrated_data;
82 end
83
84 local function get_blocklist(username)
85         local blocklist = cache[username];
86         if not blocklist then
87                 blocklist = cache2:get(username);
88         end
89         if not blocklist then
90                 if not user_exists(username, module.host) then
91                         return null_blocklist;
92                 end
93                 blocklist = storage:get(username);
94                 if not blocklist then
95                         blocklist = migrate_privacy_list(username);
96                 end
97                 cache2:set(username, blocklist);
98         end
99         cache[username] = blocklist;
100         return blocklist;
101 end
102
103 module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event)
104         local origin, stanza = event.origin, event.stanza;
105         local username = origin.username;
106         local reply = st.reply(stanza):tag("blocklist", { xmlns = "urn:xmpp:blocking" });
107         local blocklist = get_blocklist(username);
108         for jid in pairs(blocklist) do
109                 if jid then
110                         reply:tag("item", { jid = jid }):up();
111                 end
112         end
113         origin.interested_blocklist = true; -- Gets notified about changes
114         origin.send(reply);
115         return true;
116 end);
117
118 -- Add or remove some jid(s) from the blocklist
119 -- We want this to be atomic and not do a partial update
120 local function edit_blocklist(event)
121         local origin, stanza = event.origin, event.stanza;
122         local username = origin.username;
123         local action = stanza.tags[1]; -- "block" or "unblock"
124         local new = {}; -- JIDs to block depending or unblock on action
125
126         -- XEP-0191 sayeth:
127         -- > When the user blocks communications with the contact, the user's
128         -- > server MUST send unavailable presence information to the contact (but
129         -- > only if the contact is allowed to receive presence notifications [...]
130         -- So contacts we need to do that for are added to the set below.
131         local send_unavailable = {};
132
133         -- Because blocking someone currently also blocks the ability to reject
134         -- subscription requests, we'll preemptively reject such
135         local remove_pending = {};
136
137         for item in action:childtags("item") do
138                 local jid = jid_prep(item.attr.jid);
139                 if not jid then
140                         origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
141                         return true;
142                 end
143                 item.attr.jid = jid; -- echo back prepped
144                 new[jid] = true;
145                 if is_contact_subscribed(username, module.host, jid) then
146                         send_unavailable[jid] = true;
147                 elseif is_contact_pending_in(username, module.host, jid) then
148                         remove_pending[jid] = true;
149                 end
150         end
151
152         local is_blocking = action.name == "block" or nil; -- nil if unblocking
153
154         if is_blocking and not next(new) then
155                 -- <block/> element does not contain at least one <item/> child element
156                 origin.send(st_error_reply(stanza, "modify", "bad-request"));
157                 return true;
158         end
159
160         local blocklist = get_blocklist(username);
161
162         local new_blocklist = {};
163
164         if is_blocking or next(new) then
165                 for jid in pairs(blocklist) do
166                         new_blocklist[jid] = true;
167                 end
168                 for jid in pairs(new) do
169                         new_blocklist[jid] = is_blocking;
170                 end
171                 -- else empty the blocklist
172         end
173         new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice
174
175         local ok, err = set_blocklist(username, new_blocklist);
176         if ok then
177                 origin.send(st.reply(stanza));
178         else
179                 origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
180                 return true;
181         end
182
183         if is_blocking then
184                 for jid in pairs(send_unavailable) do
185                         if not blocklist[jid] then
186                                 for _, session in pairs(sessions[username].sessions) do
187                                         if session.presence then
188                                                 module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
189                                         end
190                                 end
191                         end
192                 end
193
194                 if next(remove_pending) then
195                         local roster = load_roster(username, module.host);
196                         for jid in pairs(remove_pending) do
197                                 roster[false].pending[jid] = nil;
198                         end
199                         save_roster(username, module.host, roster);
200                         -- Not much we can do about save failing here
201                 end
202         end
203
204         local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
205                 :add_child(action); -- I am lazy
206
207         for _, session in pairs(sessions[username].sessions) do
208                 if session.interested_blocklist then
209                         blocklist_push.attr.to = session.full_jid;
210                         session.send(blocklist_push);
211                 end
212         end
213
214         return true;
215 end
216
217 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist);
218 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
219
220 -- Cache invalidation, solved!
221 module:hook_global("user-deleted", function (event)
222         if event.host == module.host then
223                 cache:set(event.username, nil);
224                 cache[event.username] = nil;
225         end
226 end);
227
228 -- Buggy clients
229 module:hook("iq-error/self/blocklist-push", function (event)
230         local _, condition, text = event.stanza:get_error();
231         (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 "");
232         return true;
233 end);
234
235 local function is_blocked(user, jid)
236         local blocklist = cache[user] or get_blocklist(user);
237         if blocklist[jid] then return true; end
238         local node, host = jid_split(jid);
239         return blocklist[host] or node and blocklist[node..'@'..host];
240 end
241
242 -- Event handlers for bouncing or dropping stanzas
243 local function drop_stanza(event)
244         local stanza = event.stanza;
245         local attr = stanza.attr;
246         local to, from = attr.to, attr.from;
247         to = to and jid_split(to);
248         if to and from then
249                 return is_blocked(to, from);
250         end
251 end
252
253 local function bounce_stanza(event)
254         local origin, stanza = event.origin, event.stanza;
255         if drop_stanza(event) then
256                 origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
257                 return true;
258         end
259 end
260
261 local function bounce_iq(event)
262         local type = event.stanza.attr.type;
263         if type == "set" or type == "get" then
264                 return bounce_stanza(event);
265         end
266         return drop_stanza(event); -- result or error
267 end
268
269 local function bounce_message(event)
270         local type = event.stanza.attr.type;
271         if type == "chat" or not type or type == "normal" then
272                 return bounce_stanza(event);
273         end
274         return drop_stanza(event); -- drop headlines, groupchats etc
275 end
276
277 local function drop_outgoing(event)
278         local origin, stanza = event.origin, event.stanza;
279         local username = origin.username or jid_split(stanza.attr.from);
280         if not username then return end
281         local to = stanza.attr.to;
282         if to then return is_blocked(username, to); end
283         -- nil 'to' means a self event, don't bock those
284 end
285
286 local function bounce_outgoing(event)
287         local origin, stanza = event.origin, event.stanza;
288         local type = stanza.attr.type;
289         if type == "error" or stanza.name == "iq" and type == "result" then
290                 return drop_outgoing(event);
291         end
292         if drop_outgoing(event) then
293                 origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
294                         :tag("blocked", { xmlns = "urn:xmpp:blocking:errors" }));
295                 return true;
296         end
297 end
298
299 -- Hook all the events!
300 local prio_in, prio_out = 100, 100;
301 module:hook("presence/bare", drop_stanza, prio_in);
302 module:hook("presence/full", drop_stanza, prio_in);
303
304 module:hook("message/bare", bounce_message, prio_in);
305 module:hook("message/full", bounce_message, prio_in);
306
307 module:hook("iq/bare", bounce_iq, prio_in);
308 module:hook("iq/full", bounce_iq, prio_in);
309
310 module:hook("pre-message/bare", bounce_outgoing, prio_out);
311 module:hook("pre-message/full", bounce_outgoing, prio_out);
312 module:hook("pre-message/host", bounce_outgoing, prio_out);
313
314 -- Note: MUST bounce these, but we don't because this would produce
315 -- lots of error replies due to server-generated presence.
316 -- FIXME some day, likely needing changes to mod_presence
317 module:hook("pre-presence/bare", drop_outgoing, prio_out);
318 module:hook("pre-presence/full", drop_outgoing, prio_out);
319 module:hook("pre-presence/host", drop_outgoing, prio_out);
320
321 module:hook("pre-iq/bare", bounce_outgoing, prio_out);
322 module:hook("pre-iq/full", bounce_outgoing, prio_out);
323 module:hook("pre-iq/host", bounce_outgoing, prio_out);
324