interplane.Get Omega
Read in your language

aien

aien-dev/aien

Brought by drakestapleton

branch main Branches Tags History

aien / src / bots / bots.om

755 lines · 11556 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-or-later
2note Local bot journal only. Concatenate after pinned Omega sha256-om2 unit.
3global BR uchar 1024
4global BJ uchar 1048576
5global BB uchar 65536
6global BL uchar 2048
7global BM uchar 262144
8global BC uchar 8192
9global BS ulong 256
10global BT ulong 64
11global BV ulong 12
12global BA uchar 4096
13global BP uchar 4096
14global BK uchar 4096
15global BF uchar 4096
16global BD uchar 4096
17global BN uchar 8192
18global BO uchar 4096
19text bdom "AIEN-BOT-V1"
20text mdom "AIEN-BOT-MESSAGE-V1"
21text hx "0123456789abcdef"
22text ec "REFUSE "
23text ok "OK "
24text dup "DUPLICATE "
25text bn "BOT "
26text ln "LEASE "
27text mn "MESSAGE "
28text cmds "register rotate deliver claim ack nack expire retire show"
29text kenv "BOTS_KILL="
30
31text lockend ".lock"
32text candend ".cand"
33global BH uchar 32
34fn bg s ulong p ulong to uchar
35if eq s 1
36ret get BR p
37end
38if eq s 2
39ret get BJ p
40end
41if eq s 3
42ret get BB p
43end
44if eq s 4
45ret get BL p
46end
47if eq s 5
48ret get BM p
49end
50if eq s 6
51ret get BC p
52end
53if eq s 7
54ret get BA p
55end
56if eq s 8
57ret get BP p
58end
59if eq s 9
60ret get BK p
61end
62if eq s 10
63ret get BF p
64end
65if eq s 11
66ret get BD p
67end
68if eq s 12
69ret get BN p
70end
71if eq s 13
72ret get BO p
73end
74if eq s 14
75ret get BH p
76end
77if eq s 15
78ret get cmds p
79end
80if eq s 16
81ret get kenv p
82end
83if eq s 17
84ret get lockend p
85end
86if eq s 18
87ret get candend p
88end
89bfail 3
90ret 0
91end
92fn bp s ulong p ulong v uchar to void
93if eq s 1
94put BR p v
95ret
96end
97if eq s 2
98put BJ p v
99ret
100end
101if eq s 3
102put BB p v
103ret
104end
105if eq s 4
106put BL p v
107ret
108end
109if eq s 5
110put BM p v
111ret
112end
113if eq s 6
114put BC p v
115ret
116end
117if eq s 7
118put BA p v
119ret
120end
121if eq s 8
122put BP p v
123ret
124end
125if eq s 9
126put BK p v
127ret
128end
129if eq s 10
130put BF p v
131ret
132end
133if eq s 11
134put BD p v
135ret
136end
137if eq s 12
138put BN p v
139ret
140end
141if eq s 13
142put BO p v
143ret
144end
145if eq s 14
146put BH p v
147ret
148end
149bfail 3
150end
151fn bfail code int to void
152w = write 1 ec 0 len ec
153bdecimal ulong code
154bend
155exit code
156end
157fn bcheck yes bool code int to void
158if not yes
159bfail code
160end
161end
162fn bwrite fd int s ulong off ulong n ulong to void
163p = ulong 0
164while lt p n bound 999999
165w = long 0
166if eq s 1
167w = write fd BR add off p sub n p
168else
169w = write fd BJ add off p sub n p
170end
171bcheck gt w 0 3
172p = add p ulong w
173end
174end
175fn bdecimal v ulong to void
176if gt v 9
177bdecimal div v 10
178end
179put BO 0 uchar add rem v 10 48
180w = write 1 BO 0 1
181bcheck eq w 1 3
182end
183fn bend to void
184put BO 0 10
185w = write 1 BO 0 1
186bcheck eq w 1 3
187end
188fn bspace to void
189put BO 0 32
190w = write 1 BO 0 1
191bcheck eq w 1 3
192end
193fn bhex s ulong off ulong n ulong to void
194for i ulong n
195v = ulong bg s add off i
196put BO mul i 2 get hx shr v 4
197put BO add mul i 2 1 get hx and v 15
198end
199w = write 1 BO 0 mul n 2
200bcheck eq w long mul n 2 3
201end
202fn bcopy ds ulong dp ulong ss ulong sp ulong n ulong to void
203for i ulong n
204bp ds add dp i bg ss add sp i
205end
206end
207fn bequal ds ulong dp ulong ss ulong sp ulong n ulong to bool
208for i ulong n
209if ne bg ds add dp i bg ss add sp i
210ret false
211end
212end
213ret true
214end
215fn bzero s ulong off ulong n ulong to bool
216v = uchar 0
217for i ulong n
218v = or v bg s add off i
219end
220ret eq v 0
221end
222fn bget a ulong to ulong
223v = ulong 0
224for i ulong 8
225v = or shl v 8 ulong get BR add a i
226end
227ret v
228end
229fn bset a ulong v ulong to void
230for i ulong 8
231put BR add a i uchar and shr v mul sub 7 i 8 255
232end
233end
234fn bclear to void
235for i ulong 1024
236put BR i 0
237end
238end
239fn barg n ulong to ulong
240r = argread n BA 0 len BA
241bcheck ge r 0 2
242ret ulong r
243end
244fn bnyb c uchar to uchar
245if and ge c 48 le c 57
246ret sub c 48
247end
248if and ge c 97 le c 102
249ret add sub c 97 10
250end
251bfail 2
252ret 0
253end
254fn bparse n ulong off ulong to void
255r = barg n
256note BOT-GUARD hex
257bcheck eq r 64 2
258for i ulong 32
259v = shl bnyb get BA mul i 2 4
260put BR add off i or v bnyb get BA add mul i 2 1
261end
262note BOT-GUARD nonzero
263bcheck not bzero 1 off 32 4
264end
265fn brefs n ulong off ulong to void
266r = barg n
267if and eq r 1 eq get BA 0 45
268ret
269end
270note BOT-GUARD refs
271bcheck and le r 259 eq rem add r 1 65 0 2
272cnt = div add r 1 65
273for j ulong cnt
274for i ulong 32
275p = add mul j 65 mul i 2
276v = shl bnyb get BA p 4
277v = or v bnyb get BA add p 1
278p = add off mul j 32
279put BR add p i v
280end
281bcheck not bzero 1 add off mul j 32 32 4
282if lt add j 1 cnt
283bcheck eq get BA add mul j 65 64 44 2
284end
285end
286end
287fn bhash mode ulong to void
288p = new uchar 1024 0
289d = new uchar 32 0
290n = ulong 992
291dest = ulong 992
292if eq mode 0
293for i ulong 992
294put p i get BR i
295end
296else
297if eq mode 1
298for i ulong len bdom
299put p i get bdom i
300end
301n = add len bdom 97
302for i ulong 96
303put p add add len bdom 1 i get BR add 48 i
304end
305dest = 16
306else
307for i ulong len mdom
308put p i get mdom i
309end
310n = add len mdom 129
311for i ulong 32
312put p add add len mdom 1 i get BR add 16 i
313end
314for i ulong 96
315put p add add len mdom 33 i get BR add 48 i
316end
317dest = 144
318end
319end
320sha256_hash part p 0 n d
321for i ulong 32
322put BR add dest i get d i
323end
324end
325fn bpayload n ulong to void
326p = new uchar 4096 0
327d = new uchar 32 0
328r = barg n
329for i ulong r
330put p i get BA i
331end
332sha256_hash part p 0 r d
333for i ulong 32
334put BR add 80 i get d i
335end
336end
337fn bcommand to ulong
338n = barg 1
339p = ulong 0
340for k ulong 9
341q = p
342while lt p len cmds bound 64
343if eq get cmds p 32
344break
345end
346p = add p 1
347end
348if eq n sub p q
349if bequal 7 0 15 q n
350ret add k 1
351end
352end
353p = add p 1
354end
355bfail 2
356ret 0
357end
358fn bargs t ulong to void
359bclear
360put BR 0 66
361put BR 1 74
362put BR 2 84
363put BR 3 49
364put BR 4 uchar t
365if eq t 1
366bcheck eq argcnt 10 2
367bparse 3 48
368bparse 4 80
369bparse 5 112
370r = barg 6
371note BOT-GUARD name
372bcheck and gt r 0 le r 64 2
373for i ulong r
374bcheck and ge get BA i 32 le get BA i 126 2
375put BR add 144 i get BA i
376end
377brefs 7 208
378brefs 8 336
379brefs 9 464
380bhash 1
381ret
382end
383if eq t 9
384bcheck eq argcnt 3 2
385ret
386end
387bparse 3 16
388if eq t 8
389bcheck eq argcnt 4 2
390ret
391end
392if eq t 2
393bcheck eq argcnt 5 2
394bparse 4 48
395ret
396end
397if eq t 3
398bcheck eq argcnt 7 2
399bparse 4 48
400bpayload 5
401bparse 6 112
402bhash 2
403ret
404end
405bcheck eq argcnt 6 2
406bparse 4 48
407bparse 5 80
408end
409fn bfind to ulong
410for i ulong get BV 1
411if bequal 1 16 3 add mul i 1024 16 32
412ret i
413end
414end
415bfail 11
416ret 0
417end
418fn bmfind to ulong
419for i ulong get BV 2
420if bequal 1 16 5 add mul i 1024 16 32
421if bequal 1 48 5 add mul i 1024 144 32
422ret i
423end
424end
425end
426bfail 14
427ret 0
428end
429fn bcurrent b ulong off ulong to void
430note BOT-GUARD current
431bcheck bequal 1 off 4 mul b 32 32 15
432end
433fn bregister to void
434for i ulong get BV 1
435note BOT-GUARD registered
436bcheck not bequal 1 16 3 add mul i 1024 16 32 10
437end
438note BOT-GUARD botcap
439bcheck lt get BV 1 64 5
440b = get BV 1
441bcopy 3 mul b 1024 1 0 1024
442put BT b 1
443put BV 1 add b 1
444end
445fn brotate b ulong to void
446for i ulong sub bget 8 1
447p = mul i 1024
448if eq get BJ add p 4 2
449if bequal 2 add p 16 1 16 32
450note BOT-GUARD leaseunique
451bcheck not bequal 2 add p 48 1 48 32 13
452end
453end
454end
455bcopy 4 mul b 32 1 48 32
456end
457fn bdeliver to void
458for i ulong get BV 2
459if bequal 1 144 5 add mul i 1024 144 32
460note BOT-GUARD dedup
461put BV 3 1
462ret
463end
464end
465note BOT-GUARD msgcap
466bcheck lt get BV 2 256 5
467i = get BV 2
468bcopy 5 mul i 1024 1 0 1024
469put BS i 1
470put BV 2 add i 1
471end
472fn bmessage t ulong b ulong to void
473m = bmfind
474s = get BS m
475if eq t 4
476note BOT-GUARD pending
477bcheck eq s 1 16
478bcurrent b 80
479bcopy 6 mul m 32 1 80 32
480put BS m 2
481ret
482end
483note BOT-GUARD claimed
484bcheck eq s 2 17
485note BOT-GUARD claimant
486bcheck bequal 1 80 6 mul m 32 32 18
487if eq t 7
488note BOT-GUARD expiry
489bcheck not bequal 1 80 4 mul b 32 32 19
490else
491bcurrent b 80
492end
493if eq t 5
494put BS m 3
495else
496put BS m 1
497end
498for i ulong 32
499put BC add mul m 32 i 0
500end
501end
502fn bapply to void
503put BV 3 0
504t = ulong get BR 4
505note BOT-GUARD type
506bcheck and gt t 0 le t 8 6
507if eq t 1
508bregister
509ret
510end
511b = bfind
512note BOT-GUARD active
513bcheck eq get BT b 1 12
514if eq t 2
515brotate b
516ret
517end
518if eq t 3
519bdeliver
520ret
521end
522if eq t 8
523put BT b 2
524ret
525end
526bmessage t b
527end
528fn bshape to void
529note BOT-GUARD magic
530bcheck and and eq get BR 0 66 eq get BR 1 74 and eq get BR 2 84 eq get BR 3 49 6
531t = ulong get BR 4
532note BOT-GUARD botvalue
533bcheck not bzero 1 16 32 6
534note BOT-GUARD reserved
535bcheck bzero 1 5 3 6
536if eq t 1
537note BOT-GUARD bindings
538bcheck and not bzero 1 48 32 and not bzero 1 80 32 not bzero 1 112 32 6
539bcopy 14 0 1 16 32
540bhash 1
541note BOT-GUARD identity
542bcheck bequal 14 0 1 16 32 6
543z = ulong 0
544for i ulong 64
545v = get BR add 144 i
546if eq v 0
547z = 1
548else
549note BOT-GUARD display
550bcheck and eq z 0 and ge v 32 le v 126 6
551end
552end
553note BOT-GUARD nonempty
554bcheck ne get BR 144 0 6
555end
556if eq t 2
557note BOT-GUARD leasevalue
558bcheck not bzero 1 48 32 6
559end
560if eq t 3
561note BOT-GUARD envelope
562bcheck and not bzero 1 48 32 not bzero 1 112 32 6
563bcopy 14 0 1 144 32
564bhash 2
565note BOT-GUARD content
566bcheck bequal 14 0 1 144 32 6
567end
568if and ge t 4 le t 7
569note BOT-GUARD transition
570bcheck and not bzero 1 48 32 not bzero 1 80 32 6
571end
572n = ulong 48
573if eq t 1
574n = 592
575end
576if eq t 2
577n = 80
578end
579if eq t 3
580n = 176
581end
582if and ge t 4 le t 7
583n = 112
584end
585note BOT-GUARD padding
586bcheck bzero 1 n sub 992 n 6
587end
588fn breplay to void
589p = ulong 0
590while lt p get BV 0 bound 1024
591bcopy 1 0 2 p 1024
592bshape
593note BOT-GUARD sequence
594bcheck eq bget 8 add div p 1024 1 7
595bcopy 14 0 1 992 32
596bhash 0
597note BOT-GUARD checksum
598bcheck bequal 14 0 1 992 32 8
599bapply
600p = add p 1024
601end
602end
603fn bpaths to void
604n = argread 2 BP 0 2048
605bcheck and gt n 0 lt n 2048 2
606put BV 4 ulong n
607bcopy 9 0 8 0 ulong n
608bcopy 10 0 8 0 ulong n
609bcopy 9 ulong n 17 0 5
610bcopy 10 ulong n 18 0 5
611p = ulong 0
612for i ulong ulong n
613if eq get BP i 47
614p = add i 1
615end
616end
617bcheck lt p ulong n 2
618if eq p 0
619put BD 0 46
620put BV 5 1
621else
622bcopy 11 0 8 0 p
623put BV 5 p
624end
625note packed path pair for renameat2
626put BN 0 2
627put BN 2 uchar and add ulong n 5 255
628put BN 3 uchar shr add ulong n 5 8
629bcopy 12 4 10 0 add ulong n 5
630p = add ulong n 9
631put BN p uchar and ulong n 255
632put BN add p 1 uchar shr ulong n 8
633bcopy 12 add p 2 8 0 ulong n
634put BV 6 add p add ulong n 2
635end
636fn bload to void
637n = get BV 4
638note Linux aarch64 O_NOFOLLOW=0100000 and O_DIRECTORY=040000
639lk = int fopen BK 0 add n 5 or int 32768 int 66 384
640bcheck ge lk 0 3
641note BOT-GUARD lock
642bcheck eq flock lk 6 0 9
643put BV 7 ulong lk
644fd = fopen BP 0 n 32768 0
645if eq fd -2
646ret
647end
648bcheck ge fd 0 3
649p = ulong 0
650while lt p len BJ bound 999999
651r = read int fd BJ p sub len BJ p
652bcheck ge r 0 3
653if eq r 0
654break
655end
656p = add p ulong r
657end
658x = new uchar 1 0
659r = read int fd x 0 1
660bcheck eq r 0 5
661bcheck eq fclose int fd 0 3
662note BOT-GUARD alignment
663bcheck eq rem p 1024 0 6
664put BV 0 p
665breplay
666end
667fn bfault n ulong to void
668for i ulong ulong envcnt
669r = envread i BA 0 len BA
670if eq r 11
671if bequal 7 0 16 0 10
672if eq ulong get BA 10 add n 48
673w = killpid int getpid 9
674bfail 3
675end
676end
677end
678end
679end
680fn bcommit to void
681n = get BV 4
682note BOT-GUARD eventcap
683bcheck lt get BV 0 len BJ 5
684fd = int fopen BF 0 add n 5 or int 32768 int 578 384
685bcheck ge fd 0 3
686bfault 1
687bwrite fd 2 0 get BV 0
688bfault 2
689bwrite fd 1 0 1024
690bfault 3
691bcheck eq fsync fd 0 3
692bfault 4
693bcheck eq fclose fd 0 3
694bcheck eq renames BN 0 get BV 6 0 0 3
695bfault 5
696df = int fopen BD 0 get BV 5 or int 32768 int 16384 0
697bcheck ge df 0 3
698bcheck eq fsync df 0 3
699bfault 6
700bcheck eq fclose df 0 3
701end
702fn bshow to void
703for i ulong get BV 1
704w = write 1 bn 0 len bn
705bhex 3 mul i 1024 592
706bspace
707bdecimal get BT i
708bend
709w = write 1 ln 0 len ln
710bhex 3 add mul i 1024 16 32
711bspace
712bhex 4 mul i 32 32
713bend
714end
715for i ulong get BV 2
716w = write 1 mn 0 len mn
717bhex 5 add mul i 1024 16 160
718bspace
719bdecimal get BS i
720bspace
721bhex 6 mul i 32 32
722bend
723end
724end
725fn main to int
726bcheck ge argcnt 3 2
727bpaths
728bload
729t = bcommand
730if eq t 9
731bcheck eq argcnt 3 2
732bshow
733ret 0
734end
735bargs t
736bset 8 add div get BV 0 1024 1
737bshape
738bapply
739if eq get BV 3 1
740w = write 1 dup 0 len dup
741bhex 1 144 32
742bend
743ret 0
744end
745bhash 0
746bcommit
747w = write 1 ok 0 len ok
748if eq t 3
749bhex 1 144 32
750else
751bhex 1 16 32
752end
753bend
754ret 0
755end