Software /
code /
prosody-modules
Comparison
mod_carbons/mod_carbons.lua @ 927:a9dfa7232d88
Merge
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 12 Mar 2013 12:10:25 +0000 |
parent | 900:aca1c5eb0508 |
child | 1151:28d4b58bcc3b |
comparison
equal
deleted
inserted
replaced
926:f88381a39c56 | 927:a9dfa7232d88 |
---|---|
5 | 5 |
6 local st = require "util.stanza"; | 6 local st = require "util.stanza"; |
7 local jid_bare = require "util.jid".bare; | 7 local jid_bare = require "util.jid".bare; |
8 local xmlns_carbons = "urn:xmpp:carbons:2"; | 8 local xmlns_carbons = "urn:xmpp:carbons:2"; |
9 local xmlns_carbons_old = "urn:xmpp:carbons:1"; | 9 local xmlns_carbons_old = "urn:xmpp:carbons:1"; |
10 local xmlns_carbons_really_old = "urn:xmpp:carbons:0"; | |
10 local xmlns_forward = "urn:xmpp:forward:0"; | 11 local xmlns_forward = "urn:xmpp:forward:0"; |
11 local full_sessions, bare_sessions = full_sessions, bare_sessions; | 12 local full_sessions, bare_sessions = full_sessions, bare_sessions; |
12 | 13 |
13 local function toggle_carbons(event) | 14 local function toggle_carbons(event) |
14 local origin, stanza = event.origin, event.stanza; | 15 local origin, stanza = event.origin, event.stanza; |
24 module:hook("iq/self/"..xmlns_carbons..":enable", toggle_carbons); | 25 module:hook("iq/self/"..xmlns_carbons..":enable", toggle_carbons); |
25 | 26 |
26 -- COMPAT | 27 -- COMPAT |
27 module:hook("iq/self/"..xmlns_carbons_old..":disable", toggle_carbons); | 28 module:hook("iq/self/"..xmlns_carbons_old..":disable", toggle_carbons); |
28 module:hook("iq/self/"..xmlns_carbons_old..":enable", toggle_carbons); | 29 module:hook("iq/self/"..xmlns_carbons_old..":enable", toggle_carbons); |
30 | |
31 -- COMPAT :( | |
32 if module:get_option_boolean("carbons_v0") then | |
33 module:hook("iq/self/"..xmlns_carbons_really_old..":carbons", function(event) | |
34 local origin, stanza = event.origin, event.stanza; | |
35 if stanza.attr.type == "set" then | |
36 local state = stanza.tags[1].attr.mode; | |
37 origin.want_carbons = state == "enable" and xmlns_carbons_really_old; | |
38 origin.send(st.reply(stanza)); | |
39 return true; | |
40 end | |
41 end); | |
42 end | |
29 | 43 |
30 local function message_handler(event, c2s) | 44 local function message_handler(event, c2s) |
31 local origin, stanza = event.origin, event.stanza; | 45 local origin, stanza = event.origin, event.stanza; |
32 local orig_type = stanza.attr.type; | 46 local orig_type = stanza.attr.type; |
33 local orig_from = stanza.attr.from; | 47 local orig_from = stanza.attr.from; |
74 | 88 |
75 -- Create the carbon copy and wrap it as per the Stanza Forwarding XEP | 89 -- Create the carbon copy and wrap it as per the Stanza Forwarding XEP |
76 local copy = st.clone(stanza); | 90 local copy = st.clone(stanza); |
77 copy.attr.xmlns = "jabber:client"; | 91 copy.attr.xmlns = "jabber:client"; |
78 local carbon = st.message{ from = bare_jid, type = orig_type, } | 92 local carbon = st.message{ from = bare_jid, type = orig_type, } |
79 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }):up() | 93 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }) |
80 :tag("forwarded", { xmlns = xmlns_forward }) | 94 :tag("forwarded", { xmlns = xmlns_forward }) |
81 :add_child(copy):reset(); | 95 :add_child(copy):reset(); |
82 | 96 |
83 -- COMPAT | 97 -- COMPAT |
84 local carbon_old = st.message{ from = bare_jid, type = orig_type, } | 98 local carbon_old = st.message{ from = bare_jid, type = orig_type, } |
85 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons_old }):up() | 99 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons_old }):up() |
86 :tag("forwarded", { xmlns = xmlns_forward }) | 100 :tag("forwarded", { xmlns = xmlns_forward }) |
87 :add_child(copy):reset(); | 101 :add_child(copy):reset(); |
88 | 102 |
103 -- COMPAT | |
104 local carbon_really_old = st.clone(stanza) | |
105 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons_really_old }):up() | |
106 | |
89 user_sessions = user_sessions and user_sessions.sessions; | 107 user_sessions = user_sessions and user_sessions.sessions; |
90 for _, session in pairs(user_sessions) do | 108 for _, session in pairs(user_sessions) do |
91 -- Carbons are sent to resources that have enabled it | 109 -- Carbons are sent to resources that have enabled it |
92 if session.want_carbons | 110 if session.want_carbons |
93 -- but not the resource that sent the message, or the one that it's directed to | 111 -- but not the resource that sent the message, or the one that it's directed to |
94 and session ~= target_session | 112 and session ~= target_session |
95 -- and isn't among the top resources that would receive the message per standard routing rules | 113 -- and isn't among the top resources that would receive the message per standard routing rules |
96 and (c2s or session.priority ~= top_priority) then | 114 and (c2s or session.priority ~= top_priority) |
115 -- don't send v0 carbons (or copies) for c2s | |
116 and (not c2s or session.want_carbons ~= xmlns_carbons_really_old) then | |
97 carbon.attr.to = session.full_jid; | 117 carbon.attr.to = session.full_jid; |
98 module:log("debug", "Sending carbon to %s", session.full_jid); | 118 module:log("debug", "Sending carbon to %s", session.full_jid); |
99 local carbon = session.want_carbons == xmlns_carbons_old and carbon_old or carbon; -- COMPAT | 119 local carbon = session.want_carbons == xmlns_carbons_old and carbon_old -- COMPAT |
120 or session.want_carbons == xmlns_carbons_really_old and carbon_really_old -- COMPAT | |
121 or carbon; | |
100 session.send(carbon); | 122 session.send(carbon); |
101 end | 123 end |
102 end | 124 end |
103 end | 125 end |
104 | 126 |
105 local function c2s_message_handler(event) | 127 local function c2s_message_handler(event) |
106 return message_handler(event, true) | 128 return message_handler(event, true) |
107 end | 129 end |
108 | 130 |
109 -- Stanzas sent by local clients | 131 -- Stanzas sent by local clients |
132 module:hook("pre-message/host", c2s_message_handler, 1); | |
110 module:hook("pre-message/bare", c2s_message_handler, 1); | 133 module:hook("pre-message/bare", c2s_message_handler, 1); |
111 module:hook("pre-message/full", c2s_message_handler, 1); | 134 module:hook("pre-message/full", c2s_message_handler, 1); |
112 -- Stanzas to local clients | 135 -- Stanzas to local clients |
113 module:hook("message/bare", message_handler, 1); | 136 module:hook("message/bare", message_handler, 1); |
114 module:hook("message/full", message_handler, 1); | 137 module:hook("message/full", message_handler, 1); |
115 | 138 |
116 module:add_feature(xmlns_carbons); | 139 module:add_feature(xmlns_carbons); |
117 module:add_feature(xmlns_carbons_old); | 140 module:add_feature(xmlns_carbons_old); |
141 if module:get_option_boolean("carbons_v0") then | |
142 module:add_feature(xmlns_carbons_really_old); | |
143 end |