28 end |
28 end |
29 end |
29 end |
30 |
30 |
31 function proxy_component(origin, stanza) |
31 function proxy_component(origin, stanza) |
32 local ibb_tag = stanza.tags[1]; |
32 local ibb_tag = stanza.tags[1]; |
33 if (not (stanza.name == "iq" and stanza.attr.type == "set") |
33 if (not (stanza.name == "iq" and stanza.attr.type == "set") |
34 and stanza.name ~= "message") |
34 and stanza.name ~= "message") |
35 or |
35 or |
36 (not (ibb_tag) |
36 (not (ibb_tag) |
37 or ibb_tag.attr.xmlns ~= xmlns_ibb) then |
37 or ibb_tag.attr.xmlns ~= xmlns_ibb) then |
38 if stanza.attr.type ~= "error" then |
38 if stanza.attr.type ~= "error" then |
39 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
39 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
40 end |
40 end |
41 return; |
41 return; |
42 end |
42 end |
43 |
43 |
44 if ibb_tag.name == "open" then |
44 if ibb_tag.name == "open" then |
45 -- Starting a new stream |
45 -- Starting a new stream |
46 local to_host, to_port = ibb_tag.attr[host_attr], ibb_tag.attr[port_attr]; |
46 local to_host, to_port = ibb_tag.attr[host_attr], ibb_tag.attr[port_attr]; |
47 local jid, sid = stanza.attr.from, ibb_tag.attr.sid; |
47 local jid, sid = stanza.attr.from, ibb_tag.attr.sid; |
48 if not (to_host and to_port) then |
48 if not (to_host and to_port) then |
55 local conn, err = socket.tcp(); |
55 local conn, err = socket.tcp(); |
56 if not conn then |
56 if not conn then |
57 return origin.send(st.error_reply(stanza, "wait", "resource-constraint", err)); |
57 return origin.send(st.error_reply(stanza, "wait", "resource-constraint", err)); |
58 end |
58 end |
59 conn:settimeout(0); |
59 conn:settimeout(0); |
60 |
60 |
61 local success, err = conn:connect(to_host, to_port); |
61 local success, err = conn:connect(to_host, to_port); |
62 if not success and err ~= "timeout" then |
62 if not success and err ~= "timeout" then |
63 return origin.send(st.error_reply(stanza, "wait", "remote-server-not-found", err)); |
63 return origin.send(st.error_reply(stanza, "wait", "remote-server-not-found", err)); |
64 end |
64 end |
65 |
65 |
66 local listener,seq = {}, 0; |
66 local listener,seq = {}, 0; |
67 function listener.onconnect(conn) |
67 function listener.onconnect(conn) |
68 origin.send(st.reply(stanza)); |
68 origin.send(st.reply(stanza)); |
69 end |
69 end |
70 function listener.onincoming(conn, data) |
70 function listener.onincoming(conn, data) |
76 function listener.ondisconnect(conn, err) |
76 function listener.ondisconnect(conn, err) |
77 origin.send(st.message({to=jid,from=host}) |
77 origin.send(st.message({to=jid,from=host}) |
78 :tag("close", {xmlns=xmlns_ibb,sid=sid})); |
78 :tag("close", {xmlns=xmlns_ibb,sid=sid})); |
79 close_session(jid, sid); |
79 close_session(jid, sid); |
80 end |
80 end |
81 |
81 |
82 conn = server.wrapclient(conn, to_host, to_port, listener, "*a" ); |
82 conn = server.wrapclient(conn, to_host, to_port, listener, "*a" ); |
83 new_session(jid, sid, conn); |
83 new_session(jid, sid, conn); |
84 elseif ibb_tag.name == "data" then |
84 elseif ibb_tag.name == "data" then |
85 local conn = open_connections[stanza.attr.from][ibb_tag.attr.sid]; |
85 local conn = open_connections[stanza.attr.from][ibb_tag.attr.sid]; |
86 if conn then |
86 if conn then |