plugins/mod_component.lua

Wed, 22 Apr 2009 21:32:23 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 22 Apr 2009 21:32:23 +0100
changeset 1042
a3d77353c18a
parent 981
71fce47dff7b
child 1108
368754c54045
permissions
-rw-r--r--

mod_*: Fix a load of global accesses

902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM v0.4
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if module:get_host_type() ~= "component" then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 error("Don't load mod_component manually, it should be for a component, please see http://prosody.im/doc/components", 0);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
13 local hosts = _G.hosts;
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
14
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local t_concat = table.concat;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
17 local lxp = require "lxp";
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
18 local logger = require "util.logger";
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
19 local config = require "core.configmanager";
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local connlisteners = require "net.connlisteners";
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local cm_register_component = require "core.componentmanager".register_component;
981
71fce47dff7b mod_component: Deregister component on disconnect
Matthew Wild <mwild1@gmail.com>
parents: 979
diff changeset
22 local cm_deregister_component = require "core.componentmanager".deregister_component;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local uuid_gen = require "util.uuid".generate;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local sha1 = require "util.hashes".sha1;
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
25 local st = require "util.stanza";
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local init_xmlhandlers = require "core.xmlhandlers";
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local sessions = {};
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local log = module._log;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
909
505a2cbb823d mod_component: Set default listening interface to 127.0.0.1
Matthew Wild <mwild1@gmail.com>
parents: 902
diff changeset
32 local component_listener = { default_port = 5347; default_mode = "*a"; default_interface = config.get("*", "core", "component_interface") or "127.0.0.1" };
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local xmlns_component = 'jabber:component:accept';
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 --- Callbacks/data for xmlhandlers to handle streams for us ---
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", default_ns = xmlns_component };
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 function stream_callbacks.error(session, error, data, data2)
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 log("warn", "Error processing component stream: "..tostring(error));
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if error == "no-stream" then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 session:close("invalid-namespace");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 elseif error == "xml-parse-error" and data == "unexpected-element-close" then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 session.log("debug", "Unexpected close of '%s' tag", data2);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 session:close("xml-not-well-formed");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 else
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 session.log("debug", "External component %s XML parse error: %s", tostring(session.host), tostring(error));
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 print(data, debug.traceback())
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 session:close("xml-not-well-formed");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 function stream_callbacks.streamopened(session, attr)
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 if config.get(attr.to, "core", "component_module") ~= "component" then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 -- Trying to act as a component domain which
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 -- hasn't been configured
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 session:close{ condition = "host-unknown", text = tostring(attr.to).." does not match any configured external components" };
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 return;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 -- Store the original host (this is used for config, etc.)
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 session.user = attr.to;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 -- Set the host for future reference
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 session.host = config.get(attr.to, "core", "component_address") or attr.to;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 -- Note that we don't create the internal component
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 -- until after the external component auths successfully
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 session.streamid = uuid_gen();
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 session.notopen = nil;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 session.send(st.stanza("stream:stream", { xmlns=xmlns_component,
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag());
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 function stream_callbacks.streamclosed(session)
959
e3db909065f2 sessionmanager, s2smanager, mod_component: Send reply </stream:stream> when client closes stream
Matthew Wild <mwild1@gmail.com>
parents: 914
diff changeset
78 session.send("</stream:stream>");
e3db909065f2 sessionmanager, s2smanager, mod_component: Send reply </stream:stream> when client closes stream
Matthew Wild <mwild1@gmail.com>
parents: 914
diff changeset
79 session.notopen = true;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local core_process_stanza = core_process_stanza;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 function stream_callbacks.handlestanza(session, stanza)
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 -- Namespaces are icky.
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 log("warn", "Handing stanza with name %s", stanza.name);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 if stanza.name ~= "handshake" then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 return core_process_stanza(session, stanza);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 else
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 handle_component_auth(session, stanza);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 --- Handle authentication attempts by components
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 function handle_component_auth(session, stanza)
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 if (not session.host) or #stanza.tags > 0 then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 session:close("not-authorized");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 return;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local secret = config.get(session.user, "core", "component_secret");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 if not secret then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 log("warn", "Component attempted to identify as %s, but component_password is not set", session.user);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 session:close("not-authorized");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 return;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 local supplied_token = t_concat(stanza);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local calculated_token = sha1(session.streamid..secret, true);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 if supplied_token:lower() ~= calculated_token:lower() then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 return;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 -- Authenticated now
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 -- If component not already created for this host, create one now
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 if not hosts[session.host].connected then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 local send = session.send;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 session.component_session = cm_register_component(session.host, function (_, data) return send(data); end);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 hosts[session.host].connected = true;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 else
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 -- Signal successful authentication
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 session.send(st.stanza("handshake"));
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 module:add_handler("component", "handshake", xmlns_component, handle_component_auth);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 --- Closing a component connection
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 local function session_close(session, reason)
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local log = session.log or log;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 if session.conn then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 if reason then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 if type(reason) == "string" then -- assume stream error
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 log("info", "Disconnecting component, <stream:error> is: %s", reason);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 elseif type(reason) == "table" then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 if reason.condition then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 if reason.text then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 if reason.extra then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 stanza:add_child(reason.extra);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza));
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 session.send(stanza);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 elseif reason.name then -- a stanza
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason));
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 session.send(reason);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 session.send("</stream:stream>");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 session.conn.close();
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 component_listener.disconnect(session.conn, "stream error");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 --- Component connlistener
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 function component_listener.listener(conn, data)
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 local session = sessions[conn];
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 if not session then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 local _send = conn.write;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 session = { type = "component", conn = conn, send = function (data) return _send(tostring(data)); end };
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 sessions[conn] = session;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 -- Logging functions --
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 local conn_name = "xep114-"..tostring(conn):match("[a-f0-9]+$");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 session.log = logger.init(conn_name);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 session.close = session_close;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 session.log("info", "Incoming XEP-0114 connection");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 session.parser = parser;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 session.notopen = true;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 function session.data(conn, data)
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 local ok, err = parser:parse(data);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 if ok then return; end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 session:close("xml-not-well-formed");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 session.dispatch_stanza = stream_callbacks.handlestanza;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 if data then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 session.data(conn, data);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 function component_listener.disconnect(conn, err)
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 local session = sessions[conn];
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 if session then
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 (session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err));
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 if session.host then
981
71fce47dff7b mod_component: Deregister component on disconnect
Matthew Wild <mwild1@gmail.com>
parents: 979
diff changeset
205 log("debug", "deregistering component");
71fce47dff7b mod_component: Deregister component on disconnect
Matthew Wild <mwild1@gmail.com>
parents: 979
diff changeset
206 cm_deregister_component(session.host);
71fce47dff7b mod_component: Deregister component on disconnect
Matthew Wild <mwild1@gmail.com>
parents: 979
diff changeset
207 hosts[session.host].connected = nil;
902
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 sessions[conn] = nil;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 session = nil;
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 collectgarbage("collect");
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 connlisteners.register('component', component_listener);
00daf63c129e Add initial mod_component for XEP-0114 support. Albert, where are you?
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216
910
27d909db4714 mod_component: Use net_activate_ports to start port listener based on config
Matthew Wild <mwild1@gmail.com>
parents: 909
diff changeset
217 module:add_event_hook("server-started",
27d909db4714 mod_component: Use net_activate_ports to start port listener based on config
Matthew Wild <mwild1@gmail.com>
parents: 909
diff changeset
218 function ()
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
219 if _G.net_activate_ports then
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 981
diff changeset
220 _G.net_activate_ports("component", "component", {5437}, "tcp");
910
27d909db4714 mod_component: Use net_activate_ports to start port listener based on config
Matthew Wild <mwild1@gmail.com>
parents: 909
diff changeset
221 else
27d909db4714 mod_component: Use net_activate_ports to start port listener based on config
Matthew Wild <mwild1@gmail.com>
parents: 909
diff changeset
222 error("No net_activate_ports: Using an incompatible version of Prosody?");
27d909db4714 mod_component: Use net_activate_ports to start port listener based on config
Matthew Wild <mwild1@gmail.com>
parents: 909
diff changeset
223 end
27d909db4714 mod_component: Use net_activate_ports to start port listener based on config
Matthew Wild <mwild1@gmail.com>
parents: 909
diff changeset
224 end);

mercurial