shell.ml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * Copyright (C) The #Seppo contributors. All rights reserved.
  12. *
  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.
  17. *
  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.
  22. *
  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/>.
  25. *)
  26. (* https://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html *)
  27. let ( >>= ) = Result.bind
  28. let ( let* ) = Result.bind
  29. let err i msgs =
  30. let exe = Filename.basename Sys.executable_name in
  31. msgs |> List.cons exe |> String.concat ": " |> prerr_endline;
  32. i
  33. open Seppo_lib
  34. let webfinger acct =
  35. acct
  36. |> Webfinger.well_known_uri
  37. |> Webfinger.Client.http_get
  38. |> Lwt_main.run
  39. (* TODO add more compliance rules. Check pk. *)
  40. let actor ?(key = None) u =
  41. u
  42. |> Ap.Actor.http_get ~key
  43. |> Lwt_main.run
  44. (*
  45. >>= As2.Profile.pubkey_pem
  46. >>= As2.PubKeyPem.pub_from_pem
  47. >>= As2.PubKeyPem.check
  48. *)
  49. let exec (args : string list) =
  50. let print_version oc =
  51. let exe = Filename.basename Sys.executable_name in
  52. Printf.fprintf oc "%s: https://Seppo.mro.name/v/%s+%s\n" exe Version.dune_project_version Version.git_sha;
  53. 0
  54. and print_help oc =
  55. let _exe = Filename.basename Sys.executable_name in
  56. Printf.fprintf oc
  57. "\n\
  58. Query AP servers.\n\n\
  59. If run from commandline:\n\n\
  60. OPTIONS\n\n\
  61. \ --help, -h\n\
  62. \ print this help\n\n\
  63. \ --version, -V\n\
  64. \ print version\n\n\
  65. COMMANDS\n\n\
  66. \ webfinger alice@example.org\n\
  67. \ the webfinger json parsed and re-written.\n\n\
  68. \ actor https://example.org/actors/alice\n\
  69. \ the AP profile page parsed and re-written.\n\n\
  70. \ doap\n\
  71. \ show 'description of a project'\n\n";
  72. 0
  73. and oc = stdout in
  74. let tail s = function
  75. | Error e ->
  76. Logr.err (fun m -> m "%s '%s': %s" E.e1006 s e);
  77. 1
  78. | Ok _ ->
  79. Logr.info (fun m -> m "%s." s);
  80. 0
  81. in
  82. match args with
  83. | [ _; "-h" ] | [ _; "--help" ] -> print_help oc
  84. | [ _; "-V" ] | [ _; "--version" ] -> print_version oc
  85. | [ _; "doap" ] ->
  86. (match "doap.rdf" |> Res.read with
  87. | Some v -> Printf.fprintf oc "%s" v
  88. | None -> ());
  89. 0
  90. | [ _; "webfinger"; acct ] ->
  91. (let* q = acct
  92. |> Rfc7565.of_string
  93. |> Result.get_ok
  94. |> webfinger
  95. in
  96. q
  97. |> As2_vocab.Encode.Webfinger.query_result ~base:Uri.empty
  98. |> Ezjsonm.value_to_channel stdout;
  99. Ok q)
  100. |> tail "webfinger"
  101. | [ _; "actor"; url] ->
  102. (let* p = url |> Uri.of_string |> actor in
  103. let lang = As2_vocab.Constants.ActivityStreams.und in
  104. p
  105. |> As2_vocab.Encode.actor ~lang ~base:Uri.empty
  106. |> Ezjsonm.value_to_channel stdout;
  107. Ok p)
  108. |> tail "actor"
  109. | _ -> err 2 [ "get help with -h" ]