git » sdk » commit 27f6c56

Forgot to add this

author Stephen Paul Weber
2024-10-12 02:34:23 UTC
committer Stephen Paul Weber
2024-10-12 02:34:23 UTC
parent 9763b95e5eb7466f4013ab0d8e1640e4114890db

Forgot to add this

snikket/queries/BoB.hx +53 -0

diff --git a/snikket/queries/BoB.hx b/snikket/queries/BoB.hx
new file mode 100644
index 0000000..96ce1c2
--- /dev/null
+++ b/snikket/queries/BoB.hx
@@ -0,0 +1,53 @@
+package snikket.queries;
+
+import haxe.crypto.Base64;
+import haxe.io.Bytes;
+using StringTools;
+
+class BoB extends GenericQuery {
+	private static inline function uri(hash: Hash):String {
+		final algo = hash.algorithm == "sha-1" ? "sha1" : hash.algorithm;
+		return "cid:" + algo.urlEncode() + "+" + hash.toHex() + "@bob.xmpp.org";
+	}
+
+	public final xmlns = "urn:xmpp:bob";
+	public final queryId: String;
+	private var responseStanza:Stanza;
+	private var result: {bytes: Bytes, type: String, maxAge: Null<Int>};
+
+	public function new(to: Null<String>, uri: String) {
+		if (!uri.startsWith("cid:") || !uri.endsWith("@bob.xmpp.org") || !uri.contains("+")) throw "invalid BoB URI";
+
+		queryId = ID.short();
+		queryStanza = new Stanza("iq", { to: to, type: "get", id: queryId })
+			.tag("data", { xmlns: xmlns, cid: uri.substr(4) }).up();
+	}
+
+	public static function forHash(to: Null<String>, hash: Hash) {
+		return new BoB(to, uri(hash));
+	}
+
+	public function handleResponse(stanza:Stanza) {
+		responseStanza = stanza;
+		finish();
+	}
+
+	public function getResult() {
+		if (responseStanza == null) {
+			return null;
+		}
+		if(result == null) {
+			final data = responseStanza.getChild("data", xmlns);
+			if(data == null) {
+				return null;
+			}
+			final maxAge = data.attr.get("max-age");
+			result = {
+				bytes: Base64.decode(data.getText().replace("\n", "")),
+				type: data.attr.get("type"),
+				maxAge: maxAge == null ? null : Std.parseInt(maxAge)
+			};
+		}
+		return result;
+	}
+}