Skip to content

Commit

Permalink
Forgot to add this
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Oct 12, 2024
1 parent 9763b95 commit 27f6c56
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions snikket/queries/BoB.hx
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 27f6c56

Please sign in to comment.