summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/stream.ml17
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/stream.ml b/lib/stream.ml
index 01d6a55..ee33575 100644
--- a/lib/stream.ml
+++ b/lib/stream.ml
@@ -47,3 +47,20 @@ 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
+
+(** [initiate domain] initiates a stream with the XMPP server [domain].
+
+ Once [None] is pushed into the stream, the receiving stream is drained and the
+ socket is closed. *)
+let initiate (domain : string) : (Portal.t * features) Lwt.t =
+ let open Portal in
+ let* p = connect domain
+ in let push = function
+ | Some n -> p.push (Some n)
+ | None ->
+ p.push (Some close);
+ (* Empty the stream completely, then close the socket. *)
+ Lwt.async (fun () -> let+ () = Markup_lwt.drain p.stream in p.push None)
+ in let portal = {p with push} in
+ let+ features = negotiate domain portal
+ in (portal, features)