interplane.Get Omega
Read in your language

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

branch main Branches Tags History

mailhand / src / smtp.om

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