open Lwt.Syntax open Markup exception ClosedStream exception InvalidStanza of string type auth_mechanism = | PLAIN | Unknown of string [@@deriving show { with_path = false }] let parse_auth_mechanism = function | "PLAIN" -> PLAIN | other -> Unknown other let get (stream : (signal, async) stream) : (signal, sync) stream Lwt.t = (** [stanza stream] is a promise containing a full stanza of the fragments of [stream]. *) let traverse_stanza depth fragment = let depth = match fragment with | `Start_element _ -> depth + 1 | `End_element -> depth - 1 | _ -> depth in ([fragment], if depth = 0 then None else Some depth) in transform traverse_stanza 0 stream |> Markup_lwt.load let start domain : Portal.t Lwt.t = (** [start domain] is a promise containing a Portal (stream * push) connected to the XMPP server [domain]. Currently, it doesn't handle anything except the initial [] stanza. *) let* stream, _push = Portal.connect domain in let push = function | None -> _push (Some Portal.stanza_close); _push None; | anything -> _push anything in Some (Portal.stanza_open domain) |> push; (* TODO: check this is a good stanza *) let+ _ = get stream in stream, push