Software /
code /
verse
Annotate
plugins/jingle_ibb.lua @ 465:6707e3a47f71
Add 'shutdown' event for a self-initiated disconnect
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 17 Mar 2023 09:23:15 +0000 |
parent | 457:73d4eb93657b |
child | 469:b49d5df43a4c |
rev | line source |
---|---|
250 | 1 local verse = require "verse"; |
2 local base64 = require "util.encodings".base64; | |
457
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
428
diff
changeset
|
3 local new_id = require "util.id".short; |
250 | 4 |
219
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local xmlns_jingle_ibb = "urn:xmpp:jingle:transports:ibb:1"; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local xmlns_ibb = "http://jabber.org/protocol/ibb"; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 assert(base64.encode("This is a test.") == "VGhpcyBpcyBhIHRlc3Qu", "Base64 encoding failed"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 assert(base64.decode("VGhpcyBpcyBhIHRlc3Qu") == "This is a test.", "Base64 decoding failed"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local t_concat = table.concat |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local ibb_conn = {}; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local ibb_conn_mt = { __index = ibb_conn }; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
284
c78d1780f1d2
plugins.jingle_ibb: new_ibb() doesn't need to be global
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
14 local function new_ibb(stream) |
219
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local conn = setmetatable({ stream = stream }, ibb_conn_mt) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 conn = verse.eventable(conn); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 return conn; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 function ibb_conn:initiate(peer, sid, stanza) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 self.block = 2048; -- ignored for now |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 self.stanza = stanza or 'iq'; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 self.peer = peer; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 self.sid = sid or tostring(self):match("%x+$"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 self.iseq = 0; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 self.oseq = 0; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 local feeder = function(stanza) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 return self:feed(stanza) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 self.feeder = feeder; |
428
bde804b01f28
Fix typos (thanks Link Mauve and codespell)
Kim Alvefur <zash@zash.se>
parents:
284
diff
changeset
|
31 print("Hooking incoming IQs"); |
219
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 local stream = self.stream; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 stream:hook("iq/".. xmlns_ibb, feeder) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 if stanza == "message" then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 stream:hook("message", feeder) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 function ibb_conn:open(callback) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 self.stream:send_iq(verse.iq{ to = self.peer, type = "set" } |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 :tag("open", { |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 xmlns = xmlns_ibb, |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 ["block-size"] = self.block, |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 sid = self.sid, |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 stanza = self.stanza |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 }) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 , function(reply) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 if callback then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 if reply.attr.type ~= "error" then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 callback(true) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 else |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 callback(false, reply:get_error()) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 end); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 function ibb_conn:send(data) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 local stanza = self.stanza; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 local st; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 if stanza == "iq" then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 st = verse.iq{ type = "set", to = self.peer } |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 elseif stanza == "message" then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 st = verse.message{ to = self.peer } |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 local seq = self.oseq; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 self.oseq = seq + 1; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 st:tag("data", { xmlns = xmlns_ibb, sid = self.sid, seq = seq }) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 :text(base64.encode(data)); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 if stanza == "iq" then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 self.stream:send_iq(st, function(reply) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 self:event(reply.attr.type == "result" and "drained" or "error"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 end) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 else |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 stream:send(st) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 self:event("drained"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 function ibb_conn:feed(stanza) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 if stanza.attr.from ~= self.peer then return end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 local child = stanza[1]; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 if child.attr.sid ~= self.sid then return end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 local ok; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 if child.name == "open" then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 self:event("connected"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 self.stream:send(verse.reply(stanza)) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 return true |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 elseif child.name == "data" then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 local bdata = stanza:get_child_text("data", xmlns_ibb); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 local seq = tonumber(child.attr.seq); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 local expected_seq = self.iseq; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 if bdata and seq then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 if seq ~= expected_seq then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 self.stream:send(verse.error_reply(stanza, "cancel", "not-acceptable", "Wrong sequence. Packet lost?")) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 self:close(); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 self:event("error"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 return true; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 self.iseq = seq + 1; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 local data = base64.decode(bdata); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 if self.stanza == "iq" then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 self.stream:send(verse.reply(stanza)) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 self:event("incoming-raw", data); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 return true; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 elseif child.name == "close" then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 self.stream:send(verse.reply(stanza)) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 self:close(); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 return true |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 --[[ FIXME some day |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 function ibb_conn:receive(patt) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 -- is this even used? |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 print("ibb_conn:receive("..tostring(patt)..")"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 assert(patt == "*a" or tonumber(patt)); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 local data = t_concat(self.ibuffer):sub(self.pos, tonumber(patt) or nil); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 self.pos = self.pos + #data; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 return data |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 function ibb_conn:dirty() |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 print("ibb_conn:dirty()"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 return false -- ???? |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 function ibb_conn:getfd() |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 return 0 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 function ibb_conn:settimeout(n) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 -- ignore? |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 -]] |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 function ibb_conn:close() |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 self.stream:unhook("iq/".. xmlns_ibb, self.feeder) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 self:event("disconnected"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 function verse.plugins.jingle_ibb(stream) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 stream:hook("ready", function () |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 stream:add_disco_feature(xmlns_jingle_ibb); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 end, 10); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 local ibb = {}; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 function ibb:_setup() |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 local conn = new_ibb(self.stream); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 conn.sid = self.sid or conn.sid; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 conn.stanza = self.stanza or conn.stanza; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 conn.block = self.block or conn.block; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 conn:initiate(self.peer, self.sid, self.stanza); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 self.conn = conn; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 function ibb:generate_initiate() |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 print("ibb:generate_initiate() as ".. self.role); |
457
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
428
diff
changeset
|
162 local sid = new_id(); |
219
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 self.sid = sid; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 self.stanza = 'iq'; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 self.block = 2048; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 local transport = verse.stanza("transport", { xmlns = xmlns_jingle_ibb, |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 sid = self.sid, stanza = self.stanza, ["block-size"] = self.block }); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 return transport; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 function ibb:generate_accept(initiate_transport) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 print("ibb:generate_accept() as ".. self.role); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 local attr = initiate_transport.attr; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 self.sid = attr.sid or self.sid; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 self.stanza = attr.stanza or self.stanza; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 self.block = attr["block-size"] or self.block; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 self:_setup(); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 return initiate_transport; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 function ibb:connect(callback) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 if not self.conn then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 self:_setup(); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 local conn = self.conn; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 print("ibb:connect() as ".. self.role); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 if self.role == "initiator" then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 conn:open(function(ok, ...) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 assert(ok, table.concat({...}, ", ")); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 callback(conn); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 end); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 else |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 callback(conn); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 function ibb:info_received(jingle_tag) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 print("ibb:info_received()"); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 -- TODO, what exactly? |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 function ibb:disconnect() |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 if self.conn then |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 self.conn:close() |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 function ibb:handle_accepted(jingle_tag) end |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 local ibb_mt = { __index = ibb }; |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 stream:hook("jingle/transport/"..xmlns_jingle_ibb, function (jingle) |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 return setmetatable({ |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 role = jingle.role, |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 peer = jingle.peer, |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 stream = jingle.stream, |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 jingle = jingle, |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 }, ibb_mt); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 end); |
ce8ed17710cb
plugins.jingle_ibb: In-Band Bytestreams, initial commit.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 end |