Software /
code /
verse
Annotate
client.lua @ 488:33c922e48b57
util.xstanza: Remove, unused since c95b84ed366b
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 23 May 2023 19:41:27 +0200 |
parent | 460:a523535d8937 |
child | 490:6b2f31da9610 |
rev | line source |
---|---|
26 | 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; |
137
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
5 local adns = require "net.adns"; |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local st = require "util.stanza"; |
457
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
7 local new_id = require "util.id".short; |
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
8 |
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
9 math.randomseed((require"socket".gettime() * 1000000) % 0x80000000); |
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
|
10 |
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 -- Shortcuts to save having to load util.stanza |
52
8416508bfeb4
verse.client: Add verse.error_reply() helper
Matthew Wild <mwild1@gmail.com>
parents:
50
diff
changeset
|
12 verse.message, verse.presence, verse.iq, verse.stanza, verse.reply, verse.error_reply = |
8416508bfeb4
verse.client: Add verse.error_reply() helper
Matthew Wild <mwild1@gmail.com>
parents:
50
diff
changeset
|
13 st.message, st.presence, st.iq, st.stanza, st.reply, st.error_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
|
14 |
457
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
15 function verse.iq(attr) |
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
16 if not attr.id then |
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
17 attr.id = new_id(); |
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
18 end |
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
19 return st.iq(attr); |
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
20 end |
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
21 |
161
b177bcea2006
squishy, verse.client, verse.component, verse.bosh: Port to util.xmppstream instead of xmlhandlers which has been removed from Prosody. Also remove util.ztact from squishy for the same reason.
Matthew Wild <mwild1@gmail.com>
parents:
137
diff
changeset
|
22 local new_xmpp_stream = require "util.xmppstream".new; |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
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
|
24 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
|
25 |
458
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
26 local function compare_srv_priorities(a, b) |
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
27 if a.priority == b.priority then |
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
28 if not a.weight_r then |
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
29 a.weight_r = math.random(); |
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
30 end |
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
31 if not b.weight_r then |
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
32 b.weight_r = math.random(); |
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
33 end |
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
34 return (1 + a.weight) * a.weight_r > (1 + b.weight) * b.weight_r; |
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
35 end |
6c3797c0bb44
client: Correctly randomize SRV targets (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
457
diff
changeset
|
36 return a.priority < b.priority; |
137
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
37 end |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
38 |
48
abccad4b8559
verse.client: Update stream_callbacks format for new xmlhandlers API (thanks to Bill Clark for the patch)
Matthew Wild <mwild1@gmail.com>
parents:
47
diff
changeset
|
39 local stream_callbacks = { |
abccad4b8559
verse.client: Update stream_callbacks format for new xmlhandlers API (thanks to Bill Clark for the patch)
Matthew Wild <mwild1@gmail.com>
parents:
47
diff
changeset
|
40 stream_ns = xmlns_stream, |
abccad4b8559
verse.client: Update stream_callbacks format for new xmlhandlers API (thanks to Bill Clark for the patch)
Matthew Wild <mwild1@gmail.com>
parents:
47
diff
changeset
|
41 stream_tag = "stream", |
abccad4b8559
verse.client: Update stream_callbacks format for new xmlhandlers API (thanks to Bill Clark for the patch)
Matthew Wild <mwild1@gmail.com>
parents:
47
diff
changeset
|
42 default_ns = "jabber:client" }; |
411 | 43 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 function stream_callbacks.streamopened(stream, attr) |
83
8221f3c25fd4
verse.client: Add stream_id property
Matthew Wild <mwild1@gmail.com>
parents:
81
diff
changeset
|
45 stream.stream_id = attr.id; |
8221f3c25fd4
verse.client: Add stream_id property
Matthew Wild <mwild1@gmail.com>
parents:
81
diff
changeset
|
46 if not stream:event("opened", attr) then |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 stream.notopen = nil; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 return true; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 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 function stream_callbacks.streamclosed(stream) |
329
2787e038bea2
verse.client: Use a different flag to indicate that our outgoing stream is closed
Kim Alvefur <zash@zash.se>
parents:
323
diff
changeset
|
53 stream.notopen = true; |
2787e038bea2
verse.client: Use a different flag to indicate that our outgoing stream is closed
Kim Alvefur <zash@zash.se>
parents:
323
diff
changeset
|
54 if not stream.closed then |
323
5bf3b13edb80
verse.client: Try to behave better when stream is closed gracefully
Kim Alvefur <zash@zash.se>
parents:
282
diff
changeset
|
55 stream:send("</stream:stream>"); |
329
2787e038bea2
verse.client: Use a different flag to indicate that our outgoing stream is closed
Kim Alvefur <zash@zash.se>
parents:
323
diff
changeset
|
56 stream.closed = true; |
323
5bf3b13edb80
verse.client: Try to behave better when stream is closed gracefully
Kim Alvefur <zash@zash.se>
parents:
282
diff
changeset
|
57 end |
5bf3b13edb80
verse.client: Try to behave better when stream is closed gracefully
Kim Alvefur <zash@zash.se>
parents:
282
diff
changeset
|
58 stream:event("closed"); |
5bf3b13edb80
verse.client: Try to behave better when stream is closed gracefully
Kim Alvefur <zash@zash.se>
parents:
282
diff
changeset
|
59 return stream:close("stream closed") |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end |
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 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 return stream:event("stanza", stanza); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 |
336
658c62c9ecc4
verse.client: Fire stream error events, so they can be handled
Kim Alvefur <zash@zash.se>
parents:
329
diff
changeset
|
72 function stream_callbacks.error(stream, e, stanza) |
658c62c9ecc4
verse.client: Fire stream error events, so they can be handled
Kim Alvefur <zash@zash.se>
parents:
329
diff
changeset
|
73 if stream:event(e, stanza) == nil then |
364
69fc23b44819
client: Deal with eg no-stream errors where no stanza is passed
Kim Alvefur <zash@zash.se>
parents:
344
diff
changeset
|
74 if stanza then |
69fc23b44819
client: Deal with eg no-stream errors where no stanza is passed
Kim Alvefur <zash@zash.se>
parents:
344
diff
changeset
|
75 local err = stanza:get_child(nil, "urn:ietf:params:xml:ns:xmpp-streams"); |
69fc23b44819
client: Deal with eg no-stream errors where no stanza is passed
Kim Alvefur <zash@zash.se>
parents:
344
diff
changeset
|
76 local text = stanza:get_child_text("text", "urn:ietf:params:xml:ns:xmpp-streams"); |
69fc23b44819
client: Deal with eg no-stream errors where no stanza is passed
Kim Alvefur <zash@zash.se>
parents:
344
diff
changeset
|
77 error(err.name..(text and ": "..text or "")); |
69fc23b44819
client: Deal with eg no-stream errors where no stanza is passed
Kim Alvefur <zash@zash.se>
parents:
344
diff
changeset
|
78 else |
69fc23b44819
client: Deal with eg no-stream errors where no stanza is passed
Kim Alvefur <zash@zash.se>
parents:
344
diff
changeset
|
79 error(stanza and stanza.name or e or "unknown-error"); |
69fc23b44819
client: Deal with eg no-stream errors where no stanza is passed
Kim Alvefur <zash@zash.se>
parents:
344
diff
changeset
|
80 end |
336
658c62c9ecc4
verse.client: Fire stream error events, so they can be handled
Kim Alvefur <zash@zash.se>
parents:
329
diff
changeset
|
81 end |
658c62c9ecc4
verse.client: Fire stream error events, so they can be handled
Kim Alvefur <zash@zash.se>
parents:
329
diff
changeset
|
82 end |
658c62c9ecc4
verse.client: Fire stream error events, so they can be handled
Kim Alvefur <zash@zash.se>
parents:
329
diff
changeset
|
83 |
70
36d113fb0f3c
verse.client: Add stream:reset(), keep self.data static between resets
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
84 function stream:reset() |
161
b177bcea2006
squishy, verse.client, verse.component, verse.bosh: Port to util.xmppstream instead of xmlhandlers which has been removed from Prosody. Also remove util.ztact from squishy for the same reason.
Matthew Wild <mwild1@gmail.com>
parents:
137
diff
changeset
|
85 if self.stream then |
b177bcea2006
squishy, verse.client, verse.component, verse.bosh: Port to util.xmppstream instead of xmlhandlers which has been removed from Prosody. Also remove util.ztact from squishy for the same reason.
Matthew Wild <mwild1@gmail.com>
parents:
137
diff
changeset
|
86 self.stream:reset(); |
b177bcea2006
squishy, verse.client, verse.component, verse.bosh: Port to util.xmppstream instead of xmlhandlers which has been removed from Prosody. Also remove util.ztact from squishy for the same reason.
Matthew Wild <mwild1@gmail.com>
parents:
137
diff
changeset
|
87 else |
b177bcea2006
squishy, verse.client, verse.component, verse.bosh: Port to util.xmppstream instead of xmlhandlers which has been removed from Prosody. Also remove util.ztact from squishy for the same reason.
Matthew Wild <mwild1@gmail.com>
parents:
137
diff
changeset
|
88 self.stream = new_xmpp_stream(self, stream_callbacks); |
b177bcea2006
squishy, verse.client, verse.component, verse.bosh: Port to util.xmppstream instead of xmlhandlers which has been removed from Prosody. Also remove util.ztact from squishy for the same reason.
Matthew Wild <mwild1@gmail.com>
parents:
137
diff
changeset
|
89 end |
70
36d113fb0f3c
verse.client: Add stream:reset(), keep self.data static between resets
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
90 self.notopen = true; |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 return true; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 |
452
628896d39d8e
client: Allow passing SCRAM hashes for use in authentication
Kim Alvefur <zash@zash.se>
parents:
444
diff
changeset
|
94 function stream:connect_client(jid, pass, client_key, server_key) |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 self.jid, self.password = jid, pass; |
452
628896d39d8e
client: Allow passing SCRAM hashes for use in authentication
Kim Alvefur <zash@zash.se>
parents:
444
diff
changeset
|
96 self.client_key, self.server_key = client_key, server_key; |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 self.username, self.host, self.resource = jid_split(jid); |
411 | 98 |
62
d4b6f9e33c6e
verse.client: Load TLS along with other core plugins
Matthew Wild <mwild1@gmail.com>
parents:
52
diff
changeset
|
99 -- Required XMPP features |
d4b6f9e33c6e
verse.client: Load TLS along with other core plugins
Matthew Wild <mwild1@gmail.com>
parents:
52
diff
changeset
|
100 self:add_plugin("tls"); |
38
c40cc28ac7df
verse.client: Automatically load sasl and bind plugins
Matthew Wild <mwild1@gmail.com>
parents:
37
diff
changeset
|
101 self:add_plugin("sasl"); |
c40cc28ac7df
verse.client: Automatically load sasl and bind plugins
Matthew Wild <mwild1@gmail.com>
parents:
37
diff
changeset
|
102 self:add_plugin("bind"); |
411 | 103 |
70
36d113fb0f3c
verse.client: Add stream:reset(), keep self.data static between resets
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
104 function self.data(conn, data) |
161
b177bcea2006
squishy, verse.client, verse.component, verse.bosh: Port to util.xmppstream instead of xmlhandlers which has been removed from Prosody. Also remove util.ztact from squishy for the same reason.
Matthew Wild <mwild1@gmail.com>
parents:
137
diff
changeset
|
105 local ok, err = self.stream:feed(data); |
70
36d113fb0f3c
verse.client: Add stream:reset(), keep self.data static between resets
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
106 if ok then return; end |
400
0db9cb909cf1
client, component: Fix logging of invalid XML
Matthew Wild <mwild1@gmail.com>
parents:
364
diff
changeset
|
107 self:debug("Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " ")); |
171
741f5311d30c
verse.client: Fix two cases where we called methods on the base stream object instead of the current one
Matthew Wild <mwild1@gmail.com>
parents:
166
diff
changeset
|
108 self:close("xml-not-well-formed"); |
70
36d113fb0f3c
verse.client: Add stream:reset(), keep self.data static between resets
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
109 end |
411 | 110 |
199
33b54389ed9c
verse.client: Reopen stream in response to 'connected' event
Matthew Wild <mwild1@gmail.com>
parents:
171
diff
changeset
|
111 self:hook("connected", function () self:reopen(); end); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 self:hook("incoming-raw", function (data) return self.data(self.conn, data); end); |
444
12c1be0044c6
client: Send whitespace keeplives
Kim Alvefur <zash@zash.se>
parents:
430
diff
changeset
|
113 self:hook("read-timeout", function () self:send(" "); return true; end, -1); |
411 | 114 |
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
|
115 self.curr_id = 0; |
411 | 116 |
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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 end); |
411 | 126 |
37
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
127 self:hook("stanza", function (stanza) |
282
52b971d9ebc3
client, component: `ret` was probably meant to be a local
Kim Alvefur <zash@zash.se>
parents:
199
diff
changeset
|
128 local ret; |
37
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
129 if stanza.attr.xmlns == nil or stanza.attr.xmlns == "jabber:client" then |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
130 if stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
131 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns; |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
132 if xmlns then |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
133 ret = self:event("iq/"..xmlns, stanza); |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
134 if not ret then |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
135 ret = self:event("iq", stanza); |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
136 end |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
137 end |
81
0924a3d05e40
Bounce service-unavailable on unhandled iq stanzas (thanks Kev, Florob and anyone else who poked me)
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
138 if ret == nil then |
0924a3d05e40
Bounce service-unavailable on unhandled iq stanzas (thanks Kev, Florob and anyone else who poked me)
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
139 self:send(verse.error_reply(stanza, "cancel", "service-unavailable")); |
0924a3d05e40
Bounce service-unavailable on unhandled iq stanzas (thanks Kev, Florob and anyone else who poked me)
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
140 return true; |
0924a3d05e40
Bounce service-unavailable on unhandled iq stanzas (thanks Kev, Florob and anyone else who poked me)
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
141 end |
37
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
142 else |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
143 ret = self:event(stanza.name, stanza); |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
144 end |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
145 end |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
146 return ret; |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
147 end, -1); |
0ccd523e110a
verse.client: Don't hook the stanza event every time a stanza comes in :)
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
148 |
166
3499b4ea3277
verse.client: Fire 'stanza-out' and {message,presence,iq}'-out' for outgoing stanzas
Matthew Wild <mwild1@gmail.com>
parents:
161
diff
changeset
|
149 self:hook("outgoing", function (data) |
3499b4ea3277
verse.client: Fire 'stanza-out' and {message,presence,iq}'-out' for outgoing stanzas
Matthew Wild <mwild1@gmail.com>
parents:
161
diff
changeset
|
150 if data.name then |
3499b4ea3277
verse.client: Fire 'stanza-out' and {message,presence,iq}'-out' for outgoing stanzas
Matthew Wild <mwild1@gmail.com>
parents:
161
diff
changeset
|
151 self:event("stanza-out", data); |
3499b4ea3277
verse.client: Fire 'stanza-out' and {message,presence,iq}'-out' for outgoing stanzas
Matthew Wild <mwild1@gmail.com>
parents:
161
diff
changeset
|
152 end |
3499b4ea3277
verse.client: Fire 'stanza-out' and {message,presence,iq}'-out' for outgoing stanzas
Matthew Wild <mwild1@gmail.com>
parents:
161
diff
changeset
|
153 end); |
411 | 154 |
166
3499b4ea3277
verse.client: Fire 'stanza-out' and {message,presence,iq}'-out' for outgoing stanzas
Matthew Wild <mwild1@gmail.com>
parents:
161
diff
changeset
|
155 self:hook("stanza-out", function (stanza) |
3499b4ea3277
verse.client: Fire 'stanza-out' and {message,presence,iq}'-out' for outgoing stanzas
Matthew Wild <mwild1@gmail.com>
parents:
161
diff
changeset
|
156 if not stanza.attr.xmlns then |
3499b4ea3277
verse.client: Fire 'stanza-out' and {message,presence,iq}'-out' for outgoing stanzas
Matthew Wild <mwild1@gmail.com>
parents:
161
diff
changeset
|
157 self:event(stanza.name.."-out", stanza); |
3499b4ea3277
verse.client: Fire 'stanza-out' and {message,presence,iq}'-out' for outgoing stanzas
Matthew Wild <mwild1@gmail.com>
parents:
161
diff
changeset
|
158 end |
3499b4ea3277
verse.client: Fire 'stanza-out' and {message,presence,iq}'-out' for outgoing stanzas
Matthew Wild <mwild1@gmail.com>
parents:
161
diff
changeset
|
159 end); |
411 | 160 |
76
927167321283
verse.client: Fire 'ready' event on stream when resource binding or session negotiation is complete, hook this instead of binding-success
Matthew Wild <mwild1@gmail.com>
parents:
70
diff
changeset
|
161 local function stream_ready() |
927167321283
verse.client: Fire 'ready' event on stream when resource binding or session negotiation is complete, hook this instead of binding-success
Matthew Wild <mwild1@gmail.com>
parents:
70
diff
changeset
|
162 self:event("ready"); |
927167321283
verse.client: Fire 'ready' event on stream when resource binding or session negotiation is complete, hook this instead of binding-success
Matthew Wild <mwild1@gmail.com>
parents:
70
diff
changeset
|
163 end |
927167321283
verse.client: Fire 'ready' event on stream when resource binding or session negotiation is complete, hook this instead of binding-success
Matthew Wild <mwild1@gmail.com>
parents:
70
diff
changeset
|
164 self:hook("session-success", stream_ready, -1) |
78
f4188eff53a7
verse.client, verse.plugins.bind, verse.plugins.session: Rename binding-success and binding-failure to bind-success and bind-failure for consistency
Matthew Wild <mwild1@gmail.com>
parents:
76
diff
changeset
|
165 self:hook("bind-success", stream_ready, -1); |
76
927167321283
verse.client: Fire 'ready' event on stream when resource binding or session negotiation is complete, hook this instead of binding-success
Matthew Wild <mwild1@gmail.com>
parents:
70
diff
changeset
|
166 |
97
ad6006779416
verse.client: Update stream:close() to use base stream:close(), and not add an XMPP-specific :close() to the base stream
Matthew Wild <mwild1@gmail.com>
parents:
83
diff
changeset
|
167 local _base_close = self.close; |
ad6006779416
verse.client: Update stream:close() to use base stream:close(), and not add an XMPP-specific :close() to the base stream
Matthew Wild <mwild1@gmail.com>
parents:
83
diff
changeset
|
168 function self:close(reason) |
323
5bf3b13edb80
verse.client: Try to behave better when stream is closed gracefully
Kim Alvefur <zash@zash.se>
parents:
282
diff
changeset
|
169 self.close = _base_close; |
329
2787e038bea2
verse.client: Use a different flag to indicate that our outgoing stream is closed
Kim Alvefur <zash@zash.se>
parents:
323
diff
changeset
|
170 if not self.closed then |
97
ad6006779416
verse.client: Update stream:close() to use base stream:close(), and not add an XMPP-specific :close() to the base stream
Matthew Wild <mwild1@gmail.com>
parents:
83
diff
changeset
|
171 self:send("</stream:stream>"); |
329
2787e038bea2
verse.client: Use a different flag to indicate that our outgoing stream is closed
Kim Alvefur <zash@zash.se>
parents:
323
diff
changeset
|
172 self.closed = true; |
323
5bf3b13edb80
verse.client: Try to behave better when stream is closed gracefully
Kim Alvefur <zash@zash.se>
parents:
282
diff
changeset
|
173 else |
5bf3b13edb80
verse.client: Try to behave better when stream is closed gracefully
Kim Alvefur <zash@zash.se>
parents:
282
diff
changeset
|
174 return self:close(reason); |
97
ad6006779416
verse.client: Update stream:close() to use base stream:close(), and not add an XMPP-specific :close() to the base stream
Matthew Wild <mwild1@gmail.com>
parents:
83
diff
changeset
|
175 end |
ad6006779416
verse.client: Update stream:close() to use base stream:close(), and not add an XMPP-specific :close() to the base stream
Matthew Wild <mwild1@gmail.com>
parents:
83
diff
changeset
|
176 end |
411 | 177 |
137
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
178 local function start_connect() |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
179 -- Initialise connection |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
180 self:connect(self.connect_host or self.host, self.connect_port or 5222); |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
181 end |
411 | 182 |
137
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
183 if not (self.connect_host or self.connect_port) then |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
184 -- Look up SRV records |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
185 adns.lookup(function (answer) |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
186 if answer then |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
187 local srv_hosts = {}; |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
188 self.srv_hosts = srv_hosts; |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
189 for _, record in ipairs(answer) do |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
190 table.insert(srv_hosts, record.srv); |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
191 end |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
192 table.sort(srv_hosts, compare_srv_priorities); |
411 | 193 |
137
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
194 local srv_choice = srv_hosts[1]; |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
195 self.srv_choice = 1; |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
196 if srv_choice then |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
197 self.connect_host, self.connect_port = srv_choice.target, srv_choice.port; |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
198 self:debug("Best record found, will connect to %s:%d", self.connect_host or self.host, self.connect_port or 5222); |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
199 end |
411 | 200 |
137
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
201 self:hook("disconnected", function () |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
202 if self.srv_hosts and self.srv_choice < #self.srv_hosts then |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
203 self.srv_choice = self.srv_choice + 1; |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
204 local srv_choice = srv_hosts[self.srv_choice]; |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
205 self.connect_host, self.connect_port = srv_choice.target, srv_choice.port; |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
206 start_connect(); |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
207 return true; |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
208 end |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
209 end, 1000); |
411 | 210 |
137
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
211 self:hook("connected", function () |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
212 self.srv_hosts = nil; |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
213 end, 1000); |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
214 end |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
215 start_connect(); |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
216 end, "_xmpp-client._tcp."..(self.host)..".", "SRV"); |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
217 else |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
218 start_connect(); |
e4b9d3c5332c
verse.client: Support for SRV record lookups
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
219 end |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 |
11
ce349990bd21
verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
222 function stream:reopen() |
70
36d113fb0f3c
verse.client: Add stream:reset(), keep self.data static between resets
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
223 self:reset(); |
49
9c10ff584e87
verse.client: Add missing version to stream header (thanks Bill Clark)
Matthew Wild <mwild1@gmail.com>
parents:
48
diff
changeset
|
224 self:send(st.stanza("stream:stream", { to = self.host, ["xmlns:stream"]='http://etherx.jabber.org/streams', |
460
a523535d8937
client: Include xml:lang in stream header, if known
Matthew Wild <mwild1@gmail.com>
parents:
459
diff
changeset
|
225 xmlns = "jabber:client", version = "1.0", ["xml:lang"] = self.lang }):top_tag()); |
11
ce349990bd21
verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
226 end |
ce349990bd21
verse.client: Add stream:reopen()
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
227 |
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
|
228 function stream:send_iq(iq, callback) |
457
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
452
diff
changeset
|
229 local id = iq.attr.id or uuid.generate(); |
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
|
230 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
|
231 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
|
232 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
|
233 end |