Annotate

client.lua @ 30:9c96318913f7

Revert module names throughout to their Prosody equivalents
author Matthew Wild <mwild1@gmail.com>
date Wed, 09 Dec 2009 20:58:04 +0000
parent 28:afe9e6d6c87a
child 37:0ccd523e110a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
6c5fab6c11cf Rename verse2 -> verse
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
1 local verse = require "verse";
1
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
30
9c96318913f7 Revert module names throughout to their Prosody equivalents
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
4 local jid_split = require "util.jid".split;
1
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";
22
e6fad7c411fe verse.client: Extend verse object with message/iq/presence/stanza methods from util.stanza
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
7
e6fad7c411fe verse.client: Extend verse object with message/iq/presence/stanza methods from util.stanza
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
8 -- Shortcuts to save having to load util.stanza
25
92d3a333ea8a verse.client: Add verse.reply() as shortcut for util.stanza.reply()
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
9 verse.message, verse.presence, verse.iq, verse.stanza, verse.reply =
92d3a333ea8a verse.client: Add verse.reply() as shortcut for util.stanza.reply()
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
10 st.message, st.presence, st.iq, st.stanza, st.reply;
22
e6fad7c411fe verse.client: Extend verse object with message/iq/presence/stanza methods from util.stanza
Matthew Wild <mwild1@gmail.com>
parents: 21
diff changeset
11
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local init_xmlhandlers = require "xmlhandlers";
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
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
14 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
15
21
00da62000b83 verse.client: Fixes for new xmlhandlers namespace seperator
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
16 local stream_callbacks = { stream_tag = xmlns_stream.."\1stream",
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 default_ns = "jabber:client" };
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 function stream_callbacks.streamopened(stream, attr)
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 if not stream:event("opened") then
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 stream.notopen = nil;
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 return true;
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 function stream_callbacks.streamclosed(stream)
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 return stream:event("closed");
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 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
31 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
32 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
33 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
34 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
35 end
28
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
36
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
37 stream:hook("stanza", function (stanza)
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
38 if stanza.attr.xmlns == nil or stanza.attr.xmlns == "jabber:client" then
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
39 if stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
40 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
41 if xmlns then
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
42 ret = stream:event("iq/"..xmlns, stanza);
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
43 if not ret then
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
44 ret = stream:event("iq", stanza);
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
45 end
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
46 end
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
47 else
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
48 ret = stream:event(stanza.name, stanza);
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
49 end
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
50 end
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
51 return ret;
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
52 end, -1);
afe9e6d6c87a verse.client: New stanza dispatcher to fire events based on the name (and in the case of iq, xmlns) of the stanza
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
53
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 return stream:event("stanza", stanza);
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
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 local function reset_stream(stream)
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 -- Reset stream
21
00da62000b83 verse.client: Fixes for new xmlhandlers namespace seperator
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
59 local parser = lxp.new(init_xmlhandlers(stream, stream_callbacks), "\1");
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 stream.parser = parser;
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 stream.notopen = true;
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 function stream.data(conn, data)
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local ok, err = parser:parse(data);
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 if ok then return; end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 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
68 stream:close("xml-not-well-formed");
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 return true;
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 function stream:connect_client(jid, pass)
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 self.jid, self.password = jid, pass;
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 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
77
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 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
79
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
80 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
81
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
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 -- Initialise connection
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 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
94 --reset_stream(self);
ce349990bd21 verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
95 self:reopen();
1
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end
7c8d0a2fc004 Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
11
ce349990bd21 verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
98 function stream:reopen()
ce349990bd21 verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
99 reset_stream(self);
ce349990bd21 verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
100 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
101 end
ce349990bd21 verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
102
12
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
103 function stream:close(reason)
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
104 if not self.notopen then
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
105 self:send("</stream:stream>");
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
106 end
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
107 self.conn:close();
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
108 end
73f466054ead verse.client: Add stream:close()
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
109
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
110 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
111 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
112 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
113 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
114 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
115 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
116
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
117 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
118 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
119 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
120 end