Comparison

plugins/mod_blocklist.lua @ 6833:aeb088bb1a20

mod_blocklist: Explicitly halt event propagation after returning a reply (send returns nil sometimes)
author Kim Alvefur <zash@zash.se>
date Wed, 16 Sep 2015 15:16:51 +0200
parent 6629:42aeb882b3e1
child 6944:62b6f6d230f1
comparison
equal deleted inserted replaced
6832:9566a15d3e59 6833:aeb088bb1a20
93 if jid then 93 if jid then
94 reply:tag("item", { jid = jid }):up(); 94 reply:tag("item", { jid = jid }):up();
95 end 95 end
96 end 96 end
97 origin.interested_blocklist = true; -- Gets notified about changes 97 origin.interested_blocklist = true; -- Gets notified about changes
98 return origin.send(reply); 98 origin.send(reply);
99 return true;
99 end); 100 end);
100 101
101 -- Add or remove some jid(s) from the blocklist 102 -- Add or remove some jid(s) from the blocklist
102 -- We want this to be atomic and not do a partial update 103 -- We want this to be atomic and not do a partial update
103 local function edit_blocklist(event) 104 local function edit_blocklist(event)
107 local new = {}; 108 local new = {};
108 109
109 for item in action:childtags("item") do 110 for item in action:childtags("item") do
110 local jid = jid_prep(item.attr.jid); 111 local jid = jid_prep(item.attr.jid);
111 if not jid then 112 if not jid then
112 return origin.send(st_error_reply(stanza, "modify", "jid-malformed")); 113 origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
114 return true;
113 end 115 end
114 item.attr.jid = jid; -- echo back prepped 116 item.attr.jid = jid; -- echo back prepped
115 new[jid] = is_contact_subscribed(username, module.host, jid) or false; 117 new[jid] = is_contact_subscribed(username, module.host, jid) or false;
116 end 118 end
117 119
118 local mode = action.name == "block" or nil; 120 local mode = action.name == "block" or nil;
119 121
120 if mode and not next(new) then 122 if mode and not next(new) then
121 -- <block/> element does not contain at least one <item/> child element 123 -- <block/> element does not contain at least one <item/> child element
122 return origin.send(st_error_reply(stanza, "modify", "bad-request")); 124 origin.send(st_error_reply(stanza, "modify", "bad-request"));
125 return true;
123 end 126 end
124 127
125 local blocklist = get_blocklist(username); 128 local blocklist = get_blocklist(username);
126 129
127 local new_blocklist = {}; 130 local new_blocklist = {};
139 142
140 local ok, err = set_blocklist(username, new_blocklist); 143 local ok, err = set_blocklist(username, new_blocklist);
141 if ok then 144 if ok then
142 origin.send(st.reply(stanza)); 145 origin.send(st.reply(stanza));
143 else 146 else
144 return origin.send(st_error_reply(stanza, "wait", "internal-server-error", err)); 147 origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
148 return true;
145 end 149 end
146 150
147 if mode then 151 if mode then
148 for jid, in_roster in pairs(new) do 152 for jid, in_roster in pairs(new) do
149 if not blocklist[jid] and in_roster and sessions[username] then 153 if not blocklist[jid] and in_roster and sessions[username] then
206 end 210 end
207 211
208 local function bounce_stanza(event) 212 local function bounce_stanza(event)
209 local origin, stanza = event.origin, event.stanza; 213 local origin, stanza = event.origin, event.stanza;
210 if drop_stanza(event) then 214 if drop_stanza(event) then
211 return origin.send(st_error_reply(stanza, "cancel", "service-unavailable")); 215 origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
216 return true;
212 end 217 end
213 end 218 end
214 219
215 local function bounce_iq(event) 220 local function bounce_iq(event)
216 local type = event.stanza.attr.type; 221 local type = event.stanza.attr.type;
242 local type = stanza.attr.type; 247 local type = stanza.attr.type;
243 if type == "error" or stanza.name == "iq" and type == "result" then 248 if type == "error" or stanza.name == "iq" and type == "result" then
244 return drop_outgoing(event); 249 return drop_outgoing(event);
245 end 250 end
246 if drop_outgoing(event) then 251 if drop_outgoing(event) then
247 return origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID") 252 origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
248 :tag("blocked", { xmlns = "urn:xmpp:blocking:errors" })); 253 :tag("blocked", { xmlns = "urn:xmpp:blocking:errors" }));
254 return true;
249 end 255 end
250 end 256 end
251 257
252 -- Hook all the events! 258 -- Hook all the events!
253 local prio_in, prio_out = 100, 100; 259 local prio_in, prio_out = 100, 100;