summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorClombrong <cromblong@egregore.fun>2025-06-28 12:32:57 +0200
committerClombrong <cromblong@egregore.fun>2025-06-28 16:44:54 +0200
commitb0e746b6c0f9cd9393ff105e6d0e0f0e33ee6a43 (patch)
tree532b1b1e597f77ee468ec475b13bb86b3ef7e70c /lib
parent49e1664c5496146db69520402066ba5a9956d8b3 (diff)
feat(stream): handle STARTTLS negotiation
Diffstat (limited to 'lib')
-rw-r--r--lib/stream.ml16
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/stream.ml b/lib/stream.ml
index ee33575..2b98192 100644
--- a/lib/stream.ml
+++ b/lib/stream.ml
@@ -43,10 +43,18 @@ let parse_features (el : Xml.element) : features =
Basically, it conforms to
{{: https://datatracker.ietf.org/doc/html/rfc6120#section-4.3 }}. *)
-let negotiate (domain : string) (portal : Portal.t) : features Lwt.t =
- let* _id = Portal.header domain portal
- in let+ features = Wire.get portal.stream >|= parse_features
- in features
+let negotiate ?(prefer_starttls = true) (domain : string) (portal : Portal.t) : features Lwt.t =
+ (* Restart a stream: Send the usual business, ask for features. *)
+ let start_stream () : features Lwt.t =
+ let* _id = Portal.header domain portal
+ in Wire.get portal.stream >|= parse_features
+ in
+ let starttls features =
+ match features.starttls, prefer_starttls with
+ | `Optional, false | `None, _ -> Lwt.return features
+ | `Optional, true | `Required, _->
+ Starttls.upgrade portal >>= start_stream
+ in start_stream () >>= starttls
(** [initiate domain] initiates a stream with the XMPP server [domain].