2 -- Copyright (C) 2009-2010 Matthew Wild
3 -- Copyright (C) 2009-2010 Waqas Hussain
4 -- Copyright (C) 2014 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 new = {}; -- JIDs to block depending or unblock on action
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 = {};
133 -- Because blocking someone currently also blocks the ability to reject
134 -- subscription requests, we'll preemptively reject such
135 local remove_pending = {};
137 for item in action:childtags("item") do
138 local jid = jid_prep(item.attr.jid);
140 origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
143 item.attr.jid = jid; -- echo back prepped
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;
152 local is_blocking = action.name == "block" or nil; -- nil if unblocking
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"));
160 local blocklist = get_blocklist(username);
162 local new_blocklist = {};
164 if is_blocking or next(new) then
165 for jid in pairs(blocklist) do
166 new_blocklist[jid] = true;
168 for jid in pairs(new) do
169 new_blocklist[jid] = is_blocking;
171 -- else empty the blocklist
173 new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice
175 local ok, err = set_blocklist(username, new_blocklist);
177 origin.send(st.reply(stanza));
179 origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
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 }));
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;
199 save_roster(username, module.host, roster);
200 -- Not much we can do about save failing here
204 local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
205 :add_child(action); -- I am lazy
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);
217 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist);
218 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
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;
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 "");
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];
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);
249 return is_blocked(to, from);
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"));
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);
266 return drop_stanza(event); -- result or error
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);
274 return drop_stanza(event); -- drop headlines, groupchats etc
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
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);
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" }));
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);
304 module:hook("message/bare", bounce_message, prio_in);
305 module:hook("message/full", bounce_message, prio_in);
307 module:hook("iq/bare", bounce_iq, prio_in);
308 module:hook("iq/full", bounce_iq, prio_in);
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);
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);
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);