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 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;
19 local storage = module:open_store();
20 local sessions = prosody.hosts[module.host].sessions;
22 -- First level cache of blocklists by username.
23 -- Weak table so may randomly expire at any time.
24 local cache = setmetatable({}, { __mode = "v" });
26 -- Second level of caching, keeps a fixed number of items, also anchors
27 -- items in the above cache.
29 -- The size of this affects how often we will need to load a blocklist from
30 -- disk, which we want to avoid during routing. On the other hand, we don't
31 -- want to use too much memory either, so this can be tuned by advanced
32 -- users. TODO use science to figure out a better default, 64 is just a guess.
33 local cache_size = module:get_option_number("blocklist_cache_size", 64);
34 local cache2 = require"util.cache".new(cache_size);
36 local null_blocklist = {};
38 module:add_feature("urn:xmpp:blocking");
40 local function set_blocklist(username, blocklist)
41 local ok, err = storage:set(username, blocklist);
45 -- Successful save, update the cache
46 cache2:set(username, blocklist);
47 cache[username] = blocklist;
51 -- Migrates from the old mod_privacy storage
52 local function migrate_privacy_list(username)
53 local migrated_data = { [false] = "not empty" };
54 local legacy_data = module:open_store("privacy"):get(username);
55 if legacy_data and legacy_data.lists and legacy_data.default then
56 legacy_data = legacy_data.lists[legacy_data.default];
57 legacy_data = legacy_data and legacy_data.items;
62 module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username);
64 for i = 1, #legacy_data do
65 item = legacy_data[i];
66 if item.type == "jid" and item.action == "deny" then
67 jid = jid_prep(item.value);
69 module:log("warn", "Invalid JID in privacy store for user '%s' not migrated: %s", username, tostring(item.value));
71 migrated_data[jid] = true;
76 set_blocklist(username, migrated_data);
80 local function get_blocklist(username)
81 local blocklist = cache[username];
83 blocklist = cache2:get(username);
86 if not user_exists(username, module.host) then
87 return null_blocklist;
89 blocklist = storage:get(username);
91 blocklist = migrate_privacy_list(username);
93 cache2:set(username, blocklist);
95 cache[username] = blocklist;
99 module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event)
100 local origin, stanza = event.origin, event.stanza;
101 local username = origin.username;
102 local reply = st.reply(stanza):tag("blocklist", { xmlns = "urn:xmpp:blocking" });
103 local blocklist = get_blocklist(username);
104 for jid in pairs(blocklist) do
106 reply:tag("item", { jid = jid }):up();
109 origin.interested_blocklist = true; -- Gets notified about changes
114 -- Add or remove some jid(s) from the blocklist
115 -- We want this to be atomic and not do a partial update
116 local function edit_blocklist(event)
117 local origin, stanza = event.origin, event.stanza;
118 local username = origin.username;
119 local action = stanza.tags[1]; -- "block" or "unblock"
120 local new = {}; -- JIDs to block depending or unblock on action
122 for item in action:childtags("item") do
123 local jid = jid_prep(item.attr.jid);
125 origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
128 item.attr.jid = jid; -- echo back prepped
129 new[jid] = is_contact_subscribed(username, module.host, jid) or false;
132 local is_blocking = action.name == "block" or nil; -- nil if unblocking
134 if is_blocking and not next(new) then
135 -- <block/> element does not contain at least one <item/> child element
136 origin.send(st_error_reply(stanza, "modify", "bad-request"));
140 local blocklist = get_blocklist(username);
142 local new_blocklist = {};
144 if is_blocking or next(new) then
145 for jid in pairs(blocklist) do
146 new_blocklist[jid] = true;
148 for jid in pairs(new) do
149 new_blocklist[jid] = is_blocking;
151 -- else empty the blocklist
153 new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice
155 local ok, err = set_blocklist(username, new_blocklist);
157 origin.send(st.reply(stanza));
159 origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
164 for jid, in_roster in pairs(new) do
165 if not blocklist[jid] and in_roster then
166 for _, session in pairs(sessions[username].sessions) do
167 if session.presence then
168 module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
175 local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
176 :add_child(action); -- I am lazy
178 for _, session in pairs(sessions[username].sessions) do
179 if session.interested_blocklist then
180 blocklist_push.attr.to = session.full_jid;
181 session.send(blocklist_push);
188 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist);
189 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
191 -- Cache invalidation, solved!
192 module:hook_global("user-deleted", function (event)
193 if event.host == module.host then
194 cache:set(event.username, nil);
195 cache[event.username] = nil;
200 module:hook("iq-error/self/blocklist-push", function (event)
201 local _, condition, text = event.stanza:get_error();
202 (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 "");
206 local function is_blocked(user, jid)
207 local blocklist = cache[user] or get_blocklist(user);
208 if blocklist[jid] then return true; end
209 local node, host = jid_split(jid);
210 return blocklist[host] or node and blocklist[node..'@'..host];
213 -- Event handlers for bouncing or dropping stanzas
214 local function drop_stanza(event)
215 local stanza = event.stanza;
216 local attr = stanza.attr;
217 local to, from = attr.to, attr.from;
218 to = to and jid_split(to);
220 return is_blocked(to, from);
224 local function bounce_stanza(event)
225 local origin, stanza = event.origin, event.stanza;
226 if drop_stanza(event) then
227 origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
232 local function bounce_iq(event)
233 local type = event.stanza.attr.type;
234 if type == "set" or type == "get" then
235 return bounce_stanza(event);
237 return drop_stanza(event); -- result or error
240 local function bounce_message(event)
241 local type = event.stanza.attr.type;
242 if type == "chat" or not type or type == "normal" then
243 return bounce_stanza(event);
245 return drop_stanza(event); -- drop headlines, groupchats etc
248 local function drop_outgoing(event)
249 local origin, stanza = event.origin, event.stanza;
250 local username = origin.username or jid_split(stanza.attr.from);
251 if not username then return end
252 local to = stanza.attr.to;
253 if to then return is_blocked(username, to); end
254 -- nil 'to' means a self event, don't bock those
257 local function bounce_outgoing(event)
258 local origin, stanza = event.origin, event.stanza;
259 local type = stanza.attr.type;
260 if type == "error" or stanza.name == "iq" and type == "result" then
261 return drop_outgoing(event);
263 if drop_outgoing(event) then
264 origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
265 :tag("blocked", { xmlns = "urn:xmpp:blocking:errors" }));
270 -- Hook all the events!
271 local prio_in, prio_out = 100, 100;
272 module:hook("presence/bare", drop_stanza, prio_in);
273 module:hook("presence/full", drop_stanza, prio_in);
275 module:hook("message/bare", bounce_message, prio_in);
276 module:hook("message/full", bounce_message, prio_in);
278 module:hook("iq/bare", bounce_iq, prio_in);
279 module:hook("iq/full", bounce_iq, prio_in);
281 module:hook("pre-message/bare", bounce_outgoing, prio_out);
282 module:hook("pre-message/full", bounce_outgoing, prio_out);
283 module:hook("pre-message/host", bounce_outgoing, prio_out);
285 -- Note: MUST bounce these, but we don't because this would produce
286 -- lots of error replies due to server-generated presence.
287 -- FIXME some day, likely needing changes to mod_presence
288 module:hook("pre-presence/bare", drop_outgoing, prio_out);
289 module:hook("pre-presence/full", drop_outgoing, prio_out);
290 module:hook("pre-presence/host", drop_outgoing, prio_out);
292 module:hook("pre-iq/bare", bounce_outgoing, prio_out);
293 module:hook("pre-iq/full", bounce_outgoing, prio_out);
294 module:hook("pre-iq/host", bounce_outgoing, prio_out);