Comparison

mod_carbons/mod_carbons.lua @ 832:9087431d35f6

mod_carbons: Add comments and rename some variables to make it clearer
author Kim Alvefur <zash@zash.se>
date Tue, 18 Sep 2012 18:58:13 +0200
parent 804:9927d88a1b2a
child 833:30d49c26d219
comparison
equal deleted inserted replaced
831:0c3638673464 832:9087431d35f6
49 module:log("debug", "Message was to resource %s, it will not get carbon", resource); 49 module:log("debug", "Message was to resource %s, it will not get carbon", resource);
50 no_carbon_to[resource] = true; 50 no_carbon_to[resource] = true;
51 elseif user_sessions then 51 elseif user_sessions then
52 local top_resources = user_sessions.top_resources; 52 local top_resources = user_sessions.top_resources;
53 if top_resources then 53 if top_resources then
54 for _, session in ipairs(top_resources) do 54 -- These will already receive this message per normal routing rules,
55 module:log("debug", "Not sending carbons to top resource %s", session.resource); 55 -- so we skip them to avoid duplicated messages.
56 no_carbon_to[session.resource] = true; 56 for i=1,#top_resources do
57 local resource = top_resources[i].resource;
58 module:log("debug", "Not sending carbons to top resource %s", resource);
59 no_carbon_to[resource] = true;
57 end 60 end
58 end 61 end
59 end 62 end
60 end 63 end
61 64
71 end); 74 end);
72 module:log("debug", "Message tagged private, ignoring"); 75 module:log("debug", "Message tagged private, ignoring");
73 return 76 return
74 end 77 end
75 78
76 local msg = st.clone(stanza); 79 -- Create the carbon copy and wrap it as per the Stanza Forwarding XEP
77 msg.attr.xmlns = "jabber:client"; 80 local copy = st.clone(stanza);
78 local fwd = st.message{ from = bare_jid, type = orig_type, } 81 copy.attr.xmlns = "jabber:client";
82 local carbon = st.message{ from = bare_jid, type = orig_type, }
79 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }):up() 83 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }):up()
80 :tag("forwarded", { xmlns = xmlns_forward }) 84 :tag("forwarded", { xmlns = xmlns_forward })
81 :add_child(msg):reset(); 85 :add_child(copy):reset();
82 86
87 -- And finally, send the carbon to the sessions that should have it.
83 user_sessions = user_sessions and user_sessions.sessions; 88 user_sessions = user_sessions and user_sessions.sessions;
84 for resource, session in pairs(user_sessions) do 89 for resource, session in pairs(user_sessions) do
85 local full_jid = bare_jid .. "/" .. resource; 90 local full_jid = bare_jid .. "/" .. resource;
86 if session.want_carbons and ((c2s and session ~= origin) or (not c2s and not no_carbon_to[resource])) then 91 if session.want_carbons and ((c2s and session ~= origin) or (not c2s and not no_carbon_to[resource])) then
87 fwd.attr.to = full_jid; 92 carbon.attr.to = full_jid;
88 module:log("debug", "Sending carbon to %s", full_jid); 93 module:log("debug", "Sending carbon to %s", full_jid);
89 session.send(fwd); 94 session.send(carbon);
90 end 95 end
91 end 96 end
92 end 97 end
93 98
94 local function c2s_message_handler(event) 99 local function c2s_message_handler(event)