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