Merge 0.10->trunk
[prosody.git] / plugins / mod_carbons.lua
1 -- XEP-0280: Message Carbons implementation for Prosody
2 -- Copyright (C) 2011 Kim Alvefur
3 --
4 -- This file is MIT/X11 licensed.
5
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;
11
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         origin.send(st.reply(stanza));
18         return true;
19 end
20 module:hook("iq-set/self/"..xmlns_carbons..":disable", toggle_carbons);
21 module:hook("iq-set/self/"..xmlns_carbons..":enable", toggle_carbons);
22
23 local function message_handler(event, c2s)
24         local origin, stanza = event.origin, event.stanza;
25         local orig_type = stanza.attr.type or "normal";
26         local orig_from = stanza.attr.from;
27         local orig_to = stanza.attr.to;
28         
29         if not(orig_type == "chat" or orig_type == "normal" and stanza:get_child("body")) then
30                 return -- Only chat type messages
31         end
32
33         -- Stanza sent by a local client
34         local bare_jid = jid_bare(orig_from);
35         local target_session = origin;
36         local top_priority = false;
37         local user_sessions = bare_sessions[bare_jid];
38
39         -- Stanza about to be delivered to a local client
40         if not c2s then
41                 bare_jid = jid_bare(orig_to);
42                 target_session = full_sessions[orig_to];
43                 user_sessions = bare_sessions[bare_jid];
44                 if not target_session and user_sessions then
45                         -- The top resources will already receive this message per normal routing rules,
46                         -- so we are going to skip them in order to avoid sending duplicated messages.
47                         local top_resources = user_sessions.top_resources;
48                         top_priority = top_resources and top_resources[1].priority
49                 end
50         end
51
52         if not user_sessions then
53                 module:log("debug", "Skip carbons for offline user");
54                 return -- No use in sending carbons to an offline user
55         end
56
57         if stanza:get_child("private", xmlns_carbons) then
58                 if not c2s then
59                         stanza:maptags(function(tag)
60                                 if not ( tag.attr.xmlns == xmlns_carbons and tag.name == "private" ) then
61                                         return tag;
62                                 end
63                         end);
64                 end
65                 module:log("debug", "Message tagged private, ignoring");
66                 return
67         elseif stanza:get_child("no-copy", "urn:xmpp:hints") then
68                 module:log("debug", "Message has no-copy hint, ignoring");
69                 return
70         elseif stanza:get_child("x", "http://jabber.org/protocol/muc#user") then
71                 module:log("debug", "MUC PM, ignoring");
72                 return
73         end
74
75         -- Create the carbon copy and wrap it as per the Stanza Forwarding XEP
76         local copy = st.clone(stanza);
77         copy.attr.xmlns = "jabber:client";
78         local carbon = st.message{ from = bare_jid, type = orig_type, }
79                 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons })
80                         :tag("forwarded", { xmlns = xmlns_forward })
81                                 :add_child(copy):reset();
82
83         user_sessions = user_sessions and user_sessions.sessions;
84         for _, session in pairs(user_sessions) do
85                 -- Carbons are sent to resources that have enabled it
86                 if session.want_carbons
87                 -- but not the resource that sent the message, or the one that it's directed to
88                 and session ~= target_session
89                 -- and isn't among the top resources that would receive the message per standard routing rules
90                 and (c2s or session.priority ~= top_priority) then
91                         carbon.attr.to = session.full_jid;
92                         module:log("debug", "Sending carbon to %s", session.full_jid);
93                         session.send(carbon);
94                 end
95         end
96 end
97
98 local function c2s_message_handler(event)
99         return message_handler(event, true)
100 end
101
102 -- Stanzas sent by local clients
103 module:hook("pre-message/host", c2s_message_handler, 1);
104 module:hook("pre-message/bare", c2s_message_handler, 1);
105 module:hook("pre-message/full", c2s_message_handler, 1);
106 -- Stanzas to local clients
107 module:hook("message/bare", message_handler, 1);
108 module:hook("message/full", message_handler, 1);
109
110 module:add_feature(xmlns_carbons);