MSVC/main.d
lost+skunk f318426a1d Update main.d
так более лаконично
2024-09-09 11:14:58 +00:00

56 lines
1.5 KiB
D

import std.stdio, std.json;
import std.net.curl, std.conv: to;
import core.stdc.stdlib: exit;
void main(string[] arg) {
if (arg.length < 2) {
writefln("Missing server name. Specify it by following example: '%s ebloid.ru'", arg[0]);
exit(1);
}
string serverName = arg[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);
}
}
}