client.lua

Sat, 28 Nov 2009 22:30:25 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 28 Nov 2009 22:30:25 +0000
changeset 13
c3d83b98fb4f
parent 12
73f466054ead
child 21
00da62000b83
permissions
-rw-r--r--

verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively

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
13
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
57 self.curr_id = 0;
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
58
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
59 self.tracked_iqs = {};
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
60 self:hook("stanza", function (stanza)
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
61 local id, type = stanza.attr.id, stanza.attr.type;
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
62 if id and stanza.name == "iq" and (type == "result" or type == "error") and self.tracked_iqs[id] then
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
63 self.tracked_iqs[id](stanza);
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
64 self.tracked_iqs[id] = nil;
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
65 return true;
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
66 end
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
67 end);
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
68
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 -- Initialise connection
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 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
71 --reset_stream(self);
ce349990bd21 verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
72 self:reopen();
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
11
ce349990bd21 verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
75 function stream:reopen()
ce349990bd21 verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
76 reset_stream(self);
ce349990bd21 verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
77 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
78 end
ce349990bd21 verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
79
12
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
80 function stream:close(reason)
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
81 if not self.notopen then
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
82 self:send("</stream:stream>");
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
83 end
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
84 self.conn:close();
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
85 end
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
86
13
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
87 function stream:send_iq(iq, callback)
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
88 local id = self:new_id();
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
89 self.tracked_iqs[id] = callback;
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
90 iq.attr.id = id;
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
91 self:send(iq);
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
92 end
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
93
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
94 function stream:new_id()
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
95 self.curr_id = self.curr_id + 1;
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
96 return tostring(self.curr_id);
c3d83b98fb4f verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
97 end

mercurial