Merge 0.9->0.10
[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 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 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;
18
19 local storage = module:open_store();
20 local sessions = prosody.hosts[module.host].sessions;
21
22 -- Cache of blocklists used since module was loaded
23 local cache = {};
24 if module:get_option_boolean("blocklist_weak_cache") then
25         -- Lower memory usage, more IO and latency
26         setmetatable(cache, { __mode = "v" });
27 end
28
29 local null_blocklist = {};
30
31 module:add_feature("urn:xmpp:blocking");
32
33 local function set_blocklist(username, blocklist)
34         local ok, err = storage:set(username, blocklist);
35         if not ok then
36                 return ok, err;
37         end
38         -- Successful save, update the cache
39         cache[username] = blocklist;
40         return true;
41 end
42
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;
50         else
51                 return migrated_data;
52         end
53         if legacy_data then
54                 module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username);
55                 local item, jid;
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);
60                                 if not jid then
61                                         module:log("warn", "Invalid JID in privacy store for user '%s' not migrated: %s", username, tostring(item.value));
62                                 else
63                                         migrated_data[jid] = true;
64                                 end
65                         end
66                 end
67         end
68         set_blocklist(username, migrated_data);
69         return migrated_data;
70 end
71
72 local function get_blocklist(username)
73         local blocklist = cache[username];
74         if not blocklist then
75                 if not user_exists(username, module.host) then
76                         return null_blocklist;
77                 end
78                 blocklist = storage:get(username);
79                 if not blocklist then
80                         blocklist = migrate_privacy_list(username);
81                 end
82                 cache[username] = blocklist;
83         end
84         return blocklist;
85 end
86
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
93                 if jid then
94                         reply:tag("item", { jid = jid }):up();
95                 end
96         end
97         origin.interested_blocklist = true; -- Gets notified about changes
98         origin.send(reply);
99         return true;
100 end);
101
102 -- Add or remove some jid(s) from the blocklist
103 -- We want this to be atomic and not do a partial update
104 local function edit_blocklist(event)
105         local origin, stanza = event.origin, event.stanza;
106         local username = origin.username;
107         local action = stanza.tags[1];
108         local new = {};
109
110         for item in action:childtags("item") do
111                 local jid = jid_prep(item.attr.jid);
112                 if not jid then
113                         origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
114                         return true;
115                 end
116                 item.attr.jid = jid; -- echo back prepped
117                 new[jid] = is_contact_subscribed(username, module.host, jid) or false;
118         end
119
120         local mode = action.name == "block" or nil;
121
122         if mode and not next(new) then
123                 -- <block/> element does not contain at least one <item/> child element
124                 origin.send(st_error_reply(stanza, "modify", "bad-request"));
125                 return true;
126         end
127
128         local blocklist = get_blocklist(username);
129
130         local new_blocklist = {};
131
132         if mode or next(new) then
133                 for jid in pairs(blocklist) do
134                         new_blocklist[jid] = true;
135                 end
136                 for jid in pairs(new) do
137                         new_blocklist[jid] = mode;
138                 end
139                 -- else empty the blocklist
140         end
141         new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice
142
143         local ok, err = set_blocklist(username, new_blocklist);
144         if ok then
145                 origin.send(st.reply(stanza));
146         else
147                 origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
148                 return true;
149         end
150
151         if mode then
152                 for jid, in_roster in pairs(new) do
153                         if not blocklist[jid] and in_roster and sessions[username] then
154                                 for _, session in pairs(sessions[username].sessions) do
155                                         if session.presence then
156                                                 module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
157                                         end
158                                 end
159                         end
160                 end
161         end
162         if sessions[username] then
163                 local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
164                         :add_child(action); -- I am lazy
165
166                 for _, session in pairs(sessions[username].sessions) do
167                         if session.interested_blocklist then
168                                 blocklist_push.attr.to = session.full_jid;
169                                 session.send(blocklist_push);
170                         end
171                 end
172         end
173
174         return true;
175 end
176
177 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist);
178 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
179
180 -- Cache invalidation, solved!
181 module:hook_global("user-deleted", function (event)
182         if event.host == module.host then
183                 cache[event.username] = nil;
184         end
185 end);
186
187 -- Buggy clients
188 module:hook("iq-error/self/blocklist-push", function (event)
189         local _, condition, text = event.stanza:get_error();
190         (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         return true;
192 end);
193
194 local function is_blocked(user, jid)
195         local blocklist = cache[user] or get_blocklist(user);
196         if blocklist[jid] then return true; end
197         local node, host = jid_split(jid);
198         return blocklist[host] or node and blocklist[node..'@'..host];
199 end
200
201 -- Event handlers for bouncing or dropping stanzas
202 local function drop_stanza(event)
203         local stanza = event.stanza;
204         local attr = stanza.attr;
205         local to, from = attr.to, attr.from;
206         to = to and jid_split(to);
207         if to and from then
208                 return is_blocked(to, from);
209         end
210 end
211
212 local function bounce_stanza(event)
213         local origin, stanza = event.origin, event.stanza;
214         if drop_stanza(event) then
215                 origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
216                 return true;
217         end
218 end
219
220 local function bounce_iq(event)
221         local type = event.stanza.attr.type;
222         if type == "set" or type == "get" then
223                 return bounce_stanza(event);
224         end
225         return drop_stanza(event); -- result or error
226 end
227
228 local function bounce_message(event)
229         local type = event.stanza.attr.type;
230         if type == "chat" or not type or type == "normal" then
231                 return bounce_stanza(event);
232         end
233         return drop_stanza(event); -- drop headlines, groupchats etc
234 end
235
236 local function drop_outgoing(event)
237         local origin, stanza = event.origin, event.stanza;
238         local username = origin.username or jid_split(stanza.attr.from);
239         if not username then return end
240         local to = stanza.attr.to;
241         if to then return is_blocked(username, to); end
242         -- nil 'to' means a self event, don't bock those
243 end
244
245 local function bounce_outgoing(event)
246         local origin, stanza = event.origin, event.stanza;
247         local type = stanza.attr.type;
248         if type == "error" or stanza.name == "iq" and type == "result" then
249                 return drop_outgoing(event);
250         end
251         if drop_outgoing(event) then
252                 origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
253                         :tag("blocked", { xmlns = "urn:xmpp:blocking:errors" }));
254                 return true;
255         end
256 end
257
258 -- Hook all the events!
259 local prio_in, prio_out = 100, 100;
260 module:hook("presence/bare", drop_stanza, prio_in);
261 module:hook("presence/full", drop_stanza, prio_in);
262
263 module:hook("message/bare", bounce_message, prio_in);
264 module:hook("message/full", bounce_message, prio_in);
265
266 module:hook("iq/bare", bounce_iq, prio_in);
267 module:hook("iq/full", bounce_iq, prio_in);
268
269 module:hook("pre-message/bare", bounce_outgoing, prio_out);
270 module:hook("pre-message/full", bounce_outgoing, prio_out);
271 module:hook("pre-message/host", bounce_outgoing, prio_out);
272
273 module:hook("pre-presence/bare", drop_outgoing, prio_out);
274 module:hook("pre-presence/full", drop_outgoing, prio_out);
275 module:hook("pre-presence/host", drop_outgoing, prio_out);
276
277 module:hook("pre-iq/bare", bounce_outgoing, prio_out);
278 module:hook("pre-iq/full", bounce_outgoing, prio_out);
279 module:hook("pre-iq/host", bounce_outgoing, prio_out);
280