Comparison

plugins/mod_blocklist.lua @ 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)
author Kim Alvefur <zash@zash.se>
date Sun, 06 Dec 2015 02:30:21 +0100
parent 6973:f350f840a6f7
child 6975:5bc229eb99d3
comparison
equal deleted inserted replaced
6973:f350f840a6f7 6974:bdb216e0688a
8 -- 8 --
9 -- This module implements XEP-0191: Blocking Command 9 -- This module implements XEP-0191: Blocking Command
10 -- 10 --
11 11
12 local user_exists = require"core.usermanager".user_exists; 12 local user_exists = require"core.usermanager".user_exists;
13 local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed; 13 local rostermanager = require"core.rostermanager";
14 local is_contact_subscribed = rostermanager.is_contact_subscribed;
15 local is_contact_pending_in = rostermanager.is_contact_pending_in;
16 local load_roster = rostermanager.load_roster;
17 local save_roster = rostermanager.save_roster;
14 local st = require"util.stanza"; 18 local st = require"util.stanza";
15 local st_error_reply = st.error_reply; 19 local st_error_reply = st.error_reply;
16 local jid_prep = require"util.jid".prep; 20 local jid_prep = require"util.jid".prep;
17 local jid_split = require"util.jid".split; 21 local jid_split = require"util.jid".split;
18 22
124 -- > server MUST send unavailable presence information to the contact (but 128 -- > server MUST send unavailable presence information to the contact (but
125 -- > only if the contact is allowed to receive presence notifications [...] 129 -- > only if the contact is allowed to receive presence notifications [...]
126 -- So contacts we need to do that for are added to the set below. 130 -- So contacts we need to do that for are added to the set below.
127 local send_unavailable = {}; 131 local send_unavailable = {};
128 132
133 -- Because blocking someone currently also blocks the ability to reject
134 -- subscription requests, we'll preemptively reject such
135 local remove_pending = {};
136
129 for item in action:childtags("item") do 137 for item in action:childtags("item") do
130 local jid = jid_prep(item.attr.jid); 138 local jid = jid_prep(item.attr.jid);
131 if not jid then 139 if not jid then
132 origin.send(st_error_reply(stanza, "modify", "jid-malformed")); 140 origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
133 return true; 141 return true;
134 end 142 end
135 item.attr.jid = jid; -- echo back prepped 143 item.attr.jid = jid; -- echo back prepped
136 new[jid] = true; 144 new[jid] = true;
137 if is_contact_subscribed(username, module.host, jid) then 145 if is_contact_subscribed(username, module.host, jid) then
138 send_unavailable[jid] = true; 146 send_unavailable[jid] = true;
147 elseif is_contact_pending_in(username, module.host, jid) then
148 remove_pending[jid] = true;
139 end 149 end
140 end 150 end
141 151
142 local is_blocking = action.name == "block" or nil; -- nil if unblocking 152 local is_blocking = action.name == "block" or nil; -- nil if unblocking
143 153
178 module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid })); 188 module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
179 end 189 end
180 end 190 end
181 end 191 end
182 end 192 end
193
194 if next(remove_pending) then
195 local roster = load_roster(username, module.host);
196 for jid in pairs(remove_pending) do
197 roster[false].pending[jid] = nil;
198 end
199 save_roster(username, module.host, roster);
200 -- Not much we can do about save failing here
201 end
183 end 202 end
184 203
185 local blocklist_push = st.iq({ type = "set", id = "blocklist-push" }) 204 local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
186 :add_child(action); -- I am lazy 205 :add_child(action); -- I am lazy
187 206