plugins/mod_carbons.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 27 Mar 2024 15:35:15 +0000
branch0.12
changeset 13469 54a936345aaa
parent 12312 063ce658c181
child 12981 74b9e05af71e
permissions -rw-r--r--
prosodyctl check: Warn about invalid domain names in the config file This ensures that domain names of virtual hosts and components are valid in XMPP, and that they are encoded correctly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
-- XEP-0280: Message Carbons implementation for Prosody
7673
2be85cb3bc66 mod_carbons: Make the conditions for ignoring MUC PMs more specific (fixes #744)
Kim Alvefur <zash@zash.se>
parents: 6844
diff changeset
     2
-- Copyright (C) 2011-2016 Kim Alvefur
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
--
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
-- This file is MIT/X11 licensed.
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
local st = require "util.stanza";
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
local jid_bare = require "util.jid".bare;
10806
c11f9cd6c761 mod_carbons: Clarify handling of error bounces
Kim Alvefur <zash@zash.se>
parents: 10790
diff changeset
     8
local jid_resource = require "util.jid".resource;
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
local xmlns_carbons = "urn:xmpp:carbons:2";
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
local xmlns_forward = "urn:xmpp:forward:0";
6807
9f40ae38f0de mod_carbons: Get full_ and bare_sessions from the prosody global [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6806
diff changeset
    11
local full_sessions, bare_sessions = prosody.full_sessions, prosody.bare_sessions;
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
11840
8ccb22c0fa56 mod_carbons: Advertise following of recommended rules (closes #1486)
Kim Alvefur <zash@zash.se>
parents: 11800
diff changeset
    13
module:add_feature("urn:xmpp:carbons:rules:0");
8ccb22c0fa56 mod_carbons: Advertise following of recommended rules (closes #1486)
Kim Alvefur <zash@zash.se>
parents: 11800
diff changeset
    14
10806
c11f9cd6c761 mod_carbons: Clarify handling of error bounces
Kim Alvefur <zash@zash.se>
parents: 10790
diff changeset
    15
local function is_bare(jid)
c11f9cd6c761 mod_carbons: Clarify handling of error bounces
Kim Alvefur <zash@zash.se>
parents: 10790
diff changeset
    16
	return not jid_resource(jid);
c11f9cd6c761 mod_carbons: Clarify handling of error bounces
Kim Alvefur <zash@zash.se>
parents: 10790
diff changeset
    17
end
c11f9cd6c761 mod_carbons: Clarify handling of error bounces
Kim Alvefur <zash@zash.se>
parents: 10790
diff changeset
    18
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
local function toggle_carbons(event)
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
	local origin, stanza = event.origin, event.stanza;
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
	local state = stanza.tags[1].name;
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
	module:log("debug", "%s %sd carbons", origin.full_jid, state);
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
	origin.want_carbons = state == "enable" and stanza.tags[1].attr.xmlns;
6844
be87ab2d611c plugins: Explicitly return to halt event propagation (session.send sometimes does not return true)
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
    24
	origin.send(st.reply(stanza));
be87ab2d611c plugins: Explicitly return to halt event propagation (session.send sometimes does not return true)
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
    25
	return true;
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
end
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
module:hook("iq-set/self/"..xmlns_carbons..":disable", toggle_carbons);
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
module:hook("iq-set/self/"..xmlns_carbons..":enable", toggle_carbons);
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
10822
04a0aa6d7e72 mod_carbons: Describe return types in a comment
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
    30
local function should_copy(stanza, c2s, user_bare) --> boolean, reason: string
10783
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    31
	local st_type = stanza.attr.type or "normal";
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    32
	if stanza:get_child("private", xmlns_carbons) then
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    33
		return false, "private";
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    34
	end
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    35
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    36
	if stanza:get_child("no-copy", "urn:xmpp:hints") then
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    37
		return false, "hint";
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    38
	end
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    39
10784
22bbc644c5eb mod_carbons: Fix syntax error [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10783
diff changeset
    40
	if not c2s and stanza.attr.to ~= user_bare and stanza:get_child("x", "http://jabber.org/protocol/muc#user") then
10783
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    41
		-- MUC PMs are normally sent to full JIDs
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    42
		return false, "muc-pm";
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    43
	end
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    44
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    45
	if st_type == "chat" then
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    46
		return true, "type";
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    47
	end
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    48
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    49
	if st_type == "normal" and stanza:get_child("body") then
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    50
		return true, "type";
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    51
	end
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    52
10806
c11f9cd6c761 mod_carbons: Clarify handling of error bounces
Kim Alvefur <zash@zash.se>
parents: 10790
diff changeset
    53
	-- Normal outgoing chat messages are sent to=bare JID. This clause should
c11f9cd6c761 mod_carbons: Clarify handling of error bounces
Kim Alvefur <zash@zash.se>
parents: 10790
diff changeset
    54
	-- match the error bounces from those, which would have from=bare JID and
c11f9cd6c761 mod_carbons: Clarify handling of error bounces
Kim Alvefur <zash@zash.se>
parents: 10790
diff changeset
    55
	-- be incoming (not c2s).
c11f9cd6c761 mod_carbons: Clarify handling of error bounces
Kim Alvefur <zash@zash.se>
parents: 10790
diff changeset
    56
	if st_type == "error" and not c2s and is_bare(stanza.attr.from) then
10785
f7e8d299513f mod_carbons: Carbon incoming message delivery failure reports
Kim Alvefur <zash@zash.se>
parents: 10784
diff changeset
    57
		return true, "bounce";
f7e8d299513f mod_carbons: Carbon incoming message delivery failure reports
Kim Alvefur <zash@zash.se>
parents: 10784
diff changeset
    58
	end
f7e8d299513f mod_carbons: Carbon incoming message delivery failure reports
Kim Alvefur <zash@zash.se>
parents: 10784
diff changeset
    59
12238
1c47162dd965 plugins: Update for namespace bump in XEP-0353 v0.4.0
Kim Alvefur <zash@zash.se>
parents: 11840
diff changeset
    60
	if stanza:get_child(nil, "urn:xmpp:jingle-message:0") or stanza:get_child(nil, "urn:xmpp:jingle-message:1") then
11264
08b397c21805 mod_csi_simple,mod_carbons,mod_mam: Update comment about XEP-0353
Kim Alvefur <zash@zash.se>
parents: 10824
diff changeset
    61
		-- XXX Experimental XEP
10824
e0a09d3af563 mod_carbons: Explicitly carbon XEP-0353: Jingle Message Initiation
Kim Alvefur <zash@zash.se>
parents: 10822
diff changeset
    62
		return true, "jingle call";
e0a09d3af563 mod_carbons: Explicitly carbon XEP-0353: Jingle Message Initiation
Kim Alvefur <zash@zash.se>
parents: 10822
diff changeset
    63
	end
e0a09d3af563 mod_carbons: Explicitly carbon XEP-0353: Jingle Message Initiation
Kim Alvefur <zash@zash.se>
parents: 10822
diff changeset
    64
11800
71c20650a0b4 mod_carbons: Reduce line count using new util.stanza attr method
Kim Alvefur <zash@zash.se>
parents: 11264
diff changeset
    65
	if stanza:get_child_with_attr("stanza-id", "urn:xmpp:sid:0", "by", user_bare) then
71c20650a0b4 mod_carbons: Reduce line count using new util.stanza attr method
Kim Alvefur <zash@zash.se>
parents: 11264
diff changeset
    66
		return true, "archived";
10786
6b776f80b96e mod_carbons: Carbon anything that has been archived by the current user
Kim Alvefur <zash@zash.se>
parents: 10785
diff changeset
    67
	end
6b776f80b96e mod_carbons: Carbon anything that has been archived by the current user
Kim Alvefur <zash@zash.se>
parents: 10785
diff changeset
    68
10783
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    69
	return false, "default";
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    70
end
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
    71
12312
063ce658c181 mod_carbons: Allow plugging into decision of whether to carbon-copy
Kim Alvefur <zash@zash.se>
parents: 12238
diff changeset
    72
module:hook("carbons-should-copy", function (event)
063ce658c181 mod_carbons: Allow plugging into decision of whether to carbon-copy
Kim Alvefur <zash@zash.se>
parents: 12238
diff changeset
    73
	local should, why = should_copy(event.stanza);
063ce658c181 mod_carbons: Allow plugging into decision of whether to carbon-copy
Kim Alvefur <zash@zash.se>
parents: 12238
diff changeset
    74
	event.reason = why;
063ce658c181 mod_carbons: Allow plugging into decision of whether to carbon-copy
Kim Alvefur <zash@zash.se>
parents: 12238
diff changeset
    75
	return should;
063ce658c181 mod_carbons: Allow plugging into decision of whether to carbon-copy
Kim Alvefur <zash@zash.se>
parents: 12238
diff changeset
    76
end, -1)
063ce658c181 mod_carbons: Allow plugging into decision of whether to carbon-copy
Kim Alvefur <zash@zash.se>
parents: 12238
diff changeset
    77
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    78
local function message_handler(event, c2s)
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    79
	local origin, stanza = event.origin, event.stanza;
6806
7ed87299dbf9 mod_carbons: Carbon chat messages or normal messages that have a body
Kim Alvefur <zash@zash.se>
parents: 6549
diff changeset
    80
	local orig_type = stanza.attr.type or "normal";
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    81
	local orig_from = stanza.attr.from;
7779
63f50a84318f mod_carbons: Rename some variables for clarity
Kim Alvefur <zash@zash.se>
parents: 7721
diff changeset
    82
	local bare_from = jid_bare(orig_from);
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    83
	local orig_to = stanza.attr.to;
7779
63f50a84318f mod_carbons: Rename some variables for clarity
Kim Alvefur <zash@zash.se>
parents: 7721
diff changeset
    84
	local bare_to = jid_bare(orig_to);
63f50a84318f mod_carbons: Rename some variables for clarity
Kim Alvefur <zash@zash.se>
parents: 7721
diff changeset
    85
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    86
	-- Stanza sent by a local client
7779
63f50a84318f mod_carbons: Rename some variables for clarity
Kim Alvefur <zash@zash.se>
parents: 7721
diff changeset
    87
	local bare_jid = bare_from; -- JID of the local user
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    88
	local target_session = origin;
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    89
	local top_priority = false;
7779
63f50a84318f mod_carbons: Rename some variables for clarity
Kim Alvefur <zash@zash.se>
parents: 7721
diff changeset
    90
	local user_sessions = bare_sessions[bare_from];
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    91
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    92
	-- Stanza about to be delivered to a local client
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    93
	if not c2s then
7779
63f50a84318f mod_carbons: Rename some variables for clarity
Kim Alvefur <zash@zash.se>
parents: 7721
diff changeset
    94
		bare_jid = bare_to;
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    95
		target_session = full_sessions[orig_to];
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    96
		user_sessions = bare_sessions[bare_jid];
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    97
		if not target_session and user_sessions then
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    98
			-- The top resources will already receive this message per normal routing rules,
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    99
			-- so we are going to skip them in order to avoid sending duplicated messages.
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   100
			local top_resources = user_sessions.top_resources;
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   101
			top_priority = top_resources and top_resources[1].priority
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   102
		end
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   103
	end
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   104
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   105
	if not user_sessions then
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   106
		module:log("debug", "Skip carbons for offline user");
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   107
		return -- No use in sending carbons to an offline user
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   108
	end
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   109
12312
063ce658c181 mod_carbons: Allow plugging into decision of whether to carbon-copy
Kim Alvefur <zash@zash.se>
parents: 12238
diff changeset
   110
	local event_payload = { stanza = stanza; session = origin };
063ce658c181 mod_carbons: Allow plugging into decision of whether to carbon-copy
Kim Alvefur <zash@zash.se>
parents: 12238
diff changeset
   111
	local should = module:fire_event("carbons-should-copy", event_payload);
063ce658c181 mod_carbons: Allow plugging into decision of whether to carbon-copy
Kim Alvefur <zash@zash.se>
parents: 12238
diff changeset
   112
	local why = event_payload.reason;
10783
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
   113
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
   114
	if not should then
d95e083931d1 mod_carbons: Refactor in new style (mod_mam/csi)
Kim Alvefur <zash@zash.se>
parents: 10782
diff changeset
   115
		module:log("debug", "Not copying stanza: %s (%s)", stanza:top_tag(), why);
10790
a1b633ba9bd9 mod_carbons: Check for and strip 'private' tag before stopping
Kim Alvefur <zash@zash.se>
parents: 10789
diff changeset
   116
		if why == "private" and not c2s then
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   117
			stanza:maptags(function(tag)
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   118
				if not ( tag.attr.xmlns == xmlns_carbons and tag.name == "private" ) then
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   119
					return tag;
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   120
				end
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   121
			end);
10790
a1b633ba9bd9 mod_carbons: Check for and strip 'private' tag before stopping
Kim Alvefur <zash@zash.se>
parents: 10789
diff changeset
   122
		end
a1b633ba9bd9 mod_carbons: Check for and strip 'private' tag before stopping
Kim Alvefur <zash@zash.se>
parents: 10789
diff changeset
   123
		return;
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   124
	end
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   125
10473
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   126
	local carbon;
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   127
	user_sessions = user_sessions and user_sessions.sessions;
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   128
	for _, session in pairs(user_sessions) do
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   129
		-- Carbons are sent to resources that have enabled it
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   130
		if session.want_carbons
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   131
		-- but not the resource that sent the message, or the one that it's directed to
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   132
		and session ~= target_session
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   133
		-- and isn't among the top resources that would receive the message per standard routing rules
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   134
		and (c2s or session.priority ~= top_priority) then
10473
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   135
			if not carbon then
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   136
				-- Create the carbon copy and wrap it as per the Stanza Forwarding XEP
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   137
				local copy = st.clone(stanza);
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   138
				if c2s and not orig_to then
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   139
					stanza.attr.to = bare_from;
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   140
				end
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   141
				copy.attr.xmlns = "jabber:client";
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   142
				carbon = st.message{ from = bare_jid, type = orig_type, }
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   143
					:tag(c2s and "sent" or "received", { xmlns = xmlns_carbons })
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   144
						:tag("forwarded", { xmlns = xmlns_forward })
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   145
							:add_child(copy):reset();
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   146
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   147
			end
658b759a1f7a mod_carbons: Improve performance by delaying creation of carbon payload
Kim Alvefur <zash@zash.se>
parents: 8357
diff changeset
   148
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   149
			carbon.attr.to = session.full_jid;
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   150
			module:log("debug", "Sending carbon to %s", session.full_jid);
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   151
			session.send(carbon);
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   152
		end
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   153
	end
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   154
end
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   155
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   156
local function c2s_message_handler(event)
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   157
	return message_handler(event, true)
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   158
end
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   159
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   160
-- Stanzas sent by local clients
7721
c58075c4d375 mod_message, mod_carbons: Adjust event hook priorities to negative (core modules should do this to make overriding from other modules easier)
Kim Alvefur <zash@zash.se>
parents: 7701
diff changeset
   161
module:hook("pre-message/host", c2s_message_handler, -0.5);
c58075c4d375 mod_message, mod_carbons: Adjust event hook priorities to negative (core modules should do this to make overriding from other modules easier)
Kim Alvefur <zash@zash.se>
parents: 7701
diff changeset
   162
module:hook("pre-message/bare", c2s_message_handler, -0.5);
c58075c4d375 mod_message, mod_carbons: Adjust event hook priorities to negative (core modules should do this to make overriding from other modules easier)
Kim Alvefur <zash@zash.se>
parents: 7701
diff changeset
   163
module:hook("pre-message/full", c2s_message_handler, -0.5);
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   164
-- Stanzas to local clients
7721
c58075c4d375 mod_message, mod_carbons: Adjust event hook priorities to negative (core modules should do this to make overriding from other modules easier)
Kim Alvefur <zash@zash.se>
parents: 7701
diff changeset
   165
module:hook("message/bare", message_handler, -0.5);
c58075c4d375 mod_message, mod_carbons: Adjust event hook priorities to negative (core modules should do this to make overriding from other modules easier)
Kim Alvefur <zash@zash.se>
parents: 7701
diff changeset
   166
module:hook("message/full", message_handler, -0.5);
6549
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   167
3426e903c48d mod_carbons: Import XEP-0280 implementation from prosody-modules (sans compat with older versions of the protocol)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   168
module:add_feature(xmlns_carbons);