| 1 |
using Xmpp; |
| 2 |
using Xmpp.Xep; |
| 3 |
using Gee; |
| 4 |
|
| 5 |
namespace Xmpp.Xep.HttpFileUpload { |
| 6 |
|
| 7 |
private const string NS_URI = "urn:xmpp:http:upload"; |
| 8 |
private const string NS_URI_0 = "urn:xmpp:http:upload:0"; |
| 9 |
|
| 10 |
public errordomain HttpFileTransferError { |
| 11 |
SLOT_REQUEST |
| 12 |
} |
| 13 |
|
| 14 |
public class Module : XmppStreamModule { |
| 15 |
public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0363_http_file_upload"); |
| 16 |
|
| 17 |
public signal void feature_available(XmppStream stream, long max_file_size); |
| 18 |
public signal void received_url(XmppStream stream, MessageStanza message); |
| 19 |
|
| 20 |
public delegate void OnSlotOk(XmppStream stream, string url_get, string url_put); |
| 21 |
public delegate void OnError(XmppStream stream, string error); |
| 22 |
public struct SlotResult { |
| 23 |
public string url_get { get; set; } |
| 24 |
public string url_put { get; set; } |
| 25 |
public HashMap<string, string> headers { get; set; } |
| 26 |
} |
| 27 |
public async SlotResult request_slot(XmppStream stream, string filename, int64 file_size, string? content_type) throws HttpFileTransferError { |
| 28 |
Flag? flag = stream.get_flag(Flag.IDENTITY); |
| 29 |
if (flag == null) { |
| 30 |
throw new HttpFileTransferError.SLOT_REQUEST("No flag"); |
| 31 |
} |
| 32 |
|
| 33 |
StanzaNode? request_node = null; |
| 34 |
switch (flag.ns_ver) { |
| 35 |
case NS_URI_0: |
| 36 |
request_node = new StanzaNode.build("request", NS_URI_0).add_self_xmlns(); |
| 37 |
request_node.put_attribute("filename", filename).put_attribute("size", file_size.to_string()); |
| 38 |
if (content_type != null) request_node.put_attribute("content-type", content_type); |
| 39 |
break; |
| 40 |
case NS_URI: |
| 41 |
request_node = new StanzaNode.build("request", NS_URI).add_self_xmlns() |
| 42 |
.put_node(new StanzaNode.build("filename", NS_URI).put_node(new StanzaNode.text(filename))) |
| 43 |
.put_node(new StanzaNode.build("size", NS_URI).put_node(new StanzaNode.text(file_size.to_string()))); |
| 44 |
if (content_type != null) { |
| 45 |
request_node.put_node(new StanzaNode.build("content-type", NS_URI).put_node(new StanzaNode.text(content_type))); |
| 46 |
} |
| 47 |
break; |
| 48 |
} |
| 49 |
|
| 50 |
SourceFunc callback = request_slot.callback; |
| 51 |
var slot_result = SlotResult(); |
| 52 |
|
| 53 |
Iq.Stanza iq = new Iq.Stanza.get(request_node) { to=flag.file_store_jid }; |
| 54 |
|
| 55 |
HttpFileTransferError? e = null; |
| 56 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => { |
| 57 |
if (iq.is_error()) { |
| 58 |
e = new HttpFileTransferError.SLOT_REQUEST("Error getting upload/download url (Error Iq)"); |
| 59 |
Idle.add((owned) callback); |
| 60 |
return; |
| 61 |
} |
| 62 |
string? url_get = null, url_put = null; |
| 63 |
// FIXME change back to switch on version in a while (prosody bug) |
| 64 |
url_get = iq.stanza.get_deep_attribute(flag.ns_ver + ":slot", flag.ns_ver + ":get", flag.ns_ver + ":url"); |
| 65 |
url_put = iq.stanza.get_deep_attribute(flag.ns_ver + ":slot", flag.ns_ver + ":put", flag.ns_ver + ":url"); |
| 66 |
if (url_get == null && url_put == null) { |
| 67 |
url_get = iq.stanza.get_deep_string_content(flag.ns_ver + ":slot", flag.ns_ver + ":get"); |
| 68 |
url_put = iq.stanza.get_deep_string_content(flag.ns_ver + ":slot", flag.ns_ver + ":put"); |
| 69 |
} |
| 70 |
if (url_get == null || url_put == null) { |
| 71 |
e = new HttpFileTransferError.SLOT_REQUEST("Error getting upload/download url: %s".printf(iq.stanza.to_string())); |
| 72 |
Idle.add((owned) callback); |
| 73 |
return; |
| 74 |
} |
| 75 |
if (!url_get.down().has_prefix("https://") || !url_put.down().has_prefix("https://")) { |
| 76 |
e = new HttpFileTransferError.SLOT_REQUEST("Error getting upload/download url: Received non-https URL from server"); |
| 77 |
Idle.add((owned) callback); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
slot_result.headers = new HashMap<string, string>(); |
| 82 |
|
| 83 |
foreach (StanzaNode node in iq.stanza.get_deep_subnodes(flag.ns_ver + ":slot", flag.ns_ver + ":put", flag.ns_ver + ":header")) { |
| 84 |
string header_name = node.get_attribute("name"); |
| 85 |
if (header_name == "Authorization" || header_name == "Cookie" || header_name == "Expires") { |
| 86 |
string? header_val = node.get_string_content(); |
| 87 |
if (header_val != null && header_val.length < 8192) { |
| 88 |
header_val = header_val.replace("\n", "").replace("\r", ""); |
| 89 |
slot_result.headers[header_name] = header_val; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
slot_result.url_get = url_get; |
| 95 |
slot_result.url_put = url_put; |
| 96 |
|
| 97 |
Idle.add((owned) callback); |
| 98 |
}); |
| 99 |
yield; |
| 100 |
|
| 101 |
if (e != null) { |
| 102 |
throw e; |
| 103 |
} |
| 104 |
|
| 105 |
return slot_result; |
| 106 |
} |
| 107 |
|
| 108 |
public override void attach(XmppStream stream) { |
| 109 |
stream.stream_negotiated.connect(query_availability); |
| 110 |
} |
| 111 |
|
| 112 |
public override void detach(XmppStream stream) { |
| 113 |
stream.stream_negotiated.disconnect(query_availability); |
| 114 |
} |
| 115 |
|
| 116 |
public override string get_ns() { return NS_URI; } |
| 117 |
public override string get_id() { return IDENTITY.id; } |
| 118 |
|
| 119 |
private async void query_availability(XmppStream stream) { |
| 120 |
ServiceDiscovery.InfoResult? info_result = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).request_info(stream, stream.remote_name); |
| 121 |
bool available = check_ns_in_info(stream, stream.remote_name, info_result); |
| 122 |
if (!available) { |
| 123 |
ServiceDiscovery.ItemsResult? items_result = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).request_items(stream, stream.remote_name); |
| 124 |
if (items_result == null) return; |
| 125 |
|
| 126 |
for (int i = 0; i < 2; i++) { |
| 127 |
foreach (Xep.ServiceDiscovery.Item item in items_result.items) { |
| 128 |
|
| 129 |
// First try the promising items and only afterwards all the others |
| 130 |
bool promising_upload_item = item.jid.to_string().has_prefix("upload"); |
| 131 |
if ((i == 0 && !promising_upload_item) || (i == 1) && promising_upload_item) continue; |
| 132 |
|
| 133 |
ServiceDiscovery.InfoResult? info_result2 = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).request_info(stream, item.jid); |
| 134 |
bool available2 = check_ns_in_info(stream, item.jid, info_result2); |
| 135 |
if (available2) return; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
private bool check_ns_in_info(XmppStream stream, Jid jid, Xep.ServiceDiscovery.InfoResult? info_result) { |
| 142 |
if (info_result == null) return false; |
| 143 |
|
| 144 |
bool ver_available = false; |
| 145 |
bool ver_0_available = false; |
| 146 |
foreach (string feature in info_result.features) { |
| 147 |
if (feature == NS_URI_0) { |
| 148 |
ver_0_available = true; |
| 149 |
break; |
| 150 |
} else if (feature == NS_URI) { |
| 151 |
ver_available = true; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if (ver_available || ver_0_available) { |
| 156 |
long max_file_size = extract_max_file_size(info_result); |
| 157 |
if (ver_0_available) { |
| 158 |
stream.add_flag(new Flag(jid, NS_URI_0)); |
| 159 |
} else if (ver_available) { |
| 160 |
stream.add_flag(new Flag(jid, NS_URI)); |
| 161 |
} |
| 162 |
|
| 163 |
feature_available(stream, max_file_size); |
| 164 |
return true; |
| 165 |
} |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
private long extract_max_file_size(Xep.ServiceDiscovery.InfoResult info_result) { |
| 170 |
string? max_file_size_str = null; |
| 171 |
Gee.List<StanzaNode> x_nodes = info_result.iq.stanza.get_deep_subnodes("http://jabber.org/protocol/disco#info:query", "jabber:x:data:x"); |
| 172 |
foreach(StanzaNode x_node in x_nodes) { |
| 173 |
Gee.List<StanzaNode> field_nodes = x_node.get_subnodes("field", "jabber:x:data"); |
| 174 |
foreach (StanzaNode node in field_nodes) { |
| 175 |
string? var_attr = node.get_attribute("var"); |
| 176 |
if (var_attr == "max-file-size") { |
| 177 |
StanzaNode value_node = node.get_subnode("value", "jabber:x:data"); |
| 178 |
max_file_size_str = value_node.get_string_content(); |
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
if (max_file_size_str != null) return long.parse(max_file_size_str); |
| 184 |
|
| 185 |
// If there is no max-file-size node, there is no file size limit |
| 186 |
return long.MAX; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
public class ReceivedPipelineListener : StanzaListener<MessageStanza> { |
| 191 |
|
| 192 |
private string[] after_actions_const = {"EXTRACT_MESSAGE_2"}; |
| 193 |
|
| 194 |
public override string action_group { get { return "EXTRACT_MESSAGE_2"; } } |
| 195 |
public override string[] after_actions { get { return after_actions_const; } } |
| 196 |
|
| 197 |
public override async bool run(XmppStream stream, MessageStanza message) { |
| 198 |
string? oob_url = OutOfBandData.get_url_from_message(message); |
| 199 |
if (oob_url != null && oob_url == message.body) { |
| 200 |
stream.get_module(Module.IDENTITY).received_url(stream, message); |
| 201 |
} |
| 202 |
return false; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
public class Flag : XmppStreamFlag { |
| 207 |
public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "http_file_upload"); |
| 208 |
|
| 209 |
public Jid file_store_jid; |
| 210 |
public string ns_ver; |
| 211 |
public int? max_file_size; |
| 212 |
|
| 213 |
public Flag(Jid file_store_jid, string ns_ver) { |
| 214 |
this.file_store_jid = file_store_jid; |
| 215 |
this.ns_ver = ns_ver; |
| 216 |
} |
| 217 |
|
| 218 |
public override string get_ns() { return NS_URI; } |
| 219 |
public override string get_id() { return IDENTITY.id; } |
| 220 |
} |
| 221 |
|
| 222 |
} |