-module(discovery). -export([parse_acct/1, parse_resource/1, actor_url_for/2, webfinger_body/3]). %% Discovery primitives per design §13.7. Step 10a covers the %% local-side webfinger endpoint (responding when a peer asks %% "where does acct:alice@here live?"); the peer-fetch direction %% (loading a peer's actor doc lazily on first inbound) is Step 10b %% and gates on Blockers #2 (native http-request primitive). %% %% parse_acct/1 — accept a binary in either form: %% <<"acct:alice@host:port">> (full prefixed URI) %% <<"alice@host:port">> (bare account, prefix optional) %% Returns {ok, User, Host} | {error, Reason}. %% %% parse_resource/1 — the resource= query parameter from %% /.well-known/webfinger. Same shape as parse_acct. %% %% actor_url_for/2(User, Host) — synthesises the canonical %% per-actor URL `:///actors/`. v2 hardcodes %% http://; TLS / https is v3 (Blockers gate). %% %% webfinger_body/3 — builds the JSON response body. %% ── parse_acct / parse_resource ───────────────────────────────── %% "acct:" -> 5 bytes: 97 99 99 116 58 parse_acct(Bin) when is_binary(Bin) -> AcctPrefix = <<97,99,99,116,58>>, case strip_prefix(AcctPrefix, Bin) of {ok, Rest} -> split_user_host(Rest); nomatch -> split_user_host(Bin) end; parse_acct(_) -> {error, bad_input}. parse_resource(Bin) -> parse_acct(Bin). %% strip_prefix/2 — return {ok, Rest} when Bin starts with Prefix, %% else nomatch. Substrate has no proper prefix-match BIF; this %% byte-walks. strip_prefix(<<>>, Rest) -> {ok, Rest}; strip_prefix(<>, <>) -> strip_prefix(PRest, RRest); strip_prefix(_, _) -> nomatch. %% split_user_host/1 — split a `user@host[:port]` binary at the %% first `@`. Returns {ok, User, Host} where Host may include the %% optional port suffix. split_user_host(Bin) -> case split_at(64, Bin) of % 64 = '@' {Before, After} when byte_size(Before) > 0, byte_size(After) > 0 -> {ok, Before, After}; _ -> {error, bad_acct} end. split_at(Byte, Bin) -> split_at(Byte, Bin, <<>>). split_at(_, <<>>, Acc) -> {Acc, <<>>}; split_at(Byte, <>, Acc) -> {Acc, Rest}; split_at(Byte, <>, Acc) -> split_at(Byte, Rest, <>). %% ── URL synthesis ────────────────────────────────────────────── %% "http://" -> 7 bytes | "/actors/" -> 8 bytes actor_url_for(User, Host) -> Pre = <<104,116,116,112,58,47,47>>, % "http://" Mid = <<47,97,99,116,111,114,115,47>>, % "/actors/" <
>.

%% ── webfinger JSON body ────────────────────────────────────────
%%
%% Mastodon-shape per RFC 7033:
%%   {"subject":"acct:@",
%%    "links":[{"rel":"self",
%%              "type":"application/activity+json",
%%              "href":""}]}
%%
%% Hand-rolled byte concatenation — no JSON BIF on this port. The
%% caller has already validated User + Host; we don't need to
%% re-escape (Mastodon's webfinger inputs are alphanumeric +
%% .-_ in practice).

webfinger_body(User, Host, ActorUrl) ->
    AcctPre = <<123,34,115,117,98,106,101,99,116,34,58,34,97,99,99,116,58>>,  % '{"subject":"acct:'
    AcctAt  = <<64>>,                                                          % '@'
    LinksHd = <<34,44,34,108,105,110,107,115,34,58,91,123,34,114,101,108,34,58,34,115,101,108,102,34,44,
                34,116,121,112,101,34,58,34,97,112,112,108,105,99,97,116,105,111,110,47,97,99,116,
                105,118,105,116,121,43,106,115,111,110,34,44,34,104,114,101,102,34,58,34>>,         % '","links":[{"rel":"self","type":"application/activity+json","href":"'
    LinksTl = <<34,125,93,125,10>>,                                            % '"}]}\n'
    <>.