net/xmppcomponent_listener.lua

Fri, 10 Jul 2009 03:11:45 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 10 Jul 2009 03:11:45 +0100
changeset 1523
841d61be198f
parent 1249
f22ede3e1c28
child 1617
c6e175a0d83b
permissions
-rw-r--r--

Remove version number from copyright headers

1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1249
diff changeset
1 -- Prosody IM
1106
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local hosts = _G.hosts;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local t_concat = table.concat;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local lxp = require "lxp";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local logger = require "util.logger";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local config = require "core.configmanager";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local connlisteners = require "net.connlisteners";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local cm_register_component = require "core.componentmanager".register_component;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local cm_deregister_component = require "core.componentmanager".deregister_component;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local uuid_gen = require "util.uuid".generate;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local sha1 = require "util.hashes".sha1;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local st = require "util.stanza";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local init_xmlhandlers = require "core.xmlhandlers";
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local sessions = {};
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local log = logger.init("componentlistener");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local component_listener = { default_port = 5347; default_mode = "*a"; default_interface = config.get("*", "core", "component_interface") or "127.0.0.1" };
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local xmlns_component = 'jabber:component:accept';
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 --- Callbacks/data for xmlhandlers to handle streams for us ---
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", default_ns = xmlns_component };
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 function stream_callbacks.error(session, error, data, data2)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 log("warn", "Error processing component stream: "..tostring(error));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if error == "no-stream" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 session:close("invalid-namespace");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 elseif error == "xml-parse-error" and data == "unexpected-element-close" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 session.log("warn", "Unexpected close of '%s' tag", data2);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 session:close("xml-not-well-formed");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 else
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 session.log("warn", "External component %s XML parse error: %s", tostring(session.host), tostring(error));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 session:close("xml-not-well-formed");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 function stream_callbacks.streamopened(session, attr)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if config.get(attr.to, "core", "component_module") ~= "component" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 -- Trying to act as a component domain which
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 -- hasn't been configured
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 session:close{ condition = "host-unknown", text = tostring(attr.to).." does not match any configured external components" };
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 return;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 -- Store the original host (this is used for config, etc.)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 session.user = attr.to;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 -- Set the host for future reference
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 session.host = config.get(attr.to, "core", "component_address") or attr.to;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 -- Note that we don't create the internal component
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 -- until after the external component auths successfully
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 session.streamid = uuid_gen();
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 session.notopen = nil;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 session.send(st.stanza("stream:stream", { xmlns=xmlns_component,
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag());
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 function stream_callbacks.streamclosed(session)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 session.send("</stream:stream>");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 session.notopen = true;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local core_process_stanza = core_process_stanza;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 function stream_callbacks.handlestanza(session, stanza)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 -- Namespaces are icky.
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 if not stanza.attr.xmlns and stanza.name == "handshake" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 stanza.attr.xmlns = xmlns_component;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 return core_process_stanza(session, stanza);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 --- Closing a component connection
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local function session_close(session, reason)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 local log = session.log or log;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 if session.conn then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 if reason then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 if type(reason) == "string" then -- assume stream error
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 log("info", "Disconnecting component, <stream:error> is: %s", reason);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 elseif type(reason) == "table" then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 if reason.condition then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 if reason.text then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 if reason.extra then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 stanza:add_child(reason.extra);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 session.send(stanza);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 elseif reason.name then -- a stanza
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 session.send(reason);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 session.send("</stream:stream>");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 session.conn.close();
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 component_listener.disconnect(session.conn, "stream error");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 --- Component connlistener
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 function component_listener.listener(conn, data)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 local session = sessions[conn];
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 if not session then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 local _send = conn.write;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 session = { type = "component", conn = conn, send = function (data) return _send(tostring(data)); end };
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 sessions[conn] = session;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 -- Logging functions --
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 local conn_name = "jcp"..tostring(conn):match("[a-f0-9]+$");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 session.log = logger.init(conn_name);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 session.close = session_close;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 session.log("info", "Incoming Jabber component connection");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 session.parser = parser;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 session.notopen = true;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 function session.data(conn, data)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 local ok, err = parser:parse(data);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 if ok then return; end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 session:close("xml-not-well-formed");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 session.dispatch_stanza = stream_callbacks.handlestanza;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 if data then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 session.data(conn, data);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 function component_listener.disconnect(conn, err)
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 local session = sessions[conn];
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 if session then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 (session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err));
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 if session.host then
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 log("debug", "Deregistering component");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 cm_deregister_component(session.host);
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 hosts[session.host].connected = nil;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 sessions[conn] = nil;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 session = nil;
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 collectgarbage("collect");
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169
b51a65066595 prosody, xmppcomponent_listener: Add listener for XEP-0114 component connections
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 connlisteners.register('xmppcomponent', component_listener);

mercurial