Comparison

mod_carbons/mod_carbons.lua @ 480:0cef5be669de

mod_carbons: Consolidate message handlers
author Kim Alvefur <zash@zash.se>
date Sat, 26 Nov 2011 09:06:04 +0100
parent 463:7d6a05f94941
child 510:59e80326f2b3
comparison
equal deleted inserted replaced
479:597c872d691e 480:0cef5be669de
25 origin.send(st.reply(stanza)); 25 origin.send(st.reply(stanza));
26 return true 26 return true
27 end 27 end
28 end); 28 end);
29 29
30 function c2s_message_handler(event) 30 local function message_handler(event, c2s)
31 local origin, stanza = event.origin, event.stanza; 31 local origin, stanza = event.origin, event.stanza;
32 local orig_type = stanza.attr.type; 32 local orig_type = stanza.attr.type;
33 local orig_to = stanza.attr.to; 33 local orig_to = stanza.attr.to;
34 local orig_from = stanza.attr.from;
34 35
35 if not (orig_type == nil 36 if not (orig_type == nil
36 or orig_type == "normal" 37 or orig_type == "normal"
37 or orig_type == "chat") then 38 or orig_type == "chat") then
39 return -- No carbons for messages of type error or headline
40 end
41
42 local bare_jid, user_sessions;
43 local no_carbon_to = {};
44 module:log("debug", "origin (%s) type: %s", tostring(origin), origin.type)
45 if c2s then -- Stanza sent by a local client
46 bare_jid = (origin.username.."@"..origin.host)
47 user_sessions = host_sessions[origin.username];
48 else -- Stanza about to be delivered to a local client
49 local username, hostname, resource = jid_split(orig_to);
50 bare_jid = jid_bare(orig_to);
51 user_sessions = host_sessions[username];
52 if resource then
53 no_carbon_to[resource] = true;
54 else
55 local top_resources = user_sessions.top_resources;
56 for _, session in ipairs(top_resources) do
57 no_carbon_to[session.resource] = true;
58 end
59 end
60 end
61
62 if not c2s and stanza:get_child("private", xmlns_carbons) then
63 stanza:maptags(function(tag)
64 return tag.attr.xmlns == xmlns_carbons
65 and tag.name == "private" and tag or nil;
66 end);
38 return 67 return
39 end 68 end
40 69
41 local bare_jid, user_sessions; 70 if not stanza:get_child("forwarded", xmlns_forward) then
42 if origin.type == "s2s" then
43 bare_jid = jid_bare(stanza.attr.from);
44 user_sessions = host_sessions[jid_split(orig_to)];
45 else
46 bare_jid = (origin.username.."@"..origin.host)
47 user_sessions = host_sessions[origin.username];
48 end
49
50 if not stanza:get_child("private", xmlns_carbons)
51 and not stanza:get_child("forwarded", xmlns_forward) then
52 user_sessions = user_sessions and user_sessions.sessions; 71 user_sessions = user_sessions and user_sessions.sessions;
53 for resource, session in pairs(user_sessions) do 72 for resource, session in pairs(user_sessions) do
54 local full_jid = bare_jid .. "/" .. resource; 73 local full_jid = bare_jid .. "/" .. resource;
55 if session ~= origin and session.want_carbons then 74 if session.want_carbons then
56 local msg = st.clone(stanza); 75 if (c2s and session ~= origin) or (not c2s and not no_carbon_to[resource]) then
57 msg.attr.xmlns = msg.attr.xmlns or "jabber:client"; 76 local msg = st.clone(stanza);
58 local fwd = st.message{ 77 msg.attr.xmlns = msg.attr.xmlns or "jabber:client";
59 from = bare_jid, 78 local fwd = st.message{
60 to = full_jid, 79 from = bare_jid,
61 type = orig_type, 80 to = full_jid,
62 } 81 type = orig_type,
63 :tag("forwarded", { xmlns = xmlns_forward }) 82 }
64 :tag("sent", { xmlns = xmlns_carbons }):up() 83 :tag("forwarded", { xmlns = xmlns_forward })
65 :add_child(msg); 84 :tag(c2s and "sent" or "received", { xmlns = xmlns_carbons }):up()
66 core_route_stanza(origin, fwd); 85 :add_child(msg);
86 core_post_stanza(origin, fwd);
87 end
67 end 88 end
68 end 89 end
69 end 90 end
70 end 91 end
71 92
72 function s2c_message_handler(event) 93 local function c2s_message_handler(event)
73 local origin, stanza = event.origin, event.stanza; 94 return message_handler(event, true)
74 local orig_type = stanza.attr.type;
75 local orig_to = stanza.attr.to;
76
77 if not (orig_type == nil
78 or orig_type == "normal"
79 or orig_type == "chat") then
80 return
81 end
82
83 local full_jid, bare_jid = orig_to, jid_bare(orig_to);
84 local username, hostname, resource = jid_split(full_jid);
85 local user_sessions = username and host_sessions[username];
86 if not user_sessions or hostname ~= module.host then
87 return
88 end
89
90 local no_carbon_to = {};
91 if resource then
92 no_carbon_to[resource] = true;
93 else
94 local top_resources = user_sessions.top_resources;
95 for i, session in ipairs(top_resources) do
96 no_carbon_to[session.resource] = true;
97 module:log("debug", "not going to send to /%s", resource);
98 end
99 end
100
101 if not stanza:get_child("private", xmlns_carbons)
102 and not stanza:get_child("forwarded", xmlns_forward) then
103 user_sessions = user_sessions and user_sessions.sessions;
104 for resource, session in pairs(user_sessions) do
105 local full_jid = bare_jid .. "/" .. resource;
106 if not no_carbon_to[resource] and session.want_carbons then
107 local msg = st.clone(stanza);
108 msg.attr.xmlns = msg.attr.xmlns or "jabber:client";
109 local fwd = st.message{
110 from = bare_jid,
111 to = full_jid,
112 type = orig_type,
113 }
114 :tag("forwarded", { xmlns = xmlns_forward })
115 :tag("received", { xmlns = xmlns_carbons }):up()
116 :add_child(msg);
117 core_route_stanza(origin, fwd);
118 end
119 end
120 end
121 end 95 end
122 96
123 -- Stanzas sent by local clients 97 -- Stanzas sent by local clients
124 module:hook("pre-message/bare", c2s_message_handler, 1); 98 module:hook("pre-message/bare", c2s_message_handler, 1);
125 module:hook("pre-message/full", c2s_message_handler, 1); 99 module:hook("pre-message/full", c2s_message_handler, 1);
126 -- Stanszas to local clients 100 -- Stanszas to local clients
127 module:hook("message/bare", s2c_message_handler, 1); 101 module:hook("message/bare", message_handler, 1);
128 module:hook("message/full", s2c_message_handler, 1); 102 module:hook("message/full", message_handler, 1);
129 103
130 module:add_feature(xmlns_carbons); 104 module:add_feature(xmlns_carbons);