commit d0a1e21776ce4db734ff38260a5636c2a434811c Author: lost+skunk Date: Mon Sep 9 10:50:58 2024 +0000 Add main.d diff --git a/main.d b/main.d new file mode 100644 index 0000000..dc8041d --- /dev/null +++ b/main.d @@ -0,0 +1,57 @@ +import std.stdio, std.json, std.getopt; +import std.net.curl, std.conv: to; +import core.stdc.stdlib: exit; + +void main(string[] args) { + string serverName; + try getopt(args, config.required, "server|s", "Matrix server domain", &serverName); + catch (GetOptException e) { + writeln(e.msg); + exit(1); + } + + CHECK: + serverName = "https://"~serverName; + auto resp = Request(serverName~"/_matrix/federation/v1/version"); + + if (resp.status == 200) { + JSONValue srv = parseJSON(resp.content)["server"]; + writefln("%s: %s", srv["name"].str(), srv["version"].str()); + } else { + resp = Request(serverName~"/.well-known/matrix/server"); + if (resp.status != 200) { + writeln("Coudn't find matrix server on this domain."); + exit(1); + } + + serverName = parseJSON(resp.content)["m.server"].str(); + goto CHECK; + } +} + +struct Request { + this(string url) { + this.perform(url); + } + + string content; + string[string] headers; + ushort status; + + void perform(string url) { + auto request = HTTP(url); + try { + request.onReceive = (ubyte[] data) { + foreach (s; data) + Request.content ~= to!char(s); + return data.length; + }; + request.perform(); + Request.headers = request.responseHeaders(); + Request.status = request.statusLine().code; + } catch (CurlException e) { + writeln(e.msg); + exit(1); + } + } +} \ No newline at end of file