1 -- XEP-0280: Message Carbons implementation for Prosody
2 -- Copyright (C) 2011 Kim Alvefur
4 -- This file is MIT/X11 licensed.
6 local st = require "util.stanza";
7 local jid_bare = require "util.jid".bare;
8 local xmlns_carbons = "urn:xmpp:carbons:2";
9 local xmlns_forward = "urn:xmpp:forward:0";
10 local full_sessions, bare_sessions = prosody.full_sessions, prosody.bare_sessions;
12 local function toggle_carbons(event)
13 local origin, stanza = event.origin, event.stanza;
14 local state = stanza.tags[1].name;
15 module:log("debug", "%s %sd carbons", origin.full_jid, state);
16 origin.want_carbons = state == "enable" and stanza.tags[1].attr.xmlns;
17 return origin.send(st.reply(stanza));
19 module:hook("iq-set/self/"..xmlns_carbons..":disable", toggle_carbons);
20 module:hook("iq-set/self/"..xmlns_carbons..":enable", toggle_carbons);
22 local function message_handler(event, c2s)
23 local origin, stanza = event.origin, event.stanza;
24 local orig_type = stanza.attr.type or "normal";
25 local orig_from = stanza.attr.from;
26 local orig_to = stanza.attr.to;
28 if not(orig_type == "chat" or orig_type == "normal" and stanza:get_child("body")) then
29 return -- Only chat type messages
32 -- Stanza sent by a local client
33 local bare_jid = jid_bare(orig_from);
34 local target_session = origin;
35 local top_priority = false;
36 local user_sessions = bare_sessions[bare_jid];
38 -- Stanza about to be delivered to a local client
40 bare_jid = jid_bare(orig_to);
41 target_session = full_sessions[orig_to];
42 user_sessions = bare_sessions[bare_jid];
43 if not target_session and user_sessions then
44 -- The top resources will already receive this message per normal routing rules,
45 -- so we are going to skip them in order to avoid sending duplicated messages.
46 local top_resources = user_sessions.top_resources;
47 top_priority = top_resources and top_resources[1].priority
51 if not user_sessions then
52 module:log("debug", "Skip carbons for offline user");
53 return -- No use in sending carbons to an offline user
56 if stanza:get_child("private", xmlns_carbons) then
58 stanza:maptags(function(tag)
59 if not ( tag.attr.xmlns == xmlns_carbons and tag.name == "private" ) then
64 module:log("debug", "Message tagged private, ignoring");
66 elseif stanza:get_child("no-copy", "urn:xmpp:hints") then
67 module:log("debug", "Message has no-copy hint, ignoring");
69 elseif stanza:get_child("x", "http://jabber.org/protocol/muc#user") then
70 module:log("debug", "MUC PM, ignoring");
74 -- Create the carbon copy and wrap it as per the Stanza Forwarding XEP
75 local copy = st.clone(stanza);
76 copy.attr.xmlns = "jabber:client";
77 local carbon = st.message{ from = bare_jid, type = orig_type, }
78 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons })
79 :tag("forwarded", { xmlns = xmlns_forward })
80 :add_child(copy):reset();
82 user_sessions = user_sessions and user_sessions.sessions;
83 for _, session in pairs(user_sessions) do
84 -- Carbons are sent to resources that have enabled it
85 if session.want_carbons
86 -- but not the resource that sent the message, or the one that it's directed to
87 and session ~= target_session
88 -- and isn't among the top resources that would receive the message per standard routing rules
89 and (c2s or session.priority ~= top_priority) then
90 carbon.attr.to = session.full_jid;
91 module:log("debug", "Sending carbon to %s", session.full_jid);
97 local function c2s_message_handler(event)
98 return message_handler(event, true)
101 -- Stanzas sent by local clients
102 module:hook("pre-message/host", c2s_message_handler, 1);
103 module:hook("pre-message/bare", c2s_message_handler, 1);
104 module:hook("pre-message/full", c2s_message_handler, 1);
105 -- Stanzas to local clients
106 module:hook("message/bare", message_handler, 1);
107 module:hook("message/full", message_handler, 1);
109 module:add_feature(xmlns_carbons);