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