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