Software /
code /
prosody
Annotate
plugins/mod_blocklist.lua @ 6967:d90a4d6a0e2c
mod_blocklist: Clear second level cache when user is deleted
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 05 Dec 2015 22:46:50 +0100 |
parent | 6944:62b6f6d230f1 |
child | 6968:828a10e0464b |
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 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- Copyright (C) 2014 Kim Alvefur |
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; |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed; |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 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
|
15 local st_error_reply = st.error_reply; |
6629
42aeb882b3e1
mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6531
diff
changeset
|
16 local jid_prep = require"util.jid".prep; |
42aeb882b3e1
mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6531
diff
changeset
|
17 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
|
18 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local storage = module:open_store(); |
6629
42aeb882b3e1
mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6531
diff
changeset
|
20 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
|
21 |
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
|
22 -- Cache of blocklists by username may randomly expire at any time |
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
|
23 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
|
24 |
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
|
25 -- Second level of caching, keeps a fixed number of items, |
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
|
26 -- also anchors items in the above cache |
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
|
27 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
|
28 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
|
29 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 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
|
31 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 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
|
33 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 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
|
35 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
|
36 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
|
37 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
|
38 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 -- 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
|
40 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
|
41 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
|
42 return true; |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 -- 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 else |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 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
|
54 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 else |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 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
|
66 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 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
|
71 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
|
72 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 if not blocklist then |
6629
42aeb882b3e1
mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6531
diff
changeset
|
80 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
|
81 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
|
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 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 return blocklist; |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
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 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 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
|
104 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
|
105 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
|
106 end); |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 |
6351 | 108 -- 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
|
109 -- 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
|
110 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
|
111 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
|
112 local username = origin.username; |
6352
b703e6930e4c
mod_blocklist: Use full word as variable name, we can afford that
Kim Alvefur <zash@zash.se>
parents:
6351
diff
changeset
|
113 local action = stanza.tags[1]; |
6344
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 local new = {}; |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 |
6352
b703e6930e4c
mod_blocklist: Use full word as variable name, we can afford that
Kim Alvefur <zash@zash.se>
parents:
6351
diff
changeset
|
116 for item in action:childtags("item") do |
6629
42aeb882b3e1
mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6531
diff
changeset
|
117 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
|
118 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
|
119 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
|
120 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
|
121 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 item.attr.jid = jid; -- echo back prepped |
6629
42aeb882b3e1
mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6531
diff
changeset
|
123 new[jid] = is_contact_subscribed(username, module.host, jid) or false; |
6344
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 |
6352
b703e6930e4c
mod_blocklist: Use full word as variable name, we can afford that
Kim Alvefur <zash@zash.se>
parents:
6351
diff
changeset
|
126 local mode = action.name == "block" or nil; |
6344
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 if mode and not next(new) then |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 -- <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
|
130 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
|
131 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
|
132 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 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
|
135 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 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
|
137 |
6350
bba5f4ffe75a
mod_blocklist: Fix any unblock emptying the blocklist
Kim Alvefur <zash@zash.se>
parents:
6344
diff
changeset
|
138 if mode 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
|
139 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
|
140 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
|
141 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 for jid in pairs(new) do |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 new_blocklist[jid] = mode; |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 -- 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
|
146 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 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
|
148 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 if mode then |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 for jid, in_roster in pairs(new) do |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 if not blocklist[jid] and in_roster and sessions[username] then |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 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
|
161 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
|
162 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
|
163 end |
6344
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 if sessions[username] then |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 local blocklist_push = st.iq({ type = "set", id = "blocklist-push" }) |
6352
b703e6930e4c
mod_blocklist: Use full word as variable name, we can afford that
Kim Alvefur <zash@zash.se>
parents:
6351
diff
changeset
|
170 :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
|
171 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 for _, session in pairs(sessions[username].sessions) do |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 if session.interested_blocklist then |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 blocklist_push.attr.to = session.full_jid; |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 session.send(blocklist_push); |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 return true; |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist); |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist); |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 -- 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
|
187 module:hook_global("user-deleted", function (event) |
6629
42aeb882b3e1
mod_blocklist: Some cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6531
diff
changeset
|
188 if event.host == module.host then |
6967
d90a4d6a0e2c
mod_blocklist: Clear second level cache when user is deleted
Kim Alvefur <zash@zash.se>
parents:
6944
diff
changeset
|
189 cache: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
|
190 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
|
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 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 -- Buggy clients |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 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
|
196 local _, condition, text = event.stanza:get_error(); |
6460
6d3187f24608
mod_blocklist: Capitalize log message
Kim Alvefur <zash@zash.se>
parents:
6352
diff
changeset
|
197 (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
|
198 return true; |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 end); |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 -- 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 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
|
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 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 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
|
228 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
|
229 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
|
230 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
|
231 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 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
|
233 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 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
|
236 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
|
237 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
|
238 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
|
239 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 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
|
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 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 -- 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
|
250 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 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
|
259 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
|
260 :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
|
261 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
|
262 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 end |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 -- 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
|
266 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
|
267 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
|
268 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
|
269 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 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
|
271 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
|
272 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 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
|
274 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
|
275 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 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
|
277 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
|
278 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
|
279 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 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
|
281 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
|
282 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
|
283 |
68b5c1ed18dd
mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
284 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
|
285 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
|
286 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
|
287 |