Mercurial > prosody-hg
comparison plugins/mod_blocklist.lua @ 6975:5bc229eb99d3
mod_blocklist: Skip creating some tables and some processing if unblocking
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 06 Dec 2015 02:32:16 +0100 |
| parents | bdb216e0688a |
| children | 4688ff9d4f2b |
comparison
equal
deleted
inserted
replaced
| 6974:bdb216e0688a | 6975:5bc229eb99d3 |
|---|---|
| 119 -- We want this to be atomic and not do a partial update | 119 -- We want this to be atomic and not do a partial update |
| 120 local function edit_blocklist(event) | 120 local function edit_blocklist(event) |
| 121 local origin, stanza = event.origin, event.stanza; | 121 local origin, stanza = event.origin, event.stanza; |
| 122 local username = origin.username; | 122 local username = origin.username; |
| 123 local action = stanza.tags[1]; -- "block" or "unblock" | 123 local action = stanza.tags[1]; -- "block" or "unblock" |
| 124 local is_blocking = action.name == "block" or nil; -- nil if unblocking | |
| 124 local new = {}; -- JIDs to block depending or unblock on action | 125 local new = {}; -- JIDs to block depending or unblock on action |
| 125 | 126 |
| 126 -- XEP-0191 sayeth: | 127 -- XEP-0191 sayeth: |
| 127 -- > When the user blocks communications with the contact, the user's | 128 -- > When the user blocks communications with the contact, the user's |
| 128 -- > server MUST send unavailable presence information to the contact (but | 129 -- > server MUST send unavailable presence information to the contact (but |
| 129 -- > only if the contact is allowed to receive presence notifications [...] | 130 -- > only if the contact is allowed to receive presence notifications [...] |
| 130 -- So contacts we need to do that for are added to the set below. | 131 -- So contacts we need to do that for are added to the set below. |
| 131 local send_unavailable = {}; | 132 local send_unavailable = is_blocking and {}; |
| 132 | 133 |
| 133 -- Because blocking someone currently also blocks the ability to reject | 134 -- Because blocking someone currently also blocks the ability to reject |
| 134 -- subscription requests, we'll preemptively reject such | 135 -- subscription requests, we'll preemptively reject such |
| 135 local remove_pending = {}; | 136 local remove_pending = is_blocking and {}; |
| 136 | 137 |
| 137 for item in action:childtags("item") do | 138 for item in action:childtags("item") do |
| 138 local jid = jid_prep(item.attr.jid); | 139 local jid = jid_prep(item.attr.jid); |
| 139 if not jid then | 140 if not jid then |
| 140 origin.send(st_error_reply(stanza, "modify", "jid-malformed")); | 141 origin.send(st_error_reply(stanza, "modify", "jid-malformed")); |
| 141 return true; | 142 return true; |
| 142 end | 143 end |
| 143 item.attr.jid = jid; -- echo back prepped | 144 item.attr.jid = jid; -- echo back prepped |
| 144 new[jid] = true; | 145 new[jid] = true; |
| 145 if is_contact_subscribed(username, module.host, jid) then | 146 if is_blocking then |
| 146 send_unavailable[jid] = true; | 147 if is_contact_subscribed(username, module.host, jid) then |
| 147 elseif is_contact_pending_in(username, module.host, jid) then | 148 send_unavailable[jid] = true; |
| 148 remove_pending[jid] = true; | 149 elseif is_contact_pending_in(username, module.host, jid) then |
| 149 end | 150 remove_pending[jid] = true; |
| 150 end | 151 end |
| 151 | 152 end |
| 152 local is_blocking = action.name == "block" or nil; -- nil if unblocking | 153 end |
| 153 | 154 |
| 154 if is_blocking and not next(new) then | 155 if is_blocking and not next(new) then |
| 155 -- <block/> element does not contain at least one <item/> child element | 156 -- <block/> element does not contain at least one <item/> child element |
| 156 origin.send(st_error_reply(stanza, "modify", "bad-request")); | 157 origin.send(st_error_reply(stanza, "modify", "bad-request")); |
| 157 return true; | 158 return true; |
