Comparison

plugins/mod_component.lua @ 11120:b2331f3dfeea

Merge 0.11->trunk
author Matthew Wild <mwild1@gmail.com>
date Wed, 30 Sep 2020 09:50:33 +0100
parent 11035:ba1143ddae9b
child 11240:0f7ecc9a4560
comparison
equal deleted inserted replaced
11119:68df52bf08d5 11120:b2331f3dfeea
47 env.session = false; 47 env.session = false;
48 48
49 local send; 49 local send;
50 50
51 local function on_destroy(session, err) --luacheck: ignore 212/err 51 local function on_destroy(session, err) --luacheck: ignore 212/err
52 module:set_status("warn", err and ("Disconnected: "..err) or "Disconnected");
52 env.connected = false; 53 env.connected = false;
53 env.session = false; 54 env.session = false;
54 send = nil; 55 send = nil;
55 session.on_destroy = nil; 56 session.on_destroy = nil;
56 end 57 end
100 session.component_validate_from = module:get_option_boolean("validate_from_addresses", true); 101 session.component_validate_from = module:get_option_boolean("validate_from_addresses", true);
101 session.type = "component"; 102 session.type = "component";
102 module:log("info", "External component successfully authenticated"); 103 module:log("info", "External component successfully authenticated");
103 session.send(st.stanza("handshake")); 104 session.send(st.stanza("handshake"));
104 module:fire_event("component-authenticated", { session = session }); 105 module:fire_event("component-authenticated", { session = session });
106 module:set_status("info", "Connected");
105 107
106 return true; 108 return true;
107 end 109 end
108 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth, -1); 110 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth, -1);
109 111
128 end 130 end
129 end 131 end
130 end 132 end
131 module:log("warn", "Component not connected, bouncing error for: %s", stanza:top_tag()); 133 module:log("warn", "Component not connected, bouncing error for: %s", stanza:top_tag());
132 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then 134 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
133 event.origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable")); 135 event.origin.send(st.error_reply(stanza, "wait", "remote-server-timeout", "Component unavailable", module.host)
136 :tag("not-connected", { xmlns = "xmpp:prosody.im/protocol/component" }));
134 end 137 end
135 end 138 end
136 return true; 139 return true;
137 end 140 end
138 141
163 166
164 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams"; 167 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
165 168
166 function stream_callbacks.error(session, error, data) 169 function stream_callbacks.error(session, error, data)
167 if session.destroyed then return; end 170 if session.destroyed then return; end
168 module:log("warn", "Error processing component stream: %s", tostring(error)); 171 module:log("warn", "Error processing component stream: %s", error);
169 if error == "no-stream" then 172 if error == "no-stream" then
170 session:close("invalid-namespace"); 173 session:close("invalid-namespace");
171 elseif error == "parse-error" then 174 elseif error == "parse-error" then
172 session.log("warn", "External component %s XML parse error: %s", tostring(session.host), tostring(data)); 175 session.log("warn", "External component %s XML parse error: %s", session.host, data);
173 session:close("not-well-formed"); 176 session:close("not-well-formed");
174 elseif error == "stream-error" then 177 elseif error == "stream-error" then
175 local condition, text = "undefined-condition"; 178 local condition, text = "undefined-condition";
176 for child in data:childtags(nil, xmlns_xmpp_streams) do 179 for child in data:childtags(nil, xmlns_xmpp_streams) do
177 if child.name ~= "text" then 180 if child.name ~= "text" then
204 function stream_callbacks.streamclosed(session) 207 function stream_callbacks.streamclosed(session)
205 session.log("debug", "Received </stream:stream>"); 208 session.log("debug", "Received </stream:stream>");
206 session:close(); 209 session:close();
207 end 210 end
208 211
209 local function handleerr(err) log("error", "Traceback[component]: %s", traceback(tostring(err), 2)); end 212 local function handleerr(err) log("error", "Traceback[component]: %s", traceback(err, 2)); end
210 function stream_callbacks.handlestanza(session, stanza) 213 function stream_callbacks.handlestanza(session, stanza)
211 -- Namespaces are icky. 214 -- Namespaces are icky.
212 if not stanza.attr.xmlns and stanza.name == "handshake" then 215 if not stanza.attr.xmlns and stanza.name == "handshake" then
213 stanza.attr.xmlns = xmlns_component; 216 stanza.attr.xmlns = xmlns_component;
214 end 217 end
264 stanza:tag("text", stream_xmlns_attr):text(reason.text):up(); 267 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
265 end 268 end
266 if reason.extra then 269 if reason.extra then
267 stanza:add_child(reason.extra); 270 stanza:add_child(reason.extra);
268 end 271 end
269 module:log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza)); 272 module:log("info", "Disconnecting component, <stream:error> is: %s", stanza);
270 session.send(stanza); 273 session.send(stanza);
271 elseif reason.name then -- a stanza 274 elseif reason.name then -- a stanza
272 module:log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason)); 275 module:log("info", "Disconnecting component, <stream:error> is: %s", reason);
273 session.send(reason); 276 session.send(reason);
274 end 277 end
275 end 278 end
276 end 279 end
277 session.send("</stream:stream>"); 280 session.send("</stream:stream>");
308 end 311 end
309 312
310 function session.data(_, data) 313 function session.data(_, data)
311 local ok, err = stream:feed(data); 314 local ok, err = stream:feed(data);
312 if ok then return; end 315 if ok then return; end
313 module:log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_")); 316 log("debug", "Received invalid XML (%s) %d bytes: %q", err, #data, data:sub(1, 300));
314 session:close("not-well-formed"); 317 session:close("not-well-formed");
315 end 318 end
316 319
317 session.dispatch_stanza = stream_callbacks.handlestanza; 320 session.dispatch_stanza = stream_callbacks.handlestanza;
318 321
323 session.data(conn, data); 326 session.data(conn, data);
324 end 327 end
325 function listener.ondisconnect(conn, err) 328 function listener.ondisconnect(conn, err)
326 local session = sessions[conn]; 329 local session = sessions[conn];
327 if session then 330 if session then
328 (session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err)); 331 (session.log or log)("info", "component disconnected: %s (%s)", session.host, err);
329 if session.host then 332 if session.host then
330 module:context(session.host):fire_event("component-disconnected", { session = session, reason = err }); 333 module:context(session.host):fire_event("component-disconnected", { session = session, reason = err });
331 end 334 end
332 if session.on_destroy then session:on_destroy(err); end 335 if session.on_destroy then session:on_destroy(err); end
333 sessions[conn] = nil; 336 sessions[conn] = nil;