From c02ddf92ec53dd2c0473ef98427ceb4020d1f001 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 30 Nov 2019 19:34:40 +0100 Subject: mod_carbons: Improve performance by delaying creation of carbon payload If there are no other sessions which also enabled carbons then the carbons wrapper is not used and the potentially expensive clone operation was a waste of cycles. --- plugins/mod_carbons.lua | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'plugins/mod_carbons.lua') diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua index 1dcd4a07..0f8c7c60 100644 --- a/plugins/mod_carbons.lua +++ b/plugins/mod_carbons.lua @@ -74,17 +74,7 @@ local function message_handler(event, c2s) return end - -- Create the carbon copy and wrap it as per the Stanza Forwarding XEP - local copy = st.clone(stanza); - if c2s and not orig_to then - stanza.attr.to = bare_from; - end - copy.attr.xmlns = "jabber:client"; - local carbon = st.message{ from = bare_jid, type = orig_type, } - :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }) - :tag("forwarded", { xmlns = xmlns_forward }) - :add_child(copy):reset(); - + local carbon; user_sessions = user_sessions and user_sessions.sessions; for _, session in pairs(user_sessions) do -- Carbons are sent to resources that have enabled it @@ -93,6 +83,20 @@ local function message_handler(event, c2s) and session ~= target_session -- and isn't among the top resources that would receive the message per standard routing rules and (c2s or session.priority ~= top_priority) then + if not carbon then + -- Create the carbon copy and wrap it as per the Stanza Forwarding XEP + local copy = st.clone(stanza); + if c2s and not orig_to then + stanza.attr.to = bare_from; + end + copy.attr.xmlns = "jabber:client"; + carbon = st.message{ from = bare_jid, type = orig_type, } + :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }) + :tag("forwarded", { xmlns = xmlns_forward }) + :add_child(copy):reset(); + + end + carbon.attr.to = session.full_jid; module:log("debug", "Sending carbon to %s", session.full_jid); session.send(carbon); -- cgit v1.2.3 From fe36680fba1a9010a3a9004066a33c7b0612b124 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 26 Apr 2020 20:17:43 +0200 Subject: mod_carbons: Refactor in new style (mod_mam/csi) --- plugins/mod_carbons.lua | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'plugins/mod_carbons.lua') diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua index 5c17616c..dfd2b3a4 100644 --- a/plugins/mod_carbons.lua +++ b/plugins/mod_carbons.lua @@ -20,6 +20,32 @@ end module:hook("iq-set/self/"..xmlns_carbons..":disable", toggle_carbons); module:hook("iq-set/self/"..xmlns_carbons..":enable", toggle_carbons); +local function should_copy(stanza, c2s, user_bare) + local st_type = stanza.attr.type or "normal"; + if stanza:get_child("private", xmlns_carbons) then + return false, "private"; + end + + if stanza:get_child("no-copy", "urn:xmpp:hints") then + return false, "hint"; + end + + if not c2s and and stanza.attr.to ~= user_bare and stanza:get_child("x", "http://jabber.org/protocol/muc#user") then + -- MUC PMs are normally sent to full JIDs + return false, "muc-pm"; + end + + if st_type == "chat" then + return true, "type"; + end + + if st_type == "normal" and stanza:get_child("body") then + return true, "type"; + end + + return false, "default"; +end + local function message_handler(event, c2s) local origin, stanza = event.origin, event.stanza; local orig_type = stanza.attr.type or "normal"; @@ -28,10 +54,6 @@ local function message_handler(event, c2s) local orig_to = stanza.attr.to; local bare_to = jid_bare(orig_to); - if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body"))) then - return -- Only chat type messages - end - -- Stanza sent by a local client local bare_jid = bare_from; -- JID of the local user local target_session = origin; @@ -56,22 +78,16 @@ local function message_handler(event, c2s) return -- No use in sending carbons to an offline user end - if stanza:get_child("private", xmlns_carbons) then - if not c2s then + local should, why = should_copy(stanza, c2s, bare_jid); + + if not should then + module:log("debug", "Not copying stanza: %s (%s)", stanza:top_tag(), why); + elseif why == "private" and not c2s then stanza:maptags(function(tag) if not ( tag.attr.xmlns == xmlns_carbons and tag.name == "private" ) then return tag; end end); - end - module:log("debug", "Message tagged private, ignoring"); - return - elseif stanza:get_child("no-copy", "urn:xmpp:hints") then - module:log("debug", "Message has no-copy hint, ignoring"); - return - elseif not c2s and bare_jid ~= orig_to and stanza:get_child("x", "http://jabber.org/protocol/muc#user") then - module:log("debug", "MUC PM, ignoring"); - return end local carbon; -- cgit v1.2.3 From bec170ac73c3b7763cb66909c0301d11d316ff47 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 26 Apr 2020 21:11:00 +0200 Subject: mod_carbons: Fix syntax error [luacheck] --- plugins/mod_carbons.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/mod_carbons.lua') diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua index dfd2b3a4..09518bf7 100644 --- a/plugins/mod_carbons.lua +++ b/plugins/mod_carbons.lua @@ -30,7 +30,7 @@ local function should_copy(stanza, c2s, user_bare) return false, "hint"; end - if not c2s and and stanza.attr.to ~= user_bare and stanza:get_child("x", "http://jabber.org/protocol/muc#user") then + if not c2s and stanza.attr.to ~= user_bare and stanza:get_child("x", "http://jabber.org/protocol/muc#user") then -- MUC PMs are normally sent to full JIDs return false, "muc-pm"; end -- cgit v1.2.3 From 9e3ef1c01bc14f51a41e5eb413cb05118489bd7f Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 26 Apr 2020 20:23:50 +0200 Subject: mod_carbons: Carbon incoming message delivery failure reports Ensures that all your clients know about sent messages that failed. --- plugins/mod_carbons.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'plugins/mod_carbons.lua') diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua index 09518bf7..9befc9cb 100644 --- a/plugins/mod_carbons.lua +++ b/plugins/mod_carbons.lua @@ -43,6 +43,10 @@ local function should_copy(stanza, c2s, user_bare) return true, "type"; end + if st_type == "error" and not c2s and not (stanza.attr.from or ""):find("/") then + return true, "bounce"; + end + return false, "default"; end -- cgit v1.2.3 From 608f1d1c58ceac633a29036eb1a6b70103d2e0e0 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 26 Apr 2020 20:24:51 +0200 Subject: mod_carbons: Carbon anything that has been archived by the current user This ensures rules in mod_mam apply to some extent. Messages worth archiving are probably worth sending to other clients. --- plugins/mod_carbons.lua | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'plugins/mod_carbons.lua') diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua index 9befc9cb..c5efce39 100644 --- a/plugins/mod_carbons.lua +++ b/plugins/mod_carbons.lua @@ -47,6 +47,12 @@ local function should_copy(stanza, c2s, user_bare) return true, "bounce"; end + for archived in stanza:childtags("stanza-id", "urn:xmpp:sid:0") do + if archived and archived.attr.by == user_bare then + return true, "archived"; + end + end + return false, "default"; end -- cgit v1.2.3 From 70410438020cc0646be1ec469dd88909da607b66 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 27 Apr 2020 14:43:54 +0200 Subject: mod_carbons: Don't copy messages that should not be copied The return statements were lost in d95e083931d1 --- plugins/mod_carbons.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins/mod_carbons.lua') diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua index c5efce39..67f345f1 100644 --- a/plugins/mod_carbons.lua +++ b/plugins/mod_carbons.lua @@ -92,6 +92,7 @@ local function message_handler(event, c2s) if not should then module:log("debug", "Not copying stanza: %s (%s)", stanza:top_tag(), why); + return; elseif why == "private" and not c2s then stanza:maptags(function(tag) if not ( tag.attr.xmlns == xmlns_carbons and tag.name == "private" ) then -- cgit v1.2.3 From b966c976903eecced5a16826ef2da07cb011809f Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 27 Apr 2020 14:46:15 +0200 Subject: mod_carbons: Check for and strip 'private' tag before stopping This was explicit previously --- plugins/mod_carbons.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'plugins/mod_carbons.lua') diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua index 67f345f1..fad47a7c 100644 --- a/plugins/mod_carbons.lua +++ b/plugins/mod_carbons.lua @@ -92,13 +92,14 @@ local function message_handler(event, c2s) if not should then module:log("debug", "Not copying stanza: %s (%s)", stanza:top_tag(), why); - return; - elseif why == "private" and not c2s then + if why == "private" and not c2s then stanza:maptags(function(tag) if not ( tag.attr.xmlns == xmlns_carbons and tag.name == "private" ) then return tag; end end); + end + return; end local carbon; -- cgit v1.2.3 From 92c97b036113d8b608fd3c85e7deb8712c7004e6 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 6 May 2020 12:48:09 +0200 Subject: mod_carbons: Clarify handling of error bounces The :find bit was hard to understand, this should be clearer. --- plugins/mod_carbons.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'plugins/mod_carbons.lua') diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua index fad47a7c..f07e9356 100644 --- a/plugins/mod_carbons.lua +++ b/plugins/mod_carbons.lua @@ -5,10 +5,15 @@ local st = require "util.stanza"; local jid_bare = require "util.jid".bare; +local jid_resource = require "util.jid".resource; local xmlns_carbons = "urn:xmpp:carbons:2"; local xmlns_forward = "urn:xmpp:forward:0"; local full_sessions, bare_sessions = prosody.full_sessions, prosody.bare_sessions; +local function is_bare(jid) + return not jid_resource(jid); +end + local function toggle_carbons(event) local origin, stanza = event.origin, event.stanza; local state = stanza.tags[1].name; @@ -43,7 +48,10 @@ local function should_copy(stanza, c2s, user_bare) return true, "type"; end - if st_type == "error" and not c2s and not (stanza.attr.from or ""):find("/") then + -- Normal outgoing chat messages are sent to=bare JID. This clause should + -- match the error bounces from those, which would have from=bare JID and + -- be incoming (not c2s). + if st_type == "error" and not c2s and is_bare(stanza.attr.from) then return true, "bounce"; end -- cgit v1.2.3 From 527d16b0b372a6103e23c79eb3cb9ed1588d311e Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 9 May 2020 00:50:59 +0200 Subject: mod_carbons: Describe return types in a comment For similarity with mod_mam, mod_csi_simple --- plugins/mod_carbons.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/mod_carbons.lua') diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua index f07e9356..ccd16ad5 100644 --- a/plugins/mod_carbons.lua +++ b/plugins/mod_carbons.lua @@ -25,7 +25,7 @@ end module:hook("iq-set/self/"..xmlns_carbons..":disable", toggle_carbons); module:hook("iq-set/self/"..xmlns_carbons..":enable", toggle_carbons); -local function should_copy(stanza, c2s, user_bare) +local function should_copy(stanza, c2s, user_bare) --> boolean, reason: string local st_type = stanza.attr.type or "normal"; if stanza:get_child("private", xmlns_carbons) then return false, "private"; -- cgit v1.2.3 From 4443c44e22a58b5cd2d2f16f9748648d5bd8a7e9 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 9 May 2020 00:55:18 +0200 Subject: mod_carbons: Explicitly carbon XEP-0353: Jingle Message Initiation --- plugins/mod_carbons.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'plugins/mod_carbons.lua') diff --git a/plugins/mod_carbons.lua b/plugins/mod_carbons.lua index ccd16ad5..e0f0a711 100644 --- a/plugins/mod_carbons.lua +++ b/plugins/mod_carbons.lua @@ -55,6 +55,11 @@ local function should_copy(stanza, c2s, user_bare) --> boolean, reason: string return true, "bounce"; end + if stanza:get_child(nil, "urn:xmpp:jingle-message:0") then + -- XXX Experimental XEP stuck in Proposed for almost a year at the time of this comment + return true, "jingle call"; + end + for archived in stanza:childtags("stanza-id", "urn:xmpp:sid:0") do if archived and archived.attr.by == user_bare then return true, "archived"; -- cgit v1.2.3