# HG changeset patch # User Kim Alvefur # Date 1504451610 -7200 # Node ID 8ea0484871e88c9ddd66ee512614198fb0aed32d # Parent 6de741ada3cdd7d61d4538ed6a3c9b86d84501cf mod_mam: Factor out stripping so it can be reused in two places diff -r 6de741ada3cd -r 8ea0484871e8 plugins/mod_mam/mod_mam.lua --- a/plugins/mod_mam/mod_mam.lua Sun Sep 03 12:42:25 2017 +0200 +++ b/plugins/mod_mam/mod_mam.lua Sun Sep 03 17:13:30 2017 +0200 @@ -228,6 +228,22 @@ return default; end +local function strip_stanza_id(stanza, user) + if stanza:get_child("stanza-id", xmlns_st_id) then + stanza = st.clone(stanza); + stanza:maptags(function (tag) + if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id then + local by_user, by_host, res = jid_prepped_split(tag.attr.by); + if not res and by_host == host and by_user == user then + return nil; + end + end + return tag; + end); + end + return stanza; +end + -- Handle messages local function message_handler(event, c2s) local origin, stanza = event.origin, event.stanza; @@ -243,19 +259,7 @@ local with = jid_bare(c2s and orig_to or orig_from); -- Filter out that claim to be from us - if stanza:get_child("stanza-id", xmlns_st_id) then - stanza = st.clone(stanza); - stanza:maptags(function (tag) - if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id then - local by_user, by_host, res = jid_prepped_split(tag.attr.by); - if not res and by_host == module.host and by_user == store_user then - return nil; - end - end - return tag; - end); - event.stanza = stanza; - end + event.stanza = strip_stanza_id(stanza, store_user); -- We store chat messages or normal messages that have a body if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then @@ -312,18 +316,13 @@ return message_handler(event, true); end -local function strip_stanza_id(event) - local strip_by = jid_bare(event.origin.full_jid); - event.stanza = st.clone(event.stanza); - event.stanza:maptags(function(tag) - if not ( tag.attr.xmlns == xmlns_st_id and tag.attr.by == strip_by ) then - return tag; - end - end); +-- Filter out before the message leaves the server to prevent privacy leak. +local function strip_stanza_id_after_other_events(event) + event.stanza = strip_stanza_id(event.stanza, event.origin.username); end -module:hook("pre-message/bare", strip_stanza_id, -1); -module:hook("pre-message/full", strip_stanza_id, -1); +module:hook("pre-message/bare", strip_stanza_id_after_other_events, -1); +module:hook("pre-message/full", strip_stanza_id_after_other_events, -1); local cleanup_after = module:get_option_string("archive_expires_after", "1w"); local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60);