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