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
| 1 | note ======== src/imsync.om: "sync": IMAP account -> Maildir and local changes back (ImapSync, is_) ======== |
| 2 | note mailhand sync --account NAME [--folder NAME|all] [--resync] |
| 3 | note Per folder: SELECT, then the folder's state file FOLDERDIR/mailhand-imap (next to its new/ cur/ |
| 4 | note tmp/, like dovecot-uidlist) holds the account, UIDVALIDITY, the last UID seen and one record per |
| 5 | note synced message: "m UID UNIQUE FLAGS" (FLAGS: Maildir letters at the last sync, "-" for none). |
| 6 | note 1. Push back: a record whose file is unchanged costs one lstat. A changed flag set is pushed with |
| 7 | note UID STORE +FLAGS.SILENT / -FLAGS.SILENT. A message found in another local folder is moved on |
| 8 | note the server (UID MOVE; else UID COPY + UID STORE \Deleted + UID EXPUNGE with UIDPLUS; else COPY, |
| 9 | note STORE \Deleted and EXPUNGE, reported as "risk": that EXPUNGE also removes every other message |
| 10 | note already marked \Deleted in the mailbox). The destination folder's state gets "c UIDVALIDITY UID |
| 11 | note UNIQUE FLAGS" from COPYUID, or "p UNIQUE" (adopt by content) when the server sent none. |
| 12 | note A message that is missing locally keeps its record: nothing is deleted on the server. |
| 13 | note 2. Fetch: UID FETCH last+1:* (FLAGS) lists new UIDs; each one not already recorded is fetched |
| 14 | note with BODY.PEEK[] streamed into tmp/, then renamed into new/ (no flags) or cur/ (UNIQUE:2,FLAGS). |
| 15 | note A pending record ("p") whose file has the same SHA-256 adopts the UID instead: no duplicate. |
| 16 | note A changed UIDVALIDITY is refused (exit 3, "uidvalidity_changed", nothing fetched) unless --resync |
| 17 | note is given: then every record becomes pending and is matched by content against the new UIDs, |
| 18 | note so the resync never duplicates a message that is already in the Maildir. |
| 19 | text is_kacct "account" |
| 20 | text is_kfolder "folder" |
| 21 | text is_kresync "resync" |
| 22 | text is_ksync "sync" |
| 23 | text is_kall "all" |
| 24 | text kme2 "m " |
| 25 | text is_kstate "/mailhand-imap" |
| 26 | text is_khead "mailhand-imap 1\n" |
| 27 | text is_kaccount "account " |
| 28 | text is_kuidv "uidvalidity " |
| 29 | text is_klast "last " |
| 30 | text is_klist0 "LIST \"\" \"\"" |
| 31 | text is_klist1 "LIST \"\" \"*\"" |
| 32 | text is_kselect "SELECT " |
| 33 | text is_kfetch1 "UID FETCH " |
| 34 | text is_kfetch2 ":* (FLAGS)" |
| 35 | text is_kfetch3 " (UID FLAGS INTERNALDATE RFC822.SIZE BODY.PEEK[])" |
| 36 | text is_kstore "UID STORE " |
| 37 | text is_kplus " +FLAGS.SILENT (" |
| 38 | text is_kminus " -FLAGS.SILENT (" |
| 39 | text is_kdel " +FLAGS.SILENT (\\Deleted)" |
| 40 | text is_kmove "UID MOVE " |
| 41 | text is_kcopy "UID COPY " |
| 42 | text is_kuexp "UID EXPUNGE " |
| 43 | text is_kexp "EXPUNGE" |
| 44 | text is_klogout "LOGOUT" |
| 45 | text is_kauth " AUTHENTICATE PLAIN" |
| 46 | text is_klogin " LOGIN" |
| 47 | text is_kmail ".mailhand" |
| 48 | text is_kfl2 ":2," |
| 49 | text is_d "sync: " |
| 50 | text is_kfetched "fetched" |
| 51 | text is_kpushed "flags_pushed" |
| 52 | text is_kmoves "moves_pushed" |
| 53 | text is_kadopted "adopted" |
| 54 | text is_kmissing "missing_locally" |
| 55 | text is_krefused "refused_by_server" |
| 56 | text is_kskipped "skipped_mailboxes" |
| 57 | text is_kvalid "uidvalidity" |
| 58 | text is_kold "uidvalidity_was" |
| 59 | text is_krisk "risk" |
| 60 | text is_vrisk "expunge_without_uidplus: EXPUNGE also removed messages already marked \\Deleted" |
| 61 | text is_w1 "connection_failed" |
| 62 | text is_w2 "server_refused_session" |
| 63 | text is_w3 "no_usable_authentication" |
| 64 | text is_w4 "authentication_refused" |
| 65 | text is_w5 "not_tls" |
| 66 | text is_w6 "account_has_no_imap" |
| 67 | text is_w7 "trust_store_failed" |
| 68 | text is_w8 "select_refused" |
| 69 | text is_w9 "uidvalidity_changed" |
| 70 | text is_w10 "state_belongs_to_another_account" |
| 71 | text is_w11 "protocol_error" |
| 72 | text is_w12 "bad_connect_address" |
| 73 | text is_w13 "folder_path_refused" |
| 74 | text is_w14 "state_write_failed" |
| 75 | |
| 76 | note ---- the op ---- |
| 77 | fn op_sync to int |
| 78 | sec = new uchar 262144 0 |
| 79 | if not q_get is_kacct |
| 80 | diag2 kmissing is_kacct |
| 81 | ret EUSAGE |
| 82 | end |
| 83 | for i ulong 48 |
| 84 | put im_v i 0 |
| 85 | end |
| 86 | if q_get is_kresync |
| 87 | put im_v 36 1 |
| 88 | end |
| 89 | k = cr_open sec |
| 90 | if eq k 0 |
| 91 | z = q_get is_kacct |
| 92 | k = cr_find sec S_REQ get gv VQO get gv VQN |
| 93 | end |
| 94 | if eq k 0 |
| 95 | k = is_connect sec |
| 96 | end |
| 97 | cr_wipe sec |
| 98 | if ne k 0 |
| 99 | is_ofail k |
| 100 | tx_close |
| 101 | ret k |
| 102 | end |
| 103 | k = is_folders |
| 104 | is_logout |
| 105 | tx_close |
| 106 | ret k |
| 107 | end |
| 108 | |
| 109 | fn is_ofail k int to void |
| 110 | o_ob |
| 111 | o_ks kopk is_ksync |
| 112 | o_kb kokk false |
| 113 | if ne get cr_v 18 0 |
| 114 | cr_o_why |
| 115 | else |
| 116 | is_owhy |
| 117 | end |
| 118 | o_oe |
| 119 | o_nl |
| 120 | end |
| 121 | |
| 122 | fn is_why w ulong code int to int |
| 123 | put im_v 35 w |
| 124 | ret code |
| 125 | end |
| 126 | |
| 127 | fn is_owhy to void |
| 128 | w = get im_v 35 |
| 129 | if eq w 1 |
| 130 | o_ks krefk is_w1 |
| 131 | end |
| 132 | if eq w 2 |
| 133 | o_ks krefk is_w2 |
| 134 | end |
| 135 | if eq w 3 |
| 136 | o_ks krefk is_w3 |
| 137 | end |
| 138 | if eq w 4 |
| 139 | o_ks krefk is_w4 |
| 140 | end |
| 141 | if eq w 5 |
| 142 | o_ks krefk is_w5 |
| 143 | end |
| 144 | if eq w 6 |
| 145 | o_ks krefk is_w6 |
| 146 | end |
| 147 | if eq w 7 |
| 148 | o_ks krefk is_w7 |
| 149 | end |
| 150 | if eq w 8 |
| 151 | o_ks krefk is_w8 |
| 152 | end |
| 153 | if eq w 9 |
| 154 | o_ks krefk is_w9 |
| 155 | end |
| 156 | if eq w 10 |
| 157 | o_ks krefk is_w10 |
| 158 | end |
| 159 | if eq w 11 |
| 160 | o_ks krefk is_w11 |
| 161 | end |
| 162 | if eq w 12 |
| 163 | o_ks krefk is_w12 |
| 164 | end |
| 165 | if eq w 13 |
| 166 | o_ks krefk is_w13 |
| 167 | end |
| 168 | if eq w 14 |
| 169 | o_ks krefk is_w14 |
| 170 | end |
| 171 | end |
| 172 | |
| 173 | note ---- connection, greeting, authentication ---- |
| 174 | fn is_connect sec edit uchar * to int |
| 175 | h = new uchar 256 0 |
| 176 | via = new uchar 16 0 |
| 177 | if ne get cr_v 4 1 |
| 178 | ret is_why 6 ENOSETUP |
| 179 | end |
| 180 | if gt get cr_v 14 15 |
| 181 | ret is_why 12 ENOSETUP |
| 182 | end |
| 183 | if lt cr_roots sec 0 |
| 184 | ret is_why 7 EIO |
| 185 | end |
| 186 | hn = cr_copy sec get cr_v 1 get cr_v 2 h |
| 187 | vn = cr_copy sec get cr_v 13 get cr_v 14 via |
| 188 | tx_timeout |
| 189 | if ne tx_tls part h 0 hn get cr_v 3 part via 0 vn 0 |
| 190 | tx_diag is_d |
| 191 | ret is_why 1 EIO |
| 192 | end |
| 193 | st = im_resp |
| 194 | if or ne st 6 eq get im_v 2 1 |
| 195 | tx_diag is_d |
| 196 | ret is_why 2 EIO |
| 197 | end |
| 198 | if eq get im_v 1 0 |
| 199 | im_tag |
| 200 | w_t im_kcap |
| 201 | if ne im_run 1 |
| 202 | ret is_why 11 EIO |
| 203 | end |
| 204 | end |
| 205 | ret is_auth sec |
| 206 | end |
| 207 | |
| 208 | note is_auth: AUTHENTICATE PLAIN when offered, else LOGIN unless LOGINDISABLED; over TLS only |
| 209 | fn is_auth sec edit uchar * to int |
| 210 | if ne get tx_v 0 2 |
| 211 | ret is_why 5 EREFUSED |
| 212 | end |
| 213 | c = get im_v 1 |
| 214 | st = ulong 0 |
| 215 | if ne and c 2 0 |
| 216 | st = is_plain sec |
| 217 | else |
| 218 | if ne and c 8 0 |
| 219 | ret is_why 3 EREFUSED |
| 220 | end |
| 221 | st = is_login sec |
| 222 | end |
| 223 | cr_wipe sec |
| 224 | if eq st 0 |
| 225 | tx_diag is_d |
| 226 | ret is_why 1 EIO |
| 227 | end |
| 228 | if ne st 1 |
| 229 | ret is_why 4 EREFUSED |
| 230 | end |
| 231 | put im_v 1 0 |
| 232 | im_tag |
| 233 | w_t im_kcap |
| 234 | if ne im_run 1 |
| 235 | ret is_why 11 EIO |
| 236 | end |
| 237 | ret 0 |
| 238 | end |
| 239 | |
| 240 | note is_tagbuf: "A<n><word>" into pre (a new tag); its length |
| 241 | fn is_tagbuf pre edit uchar 64 word view uchar * to ulong |
| 242 | put im_v 0 add get im_v 0 1 |
| 243 | d = new uchar 24 0 |
| 244 | put pre 0 65 |
| 245 | x = get im_v 0 |
| 246 | k = ulong 0 |
| 247 | while gt x 0 bound 24 |
| 248 | put d k uchar add 48 rem x 10 |
| 249 | x = div x 10 |
| 250 | k = add k 1 |
| 251 | end |
| 252 | for i ulong k bound 24 |
| 253 | put pre add 1 i get d sub sub k i 1 |
| 254 | end |
| 255 | n = add k 1 |
| 256 | for i ulong len word bound 40 |
| 257 | put pre add n i get word i |
| 258 | end |
| 259 | ret add n len word |
| 260 | end |
| 261 | |
| 262 | fn is_plain sec edit uchar * to ulong |
| 263 | pre = new uchar 64 0 |
| 264 | n = is_tagbuf pre is_kauth |
| 265 | if ne and get im_v 1 4 0 |
| 266 | put pre n 32 |
| 267 | if not cr_wplain sec part pre 0 add n 1 |
| 268 | ret 0 |
| 269 | end |
| 270 | ret im_wait |
| 271 | end |
| 272 | put pre n 13 |
| 273 | put pre add n 1 10 |
| 274 | if not tx_write part pre 0 add n 2 |
| 275 | ret 0 |
| 276 | end |
| 277 | st = im_wait |
| 278 | if ne st 4 |
| 279 | ret st |
| 280 | end |
| 281 | if not cr_wplain sec part pre 0 0 |
| 282 | ret 0 |
| 283 | end |
| 284 | ret im_wait |
| 285 | end |
| 286 | |
| 287 | fn is_login sec edit uchar * to ulong |
| 288 | pre = new uchar 64 0 |
| 289 | n = is_tagbuf pre is_klogin |
| 290 | put pre n 32 |
| 291 | if not tx_write part pre 0 add n 1 |
| 292 | ret 0 |
| 293 | end |
| 294 | st = is_astr sec get cr_v 9 get cr_v 10 |
| 295 | if ne st 1 |
| 296 | ret st |
| 297 | end |
| 298 | put pre 0 32 |
| 299 | if not tx_write part pre 0 1 |
| 300 | ret 0 |
| 301 | end |
| 302 | st = is_astr sec get cr_v 11 get cr_v 12 |
| 303 | if ne st 1 |
| 304 | ret st |
| 305 | end |
| 306 | put pre 0 13 |
| 307 | put pre 1 10 |
| 308 | if not tx_write part pre 0 2 |
| 309 | ret 0 |
| 310 | end |
| 311 | ret im_wait |
| 312 | end |
| 313 | |
| 314 | note is_astr: sec[o, +n) as a quoted string, or as a literal (after the server's continuation) |
| 315 | note when it holds a byte a quoted string cannot carry; 1 when sent, else an im_wait status |
| 316 | fn is_astr sec edit uchar * o ulong n ulong to ulong |
| 317 | b = new uchar 4096 0 |
| 318 | q = true |
| 319 | for i ulong n bound 9223372036854775807 |
| 320 | c = get sec add o i |
| 321 | if or lt c 32 gt c 126 |
| 322 | q = false |
| 323 | end |
| 324 | end |
| 325 | if not q |
| 326 | k = is_litw b n |
| 327 | if not tx_write part b 0 k |
| 328 | ret 0 |
| 329 | end |
| 330 | st = im_wait |
| 331 | if ne st 4 |
| 332 | ret st |
| 333 | end |
| 334 | end |
| 335 | k = ulong 0 |
| 336 | if q |
| 337 | put b 0 34 |
| 338 | k = 1 |
| 339 | end |
| 340 | ok = true |
| 341 | for i ulong n bound 9223372036854775807 |
| 342 | c = get sec add o i |
| 343 | if and q or eq c 34 eq c 92 |
| 344 | put b k 92 |
| 345 | k = add k 1 |
| 346 | end |
| 347 | put b k c |
| 348 | k = add k 1 |
| 349 | if ge k 4090 |
| 350 | ok = and ok tx_write part b 0 k |
| 351 | cr_zero b |
| 352 | k = 0 |
| 353 | end |
| 354 | end |
| 355 | if q |
| 356 | put b k 34 |
| 357 | k = add k 1 |
| 358 | end |
| 359 | ok = and ok tx_write part b 0 k |
| 360 | cr_zero b |
| 361 | if ok |
| 362 | ret 1 |
| 363 | end |
| 364 | ret 0 |
| 365 | end |
| 366 | |
| 367 | fn is_litw b edit uchar * n ulong to ulong |
| 368 | d = new uchar 24 0 |
| 369 | x = n |
| 370 | k = ulong 0 |
| 371 | while gt x 0 bound 24 |
| 372 | put d k uchar add 48 rem x 10 |
| 373 | x = div x 10 |
| 374 | k = add k 1 |
| 375 | end |
| 376 | if eq k 0 |
| 377 | put d 0 48 |
| 378 | k = 1 |
| 379 | end |
| 380 | put b 0 123 |
| 381 | for i ulong k bound 24 |
| 382 | put b add 1 i get d sub sub k i 1 |
| 383 | end |
| 384 | put b add k 1 125 |
| 385 | put b add k 2 13 |
| 386 | put b add k 3 10 |
| 387 | ret add k 4 |
| 388 | end |
| 389 | |
| 390 | fn is_logout to void |
| 391 | if ne get tx_v 0 0 |
| 392 | im_tag |
| 393 | w_t is_klogout |
| 394 | st = im_run |
| 395 | end |
| 396 | end |
| 397 | |
| 398 | note ---- folders ---- |
| 399 | fn is_folders to int |
| 400 | put im_v 32 1 |
| 401 | im_tag |
| 402 | w_t is_klist0 |
| 403 | st = im_run |
| 404 | put im_v 32 0 |
| 405 | if eq st 0 |
| 406 | ret is_why 11 EIO |
| 407 | end |
| 408 | z = s_mem S_IFL |
| 409 | if q_get is_kfolder |
| 410 | if s_eqt S_REQ get gv VQO get gv VQN is_kall |
| 411 | put im_v 19 1 |
| 412 | im_tag |
| 413 | w_t is_klist1 |
| 414 | st = im_run |
| 415 | put im_v 19 0 |
| 416 | if ne st 1 |
| 417 | ret is_why 11 EIO |
| 418 | end |
| 419 | else |
| 420 | s_adds S_IFL S_REQ get gv VQO get gv VQN |
| 421 | z = s_add S_IFL 10 |
| 422 | end |
| 423 | else |
| 424 | s_addt S_IFL kinbox |
| 425 | z = s_add S_IFL 10 |
| 426 | end |
| 427 | status = EOK |
| 428 | p = ulong 0 |
| 429 | n = s_len S_IFL |
| 430 | while lt p n bound 9223372036854775807 |
| 431 | q = p |
| 432 | while lt q n bound 9223372036854775807 |
| 433 | if eq s_get S_IFL q 10 |
| 434 | break |
| 435 | end |
| 436 | q = add q 1 |
| 437 | end |
| 438 | r = is_folder p sub q p |
| 439 | if ne r EOK |
| 440 | status = r |
| 441 | end |
| 442 | if eq r EIO |
| 443 | ret r |
| 444 | end |
| 445 | p = add q 1 |
| 446 | end |
| 447 | ret status |
| 448 | end |
| 449 | |
| 450 | note im_pfolder: gp = the directory of the folder being synced (im_fn) |
| 451 | fn im_pfolder to void |
| 452 | p_reset |
| 453 | p_r |
| 454 | n = get im_v 27 |
| 455 | if is_isinbox |
| 456 | ret |
| 457 | end |
| 458 | p_c 47 |
| 459 | p_c 46 |
| 460 | for i ulong n bound 256 |
| 461 | p_c get im_fn i |
| 462 | end |
| 463 | end |
| 464 | |
| 465 | fn is_isinbox to bool |
| 466 | n = get im_v 27 |
| 467 | if ne n len kinbox |
| 468 | ret false |
| 469 | end |
| 470 | for i ulong n bound 8 |
| 471 | if ne get im_fn i get kinbox i |
| 472 | ret false |
| 473 | end |
| 474 | end |
| 475 | ret true |
| 476 | end |
| 477 | |
| 478 | note is_mkfolder: the folder and its new/ cur/ tmp/ exist as real directories (created 0700) |
| 479 | fn is_mkfolder to bool |
| 480 | for t ulong 4 |
| 481 | im_pfolder |
| 482 | if eq t 1 |
| 483 | p_t ksnew |
| 484 | end |
| 485 | if eq t 2 |
| 486 | p_t kscur |
| 487 | end |
| 488 | if eq t 3 |
| 489 | p_t kstmp |
| 490 | end |
| 491 | k = p_kind |
| 492 | if eq k 0 |
| 493 | zl = p_mkdir |
| 494 | k = p_kind |
| 495 | end |
| 496 | if ne k 1 |
| 497 | ret false |
| 498 | end |
| 499 | end |
| 500 | ret true |
| 501 | end |
| 502 | |
| 503 | fn is_folder o ulong n ulong to int |
| 504 | dl = get im_v 18 |
| 505 | for i ulong 18 |
| 506 | put im_v add 12 i 0 |
| 507 | end |
| 508 | put im_v 3 0 |
| 509 | put im_v 35 0 |
| 510 | put im_v 18 dl |
| 511 | put im_v 31 0 |
| 512 | if or eq n 0 gt n 255 |
| 513 | ret EPATH |
| 514 | end |
| 515 | if not s_safe S_IFL o n |
| 516 | if not s_eqt S_IFL o n kinbox |
| 517 | ret EPATH |
| 518 | end |
| 519 | end |
| 520 | for i ulong n bound 256 |
| 521 | put im_fn i s_get S_IFL add o i |
| 522 | end |
| 523 | put im_v 27 n |
| 524 | if not is_mkfolder |
| 525 | ret is_orec is_why 13 EPATH |
| 526 | end |
| 527 | im_tag |
| 528 | w_t is_kselect |
| 529 | im_wremote S_IFL o n |
| 530 | st = im_run |
| 531 | if eq st 0 |
| 532 | ret is_orec is_io |
| 533 | end |
| 534 | if ne st 1 |
| 535 | ret is_orec is_why 8 EREFUSED |
| 536 | end |
| 537 | if eq get im_v 3 0 |
| 538 | ret is_orec is_io |
| 539 | end |
| 540 | k = is_sload |
| 541 | if ne k EOK |
| 542 | ret is_orec k |
| 543 | end |
| 544 | if not is_push |
| 545 | ret is_orec is_io |
| 546 | end |
| 547 | k = is_fetchnew |
| 548 | if eq k EIO |
| 549 | ret is_orec is_io |
| 550 | end |
| 551 | if ne is_ssave 0 |
| 552 | ret is_orec is_why 14 EIO |
| 553 | end |
| 554 | ret is_orec k |
| 555 | end |
| 556 | |
| 557 | note is_orec: the folder's JSON record; returns k |
| 558 | fn is_orec k int to int |
| 559 | if and or eq get im_v 35 1 eq get im_v 35 11 ne tx_why 0 |
| 560 | tx_diag is_d |
| 561 | end |
| 562 | b = new uchar 256 0 |
| 563 | n = get im_v 27 |
| 564 | for i ulong n bound 256 |
| 565 | put b i get im_fn i |
| 566 | end |
| 567 | o_ob |
| 568 | o_ks kopk is_ksync |
| 569 | o_ks kfolderk part b 0 n |
| 570 | o_kb kokk eq k EOK |
| 571 | if ne get im_v 35 0 |
| 572 | is_owhy |
| 573 | end |
| 574 | o_ku is_kvalid get im_v 3 |
| 575 | if and ne get im_v 31 0 ne get im_v 31 get im_v 3 |
| 576 | o_ku is_kold get im_v 31 |
| 577 | end |
| 578 | o_ku is_kfetched get im_v 13 |
| 579 | o_ku is_kpushed get im_v 14 |
| 580 | o_ku is_kmoves get im_v 15 |
| 581 | o_ku is_kadopted get im_v 16 |
| 582 | o_ku is_kmissing get im_v 25 |
| 583 | o_ku is_krefused get im_v 17 |
| 584 | o_ku is_kskipped get im_v 29 |
| 585 | if eq get im_v 26 1 |
| 586 | o_ks is_krisk is_vrisk |
| 587 | end |
| 588 | o_oe |
| 589 | o_nl |
| 590 | ret k |
| 591 | end |
| 592 | |
| 593 | note ---- the state file ---- |
| 594 | fn is_pstate to void |
| 595 | im_pfolder |
| 596 | p_t is_kstate |
| 597 | end |
| 598 | |
| 599 | note is_sload: read the state into S_IST and check account and UIDVALIDITY; EOK or a refusal |
| 600 | fn is_sload to int |
| 601 | put im_v 31 0 |
| 602 | put im_v 28 0 |
| 603 | put im_v 24 0 |
| 604 | is_pstate |
| 605 | r = s_file S_IST |
| 606 | if eq r -2 |
| 607 | z = s_mem S_IST |
| 608 | r = 0 |
| 609 | end |
| 610 | if ne r 0 |
| 611 | ret is_why 13 EPATH |
| 612 | end |
| 613 | n = s_len S_IST |
| 614 | p = ulong 0 |
| 615 | while lt p n bound 9223372036854775807 |
| 616 | e = st_eol S_IST p |
| 617 | if not is_headline p e |
| 618 | ret is_why 10 EREFUSED |
| 619 | end |
| 620 | p = add e 1 |
| 621 | end |
| 622 | v = get im_v 31 |
| 623 | if or eq v 0 eq v get im_v 3 |
| 624 | ret EOK |
| 625 | end |
| 626 | if ne get im_v 36 1 |
| 627 | ret is_why 9 EREFUSED |
| 628 | end |
| 629 | put im_v 24 1 |
| 630 | put im_v 28 0 |
| 631 | ret EOK |
| 632 | end |
| 633 | |
| 634 | note is_headline: account, uidvalidity and last lines; false when the account differs |
| 635 | fn is_headline p ulong e ulong to bool |
| 636 | if is_lkey p e is_kuidv |
| 637 | put im_v 31 is_lnum add p len is_kuidv e |
| 638 | ret true |
| 639 | end |
| 640 | if is_lkey p e is_klast |
| 641 | put im_v 28 is_lnum add p len is_klast e |
| 642 | ret true |
| 643 | end |
| 644 | if is_lkey p e is_kaccount |
| 645 | o = add p len is_kaccount |
| 646 | ret s_eqss S_IST o sub e o S_REQ get cr_v 21 get cr_v 22 |
| 647 | end |
| 648 | ret true |
| 649 | end |
| 650 | |
| 651 | fn is_lkey p ulong e ulong t view uchar * to bool |
| 652 | if lt sub e p len t |
| 653 | ret false |
| 654 | end |
| 655 | ret s_eqt S_IST p len t t |
| 656 | end |
| 657 | |
| 658 | fn is_lnum p ulong e ulong to ulong |
| 659 | v = ulong 0 |
| 660 | q = p |
| 661 | while lt q e bound 20 |
| 662 | c = s_get S_IST q |
| 663 | if or lt c 48 gt c 57 |
| 664 | ret v |
| 665 | end |
| 666 | v = add mul v 10 ulong sub c 48 |
| 667 | q = add q 1 |
| 668 | end |
| 669 | ret v |
| 670 | end |
| 671 | |
| 672 | fn is_ssave to long |
| 673 | w_reset |
| 674 | w_t is_khead |
| 675 | w_t is_kaccount |
| 676 | w_s S_REQ get cr_v 21 get cr_v 22 |
| 677 | w_b 10 |
| 678 | w_t is_kuidv |
| 679 | w_u get im_v 3 |
| 680 | w_b 10 |
| 681 | w_t is_klast |
| 682 | w_u get im_v 28 |
| 683 | w_b 10 |
| 684 | w_s S_Y 0 s_len S_Y |
| 685 | n = s_len S_IPD |
| 686 | p = ulong 0 |
| 687 | while lt p n bound 9223372036854775807 |
| 688 | e = st_eol S_IPD p |
| 689 | w_b 112 |
| 690 | w_b 32 |
| 691 | w_s S_IPD add p 67 sub e add p 67 |
| 692 | w_b 10 |
| 693 | p = add e 1 |
| 694 | end |
| 695 | is_pstate |
| 696 | ret f_put |
| 697 | end |
| 698 | |
| 699 | note ---- push back ---- |
| 700 | note is_push: every record of S_IST: m (and c of the current UIDVALIDITY) are checked and kept in |
| 701 | note the new state S_Y; p (and all records during a resync) become pending entries in S_IPD |
| 702 | fn is_push to bool |
| 703 | z = s_mem S_Y |
| 704 | z = s_mem S_IPD |
| 705 | n = s_len S_IST |
| 706 | p = ulong 0 |
| 707 | while lt p n bound 9223372036854775807 |
| 708 | e = st_eol S_IST p |
| 709 | if gt sub e p 2 |
| 710 | if not is_rec p e |
| 711 | ret false |
| 712 | end |
| 713 | end |
| 714 | p = add e 1 |
| 715 | end |
| 716 | ret true |
| 717 | end |
| 718 | |
| 719 | note is_rec: one record line [p, e); false on an I/O failure |
| 720 | fn is_rec p ulong e ulong to bool |
| 721 | c = s_get S_IST p |
| 722 | if ne s_get S_IST add p 1 32 |
| 723 | ret true |
| 724 | end |
| 725 | q = add p 2 |
| 726 | valid = and eq c 109 ne get im_v 24 1 |
| 727 | if eq c 99 |
| 728 | v = is_lnum q e |
| 729 | q = is_skipf q e |
| 730 | valid = and eq v get im_v 3 ne get im_v 24 1 |
| 731 | end |
| 732 | if and ne c 109 and ne c 99 ne c 112 |
| 733 | ret true |
| 734 | end |
| 735 | if eq c 112 |
| 736 | put im_v 40 q |
| 737 | put im_v 41 sub e q |
| 738 | put im_v 42 0 |
| 739 | is_pend |
| 740 | ret true |
| 741 | end |
| 742 | uid = is_lnum q e |
| 743 | q = is_skipf q e |
| 744 | u = q |
| 745 | q = is_skipf q e |
| 746 | put im_v 40 u |
| 747 | put im_v 41 sub sub q u 1 |
| 748 | put im_v 42 is_mask S_IST q sub e q |
| 749 | if not valid |
| 750 | is_pend |
| 751 | ret true |
| 752 | end |
| 753 | ret is_one uid |
| 754 | end |
| 755 | |
| 756 | note is_skipf: the position after the next space from q (or e) |
| 757 | fn is_skipf q ulong e ulong to ulong |
| 758 | r = q |
| 759 | while lt r e bound 9223372036854775807 |
| 760 | if eq s_get S_IST r 32 |
| 761 | ret add r 1 |
| 762 | end |
| 763 | r = add r 1 |
| 764 | end |
| 765 | ret e |
| 766 | end |
| 767 | |
| 768 | note is_mask: Maildir flag letters (or "-") as bits D 1, F 2, R 4, S 8, T 16 |
| 769 | fn is_mask s ulong o ulong n ulong to ulong |
| 770 | m = ulong 0 |
| 771 | for i ulong n bound 256 |
| 772 | c = s_get s add o i |
| 773 | for j ulong 5 |
| 774 | if eq c get im_letters j |
| 775 | m = or m shl ulong 1 j |
| 776 | end |
| 777 | end |
| 778 | end |
| 779 | ret m |
| 780 | end |
| 781 | |
| 782 | note is_wletters: the letters of mask into S_W ("-" when none, when dash) |
| 783 | fn is_wletters m ulong dash bool to void |
| 784 | if and eq m 0 dash |
| 785 | w_b 45 |
| 786 | ret |
| 787 | end |
| 788 | for j ulong 5 |
| 789 | if ne and m shl ulong 1 j 0 |
| 790 | w_b get im_letters j |
| 791 | end |
| 792 | end |
| 793 | end |
| 794 | |
| 795 | note is_idtmp: S_TMP = "FOLDER/UNIQUE" for the current folder and the record's unique (im_v 40/41) |
| 796 | fn is_idtmp to void |
| 797 | s_reset S_TMP |
| 798 | for i ulong get im_v 27 bound 256 |
| 799 | z = s_add S_TMP get im_fn i |
| 800 | end |
| 801 | z = s_add S_TMP 47 |
| 802 | s_adds S_TMP S_IST get im_v 40 get im_v 41 |
| 803 | end |
| 804 | |
| 805 | note is_fast: the file is where the record says (new/UNIQUE or cur/UNIQUE:2,FLAGS) |
| 806 | fn is_fast to bool |
| 807 | im_pfolder |
| 808 | m = get im_v 42 |
| 809 | if eq m 0 |
| 810 | p_t ksnew |
| 811 | p_c 47 |
| 812 | p_s S_IST get im_v 40 get im_v 41 |
| 813 | if eq p_kind 2 |
| 814 | ret true |
| 815 | end |
| 816 | im_pfolder |
| 817 | end |
| 818 | p_t kscur |
| 819 | p_c 47 |
| 820 | p_s S_IST get im_v 40 get im_v 41 |
| 821 | p_t is_kfl2 |
| 822 | for j ulong 5 |
| 823 | if ne and m shl ulong 1 j 0 |
| 824 | p_c get im_letters j |
| 825 | end |
| 826 | end |
| 827 | ret eq p_kind 2 |
| 828 | end |
| 829 | |
| 830 | fn is_emit uid ulong m ulong to void |
| 831 | s_addt S_Y kme2 |
| 832 | s_addu S_Y uid |
| 833 | z = s_add S_Y 32 |
| 834 | s_adds S_Y S_IST get im_v 40 get im_v 41 |
| 835 | z = s_add S_Y 32 |
| 836 | if eq m 0 |
| 837 | z = s_add S_Y 45 |
| 838 | end |
| 839 | for j ulong 5 |
| 840 | if ne and m shl ulong 1 j 0 |
| 841 | z = s_add S_Y get im_letters j |
| 842 | end |
| 843 | end |
| 844 | z = s_add S_Y 10 |
| 845 | end |
| 846 | |
| 847 | note is_one: one valid record (UID uid, unique im_v 40/41, recorded flags im_v 42) |
| 848 | fn is_one uid ulong to bool |
| 849 | if is_fast |
| 850 | is_emit uid get im_v 42 |
| 851 | ret true |
| 852 | end |
| 853 | is_idtmp |
| 854 | k = md_find_id S_TMP 0 s_len S_TMP |
| 855 | s_close S_MSG |
| 856 | if eq k EOK |
| 857 | fl = md_flags |
| 858 | lm = is_mask S_X get gv MD_FLO fl |
| 859 | ret is_flagsync uid lm |
| 860 | end |
| 861 | if is_findmoved |
| 862 | ret is_move uid |
| 863 | end |
| 864 | put im_v 25 add get im_v 25 1 |
| 865 | is_emit uid get im_v 42 |
| 866 | ret true |
| 867 | end |
| 868 | |
| 869 | fn is_flagsync uid ulong lm ulong to bool |
| 870 | rm = get im_v 42 |
| 871 | if eq lm rm |
| 872 | is_emit uid rm |
| 873 | ret true |
| 874 | end |
| 875 | add1 = and lm xor rm 31 |
| 876 | del1 = and rm xor lm 31 |
| 877 | ok = true |
| 878 | if ne add1 0 |
| 879 | st = is_store uid add1 is_kplus |
| 880 | if eq st 0 |
| 881 | ret false |
| 882 | end |
| 883 | ok = eq st 1 |
| 884 | end |
| 885 | if and ok ne del1 0 |
| 886 | st = is_store uid del1 is_kminus |
| 887 | if eq st 0 |
| 888 | ret false |
| 889 | end |
| 890 | ok = eq st 1 |
| 891 | end |
| 892 | if ok |
| 893 | put im_v 14 add get im_v 14 1 |
| 894 | is_emit uid lm |
| 895 | else |
| 896 | is_emit uid rm |
| 897 | end |
| 898 | ret true |
| 899 | end |
| 900 | |
| 901 | fn is_store uid ulong m ulong how view uchar * to ulong |
| 902 | im_tag |
| 903 | w_t is_kstore |
| 904 | w_u uid |
| 905 | w_t how |
| 906 | first = true |
| 907 | for j ulong 5 |
| 908 | if ne and m shl ulong 1 j 0 |
| 909 | if not first |
| 910 | w_b 32 |
| 911 | end |
| 912 | first = false |
| 913 | is_wflag j |
| 914 | end |
| 915 | end |
| 916 | w_b 41 |
| 917 | ret im_run |
| 918 | end |
| 919 | |
| 920 | fn is_wflag j ulong to void |
| 921 | if eq j 0 |
| 922 | w_t im_fd |
| 923 | end |
| 924 | if eq j 1 |
| 925 | w_t im_ff |
| 926 | end |
| 927 | if eq j 2 |
| 928 | w_t im_fa |
| 929 | end |
| 930 | if eq j 3 |
| 931 | w_t im_fs |
| 932 | end |
| 933 | if eq j 4 |
| 934 | w_t im_ft |
| 935 | end |
| 936 | end |
| 937 | |
| 938 | note is_findmoved: the record's unique in another local folder (INBOX or a Maildir++ folder); |
| 939 | note on success S_X describes it (md_setid / md_find_id) and S_AUX holds that folder's name |
| 940 | fn is_findmoved to bool |
| 941 | if not is_isinbox |
| 942 | s_reset S_TMP |
| 943 | s_addt S_TMP kinbox |
| 944 | if is_tryin |
| 945 | ret true |
| 946 | end |
| 947 | end |
| 948 | p_reset |
| 949 | p_r |
| 950 | if not d_open 3 |
| 951 | ret false |
| 952 | end |
| 953 | while d_next 3 bound 9223372036854775807 |
| 954 | if md_isfolder |
| 955 | s_reset S_TMP |
| 956 | o = get gv VDNO |
| 957 | for i ulong sub get gv VDNL 1 bound 256 |
| 958 | z = s_add S_TMP get gde add add o 1 i |
| 959 | end |
| 960 | if not is_samefolder |
| 961 | if is_tryin |
| 962 | d_close 3 |
| 963 | ret true |
| 964 | end |
| 965 | end |
| 966 | end |
| 967 | end |
| 968 | d_close 3 |
| 969 | ret false |
| 970 | end |
| 971 | |
| 972 | fn is_samefolder to bool |
| 973 | n = s_len S_TMP |
| 974 | if ne n get im_v 27 |
| 975 | ret false |
| 976 | end |
| 977 | for i ulong n bound 256 |
| 978 | if ne s_get S_TMP i get im_fn i |
| 979 | ret false |
| 980 | end |
| 981 | end |
| 982 | ret true |
| 983 | end |
| 984 | |
| 985 | note is_tryin: S_TMP holds a folder name; look for the record's unique there |
| 986 | fn is_tryin to bool |
| 987 | z = s_mem S_AUX |
| 988 | s_adds S_AUX S_TMP 0 s_len S_TMP |
| 989 | z = s_add S_TMP 47 |
| 990 | s_adds S_TMP S_IST get im_v 40 get im_v 41 |
| 991 | k = md_find_id S_TMP 0 s_len S_TMP |
| 992 | s_close S_MSG |
| 993 | ret eq k EOK |
| 994 | end |
| 995 | |
| 996 | note is_move: the server-side move of uid to the folder in S_AUX (flags from S_X) |
| 997 | fn is_move uid ulong to bool |
| 998 | fl = md_flags |
| 999 | lm = is_mask S_X get gv MD_FLO fl |
| 1000 | put im_v 12 0 |
| 1001 | put im_v 20 0 |
| 1002 | c = get im_v 1 |
| 1003 | st = ulong 0 |
| 1004 | if ne and c 16 0 |
| 1005 | im_tag |
| 1006 | w_t is_kmove |
| 1007 | w_u uid |
| 1008 | w_b 32 |
| 1009 | im_wremote S_AUX 0 s_len S_AUX |
| 1010 | st = im_run |
| 1011 | else |
| 1012 | st = is_copydel uid |
| 1013 | end |
| 1014 | if eq st 0 |
| 1015 | ret false |
| 1016 | end |
| 1017 | if ne st 1 |
| 1018 | is_emit uid get im_v 42 |
| 1019 | ret true |
| 1020 | end |
| 1021 | put im_v 15 add get im_v 15 1 |
| 1022 | is_destrec lm |
| 1023 | ret true |
| 1024 | end |
| 1025 | |
| 1026 | note is_copydel: UID COPY, UID STORE +\Deleted, then UID EXPUNGE (UIDPLUS) or EXPUNGE (risk) |
| 1027 | fn is_copydel uid ulong to ulong |
| 1028 | im_tag |
| 1029 | w_t is_kcopy |
| 1030 | w_u uid |
| 1031 | w_b 32 |
| 1032 | im_wremote S_AUX 0 s_len S_AUX |
| 1033 | st = im_run |
| 1034 | if ne st 1 |
| 1035 | ret st |
| 1036 | end |
| 1037 | im_tag |
| 1038 | w_t is_kstore |
| 1039 | w_u uid |
| 1040 | w_t is_kdel |
| 1041 | st = im_run |
| 1042 | if ne st 1 |
| 1043 | ret st |
| 1044 | end |
| 1045 | im_tag |
| 1046 | if ne and get im_v 1 32 0 |
| 1047 | w_t is_kuexp |
| 1048 | w_u uid |
| 1049 | else |
| 1050 | w_t is_kexp |
| 1051 | put im_v 26 1 |
| 1052 | end |
| 1053 | ret im_run |
| 1054 | end |
| 1055 | |
| 1056 | note is_destrec: append the moved message's record to the destination folder's state file |
| 1057 | fn is_destrec lm ulong to void |
| 1058 | w_reset |
| 1059 | if gt get im_v 12 0 |
| 1060 | w_b 99 |
| 1061 | w_b 32 |
| 1062 | w_u get im_v 20 |
| 1063 | w_b 32 |
| 1064 | w_u get im_v 12 |
| 1065 | w_b 32 |
| 1066 | w_s S_IST get im_v 40 get im_v 41 |
| 1067 | w_b 32 |
| 1068 | is_wletters lm true |
| 1069 | else |
| 1070 | w_b 112 |
| 1071 | w_b 32 |
| 1072 | w_s S_IST get im_v 40 get im_v 41 |
| 1073 | end |
| 1074 | w_b 10 |
| 1075 | p_reset |
| 1076 | p_r |
| 1077 | if not s_eqt S_AUX 0 s_len S_AUX kinbox |
| 1078 | p_c 47 |
| 1079 | p_c 46 |
| 1080 | p_s S_AUX 0 s_len S_AUX |
| 1081 | end |
| 1082 | p_t is_kstate |
| 1083 | zl = f_app |
| 1084 | end |
| 1085 | |
| 1086 | note is_pend: a record to adopt by content: "SHA256HEX M UNIQUE" in S_IPD when the file is here |
| 1087 | fn is_pend to void |
| 1088 | d = new uchar 32 0 |
| 1089 | is_idtmp |
| 1090 | k = md_find_id S_TMP 0 s_len S_TMP |
| 1091 | if ne k EOK |
| 1092 | s_close S_MSG |
| 1093 | put im_v 25 add get im_v 25 1 |
| 1094 | ret |
| 1095 | end |
| 1096 | fl = md_flags |
| 1097 | lm = is_mask S_X get gv MD_FLO fl |
| 1098 | sha_s S_MSG 0 s_len S_MSG d |
| 1099 | s_close S_MSG |
| 1100 | s_addhex S_IPD d |
| 1101 | z = s_add S_IPD 32 |
| 1102 | z = s_add S_IPD uchar add 65 lm |
| 1103 | z = s_add S_IPD 32 |
| 1104 | s_adds S_IPD S_IST get im_v 40 get im_v 41 |
| 1105 | z = s_add S_IPD 10 |
| 1106 | end |
| 1107 | |
| 1108 | note ---- fetching new mail ---- |
| 1109 | fn is_fetchnew to int |
| 1110 | z = s_mem S_ILS |
| 1111 | put im_v 19 2 |
| 1112 | im_tag |
| 1113 | w_t is_kfetch1 |
| 1114 | w_u add get im_v 28 1 |
| 1115 | w_t is_kfetch2 |
| 1116 | st = im_run |
| 1117 | put im_v 19 0 |
| 1118 | if eq st 0 |
| 1119 | ret EIO |
| 1120 | end |
| 1121 | if ne st 1 |
| 1122 | ret EREFUSED |
| 1123 | end |
| 1124 | last = get im_v 28 |
| 1125 | hole = ulong 0 |
| 1126 | n = s_len S_ILS |
| 1127 | p = ulong 0 |
| 1128 | while lt p n bound 9223372036854775807 |
| 1129 | e = st_eol S_ILS p |
| 1130 | uid = is_snum S_ILS p e |
| 1131 | p = add e 1 |
| 1132 | if le uid get im_v 28 |
| 1133 | continue |
| 1134 | end |
| 1135 | if is_known uid |
| 1136 | if gt uid last |
| 1137 | last = uid |
| 1138 | end |
| 1139 | continue |
| 1140 | end |
| 1141 | r = is_fetch1 uid |
| 1142 | if eq r 0 |
| 1143 | ret EIO |
| 1144 | end |
| 1145 | if ne r 1 |
| 1146 | hole = uid |
| 1147 | break |
| 1148 | end |
| 1149 | if gt uid last |
| 1150 | last = uid |
| 1151 | end |
| 1152 | end |
| 1153 | if and gt hole 0 ge last hole |
| 1154 | last = sub hole 1 |
| 1155 | end |
| 1156 | put im_v 28 last |
| 1157 | if gt hole 0 |
| 1158 | ret EREFUSED |
| 1159 | end |
| 1160 | ret EOK |
| 1161 | end |
| 1162 | |
| 1163 | fn is_snum s ulong p ulong e ulong to ulong |
| 1164 | v = ulong 0 |
| 1165 | for i ulong sub e p bound 20 |
| 1166 | c = s_get s add p i |
| 1167 | if or lt c 48 gt c 57 |
| 1168 | ret v |
| 1169 | end |
| 1170 | v = add mul v 10 ulong sub c 48 |
| 1171 | end |
| 1172 | ret v |
| 1173 | end |
| 1174 | |
| 1175 | note is_known: the new state S_Y already has a record for uid |
| 1176 | fn is_known uid ulong to bool |
| 1177 | n = s_len S_Y |
| 1178 | p = ulong 0 |
| 1179 | while lt p n bound 9223372036854775807 |
| 1180 | e = st_eol S_Y p |
| 1181 | if eq is_snum S_Y add p 2 e uid |
| 1182 | ret true |
| 1183 | end |
| 1184 | p = add e 1 |
| 1185 | end |
| 1186 | ret false |
| 1187 | end |
| 1188 | |
| 1189 | note is_fetch1: fetch one message; 1 delivered (or adopted), 2 refused, 0 I/O failure |
| 1190 | fn is_fetch1 uid ulong to ulong |
| 1191 | put im_v 19 3 |
| 1192 | put im_v 43 uid |
| 1193 | put im_v 44 0 |
| 1194 | im_tag |
| 1195 | w_t is_kfetch1 |
| 1196 | w_u uid |
| 1197 | w_t is_kfetch3 |
| 1198 | st = im_run |
| 1199 | put im_v 19 0 |
| 1200 | im_tmpdrop |
| 1201 | if eq st 0 |
| 1202 | ret 0 |
| 1203 | end |
| 1204 | if or ne st 1 eq get im_v 44 0 |
| 1205 | ret 2 |
| 1206 | end |
| 1207 | ret 1 |
| 1208 | end |
| 1209 | |
| 1210 | note im_fetched: a FETCH record with a streamed body for the requested UID: adopt or deliver |
| 1211 | fn im_fetched to void |
| 1212 | if or eq get im_v 10 0 ne get im_v 7 get im_v 43 |
| 1213 | ret |
| 1214 | end |
| 1215 | if not im_tmpclose |
| 1216 | ret |
| 1217 | end |
| 1218 | if gt s_len S_IPD 0 |
| 1219 | if is_adopt |
| 1220 | ret |
| 1221 | end |
| 1222 | end |
| 1223 | is_unique |
| 1224 | m = get im_v 8 |
| 1225 | im_ptmp |
| 1226 | p_save |
| 1227 | im_pfolder |
| 1228 | if eq m 0 |
| 1229 | p_t ksnew |
| 1230 | else |
| 1231 | p_t kscur |
| 1232 | end |
| 1233 | p_c 47 |
| 1234 | for i ulong get im_v 30 bound 128 |
| 1235 | p_c get im_un i |
| 1236 | end |
| 1237 | if ne m 0 |
| 1238 | p_t is_kfl2 |
| 1239 | for j ulong 5 |
| 1240 | if ne and m shl ulong 1 j 0 |
| 1241 | p_c get im_letters j |
| 1242 | end |
| 1243 | end |
| 1244 | end |
| 1245 | if lt p_ren2 1 1 0 |
| 1246 | ret |
| 1247 | end |
| 1248 | put im_v 23 0 |
| 1249 | put im_v 44 1 |
| 1250 | put im_v 13 add get im_v 13 1 |
| 1251 | z = s_mem S_AUX |
| 1252 | for i ulong get im_v 30 bound 128 |
| 1253 | z = s_add S_AUX get im_un i |
| 1254 | end |
| 1255 | s_addt S_Y kme2 |
| 1256 | s_addu S_Y get im_v 7 |
| 1257 | z = s_add S_Y 32 |
| 1258 | s_adds S_Y S_AUX 0 s_len S_AUX |
| 1259 | z = s_add S_Y 32 |
| 1260 | is_yletters m |
| 1261 | end |
| 1262 | |
| 1263 | fn is_yletters m ulong to void |
| 1264 | if eq m 0 |
| 1265 | z = s_add S_Y 45 |
| 1266 | end |
| 1267 | for j ulong 5 |
| 1268 | if ne and m shl ulong 1 j 0 |
| 1269 | z = s_add S_Y get im_letters j |
| 1270 | end |
| 1271 | end |
| 1272 | z = s_add S_Y 10 |
| 1273 | end |
| 1274 | |
| 1275 | note is_unique: im_un = "<INTERNALDATE epoch>.V<UIDVALIDITY>U<UID>F<SHA-256 of the local folder name, |
| 1276 | note first 8 hex>.mailhand": the same message always gets the same name in a folder, so a lost state |
| 1277 | note file cannot duplicate it (the rename into new/ or cur/ refuses to replace a file) |
| 1278 | fn is_unique to void |
| 1279 | f = new uchar 256 0 |
| 1280 | d = new uchar 32 0 |
| 1281 | put im_v 30 0 |
| 1282 | t = get im_v 9 |
| 1283 | if lt t 100000000 |
| 1284 | t = now_s |
| 1285 | end |
| 1286 | is_unum t |
| 1287 | is_unc 46 |
| 1288 | is_unc 86 |
| 1289 | is_unum get im_v 3 |
| 1290 | is_unc 85 |
| 1291 | is_unum get im_v 7 |
| 1292 | is_unc 70 |
| 1293 | n = get im_v 27 |
| 1294 | for i ulong n bound 256 |
| 1295 | put f i get im_fn i |
| 1296 | end |
| 1297 | sha_v part f 0 n d |
| 1298 | for i ulong 4 |
| 1299 | is_unc get khex ulong shr get d i 4 |
| 1300 | is_unc get khex ulong and get d i 15 |
| 1301 | end |
| 1302 | for i ulong len is_kmail |
| 1303 | is_unc get is_kmail i |
| 1304 | end |
| 1305 | end |
| 1306 | |
| 1307 | fn is_unc c uchar to void |
| 1308 | n = get im_v 30 |
| 1309 | if lt n 128 |
| 1310 | put im_un n c |
| 1311 | put im_v 30 add n 1 |
| 1312 | end |
| 1313 | end |
| 1314 | |
| 1315 | fn is_unum v ulong to void |
| 1316 | d = new uchar 24 0 |
| 1317 | x = v |
| 1318 | k = ulong 0 |
| 1319 | while gt x 0 bound 24 |
| 1320 | put d k uchar add 48 rem x 10 |
| 1321 | x = div x 10 |
| 1322 | k = add k 1 |
| 1323 | end |
| 1324 | if eq k 0 |
| 1325 | is_unc 48 |
| 1326 | end |
| 1327 | for i ulong k bound 24 |
| 1328 | is_unc get d sub sub k i 1 |
| 1329 | end |
| 1330 | end |
| 1331 | |
| 1332 | note is_adopt: the tmp file's SHA-256 matches a pending record: the UID adopts that local message |
| 1333 | fn is_adopt to bool |
| 1334 | d = new uchar 32 0 |
| 1335 | h = new uchar 64 0 |
| 1336 | im_ptmp |
| 1337 | r = s_file S_AUX |
| 1338 | if ne r 0 |
| 1339 | ret false |
| 1340 | end |
| 1341 | sha_s S_AUX 0 s_len S_AUX d |
| 1342 | s_close S_AUX |
| 1343 | for i ulong 32 |
| 1344 | put h mul i 2 get khex ulong shr get d i 4 |
| 1345 | put h add mul i 2 1 get khex ulong and get d i 15 |
| 1346 | end |
| 1347 | n = s_len S_IPD |
| 1348 | p = ulong 0 |
| 1349 | while lt p n bound 9223372036854775807 |
| 1350 | e = st_eol S_IPD p |
| 1351 | if s_eqt S_IPD p 64 h |
| 1352 | is_adopted p e |
| 1353 | ret true |
| 1354 | end |
| 1355 | p = add e 1 |
| 1356 | end |
| 1357 | ret false |
| 1358 | end |
| 1359 | |
| 1360 | fn is_adopted p ulong e ulong to void |
| 1361 | m = ulong sub s_get S_IPD add p 65 65 |
| 1362 | s_addt S_Y kme2 |
| 1363 | s_addu S_Y get im_v 7 |
| 1364 | z = s_add S_Y 32 |
| 1365 | s_adds S_Y S_IPD add p 67 sub e add p 67 |
| 1366 | z = s_add S_Y 32 |
| 1367 | is_yletters m |
| 1368 | im_tmpdrop |
| 1369 | put im_v 44 1 |
| 1370 | put im_v 16 add get im_v 16 1 |
| 1371 | z = s_mem S_AUX |
| 1372 | s_adds S_AUX S_IPD 0 p |
| 1373 | if lt e s_len S_IPD |
| 1374 | s_adds S_AUX S_IPD add e 1 sub sub s_len S_IPD e 1 |
| 1375 | end |
| 1376 | z = s_mem S_IPD |
| 1377 | s_adds S_IPD S_AUX 0 s_len S_AUX |
| 1378 | end |
| 1379 | |
| 1380 | note is_io: an I/O failure: connection_failed when the transport says why (timeout, closed, TLS), |
| 1381 | note else protocol_error (the server sent something mailhand cannot follow) |
| 1382 | fn is_io to int |
| 1383 | if ne tx_why 0 |
| 1384 | ret is_why 1 EIO |
| 1385 | end |
| 1386 | ret is_why 11 EIO |
| 1387 | end |