Software /
code /
verse
Annotate
client.lua @ 11:ce349990bd21
verse.client: Add stream:reopen()
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 28 Nov 2009 22:29:09 +0000 |
parent | 10:3a422606a040 |
child | 12:73f466054ead |
rev | line source |
---|---|
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local verse = require "verse2"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local stream = verse.stream_mt; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local jid_split = require "jid".split; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local lxp = require "lxp"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local st = require "util.stanza"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local init_xmlhandlers = require "xmlhandlers"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
10
3a422606a040
verse.client: Fire events on stream features, errors, etc. and on non-stream tags such as SASL and TLS
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
9 local xmlns_stream = "http://etherx.jabber.org/streams"; |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
10
3a422606a040
verse.client: Fire events on stream features, errors, etc. and on non-stream tags such as SASL and TLS
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
11 local stream_callbacks = { stream_tag = xmlns_stream.."|stream", |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 default_ns = "jabber:client" }; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 function stream_callbacks.streamopened(stream, attr) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 if not stream:event("opened") then |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 stream.notopen = nil; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 return true; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 function stream_callbacks.streamclosed(stream) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 return stream:event("closed"); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 function stream_callbacks.handlestanza(stream, stanza) |
10
3a422606a040
verse.client: Fire events on stream features, errors, etc. and on non-stream tags such as SASL and TLS
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
26 if stanza.attr.xmlns == xmlns_stream then |
3a422606a040
verse.client: Fire events on stream features, errors, etc. and on non-stream tags such as SASL and TLS
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
27 return stream:event("stream-"..stanza.name, stanza); |
3a422606a040
verse.client: Fire events on stream features, errors, etc. and on non-stream tags such as SASL and TLS
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
28 elseif stanza.attr.xmlns then |
3a422606a040
verse.client: Fire events on stream features, errors, etc. and on non-stream tags such as SASL and TLS
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
29 return stream:event("stream/"..stanza.attr.xmlns, stanza); |
3a422606a040
verse.client: Fire events on stream features, errors, etc. and on non-stream tags such as SASL and TLS
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
30 end |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 return stream:event("stanza", stanza); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local function reset_stream(stream) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 -- Reset stream |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local parser = lxp.new(init_xmlhandlers(stream, stream_callbacks), "|"); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 stream.parser = parser; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 stream.notopen = true; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 function stream.data(conn, data) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 local ok, err = parser:parse(data); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 if ok then return; end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 stream:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " ")); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 stream:close("xml-not-well-formed"); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 return true; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 function stream:connect_client(jid, pass) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 self.jid, self.password = jid, pass; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 self.username, self.host, self.resource = jid_split(jid); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 self:hook("incoming-raw", function (data) return self.data(self.conn, data); end); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 -- Initialise connection |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 self:connect(self.connect_host or self.host, self.connect_port or 5222); |
11
ce349990bd21
verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
59 --reset_stream(self); |
ce349990bd21
verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
60 self:reopen(); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
11
ce349990bd21
verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
63 function stream:reopen() |
ce349990bd21
verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
64 reset_stream(self); |
ce349990bd21
verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
65 self:send(st.stanza("stream:stream", { to = self.host, ["xmlns:stream"]='http://etherx.jabber.org/streams', xmlns = "jabber:client" }):top_tag()); |
ce349990bd21
verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
66 end |
ce349990bd21
verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
67 |