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