Merge 0.10->trunk
[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-2015 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 is_blocking = action.name == "block" or nil; -- nil if unblocking
125         local new = {}; -- JIDs to block depending or unblock on action
126
127         -- XEP-0191 sayeth:
128         -- > When the user blocks communications with the contact, the user's
129         -- > server MUST send unavailable presence information to the contact (but
130         -- > only if the contact is allowed to receive presence notifications [...]
131         -- So contacts we need to do that for are added to the set below.
132         local send_unavailable = is_blocking and {};
133
134         -- Because blocking someone currently also blocks the ability to reject
135         -- subscription requests, we'll preemptively reject such
136         local remove_pending = is_blocking and {};
137
138         for item in action:childtags("item") do
139                 local jid = jid_prep(item.attr.jid);
140                 if not jid then
141                         origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
142                         return true;
143                 end
144                 item.attr.jid = jid; -- echo back prepped
145                 new[jid] = true;
146                 if is_blocking then
147                         if is_contact_subscribed(username, module.host, jid) then
148                                 send_unavailable[jid] = true;
149                         elseif is_contact_pending_in(username, module.host, jid) then
150                                 remove_pending[jid] = true;
151                         end
152                 end
153         end
154
155         if is_blocking and not next(new) then
156                 -- <block/> element does not contain at least one <item/> child element
157                 origin.send(st_error_reply(stanza, "modify", "bad-request"));
158                 return true;
159         end
160
161         local blocklist = get_blocklist(username);
162
163         local new_blocklist = {};
164
165         if is_blocking or next(new) then
166                 for jid in pairs(blocklist) do
167                         new_blocklist[jid] = true;
168                 end
169                 for jid in pairs(new) do
170                         new_blocklist[jid] = is_blocking;
171                 end
172                 -- else empty the blocklist
173         end
174         new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice
175
176         local ok, err = set_blocklist(username, new_blocklist);
177         if ok then
178                 origin.send(st.reply(stanza));
179         else
180                 origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
181                 return true;
182         end
183
184         if is_blocking then
185                 for jid in pairs(send_unavailable) do
186                         if not blocklist[jid] then
187                                 for _, session in pairs(sessions[username].sessions) do
188                                         if session.presence then
189                                                 module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
190                                         end
191                                 end
192                         end
193                 end
194
195                 if next(remove_pending) then
196                         local roster = load_roster(username, module.host);
197                         for jid in pairs(remove_pending) do
198                                 roster[false].pending[jid] = nil;
199                         end
200                         save_roster(username, module.host, roster);
201                         -- Not much we can do about save failing here
202                 end
203         end
204
205         local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
206                 :add_child(action); -- I am lazy
207
208         for _, session in pairs(sessions[username].sessions) do
209                 if session.interested_blocklist then
210                         blocklist_push.attr.to = session.full_jid;
211                         session.send(blocklist_push);
212                 end
213         end
214
215         return true;
216 end
217
218 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist);
219 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
220
221 -- Cache invalidation, solved!
222 module:hook_global("user-deleted", function (event)
223         if event.host == module.host then
224                 cache2:set(event.username, nil);
225                 cache[event.username] = nil;
226         end
227 end);
228
229 -- Buggy clients
230 module:hook("iq-error/self/blocklist-push", function (event)
231         local _, condition, text = event.stanza:get_error();
232         (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 "");
233         return true;
234 end);
235
236 local function is_blocked(user, jid)
237         local blocklist = cache[user] or get_blocklist(user);
238         if blocklist[jid] then return true; end
239         local node, host = jid_split(jid);
240         return blocklist[host] or node and blocklist[node..'@'..host];
241 end
242
243 -- Event handlers for bouncing or dropping stanzas
244 local function drop_stanza(event)
245         local stanza = event.stanza;
246         local attr = stanza.attr;
247         local to, from = attr.to, attr.from;
248         to = to and jid_split(to);
249         if to and from then
250                 return is_blocked(to, from);
251         end
252 end
253
254 local function bounce_stanza(event)
255         local origin, stanza = event.origin, event.stanza;
256         if drop_stanza(event) then
257                 origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
258                 return true;
259         end
260 end
261
262 local function bounce_iq(event)
263         local type = event.stanza.attr.type;
264         if type == "set" or type == "get" then
265                 return bounce_stanza(event);
266         end
267         return drop_stanza(event); -- result or error
268 end
269
270 local function bounce_message(event)
271         local type = event.stanza.attr.type;
272         if type == "chat" or not type or type == "normal" then
273                 return bounce_stanza(event);
274         end
275         return drop_stanza(event); -- drop headlines, groupchats etc
276 end
277
278 local function drop_outgoing(event)
279         local origin, stanza = event.origin, event.stanza;
280         local username = origin.username or jid_split(stanza.attr.from);
281         if not username then return end
282         local to = stanza.attr.to;
283         if to then return is_blocked(username, to); end
284         -- nil 'to' means a self event, don't bock those
285 end
286
287 local function bounce_outgoing(event)
288         local origin, stanza = event.origin, event.stanza;
289         local type = stanza.attr.type;
290         if type == "error" or stanza.name == "iq" and type == "result" then
291                 return drop_outgoing(event);
292         end
293         if drop_outgoing(event) then
294                 origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
295                         :tag("blocked", { xmlns = "urn:xmpp:blocking:errors" }));
296                 return true;
297         end
298 end
299
300 -- Hook all the events!
301 local prio_in, prio_out = 100, 100;
302 module:hook("presence/bare", drop_stanza, prio_in);
303 module:hook("presence/full", drop_stanza, prio_in);
304
305 module:hook("message/bare", bounce_message, prio_in);
306 module:hook("message/full", bounce_message, prio_in);
307
308 module:hook("iq/bare", bounce_iq, prio_in);
309 module:hook("iq/full", bounce_iq, prio_in);
310
311 module:hook("pre-message/bare", bounce_outgoing, prio_out);
312 module:hook("pre-message/full", bounce_outgoing, prio_out);
313 module:hook("pre-message/host", bounce_outgoing, prio_out);
314
315 -- Note: MUST bounce these, but we don't because this would produce
316 -- lots of error replies due to server-generated presence.
317 -- FIXME some day, likely needing changes to mod_presence
318 module:hook("pre-presence/bare", drop_outgoing, prio_out);
319 module:hook("pre-presence/full", drop_outgoing, prio_out);
320 module:hook("pre-presence/host", drop_outgoing, prio_out);
321
322 module:hook("pre-iq/bare", bounce_outgoing, prio_out);
323 module:hook("pre-iq/full", bounce_outgoing, prio_out);
324 module:hook("pre-iq/host", bounce_outgoing, prio_out);
325