example.com -> example.org
[Seppo.git] / chkr / shell.ml
blob4cd027801c500a2d7c612cfdbd92c2174b28e8ab
1 (*
2 * _ _ ____ _
3 * _| || |_/ ___| ___ _ __ _ __ ___ | |
4 * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
5 * |_ _|___) | __/ |_) | |_) | (_) |_|
6 * |_||_| |____/ \___| .__/| .__/ \___/(_)
7 * |_| |_|
9 * Personal Social Web.
11 * Copyright (C) The #Seppo contributors. All rights reserved.
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 (* https://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html *)
29 let ( >>= ) = Result.bind
30 let ( let* ) = Result.bind
32 let err i msgs =
33 let exe = Filename.basename Sys.executable_name in
34 msgs |> List.cons exe |> String.concat ": " |> prerr_endline;
37 open Seppo_lib
39 let webfinger acct =
40 let w = acct |> Webfinger.well_known_uri in
41 (try w |> Webfinger.Client.http_get
42 with
43 Ezjsonm.Parse_error (_, e) ->
44 Logr.err (fun m -> m "%s failed to decipher %a" E.e9001 Uri.pp w);
45 Lwt.return (Error e))
46 |> Lwt_main.run
48 (* TODO add more compliance rules. Check pk. *)
49 let actor ?(key = None) u =
51 |> Ap.Actor.http_get ~key
52 |> Lwt_main.run
54 >>= As2.Profile.pubkey_pem
55 >>= As2.PubKeyPem.pub_from_pem
56 >>= As2.PubKeyPem.check
59 let exec (args : string list) =
60 let print_version oc =
61 let exe = Filename.basename Sys.executable_name in
62 Printf.fprintf oc "%s: https://Seppo.Social/v/%s+%s\n" exe Version.dune_project_version Version.git_sha;
64 and print_help oc =
65 let _exe = Filename.basename Sys.executable_name in
66 Printf.fprintf oc
67 "\n\
68 Query AP servers.\n\n\
69 If run from commandline:\n\n\
70 OPTIONS\n\n\
71 \ --help, -h\n\
72 \ print this help\n\n\
73 \ --version, -V\n\
74 \ print version\n\n\
75 COMMANDS\n\n\
76 \ webfinger alice@example.org\n\
77 \ the webfinger json parsed and re-written.\n\n\
78 \ actor https://example.org/actors/alice\n\
79 \ the AP profile page parsed and re-written.\n\n\
80 \ doap\n\
81 \ show 'description of a project'\n\n";
83 and oc = stdout in
84 let tail s = function
85 | Error e ->
86 Logr.err (fun m -> m "%s '%s': %s" E.e1006 s e);
88 | Ok _ ->
89 Logr.info (fun m -> m "%s." s);
92 match args with
93 | [ _; "-h" ] | [ _; "--help" ] -> print_help oc
94 | [ _; "-V" ] | [ _; "--version" ] -> print_version oc
95 | [ _; "doap" ] ->
96 (match "doap.rdf" |> Res.read with
97 | Some v -> Printf.fprintf oc "%s" v
98 | None -> ());
100 | [ _; "webfinger"; acct ] ->
101 (let* q = acct
102 |> Rfc7565.of_string
103 |> Result.get_ok
104 |> webfinger in
106 |> As2_vocab.Encode.Webfinger.query_result ~base:Uri.empty
107 |> Ezjsonm.value_to_channel stdout;
108 Ok q)
109 |> tail "webfinger"
110 | [ _; "actor"; url] ->
111 (let* p = url |> Uri.of_string |> actor in
112 let lang = As2_vocab.Constants.ActivityStreams.und in
114 |> As2_vocab.Encode.person ~lang ~base:Uri.empty
115 |> Ezjsonm.value_to_channel stdout;
116 Ok p)
117 |> tail "actor"
118 | _ -> err 2 [ "get help with -h" ]