Mercurial > prosody-hg
comparison plugins/mod_blocklist.lua @ 7780:dbd202e7c587
Merge 0.10->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 10 Dec 2016 02:55:38 +0100 |
| parents | 3733bdbe0b22 |
| children | f0e35f4db9e0 |
comparison
equal
deleted
inserted
replaced
| 7768:57d0f2d3d5c5 | 7780:dbd202e7c587 |
|---|---|
| 52 return true; | 52 return true; |
| 53 end | 53 end |
| 54 | 54 |
| 55 -- Migrates from the old mod_privacy storage | 55 -- Migrates from the old mod_privacy storage |
| 56 local function migrate_privacy_list(username) | 56 local function migrate_privacy_list(username) |
| 57 local migrated_data = { [false] = "not empty" }; | |
| 58 local legacy_data = module:open_store("privacy"):get(username); | 57 local legacy_data = module:open_store("privacy"):get(username); |
| 59 if legacy_data and legacy_data.lists and legacy_data.default then | 58 if not legacy_data or not legacy_data.lists or not legacy_data.default then return; end |
| 60 legacy_data = legacy_data.lists[legacy_data.default]; | 59 local default_list = legacy_data.lists[legacy_data.default]; |
| 61 legacy_data = legacy_data and legacy_data.items; | 60 if not default_list or not default_list.items then return; end |
| 62 else | 61 |
| 63 return migrated_data; | 62 local migrated_data = { [false] = { created = os.time(); migrated = "privacy" }}; |
| 64 end | 63 |
| 65 if legacy_data then | 64 module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username); |
| 66 module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username); | 65 for _, item in ipairs(default_list.items) do |
| 67 local item, jid; | 66 if item.type == "jid" and item.action == "deny" then |
| 68 for i = 1, #legacy_data do | 67 local jid = jid_prep(item.value); |
| 69 item = legacy_data[i]; | 68 if not jid then |
| 70 if item.type == "jid" and item.action == "deny" then | 69 module:log("warn", "Invalid JID in privacy store for user '%s' not migrated: %s", username, tostring(item.value)); |
| 71 jid = jid_prep(item.value); | 70 else |
| 72 if not jid then | 71 migrated_data[jid] = true; |
| 73 module:log("warn", "Invalid JID in privacy store for user '%s' not migrated: %s", username, tostring(item.value)); | |
| 74 else | |
| 75 migrated_data[jid] = true; | |
| 76 end | |
| 77 end | 72 end |
| 78 end | 73 end |
| 79 end | 74 end |
| 80 set_blocklist(username, migrated_data); | 75 set_blocklist(username, migrated_data); |
| 81 return migrated_data; | 76 return migrated_data; |
| 82 end | 77 end |
| 83 | 78 |
| 84 local function get_blocklist(username) | 79 local function get_blocklist(username) |
| 85 local blocklist = cache[username]; | 80 local blocklist = cache2:get(username); |
| 86 if not blocklist then | |
| 87 blocklist = cache2:get(username); | |
| 88 end | |
| 89 if not blocklist then | 81 if not blocklist then |
| 90 if not user_exists(username, module.host) then | 82 if not user_exists(username, module.host) then |
| 91 return null_blocklist; | 83 return null_blocklist; |
| 92 end | 84 end |
| 93 blocklist = storage:get(username); | 85 blocklist = storage:get(username); |
| 94 if not blocklist then | 86 if not blocklist then |
| 95 blocklist = migrate_privacy_list(username); | 87 blocklist = migrate_privacy_list(username); |
| 96 end | 88 end |
| 89 if not blocklist then | |
| 90 blocklist = { [false] = { created = os.time(); }; }; | |
| 91 end | |
| 97 cache2:set(username, blocklist); | 92 cache2:set(username, blocklist); |
| 98 end | 93 end |
| 99 cache[username] = blocklist; | 94 cache[username] = blocklist; |
| 100 return blocklist; | 95 return blocklist; |
| 101 end | 96 end |
| 102 | 97 |
| 103 module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event) | 98 module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event) |
| 104 local origin, stanza = event.origin, event.stanza; | 99 local origin, stanza = event.origin, event.stanza; |
| 105 local username = origin.username; | 100 local username = origin.username; |
| 106 local reply = st.reply(stanza):tag("blocklist", { xmlns = "urn:xmpp:blocking" }); | 101 local reply = st.reply(stanza):tag("blocklist", { xmlns = "urn:xmpp:blocking" }); |
| 107 local blocklist = get_blocklist(username); | 102 local blocklist = cache[username] or get_blocklist(username); |
| 108 for jid in pairs(blocklist) do | 103 for jid in pairs(blocklist) do |
| 109 if jid then | 104 if jid then |
| 110 reply:tag("item", { jid = jid }):up(); | 105 reply:tag("item", { jid = jid }):up(); |
| 111 end | 106 end |
| 112 end | 107 end |
| 156 -- <block/> element does not contain at least one <item/> child element | 151 -- <block/> element does not contain at least one <item/> child element |
| 157 origin.send(st_error_reply(stanza, "modify", "bad-request")); | 152 origin.send(st_error_reply(stanza, "modify", "bad-request")); |
| 158 return true; | 153 return true; |
| 159 end | 154 end |
| 160 | 155 |
| 161 local blocklist = get_blocklist(username); | 156 local blocklist = cache[username] or get_blocklist(username); |
| 162 | 157 |
| 163 local new_blocklist = {}; | 158 local new_blocklist = { |
| 159 -- We set the [false] key to someting as a signal not to migrate privacy lists | |
| 160 [false] = blocklist[false] or { created = os.time(); }; | |
| 161 }; | |
| 162 if type(blocklist[false]) == "table" then | |
| 163 new_blocklist[false].modified = os.time(); | |
| 164 end | |
| 164 | 165 |
| 165 if is_blocking or next(new) then | 166 if is_blocking or next(new) then |
| 166 for jid in pairs(blocklist) do | 167 for jid in pairs(blocklist) do |
| 167 new_blocklist[jid] = true; | 168 if jid then new_blocklist[jid] = true; end |
| 168 end | 169 end |
| 169 for jid in pairs(new) do | 170 for jid in pairs(new) do |
| 170 new_blocklist[jid] = is_blocking; | 171 new_blocklist[jid] = is_blocking; |
| 171 end | 172 end |
| 172 -- else empty the blocklist | 173 -- else empty the blocklist |
| 173 end | 174 end |
| 174 new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice | |
| 175 | 175 |
| 176 local ok, err = set_blocklist(username, new_blocklist); | 176 local ok, err = set_blocklist(username, new_blocklist); |
| 177 if ok then | 177 if ok then |
| 178 origin.send(st.reply(stanza)); | 178 origin.send(st.reply(stanza)); |
| 179 else | 179 else |
