Debian Codegraph @

xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala

raw
1 using Gee;
2 using Xmpp;
3 using Xmpp.Xep;
4
5 namespace Xmpp.Xep.JingleFileTransfer {
6
7 private const string NS_URI = "urn:xmpp:jingle:apps:file-transfer:5";
8
9 public class Module : Jingle.ContentType, XmppStreamModule {
10
11 public signal void file_incoming(XmppStream stream, FileTransfer file_transfer);
12
13 public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0234_jingle_file_transfer");
14 public SessionInfoType session_info_type = new SessionInfoType();
15
16 public override void attach(XmppStream stream) {
17 stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI);
18 stream.get_module(Jingle.Module.IDENTITY).register_content_type(this);
19 stream.get_module(Jingle.Module.IDENTITY).register_session_info_type(session_info_type);
20 }
21 public override void detach(XmppStream stream) {
22 stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
23 }
24
25 public string ns_uri { get { return NS_URI; } }
26 public Jingle.TransportType required_transport_type { get { return Jingle.TransportType.STREAMING; } }
27 public uint8 required_components { get { return 1; } }
28
29 public Jingle.ContentParameters parse_content_parameters(StanzaNode description) throws Jingle.IqError {
30 return Parameters.parse(this, description);
31 }
32
33 public Jingle.ContentParameters create_content_parameters(Object object) throws Jingle.IqError {
34 assert_not_reached();
35 }
36
37 public async bool is_available(XmppStream stream, Jid full_jid) {
38 bool? has_feature = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI);
39 if (has_feature == null || !(!)has_feature) {
40 return false;
41 }
42 return yield stream.get_module(Jingle.Module.IDENTITY).is_available(stream, required_transport_type, required_components, full_jid);
43 }
44
45 public async void offer_file_stream(XmppStream stream, Jid receiver_full_jid, InputStream input_stream, string basename, int64 size, string? precondition_name = null, Object? precondition_options = null) throws Jingle.Error {
46 StanzaNode file_node;
47 StanzaNode description = new StanzaNode.build("description", NS_URI)
48 .add_self_xmlns()
49 .put_node(file_node = new StanzaNode.build("file", NS_URI)
50 .put_node(new StanzaNode.build("name", NS_URI).put_node(new StanzaNode.text(basename))));
51 // TODO(hrxi): Add the mandatory hash field
52
53 if (size > 0) {
54 file_node.put_node(new StanzaNode.build("size", NS_URI).put_node(new StanzaNode.text(size.to_string())));
55 } else {
56 warning("Sending file %s without size, likely going to cause problems down the road...", basename);
57 }
58
59 Parameters parameters = Parameters.parse(this, description);
60
61 Jingle.Module jingle_module = stream.get_module(Jingle.Module.IDENTITY);
62
63 Jingle.Transport? transport = yield jingle_module.select_transport(stream, required_transport_type, required_components, receiver_full_jid, Set.empty());
64 if (transport == null) {
65 throw new Jingle.Error.NO_SHARED_PROTOCOLS("No suitable transports");
66 }
67 Jingle.SecurityPrecondition? precondition = jingle_module.get_security_precondition(precondition_name);
68 if (precondition_name != null && precondition == null) {
69 throw new Jingle.Error.UNSUPPORTED_SECURITY("No suitable security precondiiton found");
70 }
71 Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid;
72 if (my_jid == null) {
73 throw new Jingle.Error.GENERAL("Couldn't determine own JID");
74 }
75 Jingle.TransportParameters transport_params = transport.create_transport_parameters(stream, required_components, my_jid, receiver_full_jid);
76 Jingle.SecurityParameters? security_params = precondition != null ? precondition.create_security_parameters(stream, my_jid, receiver_full_jid, precondition_options) : null;
77
78 Jingle.Content content = new Jingle.Content.initiate_sent("a-file-offer", Jingle.Senders.INITIATOR,
79 this, parameters,
80 transport, transport_params,
81 precondition, security_params,
82 my_jid, receiver_full_jid);
83
84 ArrayList<Jingle.Content> contents = new ArrayList<Jingle.Content>();
85 contents.add(content);
86
87
88 Jingle.Session? session = null;
89 try {
90 session = yield jingle_module.create_session(stream, contents, receiver_full_jid);
91
92 // Wait for the counterpart to accept our offer
93 ulong content_notify_id = 0;
94 content_notify_id = content.notify["state"].connect(() => {
95 if (content.state == Jingle.Content.State.ACCEPTED) {
96 Idle.add(offer_file_stream.callback);
97 content.disconnect(content_notify_id);
98 }
99 });
100 yield;
101
102 // Send the file data
103 Jingle.StreamingConnection connection = content.component_connections[1] as Jingle.StreamingConnection;
104 if (connection == null || connection.stream == null) {
105 warning("Connection or stream not null");
106 return;
107 }
108 IOStream io_stream = yield connection.stream.wait_async();
109 yield io_stream.input_stream.close_async();
110 yield io_stream.output_stream.splice_async(input_stream, OutputStreamSpliceFlags.CLOSE_SOURCE|OutputStreamSpliceFlags.CLOSE_TARGET);
111 yield connection.terminate(true);
112 } catch (Error e) {
113 if (session != null) {
114 session.terminate(Jingle.ReasonElement.FAILED_TRANSPORT, e.message, e.message);
115 }
116 throw new Jingle.Error.GENERAL("Couldn't send file via Jingle: %s", e.message);
117 }
118 }
119
120 public override string get_ns() { return NS_URI; }
121 public override string get_id() { return IDENTITY.id; }
122 }
123
124 public class SessionInfoType : Jingle.SessionInfoNs, Object {
125
126 public string ns_uri { get { return NS_URI; } }
127
128 public void handle_content_session_info(XmppStream stream, Jingle.Session session, StanzaNode info, Iq.Stanza iq) throws Jingle.IqError {
129 switch (info.name) {
130 case "received":
131 break;
132 case "checksum":
133 // TODO(hrxi): handle hash
134 break;
135 default:
136 throw new Jingle.IqError.UNSUPPORTED_INFO(@"unsupported file transfer info $(info.name)");
137 }
138 }
139
140 }
141
142 public class Parameters : Jingle.ContentParameters, Object {
143
144 Module parent;
145 string? media_type;
146 public string? name { get; private set; }
147 public int64 size { get; private set; }
148 public StanzaNode original_description { get; private set; }
149
150 public Parameters(Module parent, StanzaNode original_description, string? media_type, string? name, int64 size) {
151 this.parent = parent;
152 this.original_description = original_description;
153 this.media_type = media_type;
154 this.name = name;
155 this.size = size;
156 }
157
158 public static Parameters parse(Module parent, StanzaNode description) throws Jingle.IqError {
159 Gee.List<StanzaNode> files = description.get_subnodes("file", NS_URI);
160 if (files.size != 1) {
161 throw new Jingle.IqError.BAD_REQUEST("there needs to be exactly one file node");
162 }
163 StanzaNode file = files[0];
164 StanzaNode? media_type_node = file.get_subnode("media-type", NS_URI);
165 StanzaNode? name_node = file.get_subnode("name", NS_URI);
166 StanzaNode? size_node = file.get_subnode("size", NS_URI);
167 string? media_type = media_type_node != null ? media_type_node.get_string_content() : null;
168 string? name = name_node != null ? name_node.get_string_content() : null;
169 string? size_raw = size_node != null ? size_node.get_string_content() : null;
170 // TODO(hrxi): For some reason, the ?:-expression does not work due to a type error.
171 //int64? size = size_raw != null ? int64.parse(size_raw) : null; // TODO(hrxi): this has no error handling
172 if (size_raw == null) {
173 // Jingle file transfers (XEP-0234) theoretically SHOULD send a
174 // file size, however, we do require it in order to reliably find
175 // the end of the file transfer.
176 throw new Jingle.IqError.BAD_REQUEST("file offer without file size");
177 }
178 int64 size = int64.parse(size_raw);
179 if (size < 0) {
180 throw new Jingle.IqError.BAD_REQUEST("negative file size is invalid");
181 }
182
183 return new Parameters(parent, description, media_type, name, size);
184 }
185
186 public StanzaNode get_description_node() {
187 return original_description;
188 }
189
190 public async void handle_proposed_content(XmppStream stream, Jingle.Session session, Jingle.Content content) {
191 parent.file_incoming(stream, new FileTransfer(session, content, this));
192 }
193
194 public void modify(XmppStream stream, Jingle.Session session, Jingle.Content content, Jingle.Senders senders) { }
195
196 public void accept(XmppStream stream, Jingle.Session session, Jingle.Content content) { }
197
198 public void handle_accept(XmppStream stream, Jingle.Session session, Jingle.Content content, StanzaNode description_node) { }
199
200 public void terminate(bool we_terminated, string? reason_name, string? reason_text) { }
201 }
202
203 // Does nothing except wrapping an input stream to signal EOF after reading
204 // `max_size` bytes.
205 private class FileTransferInputStream : InputStream {
206
207 public signal void closed();
208
209 InputStream inner;
210 int64 remaining_size;
211
212 public FileTransferInputStream(InputStream inner, int64 max_size) {
213 this.inner = inner;
214 this.remaining_size = max_size;
215 }
216
217 private ssize_t update_remaining(ssize_t read) {
218 this.remaining_size -= read;
219 return read;
220 }
221
222 public override ssize_t read(uint8[] buffer_, Cancellable? cancellable = null) throws IOError {
223 unowned uint8[] buffer = buffer_;
224 if (remaining_size <= 0) {
225 return 0;
226 }
227 if (buffer.length > remaining_size) {
228 buffer = buffer[0:remaining_size];
229 }
230 return update_remaining(inner.read(buffer, cancellable));
231 }
232
233 public override async ssize_t read_async(uint8[]? buffer_, int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError {
234 unowned uint8[] buffer = buffer_;
235 if (remaining_size <= 0) {
236 return 0;
237 }
238 if (buffer.length > remaining_size) {
239 buffer = buffer[0:remaining_size];
240 }
241 return update_remaining(yield inner.read_async(buffer, io_priority, cancellable));
242 }
243
244 public override bool close(Cancellable? cancellable = null) throws IOError {
245 closed();
246 return inner.close(cancellable);
247 }
248
249 public override async bool close_async(int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError {
250 closed();
251 return yield inner.close_async(io_priority, cancellable);
252 }
253 }
254
255 public class FileTransfer : Object {
256 Jingle.Session session;
257 Jingle.Content content;
258 Parameters parameters;
259
260 public Jid peer { get { return session.peer_full_jid; } }
261 public string? file_name { get { return parameters.name; } }
262 public int64 size { get { return parameters.size; } }
263 public Jingle.SecurityParameters? security { get { return session.security; } }
264
265 public InputStream? stream { get; private set; }
266
267 public FileTransfer(Jingle.Session session, Jingle.Content content, Parameters parameters) {
268 this.session = session;
269 this.content = content;
270 this.parameters = parameters;
271 }
272
273 public async void accept(XmppStream stream) throws IOError {
274 content.accept();
275
276 Jingle.StreamingConnection connection = content.component_connections.values.to_array()[0] as Jingle.StreamingConnection;
277 try {
278 IOStream io_stream = yield connection.stream.wait_async();
279 FileTransferInputStream ft_stream = new FileTransferInputStream(io_stream.input_stream, size);
280 io_stream.output_stream.close();
281 ft_stream.closed.connect(() => {
282 session.terminate(Jingle.ReasonElement.SUCCESS, null, null);
283 });
284 this.stream = ft_stream;
285 } catch (FutureError.EXCEPTION e) {
286 warning("Error accepting Jingle file-transfer: %s", connection.stream.exception.message);
287 } catch (FutureError e) {
288 warning("FutureError accepting Jingle file-transfer: %s", e.message);
289 }
290 }
291
292 public void reject(XmppStream stream) {
293 content.reject();
294 }
295 }
296
297 }