Software /
code /
prosody-modules
Annotate
mod_component_client/mod_component_client.lua @ 1204:fc42f8484451
mod_s2s_keysize_policy: Add note about required LuaSec patch
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 29 Sep 2013 19:11:29 +0200 |
parent | 993:8b14cdfe0213 |
child | 1208:defa479a7d53 |
rev | line source |
---|---|
993
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 --[[ |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 mod_component_client.lua |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 This module turns Prosody hosts into components of other XMPP servers. |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 Config: |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 VirtualHost "component.example.com" |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 component_client = { |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 host = "localhost"; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 port = 5347; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 secret = "hunter2"; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 } |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 ]] |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 local socket = require "socket" |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local logger = require "util.logger"; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 local sha1 = require "util.hashes".sha1; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local st = require "util.stanza"; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 local jid_split = require "util.jid".split; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 local new_xmpp_stream = require "util.xmppstream".new; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 local uuid_gen = require "util.uuid".generate; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 local core_process_stanza = prosody.core_process_stanza; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 local hosts = prosody.hosts; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 local log = module._log; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 local config = module:get_option("component_client", {}); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 local server_host = config.host or "localhost"; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 local server_port = config.port or 5347; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 local server_secret = config.secret or error("client_component.secret not provided"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 local __conn; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 local listener = {}; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 local session; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 local xmlns_component = 'jabber:component:accept'; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 local stream_callbacks = { default_ns = xmlns_component }; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams"; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 function stream_callbacks.error(session, error, data, data2) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 if session.destroyed then return; end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 module:log("warn", "Error processing component stream: %s", tostring(error)); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 if error == "no-stream" then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 session:close("invalid-namespace"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 elseif error == "parse-error" then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 session.log("warn", "External component %s XML parse error: %s", tostring(session.host), tostring(data)); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 session:close("not-well-formed"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 elseif error == "stream-error" then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 local condition, text = "undefined-condition"; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 for child in data:children() do |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 if child.attr.xmlns == xmlns_xmpp_streams then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 if child.name ~= "text" then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 condition = child.name; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 else |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 text = child:get_text(); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 if condition ~= "undefined-condition" and text then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 break; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 text = condition .. (text and (" ("..text..")") or ""); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 session.log("info", "Session closed by remote with error: %s", text); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 session:close(nil, text); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 function stream_callbacks.streamopened(session, attr) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 -- TODO check id~=nil, from==module.host |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 module:log("debug", "Sending handshake"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 local handshake = st.stanza("handshake"):text(sha1(attr.id..server_secret, true)); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 session.send(handshake); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 session.notopen = nil; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 function stream_callbacks.streamclosed(session) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 session.log("debug", "Received </stream:stream>"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 session:close(); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 module:hook("stanza/jabber:component:accept:handshake", function(event) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 session.type = "component"; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 module:log("debug", "Handshake complete"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 return true; -- READY! |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 end); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 module:hook("route/remote", function(event) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 return session and session.send(event.stanza); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 end); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 function stream_callbacks.handlestanza(session, stanza) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 -- Namespaces are icky. |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 if not stanza.attr.xmlns and stanza.name == "handshake" then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 stanza.attr.xmlns = xmlns_component; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 if not stanza.attr.xmlns or stanza.attr.xmlns == "jabber:client" then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 if not stanza.attr.from then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 session.log("warn", "Rejecting stanza with no 'from' address"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 session.send(st.error_reply(stanza, "modify", "bad-request", "Components MUST get a 'from' address on stanzas")); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 return; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 local _, domain = jid_split(stanza.attr.to); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 if not domain then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 session.log("warn", "Rejecting stanza with no 'to' address"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 session.send(st.error_reply(stanza, "modify", "bad-request", "Components MUST get a 'to' address on stanzas")); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 return; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 elseif domain ~= session.host then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 session.log("warn", "Component received stanza with unknown 'to' address"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 session.send(st.error_reply(stanza, "cancel", "not-allowed", "Component doesn't serve this JID")); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 return; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 return core_process_stanza(session, stanza); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" }; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 local function session_close(session, reason) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 if session.destroyed then return; end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 if session.conn then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 if session.notopen then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 session.send("<?xml version='1.0'?>"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 session.send(st.stanza("stream:stream", default_stream_attr):top_tag()); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 if reason then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 if type(reason) == "string" then -- assume stream error |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 module:log("info", "Disconnecting component, <stream:error> is: %s", reason); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' })); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 elseif type(reason) == "table" then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 if reason.condition then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up(); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 if reason.text then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 stanza:tag("text", stream_xmlns_attr):text(reason.text):up(); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 if reason.extra then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 stanza:add_child(reason.extra); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 module:log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza)); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 session.send(stanza); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 elseif reason.name then -- a stanza |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 module:log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason)); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 session.send(reason); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 session.send("</stream:stream>"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 session.conn:close(); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 listener.ondisconnect(session.conn, "stream error"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 function listener.onconnect(conn) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 session = { type = "component_unauthed", conn = conn, send = function (data) return conn:write(tostring(data)); end, host = module.host }; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 -- Logging functions -- |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 local conn_name = "jcp"..tostring(session):match("[a-f0-9]+$"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 session.log = logger.init(conn_name); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 session.close = session_close; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 session.log("info", "Outgoing Jabber component connection"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
171 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
172 local stream = new_xmpp_stream(session, stream_callbacks); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 session.stream = stream; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
175 function session.data(conn, data) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
176 local ok, err = stream:feed(data); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
177 if ok then return; end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
178 module:log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_")); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 session:close("not-well-formed"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 session.dispatch_stanza = stream_callbacks.handlestanza; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 session.notopen = true; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 session.send(st.stanza("stream:stream", { |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 to = session.host; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 ["xmlns:stream"] = 'http://etherx.jabber.org/streams'; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 xmlns = xmlns_component; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 }):top_tag()); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 --sessions[conn] = session; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 function listener.onincoming(conn, data) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 --local session = sessions[conn]; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 session.data(conn, data); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
196 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 function listener.ondisconnect(conn, err) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 --local session = sessions[conn]; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 if session then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 (session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err)); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 if session.on_destroy then session:on_destroy(err); end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 --sessions[conn] = nil; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
203 for k in pairs(session) do |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
204 if k ~= "log" and k ~= "close" then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 session[k] = nil; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
206 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
207 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 session.destroyed = true; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
209 session = nil; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 __conn = nil; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 module:log("error", "connection lost"); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
213 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
214 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 function connect() |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
216 ------------------------ |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 -- Taken from net.http |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 local conn = socket.tcp ( ) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
219 conn:settimeout ( 10 ) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
220 local ok, err = conn:connect ( server_host , server_port ) |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
221 if not ok and err ~= "timeout" then |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 return nil, err; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
223 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
224 |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
225 local handler , conn = server.wrapclient ( conn , server_host , server_port , listener , "*l") |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
226 __conn = conn; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 ------------------------ |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
228 return true; |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
229 end |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
230 assert(connect()); |
8b14cdfe0213
mod_component_client: Initial commit. Allows Prosody to act as an external component for other XMPP servers.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
231 |