(* Examples from the tutorial *) open Lwt.Syntax open Jmap open Jmap_mail (* Example: Authentication *) let auth_example () = (* Using a Fastmail API token *) let token = Sys.getenv_opt "JMAP_API_TOKEN" in match token with | None -> Printf.eprintf "Error: JMAP_API_TOKEN environment variable not set\n"; Lwt.return_none | Some token -> let+ result = Jmap_mail.login_with_token ~uri:"https://api.fastmail.com/jmap/session" ~api_token:token in (* Handle the result *) match result with | Ok conn -> (* Get the primary account ID *) let account_id = let mail_capability = Jmap_mail.Capability.to_string Jmap_mail.Capability.Mail in match List.assoc_opt mail_capability conn.session.primary_accounts with | Some id -> id | None -> match conn.session.accounts with | (id, _) :: _ -> id | [] -> failwith "No accounts found" in Printf.printf "Authenticated successfully with account ID: %s\n" account_id; Some (conn, account_id) | Error e -> Printf.eprintf "Authentication error: %s\n" (match e with | Api.Connection_error msg -> "Connection error: " ^ msg | Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body | Api.Parse_error msg -> "Parse error: " ^ msg | Api.Authentication_error -> "Authentication error"); None (* Example: Working with Mailboxes *) let mailbox_example (conn, account_id) = (* Get all mailboxes *) let+ mailboxes_result = Jmap_mail.get_mailboxes conn ~account_id in match mailboxes_result with | Ok mailboxes -> Printf.printf "Found %d mailboxes\n" (List.length mailboxes); (* Find inbox - for simplicity, just use the first mailbox *) let inbox = match mailboxes with | first :: _ -> Some first | [] -> None in (match inbox with | Some m -> Printf.printf "Inbox ID: %s, Name: %s\n" m.Types.id m.Types.name; Some (conn, account_id, m.Types.id) | None -> Printf.printf "No inbox found\n"; None) | Error e -> Printf.eprintf "Error getting mailboxes: %s\n" (match e with | Api.Connection_error msg -> "Connection error: " ^ msg | Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body | Api.Parse_error msg -> "Parse error: " ^ msg | Api.Authentication_error -> "Authentication error"); None (* Example: Working with Emails *) let email_example (conn, account_id, mailbox_id) = (* Get emails from mailbox *) let+ emails_result = Jmap_mail.get_messages_in_mailbox conn ~account_id ~mailbox_id ~limit:5 () in match emails_result with | Ok emails -> begin Printf.printf "Found %d emails\n" (List.length emails); (* Display emails *) List.iter (fun (email:Jmap_mail.Types.email) -> (* Using explicit module path for Types to avoid ambiguity *) let module Mail = Jmap_mail.Types in (* Get sender info *) let from = match email.Mail.from with | None -> "Unknown" | Some addrs -> match addrs with | [] -> "Unknown" | addr :: _ -> match addr.Mail.name with | None -> addr.Mail.email | Some name -> Printf.sprintf "%s <%s>" name addr.Mail.email in (* Check for unread status *) let is_unread = List.exists (fun (kw, active) -> match kw with | Mail.Unread -> active | Mail.Custom s when s = "$unread" -> active | _ -> false ) email.Mail.keywords in (* Display email info *) Printf.printf "[%s] %s - %s\n" (if is_unread then "UNREAD" else "READ") from (Option.value ~default:"(No Subject)" email.Mail.subject) ) emails; match emails with | [] -> None | hd::_ -> Some (conn, account_id, hd.Jmap_mail.Types.id) end | Error e -> Printf.eprintf "Error getting emails: %s\n" (match e with | Api.Connection_error msg -> "Connection error: " ^ msg | Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body | Api.Parse_error msg -> "Parse error: " ^ msg | Api.Authentication_error -> "Authentication error"); None (* Run examples with Lwt *) let () = (* Set up logging *) Jmap.init_logging ~level:2 ~enable_logs:true ~redact_sensitive:true (); (* Run the examples in sequence *) let result = Lwt_main.run ( let* auth_result = auth_example () in match auth_result with | None -> Lwt.return 1 | Some conn_account -> let* mailbox_result = mailbox_example conn_account in match mailbox_result with | None -> Lwt.return 1 | Some conn_account_mailbox -> let* email_result = email_example conn_account_mailbox in match email_result with | None -> Lwt.return 1 | Some _ -> Printf.printf "All examples completed successfully\n"; Lwt.return 0 ) in exit result