Comparison

plugins/mod_mam/mod_mam.lua @ 8193:bb0118e46c45

mod_mam: Clone stanzas before mutating (thanks waqas) (fixes #961)
author Kim Alvefur <zash@zash.se>
date Tue, 25 Jul 2017 22:01:16 +0200
parent 8172:66e32c34250b
child 8206:6de741ada3cd
comparison
equal deleted inserted replaced
8192:4354f556c5db 8193:bb0118e46c45
241 local store_user = c2s and origin.username or jid_split(orig_to); 241 local store_user = c2s and origin.username or jid_split(orig_to);
242 -- And who are they chatting with? 242 -- And who are they chatting with?
243 local with = jid_bare(c2s and orig_to or orig_from); 243 local with = jid_bare(c2s and orig_to or orig_from);
244 244
245 -- Filter out <stanza-id> that claim to be from us 245 -- Filter out <stanza-id> that claim to be from us
246 stanza:maptags(function (tag) 246 if stanza:get_child("stanza-id", xmlns_st_id) then
247 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id then 247 stanza = st.clone(stanza);
248 local by_user, by_host, res = jid_prepped_split(tag.attr.by); 248 stanza:maptags(function (tag)
249 if not res and by_host == module.host and by_user == store_user then 249 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id then
250 return nil; 250 local by_user, by_host, res = jid_prepped_split(tag.attr.by);
251 if not res and by_host == module.host and by_user == store_user then
252 return nil;
253 end
251 end 254 end
252 end 255 return tag;
253 return tag; 256 end);
254 end); 257 event.stanza = stanza;
258 end
255 259
256 -- We store chat messages or normal messages that have a body 260 -- We store chat messages or normal messages that have a body
257 if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then 261 if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then
258 log("debug", "Not archiving stanza: %s (type)", stanza:top_tag()); 262 log("debug", "Not archiving stanza: %s (type)", stanza:top_tag());
259 return; 263 return;
266 log("debug", "Not archiving stanza: %s (hint)", stanza:top_tag()); 270 log("debug", "Not archiving stanza: %s (hint)", stanza:top_tag());
267 return; 271 return;
268 end 272 end
269 end 273 end
270 274
275 local clone_for_storage;
271 if not strip_tags:empty() then 276 if not strip_tags:empty() then
272 stanza = st.clone(stanza); 277 clone_for_storage = st.clone(stanza);
273 stanza:maptags(function (tag) 278 clone_for_storage:maptags(function (tag)
274 if strip_tags:contains(tag.attr.xmlns) then 279 if strip_tags:contains(tag.attr.xmlns) then
275 return nil; 280 return nil;
276 else 281 else
277 return tag; 282 return tag;
278 end 283 end
279 end); 284 end);
280 if #stanza.tags == 0 then 285 if #clone_for_storage.tags == 0 then
281 return; 286 return;
282 end 287 end
288 else
289 clone_for_storage = stanza;
283 end 290 end
284 291
285 -- Check with the users preferences 292 -- Check with the users preferences
286 if shall_store(store_user, with) then 293 if shall_store(store_user, with) then
287 log("debug", "Archiving stanza: %s", stanza:top_tag()); 294 log("debug", "Archiving stanza: %s", stanza:top_tag());
288 295
289 -- And stash it 296 -- And stash it
290 local ok = archive:append(store_user, nil, stanza, time_now(), with); 297 local ok = archive:append(store_user, nil, clone_for_storage, time_now(), with);
291 if ok then 298 if ok then
299 local clone_for_other_handlers = st.clone(stanza);
292 local id = ok; 300 local id = ok;
293 event.stanza:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up(); 301 clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up();
302 event.stanza = clone_for_other_handlers;
294 if cleanup then cleanup[store_user] = true; end 303 if cleanup then cleanup[store_user] = true; end
295 module:fire_event("archive-message-added", { origin = origin, stanza = stanza, for_user = store_user, id = id }); 304 module:fire_event("archive-message-added", { origin = origin, stanza = clone_for_storage, for_user = store_user, id = id });
296 end 305 end
297 else 306 else
298 log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag()); 307 log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag());
299 end 308 end
300 end 309 end