interplane.Get Omega
Read in your language

mailhand

mailhand: agents look after a person's email in seeded Omega. Maildir + MIME parsing, JSONL CLI and MCP, rule triage, policy-gated sending, hash-chained audit. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

mailhand / src / cli.om

242 lines · 4229 bytes Raw Blame History
1note ======== src/cli.om: the command line, the op table, the audit hook ========
2note mailhand OP [--key value | --flag | word]... JSONL on stdout, diagnostics on stderr, exit code
3note per README "Exit codes". Every op, from the CLI or from MCP, goes through cli_run: it runs with
4note its output captured in store S_OUT, then appends the audit record, then the CLI prints S_OUT.
5global gop uchar 64
6const CL_OPN ulong 87
7text kusage "usage: mailhand OP [--key value]... ; OP is one of setup list show thread search triage label move mark-read archive draft-reply draft-new drafts queue-send approve deliver book digest audit verify stats sync parse mcp version"
8text kunknown "unknown_op"
9text knotsetup "not_set_up"
10text kversion "version"
11text kvers "mailhand 1 (seeded Omega; compiler pinned in PIN)"
12text kop_setup "setup"
13text kop_list "list"
14text kop_show "show"
15text kop_thread "thread"
16text kop_search "search"
17text kop_triage "triage"
18text kop_label "label"
19text kop_move "move"
20text kop_mr "mark-read"
21text kop_arch "archive"
22text kop_dr "draft-reply"
23text kop_dn "draft-new"
24text kop_drafts "drafts"
25text kop_qs "queue-send"
26text kop_appr "approve"
27text kop_del "deliver"
28text kop_book "book"
29text kop_dig "digest"
30text kop_aud "audit"
31text kop_ver "verify"
32text kop_stats "stats"
33text kop_sync "sync"
34text kop_parse "parse"
35text kop_mcp "mcp"
36text kauderr "could not append the audit record"
37
38
39fn cl_is t view uchar * to bool
40n = get gv CL_OPN
41if ne n len t
42ret false
43end
44for i ulong n bound 64
45if ne get gop i get t i
46ret false
47end
48end
49ret true
50end
51
52note cl_dispatch: the op named in gop; -1 when there is no such op
53fn cl_dispatch to int
54if cl_is kop_list
55ret op_list
56end
57if cl_is kop_show
58ret op_show
59end
60if cl_is kop_thread
61ret op_thread
62end
63if cl_is kop_search
64ret op_search
65end
66if cl_is kop_triage
67ret op_triage
68end
69if cl_is kop_label
70ret op_label
71end
72if cl_is kop_move
73ret op_move
74end
75if cl_is kop_mr
76ret op_markread
77end
78if cl_is kop_arch
79ret op_archive
80end
81ret cl_dispatch2
82end
83
84fn cl_dispatch2 to int
85if cl_is kop_dr
86ret op_draft_reply
87end
88if cl_is kop_dn
89ret op_draft_new
90end
91if cl_is kop_drafts
92ret op_drafts
93end
94if cl_is kop_qs
95ret op_queue_send
96end
97if cl_is kop_appr
98ret op_approve
99end
100if cl_is kop_del
101ret op_deliver
102end
103if cl_is kop_book
104ret op_book
105end
106if cl_is kop_dig
107ret op_digest
108end
109if cl_is kop_aud
110ret op_audit
111end
112if cl_is kop_stats
113ret op_stats
114end
115if cl_is kop_ver
116ret op_verify
117end
118if cl_is kop_sync
119ret op_sync
120end
121ret -1
122end
123
124note cli_run: run the op named by store range [off, +n) with the request already in S_REQ.
125note The op's JSONL ends up in S_OUT; the audit record is appended; returns the exit code.
126fn cli_run s ulong off ulong n ulong to int
127put gv CL_OPN 0
128if le n 63
129for i ulong n bound 64
130put gop i s_get s add off i
131end
132put gv CL_OPN n
133end
134z = s_mem S_OUT
135put gv VMODE 1
136put gv VOL 0
137put gv VDEP 0
138put gv VAK 0
139r = cl_dispatch
140if lt r 0
141o_ob
142o_ks kopk kunknown
143o_kb kokk false
144o_oe
145o_nl
146r = EUSAGE
147end
148o_flush
149k = cl_audit r
150if lt k 0
151diag kauderr
152if eq r EOK
153r = EIO
154end
155end
156ret r
157end
158
159
160fn main to int
161put gv VMCP 0
162z = s_mem S_REQ
163if lt argcnt 2
164diag kusage
165ret EUSAGE
166end
167b = new uchar 64 0
168k = argread 1 b 0 64
169if lt k 0
170diag kusage
171ret EUSAGE
172end
173if not q_from_argv 2
174diag kusage
175ret EUSAGE
176end
177s_reset S_Y
178s_addt S_Y part b 0 ulong k
179if s_eqt S_Y 0 s_len S_Y kversion
180diag kvers
181ret EOK
182end
183if s_eqt S_Y 0 s_len S_Y kop_parse
184put gv VMODE 0
185r = op_parse
186o_flush
187ret r
188end
189if s_eqt S_Y 0 s_len S_Y kop_setup
190ret cl_setup
191end
192r = cf_init
193if ne r EOK
194cl_notsetup r
195ret r
196end
197if s_eqt S_Y 0 s_len S_Y kop_mcp
198put gv VMCP 1
199put gv VMODE 0
200ret op_mcp
201end
202s_reset S_TMP
203s_adds S_TMP S_Y 0 s_len S_Y
204r = cli_run S_TMP 0 s_len S_TMP
205zl = s_wfd S_OUT 1
206ret r
207end
208
209fn cl_notsetup r int to void
210put gv VMODE 0
211o_ob
212o_kb kokk false
213if eq r ENOSETUP
214o_ks krefk knotsetup
215end
216o_oe
217o_nl
218o_flush
219end
220
221note cl_setup: setup runs before a config exists; its record starts the audit chain
222fn cl_setup to int
223z = s_mem S_OUT
224put gv VMODE 1
225put gv VOL 0
226r = op_setup
227o_flush
228if eq r EOK
229k = au_append kop_setup r
230end
231zl = s_wfd S_OUT 1
232ret r
233end
234
235fn cl_audit r int to long
236b = new uchar 64 0
237n = get gv CL_OPN
238for i ulong n bound 64
239put b i get gop i
240end
241ret au_append part b 0 n r
242end