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

844 lines · 13570 bytes Raw Blame History
1note ======== src/know.om: threads, the address book, triage ========
2note ---- thread ----
3note A thread is the closure of: messages with the same thread id (th_tid), plus every message whose
4note Message-ID, In-Reply-To or References meets the id set gathered so far. Passes over the whole
5note mail store repeat until a pass adds nothing (each pass adds at least one id or stops), so a chain
6note of any length is followed. S_THR holds the id set, one id per line.
7global gthr uchar 8
8const TH_CHG ulong 77
9const TH_N ulong 78
10text kthread "thread"
11text kpasses "passes"
12text kfolderall "all"
13const BK_LD ulong 79
14text kbookf "/book"
15text ksidf "/sentids"
16text ksentdir "/sent"
17text kbook "book"
18text khto "To"
19text khcc "Cc"
20text khbcc "Bcc"
21text kaddrk "addresses"
22text ksidk "sent_ids"
23text kmsgk "messages"
24const TR_F ulong 80
25global gtr ulong 16
26text ktriage "triage"
27text khlid "List-Id"
28text khlun "List-Unsubscribe"
29text khprec "Precedence"
30text khauto "Auto-Submitted"
31text kpbulk "bulk"
32text kplist "list"
33text kpjunk "junk"
34text kpno "no"
35text kr1 "R-BULK-LIST"
36text kr2 "R-BULK-PRECEDENCE"
37text kr3 "R-AUTO-SUBMITTED"
38text kr4 "R-AUTO-SENDER"
39text kr5 "R-MY-THREAD"
40text kr6 "R-KNOWN-DIRECT"
41text kr7 "R-KNOWN-CC"
42text kr8 "R-KNOWN"
43text kr9 "R-NEW-DIRECT"
44text kr10 "R-NEW-CC"
45text kr11 "R-OTHER"
46text kc1 "bulk"
47text kc3 "automated"
48text kc5 "reply-in-my-thread"
49text kc6 "known-direct"
50text kc7 "known-cc"
51text kc8 "known-other"
52text kc9 "new-direct"
53text kc10 "new-cc"
54text kc11 "other"
55text kcatk "category"
56text krulek "rule"
57text kfiredk "rules_fired"
58text kfactsk "facts"
59text ksuggk "suggest"
60text klistk "bulk"
61text kauto2 "automated"
62text kknownk "known_sender"
63text ktomek "to_me"
64text kccmek "cc_me"
65text kmythk "in_my_thread"
66text kau1 "noreply"
67text kau2 "no-reply"
68text kau3 "donotreply"
69text kau4 "do-not-reply"
70text kau5 "mailer-daemon"
71text kau6 "postmaster"
72text kau7 "bounce"
73text kau8 "bounces"
74text kau9 "notifications"
75text kau10 "notification"
76text krulesf "/rules"
77text kwif "if "
78text kwcont " contains "
79text kwthen " then label "
80text krulesp "rules:"
81text klabk "label"
82
83fn th_isin to bool
84t = new uchar 8 0
85th_tid t
86for i ulong 8
87if ne get t i get gthr i
88ret th_meets
89end
90end
91ret true
92end
93
94note th_meets: one id of the current message (Message-ID, In-Reply-To, References) is in S_THR
95fn th_meets to bool
96if th_meets1 khmid
97ret true
98end
99if th_meets1 khirt
100ret true
101end
102ret th_meets1 khrefs
103end
104
105fn th_meets1 h view uchar * to bool
106tmp_reset
107k = mi_ids h
108if eq k 0
109ret false
110end
111o = get gv MI_AO
112e = add o get gv MI_AN
113p = o
114while lt p e bound 9223372036854775807
115q = st_eolr S_TMP p e
116if gt q p
117if st_inlist S_THR S_TMP p sub q p
118ret true
119end
120end
121p = add q 1
122end
123ret false
124end
125
126fn st_eolr s ulong p ulong e ulong to ulong
127q = p
128while lt q e bound 9223372036854775807
129if eq s_get s q 10
130ret q
131end
132q = add q 1
133end
134ret e
135end
136
137note th_addids: add the current message ids missing from S_THR; sets TH_CHG when one is new
138fn th_addids h view uchar * to void
139tmp_reset
140k = mi_ids h
141if eq k 0
142ret
143end
144o = get gv MI_AO
145e = add o get gv MI_AN
146p = o
147while lt p e bound 9223372036854775807
148q = st_eolr S_TMP p e
149if gt q p
150if not st_inlist S_THR S_TMP p sub q p
151s_adds S_THR S_TMP p sub q p
152z = s_add S_THR 10
153put gv TH_CHG 1
154end
155end
156p = add q 1
157end
158end
159
160fn th_addall to void
161th_addids khmid
162th_addids khirt
163th_addids khrefs
164end
165
166fn th_visit_collect to void
167if ne get gv MD_WHY 0
168ret
169end
170if ne md_open EOK
171ret
172end
173if ne mip 0
174ret
175end
176if th_isin
177th_addall
178end
179end
180
181fn th_visit_emit to void
182if ne get gv MD_WHY 0
183ret
184end
185if ne md_open EOK
186ret
187end
188if ne mip 0
189ret
190end
191if not th_isin
192ret
193end
194o_ob
195o_ks kopk kthread
196md_o_meta
197md_o_thread
198lb_emit
199o_warn
200o_k kuntrk
201o_ob
202mi_emit_brief
203o_oe
204o_oe
205o_nl
206put gv TH_N add get gv TH_N 1
207end
208
209fn op_thread to int
210r = op_find kthread
211if ne r EOK
212ret r
213end
214c = mip
215if ne c 0
216o_ob
217o_ks kopk kthread
218o_kb kokk false
219mi_o_refusal c
220o_oe
221o_nl
222ret EPARSE
223end
224t = new uchar 8 0
225th_tid t
226for i ulong 8
227put gthr i get t i
228end
229s_reset S_THR
230th_addall
231passes = ulong 0
232passes = ulong 0
233while true bound 9223372036854775807
234put gv TH_CHG 0
235r = md_scan_all 4
236passes = add passes 1
237if eq get gv TH_CHG 0
238break
239end
240end
241put gv TH_N 0
242r = md_scan_all 5
243ret EOK
244end
245
246note ---- address book ----
247note HOME/book: one lowercase address per line; HOME/sentids: one Message-ID per line. Both are
248note rebuilt only from the human's own sent mail: the Maildir folder Sent and HOME/sent/ (what
249note mailhand delivered). Nothing a received mail says can add to them (move refuses Sent).
250
251fn bk_load to void
252if eq get gv BK_LD 1
253ret
254end
255put gv BK_LD 1
256p_reset
257p_h
258p_t kbookf
259r = s_file S_BOOK
260if lt r 0
261s_close S_BOOK
262end
263p_reset
264p_h
265p_t ksidf
266r = s_file S_SID
267if lt r 0
268s_close S_SID
269end
270end
271
272fn bk_known s ulong off ulong n ulong to bool
273bk_load
274if eq scell S_BOOK 0 0
275ret false
276end
277ret st_inlist S_BOOK s off n
278end
279
280fn bk_sentid s ulong off ulong n ulong to bool
281bk_load
282if eq scell S_SID 0 0
283ret false
284end
285ret st_inlist S_SID s off n
286end
287
288note bk_take: add the lines of S_TMP[MI_AO, +MI_AN) missing from list store l (skipping the human's own)
289fn bk_take l ulong skipme bool to void
290o = get gv MI_AO
291e = add o get gv MI_AN
292p = o
293while lt p e bound 9223372036854775807
294q = st_eolr S_TMP p e
295if gt q p
296if bk_want l p sub q p skipme
297s_adds l S_TMP p sub q p
298z = s_add l 10
299end
300end
301p = add q 1
302end
303end
304
305fn bk_want l ulong p ulong n ulong skipme bool to bool
306if skipme
307if me_is S_TMP p n
308ret false
309end
310end
311ret not st_inlist l S_TMP p n
312end
313
314fn bk_from_msg to void
315tmp_reset
316k = mi_addrs khto
317bk_take S_BOOK true
318tmp_reset
319k = mi_addrs khcc
320bk_take S_BOOK true
321tmp_reset
322k = mi_addrs khbcc
323bk_take S_BOOK true
324tmp_reset
325k = mi_ids khmid
326bk_take S_SID false
327end
328
329fn bk_visit to void
330if ne get gv MD_WHY 0
331ret
332end
333if ne md_open EOK
334ret
335end
336if ne mip 0
337ret
338end
339bk_from_msg
340put gv TH_N add get gv TH_N 1
341end
342
343note bk_sentdir: every regular *.eml file of HOME/sent (directory handle 3)
344fn bk_sentdir to void
345p_reset
346p_h
347p_t ksentdir
348if not md_dirok
349ret
350end
351if not d_open 3
352ret
353end
354while d_next 3 bound 9223372036854775807
355if and dn_safe eq get gv VDTY 8
356p_reset
357p_h
358p_t ksentdir
359p_c 47
360p_dn
361if eq s_file S_MSG 0
362if eq mip 0
363bk_from_msg
364put gv TH_N add get gv TH_N 1
365end
366end
367end
368end
369d_close 3
370end
371
372fn op_book to int
373z = s_mem S_BOOK
374z = s_mem S_SID
375put gv BK_LD 1
376put gv TH_N 0
377r0 = ulong 0
378r = md_scan_t 6 ksent
379bk_sentdir
380c = bk_write
381o_ob
382o_ks kopk kbook
383o_kb kokk eq c 0
384o_ku kmsgk get gv TH_N
385o_ku kaddrk st_lines S_BOOK
386o_ku ksidk st_lines S_SID
387o_oe
388o_nl
389if ne c 0
390ret EIO
391end
392ret EOK
393end
394
395fn st_lines s ulong to ulong
396n = ulong 0
397for i ulong s_len s bound 9223372036854775807
398if eq s_get s i 10
399n = add n 1
400end
401end
402ret n
403end
404
405fn bk_write to long
406w_reset
407w_s S_BOOK 0 s_len S_BOOK
408p_reset
409p_h
410p_t kbookf
411r = f_put
412if lt r 0
413ret r
414end
415w_reset
416w_s S_SID 0 s_len S_SID
417p_reset
418p_h
419p_t ksidf
420ret f_put
421end
422
423note ---- triage ----
424note Deterministic rules over headers; the first rule in this order decides the category, and every
425note rule that matched is listed. Rules (ids are stable; README lists them):
426note R-BULK-LIST List-Id or List-Unsubscribe present -> bulk
427note R-BULK-PRECEDENCE Precedence bulk, list or junk -> bulk
428note R-AUTO-SUBMITTED Auto-Submitted present and not "no" -> automated
429note R-AUTO-SENDER From local part is an automated sender name (noreply, no-reply, donotreply,
430note do-not-reply, mailer-daemon, postmaster, bounce, bounces, notifications, notification) -> automated
431note R-MY-THREAD In-Reply-To or References names a message the human sent -> reply-in-my-thread
432note R-KNOWN-DIRECT sender in the book and one of me in To -> known-direct
433note R-KNOWN-CC sender in the book and me only in Cc -> known-cc
434note R-KNOWN sender in the book, me in neither (Bcc or alias) -> known-other
435note R-NEW-DIRECT me in To -> new-direct
436note R-NEW-CC me in Cc -> new-cc
437note R-OTHER none of the above -> other
438note The human's rules file (HOME/rules) adds label suggestions: lines "if HEADER contains TEXT then
439note label LABEL"; each fired line is reported as rule "rules:N" (its line number).
440
441note tr_hdr: header h present; its decoded value in S_TMP at MI_DO/MI_DN
442fn tr_hdr h view uchar * to bool
443tmp_reset
444ret mi_hdec h
445end
446
447fn tr_valis t view uchar * to bool
448o = get gv MI_DO
449n = get gv MI_DN
450s = st_skipws S_TMP o add o n
451e = st_rtrim S_TMP s add o n
452ret s_eqci S_TMP s sub e s t
453end
454
455note tr_facts: gtr[1..11] = 1 for each rule that matched
456fn tr_facts to void
457for i ulong 16
458put gtr i 0
459end
460if or tr_hdr khlid tr_hdr khlun
461put gtr 1 1
462end
463if tr_hdr khprec
464if or or tr_valis kpbulk tr_valis kplist tr_valis kpjunk
465put gtr 2 1
466end
467end
468if tr_hdr khauto
469if not tr_valis kpno
470put gtr 3 1
471end
472end
473if tr_autosender
474put gtr 4 1
475end
476if tr_mythread
477put gtr 5 1
478end
479k = tr_known
480t = tr_meinh khto
481c = tr_meinh khcc
482if and k t
483put gtr 6 1
484end
485if and k and not t c
486put gtr 7 1
487end
488if and k and not t not c
489put gtr 8 1
490end
491if and not k t
492put gtr 9 1
493end
494if and not k and not t c
495put gtr 10 1
496end
497if k
498put gtr 12 1
499end
500if t
501put gtr 13 1
502end
503if c
504put gtr 14 1
505end
506end
507
508fn tr_known to bool
509tmp_reset
510k = mi_addrs khfrom
511if eq k 0
512ret false
513end
514o = get gv MI_AO
515ret bk_known S_TMP o th_firstlen
516end
517
518fn tr_meinh h view uchar * to bool
519tmp_reset
520k = mi_addrs h
521if eq k 0
522ret false
523end
524o = get gv MI_AO
525e = add o get gv MI_AN
526p = o
527while lt p e bound 9223372036854775807
528q = st_eolr S_TMP p e
529if gt q p
530if me_is S_TMP p sub q p
531ret true
532end
533end
534p = add q 1
535end
536ret false
537end
538
539fn tr_mythread to bool
540if tr_myids khirt
541ret true
542end
543ret tr_myids khrefs
544end
545
546fn tr_myids h view uchar * to bool
547tmp_reset
548k = mi_ids h
549if eq k 0
550ret false
551end
552o = get gv MI_AO
553e = add o get gv MI_AN
554p = o
555while lt p e bound 9223372036854775807
556q = st_eolr S_TMP p e
557if gt q p
558if bk_sentid S_TMP p sub q p
559ret true
560end
561end
562p = add q 1
563end
564ret false
565end
566
567
568fn tr_autosender to bool
569tmp_reset
570k = mi_addrs khfrom
571if eq k 0
572ret false
573end
574o = get gv MI_AO
575n = th_firstlen
576atp = n
577for i ulong n bound 9223372036854775807
578if eq s_get S_TMP add o i 64
579atp = i
580end
581end
582if or or s_eqci S_TMP o atp kau1 s_eqci S_TMP o atp kau2 s_eqci S_TMP o atp kau3
583ret true
584end
585if or or s_eqci S_TMP o atp kau4 s_eqci S_TMP o atp kau5 s_eqci S_TMP o atp kau6
586ret true
587end
588if or or s_eqci S_TMP o atp kau7 s_eqci S_TMP o atp kau8 s_eqci S_TMP o atp kau9
589ret true
590end
591ret s_eqci S_TMP o atp kau10
592end
593
594fn tr_rulename i ulong to void
595if eq i 1
596o_vs kr1
597end
598if eq i 2
599o_vs kr2
600end
601if eq i 3
602o_vs kr3
603end
604if eq i 4
605o_vs kr4
606end
607if eq i 5
608o_vs kr5
609end
610if eq i 6
611o_vs kr6
612end
613if eq i 7
614o_vs kr7
615end
616if eq i 8
617o_vs kr8
618end
619if eq i 9
620o_vs kr9
621end
622if eq i 10
623o_vs kr10
624end
625if eq i 11
626o_vs kr11
627end
628end
629
630fn tr_catname i ulong to void
631if le i 2
632o_vs kc1
633ret
634end
635if le i 4
636o_vs kc3
637ret
638end
639if eq i 5
640o_vs kc5
641end
642if eq i 6
643o_vs kc6
644end
645if eq i 7
646o_vs kc7
647end
648if eq i 8
649o_vs kc8
650end
651if eq i 9
652o_vs kc9
653end
654if eq i 10
655o_vs kc10
656end
657if eq i 11
658o_vs kc11
659end
660end
661
662fn tr_emit to void
663first = ulong 11
664for i ulong 10
665if and eq get gtr add i 1 1 eq first 11
666first = add i 1
667end
668end
669if eq first 11
670put gtr 11 1
671end
672o_k kcatk
673tr_catname first
674o_k krulek
675tr_rulename first
676o_k kfiredk
677o_ab
678for i ulong 11
679if eq get gtr add i 1 1
680tr_rulename add i 1
681end
682end
683o_ae
684o_k kfactsk
685o_ob
686o_kb klistk or eq get gtr 1 1 eq get gtr 2 1
687o_kb kauto2 or eq get gtr 3 1 eq get gtr 4 1
688o_kb kknownk eq get gtr 12 1
689o_kb ktomek eq get gtr 13 1
690o_kb kccmek eq get gtr 14 1
691o_kb kmythk eq get gtr 5 1
692o_oe
693tr_rules
694end
695
696note ---- the human's rules file ----
697
698fn tr_rules_load to void
699if eq get gv TR_F 1
700ret
701end
702put gv TR_F 1
703p_reset
704p_h
705p_t krulesf
706r = s_file S_RUL
707if lt r 0
708s_close S_RUL
709end
710end
711
712note tr_rules: "suggest":[{"label":L,"rule":"rules:N"}] for each rules line that matches
713fn tr_rules to void
714tr_rules_load
715o_k ksuggk
716o_ab
717n = s_len S_RUL
718p = ulong 0
719ln = ulong 1
720while lt p n bound 9223372036854775807
721e = st_eol S_RUL p
722w = st_rtrim S_RUL p e
723tr_rule1 p w ln
724p = add e 1
725ln = add ln 1
726end
727o_ae
728end
729
730note tr_rule1: line [p, w) "if HEADER contains TEXT then label LABEL"
731fn tr_rule1 p ulong w ulong ln ulong to void
732if le sub w p len kwif
733ret
734end
735if not s_eqt S_RUL p len kwif kwif
736ret
737end
738hs = add p len kwif
739c = st_findt S_RUL hs w kwcont
740if eq c w
741ret
742end
743ts = add c len kwcont
744t = st_findt S_RUL ts w kwthen
745if eq t w
746ret
747end
748ls = add t len kwthen
749if not tr_hdrr hs sub c hs
750ret
751end
752if not st_find S_TMP get gv MI_DO get gv MI_DN S_RUL ts sub t ts
753ret
754end
755o_ob
756o_kr klabk S_RUL ls sub w ls
757o_k krulek
758o_vbeg
759o_b 34
760o_raw krulesp
761o_digits ln
762o_b 34
763o_oe
764end
765
766note st_findt: first offset in [p, e) of store s where t starts, or e
767fn st_findt s ulong p ulong e ulong t view uchar * to ulong
768if lt sub e p len t
769ret e
770end
771for i ulong add sub sub e p len t 1 bound 9223372036854775807
772if s_eqt s add p i len t t
773ret add p i
774end
775end
776ret e
777end
778
779note tr_hdrr: header named by S_RUL[o, +n) present; decoded value in S_TMP (MI_DO/MI_DN)
780fn tr_hdrr o ulong n ulong to bool
781b = new uchar 256 0
782if gt n 256
783ret false
784end
785k = s_read S_RUL o part b 0 n
786tmp_reset
787ret mi_hdec part b 0 k
788end
789
790fn tri_visit to void
791if not md_openparse ktriage
792ret
793end
794o_ob
795o_ks kopk ktriage
796md_o_meta
797md_o_thread
798tr_facts
799tr_emit
800o_warn
801o_k kuntrk
802o_ob
803mi_emit_brief
804o_oe
805o_oe
806o_nl
807end
808
809note op_triage: --id ID (one message) or --folder F|all (default INBOX); --unread for unread only
810fn op_triage to int
811if not q_get kidarg
812ret md_scan 3
813end
814r = op_find ktriage
815if ne r EOK
816ret r
817end
818c = mip
819if ne c 0
820o_ob
821o_ks kopk ktriage
822o_kb kokk false
823md_o_meta
824mi_o_refusal c
825o_oe
826o_nl
827ret EPARSE
828end
829put gv MD_WHY 0
830o_ob
831o_ks kopk ktriage
832md_o_meta
833md_o_thread
834tr_facts
835tr_emit
836o_warn
837o_k kuntrk
838o_ob
839mi_emit_brief
840o_oe
841o_oe
842o_nl
843ret EOK
844end