Annotate

plugins/mod_blocklist.lua @ 7644:90a4790c2329

storagemanager: Fire event when opening a store, and allow the returned store/err to be overridden
author Matthew Wild <mwild1@gmail.com>
date Wed, 31 Aug 2016 11:50:41 +0100
parent 7621:b8c03df6e8ca
child 7771:2b288dab781a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Prosody IM
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2009-2010 Matthew Wild
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- Copyright (C) 2009-2010 Waqas Hussain
6976
4688ff9d4f2b mod_blocklist: Update Copyright header
Kim Alvefur <zash@zash.se>
parents: 6975
diff changeset
4 -- Copyright (C) 2014-2015 Kim Alvefur
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 --
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 --
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 -- This module implements XEP-0191: Blocking Command
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 --
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local user_exists = require"core.usermanager".user_exists;
6974
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
13 local rostermanager = require"core.rostermanager";
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
14 local is_contact_subscribed = rostermanager.is_contact_subscribed;
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
15 local is_contact_pending_in = rostermanager.is_contact_pending_in;
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
16 local load_roster = rostermanager.load_roster;
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
17 local save_roster = rostermanager.save_roster;
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local st = require"util.stanza";
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local st_error_reply = st.error_reply;
6629
42aeb882b3e1 mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
20 local jid_prep = require"util.jid".prep;
42aeb882b3e1 mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
21 local jid_split = require"util.jid".split;
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local storage = module:open_store();
6629
42aeb882b3e1 mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
24 local sessions = prosody.hosts[module.host].sessions;
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
6970
cde7d14052f9 mod_blocklist: Expand comments on caching of blocklists
Kim Alvefur <zash@zash.se>
parents: 6969
diff changeset
26 -- First level cache of blocklists by username.
cde7d14052f9 mod_blocklist: Expand comments on caching of blocklists
Kim Alvefur <zash@zash.se>
parents: 6969
diff changeset
27 -- Weak table so may randomly expire at any time.
6944
62b6f6d230f1 mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
Kim Alvefur <zash@zash.se>
parents: 6833
diff changeset
28 local cache = setmetatable({}, { __mode = "v" });
62b6f6d230f1 mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
Kim Alvefur <zash@zash.se>
parents: 6833
diff changeset
29
6970
cde7d14052f9 mod_blocklist: Expand comments on caching of blocklists
Kim Alvefur <zash@zash.se>
parents: 6969
diff changeset
30 -- Second level of caching, keeps a fixed number of items, also anchors
cde7d14052f9 mod_blocklist: Expand comments on caching of blocklists
Kim Alvefur <zash@zash.se>
parents: 6969
diff changeset
31 -- items in the above cache.
cde7d14052f9 mod_blocklist: Expand comments on caching of blocklists
Kim Alvefur <zash@zash.se>
parents: 6969
diff changeset
32 --
cde7d14052f9 mod_blocklist: Expand comments on caching of blocklists
Kim Alvefur <zash@zash.se>
parents: 6969
diff changeset
33 -- The size of this affects how often we will need to load a blocklist from
cde7d14052f9 mod_blocklist: Expand comments on caching of blocklists
Kim Alvefur <zash@zash.se>
parents: 6969
diff changeset
34 -- disk, which we want to avoid during routing. On the other hand, we don't
cde7d14052f9 mod_blocklist: Expand comments on caching of blocklists
Kim Alvefur <zash@zash.se>
parents: 6969
diff changeset
35 -- want to use too much memory either, so this can be tuned by advanced
cde7d14052f9 mod_blocklist: Expand comments on caching of blocklists
Kim Alvefur <zash@zash.se>
parents: 6969
diff changeset
36 -- users. TODO use science to figure out a better default, 64 is just a guess.
6944
62b6f6d230f1 mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
Kim Alvefur <zash@zash.se>
parents: 6833
diff changeset
37 local cache_size = module:get_option_number("blocklist_cache_size", 64);
62b6f6d230f1 mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
Kim Alvefur <zash@zash.se>
parents: 6833
diff changeset
38 local cache2 = require"util.cache".new(cache_size);
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local null_blocklist = {};
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 module:add_feature("urn:xmpp:blocking");
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 local function set_blocklist(username, blocklist)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 local ok, err = storage:set(username, blocklist);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 if not ok then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 return ok, err;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 -- Successful save, update the cache
6944
62b6f6d230f1 mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
Kim Alvefur <zash@zash.se>
parents: 6833
diff changeset
50 cache2:set(username, blocklist);
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 cache[username] = blocklist;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 return true;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 -- Migrates from the old mod_privacy storage
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 local function migrate_privacy_list(username)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 local migrated_data = { [false] = "not empty" };
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 local legacy_data = module:open_store("privacy"):get(username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 if legacy_data and legacy_data.lists and legacy_data.default then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 legacy_data = legacy_data.lists[legacy_data.default];
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 legacy_data = legacy_data and legacy_data.items;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 else
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 return migrated_data;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 if legacy_data then
6494
5979eaed12c0 mod_blocklist: Only log message about migrating from mod_privacy when there is data to migrate
Kim Alvefur <zash@zash.se>
parents: 6460
diff changeset
66 module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username);
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 local item, jid;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 for i = 1, #legacy_data do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 item = legacy_data[i];
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 if item.type == "jid" and item.action == "deny" then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 jid = jid_prep(item.value);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 if not jid then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 module:log("warn", "Invalid JID in privacy store for user '%s' not migrated: %s", username, tostring(item.value));
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 else
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 migrated_data[jid] = true;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 set_blocklist(username, migrated_data);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 return migrated_data;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 local function get_blocklist(username)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 local blocklist = cache[username];
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 if not blocklist then
6944
62b6f6d230f1 mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
Kim Alvefur <zash@zash.se>
parents: 6833
diff changeset
87 blocklist = cache2:get(username);
62b6f6d230f1 mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
Kim Alvefur <zash@zash.se>
parents: 6833
diff changeset
88 end
62b6f6d230f1 mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
Kim Alvefur <zash@zash.se>
parents: 6833
diff changeset
89 if not blocklist then
6629
42aeb882b3e1 mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
90 if not user_exists(username, module.host) then
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 return null_blocklist;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 blocklist = storage:get(username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 if not blocklist then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 blocklist = migrate_privacy_list(username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 end
6944
62b6f6d230f1 mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
Kim Alvefur <zash@zash.se>
parents: 6833
diff changeset
97 cache2:set(username, blocklist);
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 end
6944
62b6f6d230f1 mod_blocklist: Use util.cache to manage how many users blocklists are kept in memory
Kim Alvefur <zash@zash.se>
parents: 6833
diff changeset
99 cache[username] = blocklist;
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 return blocklist;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 local origin, stanza = event.origin, event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 local username = origin.username;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 local reply = st.reply(stanza):tag("blocklist", { xmlns = "urn:xmpp:blocking" });
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 local blocklist = get_blocklist(username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 for jid in pairs(blocklist) do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 if jid then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 reply:tag("item", { jid = jid }):up();
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 origin.interested_blocklist = true; -- Gets notified about changes
6833
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
114 origin.send(reply);
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
115 return true;
7620
c27c9695d130 mod_blocklist: Decrease priority of iq hooks to ease handling by other modules
Kim Alvefur <zash@zash.se>
parents: 7079
diff changeset
116 end, -1);
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117
6351
10dc228a45a4 mod_blocklist: Correct comment
Kim Alvefur <zash@zash.se>
parents: 6350
diff changeset
118 -- Add or remove some jid(s) from the blocklist
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 -- We want this to be atomic and not do a partial update
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 local function edit_blocklist(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 local origin, stanza = event.origin, event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 local username = origin.username;
6971
96080d86bab8 mod_blocklist: Add comments describing some variables
Kim Alvefur <zash@zash.se>
parents: 6970
diff changeset
123 local action = stanza.tags[1]; -- "block" or "unblock"
6975
5bc229eb99d3 mod_blocklist: Skip creating some tables and some processing if unblocking
Kim Alvefur <zash@zash.se>
parents: 6974
diff changeset
124 local is_blocking = action.name == "block" or nil; -- nil if unblocking
6971
96080d86bab8 mod_blocklist: Add comments describing some variables
Kim Alvefur <zash@zash.se>
parents: 6970
diff changeset
125 local new = {}; -- JIDs to block depending or unblock on action
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126
6973
f350f840a6f7 mod_blocklist: Restructure how we keep track of where to send unavailable presence
Kim Alvefur <zash@zash.se>
parents: 6972
diff changeset
127 -- XEP-0191 sayeth:
f350f840a6f7 mod_blocklist: Restructure how we keep track of where to send unavailable presence
Kim Alvefur <zash@zash.se>
parents: 6972
diff changeset
128 -- > When the user blocks communications with the contact, the user's
f350f840a6f7 mod_blocklist: Restructure how we keep track of where to send unavailable presence
Kim Alvefur <zash@zash.se>
parents: 6972
diff changeset
129 -- > server MUST send unavailable presence information to the contact (but
f350f840a6f7 mod_blocklist: Restructure how we keep track of where to send unavailable presence
Kim Alvefur <zash@zash.se>
parents: 6972
diff changeset
130 -- > only if the contact is allowed to receive presence notifications [...]
f350f840a6f7 mod_blocklist: Restructure how we keep track of where to send unavailable presence
Kim Alvefur <zash@zash.se>
parents: 6972
diff changeset
131 -- So contacts we need to do that for are added to the set below.
6975
5bc229eb99d3 mod_blocklist: Skip creating some tables and some processing if unblocking
Kim Alvefur <zash@zash.se>
parents: 6974
diff changeset
132 local send_unavailable = is_blocking and {};
6973
f350f840a6f7 mod_blocklist: Restructure how we keep track of where to send unavailable presence
Kim Alvefur <zash@zash.se>
parents: 6972
diff changeset
133
6974
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
134 -- Because blocking someone currently also blocks the ability to reject
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
135 -- subscription requests, we'll preemptively reject such
6975
5bc229eb99d3 mod_blocklist: Skip creating some tables and some processing if unblocking
Kim Alvefur <zash@zash.se>
parents: 6974
diff changeset
136 local remove_pending = is_blocking and {};
6974
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
137
6352
b703e6930e4c mod_blocklist: Use full word as variable name, we can afford that
Kim Alvefur <zash@zash.se>
parents: 6351
diff changeset
138 for item in action:childtags("item") do
6629
42aeb882b3e1 mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
139 local jid = jid_prep(item.attr.jid);
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 if not jid then
6833
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
141 origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
142 return true;
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 item.attr.jid = jid; -- echo back prepped
6973
f350f840a6f7 mod_blocklist: Restructure how we keep track of where to send unavailable presence
Kim Alvefur <zash@zash.se>
parents: 6972
diff changeset
145 new[jid] = true;
6975
5bc229eb99d3 mod_blocklist: Skip creating some tables and some processing if unblocking
Kim Alvefur <zash@zash.se>
parents: 6974
diff changeset
146 if is_blocking then
5bc229eb99d3 mod_blocklist: Skip creating some tables and some processing if unblocking
Kim Alvefur <zash@zash.se>
parents: 6974
diff changeset
147 if is_contact_subscribed(username, module.host, jid) then
5bc229eb99d3 mod_blocklist: Skip creating some tables and some processing if unblocking
Kim Alvefur <zash@zash.se>
parents: 6974
diff changeset
148 send_unavailable[jid] = true;
5bc229eb99d3 mod_blocklist: Skip creating some tables and some processing if unblocking
Kim Alvefur <zash@zash.se>
parents: 6974
diff changeset
149 elseif is_contact_pending_in(username, module.host, jid) then
5bc229eb99d3 mod_blocklist: Skip creating some tables and some processing if unblocking
Kim Alvefur <zash@zash.se>
parents: 6974
diff changeset
150 remove_pending[jid] = true;
5bc229eb99d3 mod_blocklist: Skip creating some tables and some processing if unblocking
Kim Alvefur <zash@zash.se>
parents: 6974
diff changeset
151 end
6973
f350f840a6f7 mod_blocklist: Restructure how we keep track of where to send unavailable presence
Kim Alvefur <zash@zash.se>
parents: 6972
diff changeset
152 end
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154
6968
828a10e0464b mod_blocklist: Rename variable for clarity
Kim Alvefur <zash@zash.se>
parents: 6967
diff changeset
155 if is_blocking and not next(new) then
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 -- <block/> element does not contain at least one <item/> child element
6833
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
157 origin.send(st_error_reply(stanza, "modify", "bad-request"));
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
158 return true;
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 local blocklist = get_blocklist(username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 local new_blocklist = {};
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164
6968
828a10e0464b mod_blocklist: Rename variable for clarity
Kim Alvefur <zash@zash.se>
parents: 6967
diff changeset
165 if is_blocking or next(new) then
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 for jid in pairs(blocklist) do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 new_blocklist[jid] = true;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 for jid in pairs(new) do
6968
828a10e0464b mod_blocklist: Rename variable for clarity
Kim Alvefur <zash@zash.se>
parents: 6967
diff changeset
170 new_blocklist[jid] = is_blocking;
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 -- else empty the blocklist
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 local ok, err = set_blocklist(username, new_blocklist);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 if ok then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 origin.send(st.reply(stanza));
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 else
6833
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
180 origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
181 return true;
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183
6968
828a10e0464b mod_blocklist: Rename variable for clarity
Kim Alvefur <zash@zash.se>
parents: 6967
diff changeset
184 if is_blocking then
6973
f350f840a6f7 mod_blocklist: Restructure how we keep track of where to send unavailable presence
Kim Alvefur <zash@zash.se>
parents: 6972
diff changeset
185 for jid in pairs(send_unavailable) do
f350f840a6f7 mod_blocklist: Restructure how we keep track of where to send unavailable presence
Kim Alvefur <zash@zash.se>
parents: 6972
diff changeset
186 if not blocklist[jid] then
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 for _, session in pairs(sessions[username].sessions) do
6495
44df423f8290 mod_blocklist: Don't send unavailable presence from unavailable sessions when blocking a contact
Kim Alvefur <zash@zash.se>
parents: 6494
diff changeset
188 if session.presence then
44df423f8290 mod_blocklist: Don't send unavailable presence from unavailable sessions when blocking a contact
Kim Alvefur <zash@zash.se>
parents: 6494
diff changeset
189 module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
44df423f8290 mod_blocklist: Don't send unavailable presence from unavailable sessions when blocking a contact
Kim Alvefur <zash@zash.se>
parents: 6494
diff changeset
190 end
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 end
6974
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
194
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
195 if next(remove_pending) then
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
196 local roster = load_roster(username, module.host);
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
197 for jid in pairs(remove_pending) do
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
198 roster[false].pending[jid] = nil;
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
199 end
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
200 save_roster(username, module.host, roster);
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
201 -- Not much we can do about save failing here
bdb216e0688a mod_blocklist: When blocking someone who sent a subscription request, forget that request since the user would be unable to deny it while blocked (Fixes #574)
Kim Alvefur <zash@zash.se>
parents: 6973
diff changeset
202 end
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 end
6972
9e926e48cbf9 mod_blocklist: session[username] can't possibly be unset if that user is sending queries
Kim Alvefur <zash@zash.se>
parents: 6971
diff changeset
204
9e926e48cbf9 mod_blocklist: session[username] can't possibly be unset if that user is sending queries
Kim Alvefur <zash@zash.se>
parents: 6971
diff changeset
205 local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
9e926e48cbf9 mod_blocklist: session[username] can't possibly be unset if that user is sending queries
Kim Alvefur <zash@zash.se>
parents: 6971
diff changeset
206 :add_child(action); -- I am lazy
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207
6972
9e926e48cbf9 mod_blocklist: session[username] can't possibly be unset if that user is sending queries
Kim Alvefur <zash@zash.se>
parents: 6971
diff changeset
208 for _, session in pairs(sessions[username].sessions) do
9e926e48cbf9 mod_blocklist: session[username] can't possibly be unset if that user is sending queries
Kim Alvefur <zash@zash.se>
parents: 6971
diff changeset
209 if session.interested_blocklist then
9e926e48cbf9 mod_blocklist: session[username] can't possibly be unset if that user is sending queries
Kim Alvefur <zash@zash.se>
parents: 6971
diff changeset
210 blocklist_push.attr.to = session.full_jid;
9e926e48cbf9 mod_blocklist: session[username] can't possibly be unset if that user is sending queries
Kim Alvefur <zash@zash.se>
parents: 6971
diff changeset
211 session.send(blocklist_push);
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 return true;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217
7620
c27c9695d130 mod_blocklist: Decrease priority of iq hooks to ease handling by other modules
Kim Alvefur <zash@zash.se>
parents: 7079
diff changeset
218 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist, -1);
c27c9695d130 mod_blocklist: Decrease priority of iq hooks to ease handling by other modules
Kim Alvefur <zash@zash.se>
parents: 7079
diff changeset
219 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist, -1);
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 -- Cache invalidation, solved!
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 module:hook_global("user-deleted", function (event)
6629
42aeb882b3e1 mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
223 if event.host == module.host then
7079
f094683ae6eb mod_blocklist: Clear second level cache correctly on user deletion
Kim Alvefur <zash@zash.se>
parents: 6976
diff changeset
224 cache2:set(event.username, nil);
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 cache[event.username] = nil;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 end);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 -- Buggy clients
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 module:hook("iq-error/self/blocklist-push", function (event)
6629
42aeb882b3e1 mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6531
diff changeset
231 local _, condition, text = event.stanza:get_error();
6460
6d3187f24608 mod_blocklist: Capitalize log message
Kim Alvefur <zash@zash.se>
parents: 6352
diff changeset
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 "");
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 return true;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 end);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 local function is_blocked(user, jid)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 local blocklist = cache[user] or get_blocklist(user);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 if blocklist[jid] then return true; end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 local node, host = jid_split(jid);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 return blocklist[host] or node and blocklist[node..'@'..host];
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 -- Event handlers for bouncing or dropping stanzas
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 local function drop_stanza(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 local stanza = event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 local attr = stanza.attr;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 local to, from = attr.to, attr.from;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 to = to and jid_split(to);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 if to and from then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 return is_blocked(to, from);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 local function bounce_stanza(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 local origin, stanza = event.origin, event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 if drop_stanza(event) then
6833
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
257 origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
258 return true;
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 local function bounce_iq(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 local type = event.stanza.attr.type;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 if type == "set" or type == "get" then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 return bounce_stanza(event);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 return drop_stanza(event); -- result or error
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 local function bounce_message(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 local type = event.stanza.attr.type;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 if type == "chat" or not type or type == "normal" then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 return bounce_stanza(event);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 return drop_stanza(event); -- drop headlines, groupchats etc
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 local function drop_outgoing(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 local origin, stanza = event.origin, event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 local username = origin.username or jid_split(stanza.attr.from);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 if not username then return end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 local to = stanza.attr.to;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 if to then return is_blocked(username, to); end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 -- nil 'to' means a self event, don't bock those
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 local function bounce_outgoing(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 local origin, stanza = event.origin, event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 local type = stanza.attr.type;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 if type == "error" or stanza.name == "iq" and type == "result" then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 return drop_outgoing(event);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 if drop_outgoing(event) then
6833
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
294 origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 :tag("blocked", { xmlns = "urn:xmpp:blocking:errors" }));
6833
aeb088bb1a20 mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
Kim Alvefur <zash@zash.se>
parents: 6629
diff changeset
296 return true;
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 -- Hook all the events!
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 local prio_in, prio_out = 100, 100;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 module:hook("presence/bare", drop_stanza, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 module:hook("presence/full", drop_stanza, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304
7621
b8c03df6e8ca mod_blocklist: Mention issue numbers
Kim Alvefur <zash@zash.se>
parents: 7620
diff changeset
305 -- FIXME See #690
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 module:hook("message/bare", bounce_message, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 module:hook("message/full", bounce_message, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309 module:hook("iq/bare", bounce_iq, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 module:hook("iq/full", bounce_iq, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 module:hook("pre-message/bare", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 module:hook("pre-message/full", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 module:hook("pre-message/host", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
315
7621
b8c03df6e8ca mod_blocklist: Mention issue numbers
Kim Alvefur <zash@zash.se>
parents: 7620
diff changeset
316 -- FIXME See #575 -- We MUST bounce these, but we don't because this
b8c03df6e8ca mod_blocklist: Mention issue numbers
Kim Alvefur <zash@zash.se>
parents: 7620
diff changeset
317 -- would produce lots of error replies due to server-generated presence.
b8c03df6e8ca mod_blocklist: Mention issue numbers
Kim Alvefur <zash@zash.se>
parents: 7620
diff changeset
318 -- This will likely need changes to mod_presence
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 module:hook("pre-presence/bare", drop_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 module:hook("pre-presence/full", drop_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 module:hook("pre-presence/host", drop_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 module:hook("pre-iq/bare", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 module:hook("pre-iq/full", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 module:hook("pre-iq/host", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326