···
* JMAP_API_TOKEN=your_api_token ./fastmail_list [options]
12
-
* -unread List only unread messages
13
-
* -labels Show labels/keywords associated with messages
14
-
* -debug LEVEL Set debug level (0-4, where 4 is most verbose)
12
+
* --unread List only unread messages
13
+
* --labels Show labels/keywords associated with messages
14
+
* --debug=LEVEL Set debug level (0-4, where 4 is most verbose)
15
+
* --from=PATTERN Filter messages by sender email address
16
+
* --demo-refs Demonstrate result references feature
module Mail = Jmap_mail.Types
(** Prints the email details *)
···
115
-
(** Main function *)
117
-
(* Parse command-line arguments *)
118
-
let unread_only = ref false in
119
-
let show_labels = ref false in
120
-
let debug_level = ref 0 in
121
-
let demo_refs = ref false in
122
-
let sender_filter = ref "" in
125
-
("-unread", Arg.Set unread_only, "List only unread messages");
126
-
("-labels", Arg.Set show_labels, "Show labels/keywords associated with messages");
127
-
("-debug", Arg.Int (fun level -> debug_level := level), "Set debug level (0-4, where 4 is most verbose)");
128
-
("-demo-refs", Arg.Set demo_refs, "Demonstrate result references");
129
-
("-from", Arg.Set_string sender_filter, "Filter messages by sender email address (supports wildcards: * and ?)");
132
-
let usage_msg = "Usage: JMAP_API_TOKEN=your_token fastmail_list [options]" in
133
-
Arg.parse args (fun _ -> ()) usage_msg;
118
+
(** Main function for listing emails *)
119
+
let list_emails unread_only show_labels debug_level demo_refs sender_filter =
136
-
init_logging ~level:!debug_level ~enable_logs:(!debug_level > 0) ~redact_sensitive:true ();
121
+
init_logging ~level:debug_level ~enable_logs:(debug_level > 0) ~redact_sensitive:true ();
match Sys.getenv_opt "JMAP_API_TOKEN" with
Printf.eprintf "Error: JMAP_API_TOKEN environment variable not set\n";
141
-
Printf.eprintf "Usage: JMAP_API_TOKEN=your_token ./fastmail_list [options]\n";
142
-
Printf.eprintf "Options:\n";
143
-
Printf.eprintf " -unread List only unread messages\n";
144
-
Printf.eprintf " -labels Show labels/keywords associated with messages\n";
145
-
Printf.eprintf " -debug LEVEL Set debug level (0-4, where 4 is most verbose)\n";
146
-
Printf.eprintf " -demo-refs Demonstrate result references\n";
147
-
Printf.eprintf " -from PATTERN Filter messages by sender email address (supports wildcards: * and ?)\n";
126
+
Printf.eprintf "Usage: JMAP_API_TOKEN=your_token fastmail-list [options]\n";
(* Only print token info at Info level or higher *)
···
Printf.printf "1. Get a token from: https://app.fastmail.com/settings/tokens\n";
Printf.printf "2. Create a new token with Mail scope (read/write)\n";
Printf.printf "3. Copy the full token (example: 3de40-5fg1h2-a1b2c3...)\n";
169
-
Printf.printf "4. Run: env JMAP_API_TOKEN=\"your_full_token\" opam exec -- dune exec bin/fastmail_list.exe [options]\n\n";
148
+
Printf.printf "4. Run: env JMAP_API_TOKEN=\"your_full_token\" fastmail-list [options]\n\n";
Printf.printf "Note: This example is working correctly but needs a valid Fastmail token.\n\n";
let* result = login_with_token
···
(* Run result references demo if requested *)
demo_result_references conn primary_account_id
···
(* Apply filters based on command line arguments *)
233
-
if !unread_only then
212
+
if unread_only then
List.filter is_unread emails
···
(* Apply sender filter if specified *)
241
-
if !sender_filter <> "" then begin
242
-
Printf.printf "Filtering by sender: %s\n" !sender_filter;
220
+
if sender_filter <> "" then begin
221
+
Printf.printf "Filtering by sender: %s\n" sender_filter;
List.filter (fun email ->
244
-
Jmap_mail.email_matches_sender email !sender_filter
223
+
Jmap_mail.email_matches_sender email sender_filter
···
(* Create description of applied filters *)
253
-
let parts = if !unread_only then "unread" :: parts else parts in
254
-
let parts = if !sender_filter <> "" then ("from \"" ^ !sender_filter ^ "\"") :: parts else parts in
232
+
let parts = if unread_only then "unread" :: parts else parts in
233
+
let parts = if sender_filter <> "" then ("from \"" ^ sender_filter ^ "\"") :: parts else parts in
| [] -> "the most recent"
···
(List.length filtered_emails);
Printf.printf "--------------------------------------------\n";
265
-
List.iter (print_email ~show_labels:!show_labels) filtered_emails;
244
+
List.iter (print_email ~show_labels) filtered_emails;
247
+
(** Command line interface *)
249
+
let doc = "List only unread messages" in
250
+
Arg.(value & flag & info ["unread"] ~doc)
253
+
let doc = "Show labels/keywords associated with messages" in
254
+
Arg.(value & flag & info ["labels"] ~doc)
257
+
let doc = "Set debug level (0-4, where 4 is most verbose)" in
258
+
Arg.(value & opt int 0 & info ["debug"] ~docv:"LEVEL" ~doc)
261
+
let doc = "Demonstrate result references feature" in
262
+
Arg.(value & flag & info ["demo-refs"] ~doc)
264
+
let sender_filter =
265
+
let doc = "Filter messages by sender email address (supports wildcards: * and ?)" in
266
+
Arg.(value & opt string "" & info ["from"] ~docv:"PATTERN" ~doc)
269
+
let doc = "List emails from a Fastmail account using JMAP API" in
271
+
`S Manpage.s_description;
272
+
`P "This program connects to the Fastmail JMAP API using an authentication token
273
+
from the JMAP_API_TOKEN environment variable and lists the most recent emails
274
+
with their subjects, sender details, and labels.";
275
+
`P "You must obtain a Fastmail API token from https://app.fastmail.com/settings/tokens
276
+
and set it in the JMAP_API_TOKEN environment variable.";
277
+
`S Manpage.s_environment;
278
+
`P "$(b,JMAP_API_TOKEN) The Fastmail API authentication token (required)";
279
+
`S Manpage.s_examples;
280
+
`P "List all emails:";
281
+
`P " $(mname) $(i,JMAP_API_TOKEN=your_token)";
282
+
`P "List only unread emails:";
283
+
`P " $(mname) $(i,JMAP_API_TOKEN=your_token) --unread";
284
+
`P "List emails from a specific sender:";
285
+
`P " $(mname) $(i,JMAP_API_TOKEN=your_token) --from=user@example.com";
286
+
`P "List unread emails with labels:";
287
+
`P " $(mname) $(i,JMAP_API_TOKEN=your_token) --unread --labels";
289
+
let info = Cmd.info "fastmail-list" ~doc ~man in
290
+
Cmd.v info Term.(const (fun u l d r s ->
291
+
Lwt_main.run (list_emails u l d r s)
292
+
) $ unread_only $ show_labels $ debug_level $ demo_refs $ sender_filter)
(** Program entry point *)
270
-
let exit_code = Lwt_main.run (main ()) in
295
+
let () = exit (Cmd.eval_value cmd |> function
296
+
| Ok (`Ok exit_code) -> exit_code
297
+
| Ok (`Version | `Help) -> 0