annotate mod_component_client/mod_component_client.lua @ 1209:fc39f78e2b36

mod_component_client: Added component_client.exit_on_disconnect config option
author Waqas Hussain <waqas20@gmail.com>
date Fri, 11 Oct 2013 16:59:23 -0400
parents defa479a7d53
children be5334e3f6ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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");
1209
fc39f78e2b36 mod_component_client: Added component_client.exit_on_disconnect config option
Waqas Hussain <waqas20@gmail.com>
parents: 1208
diff changeset
39 local exit_on_disconnect = config.exit_on_disconnect;
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
40
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 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
42
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 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
44 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
45
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 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
47 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
48
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 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
50
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 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
52 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
53 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
54 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
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68 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
69 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
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 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
73 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
74 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
75 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
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 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
78
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 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
80 -- 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
81 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
82 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
83 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
84 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
85 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
86
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 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
88 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
89 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
90 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
91
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 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
93 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
94 module:log("debug", "Handshake complete");
1208
defa479a7d53 mod_component_client: Fire connect (logged in) and disconnect events
Waqas Hussain <waqas20@gmail.com>
parents: 993
diff changeset
95 module:fire_event("component_client/connected", {});
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
96 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
97 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
98
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 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
100 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
101 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
102
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 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
104 -- 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
105 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
106 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
107 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
108 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
109 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
110 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
111 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
112 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
113 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
114 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
115 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
116 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
117 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
118 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
119 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
120 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
121 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
122 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
123 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
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 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
126 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
127
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 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
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136 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
137 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
138 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
139 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
140 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
141 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
142 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
143 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
144 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
145 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
146 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
147 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
148 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
149 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
150 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
151 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
152 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
153 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
154 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
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 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
157 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
158 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
159 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
160 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
161 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
162 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
163
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 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
165 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
166
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 -- 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
168 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
169 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
170 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
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 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
173
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 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
175 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
176
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 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
178 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
179 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
180 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
181 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
182 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
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.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
185
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 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
187 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
188 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
189 ["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
190 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
191 }):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
192
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 --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
194 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
195 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
196 --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
197 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
198 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
199 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
200 --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
201 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
202 (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
203 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
204 --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
205 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
206 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
207 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
208 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
209 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
210 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
211 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
212 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
213 __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
214 module:log("error", "connection lost");
1208
defa479a7d53 mod_component_client: Fire connect (logged in) and disconnect events
Waqas Hussain <waqas20@gmail.com>
parents: 993
diff changeset
215 module:fire_event("component_client/disconnected", { reason = err });
1209
fc39f78e2b36 mod_component_client: Added component_client.exit_on_disconnect config option
Waqas Hussain <waqas20@gmail.com>
parents: 1208
diff changeset
216 if exit_on_disconnect then
fc39f78e2b36 mod_component_client: Added component_client.exit_on_disconnect config option
Waqas Hussain <waqas20@gmail.com>
parents: 1208
diff changeset
217 prosody.shutdown("Shutdown by component_client disconnect");
fc39f78e2b36 mod_component_client: Added component_client.exit_on_disconnect config option
Waqas Hussain <waqas20@gmail.com>
parents: 1208
diff changeset
218 end
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
219 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
220
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 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
222 ------------------------
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 -- 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
224 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
225 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
226 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
227 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
228 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
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
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 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
232 __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
233 ------------------------
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
234 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
235 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
236 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
237