2 -- Copyright (C) 2009-2010 Matthew Wild
3 -- Copyright (C) 2009-2010 Waqas Hussain
4 -- Copyright (C) 2014-2015 Kim Alvefur
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
9 -- This module implements XEP-0191: Blocking Command
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;
23 local storage = module:open_store();
24 local sessions = prosody.hosts[module.host].sessions;
26 -- First level cache of blocklists by username.
27 -- Weak table so may randomly expire at any time.
28 local cache = setmetatable({}, { __mode = "v" });
30 -- Second level of caching, keeps a fixed number of items, also anchors
31 -- items in the above cache.
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);
40 local null_blocklist = {};
42 module:add_feature("urn:xmpp:blocking");
44 local function set_blocklist(username, blocklist)
45 local ok, err = storage:set(username, blocklist);
49 -- Successful save, update the cache
50 cache2:set(username, blocklist);
51 cache[username] = blocklist;
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;
66 module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username);
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);
73 module:log("warn", "Invalid JID in privacy store for user '%s' not migrated: %s", username, tostring(item.value));
75 migrated_data[jid] = true;
80 set_blocklist(username, migrated_data);
84 local function get_blocklist(username)
85 local blocklist = cache[username];
87 blocklist = cache2:get(username);
90 if not user_exists(username, module.host) then
91 return null_blocklist;
93 blocklist = storage:get(username);
95 blocklist = migrate_privacy_list(username);
97 cache2:set(username, blocklist);
99 cache[username] = blocklist;
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
110 reply:tag("item", { jid = jid }):up();
113 origin.interested_blocklist = true; -- Gets notified about changes
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
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 {};
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 {};
138 for item in action:childtags("item") do
139 local jid = jid_prep(item.attr.jid);
141 origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
144 item.attr.jid = jid; -- echo back prepped
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;
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"));
161 local blocklist = get_blocklist(username);
163 local new_blocklist = {};
165 if is_blocking or next(new) then
166 for jid in pairs(blocklist) do
167 new_blocklist[jid] = true;
169 for jid in pairs(new) do
170 new_blocklist[jid] = is_blocking;
172 -- else empty the blocklist
174 new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice
176 local ok, err = set_blocklist(username, new_blocklist);
178 origin.send(st.reply(stanza));
180 origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
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 }));
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;
200 save_roster(username, module.host, roster);
201 -- Not much we can do about save failing here
205 local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
206 :add_child(action); -- I am lazy
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);
218 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist);
219 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
221 -- Cache invalidation, solved!
222 module:hook_global("user-deleted", function (event)
223 if event.host == module.host then
224 cache:set(event.username, nil);
225 cache[event.username] = nil;
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 "");
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];
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);
250 return is_blocked(to, from);
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"));
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);
267 return drop_stanza(event); -- result or error
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);
275 return drop_stanza(event); -- drop headlines, groupchats etc
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
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);
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" }));
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);
305 module:hook("message/bare", bounce_message, prio_in);
306 module:hook("message/full", bounce_message, prio_in);
308 module:hook("iq/bare", bounce_iq, prio_in);
309 module:hook("iq/full", bounce_iq, prio_in);
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);
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);
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);