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/tests/srv/sv.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 / tests / srv / sv.om` 343 lines ยท 5944 bytes Raw Blame History 1 note ======== tests/srv/sv.om: shared code of the fake mail servers (tests only, prefix sv_) ======== 2 note "srv imap DIR", "srv smtp DIR SCRIPT COUNT" and "srv acct OUT DIR NAME KIND HOST USER PASS MODE". 3 note Each server listens on 127.0.0.1 at a port chosen at random in 19000..19999 for every run (a busy 4 note port is retried), writes it to DIR/port, serves its connections (plain, or TLS through the 5 note omtls test server, ot_ts_load and ot_ts_accept, then ot_tl_read/write/close) and logs to DIR/log. 6 note Mail is synthetic (example.org / example.test names). Nothing here is used by mailhand itself. 7 global sv_v ulong 16 8 note sv_v: 0 connection fd + 1, 1 TLS on (1), 2 window read position, 3 window bytes, 4 listen fd + 1, 9 note 5 port, 6 DIR length, 7 accept deadline ns 10 global sv_rb uchar 65536 11 global sv_dir uchar 4096 12 text sv_kport "port" 13 text sv_klog "log" 14 text sv_crlf "\n" 15 16 note sv_path: gp = DIR "/" t 17 fn sv_path t view uchar * to void 18 p_reset 19 for i ulong get sv_v 6 bound 4096 20 p_c get sv_dir i 21 end 22 p_c 47 23 p_t t 24 end 25 26 note sv_logs: append store range [o, +n) and a newline to DIR/log 27 fn sv_logs s ulong o ulong n ulong to void 28 w_reset 29 w_s s o n 30 w_b 10 31 sv_path sv_klog 32 zl = f_app 33 end 34 35 fn sv_logt t view uchar * to void 36 w_reset 37 w_t t 38 w_b 10 39 sv_path sv_klog 40 zl = f_app 41 end 42 43 note sv_listen: bind a random free port in 19000..19999 and listen; writes DIR/port 44 fn sv_listen to bool 45 r = new uchar 2 0 46 a = new uchar 16 0 47 fd = socknew 2 526337 0 48 if lt fd 0 49 ret false 50 end 51 put a 0 2 52 put a 4 127 53 put a 7 1 54 ok = false 55 for t ulong 4000 56 z = random r 0 2 0 57 port = add 19000 rem or shl ulong get r 0 8 ulong get r 1 1000 58 put a 2 uchar shr port 8 59 put a 3 uchar and port 255 60 b = bind int fd a 0 16 61 if eq b 0 62 put sv_v 5 port 63 ok = true 64 break 65 end 66 end 67 if not ok 68 ret false 69 end 70 if lt listen int fd 8 0 71 ret false 72 end 73 put sv_v 4 add ulong fd 1 74 w_reset 75 w_u get sv_v 5 76 w_b 10 77 sv_path sv_kport 78 ret eq f_put 0 79 end 80 81 note sv_accept: the next connection within ns; true when one arrived 82 fn sv_accept ns long to bool 83 put sv_v 1 0 84 put sv_v 2 0 85 put sv_v 3 0 86 lf = int sub get sv_v 4 1 87 t = add monons ns 88 while true bound `9223372036854775807` 89 r = sub t monons 90 if le r 0 91 ret false 92 end 93 z = waitio lf 1 r 94 if eq z -4 95 continue 96 end 97 if le z 0 98 ret false 99 end 100 c = accept lf 526336 101 if ge c 0 102 put sv_v 0 add ulong c 1 103 ret true 104 end 105 if and ne c -11 ne c -4 106 ret false 107 end 108 end 109 ret false 110 end 111 112 fn sv_fd to int 113 ret int sub get sv_v 0 1 114 end 115 116 fn sv_tls to bool 117 c = new uchar 4096 0 118 k = new uchar 4096 0 119 cn = s_read S_DEC 0 c 120 kn = s_read S_MIME 0 k 121 if not ot_ts_load part c 0 cn part k 0 kn 122 ret false 123 end 124 if ne ot_ts_accept sv_fd 0 125 ret false 126 end 127 put sv_v 1 1 128 ret true 129 end 130 131 fn sv_write d view uchar * to bool 132 if eq get sv_v 1 1 133 ret ot_tl_write d 134 end 135 ret sv_raw sv_fd d 136 end 137 138 fn sv_wt t view uchar * to bool 139 ret sv_write t 140 end 141 142 note sv_ws: write store range [o, +n) 143 fn sv_ws s ulong o ulong n ulong to bool 144 b = new uchar 65536 0 145 p = ulong 0 146 while lt p n bound `9223372036854775807` 147 m = sub n p 148 if gt m 65536 149 m = 65536 150 end 151 k = s_read s add o p part b 0 m 152 if eq k 0 153 ret false 154 end 155 if not sv_write part b 0 k 156 ret false 157 end 158 p = add p k 159 end 160 ret true 161 end 162 163 fn sv_fill to long 164 put sv_v 2 0 165 put sv_v 3 0 166 b = new uchar 65536 0 167 r = long 0 168 if eq get sv_v 1 1 169 r = ot_tl_read b 170 else 171 fd = sv_fd 172 t = add monons `30000000000` 173 while true bound `9223372036854775807` 174 if not sv_until fd 1 t 175 ret -110 176 end 177 r = recv fd b 0 65536 0 178 if and ne r -4 ne r -11 179 break 180 end 181 end 182 end 183 if le r 0 184 ret r 185 end 186 for i ulong ulong r bound 65536 187 put sv_rb i get b i 188 end 189 put sv_v 3 ulong r 190 ret r 191 end 192 193 fn sv_getc to long 194 p = get sv_v 2 195 if ge p get sv_v 3 196 if le sv_fill 0 197 ret -1 198 end 199 p = 0 200 end 201 put sv_v 2 add p 1 202 ret long get sv_rb p 203 end 204 205 note sv_pending: bytes already received but not yet read 206 fn sv_pending to bool 207 ret lt get sv_v 2 get sv_v 3 208 end 209 210 note sv_line: one line into S_AUX without CR LF; false at the end of the stream 211 fn sv_line to bool 212 z = s_mem S_AUX 213 while true bound `9223372036854775807` 214 c = sv_getc 215 if lt c 0 216 ret false 217 end 218 if eq c 10 219 n = s_len S_AUX 220 if gt n 0 221 if eq s_get S_AUX sub n 1 13 222 s_trunc S_AUX sub n 1 223 end 224 end 225 ret true 226 end 227 z = s_add S_AUX uchar c 228 end 229 ret false 230 end 231 232 note sv_bytes: n received bytes appended to store s 233 fn sv_bytes s ulong n ulong to bool 234 for i ulong n bound `9223372036854775807` 235 c = sv_getc 236 if lt c 0 237 ret false 238 end 239 z = s_add s uchar c 240 end 241 ret true 242 end 243 244 fn sv_close to void 245 if gt get sv_v 0 0 246 if eq get sv_v 1 1 247 ot_tl_close 248 else 249 z = fclose sv_fd 250 end 251 end 252 put sv_v 0 0 253 put sv_v 1 0 254 end 255 256 note sv_num: decimal value of store range (0 when not a number) 257 fn sv_num s ulong o ulong n ulong to ulong 258 v = ulong 0 259 for i ulong n bound 20 260 c = s_get s add o i 261 if or lt c 48 gt c 57 262 ret v 263 end 264 v = add mul v 10 ulong sub c 48 265 end 266 ret v 267 end 268 269 note sv_b64: decode base64 store range into store d; false when malformed 270 text sv_b64a "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 271 fn sv_b64 s ulong o ulong n ulong d ulong to bool 272 if ne rem n 4 0 273 ret false 274 end 275 x = ulong 0 276 k = ulong 0 277 for i ulong n bound `9223372036854775807` 278 c = s_get s add o i 279 v = sv_b64v c 280 if eq c 61 281 k = add k 1 282 v = 0 283 else 284 if gt v 63 285 ret false 286 end 287 end 288 x = or shl x 6 v 289 if eq rem add i 1 4 0 290 z = s_add d uchar and shr x 16 255 291 if lt k 2 292 z = s_add d uchar and shr x 8 255 293 end 294 if lt k 1 295 z = s_add d uchar and x 255 296 end 297 x = 0 298 end 299 end 300 ret true 301 end 302 303 fn sv_b64v c uchar to ulong 304 for j ulong 64 305 if eq c get sv_b64a j 306 ret j 307 end 308 end 309 ret 64 310 end 311 312 note sv_until: poll fd for ev until the monotonic deadline (EINTR: retry) 313 fn sv_until fd int ev int until long to bool 314 r = sub until monons 315 if le r 0 316 ret false 317 end 318 z = waitio fd ev r 319 if eq z -4 320 ret true 321 end 322 ret gt z 0 323 end 324 325 note sv_raw: all of d on the plain socket fd (partial sends and EINTR/EAGAIN looped, 30 s deadline) 326 fn sv_raw fd int d view uchar * to bool 327 p = ulong 0 328 t = add monons `30000000000` 329 while lt p len d bound `9223372036854775807` 330 if not sv_until fd 4 t 331 ret false 332 end 333 r = send fd d p sub len d p 0 334 if or eq r -4 eq r -11 335 continue 336 end 337 if le r 0 338 ret false 339 end 340 p = add p ulong r 341 end 342 ret true 343 end