Software /
code /
verse
Annotate
client.lua @ 1:7c8d0a2fc004
Break client-specific code into verse.client module
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 04 Aug 2009 18:25:56 +0100 |
child | 10:3a422606a040 |
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 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 default_ns = "jabber:client" }; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 function stream_callbacks.streamopened(stream, attr) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 if not stream:event("opened") then |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 stream.notopen = nil; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 return true; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 function stream_callbacks.streamclosed(stream) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 return stream:event("closed"); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 function stream_callbacks.handlestanza(stream, stanza) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 return stream:event("stanza", stanza); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local function reset_stream(stream) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 -- Reset stream |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 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
|
31 stream.parser = parser; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 stream.notopen = true; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 function stream.data(conn, data) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local ok, err = parser:parse(data); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 if ok then return; end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 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
|
39 stream:close("xml-not-well-formed"); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 return true; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 function stream:connect_client(jid, pass) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 self.jid, self.password = jid, pass; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 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
|
48 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 reset_stream(self); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 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
|
51 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 -- Initialise connection |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 self:connect(self.connect_host or self.host, self.connect_port or 5222); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 self:send(st.stanza("stream:stream", { to = self.host, ["xmlns:stream"]='http://etherx.jabber.org/streams' }):top_tag()); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 |