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 / tests / srv / sv.om

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