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, jid_split = import("util.jid", "prep", "split");
18 local host = module.host;
19 local storage = module:open_store();
20 local sessions = prosody.hosts[host].sessions;
22 -- Cache of blocklists used since module was loaded
24 if module:get_option_boolean("blocklist_weak_cache") then
25 -- Lower memory usage, more IO and latency
26 setmetatable(cache, { __mode = "v" });
29 local null_blocklist = {};
31 module:add_feature("urn:xmpp:blocking");
33 local function set_blocklist(username, blocklist)
34 local ok, err = storage:set(username, blocklist);
38 -- Successful save, update the cache
39 cache[username] = blocklist;
43 -- Migrates from the old mod_privacy storage
44 local function migrate_privacy_list(username)
45 local migrated_data = { [false] = "not empty" };
46 local legacy_data = module:open_store("privacy"):get(username);
47 if legacy_data and legacy_data.lists and legacy_data.default then
48 legacy_data = legacy_data.lists[legacy_data.default];
49 legacy_data = legacy_data and legacy_data.items;
54 module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username);
56 for i = 1, #legacy_data do
57 item = legacy_data[i];
58 if item.type == "jid" and item.action == "deny" then
59 jid = jid_prep(item.value);
61 module:log("warn", "Invalid JID in privacy store for user '%s' not migrated: %s", username, tostring(item.value));
63 migrated_data[jid] = true;
68 set_blocklist(username, migrated_data);
72 local function get_blocklist(username)
73 local blocklist = cache[username];
75 if not user_exists(username, host) then
76 return null_blocklist;
78 blocklist = storage:get(username);
80 blocklist = migrate_privacy_list(username);
82 cache[username] = blocklist;
87 module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event)
88 local origin, stanza = event.origin, event.stanza;
89 local username = origin.username;
90 local reply = st.reply(stanza):tag("blocklist", { xmlns = "urn:xmpp:blocking" });
91 local blocklist = get_blocklist(username);
92 for jid in pairs(blocklist) do
94 reply:tag("item", { jid = jid }):up();
97 origin.interested_blocklist = true; -- Gets notified about changes
98 return origin.send(reply);
101 -- Add or remove some jid(s) from the blocklist
102 -- We want this to be atomic and not do a partial update
103 local function edit_blocklist(event)
104 local origin, stanza = event.origin, event.stanza;
105 local username = origin.username;
106 local action = stanza.tags[1];
110 for item in action:childtags("item") do
111 jid = jid_prep(item.attr.jid);
113 return origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
115 item.attr.jid = jid; -- echo back prepped
116 new[jid] = is_contact_subscribed(username, host, jid) or false;
119 local mode = action.name == "block" or nil;
121 if mode and not next(new) then
122 -- <block/> element does not contain at least one <item/> child element
123 return origin.send(st_error_reply(stanza, "modify", "bad-request"));
126 local blocklist = get_blocklist(username);
128 local new_blocklist = {};
130 if mode or next(new) then
131 for jid in pairs(blocklist) do
132 new_blocklist[jid] = true;
134 for jid in pairs(new) do
135 new_blocklist[jid] = mode;
137 -- else empty the blocklist
139 new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice
141 local ok, err = set_blocklist(username, new_blocklist);
143 origin.send(st.reply(stanza));
145 return origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
149 for jid, in_roster in pairs(new) do
150 if not blocklist[jid] and in_roster and sessions[username] then
151 for _, session in pairs(sessions[username].sessions) do
152 if session.presence then
153 module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
159 if sessions[username] then
160 local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
161 :add_child(action); -- I am lazy
163 for _, session in pairs(sessions[username].sessions) do
164 if session.interested_blocklist then
165 blocklist_push.attr.to = session.full_jid;
166 session.send(blocklist_push);
174 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist);
175 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
177 -- Cache invalidation, solved!
178 module:hook_global("user-deleted", function (event)
179 if event.host == host then
180 cache[event.username] = nil;
185 module:hook("iq-error/self/blocklist-push", function (event)
186 local type, condition, text = event.stanza:get_error();
187 (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 "");
191 local function is_blocked(user, jid)
192 local blocklist = cache[user] or get_blocklist(user);
193 if blocklist[jid] then return true; end
194 local node, host = jid_split(jid);
195 return blocklist[host] or node and blocklist[node..'@'..host];
198 -- Event handlers for bouncing or dropping stanzas
199 local function drop_stanza(event)
200 local stanza = event.stanza;
201 local attr = stanza.attr;
202 local to, from = attr.to, attr.from;
203 to = to and jid_split(to);
205 return is_blocked(to, from);
209 local function bounce_stanza(event)
210 local origin, stanza = event.origin, event.stanza;
211 if drop_stanza(event) then
212 return origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
216 local function bounce_iq(event)
217 local type = event.stanza.attr.type;
218 if type == "set" or type == "get" then
219 return bounce_stanza(event);
221 return drop_stanza(event); -- result or error
224 local function bounce_message(event)
225 local type = event.stanza.attr.type;
226 if type == "chat" or not type or type == "normal" then
227 return bounce_stanza(event);
229 return drop_stanza(event); -- drop headlines, groupchats etc
232 local function drop_outgoing(event)
233 local origin, stanza = event.origin, event.stanza;
234 local username = origin.username or jid_split(stanza.attr.from);
235 if not username then return end
236 local to = stanza.attr.to;
237 if to then return is_blocked(username, to); end
238 -- nil 'to' means a self event, don't bock those
241 local function bounce_outgoing(event)
242 local origin, stanza = event.origin, event.stanza;
243 local type = stanza.attr.type;
244 if type == "error" or stanza.name == "iq" and type == "result" then
245 return drop_outgoing(event);
247 if drop_outgoing(event) then
248 return origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
249 :tag("blocked", { xmlns = "urn:xmpp:blocking:errors" }));
253 -- Hook all the events!
254 local prio_in, prio_out = 100, 100;
255 module:hook("presence/bare", drop_stanza, prio_in);
256 module:hook("presence/full", drop_stanza, prio_in);
258 module:hook("message/bare", bounce_message, prio_in);
259 module:hook("message/full", bounce_message, prio_in);
261 module:hook("iq/bare", bounce_iq, prio_in);
262 module:hook("iq/full", bounce_iq, prio_in);
264 module:hook("pre-message/bare", bounce_outgoing, prio_out);
265 module:hook("pre-message/full", bounce_outgoing, prio_out);
266 module:hook("pre-message/host", bounce_outgoing, prio_out);
268 module:hook("pre-presence/bare", drop_outgoing, prio_out);
269 module:hook("pre-presence/full", drop_outgoing, prio_out);
270 module:hook("pre-presence/host", drop_outgoing, prio_out);
272 module:hook("pre-iq/bare", bounce_outgoing, prio_out);
273 module:hook("pre-iq/full", bounce_outgoing, prio_out);
274 module:hook("pre-iq/host", bounce_outgoing, prio_out);