68
|
1 |
|
|
2 |
-- IN-BAND BYTESTREAMS (XEP-0047) |
|
3 |
|
|
4 |
-- TODO bidirectionality |
|
5 |
-- thus, on stream accept we can add our sid to incoming files structure, |
|
6 |
-- as if we received and accepted incoming request. |
|
7 |
-- message stanzas |
|
8 |
|
|
9 |
-- library |
|
10 |
|
|
11 |
local lm = require 'lm' |
|
12 |
local iq = require 'lm.iq' |
|
13 |
local base64 = require 'base64' |
|
14 |
|
|
15 |
-- |
|
16 |
|
|
17 |
local F = { } |
|
18 |
local M = { } |
|
19 |
M.__index = M |
|
20 |
local O = { |
|
21 |
handler = |
|
22 |
function ( accept, reject ) |
|
23 |
reject () |
|
24 |
end, |
|
25 |
} |
|
26 |
|
|
27 |
function F.new ( conn, to, bs, sid ) |
|
28 |
local obj = { |
|
29 |
conn = conn, |
|
30 |
to = to, |
|
31 |
sbs = bs, |
|
32 |
bs = math.floor ( bs * 3 / 4 ), |
|
33 |
sid = sid, |
|
34 |
seq = 0, |
|
35 |
} |
|
36 |
setmetatable ( obj, M ) |
|
37 |
return obj |
|
38 |
end |
|
39 |
|
|
40 |
function M.open ( obj, success, fail ) |
|
41 |
iq.send ( obj.conn, obj.to, 'set', |
|
42 |
{ |
|
43 |
open = { sid = obj.sid, ['block-size'] = obj.sbs, xmlns = 'http://jabber.org/protocol/ibb' } |
|
44 |
}, success, fail ) |
|
45 |
end |
|
46 |
|
|
47 |
function M.send ( obj, data, success, fail ) |
|
48 |
if data and data ~= '' then |
|
49 |
local start = 0 |
|
50 |
while start < data:len () do |
|
51 |
local chunk = base64.encode ( data:sub ( start, obj.bs ) ) |
|
52 |
local cseq = obj.seq -- local instance |
|
53 |
iq.send ( obj.conn, obj.to, 'set', |
|
54 |
{ |
|
55 |
data = { sid = obj.sid, xmlns = 'http://jabber.org/protocol/ibb', seq = cseq, |
|
56 |
chunk, |
|
57 |
}, |
|
58 |
}, |
|
59 |
function () |
|
60 |
success ( cseq ) |
|
61 |
end, |
|
62 |
function ( mesg ) |
|
63 |
noerr = false |
|
64 |
fail ( mesg ) |
|
65 |
end ) |
|
66 |
start = start + obj.bs |
|
67 |
obj.seq = obj.seq + 1 |
|
68 |
end |
|
69 |
end |
|
70 |
end |
|
71 |
|
|
72 |
function M.close ( obj, success, fail ) |
|
73 |
iq.send ( obj.conn, obj.to, 'set', |
|
74 |
{ |
|
75 |
close = { sid = obj.sid, xmlns = 'http://jabber.org/protocol/ibb' }, |
|
76 |
}, success, fail ) |
|
77 |
end |
|
78 |
|
|
79 |
function F.handler ( handler ) |
|
80 |
O.handler = handler |
|
81 |
end |
|
82 |
|
|
83 |
local ibb_files = {} |
|
84 |
|
|
85 |
function F.iq_handler ( conn, mess ) |
|
86 |
local mtype, smtype = mess:type () |
|
87 |
if smtype ~= 'set' then |
|
88 |
return false |
|
89 |
end |
|
90 |
|
|
91 |
local child = mess:child () |
|
92 |
if not child or child:attribute ( 'xmlns' ) ~= 'http://jabber.org/protocol/ibb' then |
|
93 |
return false |
|
94 |
end |
|
95 |
|
|
96 |
local id = mess:attribute ( 'id' ) |
|
97 |
local from = mess:attribute ( 'from' ) |
|
98 |
local action = child:name () |
|
99 |
local sid = child:attribute ( 'sid' ) |
|
100 |
|
|
101 |
if action == 'open' then |
|
102 |
if not ibb_files[sid] then |
|
103 |
O.handler ( from, |
|
104 |
function ( success, fail ) |
|
105 |
ibb_files[sid] = { from = from, success = success, fail = fail } |
|
106 |
conn:send ( lm.message.create { to = from, mtype = 'iq-result', id = id } ) |
|
107 |
end, |
|
108 |
function () |
|
109 |
conn:send ( |
|
110 |
lm.message.create { to = from, mtype = 'iq-error', id = id, |
|
111 |
error = { code = '405', type = 'cancel', |
|
112 |
['not-allowed'] = { xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas' }, |
|
113 |
}, |
|
114 |
} ) |
|
115 |
end ) |
|
116 |
else |
|
117 |
conn:send ( |
|
118 |
lm.message.create { to = from, mtype = 'iq-error', id = id, |
|
119 |
error = { code = '409', type = 'cancel', |
|
120 |
conflict = { xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas' }, |
|
121 |
}, |
|
122 |
} ) |
|
123 |
end |
|
124 |
elseif action == 'data' then |
|
125 |
local seq = child:attribute ( 'seq' ) |
|
126 |
if ibb_files[sid] and from == ibb_files[sid].from and not ibb_files[sid][tonumber(seq)+1] then |
|
127 |
local data = child:value () |
|
128 |
conn:send ( lm.message.create { to = from, mtype = 'iq-result', id = id } ) |
|
129 |
ibb_files[sid][tonumber(seq)+1] = data |
|
130 |
-- XXX ibb_files[sid].success ( seq ) |
|
131 |
else |
|
132 |
if ibb_files[sid] then |
|
133 |
ibb_files[sid].fail ( 'conflict' ) |
|
134 |
ibb_files[sid] = nil -- invalidate session |
|
135 |
conn:send ( |
|
136 |
lm.message.create { to = from, mtype = 'iq-error', id = id, |
|
137 |
error = { code = '409', type = 'cancel', |
|
138 |
conflict = { xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas' }, |
|
139 |
}, |
|
140 |
} ) |
|
141 |
else |
|
142 |
conn:send ( |
|
143 |
lm.message.create { to = from, mtype = 'iq-error', id = id, |
|
144 |
error = { code = '404', type = 'cancel', -- XXX: check |
|
145 |
['item-not-found'] = { xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas' }, |
|
146 |
}, |
|
147 |
} ) |
|
148 |
end |
|
149 |
end |
|
150 |
elseif action == 'close' then |
|
151 |
if ibb_files[sid] and from == ibb_files[sid].from then |
|
152 |
local data = '' |
|
153 |
for seq, chunk in ipairs ( ibb_files[sid] ) do |
|
154 |
data = data .. chunk |
|
155 |
end |
|
156 |
local decoded = base64.decode ( data ) |
|
157 |
ibb_files[sid].success ( decoded ) |
|
158 |
conn:send ( lm.message.create { to = from, mtype = 'iq-result', id = id } ) |
|
159 |
ibb_files[sid] = nil |
|
160 |
else |
|
161 |
if ibb_files[sid] then |
|
162 |
ibb_files[sid].fail ( 'conflict' ) |
|
163 |
ibb_files[sid] = nil -- invalidate session |
|
164 |
conn:send ( |
|
165 |
lm.message.create { to = from, mtype = 'iq-error', id = id, |
|
166 |
error = { code = '409', type = 'cancel', |
|
167 |
conflict = { xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas' }, |
|
168 |
}, |
|
169 |
} ) |
|
170 |
else |
|
171 |
conn:send ( |
|
172 |
lm.message.create { to = from, mtype = 'iq-error', id = id, |
|
173 |
error = { code = '404', type = 'cancel', -- XXX: check |
|
174 |
['item-not-found'] = { xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas' }, |
|
175 |
}, |
|
176 |
} ) |
|
177 |
end |
|
178 |
end |
|
179 |
else |
|
180 |
return false |
|
181 |
end |
|
182 |
|
|
183 |
return true |
|
184 |
end |
|
185 |
|
|
186 |
return F |
|
187 |
|
|
188 |
-- vim: se ts=4: -- |