Annotate

plugins/jingle.lua @ 391:b496f0262a3f

libs.encodings: mime.unb64 is buggy with \0 should have been fixed
author Kim Alvefur <zash@zash.se>
date Tue, 25 Aug 2015 16:27:38 +0200
parent 380:0891b4e27766
child 392:cdea6a28369e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
250
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 220
diff changeset
1 local verse = require "verse";
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local sha1 = require "util.sha1".sha1;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local timer = require "util.timer";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local uuid_generate = require "util.uuid".generate;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local xmlns_jingle = "urn:xmpp:jingle:1";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local xmlns_jingle_errors = "urn:xmpp:jingle:errors:1";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local jingle_mt = {};
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 jingle_mt.__index = jingle_mt;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local registered_transports = {};
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local registered_content_types = {};
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 function verse.plugins.jingle(stream)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 stream:hook("ready", function ()
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 stream:add_disco_feature(xmlns_jingle);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end, 10);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
19
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 function stream:jingle(to)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 return verse.eventable(setmetatable(base or {
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 role = "initiator";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 peer = to;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 sid = uuid_generate();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 stream = stream;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 }, jingle_mt));
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
28
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 function stream:register_jingle_transport(transport)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 -- transport is a function that receives a
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 -- <transport> element, and returns a connection
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 -- We wait for 'connected' on that connection,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 -- and use :send() and 'incoming-raw'.
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
35
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 function stream:register_jingle_content_type(content)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 -- Call content() for every 'incoming-raw'?
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 -- I think content() returns the object we return
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 -- on jingle:accept()
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
41
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local function handle_incoming_jingle(stanza)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local jingle_tag = stanza:get_child("jingle", xmlns_jingle);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local sid = jingle_tag.attr.sid;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local action = jingle_tag.attr.action;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local result = stream:event("jingle/"..sid, stanza);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if result == true then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 -- Ack
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 stream:send(verse.reply(stanza));
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 return true;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 -- No existing Jingle object handled this action, our turn...
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if action ~= "session-initiate" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 -- Trying to send a command to a session we don't know
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 140
diff changeset
55 local reply = verse.error_reply(stanza, "cancel", "item-not-found")
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 :tag("unknown-session", { xmlns = xmlns_jingle_errors }):up();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 stream:send(reply);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 return;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
60
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 -- Ok, session-initiate, new session
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
62
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 -- Create new Jingle object
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local sid = jingle_tag.attr.sid;
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
65
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local jingle = verse.eventable{
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 role = "receiver";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 peer = stanza.attr.from;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 sid = sid;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 stream = stream;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 };
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
72
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 setmetatable(jingle, jingle_mt);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
74
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local content_tag;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local content, transport;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 for tag in jingle_tag:childtags() do
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 if tag.name == "content" and tag.attr.xmlns == xmlns_jingle then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local description_tag = tag:child_with_name("description");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 local description_xmlns = description_tag.attr.xmlns;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 if description_xmlns then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local desc_handler = stream:event("jingle/content/"..description_xmlns, jingle, description_tag);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if desc_handler then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 content = desc_handler;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
87
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 local transport_tag = tag:child_with_name("transport");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local transport_xmlns = transport_tag.attr.xmlns;
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
90
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 transport = stream:event("jingle/transport/"..transport_xmlns, jingle, transport_tag);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 if content and transport then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 content_tag = tag;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 break;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 if not content then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 -- FIXME: Fail, no content
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 140
diff changeset
100 stream:send(verse.error_reply(stanza, "cancel", "feature-not-implemented", "The specified content is not supported"));
220
5e5af66b03fd plugins.jingle: Stop event propagation after returning errors
Kim Alvefur <zash@zash.se>
parents: 211
diff changeset
101 return true;
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
103
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 if not transport then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 -- FIXME: Refuse session, no transport
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 140
diff changeset
106 stream:send(verse.error_reply(stanza, "cancel", "feature-not-implemented", "The specified transport is not supported"));
220
5e5af66b03fd plugins.jingle: Stop event propagation after returning errors
Kim Alvefur <zash@zash.se>
parents: 211
diff changeset
107 return true;
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
109
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 140
diff changeset
110 stream:send(verse.reply(stanza));
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
111
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 jingle.content_tag = content_tag;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 jingle.creator, jingle.name = content_tag.attr.creator, content_tag.attr.name;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 jingle.content, jingle.transport = content, transport;
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
115
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 function jingle:decline()
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 -- FIXME: Decline session
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
119
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 stream:hook("jingle/"..sid, function (stanza)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 if stanza.attr.from ~= jingle.peer then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 return false;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 local jingle_tag = stanza:get_child("jingle", xmlns_jingle);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 return jingle:handle_command(jingle_tag);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 end);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
127
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 stream:event("jingle", jingle);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 return true;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
131
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 function jingle_mt:handle_command(jingle_tag)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 local action = jingle_tag.attr.action;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 stream:debug("Handling Jingle command: %s", action);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 if action == "session-terminate" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 self:destroy();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 elseif action == "session-accept" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 -- Yay!
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 self:handle_accepted(jingle_tag);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 elseif action == "transport-info" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 stream:debug("Handling transport-info");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 self.transport:info_received(jingle_tag);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 elseif action == "transport-replace" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 -- FIXME: Used for IBB fallback
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 stream:error("Peer wanted to swap transport, not implemented");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 else
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 -- FIXME: Reply unhandled command
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 stream:warn("Unhandled Jingle command: %s", action);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 return nil;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 return true;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 function jingle_mt:send_command(command, element, callback)
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 140
diff changeset
155 local stanza = verse.iq({ to = self.peer, type = "set" })
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 :tag("jingle", {
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 xmlns = xmlns_jingle,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 sid = self.sid,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 action = command,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 initiator = self.role == "initiator" and self.stream.jid or nil,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 responder = self.role == "responder" and self.jid or nil,
140
97bf22d6ff96 plugins.jingle, plugins.jingle_s5b: API changes to allow not sending <reason> inside <content> when transfer succeeds
Matthew Wild <mwild1@gmail.com>
parents: 118
diff changeset
162 }):add_child(element);
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 if not callback then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 self.stream:send(stanza);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 else
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 self.stream:send_iq(stanza, callback);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
169
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 function jingle_mt:accept(options)
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 140
diff changeset
171 local accept_stanza = verse.iq({ to = self.peer, type = "set" })
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 :tag("jingle", {
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 xmlns = xmlns_jingle,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 sid = self.sid,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 action = "session-accept",
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 responder = stream.jid,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 })
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 :tag("content", { creator = self.creator, name = self.name });
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
179
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 local content_accept_tag = self.content:generate_accept(self.content_tag:child_with_name("description"), options);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 accept_stanza:add_child(content_accept_tag);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
182
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 local transport_accept_tag = self.transport:generate_accept(self.content_tag:child_with_name("transport"), options);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 accept_stanza:add_child(transport_accept_tag);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
185
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 local jingle = self;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 stream:send_iq(accept_stanza, function (result)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 if result.attr.type == "error" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 local type, condition, text = result:get_error();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 stream:error("session-accept rejected: %s", condition); -- FIXME: Notify
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 return false;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 jingle.transport:connect(function (conn)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 stream:warn("CONNECTED (receiver)!!!");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 jingle.state = "active";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 jingle:event("connected", conn);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 end);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 end);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
200
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 stream:hook("iq/"..xmlns_jingle, handle_incoming_jingle);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 return true;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 function jingle_mt:offer(name, content)
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 140
diff changeset
207 local session_initiate = verse.iq({ to = self.peer, type = "set" })
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 :tag("jingle", { xmlns = xmlns_jingle, action = "session-initiate",
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 initiator = self.stream.jid, sid = self.sid });
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
210
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 -- Content tag
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 session_initiate:tag("content", { creator = self.role, name = name });
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
213
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 -- Need description element from someone who can turn 'content' into XML
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 local description = self.stream:event("jingle/describe/"..name, content);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
216
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 if not description then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 return false, "Unknown content type";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
220
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 session_initiate:add_child(description);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
222
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 -- FIXME: Sort transports by 1) recipient caps 2) priority (SOCKS vs IBB, etc.)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 -- Fixed to s5b in the meantime
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 local transport = self.stream:event("jingle/transport/".."urn:xmpp:jingle:transports:s5b:1", self);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 self.transport = transport;
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
227
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 session_initiate:add_child(transport:generate_initiate());
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
229
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 self.stream:debug("Hooking %s", "jingle/"..self.sid);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 self.stream:hook("jingle/"..self.sid, function (stanza)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 if stanza.attr.from ~= self.peer then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 return false;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 local jingle_tag = stanza:get_child("jingle", xmlns_jingle);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 return self:handle_command(jingle_tag)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 end);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
238
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 self.stream:send_iq(session_initiate, function (result)
341
a95890d86fe4 plugins.adhoc, plugins.jingle: Fix checking of type attribute (thanks Mark)
Matthew Wild <mwild1@gmail.com>
parents: 250
diff changeset
240 if result.attr.type == "error" then
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 self.state = "terminated";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 local type, condition, text = result:get_error();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 return self:event("error", { type = type, condition = condition, text = text });
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 end);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 self.state = "pending";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 function jingle_mt:terminate(reason)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 local reason_tag = verse.stanza("reason"):tag(reason or "success");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 self:send_command("session-terminate", reason_tag, function (result)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 self.state = "terminated";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 self.transport:disconnect();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 self:destroy();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 end);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 function jingle_mt:destroy()
118
d076948cd0e1 plugins.jingle: Add 'terminated' event
Matthew Wild <mwild1@gmail.com>
parents: 100
diff changeset
259 self:event("terminated");
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 self.stream:unhook("jingle/"..self.sid, self.handle_command);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 function jingle_mt:handle_accepted(jingle_tag)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 local transport_tag = jingle_tag:child_with_name("transport");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 self.transport:handle_accepted(transport_tag);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 self.transport:connect(function (conn)
211
837cd09fcf01 plugins.jingle: Logging fix
Kim Alvefur <zash@zash.se>
parents: 197
diff changeset
267 self.stream:debug("CONNECTED (initiator)!")
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 -- Connected, send file
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 self.state = "active";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 self:event("connected", conn);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 end);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 function jingle_mt:set_source(source, auto_close)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 local function pump()
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 local chunk, err = source();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 if chunk and chunk ~= "" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 self.transport.conn:send(chunk);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 elseif chunk == "" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 return pump(); -- We need some data!
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 elseif chunk == nil then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 if auto_close then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 self:terminate();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 self.transport.conn:unhook("drained", pump);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 source = nil;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 self.transport.conn:hook("drained", pump);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 pump();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 function jingle_mt:set_sink(sink)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 self.transport.conn:hook("incoming-raw", sink);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 self.transport.conn:hook("disconnected", function (event)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 self.stream:debug("Closing sink...");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 local reason = event.reason;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 if reason == "closed" then reason = nil; end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 sink(nil, reason);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 end);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 end