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 / setup.om

236 lines · 4662 bytes Raw Blame History
1note ======== src/setup.om: the human's one setup command, and the digest ========
2note setup --maildir PATH --me ADDRESS [--name TEXT] [--smtp unix:/PATH|127.0.0.1:PORT] [--helo NAME]
3note creates HOME (0700) with config, policy (default: nothing is sent without approval), rules
4note (examples, all commented out), approvals, drafts/ outbox/ sent/ labels/, audit.log, and the
5note Maildir root with INBOX, Sent, Drafts and Archive when missing. It refuses when HOME/config
6note exists, so it never overwrites a policy the human edited.
7text ksetup "setup"
8text kmdarg "maildir"
9text kmearg "me"
10text knamearg "name"
11text knamek "name"
12text ksmtpk "smtp"
13text khelok "helo"
14text ksmtparg "smtp"
15text kheloarg "helo"
16text kalready "already_set_up"
17text kbadarg "bad_argument"
18text kpolf "/policy"
19text kapprf "/approvals"
20text kdraftd "/drafts"
21text koutd "/outbox"
22text klabd "/labels"
23text kcfghead "# mailhand config, written by setup. One \"key value\" per line.\n"
24text kpolicy "# mailhand send policy. Only the human edits this file; nothing in any mail changes it.\n# Default: nothing leaves without your approval of that exact draft:\n# mailhand approve --draft SHA (shows the draft, asks yes)\n# Standing permissions you may add (each covers only recipients already in your address book,\n# never attachments):\n# allow reply-known-thread replies, to known people, in threads you took part in\n# allow known any draft to known people\n# allow known max-recipients 3\ndefault approval\n"
25text krules "# mailhand triage rules. Only the human edits this file. One rule per line:\n# if HEADER contains TEXT then label LABEL\n# Examples (remove the # to use):\n# if List-Id contains example.org then label lists\n# if Subject contains invoice then label bills\n"
26text kfsent ".Sent"
27text kfdraft ".Drafts"
28text kfarch ".Archive"
29text khomek2 "home"
30text kmaildirk "maildir"
31text kpolk "policy"
32text kdefpol "default approval: nothing is sent without your approval of the exact draft"
33text kneedabs "maildir must be an absolute path"
34text kerrroot "cannot create the Maildir root (its parent must exist)"
35
36fn su_cfgline k view uchar * a view uchar * to bool
37if not q_get a
38ret false
39end
40o = get gv VQO
41n = get gv VQN
42for i ulong n bound 9223372036854775807
43c = s_get S_REQ add o i
44if or lt c 32 eq c 127
45ret false
46end
47end
48w_t k
49w_b 32
50w_qv
51w_b 10
52ret true
53end
54
55note su_mkdirs: dir gp and its cur, new, tmp
56fn su_maildir to bool
57p_save
58zl = p_mkdir
59if ne p_kind 1
60ret false
61end
62for t ulong 3
63p_restore
64mv_sub t
65zl = p_mkdir
66if ne p_kind 1
67ret false
68end
69end
70p_restore
71ret true
72end
73
74fn su_folder f view uchar * to bool
75p_reset
76p_r
77p_c 47
78p_t f
79ret su_maildir
80end
81
82fn su_homedir t view uchar * to bool
83p_reset
84p_h
85p_t t
86zl = p_mkdir
87ret eq p_kind 1
88end
89
90fn su_file t view uchar * body view uchar * to long
91w_reset
92w_t body
93p_reset
94p_h
95p_t t
96ret f_new
97end
98
99fn op_setup to int
100if not cf_home
101diag kerrhome
102ret EUSAGE
103end
104p_reset
105p_h
106p_t kcfgf
107if ne p_kind 0
108o_ob
109o_ks kopk ksetup
110o_kb kokk false
111o_ks krefk kalready
112o_oe
113o_nl
114ret EUSAGE
115end
116if not q_get kmdarg
117diag2 kmissing kmdarg
118ret EUSAGE
119end
120if or eq get gv VQN 0 gt get gv VQN 3000
121ret su_bad
122end
123if ne s_get S_REQ get gv VQO 47
124diag kneedabs
125ret su_bad
126end
127for i ulong get gv VQN bound 4096
128put gr i s_get S_REQ add get gv VQO i
129end
130put gv VRL get gv VQN
131w_reset
132w_t kcfghead
133if not su_cfgline kmaildirk kmdarg
134ret su_bad
135end
136if not su_cfgline kme kmearg
137diag2 kmissing kmearg
138ret su_bad
139end
140z = su_cfgline knamek knamearg
141z = su_cfgline ksmtpk ksmtparg
142z = su_cfgline khelok kheloarg
143p_reset
144p_h
145p_save
146zl = p_mkdir
147if ne p_kind 1
148ret su_bad
149end
150s_reset S_AUX
151s_adds S_AUX S_W 0 s_len S_W
152r = su_rest
153if ne r EOK
154ret r
155end
156w_reset
157w_s S_AUX 0 s_len S_AUX
158p_reset
159p_h
160p_t kcfgf
161k = f_new
162if lt k 0
163ret EIO
164end
165r = cf_init
166if ne r EOK
167ret r
168end
169r = op_book
170o_ob
171o_ks kopk ksetup
172o_kb kokk true
173o_k khomek2
174o_vbeg
175o_b 34
176for i ulong get gv VHL bound 4096
177o_jc get gh i
178end
179o_b 34
180o_k kpolk
181o_vs kdefpol
182o_oe
183o_nl
184ret EOK
185end
186
187
188fn su_bad to int
189o_ob
190o_ks kopk ksetup
191o_kb kokk false
192o_ks krefk kbadarg
193o_oe
194o_nl
195ret EUSAGE
196end
197
198note su_rest: directories and default files (all created exclusively; existing ones are kept)
199fn su_rest to int
200if not su_homedir kdraftd
201ret EIO
202end
203if not su_homedir koutd
204ret EIO
205end
206if not su_homedir ksentdir
207ret EIO
208end
209if not su_homedir klabd
210ret EIO
211end
212zl = su_file kpolf kpolicy
213zl = su_file krulesf krules
214w_reset
215p_reset
216p_h
217p_t kapprf
218zl = f_new
219p_reset
220p_r
221if not su_maildir
222diag kerrroot
223ret EIO
224end
225if not su_folder kfsent
226ret EIO
227end
228if not su_folder kfdraft
229ret EIO
230end
231if not su_folder kfarch
232ret EIO
233end
234ret EOK
235end
236