aboutsummaryrefslogtreecommitdiff
path: root/test/hello.ml
blob: 5bc38adaa9cf95491d7eea7bdcc1e6e0eea1ee4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
open! Lwt.Syntax
open! Lwt.Infix
open! Flesh

let program (stream, push) config =
  let+ _auth = Sasl.authenticate (stream, push) config
  in begin match _auth with
     | Error (NotAuthorized, Some (_, text)) -> print_endline ("Not authorized: " ^ text)
     | Error (MalformedRequest, Some (_, text)) -> print_endline ("Malformed request: " ^ text)
     | Error _ -> print_endline "Error!"
     | Ok _ -> print_endline "Success!"
     end;
     Portal.close (stream, push)

let main =
  let config : Sasl.auth_config = {
      jid = (Sys.getenv "FLESH_JID");
      password = (Sys.getenv "FLESH_PASSWORD");
      preferred_mechanisms = [Stream.PLAIN]
    }
  in let domain = (List.nth (String.split_on_char '@' config.jid) 1) in
     let* stream, push = Portal.connect domain in
     let* _id = Portal.header domain (stream, push) in
     Lwt.catch
       (fun () -> program (stream, push) config >|= (fun () -> push None))
       (fun exn ->
         push None;
         (* I suspect JavaScript's [wrap_callback] swallows the Exceptions thrown by
            OCaml, so... The next best thing is probably printing something. *)
         print_endline
           (match exn with
            | Xml.InvalidStanza stanza -> "Invalid stanza: " ^ stanza
            | _ -> "... and so I stumble back to bed.");
         Lwt.fail exn)