37 end |
37 end |
38 |
38 |
39 return stream:event("stanza", stanza); |
39 return stream:event("stanza", stanza); |
40 end |
40 end |
41 |
41 |
42 local function reset_stream(stream) |
42 function stream:reset() |
43 -- Reset stream |
43 -- Reset stream |
44 local parser = lxp.new(init_xmlhandlers(stream, stream_callbacks), "\1"); |
44 local parser = lxp.new(init_xmlhandlers(self, stream_callbacks), "\1"); |
45 stream.parser = parser; |
45 self.parser = parser; |
46 |
46 |
47 stream.notopen = true; |
47 self.notopen = true; |
48 |
|
49 function stream.data(conn, data) |
|
50 local ok, err = parser:parse(data); |
|
51 if ok then return; end |
|
52 stream:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " ")); |
|
53 stream:close("xml-not-well-formed"); |
|
54 end |
|
55 |
48 |
56 return true; |
49 return true; |
57 end |
50 end |
58 |
51 |
59 function stream:connect_client(jid, pass) |
52 function stream:connect_client(jid, pass) |
63 -- Required XMPP features |
56 -- Required XMPP features |
64 self:add_plugin("tls"); |
57 self:add_plugin("tls"); |
65 self:add_plugin("sasl"); |
58 self:add_plugin("sasl"); |
66 self:add_plugin("bind"); |
59 self:add_plugin("bind"); |
67 self:add_plugin("session"); |
60 self:add_plugin("session"); |
|
61 |
|
62 function self.data(conn, data) |
|
63 local ok, err = self.parser:parse(data); |
|
64 if ok then return; end |
|
65 stream:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " ")); |
|
66 stream:close("xml-not-well-formed"); |
|
67 end |
68 |
68 |
69 self:hook("incoming-raw", function (data) return self.data(self.conn, data); end); |
69 self:hook("incoming-raw", function (data) return self.data(self.conn, data); end); |
70 |
70 |
71 self.curr_id = 0; |
71 self.curr_id = 0; |
72 |
72 |
97 return ret; |
97 return ret; |
98 end, -1); |
98 end, -1); |
99 |
99 |
100 -- Initialise connection |
100 -- Initialise connection |
101 self:connect(self.connect_host or self.host, self.connect_port or 5222); |
101 self:connect(self.connect_host or self.host, self.connect_port or 5222); |
102 --reset_stream(self); |
|
103 self:reopen(); |
102 self:reopen(); |
104 end |
103 end |
105 |
104 |
106 function stream:reopen() |
105 function stream:reopen() |
107 reset_stream(self); |
106 self:reset(); |
108 self:send(st.stanza("stream:stream", { to = self.host, ["xmlns:stream"]='http://etherx.jabber.org/streams', |
107 self:send(st.stanza("stream:stream", { to = self.host, ["xmlns:stream"]='http://etherx.jabber.org/streams', |
109 xmlns = "jabber:client", version = "1.0" }):top_tag()); |
108 xmlns = "jabber:client", version = "1.0" }):top_tag()); |
110 end |
109 end |
111 |
110 |
112 function stream:close(reason) |
111 function stream:close(reason) |