Software / code / prosody
Comparison
plugins/mod_mam/mod_mam.lua @ 8207:8ea0484871e8
mod_mam: Factor out <stanza-id> stripping so it can be reused in two places
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 03 Sep 2017 17:13:30 +0200 |
| parent | 8206:6de741ada3cd |
| child | 8230:154852646095 |
comparison
equal
deleted
inserted
replaced
| 8206:6de741ada3cd | 8207:8ea0484871e8 |
|---|---|
| 226 return has_in_roster(user, who); | 226 return has_in_roster(user, who); |
| 227 end | 227 end |
| 228 return default; | 228 return default; |
| 229 end | 229 end |
| 230 | 230 |
| 231 local function strip_stanza_id(stanza, user) | |
| 232 if stanza:get_child("stanza-id", xmlns_st_id) then | |
| 233 stanza = st.clone(stanza); | |
| 234 stanza:maptags(function (tag) | |
| 235 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id then | |
| 236 local by_user, by_host, res = jid_prepped_split(tag.attr.by); | |
| 237 if not res and by_host == host and by_user == user then | |
| 238 return nil; | |
| 239 end | |
| 240 end | |
| 241 return tag; | |
| 242 end); | |
| 243 end | |
| 244 return stanza; | |
| 245 end | |
| 246 | |
| 231 -- Handle messages | 247 -- Handle messages |
| 232 local function message_handler(event, c2s) | 248 local function message_handler(event, c2s) |
| 233 local origin, stanza = event.origin, event.stanza; | 249 local origin, stanza = event.origin, event.stanza; |
| 234 local log = c2s and origin.log or module._log; | 250 local log = c2s and origin.log or module._log; |
| 235 local orig_type = stanza.attr.type or "normal"; | 251 local orig_type = stanza.attr.type or "normal"; |
| 241 local store_user = c2s and origin.username or jid_split(orig_to); | 257 local store_user = c2s and origin.username or jid_split(orig_to); |
| 242 -- And who are they chatting with? | 258 -- And who are they chatting with? |
| 243 local with = jid_bare(c2s and orig_to or orig_from); | 259 local with = jid_bare(c2s and orig_to or orig_from); |
| 244 | 260 |
| 245 -- Filter out <stanza-id> that claim to be from us | 261 -- Filter out <stanza-id> that claim to be from us |
| 246 if stanza:get_child("stanza-id", xmlns_st_id) then | 262 event.stanza = strip_stanza_id(stanza, store_user); |
| 247 stanza = st.clone(stanza); | |
| 248 stanza:maptags(function (tag) | |
| 249 if tag.name == "stanza-id" and tag.attr.xmlns == xmlns_st_id then | |
| 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 | |
| 254 end | |
| 255 return tag; | |
| 256 end); | |
| 257 event.stanza = stanza; | |
| 258 end | |
| 259 | 263 |
| 260 -- We store chat messages or normal messages that have a body | 264 -- We store chat messages or normal messages that have a body |
| 261 if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then | 265 if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then |
| 262 log("debug", "Not archiving stanza: %s (type)", stanza:top_tag()); | 266 log("debug", "Not archiving stanza: %s (type)", stanza:top_tag()); |
| 263 return; | 267 return; |
| 310 | 314 |
| 311 local function c2s_message_handler(event) | 315 local function c2s_message_handler(event) |
| 312 return message_handler(event, true); | 316 return message_handler(event, true); |
| 313 end | 317 end |
| 314 | 318 |
| 315 local function strip_stanza_id(event) | 319 -- Filter out <stanza-id> before the message leaves the server to prevent privacy leak. |
| 316 local strip_by = jid_bare(event.origin.full_jid); | 320 local function strip_stanza_id_after_other_events(event) |
| 317 event.stanza = st.clone(event.stanza); | 321 event.stanza = strip_stanza_id(event.stanza, event.origin.username); |
| 318 event.stanza:maptags(function(tag) | 322 end |
| 319 if not ( tag.attr.xmlns == xmlns_st_id and tag.attr.by == strip_by ) then | 323 |
| 320 return tag; | 324 module:hook("pre-message/bare", strip_stanza_id_after_other_events, -1); |
| 321 end | 325 module:hook("pre-message/full", strip_stanza_id_after_other_events, -1); |
| 322 end); | |
| 323 end | |
| 324 | |
| 325 module:hook("pre-message/bare", strip_stanza_id, -1); | |
| 326 module:hook("pre-message/full", strip_stanza_id, -1); | |
| 327 | 326 |
| 328 local cleanup_after = module:get_option_string("archive_expires_after", "1w"); | 327 local cleanup_after = module:get_option_string("archive_expires_after", "1w"); |
| 329 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60); | 328 local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60); |
| 330 if cleanup_after ~= "never" then | 329 if cleanup_after ~= "never" then |
| 331 local day = 86400; | 330 local day = 86400; |