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
mailhand / tests / srv / main.om
| 1 | note ======== tests/srv/main.om: the "srv" test program (fake IMAP/SMTP servers, accounts file) ======== |
| 2 | note srv imap DIR fake IMAP server (tests/srv/imap.om) |
| 3 | note srv smtp DIR SCRIPT COUNT fake SMTP submission server (tests/srv/smtp.om) |
| 4 | note srv acct OUT DIR NAME KIND HOST USER PASS MODE write one account block to OUT (replaced), with |
| 5 | note octal MODE: waits for DIR/port, then writes "account NAME", "imap imaps://HOST:PORT" (KIND |
| 6 | note imaps) or "smtp KIND://HOST:PORT" (smtps, submission), user, password, "connect 127.0.0.1" |
| 7 | note and the "roots" line of DIR/state (the trust store). |
| 8 | text sm_kimap "imap" |
| 9 | text sm_ksmtp "smtp" |
| 10 | text sm_kacct "acct" |
| 11 | text sm_kbig "big" |
| 12 | text sm_kimaps "imaps" |
| 13 | text sm_kurl "://" |
| 14 | text sm_kacc "account " |
| 15 | text sm_kimapl "imap " |
| 16 | text sm_ksmtpl "smtp " |
| 17 | text sm_kuser "user " |
| 18 | text sm_kpass "password " |
| 19 | text sm_kconn "connect 127.0.0.1\n" |
| 20 | text sm_kroots "roots " |
| 21 | text sm_usage "usage: srv imap DIR | srv smtp DIR SCRIPT COUNT | srv acct OUT DIR NAME KIND HOST USER PASS MODE\n" |
| 22 | |
| 23 | fn sm_arg i ulong s ulong to bool |
| 24 | b = new uchar 4096 0 |
| 25 | r = argread i b 0 4096 |
| 26 | if lt r 0 |
| 27 | ret false |
| 28 | end |
| 29 | z = s_mem s |
| 30 | s_addt s part b 0 ulong r |
| 31 | ret true |
| 32 | end |
| 33 | |
| 34 | fn sm_setdir to bool |
| 35 | if not sm_arg 2 S_TMP |
| 36 | ret false |
| 37 | end |
| 38 | n = s_len S_TMP |
| 39 | if or eq n 0 gt n 4000 |
| 40 | ret false |
| 41 | end |
| 42 | for i ulong n bound 4096 |
| 43 | put sv_dir i s_get S_TMP i |
| 44 | end |
| 45 | put sv_v 6 n |
| 46 | ret true |
| 47 | end |
| 48 | |
| 49 | fn main to int |
| 50 | if lt argcnt 3 |
| 51 | r = write 2 sm_usage 0 len sm_usage |
| 52 | ret 1 |
| 53 | end |
| 54 | if not sm_arg 1 S_REQ |
| 55 | ret 1 |
| 56 | end |
| 57 | if not sm_setdir |
| 58 | ret 1 |
| 59 | end |
| 60 | if s_eqt S_REQ 0 s_len S_REQ sm_kimap |
| 61 | ret fi_main |
| 62 | end |
| 63 | if s_eqt S_REQ 0 s_len S_REQ sm_ksmtp |
| 64 | ret sm_smtp |
| 65 | end |
| 66 | if s_eqt S_REQ 0 s_len S_REQ sm_kbig |
| 67 | ret sm_big |
| 68 | end |
| 69 | if s_eqt S_REQ 0 s_len S_REQ sm_kacct |
| 70 | ret sm_acct |
| 71 | end |
| 72 | r = write 2 sm_usage 0 len sm_usage |
| 73 | ret 1 |
| 74 | end |
| 75 | |
| 76 | fn sm_smtp to int |
| 77 | if lt argcnt 5 |
| 78 | ret 1 |
| 79 | end |
| 80 | if not sm_arg 3 S_REQ |
| 81 | ret 1 |
| 82 | end |
| 83 | scr = ulong 0 |
| 84 | if s_eqt S_REQ 0 s_len S_REQ fs_s1 |
| 85 | scr = 1 |
| 86 | end |
| 87 | if s_eqt S_REQ 0 s_len S_REQ fs_s2 |
| 88 | scr = 2 |
| 89 | end |
| 90 | if s_eqt S_REQ 0 s_len S_REQ fs_s3 |
| 91 | scr = 3 |
| 92 | end |
| 93 | if s_eqt S_REQ 0 s_len S_REQ fs_s4 |
| 94 | scr = 4 |
| 95 | end |
| 96 | if s_eqt S_REQ 0 s_len S_REQ fs_s5 |
| 97 | scr = 5 |
| 98 | end |
| 99 | if s_eqt S_REQ 0 s_len S_REQ fs_s6 |
| 100 | scr = 6 |
| 101 | end |
| 102 | if s_eqt S_REQ 0 s_len S_REQ fs_s7 |
| 103 | scr = 7 |
| 104 | end |
| 105 | if s_eqt S_REQ 0 s_len S_REQ fs_s8 |
| 106 | scr = 8 |
| 107 | end |
| 108 | if eq scr 0 |
| 109 | ret 1 |
| 110 | end |
| 111 | if not sm_arg 4 S_REQ |
| 112 | ret 1 |
| 113 | end |
| 114 | ret fs_main scr sv_num S_REQ 0 s_len S_REQ |
| 115 | end |
| 116 | |
| 117 | note sm_acct: args 2 OUT, 3 DIR, 4 NAME, 5 KIND, 6 HOST, 7 USER, 8 PASS, 9 MODE (octal) |
| 118 | fn sm_acct to int |
| 119 | if ne argcnt 10 |
| 120 | ret 1 |
| 121 | end |
| 122 | if not sm_arg 3 S_TMP |
| 123 | ret 1 |
| 124 | end |
| 125 | n = s_len S_TMP |
| 126 | for i ulong n bound 4096 |
| 127 | put sv_dir i s_get S_TMP i |
| 128 | end |
| 129 | put sv_v 6 n |
| 130 | t = add monons 30000000000 |
| 131 | while true bound 9223372036854775807 |
| 132 | sv_path sv_kport |
| 133 | if eq p_kind 2 |
| 134 | break |
| 135 | end |
| 136 | if gt monons t |
| 137 | ret 4 |
| 138 | end |
| 139 | z = sleepns 10000000 |
| 140 | end |
| 141 | r = s_file S_AUX |
| 142 | if ne r 0 |
| 143 | ret 4 |
| 144 | end |
| 145 | w_reset |
| 146 | w_t sm_kacc |
| 147 | z = sm_warg 4 |
| 148 | w_b 10 |
| 149 | z = sm_arg 5 S_REQ |
| 150 | if s_eqt S_REQ 0 s_len S_REQ sm_kimaps |
| 151 | w_t sm_kimapl |
| 152 | else |
| 153 | w_t sm_ksmtpl |
| 154 | end |
| 155 | w_s S_REQ 0 s_len S_REQ |
| 156 | w_t sm_kurl |
| 157 | z = sm_warg 6 |
| 158 | w_b 58 |
| 159 | w_s S_AUX 0 sub s_len S_AUX 1 |
| 160 | w_b 10 |
| 161 | w_t sm_kuser |
| 162 | z = sm_warg 7 |
| 163 | w_b 10 |
| 164 | w_t sm_kpass |
| 165 | z = sm_warg 8 |
| 166 | w_b 10 |
| 167 | w_t sm_kconn |
| 168 | w_t sm_kroots |
| 169 | sm_roots |
| 170 | w_b 10 |
| 171 | ret sm_wout |
| 172 | end |
| 173 | |
| 174 | fn sm_warg i ulong to bool |
| 175 | if not sm_arg i S_REQ |
| 176 | ret false |
| 177 | end |
| 178 | w_s S_REQ 0 s_len S_REQ |
| 179 | ret true |
| 180 | end |
| 181 | |
| 182 | fn sm_wout to int |
| 183 | if not sm_arg 9 S_REQ |
| 184 | ret 1 |
| 185 | end |
| 186 | m = ulong 0 |
| 187 | for i ulong s_len S_REQ bound 6 |
| 188 | m = add mul m 8 ulong sub s_get S_REQ i 48 |
| 189 | end |
| 190 | if not sm_arg 2 S_TMP |
| 191 | ret 1 |
| 192 | end |
| 193 | p_reset |
| 194 | p_s S_TMP 0 s_len S_TMP |
| 195 | fd = fopen gp 0 get gv VPL 557633 int m |
| 196 | if lt fd 0 |
| 197 | ret 6 |
| 198 | end |
| 199 | r = fchmod int fd int m |
| 200 | k = s_wfd S_W int fd |
| 201 | z = fclose int fd |
| 202 | if or ne r 0 ne k 0 |
| 203 | ret 6 |
| 204 | end |
| 205 | ret 0 |
| 206 | end |
| 207 | |
| 208 | note sm_big: "srv big OUT N": OUT (mode 0600, replaced) holds N bytes "#" (one long comment line) |
| 209 | fn sm_big to int |
| 210 | b = new uchar 65536 35 |
| 211 | if not sm_arg 3 S_REQ |
| 212 | ret 1 |
| 213 | end |
| 214 | n = sv_num S_REQ 0 s_len S_REQ |
| 215 | p_reset |
| 216 | for i ulong get sv_v 6 bound 4096 |
| 217 | p_c get sv_dir i |
| 218 | end |
| 219 | fd = fopen gp 0 get gv VPL 557633 384 |
| 220 | if lt fd 0 |
| 221 | ret 6 |
| 222 | end |
| 223 | q = ulong 0 |
| 224 | while lt q n bound 9223372036854775807 |
| 225 | m = sub n q |
| 226 | if gt m 65536 |
| 227 | m = 65536 |
| 228 | end |
| 229 | r = write int fd b 0 m |
| 230 | if le r 0 |
| 231 | ret 6 |
| 232 | end |
| 233 | q = add q ulong r |
| 234 | end |
| 235 | z = fclose int fd |
| 236 | ret 0 |
| 237 | end |
| 238 | |
| 239 | note sm_roots: the trust store line of the account: "roots PATH" from DIR/state when it has one, |
| 240 | note else DIR/port (a file that is not a trust store: the sync is then refused) |
| 241 | fn sm_roots to void |
| 242 | sv_path fi_kstate |
| 243 | r = s_file S_CFG |
| 244 | if eq r 0 |
| 245 | n = s_len S_CFG |
| 246 | p = ulong 0 |
| 247 | while lt p n bound 9223372036854775807 |
| 248 | e = fi_eol p |
| 249 | if fi_skey p e fi_kroots |
| 250 | w_s S_CFG add p 6 sub e add p 6 |
| 251 | ret |
| 252 | end |
| 253 | p = add e 1 |
| 254 | end |
| 255 | end |
| 256 | z = sm_warg 3 |
| 257 | w_b 47 |
| 258 | w_t sv_kport |
| 259 | end |