-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimaplet_irmin_read.ml
164 lines (149 loc) · 5.54 KB
/
imaplet_irmin_read.ml
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(*
*
* Simple example showing how to create and use a Git store.
*
* $ make # Compile
* $ ./git_store # Run
* $ cd /tmp/irmin/test && git log # Show the Git history
*
*)
open Lwt
open IrminStorage
open Sexplib
open Irmin_unix
exception InvalidCmd
let uinput = ref []
let arg n =
if Core.Std.List.length !uinput > n then
Core.Std.List.nth_exn !uinput n
else
raise InvalidCmd
(* Enable debug outputs if DEBUG is set *)
let () =
try match Sys.getenv "DEBUG" with
| "" -> ()
| _ ->
Log.color_on ();
Log.set_log_level Log.DEBUG
with Not_found -> ()
let path = "/tmp/irmin/test"
(*
module Git =
IrminGit.Make(IrminKey.SHA1)(IrminContents.String)(IrminReference.String)
module Store = (val Git.create ~bare:true ~kind:`Disk ~root:path ())
*)
module Git = IrminGit.FS(struct
let root = Some path
let bare = true
end)
module Store = Git.Make(IrminKey.SHA1)(IrminContents.String)(IrminTag.String)
let in_line () =
Lwt_io.read_line Lwt_io.stdin
let out_line str =
Lwt_io.write Lwt_io.stdout str >>= fun () ->
Lwt_io.flush Lwt_io.stdout
let prompt str =
out_line str >>= fun () ->
in_line () >>= fun msg ->
uinput := (Core.Std.String.split msg ~on:' ');
return (arg 0)
let rec selected user mbox =
let open StorageMeta in
let open Email_message in
try
prompt (user ^ ":" ^ (IrminMailbox.to_string mbox) ^ ": ") >>= function
| "help" -> Printf.printf "all\nexists\nhelp\nlist\nmeta\nmessage
#\nclose\nremove uid\nexpunge\nstore # +-| flags-list%!";
selected user mbox
| "all" -> IrminMailbox.show_all mbox >>= fun () -> selected user mbox
| "exists" -> IrminMailbox.exists mbox >>= fun res ->
(
match res with
| `No -> Printf.printf "no\n%!"
| `Folder -> Printf.printf "folder\n%!"
| `Storage -> Printf.printf "storage\n%!"
); selected user mbox
| "meta" -> IrminMailbox.get_mailbox_metadata mbox >>= fun meta ->
Printf.printf "%s\n%!" (Sexp.to_string (sexp_of_mailbox_metadata meta));
selected user mbox
| "message" -> let pos = arg 1 in
(
let pos = int_of_string pos in
IrminMailbox.read_message mbox (`Position pos) >>= function
| `Ok (message,meta) ->
Printf.printf "%s\n%!" (Sexp.to_string (sexp_of_mailbox_message_metadata meta));
Printf.printf "%s\n%!" (Sexp.to_string (Mailbox.Message.sexp_of_t message));
return ()
| `NotFound -> Printf.printf "not found\n%!"; return ()
| `Eof -> Printf.printf "eof\n%!"; return ()
) >>= fun() -> selected user mbox
| "store" -> let pos = arg 1 in
(
let pos = int_of_string pos in
IrminMailbox.read_message mbox (`Position pos) >>= function
| `Ok (_,meta) ->
let flags = Core.Std.List.foldi !uinput ~init:[] ~f:(
fun i acc el -> Printf.printf "%s\n%!" el;if i < 3 then acc else
(States.str_to_fl ("\\" ^ el)) :: acc) in
let find l i = (Core.Std.List.find l ~f:(fun el -> if el = i then true else false)) <> None in
let meta =
(
match (arg 2) with
| "+" -> let flags = Core.Std.List.fold flags ~init:meta.flags ~f:(fun acc i ->
if find acc i then acc else i :: acc) in {meta with flags}
| "-" -> let flags = Core.Std.List.fold meta.flags ~init:[] ~f:(fun acc i ->
if find flags i then acc else i :: acc) in {meta with flags}
| "|" -> {meta with flags}
| _ -> raise InvalidCmd
)
in IrminMailbox.update_metadata mbox (`Position pos) meta >>= fun res ->
( match res with
| `Ok -> Printf.printf "updated\n%!"
| `Eof -> Printf.printf "eof\n%!"
| `NotFound -> Printf.printf "not found\n%!"
); return ()
| `NotFound -> Printf.printf "not found\n%!"; return ()
| `Eof -> Printf.printf "eof\n%!"; return ()
) >>= fun () -> selected user mbox
| "remove" -> let uid = arg 1 in IrminMailbox.remove mbox uid >>= fun () ->
selected user mbox
| "expunge" -> IrminMailbox.expunge mbox >>= fun deleted ->
Core.Std.List.iter deleted ~f:(fun i -> Printf.printf "deleted %d\n%!" i);
selected user mbox
| "list" ->
IrminMailbox.list_store mbox >>= fun l ->
Core.Std.List.iter l ~f:(fun i ->
match i with
| `Folder (f,i) -> Printf.printf "folder/%d %s\n%!" i f;
| `Storage s -> Printf.printf "storage %s\n%!" s;
);
selected user mbox
| "close" -> return ()
| _ -> Printf.printf "unknown command\n%!"; selected user mbox
with InvalidCmd -> Printf.printf "unknown command\n%!"; selected user mbox
let main () =
Store.create () >>= fun t ->
out_line "type help for commands\n" >>= fun () ->
let rec request user =
try
prompt (user ^ ": ") >>= function
| "help" -> Printf.printf "help\nselect mbox\nlist\nuser\nquit\n%!"; request user
| "user" -> prompt "user? " >>= fun user -> request user
| "select" ->
let mailbox = Str.replace_first (Str.regexp "+") " " (arg 1) in
let mbox = IrminMailbox.create user mailbox in
selected user mbox >>= fun () -> request user
| "list" ->
let mbox = IrminMailbox.create user "" in
IrminMailbox.list_store mbox >>= fun l ->
Core.Std.List.iter l ~f:(fun i -> match i with
| `Folder (i,c) -> Printf.printf "folder children:%d %s\n%!" c i
| `Storage c -> Printf.printf "storage %s\n%!" c); request user
| "quit" -> return ()
| _ -> Printf.printf "unknown command\n%!"; request user
with InvalidCmd -> Printf.printf "unknown command\n%!"; request user
in
prompt "user? " >>= fun user ->
request user
let () =
Lwt_unix.run (main())