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

892 lines · 14840 bytes Raw Blame History
1note ======== src/im.om: IMAP4rev1 client over the transport (Imap, prefix im_) ========
2note RFC 3501 commands CAPABILITY, LOGIN, AUTHENTICATE PLAIN (RFC 4616; SASL-IR RFC 4959 or a
3note continuation), LIST, SELECT, UID FETCH, UID STORE, UID MOVE (RFC 6851), UID COPY and UID EXPUNGE
4note (RFC 4315 UIDPLUS), EXPUNGE, LOGOUT. One response is read whole into store S_IRS: each literal
5note {n} is kept inline as "{n}" LF and its n bytes, except the BODY[] literal of a fetch, which is
6note streamed straight into a Maildir tmp/ file and replaced by NIL (no size limit: the arrays below
7note are windows). Tagged NO and BAD are returned as defined refusals; nothing traps on input.
8global im_v ulong 48
9note im_v: 0 tag number, 1 capabilities (1 IMAP4rev1, 2 AUTH=PLAIN, 4 SASL-IR, 8 LOGINDISABLED,
10note 16 MOVE, 32 UIDPLUS), 2 untagged BYE seen, 3 UIDVALIDITY of the selected mailbox, 4 parse cursor,
11note 5/6 token offset/length, 7 fetch UID, 8 fetch flags (D 1, F 2, R 4, S 8, T 16), 9 INTERNALDATE
12note (epoch), 10 streamed body bytes + 1, 11 tmp file fd + 1, 12 COPYUID destination UID,
13note 13 fetched, 14 flag changes pushed, 15 moves pushed, 16 adopted, 17 refused by the server,
14note 18 hierarchy delimiter (0 none), 19 response mode (0 plain, 1 LIST collect, 2 FETCH flags,
15note 3 FETCH body), 20 COPYUID destination UIDVALIDITY, 21 EXISTS, 22 literal brace position,
16note 23 tmp path length, 24 resync, 25 missing locally, 26 EXPUNGE without UIDPLUS used,
17note 27 folder name length, 28 last UID, 29 skipped mailboxes, 30 unique length, 31 state UIDVALIDITY,
18note 32 LIST: delimiter only, 33 records written, 34 tmp sequence
19global im_un uchar 128
20global im_tp uchar 4096
21global im_fn uchar 256
22text im_kbody "BODY[] "
23text im_knil "NIL"
24text im_kok "OK"
25text im_kno "NO"
26text im_kbad "BAD"
27text im_kbye "BYE"
28text im_kpreauth "PREAUTH"
29text im_kcap "CAPABILITY"
30text im_klist "LIST"
31text im_kfetch "FETCH"
32text im_kexists "EXISTS"
33text im_kuid "UID"
34text im_kflags "FLAGS"
35text im_kidate "INTERNALDATE"
36text im_kbodyk "BODY[]"
37text im_kuidv "UIDVALIDITY"
38text im_kcopyuid "COPYUID"
39text im_c1 "IMAP4rev1"
40text im_c2 "AUTH=PLAIN"
41text im_c3 "SASL-IR"
42text im_c4 "LOGINDISABLED"
43text im_c5 "MOVE"
44text im_c6 "UIDPLUS"
45text im_fd "\\Draft"
46text im_ff "\\Flagged"
47text im_fa "\\Answered"
48text im_fs "\\Seen"
49text im_ft "\\Deleted"
50text im_noselect "\\Noselect"
51text im_nonexist "\\NonExistent"
52text im_letters "DFRST"
53text im_ktmp "/tmp/"
54text im_ktmpx ".mailhand-tmp"
55
56note ---- sending ----
57note im_tag: start a command in S_W: "A<n> "
58fn im_tag to void
59put im_v 0 add get im_v 0 1
60w_reset
61w_b 65
62w_u get im_v 0
63w_b 32
64end
65
66note im_run: finish the command in S_W (CRLF), send it and read to its tagged reply:
67note 1 OK, 2 NO, 3 BAD, 4 continuation request, 0 I/O failure or protocol error
68fn im_run to ulong
69sm_crlf
70if not tx_wstore S_W 0 s_len S_W
71ret 0
72end
73ret im_wait
74end
75
76fn im_wait to ulong
77while true bound 9223372036854775807
78st = im_resp
79if ne st 6
80if eq st 2
81put im_v 17 add get im_v 17 1
82end
83if eq st 3
84put im_v 17 add get im_v 17 1
85end
86ret st
87end
88end
89ret 0
90end
91
92note ---- reading one response ----
93fn im_resp to ulong
94s_reset S_IRS
95while true bound 9223372036854775807
96if not im_rline
97im_tmpdrop
98ret 0
99end
100n = im_litlen
101if eq n 0
102break
103end
104n = sub n 1
105if and eq get im_v 19 3 im_isbody
106if not im_stream n
107ret 0
108end
109s_trunc S_IRS get im_v 22
110s_addt S_IRS im_knil
111else
112z = s_add S_IRS 10
113if not im_inline n
114ret 0
115end
116end
117end
118ret im_classify
119end
120
121note im_rline: append one line to S_IRS without its LF (a CR before the LF is dropped)
122fn im_rline to bool
123while true bound 9223372036854775807
124c = tx_getc
125if lt c 0
126ret false
127end
128if eq c 10
129n = s_len S_IRS
130if gt n 0
131if eq s_get S_IRS sub n 1 13
132s_trunc S_IRS sub n 1
133end
134end
135ret true
136end
137if not s_add S_IRS uchar c
138ret false
139end
140end
141ret false
142end
143
144note im_litlen: the line ends with "{n}": n + 1 (im_v 22 = position of "{"), else 0
145fn im_litlen to ulong
146e = s_len S_IRS
147if lt e 3
148ret 0
149end
150if ne s_get S_IRS sub e 1 125
151ret 0
152end
153q = sub e 1
154while gt q 0 bound 20
155c = s_get S_IRS sub q 1
156if or lt c 48 gt c 57
157break
158end
159q = sub q 1
160end
161if or eq q 0 eq q sub e 1
162ret 0
163end
164if gt sub sub e 1 q 18
165ret 0
166end
167if ne s_get S_IRS sub q 1 123
168ret 0
169end
170v = ulong 0
171for i ulong sub sub e 1 q bound 18
172v = add mul v 10 ulong sub s_get S_IRS add q i 48
173end
174put im_v 22 sub q 1
175ret add v 1
176end
177
178fn im_isbody to bool
179b = get im_v 22
180if lt b 7
181ret false
182end
183ret s_eqci S_IRS sub b 7 7 im_kbody
184end
185
186note im_inline: n literal bytes into S_IRS
187fn im_inline n ulong to bool
188b = new uchar 65536 0
189p = ulong 0
190while lt p n bound 9223372036854775807
191m = sub n p
192if gt m 65536
193m = 65536
194end
195k = tx_take part b 0 m
196if eq k 0
197ret false
198end
199for i ulong k bound 65536
200if not s_add S_IRS get b i
201ret false
202end
203end
204p = add p k
205end
206ret true
207end
208
209note im_stream: n literal bytes (a message) into a new file in the folder's tmp/
210fn im_stream n ulong to bool
211b = new uchar 65536 0
212im_tmpdrop
213if not im_tmpopen
214ret false
215end
216fd = int sub get im_v 11 1
217p = ulong 0
218while lt p n bound 9223372036854775807
219m = sub n p
220if gt m 65536
221m = 65536
222end
223k = tx_take part b 0 m
224if eq k 0
225im_tmpdrop
226ret false
227end
228if not im_wall fd part b 0 k
229im_tmpdrop
230ret false
231end
232p = add p k
233end
234put im_v 10 add n 1
235ret true
236end
237
238fn im_wall fd int b view uchar * to bool
239p = ulong 0
240while lt p len b bound 9223372036854775807
241r = write fd b p sub len b p
242if eq r -4
243continue
244end
245if le r 0
246ret false
247end
248p = add p ulong r
249end
250ret true
251end
252
253note im_tmpopen: create FOLDER/tmp/<now>.M<pid>Q<seq>.mailhand-tmp (exclusive, 0600)
254fn im_tmpopen to bool
255im_pfolder
256p_t im_ktmp
257p_u now_s
258p_c 46
259p_c 77
260p_u ulong getpid
261p_c 81
262put im_v 34 add get im_v 34 1
263p_u get im_v 34
264p_t im_ktmpx
265if not p_ok
266ret false
267end
268fd = fopen gp 0 get gv VPL OEXCL 384
269if lt fd 0
270ret false
271end
272n = get gv VPL
273for i ulong n bound 4096
274put im_tp i get gp i
275end
276put im_v 23 n
277put im_v 11 add ulong fd 1
278ret true
279end
280
281note im_ptmp: gp = the tmp file path
282fn im_ptmp to void
283p_reset
284for i ulong get im_v 23 bound 4096
285p_c get im_tp i
286end
287end
288
289note im_tmpclose: fsync and close the tmp file; false on an error
290fn im_tmpclose to bool
291f = get im_v 11
292if eq f 0
293ret false
294end
295put im_v 11 0
296r = fsync int sub f 1
297z = fclose int sub f 1
298ret and eq r 0 eq z 0
299end
300
301note im_tmpdrop: close and remove a tmp file that was not delivered
302fn im_tmpdrop to void
303f = get im_v 11
304if gt f 0
305z = fclose int sub f 1
306put im_v 11 0
307end
308if gt get im_v 23 0
309im_ptmp
310zl = p_unlink
311put im_v 23 0
312end
313put im_v 10 0
314end
315
316note ---- classifying ----
317fn im_classify to ulong
318c = s_get S_IRS 0
319if eq c 43
320ret 4
321end
322put im_v 4 0
323if eq c 42
324put im_v 4 1
325im_untagged
326ret 6
327end
328if not im_ourtag
329ret 0
330end
331im_word
332st = ulong 0
333if im_tok im_kok
334st = 1
335end
336if im_tok im_kno
337st = 2
338end
339if im_tok im_kbad
340st = 3
341end
342if eq st 0
343ret 0
344end
345im_rcode
346ret st
347end
348
349note im_ourtag: S_IRS starts with "A<tag> "; the cursor is left after it
350fn im_ourtag to bool
351if ne s_get S_IRS 0 65
352ret false
353end
354v = ulong 0
355p = ulong 1
356n = s_len S_IRS
357while lt p n bound 21
358c = s_get S_IRS p
359if or lt c 48 gt c 57
360break
361end
362v = add mul v 10 ulong sub c 48
363p = add p 1
364end
365if or eq p 1 ne v get im_v 0
366ret false
367end
368if ne s_get S_IRS p 32
369ret false
370end
371put im_v 4 p
372ret true
373end
374
375fn im_untagged to void
376im_word
377if eq get im_v 6 0
378ret
379end
380if or im_tok im_kok or im_tok im_kno im_tok im_kpreauth
381im_rcode
382ret
383end
384if im_tok im_kbye
385put im_v 2 1
386ret
387end
388if im_tok im_kcap
389im_caps
390ret
391end
392if im_tok im_klist
393im_listrec
394ret
395end
396n = im_tnum
397im_word
398if im_tok im_kexists
399put im_v 21 n
400ret
401end
402if im_tok im_kfetch
403im_fetchrec
404end
405end
406
407note im_rcode: an optional "[CODE ...]" after OK/NO/BAD: CAPABILITY, UIDVALIDITY, COPYUID
408fn im_rcode to void
409im_sp
410if ne im_peek 91
411ret
412end
413put im_v 4 add get im_v 4 1
414im_word
415if im_tok im_kcap
416im_caps
417ret
418end
419if im_tok im_kuidv
420im_word
421put im_v 3 im_tnum
422ret
423end
424if im_tok im_kcopyuid
425im_word
426put im_v 20 im_tnum
427im_word
428im_word
429put im_v 12 im_tnum
430end
431end
432
433fn im_caps to void
434put im_v 1 0
435while true bound 9223372036854775807
436im_word
437if eq get im_v 6 0
438ret
439end
440f = ulong 0
441if im_tok im_c1
442f = 1
443end
444if im_tok im_c2
445f = 2
446end
447if im_tok im_c3
448f = 4
449end
450if im_tok im_c4
451f = 8
452end
453if im_tok im_c5
454f = 16
455end
456if im_tok im_c6
457f = 32
458end
459put im_v 1 or get im_v 1 f
460end
461end
462
463note ---- tokens over S_IRS at cursor im_v 4 ----
464fn im_peek to uchar
465ret s_get S_IRS get im_v 4
466end
467
468fn im_sp to void
469p = get im_v 4
470n = s_len S_IRS
471while lt p n bound 9223372036854775807
472if ne s_get S_IRS p 32
473break
474end
475p = add p 1
476end
477put im_v 4 p
478end
479
480note im_word: the next atom (stops at space, "(", ")", end; "]" ends it unless it holds "[")
481fn im_word to void
482im_sp
483p = get im_v 4
484q = p
485n = s_len S_IRS
486br = false
487while lt q n bound 9223372036854775807
488c = s_get S_IRS q
489if or or eq c 32 eq c 40 eq c 41
490break
491end
492if eq c 91
493br = true
494end
495if and eq c 93 not br
496break
497end
498q = add q 1
499end
500put im_v 5 p
501put im_v 6 sub q p
502put im_v 4 q
503end
504
505fn im_tok t view uchar * to bool
506ret s_eqci S_IRS get im_v 5 get im_v 6 t
507end
508
509fn im_tnum to ulong
510o = get im_v 5
511n = get im_v 6
512if or eq n 0 gt n 19
513ret 0
514end
515v = ulong 0
516for i ulong n bound 19
517c = s_get S_IRS add o i
518if or lt c 48 gt c 57
519ret 0
520end
521v = add mul v 10 ulong sub c 48
522end
523ret v
524end
525
526note im_str: a string (quoted, literal, NIL or atom) appended to store d: 1 string, 2 NIL, 0 error
527fn im_str d ulong to ulong
528im_sp
529c = im_peek
530if eq c 34
531ret im_quoted d
532end
533if eq c 123
534ret im_litcopy d
535end
536im_word
537if eq get im_v 6 0
538ret 0
539end
540if im_tok im_knil
541ret 2
542end
543if lt d 1000
544s_adds d S_IRS get im_v 5 get im_v 6
545end
546ret 1
547end
548
549fn im_quoted d ulong to ulong
550p = add get im_v 4 1
551n = s_len S_IRS
552while lt p n bound 9223372036854775807
553c = s_get S_IRS p
554if eq c 34
555put im_v 4 add p 1
556ret 1
557end
558if eq c 92
559p = add p 1
560c = s_get S_IRS p
561end
562if lt d 1000
563z = s_add d c
564end
565p = add p 1
566end
567put im_v 4 n
568ret 0
569end
570
571note im_litcopy: "{n}" LF and n bytes (kept inline by im_resp) appended to d
572fn im_litcopy d ulong to ulong
573p = add get im_v 4 1
574n = s_len S_IRS
575v = ulong 0
576k = ulong 0
577while lt p n bound 20
578c = s_get S_IRS p
579if or lt c 48 gt c 57
580break
581end
582v = add mul v 10 ulong sub c 48
583k = add k 1
584p = add p 1
585end
586if or eq k 0 or ne s_get S_IRS p 125 ne s_get S_IRS add p 1 10
587put im_v 4 n
588ret 0
589end
590p = add p 2
591if gt v sub n p
592put im_v 4 n
593ret 0
594end
595if lt d 1000
596s_adds d S_IRS p v
597end
598put im_v 4 add p v
599ret 1
600end
601
602note im_skipval: skip one value (atom, string, literal or a parenthesized list)
603fn im_skipval to void
604im_sp
605c = im_peek
606if eq c 40
607im_skiplist
608ret
609end
610if or eq c 34 eq c 123
611z = im_str 1000
612ret
613end
614im_word
615end
616
617fn im_skiplist to void
618n = s_len S_IRS
619depth = ulong 0
620while lt get im_v 4 n bound 9223372036854775807
621im_sp
622c = im_peek
623if eq c 40
624depth = add depth 1
625put im_v 4 add get im_v 4 1
626continue
627end
628if eq c 41
629depth = sub depth 1
630put im_v 4 add get im_v 4 1
631if eq depth 0
632ret
633end
634continue
635end
636if or eq c 34 eq c 123
637z = im_str 1000
638continue
639end
640im_word
641if eq get im_v 6 0
642put im_v 4 add get im_v 4 1
643end
644end
645end
646
647note ---- FETCH and LIST records ----
648fn im_fetchrec to void
649im_sp
650if ne im_peek 40
651ret
652end
653put im_v 4 add get im_v 4 1
654put im_v 7 0
655put im_v 8 0
656put im_v 9 0
657n = s_len S_IRS
658while lt get im_v 4 n bound 9223372036854775807
659im_sp
660if eq im_peek 41
661break
662end
663im_word
664if eq get im_v 6 0
665break
666end
667im_fetchitem
668end
669m = get im_v 19
670if eq m 2
671if gt get im_v 7 0
672s_addu S_ILS get im_v 7
673z = s_add S_ILS 10
674end
675end
676if eq m 3
677im_fetched
678end
679end
680
681fn im_fetchitem to void
682if im_tok im_kuid
683im_word
684put im_v 7 im_tnum
685ret
686end
687if im_tok im_kflags
688im_flags
689ret
690end
691if im_tok im_kidate
692s_reset S_TMP
693if eq im_str S_TMP 1
694put im_v 9 im_date
695end
696ret
697end
698im_skipval
699end
700
701note im_flags: "(\Seen \Answered ...)" into im_v 8; keywords and \Recent are ignored
702fn im_flags to void
703im_sp
704if ne im_peek 40
705im_skipval
706ret
707end
708put im_v 4 add get im_v 4 1
709f = ulong 0
710while true bound 9223372036854775807
711im_word
712if eq get im_v 6 0
713break
714end
715if im_tok im_fd
716f = or f 1
717end
718if im_tok im_ff
719f = or f 2
720end
721if im_tok im_fa
722f = or f 4
723end
724if im_tok im_fs
725f = or f 8
726end
727if im_tok im_ft
728f = or f 16
729end
730end
731if eq im_peek 41
732put im_v 4 add get im_v 4 1
733end
734put im_v 8 f
735end
736
737note im_date: INTERNALDATE "dd-Mon-yyyy hh:mm:ss +zzzz" in S_TMP as epoch seconds (0 if malformed)
738fn im_date to ulong
739if ne s_len S_TMP 26
740ret 0
741end
742d = im_dnum 0 2
743mo = im_mon
744y = im_dnum 7 4
745if or or eq mo 0 lt y 1970 or gt d 31 eq d 0
746ret 0
747end
748tod = im_tod
749if gt tod 86400
750ret 0
751end
752t = add mul ulong days_civil long y long mo long d 86400 tod
753z = add mul im_dnum 22 2 3600 mul im_dnum 24 2 60
754if eq s_get S_TMP 21 45
755ret add t z
756end
757if lt t z
758ret 0
759end
760ret sub t z
761end
762
763note im_tod: seconds since midnight of "hh:mm:ss" at offset 12 (more than 86400 when malformed)
764fn im_tod to ulong
765h = im_dnum 12 2
766mi = im_dnum 15 2
767s = im_dnum 18 2
768if or or gt h 23 gt mi 59 gt s 60
769ret 100000
770end
771ret add add mul h 3600 mul mi 60 s
772end
773
774fn im_mon to ulong
775for i ulong 12
776ok = true
777for j ulong 3
778if ne lc s_get S_TMP add 3 j lc get sd_months add mul i 3 j
779ok = false
780end
781end
782if ok
783ret add i 1
784end
785end
786ret 0
787end
788
789fn im_dnum o ulong n ulong to ulong
790v = ulong 0
791for i ulong n bound 4
792c = s_get S_TMP add o i
793if eq c 32
794c = 48
795end
796if or lt c 48 gt c 57
797ret 100000
798end
799v = add mul v 10 ulong sub c 48
800end
801ret v
802end
803
804note im_listrec: "* LIST (flags) delim name": the delimiter, and with mode 1 the local folder name
805note of a selectable mailbox appended to S_IFL (unsafe or unmappable names are counted, not kept)
806fn im_listrec to void
807im_sp
808noselect = false
809if eq im_peek 40
810put im_v 4 add get im_v 4 1
811while true bound 9223372036854775807
812im_word
813if eq get im_v 6 0
814break
815end
816if or im_tok im_noselect im_tok im_nonexist
817noselect = true
818end
819end
820if eq im_peek 41
821put im_v 4 add get im_v 4 1
822end
823end
824s_reset S_TMP
825k = im_str S_TMP
826put im_v 18 0
827if and eq k 1 eq s_len S_TMP 1
828put im_v 18 ulong s_get S_TMP 0
829end
830if eq get im_v 32 1
831ret
832end
833s_reset S_TMP
834if ne im_str S_TMP 1
835ret
836end
837if or noselect ne get im_v 19 1
838ret
839end
840if not im_tolocal
841put im_v 29 add get im_v 29 1
842ret
843end
844s_adds S_IFL S_TMP 0 s_len S_TMP
845z = s_add S_IFL 10
846end
847
848note im_tolocal: the remote mailbox name in S_TMP becomes the local folder name (INBOX, or the
849note name with the delimiter turned into "."); false when it is not a safe Maildir++ name
850fn im_tolocal to bool
851n = s_len S_TMP
852if s_eqci S_TMP 0 n kinbox
853s_reset S_TMP
854s_addt S_TMP kinbox
855ret true
856end
857dl = uchar get im_v 18
858b = new uchar 256 0
859if or eq n 0 gt n 255
860ret false
861end
862for i ulong n bound 256
863c = s_get S_TMP i
864if and ne dl 46 eq c 46
865ret false
866end
867if and ne dl 0 eq c dl
868c = 46
869end
870put b i c
871end
872s_reset S_TMP
873s_addt S_TMP part b 0 n
874ret s_safe S_TMP 0 n
875end
876
877note im_wremote: append the remote name of local folder name s[o, +n) to S_W as a quoted string
878fn im_wremote s ulong o ulong n ulong to void
879w_b 34
880dl = uchar get im_v 18
881for i ulong n bound 9223372036854775807
882c = s_get s add o i
883if and ne dl 0 eq c 46
884c = dl
885end
886if or eq c 34 eq c 92
887w_b 92
888end
889w_b c
890end
891w_b 34
892end