| 1 |
using Gee; |
| 2 |
using Xmpp; |
| 3 |
|
| 4 |
namespace Xmpp.Xep.StatelessFileSharing { |
| 5 |
|
| 6 |
public const string NS_URI = "urn:xmpp:sfs:0"; |
| 7 |
|
| 8 |
public static Gee.List<FileShare> get_file_shares(MessageStanza message) { |
| 9 |
var ret = new ArrayList<FileShare>(); |
| 10 |
foreach (StanzaNode file_sharing_node in message.stanza.get_subnodes("file-sharing", NS_URI)) { |
| 11 |
var metadata = Xep.FileMetadataElement.get_file_metadata(file_sharing_node); |
| 12 |
if (metadata == null) continue; |
| 13 |
|
| 14 |
var sources_node = file_sharing_node.get_subnode("sources", NS_URI); |
| 15 |
|
| 16 |
ret.add(new FileShare() { |
| 17 |
id = file_sharing_node.get_attribute("id", NS_URI), |
| 18 |
metadata = Xep.FileMetadataElement.get_file_metadata(file_sharing_node), |
| 19 |
sources = sources_node != null ? get_sources(sources_node) : null |
| 20 |
}); |
| 21 |
} |
| 22 |
|
| 23 |
if (ret.size == 0) return null; |
| 24 |
|
| 25 |
return ret; |
| 26 |
} |
| 27 |
|
| 28 |
public static Gee.List<SourceAttachment>? get_source_attachments(MessageStanza message) { |
| 29 |
Gee.List<StanzaNode> sources_nodes = message.stanza.get_subnodes("sources", NS_URI); |
| 30 |
if (sources_nodes.is_empty) return null; |
| 31 |
|
| 32 |
string? attach_to_id = MessageAttaching.get_attach_to(message.stanza); |
| 33 |
if (attach_to_id == null) return null; |
| 34 |
|
| 35 |
var ret = new ArrayList<SourceAttachment>(); |
| 36 |
|
| 37 |
foreach (StanzaNode sources_node in sources_nodes) { |
| 38 |
ret.add(new SourceAttachment() { |
| 39 |
to_message_id = attach_to_id, |
| 40 |
to_file_transfer_id = sources_node.get_attribute("id", NS_URI), |
| 41 |
sources = get_sources(sources_node) |
| 42 |
}); |
| 43 |
} |
| 44 |
return ret; |
| 45 |
} |
| 46 |
|
| 47 |
public static bool is_sfs_fallback_message(MessageStanza message) { |
| 48 |
Gee.List<FallbackIndication.Fallback> fallbacks = Xep.FallbackIndication.get_fallbacks(message); |
| 49 |
foreach (var fallback in fallbacks) { |
| 50 |
if (fallback.ns_uri == StatelessFileSharing.NS_URI && fallback.locations.any_match((it) => it.is_whole)) { |
| 51 |
return true; |
| 52 |
} |
| 53 |
} |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
// Currently only returns a single http source |
| 58 |
private static Gee.List<Source>? get_sources(StanzaNode sources_node) { |
| 59 |
string? url = HttpSchemeForUrlData.get_url(sources_node); |
| 60 |
if (url == null) return null; |
| 61 |
|
| 62 |
var http_source = new HttpSource() { url=url }; |
| 63 |
var sources = new Gee.ArrayList<Source>(); |
| 64 |
sources.add(http_source); |
| 65 |
|
| 66 |
return sources; |
| 67 |
} |
| 68 |
|
| 69 |
public static void set_sfs_element(MessageStanza message, string file_sharing_id, FileMetadataElement.FileMetadata metadata, Gee.List<Xep.StatelessFileSharing.Source>? sources) { |
| 70 |
var file_sharing_node = new StanzaNode.build("file-sharing", NS_URI).add_self_xmlns() |
| 71 |
.put_attribute("id", file_sharing_id, NS_URI) |
| 72 |
.put_node(metadata.to_stanza_node()); |
| 73 |
if (sources != null && !sources.is_empty) { |
| 74 |
file_sharing_node.put_node(create_sources_node(file_sharing_id, sources)); |
| 75 |
} |
| 76 |
message.stanza.put_node(file_sharing_node); |
| 77 |
} |
| 78 |
|
| 79 |
public static void set_sfs_attachment(MessageStanza message, string attach_to_id, string attach_to_file_id, Gee.List<Xep.StatelessFileSharing.Source> sources) { |
| 80 |
message.stanza.put_node(MessageAttaching.to_stanza_node(attach_to_id)); |
| 81 |
message.stanza.put_node(create_sources_node(attach_to_file_id, sources).add_self_xmlns()); |
| 82 |
} |
| 83 |
|
| 84 |
private static StanzaNode create_sources_node(string file_sharing_id, Gee.List<Xep.StatelessFileSharing.Source> sources) { |
| 85 |
StanzaNode sources_node = new StanzaNode.build("sources", NS_URI) |
| 86 |
.put_attribute("id", file_sharing_id, NS_URI); |
| 87 |
foreach (var source in sources) { |
| 88 |
sources_node.put_node(source.to_stanza_node()); |
| 89 |
} |
| 90 |
return sources_node; |
| 91 |
} |
| 92 |
|
| 93 |
public class FileShare : Object { |
| 94 |
public string? id { get; set; } |
| 95 |
public Xep.FileMetadataElement.FileMetadata metadata { get; set; } |
| 96 |
public Gee.List<Source>? sources { get; set; } |
| 97 |
} |
| 98 |
|
| 99 |
public class SourceAttachment : Object { |
| 100 |
public string to_message_id { get; set; } |
| 101 |
public string? to_file_transfer_id { get; set; } |
| 102 |
public Gee.List<Source>? sources { get; set; } |
| 103 |
} |
| 104 |
|
| 105 |
public interface Source: Object { |
| 106 |
public abstract string type(); |
| 107 |
public abstract StanzaNode to_stanza_node(); |
| 108 |
public abstract bool equals(Source source); |
| 109 |
|
| 110 |
public static bool equals_func(Source s1, Source s2) { |
| 111 |
return s1.equals(s2); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public class HttpSource : Object, Source { |
| 116 |
public string url { get; set; } |
| 117 |
|
| 118 |
public string type() { |
| 119 |
return "http"; |
| 120 |
} |
| 121 |
|
| 122 |
public StanzaNode to_stanza_node() { |
| 123 |
return HttpSchemeForUrlData.to_stanza_node(url); |
| 124 |
} |
| 125 |
|
| 126 |
public bool equals(Source source) { |
| 127 |
HttpSource? http_source = source as HttpSource; |
| 128 |
if (http_source == null) return false; |
| 129 |
return http_source.url == this.url; |
| 130 |
} |
| 131 |
} |
| 132 |
} |