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/smtp.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 / smtp.om` 325 lines ยท 5905 bytes Raw Blame History 1 text sm_lit1 "unix:" 2 text sm_lit2 "127.0.0.1:" 3 text sm_lit3 "RCPT TO:<" 4 text sm_lit4 ">" 5 text sm_lit5 "." 6 text sm_lit6 "smtp" 7 text sm_lit7 "EHLO " 8 text sm_lit8 "helo" 9 text sm_lit9 "localhost" 10 text sm_lit10 "me" 11 text sm_lit11 "MAIL FROM:<" 12 text sm_lit12 "DATA" 13 text sm_lit13 "QUIT" 14 text sm_lit14 "account:" 15 text sm_lit15 "smtp: " 16 note SMTP client. Every byte goes through the transport (src/tx.om): the loopback endpoints 17 note (unix:PATH, 127.0.0.1:PORT) as plain connections, and "smtp account:NAME" as SMTP submission 18 note over TLS (src/ss.om). Deadlines, EINTR and partial writes are handled by tx_. 19 note 110 bytes is Linux sockaddr_un; 65536 byte arrays are streaming windows, not input limits. 20 const SM_NS long `30000000000` 21 fn sm_write b view uchar * to bool 22 ret tx_write b 23 end 24 note One line into S_AUX; no line size limit. Return false for EOF/timeouts/errors. 25 fn sm_line to bool 26 sm_fresh S_AUX 27 while true bound `9223372036854775807` 28 c = tx_getc 29 if lt c 0 30 ret false 31 end 32 if not s_add S_AUX uchar c 33 ret false 34 end 35 if eq c 10 36 ret true 37 end 38 end 39 ret false 40 end 41 fn sm_reply expected ulong to bool 42 while true bound `9223372036854775807` 43 if not sm_line 44 ret false 45 end 46 if not sm_code expected 47 ret false 48 end 49 if eq s_get S_AUX 3 32 50 ret true 51 end 52 end 53 ret false 54 end 55 note sm_code: S_AUX is a reply line "NNN-..." or "NNN ..." with code expected (else it is shown) 56 fn sm_code expected ulong to bool 57 if lt s_len S_AUX 5 58 z = s_wfd S_AUX 2 59 ret false 60 end 61 v = ulong 0 62 for i ulong 3 63 c = s_get S_AUX i 64 if or lt c 48 gt c 57 65 z = s_wfd S_AUX 2 66 ret false 67 end 68 v = add mul v 10 ulong sub c 48 69 end 70 if ne v expected 71 z = s_wfd S_AUX 2 72 ret false 73 end 74 c3 = s_get S_AUX 3 75 if and ne c3 32 ne c3 45 76 z = s_wfd S_AUX 2 77 ret false 78 end 79 ret true 80 end 81 fn sm_endpoint s ulong o ulong n ulong to long 82 a = new uchar 110 0 83 if ge n 6 84 if s_eqt s o 5 sm_lit1 85 if ne s_get s add o 5 47 86 ret -22 87 end 88 m = sub n 5 89 if ge m 108 90 ret -36 91 end 92 put a 0 1 93 for i ulong m bound 108 94 c = s_get s add add o 5 i 95 if eq c 0 96 ret -22 97 end 98 put a add 2 i c 99 end 100 ret sm_connect 1 part a 0 add m 3 101 end 102 end 103 if le n 10 104 ret -22 105 end 106 if not s_eqt s o 10 sm_lit2 107 ret -22 108 end 109 v = ulong 0 110 for i ulong sub n 10 bound `9223372036854775807` 111 c = s_get s add add o 10 i 112 if or lt c 48 gt c 57 113 ret -22 114 end 115 if gt v 6553 116 ret -22 117 end 118 v = add mul v 10 ulong sub c 48 119 end 120 if or eq v 0 gt v 65535 121 ret -22 122 end 123 put a 0 2 124 put a 2 uchar shr v 8 125 put a 3 uchar and v 255 126 put a 4 127 127 put a 7 1 128 ret sm_connect 2 part a 0 16 129 end 130 fn sm_connect family int a view uchar * to long 131 fd = socknew family 526337 0 132 if lt fd 0 133 ret fd 134 end 135 r = connect int fd a 0 len a 136 if eq r -115 137 if tx_wait int fd 4 add monons SM_NS 138 ret fd 139 end 140 end 141 if eq r 0 142 ret fd 143 end 144 z = fclose int fd 145 ret -5 146 end 147 fn sm_cmd cmd view uchar * code ulong to bool 148 if not sm_write cmd 149 ret false 150 end 151 ret sm_reply code 152 end 153 fn sm_crlf to void 154 w_b 13 155 w_b 10 156 end 157 fn sm_command code ulong to bool 158 if not tx_wstore S_W 0 s_len S_W 159 ret false 160 end 161 ret sm_reply code 162 end 163 note Caller supplies trusted checked envelope addresses in a newline-separated store range. 164 fn sm_envelope s ulong o ulong n ulong to bool 165 p = o 166 e = add o n 167 while lt p e bound `9223372036854775807` 168 q = p 169 while lt q e bound `9223372036854775807` 170 if eq s_get s q 10 171 break 172 end 173 q = add q 1 174 end 175 w_reset 176 w_t sm_lit3 177 w_s s p sub q p 178 w_t sm_lit4 179 sm_crlf 180 if not sm_command 250 181 ret false 182 end 183 p = add q 1 184 end 185 ret true 186 end 187 note Message canonicalization: accept LF or CRLF; emit CRLF and SMTP dot stuffing. 188 fn sm_data to bool 189 b = new uchar 65536 0 190 p = ulong 0 191 k = ulong 0 192 bol = true 193 while lt p s_len S_MSG bound `9223372036854775807` 194 c = s_get S_MSG p 195 p = add p 1 196 if eq c 13 197 if eq s_get S_MSG p 10 198 continue 199 end 200 end 201 if and bol eq c 46 202 put b k 46 203 k = add k 1 204 end 205 if eq c 10 206 put b k 13 207 k = add k 1 208 end 209 put b k c 210 k = add k 1 211 bol = eq c 10 212 if ge k 65532 213 if not sm_write part b 0 k 214 ret false 215 end 216 k = ulong 0 217 end 218 end 219 if not bol 220 put b k 13 221 put b add k 1 10 222 k = add k 2 223 end 224 if not sm_write part b 0 k 225 ret false 226 end 227 w_reset 228 w_t sm_lit5 229 sm_crlf 230 ret sm_command 250 231 end 232 note sm_submit takes the endpoint from config; S_MSG holds the immutable draft snapshot. The send 233 note gate (sd_gate) has already passed when this runs: no connection is opened before it. 234 fn sm_submit s ulong o ulong n ulong to bool 235 if not cf_get sm_lit6 236 ret false 237 end 238 tx_timeout 239 co = get gv VCFO 240 cn = get gv VCFN 241 if ge cn len sm_lit14 242 if s_eqt S_CFG co len sm_lit14 sm_lit14 243 ret ss_submit add co len sm_lit14 sub cn len sm_lit14 s o n 244 end 245 end 246 fd = sm_endpoint S_CFG co cn 247 if lt fd 0 248 ret false 249 end 250 tx_plain fd 251 ok = false 252 if sm_hello 253 ok = sm_mail s o n 254 end 255 if not ok 256 tx_diag sm_lit15 257 end 258 tx_close 259 ret ok 260 end 261 note sm_hello: greeting and EHLO on the loopback endpoints (no authentication there) 262 fn sm_hello to bool 263 if not sm_reply 220 264 ret false 265 end 266 if not sm_ehlo 267 ret false 268 end 269 ret sm_reply 250 270 end 271 note sm_ehlo: send "EHLO name" (config helo, else localhost); the caller reads the reply 272 fn sm_ehlo to bool 273 w_reset 274 w_t sm_lit7 275 if cf_get sm_lit8 276 if not sd_header_safe S_CFG get gv VCFO get gv VCFN 277 ret false 278 end 279 w_s S_CFG get gv VCFO get gv VCFN 280 else 281 w_t sm_lit9 282 end 283 sm_crlf 284 ret tx_wstore S_W 0 s_len S_W 285 end 286 note sm_mail: one transaction (MAIL, RCPT, DATA, QUIT) on a greeted (and authenticated) session 287 fn sm_mail s ulong o ulong n ulong to bool 288 if not cf_get sm_lit10 289 ret false 290 end 291 if not sd_addr_safe S_CFG get gv VCFO get gv VCFN 292 ret false 293 end 294 w_reset 295 w_t sm_lit11 296 w_s S_CFG get gv VCFO get gv VCFN 297 w_t sm_lit4 298 sm_crlf 299 if not sm_command 250 300 ret false 301 end 302 if not sm_envelope s o n 303 ret false 304 end 305 w_reset 306 w_t sm_lit12 307 sm_crlf 308 if not sm_command 354 309 ret false 310 end 311 if not sm_data 312 ret false 313 end 314 note DATA 250 is the delivery boundary; QUIT failure cannot undo acceptance. 315 w_reset 316 w_t sm_lit13 317 sm_crlf 318 ok = sm_command 221 319 ret true 320 end 321 note sm_fresh: store s becomes a new empty memfd. s_reset cannot empty a store that s_file opened 322 note read-only (it would keep serving the old file's bytes), and S_AUX is loaded that way by sd_file. 323 fn sm_fresh s ulong to void 324 z = s_mem s 325 end