Annotate

plugins/jingle.lua @ 482:4c4f3dd62c54

squishy: Comment legacy protocol plugins by default
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Mar 2023 12:31:28 +0000
parent 457:73d4eb93657b
child 490:6b2f31da9610
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 timer = require "util.timer";
457
73d4eb93657b Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents: 392
diff changeset
3 local new_id = require "util.id".short;
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local xmlns_jingle = "urn:xmpp:jingle:1";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 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
7
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local jingle_mt = {};
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 jingle_mt.__index = jingle_mt;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local registered_transports = {};
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local registered_content_types = {};
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 function verse.plugins.jingle(stream)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 stream:hook("ready", function ()
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 stream:add_disco_feature(xmlns_jingle);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end, 10);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
18
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 function stream:jingle(to)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return verse.eventable(setmetatable(base or {
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 role = "initiator";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 peer = to;
457
73d4eb93657b Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents: 392
diff changeset
23 sid = new_id();
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 stream = stream;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 }, jingle_mt));
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
27
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 function stream:register_jingle_transport(transport)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 -- transport is a function that receives a
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 -- <transport> element, and returns a connection
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 -- We wait for 'connected' on that connection,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 -- and use :send() and 'incoming-raw'.
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
34
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 function stream:register_jingle_content_type(content)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 -- Call content() for every 'incoming-raw'?
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 -- I think content() returns the object we return
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 -- on jingle:accept()
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
40
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local function handle_incoming_jingle(stanza)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 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
43 local sid = jingle_tag.attr.sid;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local action = jingle_tag.attr.action;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local result = stream:event("jingle/"..sid, stanza);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if result == true then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 -- Ack
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 stream:send(verse.reply(stanza));
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 return true;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 -- 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
52 if action ~= "session-initiate" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 -- 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
54 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
55 :tag("unknown-session", { xmlns = xmlns_jingle_errors }):up();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 stream:send(reply);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 return;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
59
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 -- Ok, session-initiate, new session
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
61
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 -- Create new Jingle object
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local sid = jingle_tag.attr.sid;
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
64
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local jingle = verse.eventable{
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 role = "receiver";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 peer = stanza.attr.from;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 sid = sid;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 stream = stream;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 };
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
71
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 setmetatable(jingle, jingle_mt);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
73
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local content_tag;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local content, transport;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 for tag in jingle_tag:childtags() do
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 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
78 local description_tag = tag:child_with_name("description");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local description_xmlns = description_tag.attr.xmlns;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if description_xmlns then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 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
82 if desc_handler then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 content = desc_handler;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
86
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local transport_tag = tag:child_with_name("transport");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 local transport_xmlns = transport_tag.attr.xmlns;
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
89
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 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
91 if content and transport then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 content_tag = tag;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 break;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
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 if not content then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 -- 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
99 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
100 return true;
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
102
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 if not transport then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 -- 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
105 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
106 return true;
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
108
197
7e98cf2c1d8d plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents: 140
diff changeset
109 stream:send(verse.reply(stanza));
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
110
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 jingle.content_tag = content_tag;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 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
113 jingle.content, jingle.transport = content, transport;
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
114
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 function jingle:decline()
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 -- FIXME: Decline session
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
118
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 stream:hook("jingle/"..sid, function (stanza)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 if stanza.attr.from ~= jingle.peer then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 return false;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 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
124 return jingle:handle_command(jingle_tag);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
126
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 stream:event("jingle", jingle);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 return true;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
130
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 function jingle_mt:handle_command(jingle_tag)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local action = jingle_tag.attr.action;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 stream:debug("Handling Jingle command: %s", action);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 if action == "session-terminate" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 self:destroy();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 elseif action == "session-accept" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 -- Yay!
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 self:handle_accepted(jingle_tag);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 elseif action == "transport-info" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 stream:debug("Handling transport-info");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 self.transport:info_received(jingle_tag);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 elseif action == "transport-replace" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 -- FIXME: Used for IBB fallback
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 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
145 else
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 -- FIXME: Reply unhandled command
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 stream:warn("Unhandled Jingle command: %s", action);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 return nil;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 return true;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 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
154 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
155 :tag("jingle", {
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 xmlns = xmlns_jingle,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 sid = self.sid,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 action = command,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 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
160 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
161 }):add_child(element);
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 if not callback then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 self.stream:send(stanza);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 else
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 self.stream:send_iq(stanza, callback);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
168
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 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
170 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
171 :tag("jingle", {
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 xmlns = xmlns_jingle,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 sid = self.sid,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 action = "session-accept",
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 responder = stream.jid,
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 })
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 :tag("content", { creator = self.creator, name = self.name });
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
178
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 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
180 accept_stanza:add_child(content_accept_tag);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
181
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 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
183 accept_stanza:add_child(transport_accept_tag);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
184
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 local jingle = self;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 stream:send_iq(accept_stanza, function (result)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 if result.attr.type == "error" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 local type, condition, text = result:get_error();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 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
190 return false;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 jingle.transport:connect(function (conn)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 stream:warn("CONNECTED (receiver)!!!");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 jingle.state = "active";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 jingle:event("connected", conn);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 end);
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
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
199
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 stream:hook("iq/"..xmlns_jingle, handle_incoming_jingle);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 return true;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 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
206 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
207 :tag("jingle", { xmlns = xmlns_jingle, action = "session-initiate",
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 initiator = self.stream.jid, sid = self.sid });
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
209
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 -- Content tag
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 session_initiate:tag("content", { creator = self.role, name = name });
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
212
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 -- 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
214 local description = self.stream:event("jingle/describe/"..name, content);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
215
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 if not description then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 return false, "Unknown content type";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
219
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 session_initiate:add_child(description);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
221
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 -- 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
223 -- Fixed to s5b in the meantime
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 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
225 self.transport = transport;
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
226
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 session_initiate:add_child(transport:generate_initiate());
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
228
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 self.stream:debug("Hooking %s", "jingle/"..self.sid);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 self.stream:hook("jingle/"..self.sid, function (stanza)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 if stanza.attr.from ~= self.peer then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 return false;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 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
235 return self:handle_command(jingle_tag)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 end);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
237
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 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
239 if result.attr.type == "error" then
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 self.state = "terminated";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 local type, condition, text = result:get_error();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 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
243 end
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 self.state = "pending";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 function jingle_mt:terminate(reason)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 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
250 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
251 self.state = "terminated";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 self.transport:disconnect();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 self:destroy();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 end);
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
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 function jingle_mt:destroy()
118
d076948cd0e1 plugins.jingle: Add 'terminated' event
Matthew Wild <mwild1@gmail.com>
parents: 100
diff changeset
258 self:event("terminated");
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 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
260 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 function jingle_mt:handle_accepted(jingle_tag)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 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
264 self.transport:handle_accepted(transport_tag);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 self.transport:connect(function (conn)
211
837cd09fcf01 plugins.jingle: Logging fix
Kim Alvefur <zash@zash.se>
parents: 197
diff changeset
266 self.stream:debug("CONNECTED (initiator)!")
100
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 -- Connected, send file
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 self.state = "active";
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 self:event("connected", conn);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 end);
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
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 function jingle_mt:set_source(source, auto_close)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 local function pump()
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 local chunk, err = source();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 if chunk and chunk ~= "" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 self.transport.conn:send(chunk);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 elseif chunk == "" then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 return pump(); -- We need some data!
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 elseif chunk == nil then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 if auto_close then
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 self:terminate();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 self.transport.conn:unhook("drained", pump);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 source = nil;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 end
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 self.transport.conn:hook("drained", pump);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 pump();
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 function jingle_mt:set_sink(sink)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 self.transport.conn:hook("incoming-raw", sink);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 self.transport.conn:hook("disconnected", function (event)
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 self.stream:debug("Closing sink...");
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 local reason = event.reason;
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 if reason == "closed" then reason = nil; end
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 sink(nil, reason);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 end);
e45883a3f39a verse.plugins.jingle: XEP-0166 Jingle plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 end