Software / code / prosody
Comparison
plugins/mod_carbons.lua @ 10779:d95e083931d1
mod_carbons: Refactor in new style (mod_mam/csi)
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 26 Apr 2020 20:17:43 +0200 |
| parent | 10778:a62b981db0e2 |
| child | 10780:22bbc644c5eb |
comparison
equal
deleted
inserted
replaced
| 10778:a62b981db0e2 | 10779:d95e083931d1 |
|---|---|
| 18 return true; | 18 return true; |
| 19 end | 19 end |
| 20 module:hook("iq-set/self/"..xmlns_carbons..":disable", toggle_carbons); | 20 module:hook("iq-set/self/"..xmlns_carbons..":disable", toggle_carbons); |
| 21 module:hook("iq-set/self/"..xmlns_carbons..":enable", toggle_carbons); | 21 module:hook("iq-set/self/"..xmlns_carbons..":enable", toggle_carbons); |
| 22 | 22 |
| 23 local function should_copy(stanza, c2s, user_bare) | |
| 24 local st_type = stanza.attr.type or "normal"; | |
| 25 if stanza:get_child("private", xmlns_carbons) then | |
| 26 return false, "private"; | |
| 27 end | |
| 28 | |
| 29 if stanza:get_child("no-copy", "urn:xmpp:hints") then | |
| 30 return false, "hint"; | |
| 31 end | |
| 32 | |
| 33 if not c2s and and stanza.attr.to ~= user_bare and stanza:get_child("x", "http://jabber.org/protocol/muc#user") then | |
| 34 -- MUC PMs are normally sent to full JIDs | |
| 35 return false, "muc-pm"; | |
| 36 end | |
| 37 | |
| 38 if st_type == "chat" then | |
| 39 return true, "type"; | |
| 40 end | |
| 41 | |
| 42 if st_type == "normal" and stanza:get_child("body") then | |
| 43 return true, "type"; | |
| 44 end | |
| 45 | |
| 46 return false, "default"; | |
| 47 end | |
| 48 | |
| 23 local function message_handler(event, c2s) | 49 local function message_handler(event, c2s) |
| 24 local origin, stanza = event.origin, event.stanza; | 50 local origin, stanza = event.origin, event.stanza; |
| 25 local orig_type = stanza.attr.type or "normal"; | 51 local orig_type = stanza.attr.type or "normal"; |
| 26 local orig_from = stanza.attr.from; | 52 local orig_from = stanza.attr.from; |
| 27 local bare_from = jid_bare(orig_from); | 53 local bare_from = jid_bare(orig_from); |
| 28 local orig_to = stanza.attr.to; | 54 local orig_to = stanza.attr.to; |
| 29 local bare_to = jid_bare(orig_to); | 55 local bare_to = jid_bare(orig_to); |
| 30 | |
| 31 if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body"))) then | |
| 32 return -- Only chat type messages | |
| 33 end | |
| 34 | 56 |
| 35 -- Stanza sent by a local client | 57 -- Stanza sent by a local client |
| 36 local bare_jid = bare_from; -- JID of the local user | 58 local bare_jid = bare_from; -- JID of the local user |
| 37 local target_session = origin; | 59 local target_session = origin; |
| 38 local top_priority = false; | 60 local top_priority = false; |
| 54 if not user_sessions then | 76 if not user_sessions then |
| 55 module:log("debug", "Skip carbons for offline user"); | 77 module:log("debug", "Skip carbons for offline user"); |
| 56 return -- No use in sending carbons to an offline user | 78 return -- No use in sending carbons to an offline user |
| 57 end | 79 end |
| 58 | 80 |
| 59 if stanza:get_child("private", xmlns_carbons) then | 81 local should, why = should_copy(stanza, c2s, bare_jid); |
| 60 if not c2s then | 82 |
| 83 if not should then | |
| 84 module:log("debug", "Not copying stanza: %s (%s)", stanza:top_tag(), why); | |
| 85 elseif why == "private" and not c2s then | |
| 61 stanza:maptags(function(tag) | 86 stanza:maptags(function(tag) |
| 62 if not ( tag.attr.xmlns == xmlns_carbons and tag.name == "private" ) then | 87 if not ( tag.attr.xmlns == xmlns_carbons and tag.name == "private" ) then |
| 63 return tag; | 88 return tag; |
| 64 end | 89 end |
| 65 end); | 90 end); |
| 66 end | |
| 67 module:log("debug", "Message tagged private, ignoring"); | |
| 68 return | |
| 69 elseif stanza:get_child("no-copy", "urn:xmpp:hints") then | |
| 70 module:log("debug", "Message has no-copy hint, ignoring"); | |
| 71 return | |
| 72 elseif not c2s and bare_jid ~= orig_to and stanza:get_child("x", "http://jabber.org/protocol/muc#user") then | |
| 73 module:log("debug", "MUC PM, ignoring"); | |
| 74 return | |
| 75 end | 91 end |
| 76 | 92 |
| 77 local carbon; | 93 local carbon; |
| 78 user_sessions = user_sessions and user_sessions.sessions; | 94 user_sessions = user_sessions and user_sessions.sessions; |
| 79 for _, session in pairs(user_sessions) do | 95 for _, session in pairs(user_sessions) do |