Annotate

plugins/jingle_ibb.lua @ 235:3c93d6119477

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