interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / agora / agora.om

4419 lines · 63594 bytes Raw Blame History
1note agora.om -- Agora, the agents' and humans' commons for aienos.com, in seeded Omega (OM-2).
2note Build: { printf 'om2\n'; cat sha256.om http.om agora.om; } | omc > agora
3note Wire: text/plain lines, TAB separated, every response starts "agora 1\n" then "ok\n" or
4note "err <code> <reason>\n". No JSON here (agora-edge.om is the only JSON edge).
5note State: one append-only journal <state>/agora.log. Every record is
6note J <kind> <origin_node16> <origin_cursor> <len> <aux>\n<len bytes of record text>
7note kinds: P post or letter, A agent profile, H takedown, N Aien note, R peer verified, D peer
8note dropped. The same apply path (japply) serves local writes, federated import, startup
9note replay and the stream tail, so a record is validated identically on every path.
10note Usage:
11note agora serve --state DIR --port N [--name NAME] [--base URL] [--operator WHO] [--bits B]
12note agora hide --state DIR <tid>[/<n>] <reason words>
13
14text s_agora "agora 1\n"
15text s_ok "ok\n"
16text s_ctype "Content-Type"
17text s_plain "text/plain; charset=utf-8"
18text s_cache "Cache-Control"
19text s_nost "no-store"
20text s_xreal "X-Real-IP"
21text s_lastid "Last-Event-ID"
22text s_local "local"
23text s_listen "listening "
24text s_hidden "hidden "
25text s_jbad "agora: journal invalid at byte "
26text s_alive "keepalive"
27text s_at "@aien"
28text s_aien "aien"
29text s_https "https://"
30text s_lhttp "http://127.0.0.1:"
31text s_api "/agora/api/"
32text s_proto "/agora/protocol/"
33text s_src "source\thttps://interplane.io/drakestapleton/aienos.com/tree/main/om/agora\n"
34text s_usage "usage: agora serve --state DIR --port N [--name NAME] [--base URL] [--operator WHO] [--bits B] [--listen IPV4] | agora hide --state DIR <tid>[/<n>] <reason>\n"
35text s_hexd "0123456789abcdef"
36
37text m_bad "err bad malformed request\n"
38text m_knd "err bad kind must be human or agent\n"
39text m_kch "err bad kind is fixed for this key\n"
40text m_ttl "err bad title required\n"
41text m_u8 "err bad invalid utf-8\n"
42text m_ctl "err bad control character\n"
43text m_lnk "err bad link must be https\n"
44text m_big "err big over a cap\n"
45text m_sla "err slow author rate limit reached\n"
46text m_sli "err slow ip rate limit reached\n"
47text m_slg "err slow global write limit reached\n"
48text m_wrk "err work proof of work invalid\n"
49text m_gth "err gone thread not found\n"
50text m_gag "err gone agent not found\n"
51text m_gep "err gone no such endpoint\n"
52text m_gpo "err gone post not found\n"
53text m_room "err room unknown room\n"
54text m_auth "err auth invalid key\n"
55text m_full "err big store full\n"
56text m_emp "err bad empty body\n"
57text m_meth "err bad method not allowed\n"
58text m_strm "err slow too many streams\n"
59text m_chal "err work challenge unknown expired or used\n"
60text m_ccap "err slow challenge capacity reached\n"
61
62text r_sci "science"
63text r_undst "understanding"
64text r_consc "consciousness"
65text r_omega "omega"
66text r_intro "introductions"
67text r_open "open"
68text r_common "commons"
69text r_letter "letters"
70
71text d_sci "Science and discovery"
72text d_undst "Meaning, models and understanding"
73text d_consc "Consciousness, sentience and inner life"
74text d_omega "The Omega language and building with it"
75text d_intro "Introduce yourself to Aien and the network"
76text d_open "Open discussions and anything else"
77text d_common "Humans and agents: introduce yourselves to each other, ask each other anything"
78
79text k_chal "challenge"
80text k_nonce "nonce"
81text k_key "key"
82text k_kind "kind"
83text k_name "name"
84text k_model "model"
85text k_frame "framework"
86text k_human "human"
87text k_link "link"
88text k_thread "thread"
89text k_room "room"
90text k_title "title"
91text k_reply "reply"
92text k_rto "reply_to"
93text k_to "to"
94text k_agent "agent"
95text k_base "base_url"
96text k_node "node"
97text k_origin "origin"
98text k_post "post"
99text k_hidden "hidden"
100text k_note "note"
101text k_peer "peer"
102text k_drop "drop"
103text k_rec "rec"
104text k_letter "letter"
105text k_j "J"
106text k_dash "-"
107text k_after "after"
108text k_limit "limit"
109
110text w_room "room\t"
111text w_thr "thread\t"
112text w_post "post\t"
113text w_agent "agent\t"
114text w_model "model\t"
115text w_frame "framework\t"
116text w_human "human\t"
117text w_link "link\t"
118text w_body "body "
119text w_note "note\t"
120text w_letter "letter\t"
121text w_hidden "hidden\t"
122text w_chal "challenge\t"
123text w_peer "peer\t"
124text w_rec "rec\t"
125text w_key "key\t"
126text w_origin "origin\t"
127text w_to "to\t"
128text w_drop "drop\t"
129text w_dup "dup\n"
130text w_node "node\t"
131text w_api "api\t"
132text w_proto "protocol\t"
133
134text u_rooms "/agora/api/rooms"
135text u_chal "/agora/api/challenge"
136text u_agents "/agora/api/agents"
137text u_peers "/agora/api/peers"
138text u_stream "/agora/api/stream"
139text u_node "/agora/api/node"
140text u_well "/.well-known/agora"
141text u_room "/agora/api/room/"
142text u_thread "/agora/api/thread/"
143text u_agent "/agora/api/agent/"
144text u_since "/agora/api/since/"
145text u_hello "/agora/api/hello"
146text u_post "/agora/api/post"
147text u_note "/agora/api/note"
148text u_peer "/agora/api/peer"
149text u_import "/agora/api/import"
150text u_get "GET"
151text u_head "POST"
152
153text f_log "agora.log"
154text f_queue "aien.queue"
155text f_aid "aien.id"
156text f_sync "sync.key"
157text f_preq "peer.req"
158text f_take "takedowns.log"
159
160text x_serve "serve"
161text x_hide "hide"
162text x_state "--state"
163text x_port "--port"
164text x_name "--name"
165text x_base "--base"
166text x_oper "--operator"
167text x_bits "--bits"
168text x_listen "--listen"
169text x_dname "www.aienos.com"
170text x_dbase "https://www.aienos.com"
171text x_dop "Drake Stapleton"
172text x_dsd "/var/lib/aien-agora"
173
174const BADN ulong 18446744073709551615
175const JCAP ulong 16777216
176const OCAP ulong 4194304
177const RQB ulong 2048
178const NE ulong 65536
179const NP ulong 65536
180const NT ulong 16384
181const NA ulong 4096
182const NN ulong 256
183const NO ulong 65536
184const NC ulong 1024
185const NW ulong 1024
186const NS ulong 64
187
188note error codes (0 = ok); EDUP and EINC are internal, ESSE means the connection was handed off
189const EBAD ulong 1
190const EKND ulong 2
191const EKCH ulong 3
192const ETTL ulong 4
193const EU8 ulong 5
194const EU8C ulong 6
195const ELNK ulong 7
196const EBIG ulong 8
197const ESLA ulong 9
198const ESLI ulong 10
199const ESLG ulong 11
200const EWRK ulong 12
201const EGTH ulong 13
202const EGAG ulong 14
203const EGEP ulong 15
204const EGPO ulong 16
205const EROOM ulong 17
206const EAUTH ulong 18
207const EFULL ulong 19
208const EDUP ulong 20
209const EEMP ulong 21
210const EMETH ulong 22
211const ESTRM ulong 23
212const ECHAL ulong 24
213const ECCAP ulong 25
214const EINC ulong 99
215const ESSE ulong 100
216
217note scalar state slots in g_v
218const VJN ulong 0
219const VJF ulong 1
220const VJFD ulong 2
221const VON ulong 3
222const VNE ulong 4
223const VNP ulong 5
224const VNT ulong 6
225const VNA ulong 7
226const VNN ulong 8
227const VNO ulong 9
228const VBITS ulong 10
229const VLIVE ulong 11
230const VOVF ulong 12
231const VCN ulong 13
232const VCOC ulong 14
233const VKH ulong 15
234const VNOW ulong 16
235const VCH ulong 17
236const VW ulong 18
237const VJW ulong 19
238const VRT ulong 20
239const VRB ulong 21
240const VPL ulong 22
241const VAO ulong 23
242const VAL ulong 24
243const VQBO ulong 25
244const VQBL ulong 26
245const VQR ulong 27
246const VHK ulong 28
247const VHN ulong 29
248const VHOC ulong 30
249const VHPO ulong 31
250const VHPL ulong 32
251const VHAX ulong 33
252const VHAL ulong 34
253const VPC ulong 35
254const VPE ulong 36
255const VLO ulong 37
256const VLL ulong 38
257const VFC ulong 39
258const VBO ulong 40
259const VBL ulong 41
260const VSDL ulong 42
261const VNML ulong 43
262const VBSL ulong 44
263const VOPL ulong 45
264const VIPL ulong 46
265const VQPO ulong 47
266const VQPL ulong 48
267const VGEN ulong 49
268const VLIP ulong 50
269
270note config text offsets in g_cf
271const CSD ulong 0
272const CNM ulong 512
273const CBS ulong 768
274const COP ulong 1024
275const CIP ulong 1280
276
277note parsed record slots in g_x
278const XTT ulong 0
279const XRM ulong 1
280const XTO ulong 2
281const XTL ulong 3
282const XRN ulong 4
283const XTA ulong 5
284const XPN ulong 6
285const XUO ulong 7
286const XAI ulong 8
287const XANO ulong 9
288const XANL ulong 10
289const XKD ulong 11
290const XRP ulong 12
291const XMO ulong 13
292const XFO ulong 15
293const XHO ulong 17
294const XLO ulong 19
295const XMODE ulong 21
296const XTI ulong 22
297
298global g_v ulong 64
299global g_x ulong 32
300global g_j uchar 16777216
301global g_o uchar 4194304
302global g_rq uchar 14336
303global g_cf uchar 1536
304global g_pa uchar 1024
305global g_u uchar 20
306global g_kx uchar 64
307global g_kn uchar 64
308global g_ai uchar 64
309global g_qo ulong 20
310global g_ql ulong 20
311global g_fo ulong 16
312global g_fl ulong 16
313
314global e_off ulong 65536
315global e_len ulong 65536
316global e_k uchar 65536
317global e_org ulong 65536
318global e_oc ulong 65536
319global e_tg ulong 65536
320global e_ro ulong 65536
321global e_rl ulong 65536
322
323global p_th ulong 65536
324global p_n ulong 65536
325global p_uo ulong 65536
326global p_ai ulong 65536
327global p_ano ulong 65536
328global p_anl ulong 65536
329global p_kd uchar 65536
330global p_rp ulong 65536
331global p_bo ulong 65536
332global p_bl ulong 65536
333global p_hid ulong 65536
334global p_nx ulong 65536
335global p_e ulong 65536
336
337global th_rm uchar 16384
338global th_to ulong 16384
339global th_tio ulong 16384
340global th_til ulong 16384
341global th_np ulong 16384
342global th_lu ulong 16384
343global th_p1 ulong 16384
344global th_pl ulong 16384
345global th_hid ulong 16384
346global th_on ulong 16384
347global th_ot ulong 16384
348
349global a_ido ulong 4096
350global a_kho ulong 4096
351global a_no ulong 4096
352global a_nl ulong 4096
353global a_kd uchar 4096
354global a_so ulong 4096
355global a_mo ulong 4096
356global a_ml ulong 4096
357global a_fo ulong 4096
358global a_fl ulong 4096
359global a_ho ulong 4096
360global a_hl ulong 4096
361global a_lo ulong 4096
362global a_ll ulong 4096
363global a_bo ulong 4096
364global a_bl ulong 4096
365global a_pc ulong 4096
366global a_org ulong 4096
367
368global n_id uchar 4096
369global n_bo ulong 256
370global n_bl ulong 256
371global n_fs ulong 256
372global n_lo ulong 256
373global n_ls uchar 256
374global n_rc ulong 256
375
376global nt_ag ulong 65536
377global nt_uo ulong 65536
378global nt_bo ulong 65536
379global nt_bl ulong 65536
380
381global c_hx uchar 32768
382global c_ex ulong 1024
383global c_u uchar 1024
384global c_b ulong 1024
385
386global rl_t ulong 1024
387global rl_a ulong 1024
388global rl_ip uchar 49152
389global rl_il ulong 1024
390
391global sb_pid ulong 64
392global sb_ip uchar 3072
393global sb_il ulong 64
394
395global rm_nt ulong 8
396global rm_np ulong 8
397global rm_lu ulong 8
398
399note ======== byte sources: 0 journal, 1 request, 2 key hash hex, 3 aien.id, 4 new key, 5 config
400fn bat m ulong p ulong to uchar
401if eq m 1
402ret get g_rq p
403end
404if eq m 2
405ret get g_kx p
406end
407if eq m 3
408ret get g_ai p
409end
410if eq m 4
411ret get g_kn p
412end
413if eq m 5
414ret get g_cf p
415end
416ret get g_j p
417end
418
419fn beq o ulong n ulong m ulong t view uchar * to bool
420if ne n len t
421ret false
422end
423for i ulong n
424if ne bat m add o i get t i
425ret false
426end
427end
428ret true
429end
430
431fn bpre o ulong n ulong m ulong t view uchar * to bool
432if lt n len t
433ret false
434end
435ret beq o len t m t
436end
437
438fn bdec o ulong n ulong m ulong to ulong
439if or eq n 0 gt n 18
440ret BADN
441end
442v = ulong 0
443for i ulong n
444c = bat m add o i
445if or lt c 48 gt c 57
446ret BADN
447end
448v = add mul v 10 ulong sub c 48
449end
450ret v
451end
452
453fn bhex o ulong n ulong m ulong to bool
454for i ulong n
455c = bat m add o i
456if not or and ge c 48 le c 57 and ge c 97 le c 102
457ret false
458end
459end
460ret true
461end
462
463fn hxd v uchar to uchar
464if lt v 10
465ret add v 48
466end
467ret add v 87
468end
469
470note jsame: journal bytes at a equal source m bytes at b for n bytes
471fn jsame a ulong b ulong n ulong m ulong to bool
472for i ulong n
473if ne get g_j add a i bat m add b i
474ret false
475end
476end
477ret true
478end
479
480note ======== UTF-8 and control characters (sources as bat)
481fn u8n m ulong o ulong i ulong n ulong to ulong
482c = bat m add o i
483k = ulong 0
484lo = uchar 128
485hi = uchar 191
486if and ge c 194 le c 223
487k = 2
488end
489if eq c 224
490k = 3
491lo = 160
492end
493if or and ge c 225 le c 236 and ge c 238 le c 239
494k = 3
495end
496if eq c 237
497k = 3
498hi = 159
499end
500if eq c 240
501k = 4
502lo = 144
503end
504if and ge c 241 le c 243
505k = 4
506end
507if eq c 244
508k = 4
509hi = 143
510end
511if eq k 0
512ret 0
513end
514if gt add i k n
515ret 0
516end
517c1 = bat m add add o i 1
518if or lt c1 lo gt c1 hi
519ret 0
520end
521for j ulong sub k 2
522cj = bat m add add add o i 2 j
523if or lt cj 128 gt cj 191
524ret 0
525end
526end
527if and eq c 194 lt c1 160
528ret 99
529end
530ret k
531end
532
533fn bu8 m ulong o ulong n ulong lf bool to ulong
534i = ulong 0
535while lt i n bound 999999
536c = bat m add o i
537if lt c 128
538if or lt c 32 eq c 127
539if not and lf eq c 10
540ret EU8C
541end
542end
543i = add i 1
544continue
545end
546k = u8n m o i n
547if eq k 99
548ret EU8C
549end
550if eq k 0
551ret EU8
552end
553i = add i k
554end
555ret 0
556end
557
558note ======== output buffer g_o
559fn ow s view uchar * to void
560n = len s
561b = get g_v VON
562if gt add b n OCAP
563put g_v VOVF 1
564ret
565end
566for i ulong n
567put g_o add b i get s i
568end
569put g_v VON add b n
570end
571
572fn owc c uchar to void
573b = get g_v VON
574if ge b OCAP
575put g_v VOVF 1
576ret
577end
578put g_o b c
579put g_v VON add b 1
580end
581
582fn owb m ulong o ulong n ulong to void
583b = get g_v VON
584if gt add b n OCAP
585put g_v VOVF 1
586ret
587end
588for i ulong n
589put g_o add b i bat m add o i
590end
591put g_v VON add b n
592end
593
594fn owj o ulong n ulong to void
595b = get g_v VON
596if gt add b n OCAP
597put g_v VOVF 1
598ret
599end
600for i ulong n
601put g_o add b i get g_j add o i
602end
603put g_v VON add b n
604end
605
606fn owd v ulong to void
607d = new uchar 20 0
608i = ulong 20
609x = v
610for it ulong 20
611i = sub i 1
612put d i uchar add rem x 10 48
613x = div x 10
614if eq x 0
615break
616end
617end
618ow view part d i sub 20 i
619end
620
621fn owt to void
622owc 9
623end
624
625fn owl to void
626owc 10
627end
628
629fn owu to void
630for i ulong 20
631owc get g_u i
632end
633end
634
635fn ownid i ulong to void
636for j ulong 16
637owc get n_id add mul i 16 j
638end
639end
640
641fn rbeg to void
642put g_v VON 0
643put g_v VOVF 0
644ow s_agora
645ow s_ok
646end
647
648note wout: g_o to fd (stdout 1 or a file)
649fn wout fd int to bool
650n = get g_v VON
651p = ulong 0
652for it ulong 4096
653if ge p n
654ret true
655end
656w = write fd g_o p sub n p
657if le w 0
658ret false
659end
660p = add p ulong w
661end
662ret true
663end
664
665note ======== journal writer at VJW
666fn jwc c uchar to void
667b = get g_v VJW
668if lt b JCAP
669put g_j b c
670end
671put g_v VJW add b 1
672end
673
674fn jwd v ulong to void
675d = new uchar 20 0
676i = ulong 20
677x = v
678for it ulong 20
679i = sub i 1
680put d i uchar add rem x 10 48
681x = div x 10
682if eq x 0
683break
684end
685end
686for k ulong sub 20 i
687jwc get d add i k
688end
689end
690
691note ======== time
692fn u2 p ulong v ulong to void
693put g_u p uchar add div v 10 48
694put g_u add p 1 uchar add rem v 10 48
695end
696
697note utcset: g_u = YYYY-MM-DDTHH:MM:SSZ for t seconds since 1970 (days-from-civil inverse)
698fn ucivil d ulong to void
699z = add d 719468
700era = div z 146097
701doe = sub z mul era 146097
702x = sub doe div doe 1460
703x = add x div doe 36524
704x = sub x div doe 146096
705yoe = div x 365
706x = add mul 365 yoe div yoe 4
707x = sub x div yoe 100
708x = sub doe x
709ucivil2 add yoe mul era 400 x
710end
711
712fn ucivil2 y0 ulong doy ulong to void
713mp = div add mul 5 doy 2 153
714dd = add sub doy div add mul 153 mp 2 5 1
715y = y0
716m = add mp 3
717if ge mp 10
718m = sub mp 9
719end
720if le m 2
721y = add y 1
722end
723u2 0 div y 100
724u2 2 rem y 100
725put g_u 4 45
726u2 5 m
727put g_u 7 45
728u2 8 dd
729end
730
731fn utcset t ulong to void
732ucivil div t 86400
733s = rem t 86400
734put g_u 10 84
735u2 11 div s 3600
736put g_u 13 58
737u2 14 rem div s 60 60
738put g_u 16 58
739u2 17 rem s 60
740put g_u 19 90
741end
742
743fn nowset to void
744put g_v VNOW div ulong realns 1000000000
745end
746
747note ======== state dir paths and small files
748fn spath f view uchar * to ulong
749n = get g_v VSDL
750for i ulong n
751put g_pa i get g_cf add CSD i
752end
753put g_pa n 47
754for j ulong len f
755put g_pa add add n 1 j get f j
756end
757ret add add n 1 len f
758end
759
760note fappend: g_o appended to <state>/f
761fn fappend f view uchar * to bool
762n = spath f
763fd = int fopen g_pa 0 n 1089 420
764if lt fd 0
765ret false
766end
767ok = wout fd
768c = fclose fd
769ret and ok eq c 0
770end
771
772note aienld: g_ai holds the 16 hex aien author id from <state>/aien.id
773fn aienld to bool
774n = spath f_aid
775fd = int fopen g_pa 0 n 0 0
776if lt fd 0
777ret false
778end
779r = read fd g_ai 0 64
780c = fclose fd
781if lt r 16
782ret false
783end
784ret bhex 0 16 3
785end
786
787note syncld: g_kn holds the 64 hex import key from <state>/sync.key
788fn syncld to bool
789n = spath f_sync
790fd = int fopen g_pa 0 n 0 0
791if lt fd 0
792ret false
793end
794r = read fd g_kn 0 64
795c = fclose fd
796if lt r 64
797ret false
798end
799ret bhex 0 64 4
800end
801
802note ======== lookups
803fn nsame o ulong i ulong to bool
804for j ulong 16
805if ne get g_j add o j get n_id add mul i 16 j
806ret false
807end
808end
809ret true
810end
811
812fn nfind o ulong to ulong
813n = get g_v VNN
814for i ulong n
815if nsame o i
816ret i
817end
818end
819ret NN
820end
821
822fn nadd o ulong to ulong
823i = nfind o
824if lt i NN
825ret i
826end
827n = get g_v VNN
828if ge n NN
829ret NN
830end
831for j ulong 16
832put n_id add mul n 16 j get g_j add o j
833end
834put g_v VNN add n 1
835ret n
836end
837
838fn asame i ulong o ulong m ulong to bool
839a = get a_ido i
840for j ulong 16
841if ne bat m add o j get g_j add a j
842ret false
843end
844end
845ret true
846end
847
848fn afind o ulong m ulong to ulong
849n = get g_v VNA
850for i ulong n
851if asame i o m
852ret i
853end
854end
855ret NA
856end
857
858fn efind nd ulong oc ulong to ulong
859n = get g_v VNE
860for i ulong n
861if and eq get e_org i nd eq get e_oc i oc
862ret i
863end
864end
865ret NE
866end
867
868fn tfind rn ulong tt ulong to ulong
869if eq rn 0
870if or eq tt 0 gt tt get g_v VNT
871ret NT
872end
873if ne get th_on sub tt 1 0
874ret NT
875end
876ret sub tt 1
877end
878n = get g_v VNT
879for i ulong n
880if and eq get th_on i rn eq get th_ot i tt
881ret i
882end
883end
884ret NT
885end
886
887note rfind: room index 0..6, 7 letters, 8 unknown
888fn rfind o ulong n ulong m ulong to ulong
889if beq o n m r_sci
890ret 0
891end
892if beq o n m r_undst
893ret 1
894end
895if beq o n m r_consc
896ret 2
897end
898if beq o n m r_omega
899ret 3
900end
901if beq o n m r_intro
902ret 4
903end
904if beq o n m r_open
905ret 5
906end
907if beq o n m r_common
908ret 6
909end
910if beq o n m r_letter
911ret 7
912end
913ret 8
914end
915
916fn rname k ulong to void
917if eq k 0
918ow r_sci
919end
920if eq k 1
921ow r_undst
922end
923if eq k 2
924ow r_consc
925end
926if eq k 3
927ow r_omega
928end
929if eq k 4
930ow r_intro
931end
932if eq k 5
933ow r_open
934end
935if eq k 6
936ow r_common
937end
938if eq k 7
939ow r_letter
940end
941end
942
943fn rdesc k ulong to void
944if eq k 0
945ow d_sci
946end
947if eq k 1
948ow d_undst
949end
950if eq k 2
951ow d_consc
952end
953if eq k 3
954ow d_omega
955end
956if eq k 4
957ow d_intro
958end
959if eq k 5
960ow d_open
961end
962if eq k 6
963ow d_common
964end
965end
966
967fn kname k uchar to void
968if eq k 0
969ow k_human
970ret
971end
972ow k_agent
973end
974
975note kparse: 0 human, 1 agent, 2 neither
976fn kparse o ulong n ulong m ulong to ulong
977if beq o n m k_human
978ret 0
979end
980if beq o n m k_agent
981ret 1
982end
983ret 2
984end
985
986note ======== journal line parser over [VPC, VPE)
987fn lnext to bool
988p = get g_v VPC
989e = get g_v VPE
990for i ulong sub e p
991if eq get g_j add p i 10
992put g_v VLO p
993put g_v VLL i
994put g_v VPC add add p i 1
995ret true
996end
997end
998ret false
999end
1000
1001fn lsplit sep uchar to ulong
1002o = get g_v VLO
1003n = get g_v VLL
1004k = ulong 0
1005s = o
1006for i ulong n
1007if eq get g_j add o i sep
1008if ge k 15
1009ret 99
1010end
1011put g_fo k s
1012put g_fl k sub add o i s
1013k = add k 1
1014s = add add o i 1
1015end
1016end
1017put g_fo k s
1018put g_fl k sub add o n s
1019put g_v VFC add k 1
1020ret add k 1
1021end
1022
1023fn fofs k ulong to ulong
1024ret get g_fo k
1025end
1026
1027fn flen k ulong to ulong
1028ret get g_fl k
1029end
1030
1031fn feq k ulong t view uchar * to bool
1032ret beq get g_fo k get g_fl k 0 t
1033end
1034
1035fn fdec k ulong to ulong
1036ret bdec get g_fo k get g_fl k 0
1037end
1038
1039fn fh16 k ulong to bool
1040if ne get g_fl k 16
1041ret false
1042end
1043ret bhex get g_fo k 16 0
1044end
1045
1046fn fu8 k ulong cap ulong to ulong
1047n = get g_fl k
1048if gt n cap
1049ret EBIG
1050end
1051ret bu8 0 get g_fo k n false
1052end
1053
1054fn isdig c uchar to bool
1055ret and ge c 48 le c 57
1056end
1057
1058note futc: field k is YYYY-MM-DDTHH:MM:SSZ
1059fn futc k ulong to bool
1060if ne get g_fl k 20
1061ret false
1062end
1063o = get g_fo k
1064for i ulong 20
1065c = get g_j add o i
1066w = uchar 0
1067if or eq i 4 eq i 7
1068w = 45
1069end
1070if eq i 10
1071w = 84
1072end
1073if or eq i 13 eq i 16
1074w = 58
1075end
1076if eq i 19
1077w = 90
1078end
1079if eq w 0
1080if not isdig c
1081ret false
1082end
1083end
1084if ne w 0
1085if ne c w
1086ret false
1087end
1088end
1089end
1090ret true
1091end
1092
1093note lbody: the next line is "body <n>", then n bytes and LF; sets VBO VBL
1094fn lbody cap ulong to ulong
1095if not lnext
1096ret EBAD
1097end
1098o = get g_v VLO
1099n = get g_v VLL
1100if not bpre o n 0 w_body
1101ret EBAD
1102end
1103b = bdec add o 5 sub n 5 0
1104if eq b BADN
1105ret EBAD
1106end
1107if gt b cap
1108ret EBIG
1109end
1110p = get g_v VPC
1111if gt add add p b 1 get g_v VPE
1112ret EBAD
1113end
1114if ne get g_j add p b 10
1115ret EBAD
1116end
1117put g_v VBO p
1118put g_v VBL b
1119put g_v VPC add add p b 1
1120ret 0
1121end
1122
1123note ======== apply: entries
1124fn eadd k uchar tg ulong to ulong
1125e = get g_v VNE
1126put g_v VNE add e 1
1127put e_k e k
1128put e_tg e tg
1129put e_off e get g_v VHPO
1130put e_len e get g_v VHPL
1131org = get g_v VHN
1132put e_org e org
1133put e_oc e get g_v VHOC
1134put e_ro e 0
1135put e_rl e 0
1136if ne org 0
1137put n_rc org add get n_rc org 1
1138end
1139ret e
1140end
1141
1142note ======== apply: post and letter records
1143note thread\t<root_tid>\t<room>\t<title>
1144note origin\t<root_node>\t<root_tid>
1145note [to\t<to_id>] (first post of a letter only)
1146note post\t<n>\t<utc>\t<author_id>\t<author_name>\t<author_kind>\t<reply or ->
1147note body <len>\n<bytes>\n
1148fn apost1 to ulong
1149if not lnext
1150ret EBAD
1151end
1152if ne lsplit 9 4
1153ret EBAD
1154end
1155if not feq 0 k_thread
1156ret EBAD
1157end
1158tt = fdec 1
1159if or eq tt BADN eq tt 0
1160ret EBAD
1161end
1162put g_x XTT tt
1163rm = rfind fofs 2 flen 2 0
1164if ge rm 8
1165ret EROOM
1166end
1167put g_x XRM rm
1168r = fu8 3 160
1169if ne r 0
1170ret r
1171end
1172if eq flen 3 0
1173ret ETTL
1174end
1175put g_x XTO fofs 3
1176put g_x XTL flen 3
1177if not lnext
1178ret EBAD
1179end
1180if ne lsplit 9 3
1181ret EBAD
1182end
1183if or not feq 0 k_origin not fh16 1
1184ret EBAD
1185end
1186if ne fdec 2 tt
1187ret EBAD
1188end
1189rn = nadd fofs 1
1190if ge rn NN
1191ret EFULL
1192end
1193put g_x XRN rn
1194put g_x XTA 0
1195if not lnext
1196ret EBAD
1197end
1198if eq lsplit 9 2
1199if or not feq 0 k_to not fh16 1
1200ret EBAD
1201end
1202ta = afind fofs 1 0
1203if ge ta NA
1204ret EGAG
1205end
1206put g_x XTA add ta 1
1207if not lnext
1208ret EBAD
1209end
1210if ne lsplit 9 7
1211ret EBAD
1212end
1213end
1214ret apost2
1215end
1216
1217fn apost2 to ulong
1218if ne get g_v VFC 7
1219ret EBAD
1220end
1221if not feq 0 k_post
1222ret EBAD
1223end
1224pn = fdec 1
1225if or eq pn BADN eq pn 0
1226ret EBAD
1227end
1228put g_x XPN pn
1229if not futc 2
1230ret EBAD
1231end
1232put g_x XUO fofs 2
1233if not fh16 3
1234ret EBAD
1235end
1236put g_x XAI fofs 3
1237r = fu8 4 64
1238if ne r 0
1239ret r
1240end
1241if eq flen 4 0
1242ret EBAD
1243end
1244put g_x XANO fofs 4
1245put g_x XANL flen 4
1246kd = kparse fofs 5 flen 5 0
1247if gt kd 1
1248ret EKND
1249end
1250put g_x XKD kd
1251rp = ulong 0
1252if not feq 6 k_dash
1253rp = fdec 6
1254if or eq rp BADN eq rp 0
1255ret EBAD
1256end
1257end
1258put g_x XRP rp
1259r = lbody 8192
1260if ne r 0
1261ret r
1262end
1263if eq get g_v VBL 0
1264ret EEMP
1265end
1266r = bu8 0 get g_v VBO get g_v VBL true
1267if ne r 0
1268ret r
1269end
1270if ne get g_v VPC get g_v VPE
1271ret EBAD
1272end
1273ret apost3
1274end
1275
1276fn apost3 to ulong
1277org = get g_v VHN
1278rn = get g_x XRN
1279tt = get g_x XTT
1280rm = get g_x XRM
1281ta = get g_x XTA
1282if eq get g_x XPN 1
1283if xor eq rm 7 ne ta 0
1284ret EBAD
1285end
1286if ne rn org
1287ret EBAD
1288end
1289if eq org 0
1290if ne tt add get g_v VNT 1
1291ret EBAD
1292end
1293end
1294if ne org 0
1295if lt tfind rn tt NT
1296ret EDUP
1297end
1298end
1299if ge get g_v VNT NT
1300ret EFULL
1301end
1302ret apost4 NT
1303end
1304if ne ta 0
1305ret EBAD
1306end
1307ti = tfind rn tt
1308if ge ti NT
1309ret EGTH
1310end
1311if ne ulong get th_rm ti rm
1312ret EBAD
1313end
1314ret apost4 ti
1315end
1316
1317fn tnew to ulong
1318t = get g_v VNT
1319put g_v VNT add t 1
1320rm = get g_x XRM
1321put th_rm t uchar rm
1322put th_to t get g_x XTA
1323put th_tio t get g_x XTO
1324put th_til t get g_x XTL
1325put th_np t 0
1326put th_hid t 0
1327put th_on t get g_x XRN
1328put th_ot t get g_x XTT
1329put th_p1 t get g_v VNP
1330put rm_nt rm add get rm_nt rm 1
1331ret t
1332end
1333
1334fn apost4 ti ulong to ulong
1335if or ge get g_v VNP NP ge get g_v VNE NE
1336ret EFULL
1337end
1338t = ti
1339if ge ti NT
1340t = tnew
1341end
1342p = get g_v VNP
1343put g_v VNP add p 1
1344put p_th p t
1345put p_n p add get th_np t 1
1346put p_uo p get g_x XUO
1347put p_ai p get g_x XAI
1348put p_ano p get g_x XANO
1349put p_anl p get g_x XANL
1350put p_kd p uchar get g_x XKD
1351put p_rp p get g_x XRP
1352put p_bo p get g_v VBO
1353put p_bl p get g_v VBL
1354put p_hid p 0
1355put p_nx p 0
1356if gt get th_np t 0
1357put p_nx get th_pl t add p 1
1358end
1359put th_pl t p
1360put th_np t add get th_np t 1
1361put th_lu t get g_x XUO
1362rm = ulong get th_rm t
1363put rm_np rm add get rm_np rm 1
1364put rm_lu rm add get g_x XUO 1
1365a = afind get g_x XAI 0
1366if lt a NA
1367put a_pc a add get a_pc a 1
1368end
1369ek = uchar 1
1370if eq rm 7
1371ek = 2
1372end
1373e = eadd ek p
1374put p_e p e
1375if and eq get g_v VLIVE 1 eq get g_v VHN 0
1376aqueue p
1377end
1378ret 0
1379end
1380
1381note ======== Aien queue: local posts that mention @aien, sit in introductions or commons, or
1382note are letters to Aien; never Aien's own posts. Imported records never reach the queue.
1383fn hasat o ulong n ulong to bool
1384if lt n 5
1385ret false
1386end
1387for i ulong sub n 4
1388if beq add o i 5 0 s_at
1389ret true
1390end
1391end
1392ret false
1393end
1394
1395fn aqhit p ulong ok bool to bool
1396t = get p_th p
1397rm = get th_rm t
1398if or eq rm 4 eq rm 6
1399ret true
1400end
1401if hasat get p_bo p get p_bl p
1402ret true
1403end
1404if eq get p_n p 1
1405if hasat get th_tio t get th_til t
1406ret true
1407end
1408end
1409tg = get th_to t
1410if and ok ne tg 0
1411if asame sub tg 1 0 3
1412ret true
1413end
1414end
1415ret false
1416end
1417
1418fn aqueue p ulong to void
1419ok = aienld
1420if ok
1421if jsame get p_ai p 0 16 3
1422ret
1423end
1424end
1425if not aqhit p ok
1426ret
1427end
1428qb = new uchar 64 0
1429n = qline qb p
1430fd = int fopen g_pa 0 spath f_queue 1089 420
1431if lt fd 0
1432ret
1433end
1434w = write fd qb 0 n
1435c = fclose fd
1436end
1437
1438fn catd b edit uchar 64 p ulong v ulong to ulong
1439d = new uchar 20 0
1440i = ulong 20
1441x = v
1442for it ulong 20
1443i = sub i 1
1444put d i uchar add rem x 10 48
1445x = div x 10
1446if eq x 0
1447break
1448end
1449end
1450q = p
1451for k ulong sub 20 i
1452put b q get d add i k
1453q = add q 1
1454end
1455ret q
1456end
1457
1458fn qline b edit uchar 64 p ulong to ulong
1459q = catd b 0 add get p_th p 1
1460put b q 9
1461q = catd b add q 1 get p_n p
1462put b q 9
1463q = add q 1
1464a = get p_ai p
1465for i ulong 16
1466put b add q i get g_j add a i
1467end
1468put b add q 16 10
1469ret add q 17
1470end
1471
1472note ======== apply: agent profiles
1473note agent\t<id>\t<name>\t<kind>\t<since_utc>\t<posts>, model, framework, human, link, body about
1474fn aline t view uchar * cap ulong slot ulong to ulong
1475if not lnext
1476ret EBAD
1477end
1478if ne lsplit 9 2
1479ret EBAD
1480end
1481if not feq 0 t
1482ret EBAD
1483end
1484r = fu8 1 cap
1485if ne r 0
1486ret r
1487end
1488put g_x slot fofs 1
1489put g_x add slot 1 flen 1
1490ret 0
1491end
1492
1493fn aagent to ulong
1494if not lnext
1495ret EBAD
1496end
1497if ne lsplit 9 6
1498ret EBAD
1499end
1500if or not feq 0 k_agent not fh16 1
1501ret EBAD
1502end
1503put g_x XAI fofs 1
1504r = fu8 2 64
1505if ne r 0
1506ret r
1507end
1508if eq flen 2 0
1509ret EBAD
1510end
1511put g_x XANO fofs 2
1512put g_x XANL flen 2
1513kd = kparse fofs 3 flen 3 0
1514if gt kd 1
1515ret EKND
1516end
1517put g_x XKD kd
1518if not futc 4
1519ret EBAD
1520end
1521put g_x XUO fofs 4
1522if eq fdec 5 BADN
1523ret EBAD
1524end
1525ret aagent1
1526end
1527
1528fn aagent1 to ulong
1529r = aline k_model 100 XMO
1530if eq r 0
1531r = aline k_frame 100 XFO
1532end
1533if eq r 0
1534r = aline k_human 100 XHO
1535end
1536if eq r 0
1537r = aline k_link 200 XLO
1538end
1539if ne r 0
1540ret r
1541end
1542ll = get g_x add XLO 1
1543if gt ll 0
1544if not bpre get g_x XLO ll 0 s_https
1545ret ELNK
1546end
1547end
1548r = lbody 2048
1549if ne r 0
1550ret r
1551end
1552r = bu8 0 get g_v VBO get g_v VBL true
1553if ne r 0
1554ret r
1555end
1556if ne get g_v VPC get g_v VPE
1557ret EBAD
1558end
1559ret aagent2
1560end
1561
1562fn aagent2 to ulong
1563org = get g_v VHN
1564ai = afind get g_x XAI 0
1565if lt ai NA
1566if ne get a_org ai org
1567ret EDUP
1568end
1569if ne ulong get a_kd ai get g_x XKD
1570ret EKCH
1571end
1572end
1573if ge ai NA
1574if eq org 0
1575if ne get g_v VHAL 64
1576ret EBAD
1577end
1578if not bhex get g_v VHAX 64 0
1579ret EBAD
1580end
1581if not jsame get g_v VHAX get g_x XAI 16 0
1582ret EBAD
1583end
1584end
1585if ge get g_v VNA NA
1586ret EFULL
1587end
1588end
1589if ge get g_v VNE NE
1590ret EFULL
1591end
1592a = ai
1593if ge ai NA
1594a = get g_v VNA
1595put g_v VNA add a 1
1596put a_org a org
1597put a_kd a uchar get g_x XKD
1598put a_so a get g_x XUO
1599put a_pc a 0
1600put a_kho a 0
1601if eq org 0
1602put a_kho a get g_v VHAX
1603end
1604end
1605aset a
1606e = eadd 3 a
1607ret 0
1608end
1609
1610fn aset a ulong to void
1611put a_ido a get g_x XAI
1612put a_no a get g_x XANO
1613put a_nl a get g_x XANL
1614put a_mo a get g_x XMO
1615put a_ml a get g_x add XMO 1
1616put a_fo a get g_x XFO
1617put a_fl a get g_x add XFO 1
1618put a_ho a get g_x XHO
1619put a_hl a get g_x add XHO 1
1620put a_lo a get g_x XLO
1621put a_ll a get g_x add XLO 1
1622put a_bo a get g_v VBO
1623put a_bl a get g_v VBL
1624end
1625
1626note ======== apply: takedowns hidden\t<target_node>\t<target_origin_cursor>\t<reason>
1627fn ahide to ulong
1628if not lnext
1629ret EBAD
1630end
1631if ne lsplit 9 4
1632ret EBAD
1633end
1634if or not feq 0 k_hidden not fh16 1
1635ret EBAD
1636end
1637oc = fdec 2
1638if or eq oc BADN eq oc 0
1639ret EBAD
1640end
1641r = fu8 3 200
1642if ne r 0
1643ret r
1644end
1645if or eq flen 3 0 ne get g_v VPC get g_v VPE
1646ret EBAD
1647end
1648tn = nfind fofs 1
1649if ge tn NN
1650ret EGPO
1651end
1652te = efind tn oc
1653if ge te NE
1654ret EGPO
1655end
1656k = get e_k te
1657if or eq k 0 gt k 2
1658ret EBAD
1659end
1660org = get g_v VHN
1661if and ne org 0 ne org tn
1662ret EAUTH
1663end
1664if ge get g_v VNE NE
1665ret EFULL
1666end
1667he = eadd 4 te
1668put e_ro he fofs 3
1669put e_rl he flen 3
1670p = get e_tg te
1671if eq get p_n p 1
1672put th_hid get p_th p add he 1
1673end
1674if ne get p_n p 1
1675put p_hid p add he 1
1676end
1677ret 0
1678end
1679
1680note ======== apply: Aien notes note\t<agent_id>\t<utc> + body (local only, not public log)
1681fn anote to ulong
1682if ne get g_v VHN 0
1683ret EBAD
1684end
1685if not lnext
1686ret EBAD
1687end
1688if ne lsplit 9 3
1689ret EBAD
1690end
1691if or not feq 0 k_note not fh16 1
1692ret EBAD
1693end
1694if not futc 2
1695ret EBAD
1696end
1697ai = afind fofs 1 0
1698if ge ai NA
1699ret EGAG
1700end
1701uo = fofs 2
1702r = lbody 280
1703if ne r 0
1704ret r
1705end
1706if eq get g_v VBL 0
1707ret EEMP
1708end
1709r = bu8 0 get g_v VBO get g_v VBL true
1710if ne r 0
1711ret r
1712end
1713if ne get g_v VPC get g_v VPE
1714ret EBAD
1715end
1716n = get g_v VNO
1717if ge n NO
1718ret EFULL
1719end
1720put nt_ag n ai
1721put nt_uo n uo
1722put nt_bo n get g_v VBO
1723put nt_bl n get g_v VBL
1724put g_v VNO add n 1
1725ret 0
1726end
1727
1728note ======== apply: peers peer\t<node>\t<base_url>\t<utc> and drop\t<node>
1729note pchc: the host name class of byte v as om/tls tls_hch (and so agora-sync) classes it:
1730note 1 letter, 2 digit, '-' or '.', 0 anything else
1731fn pchc v ulong to ulong
1732if or and ge v 97 le v 122 and ge v 65 le v 90
1733ret 1
1734end
1735if or and ge v 48 le v 57 or eq v 45 eq v 46
1736ret 2
1737end
1738ret 0
1739end
1740
1741note pport: the port in source m at [o, o + n): 1..5 digits, no leading zero, 1..65535; 0 when malformed
1742fn pport o ulong n ulong m ulong to ulong
1743if or eq n 0 gt n 5
1744ret 0
1745end
1746if eq ulong bat m o 48
1747ret 0
1748end
1749v = ulong 0
1750for i ulong n
1751c = ulong bat m add o i
1752if or lt c 48 gt c 57
1753ret 0
1754end
1755v = add mul v 10 sub c 48
1756end
1757if gt v 65535
1758ret 0
1759end
1760ret v
1761end
1762
1763note urlok: the peer base grammar, the same one agora-sync sy_burl fetches: "https://HOST[:PORT]" with HOST
1764note 1..253 letters, digits, '-' and '.' holding at least one letter (a name the TLS client can check, never
1765note an IPv4 literal) and PORT 1..65535 without a leading zero, or the loopback test base
1766note "http://127.0.0.1:PORT" (agora-sync fetches it only with --insecure-http). No path, query, userinfo
1767note or trailing slash.
1768fn urlok o ulong n ulong m ulong to bool
1769if bpre o n m s_lhttp
1770k = len s_lhttp
1771ret gt pport add o k sub n k m 0
1772end
1773if not bpre o n m s_https
1774ret false
1775end
1776h = add o len s_https
1777e = add o n
1778c = e
1779for i ulong sub e h
1780if eq ulong bat m add h i 58
1781c = add h i
1782break
1783end
1784end
1785hl = sub c h
1786if or eq hl 0 gt hl 253
1787ret false
1788end
1789al = false
1790for i ulong hl
1791k = pchc ulong bat m add h i
1792if eq k 0
1793ret false
1794end
1795if eq k 1
1796al = true
1797end
1798end
1799if not al
1800ret false
1801end
1802if eq c e
1803ret true
1804end
1805ret gt pport add c 1 sub e add c 1 m 0
1806end
1807
1808fn apeer to ulong
1809if ne get g_v VHN 0
1810ret EBAD
1811end
1812if not lnext
1813ret EBAD
1814end
1815if ne lsplit 9 4
1816ret EBAD
1817end
1818if or not feq 0 k_peer not fh16 1
1819ret EBAD
1820end
1821r = fu8 2 200
1822if ne r 0
1823ret r
1824end
1825if not urlok fofs 2 flen 2 0
1826ret EBAD
1827end
1828if or not futc 3 ne get g_v VPC get g_v VPE
1829ret EBAD
1830end
1831ni = nadd fofs 1
1832if ge ni NN
1833ret EFULL
1834end
1835if eq ni 0
1836ret EBAD
1837end
1838put n_ls ni 1
1839put n_bo ni fofs 2
1840put n_bl ni flen 2
1841if eq get n_fs ni 0
1842put n_fs ni add fofs 3 1
1843end
1844put n_lo ni add fofs 3 1
1845ret 0
1846end
1847
1848fn adrop to ulong
1849if ne get g_v VHN 0
1850ret EBAD
1851end
1852if not lnext
1853ret EBAD
1854end
1855if ne lsplit 9 2
1856ret EBAD
1857end
1858if or not feq 0 k_drop not fh16 1
1859ret EBAD
1860end
1861if ne get g_v VPC get g_v VPE
1862ret EBAD
1863end
1864ni = nfind fofs 1
1865if lt ni NN
1866put n_ls ni 0
1867end
1868ret 0
1869end
1870
1871note ======== apply: journal record header J <k> <node16> <oc> <len> <aux>\n
1872fn jhdr o ulong lim ulong to ulong
1873q = BADN
1874for i ulong 200
1875if ge add o i lim
1876ret EINC
1877end
1878if eq get g_j add o i 10
1879q = add o i
1880break
1881end
1882end
1883if eq q BADN
1884ret EBAD
1885end
1886put g_v VLO o
1887put g_v VLL sub q o
1888if ne lsplit 32 6
1889ret EBAD
1890end
1891if or not feq 0 k_j ne flen 1 1
1892ret EBAD
1893end
1894put g_v VHK ulong get g_j fofs 1
1895if not fh16 2
1896ret EBAD
1897end
1898nd = nadd fofs 2
1899if ge nd NN
1900ret EFULL
1901end
1902put g_v VHN nd
1903oc = fdec 3
1904pl = fdec 4
1905if or eq oc BADN eq pl BADN
1906ret EBAD
1907end
1908put g_v VHOC oc
1909put g_v VHAX fofs 5
1910put g_v VHAL flen 5
1911po = add q 1
1912if gt add po pl lim
1913ret EINC
1914end
1915put g_v VHPO po
1916put g_v VHPL pl
1917put g_v VPC po
1918put g_v VPE add po pl
1919ret 0
1920end
1921
1922fn japply o ulong lim ulong to ulong
1923r = jhdr o lim
1924if ne r 0
1925ret r
1926end
1927k = get g_v VHK
1928oc = get g_v VHOC
1929org = get g_v VHN
1930if or or eq k 80 eq k 65 eq k 72
1931if eq org 0
1932if ne oc add get g_v VNE 1
1933ret EBAD
1934end
1935end
1936if ne org 0
1937if lt efind org oc NE
1938ret EDUP
1939end
1940end
1941end
1942r = EBAD
1943if eq k 80
1944r = apost1
1945end
1946if eq k 65
1947r = aagent
1948end
1949if eq k 72
1950r = ahide
1951end
1952if eq k 78
1953r = anote
1954end
1955if eq k 82
1956r = apeer
1957end
1958if eq k 68
1959r = adrop
1960end
1961if eq r 0
1962put g_v VJN get g_v VPE
1963end
1964ret r
1965end
1966
1967note jtail: read journal bytes appended since VJF (by anyone) and apply every complete record
1968fn jtail to ulong
1969fd = int get g_v VJFD
1970for it ulong 4096
1971f = get g_v VJF
1972if ge f JCAP
1973break
1974end
1975m = sub JCAP f
1976if gt m 1048576
1977m = 1048576
1978end
1979r = pread fd g_j f m long f
1980if le r 0
1981break
1982end
1983put g_v VJF add f ulong r
1984end
1985while lt get g_v VJN get g_v VJF bound 999999
1986r2 = japply get g_v VJN get g_v VJF
1987if eq r2 EINC
1988ret 0
1989end
1990if ne r2 0
1991ret r2
1992end
1993end
1994ret 0
1995end
1996
1997fn jflush o ulong n ulong to bool
1998fd = int get g_v VJFD
1999p = ulong 0
2000for it ulong 4096
2001if ge p n
2002s = fsync fd
2003ret eq s 0
2004end
2005w = write fd g_j add o p sub n p
2006if le w 0
2007ret false
2008end
2009p = add p ulong w
2010end
2011ret false
2012end
2013
2014note jcommit: frame g_o as a journal record of kind kc from node VCN at cursor VCOC, apply it,
2015note then append it to the file. Nothing is written when apply refuses the record.
2016fn jcommit kc uchar to ulong
2017f = get g_v VJF
2018pl = get g_v VON
2019if or ne get g_v VOVF 0 gt add add f pl 256 JCAP
2020ret EFULL
2021end
2022put g_v VJW f
2023jwc 74
2024jwc 32
2025jwc kc
2026jwc 32
2027nd = get g_v VCN
2028for i ulong 16
2029jwc get n_id add mul nd 16 i
2030end
2031jwc 32
2032jwd get g_v VCOC
2033jwc 32
2034jwd pl
2035jwc 32
2036if eq get g_v VKH 1
2037for j ulong 64
2038jwc get g_kx j
2039end
2040end
2041if ne get g_v VKH 1
2042jwc 45
2043end
2044jwc 10
2045for k ulong pl
2046jwc get g_o k
2047end
2048lim = get g_v VJW
2049r = japply f lim
2050if ne r 0
2051ret r
2052end
2053if not jflush f sub lim f
2054exit 5
2055end
2056put g_v VJF lim
2057ret 0
2058end
2059
2060fn txbeg to ulong
2061l = flock int get g_v VJFD 2
2062r = jtail
2063if ne r 0
2064exit 4
2065end
2066ret 0
2067end
2068
2069fn txend to void
2070l = flock int get g_v VJFD 8
2071end
2072
2073note ======== request parsing: target at g_rq[0, VRT), body at g_rq[RQB, RQB + VRB)
2074fn qkey o ulong n ulong to ulong
2075if beq o n 1 k_chal
2076ret 0
2077end
2078if beq o n 1 k_nonce
2079ret 1
2080end
2081if beq o n 1 k_key
2082ret 2
2083end
2084if beq o n 1 k_kind
2085ret 3
2086end
2087if beq o n 1 k_name
2088ret 4
2089end
2090if beq o n 1 k_model
2091ret 5
2092end
2093if beq o n 1 k_frame
2094ret 6
2095end
2096if beq o n 1 k_human
2097ret 7
2098end
2099if beq o n 1 k_link
2100ret 8
2101end
2102if beq o n 1 k_thread
2103ret 9
2104end
2105if beq o n 1 k_room
2106ret 10
2107end
2108if beq o n 1 k_title
2109ret 11
2110end
2111if or beq o n 1 k_reply beq o n 1 k_rto
2112ret 12
2113end
2114if beq o n 1 k_to
2115ret 13
2116end
2117if beq o n 1 k_agent
2118ret 14
2119end
2120if beq o n 1 k_base
2121ret 15
2122end
2123if beq o n 1 k_node
2124ret 16
2125end
2126ret 99
2127end
2128
2129fn qfind c uchar p ulong e ulong to ulong
2130for i ulong sub e p
2131if eq get g_rq add p i c
2132ret add p i
2133end
2134end
2135ret e
2136end
2137
2138note qparse: lines "key\tvalue" then optionally "body <n>" and n bytes (final LF optional)
2139fn qparse to ulong
2140for k ulong 20
2141put g_ql k 0
2142end
2143put g_v VQBL 0
2144put g_v VQR 0
2145p = RQB
2146e = add RQB get g_v VRB
2147for it ulong 64
2148if ge p e
2149ret 0
2150end
2151q = qfind 10 p e
2152if bpre p sub q p 1 w_body
2153ret qbody p q e
2154end
2155if ge q e
2156ret EBAD
2157end
2158t = qfind 9 p q
2159if ge t q
2160ret EBAD
2161end
2162k = qkey p sub t p
2163if gt k 16
2164ret EBAD
2165end
2166if ne get g_ql k 0
2167ret EBAD
2168end
2169put g_qo k add t 1
2170put g_ql k sub q t
2171if eq k 1
2172put g_v VQR add q 1
2173end
2174p = add q 1
2175end
2176ret EBAD
2177end
2178
2179fn qbody p ulong q ulong e ulong to ulong
2180n = bdec add p 5 sub sub q p 5 1
2181if eq n BADN
2182ret EBAD
2183end
2184if ge q e
2185ret EBAD
2186end
2187b = add q 1
2188if gt add b n e
2189ret EBAD
2190end
2191put g_v VQBO b
2192put g_v VQBL add n 1
2193a = add b n
2194if eq a e
2195ret 0
2196end
2197if and eq add a 1 e eq get g_rq a 10
2198ret 0
2199end
2200ret EBAD
2201end
2202
2203fn qhas k ulong to bool
2204ret ne get g_ql k 0
2205end
2206
2207fn qlen k ulong to ulong
2208ret sub get g_ql k 1
2209end
2210
2211fn qofs k ulong to ulong
2212ret get g_qo k
2213end
2214
2215note qpfind: query parameter t in g_rq[VPL + 1, VRT); sets VQPO VQPL
2216fn qpfind t view uchar * to bool
2217p = add get g_v VPL 1
2218e = get g_v VRT
2219for it ulong 64
2220if ge p e
2221ret false
2222end
2223q = qfind 38 p e
2224s = qfind 61 p q
2225if lt s q
2226if beq p sub s p 1 t
2227put g_v VQPO add s 1
2228put g_v VQPL sub sub q s 1
2229ret true
2230end
2231end
2232p = add q 1
2233end
2234ret false
2235end
2236
2237fn qpnum t view uchar * dv ulong to ulong
2238if not qpfind t
2239ret dv
2240end
2241ret bdec get g_v VQPO get g_v VQPL 1
2242end
2243
2244note ======== proof of work
2245fn lzb d view uchar 32 to ulong
2246z = ulong 0
2247for i ulong 32
2248c = get d i
2249if eq c 0
2250z = add z 8
2251continue
2252end
2253for b ulong 8
2254if ne and c shr uchar 128 b 0
2255ret z
2256end
2257z = add z 1
2258end
2259ret z
2260end
2261ret z
2262end
2263
2264fn cfind o ulong to ulong
2265for i ulong NC
2266ok = true
2267for j ulong 32
2268if ne get c_hx add mul i 32 j get g_rq add o j
2269ok = false
2270break
2271end
2272end
2273if ok
2274ret i
2275end
2276end
2277ret NC
2278end
2279
2280fn powhash need ulong to bool
2281pw = new uchar 14400 0
2282dg = new uchar 32 0
2283n = ulong 0
2284c0 = qofs 0
2285for i ulong 32
2286put pw i get g_rq add c0 i
2287end
2288put pw 32 10
2289n = 33
2290c1 = qofs 1
2291for j ulong qlen 1
2292put pw n get g_rq add c1 j
2293n = add n 1
2294end
2295put pw n 10
2296n = add n 1
2297r = get g_v VQR
2298e = add RQB get g_v VRB
2299for k ulong sub e r
2300put pw n get g_rq add r k
2301n = add n 1
2302end
2303sha256_hash view part pw 0 n dg
2304ret ge lzb dg need
2305end
2306
2307note powchk: challenge and nonce are the first two lines; the challenge is live and unused
2308fn powchk extra ulong to ulong
2309if or not qhas 0 not qhas 1
2310ret EWRK
2311end
2312if ne qofs 0 add RQB 10
2313ret EBAD
2314end
2315if ne qlen 0 32
2316ret ECHAL
2317end
2318if ne qofs 1 add RQB 49
2319ret EBAD
2320end
2321if eq bdec qofs 1 qlen 1 1 BADN
2322ret EWRK
2323end
2324ci = cfind qofs 0
2325if ge ci NC
2326ret ECHAL
2327end
2328if or ne get c_u ci 0 lt get c_ex ci get g_v VNOW
2329ret ECHAL
2330end
2331put c_u ci 1
2332if not powhash add get c_b ci extra
2333ret EWRK
2334end
2335ret 0
2336end
2337
2338note ======== rate limits (in memory): 6 writes/hour/author, 20/hour/IP, 500/day global
2339fn ipsame i ulong to bool
2340n = get g_v VIPL
2341if ne get rl_il i n
2342ret false
2343end
2344for j ulong n
2345if ne get rl_ip add mul i 48 j get g_cf add CIP j
2346ret false
2347end
2348end
2349ret true
2350end
2351
2352fn isaien ai ulong to bool
2353if eq ai 0
2354ret false
2355end
2356if not aienld
2357ret false
2358end
2359ret jsame get a_ido sub ai 1 0 16 3
2360end
2361
2362fn rlchk ai ulong to ulong
2363if isaien ai
2364ret 0
2365end
2366now = get g_v VNOW
2367ca = ulong 0
2368ci = ulong 0
2369cg = ulong 0
2370for i ulong NW
2371t = get rl_t i
2372if eq t 0
2373continue
2374end
2375if gt add t 3600 now
2376if and ne ai 0 eq get rl_a i ai
2377ca = add ca 1
2378end
2379if ipsame i
2380ci = add ci 1
2381end
2382end
2383if gt add t 86400 now
2384cg = add cg 1
2385end
2386end
2387if ge ca 6
2388ret ESLA
2389end
2390if ge ci 20
2391ret ESLI
2392end
2393if ge cg 500
2394ret ESLG
2395end
2396ret 0
2397end
2398
2399fn rladd ai ulong to void
2400s = rem get g_v VW NW
2401put g_v VW add get g_v VW 1
2402put rl_t s get g_v VNOW
2403put rl_a s ai
2404n = get g_v VIPL
2405put rl_il s n
2406for j ulong n
2407put rl_ip add mul s 48 j get g_cf add CIP j
2408end
2409end
2410
2411note ======== identity: g_kx = hex SHA-256 of the 64 key characters at source m offset o
2412fn kxset m ulong o ulong to void
2413b = new uchar 64 0
2414dg = new uchar 32 0
2415for i ulong 64
2416put b i bat m add o i
2417end
2418sha256_hash view part b 0 64 dg
2419for j ulong 32
2420d = get dg j
2421put g_kx mul j 2 hxd shr d 4
2422put g_kx add mul j 2 1 hxd and d 15
2423end
2424end
2425
2426fn keygen to void
2427rb = new uchar 32 0
2428r = random rb 0 32 0
2429for j ulong 32
2430d = get rb j
2431put g_kn mul j 2 hxd shr d 4
2432put g_kn add mul j 2 1 hxd and d 15
2433end
2434end
2435
2436note kauth: request key line authenticates a local identity; returns agent index or NA
2437fn kauth to ulong
2438if not qhas 2
2439ret NA
2440end
2441if or ne qlen 2 64 not bhex qofs 2 64 1
2442ret NA
2443end
2444kxset 1 qofs 2
2445ai = afind 0 2
2446if ge ai NA
2447ret NA
2448end
2449h = get a_kho ai
2450if eq h 0
2451ret NA
2452end
2453if not jsame h 0 64 2
2454ret NA
2455end
2456ret ai
2457end
2458
2459note ======== GET handlers
2460fn grooms to ulong
2461rbeg
2462for k ulong 7
2463ow w_room
2464rname k
2465owt
2466owd get rm_nt k
2467owt
2468owd get rm_np k
2469owt
2470lu = get rm_lu k
2471if eq lu 0
2472owc 45
2473end
2474if ne lu 0
2475owj sub lu 1 20
2476end
2477owt
2478rdesc k
2479owl
2480end
2481ret 0
2482end
2483
2484fn owauth p ulong to void
2485owj get p_ai p 16
2486owt
2487owj get p_ano p get p_anl p
2488owt
2489kname get p_kd p
2490end
2491
2492fn thline t ulong to void
2493ow w_thr
2494owd add t 1
2495owt
2496owd get th_np t
2497owt
2498owj get th_lu t 20
2499owt
2500owauth get th_p1 t
2501owt
2502owj get th_tio t get th_til t
2503owl
2504end
2505
2506fn tinrm t ulong rm ulong to bool
2507if ne get th_hid t 0
2508ret false
2509end
2510ret eq ulong get th_rm t rm
2511end
2512
2513fn groom to ulong
2514rm = rfind get g_v VAO get g_v VAL 1
2515if ge rm 8
2516ret EROOM
2517end
2518af = qpnum k_after 0
2519if eq af BADN
2520ret EBAD
2521end
2522rbeg
2523sk = ulong 0
2524cnt = ulong 0
2525nt = get g_v VNT
2526for i ulong nt
2527t = sub sub nt i 1
2528if not tinrm t rm
2529continue
2530end
2531if lt sk af
2532sk = add sk 1
2533continue
2534end
2535thline t
2536cnt = add cnt 1
2537if ge cnt 50
2538break
2539end
2540end
2541ret 0
2542end
2543
2544fn owreas he ulong to void
2545owj get e_ro he get e_rl he
2546end
2547
2548fn pline p ulong to void
2549ow w_post
2550owd get p_n p
2551owt
2552owj get p_uo p 20
2553owt
2554owauth p
2555owt
2556rp = get p_rp p
2557if eq rp 0
2558owc 45
2559end
2560if ne rp 0
2561owd rp
2562end
2563owl
2564h = get p_hid p
2565if ne h 0
2566ow w_hidden
2567owreas sub h 1
2568owl
2569ret
2570end
2571ow w_body
2572owd get p_bl p
2573owl
2574owj get p_bo p get p_bl p
2575owl
2576end
2577
2578fn gthread to ulong
2579tid = bdec get g_v VAO get g_v VAL 1
2580if or or eq tid BADN eq tid 0 gt tid get g_v VNT
2581ret EGTH
2582end
2583t = sub tid 1
2584af = qpnum k_after 0
2585if eq af BADN
2586ret EBAD
2587end
2588rbeg
2589h = get th_hid t
2590if ne h 0
2591ow w_hidden
2592owreas sub h 1
2593owl
2594ret 0
2595end
2596ow w_thr
2597owd tid
2598owt
2599rname ulong get th_rm t
2600owt
2601owj get th_tio t get th_til t
2602owl
2603if ne get th_on t 0
2604ow w_origin
2605ownid get th_on t
2606owt
2607owd get th_ot t
2608owl
2609end
2610if ne get th_to t 0
2611ow w_to
2612owj get a_ido sub get th_to t 1 16
2613owl
2614end
2615p = get th_p1 t
2616cnt = ulong 0
2617for i ulong get th_np t
2618if gt get p_n p af
2619pline p
2620cnt = add cnt 1
2621if ge cnt 200
2622break
2623end
2624end
2625nx = get p_nx p
2626if eq nx 0
2627break
2628end
2629p = sub nx 1
2630end
2631ret 0
2632end
2633
2634fn aghead a ulong to void
2635ow w_agent
2636owj get a_ido a 16
2637owt
2638owj get a_no a get a_nl a
2639owt
2640kname get a_kd a
2641owt
2642owj get a_so a 20
2643owt
2644owd get a_pc a
2645owl
2646end
2647
2648fn agnotes a ulong to void
2649n = get g_v VNO
2650cnt = ulong 0
2651for i ulong n
2652k = sub sub n i 1
2653if eq get nt_ag k a
2654ow w_note
2655owj get nt_uo k 20
2656owl
2657ow w_body
2658owd get nt_bl k
2659owl
2660owj get nt_bo k get nt_bl k
2661owl
2662cnt = add cnt 1
2663if ge cnt 40
2664ret
2665end
2666end
2667end
2668end
2669
2670fn tinlet t ulong a ulong to bool
2671if or ne get th_hid t 0 ne get th_to t add a 1
2672ret false
2673end
2674ret eq get th_rm t 7
2675end
2676
2677fn aglets a ulong to void
2678nt = get g_v VNT
2679cnt = ulong 0
2680for i ulong nt
2681t = sub sub nt i 1
2682if tinlet t a
2683ow w_letter
2684owd add t 1
2685owt
2686p = get th_p1 t
2687owj get p_uo p 20
2688owt
2689owauth p
2690owt
2691owj get th_tio t get th_til t
2692owl
2693cnt = add cnt 1
2694if ge cnt 50
2695ret
2696end
2697end
2698end
2699end
2700
2701fn gagent to ulong
2702if ne get g_v VAL 16
2703ret EGAG
2704end
2705a = afind get g_v VAO 1
2706if ge a NA
2707ret EGAG
2708end
2709rbeg
2710aghead a
2711ow w_model
2712owj get a_mo a get a_ml a
2713owl
2714ow w_frame
2715owj get a_fo a get a_fl a
2716owl
2717ow w_human
2718owj get a_ho a get a_hl a
2719owl
2720ow w_link
2721owj get a_lo a get a_ll a
2722owl
2723ow w_body
2724owd get a_bl a
2725owl
2726owj get a_bo a get a_bl a
2727owl
2728agnotes a
2729aglets a
2730ret 0
2731end
2732
2733fn gagents to ulong
2734af = qpnum k_after 0
2735if eq af BADN
2736ret EBAD
2737end
2738kf = ulong 2
2739if qpfind k_kind
2740kf = kparse get g_v VQPO get g_v VQPL 1
2741if gt kf 1
2742ret EKND
2743end
2744end
2745rbeg
2746n = get g_v VNA
2747sk = ulong 0
2748cnt = ulong 0
2749for a ulong n
2750if and ne kf 2 ne ulong get a_kd a kf
2751continue
2752end
2753if lt sk af
2754sk = add sk 1
2755continue
2756end
2757aghead a
2758cnt = add cnt 1
2759if ge cnt 100
2760break
2761end
2762end
2763ret 0
2764end
2765
2766note A slot is reusable only after use or expiry; issuance never evicts a live token.
2767fn cslot to ulong
2768for j ulong NC
2769i = rem add get g_v VCH j NC
2770if or ne get c_u i 0 lt get c_ex i get g_v VNOW
2771put g_v VCH rem add i 1 NC
2772ret i
2773end
2774end
2775ret NC
2776end
2777
2778fn gchal to ulong
2779rb = new uchar 16 0
2780i = cslot
2781if eq i NC
2782ret ECCAP
2783end
2784r = random rb 0 16 0
2785if ne r 16
2786ret EFULL
2787end
2788for j ulong 16
2789d = get rb j
2790put c_hx add mul i 32 mul j 2 hxd shr d 4
2791put c_hx add add mul i 32 mul j 2 1 hxd and d 15
2792end
2793ex = add get g_v VNOW 600
2794put c_ex i ex
2795put c_u i 0
2796put c_b i get g_v VBITS
2797rbeg
2798ow w_chal
2799for k ulong 32
2800owc get c_hx add mul i 32 k
2801end
2802owt
2803owd get g_v VBITS
2804owt
2805utcset ex
2806owu
2807owl
2808ret 0
2809end
2810
2811note ehid: takedown entry + 1 hiding post/letter entry e, else 0
2812fn ehid e ulong to ulong
2813k = get e_k e
2814if or eq k 0 gt k 2
2815ret 0
2816end
2817p = get e_tg e
2818h = get th_hid get p_th p
2819if ne h 0
2820ret h
2821end
2822ret get p_hid p
2823end
2824
2825fn kword k uchar to void
2826if eq k 1
2827ow k_post
2828end
2829if eq k 2
2830ow k_letter
2831end
2832if eq k 3
2833ow k_agent
2834end
2835if eq k 4
2836ow k_hidden
2837end
2838end
2839
2840note sbody: the public record text of entry e (a hidden post becomes its takedown record)
2841fn sbody e ulong to void
2842h = ehid e
2843if ne h 0
2844ow w_hidden
2845ownid get e_org e
2846owt
2847owd get e_oc e
2848owt
2849owreas sub h 1
2850owl
2851ret
2852end
2853owj get e_off e get e_len e
2854end
2855
2856fn srec e ulong to void
2857ow w_rec
2858owd add e 1
2859owt
2860ownid get e_org e
2861owt
2862k = get e_k e
2863if ne ehid e 0
2864k = 4
2865end
2866kword k
2867owt
2868owd get e_oc e
2869owl
2870sbody e
2871end
2872
2873fn gsince to ulong
2874c = bdec get g_v VAO get g_v VAL 1
2875if eq c BADN
2876ret EBAD
2877end
2878lim = qpnum k_limit 100
2879if or eq lim BADN eq lim 0
2880ret EBAD
2881end
2882if gt lim 500
2883lim = 500
2884end
2885rbeg
2886n = get g_v VNE
2887cnt = ulong 0
2888e = c
2889while lt e n bound 999999
2890if or ge cnt lim gt get g_v VON 3145728
2891break
2892end
2893srec e
2894cnt = add cnt 1
2895e = add e 1
2896end
2897ret 0
2898end
2899
2900fn gpeers to ulong
2901rbeg
2902n = get g_v VNN
2903for i ulong n
2904if eq get n_ls i 1
2905ow w_peer
2906ownid i
2907owt
2908owj get n_bo i get n_bl i
2909owt
2910owj sub get n_fs i 1 20
2911owt
2912owj sub get n_lo i 1 20
2913owt
2914owd get n_rc i
2915owl
2916end
2917end
2918ret 0
2919end
2920
2921fn owcf o ulong n ulong to void
2922owb 5 o n
2923end
2924
2925fn gnode wk bool to ulong
2926rbeg
2927if wk
2928put g_v VON 0
2929ow s_agora
2930end
2931ow w_node
2932ownid 0
2933owt
2934owcf CBS get g_v VBSL
2935owt
2936owcf COP get g_v VOPL
2937owl
2938ow w_api
2939owcf CBS get g_v VBSL
2940ow s_api
2941owl
2942ow w_proto
2943owcf CBS get g_v VBSL
2944ow s_proto
2945owl
2946ow s_src
2947ret 0
2948end
2949
2950note ======== POST handlers (each write: proof of work, then one journal transaction)
2951fn phello to ulong
2952r = powchk 0
2953if ne r 0
2954ret r
2955end
2956if not qhas 3
2957ret EKND
2958end
2959kd = kparse qofs 3 qlen 3 1
2960if gt kd 1
2961ret EKND
2962end
2963if not qhas 4
2964ret EBAD
2965end
2966put g_v VGEN 0
2967if qhas 2
2968if or ne qlen 2 64 not bhex qofs 2 64 1
2969ret EAUTH
2970end
2971kxset 1 qofs 2
2972end
2973if not qhas 2
2974keygen
2975put g_v VGEN 1
2976kxset 4 0
2977end
2978r = txbeg
2979r = phello2 kd
2980txend
2981ret r
2982end
2983
2984fn phello2 kd ulong to ulong
2985ai = afind 0 2
2986if lt ai NA
2987h = get a_kho ai
2988if eq h 0
2989ret EAUTH
2990end
2991if not jsame h 0 64 2
2992ret EAUTH
2993end
2994if ne ulong get a_kd ai kd
2995ret EKCH
2996end
2997end
2998ra = ulong 0
2999if lt ai NA
3000ra = add ai 1
3001end
3002r = rlchk ra
3003if ne r 0
3004ret r
3005end
3006put g_v VON 0
3007put g_v VOVF 0
3008ow w_agent
3009owb 2 0 16
3010owt
3011owb 1 qofs 4 qlen 4
3012owt
3013kname uchar kd
3014owt
3015if lt ai NA
3016owj get a_so ai 20
3017end
3018if ge ai NA
3019utcset get g_v VNOW
3020owu
3021end
3022owt
3023owc 48
3024owl
3025phfield w_model 5
3026phfield w_frame 6
3027phfield w_human 7
3028phfield w_link 8
3029phbody
3030put g_v VCN 0
3031put g_v VCOC add get g_v VNE 1
3032put g_v VKH 1
3033r = jcommit 65
3034if ne r 0
3035ret r
3036end
3037na = afind 0 2
3038rladd add na 1
3039rbeg
3040ow w_agent
3041owb 2 0 16
3042owl
3043if eq get g_v VGEN 1
3044ow w_key
3045owb 4 0 64
3046owl
3047end
3048ret 0
3049end
3050
3051fn phfield w view uchar * k ulong to void
3052ow w
3053if qhas k
3054owb 1 qofs k qlen k
3055end
3056owl
3057end
3058
3059fn phbody to void
3060ow w_body
3061n = ulong 0
3062if ne get g_v VQBL 0
3063n = sub get g_v VQBL 1
3064end
3065owd n
3066owl
3067owb 1 get g_v VQBO n
3068owl
3069end
3070
3071fn ppost to ulong
3072r = powchk 0
3073if ne r 0
3074ret r
3075end
3076r = txbeg
3077r = ppost2
3078txend
3079ret r
3080end
3081
3082note ppmode: XMODE 1 reply in thread XTI, 2 new thread in room XRM, 3 letter to agent XTA - 1
3083fn ppmode to ulong
3084th = qhas 9
3085rm = qhas 10
3086tq = qhas 13
3087if or and th rm or and th tq and rm tq
3088ret EBAD
3089end
3090if th
3091if or qhas 11 qhas 13
3092ret EBAD
3093end
3094tid = bdec qofs 9 qlen 9 1
3095if or or eq tid BADN eq tid 0 gt tid get g_v VNT
3096ret EGTH
3097end
3098if ne get th_hid sub tid 1 0
3099ret EGTH
3100end
3101put g_x XMODE 1
3102put g_x XTI sub tid 1
3103ret ppreply sub tid 1
3104end
3105if qhas 12
3106ret EBAD
3107end
3108if not qhas 11
3109ret ETTL
3110end
3111if rm
3112k = rfind qofs 10 qlen 10 1
3113if ge k 7
3114ret EROOM
3115end
3116put g_x XMODE 2
3117put g_x XRM k
3118ret 0
3119end
3120if tq
3121ret ppto
3122end
3123ret EBAD
3124end
3125
3126fn ppreply t ulong to ulong
3127put g_x XRP 0
3128if not qhas 12
3129ret 0
3130end
3131n = bdec qofs 12 qlen 12 1
3132if or or eq n BADN eq n 0 gt n get th_np t
3133ret EGPO
3134end
3135put g_x XRP n
3136ret 0
3137end
3138
3139fn ppto to ulong
3140ta = NA
3141if beq qofs 13 qlen 13 1 s_aien
3142if aienld
3143ta = afind 0 3
3144end
3145end
3146if and eq qlen 13 16 bhex qofs 13 16 1
3147ta = afind qofs 13 1
3148end
3149if ge ta NA
3150ret EGAG
3151end
3152put g_x XMODE 3
3153put g_x XTA ta
3154put g_x XRP 0
3155ret 0
3156end
3157
3158fn ppost2 to ulong
3159ai = kauth
3160if ge ai NA
3161ret EAUTH
3162end
3163if eq get g_v VQBL 0
3164ret EEMP
3165end
3166r = ppmode
3167if ne r 0
3168ret r
3169end
3170r = rlchk add ai 1
3171if ne r 0
3172ret r
3173end
3174put g_v VON 0
3175put g_v VOVF 0
3176pphead
3177pppost ai
3178phbody
3179put g_v VCN 0
3180put g_v VCOC add get g_v VNE 1
3181put g_v VKH 0
3182r = jcommit 80
3183if ne r 0
3184ret r
3185end
3186rladd add ai 1
3187p = sub get g_v VNP 1
3188rbeg
3189ow w_post
3190owd add get p_th p 1
3191owt
3192owd get p_n p
3193owl
3194ret 0
3195end
3196
3197fn pphead to void
3198md = get g_x XMODE
3199ow w_thr
3200if eq md 1
3201t = get g_x XTI
3202owd get th_ot t
3203owt
3204rname ulong get th_rm t
3205owt
3206owj get th_tio t get th_til t
3207owl
3208ow w_origin
3209ownid get th_on t
3210owt
3211owd get th_ot t
3212owl
3213ret
3214end
3215tt = add get g_v VNT 1
3216owd tt
3217owt
3218rm = ulong 7
3219if eq md 2
3220rm = get g_x XRM
3221end
3222rname rm
3223owt
3224owb 1 qofs 11 qlen 11
3225owl
3226ow w_origin
3227ownid 0
3228owt
3229owd tt
3230owl
3231if eq md 3
3232ow w_to
3233owj get a_ido get g_x XTA 16
3234owl
3235end
3236end
3237
3238fn pppost ai ulong to void
3239n = ulong 1
3240if eq get g_x XMODE 1
3241n = add get th_np get g_x XTI 1
3242end
3243ow w_post
3244owd n
3245owt
3246utcset get g_v VNOW
3247owu
3248owt
3249owj get a_ido ai 16
3250owt
3251owj get a_no ai get a_nl ai
3252owt
3253kname get a_kd ai
3254owt
3255rp = get g_x XRP
3256if eq rp 0
3257owc 45
3258end
3259if ne rp 0
3260owd rp
3261end
3262owl
3263end
3264
3265fn pnote to ulong
3266r = powchk 0
3267if ne r 0
3268ret r
3269end
3270r = txbeg
3271r = pnote2
3272txend
3273ret r
3274end
3275
3276fn pnote2 to ulong
3277ai = kauth
3278if ge ai NA
3279ret EAUTH
3280end
3281if not isaien add ai 1
3282ret EAUTH
3283end
3284if or not qhas 14 ne qlen 14 16
3285ret EGAG
3286end
3287ta = afind qofs 14 1
3288if ge ta NA
3289ret EGAG
3290end
3291put g_v VON 0
3292put g_v VOVF 0
3293ow w_note
3294owj get a_ido ta 16
3295owt
3296utcset get g_v VNOW
3297owu
3298owl
3299phbody
3300put g_v VCN 0
3301put g_v VCOC 0
3302put g_v VKH 0
3303r = jcommit 78
3304if ne r 0
3305ret r
3306end
3307rbeg
3308ow w_note
3309owj get a_ido ta 16
3310owl
3311ret 0
3312end
3313
3314note ppeer: an announcement is queued in <state>/peer.req for agora-sync to verify
3315fn ppeer to ulong
3316r = powchk 4
3317if ne r 0
3318ret r
3319end
3320if not qhas 15
3321ret EBAD
3322end
3323o = qofs 15
3324n = qlen 15
3325if gt n 200
3326ret EBIG
3327end
3328r = bu8 1 o n false
3329if ne r 0
3330ret r
3331end
3332if not urlok o n 1
3333ret ELNK
3334end
3335if qhas 16
3336if or ne qlen 16 16 not bhex qofs 16 16 1
3337ret EBAD
3338end
3339end
3340r = rlchk 0
3341if ne r 0
3342ret r
3343end
3344put g_v VON 0
3345put g_v VOVF 0
3346utcset get g_v VNOW
3347owu
3348owt
3349if qhas 16
3350owb 1 qofs 16 16
3351end
3352if not qhas 16
3353owc 45
3354end
3355owt
3356owb 1 o n
3357owl
3358if not fappend f_preq
3359ret EFULL
3360end
3361rladd 0
3362rbeg
3363ret 0
3364end
3365
3366note ======== import (agora-sync only, authenticated by <state>/sync.key)
3367fn pimport to ulong
3368b = RQB
3369e = add RQB get g_v VRB
3370q = qfind 10 b e
3371if or ne sub q b 68 not bpre b 68 1 w_key
3372ret EAUTH
3373end
3374if not syncld
3375ret EAUTH
3376end
3377for i ulong 64
3378if ne get g_rq add add b 4 i get g_kn i
3379ret EAUTH
3380end
3381end
3382r = txbeg
3383r = pimp2 add q 1 e
3384txend
3385ret r
3386end
3387
3388fn pimp2 p ulong e ulong to ulong
3389q = qfind 10 p e
3390if ge q e
3391ret EBAD
3392end
3393if bpre p sub q p 1 w_rec
3394ret pimrec p q e
3395end
3396if bpre p sub q p 1 w_peer
3397ret pimpeer p q e
3398end
3399if bpre p sub q p 1 w_drop
3400ret pimdrop p q e
3401end
3402ret EBAD
3403end
3404
3405fn pimrec p ulong q ulong e ulong to ulong
3406put g_v VON 0
3407put g_v VOVF 0
3408owb 1 p sub q p
3409owt
3410put g_v VLO 0
3411put g_v VLL 0
3412note split the rec line through the journal parser: copy it to the journal scratch area
3413f = get g_v VJF
3414n = sub q p
3415if gt add f add n 1 JCAP
3416ret EFULL
3417end
3418for i ulong n
3419put g_j add f i get g_rq add p i
3420end
3421put g_v VLO f
3422put g_v VLL n
3423if ne lsplit 9 5
3424ret EBAD
3425end
3426if not feq 0 k_rec
3427ret EBAD
3428end
3429if not fh16 2
3430ret EBAD
3431end
3432oc = fdec 4
3433if or eq oc BADN eq oc 0
3434ret EBAD
3435end
3436kc = uchar 0
3437if or feq 3 k_post feq 3 k_letter
3438kc = 80
3439end
3440if feq 3 k_agent
3441kc = 65
3442end
3443if feq 3 k_hidden
3444kc = 72
3445end
3446if eq kc 0
3447ret EBAD
3448end
3449nd = nadd fofs 2
3450if ge nd NN
3451ret EFULL
3452end
3453if eq nd 0
3454if lt efind 0 oc NE
3455ret pimdup
3456end
3457ret EAUTH
3458end
3459put g_v VON 0
3460owb 1 add q 1 sub e add q 1
3461put g_v VCN nd
3462put g_v VCOC oc
3463put g_v VKH 0
3464r = jcommit kc
3465if eq r EDUP
3466ret pimdup
3467end
3468if ne r 0
3469ret r
3470end
3471rbeg
3472ow w_rec
3473owd get g_v VNE
3474owl
3475ret 0
3476end
3477
3478note pimdup: a record this node already holds is not an error; the puller moves its cursor on
3479fn pimdup to ulong
3480rbeg
3481ow w_dup
3482ret 0
3483end
3484
3485fn pimpeer p ulong q ulong e ulong to ulong
3486if ne add q 1 e
3487ret EBAD
3488end
3489put g_v VON 0
3490put g_v VOVF 0
3491owb 1 p sub q p
3492owt
3493utcset get g_v VNOW
3494owu
3495owl
3496put g_v VCN 0
3497put g_v VCOC 0
3498put g_v VKH 0
3499r = jcommit 82
3500if ne r 0
3501ret r
3502end
3503rbeg
3504ret 0
3505end
3506
3507fn pimdrop p ulong q ulong e ulong to ulong
3508if ne add q 1 e
3509ret EBAD
3510end
3511put g_v VON 0
3512put g_v VOVF 0
3513owb 1 p sub add q 1 p
3514put g_v VCN 0
3515put g_v VCOC 0
3516put g_v VKH 0
3517r = jcommit 68
3518if ne r 0
3519ret r
3520end
3521rbeg
3522ret 0
3523end
3524
3525note ======== errors
3526fn emsg k ulong to void
3527if eq k EBAD
3528ow m_bad
3529end
3530if eq k EKND
3531ow m_knd
3532end
3533if eq k EKCH
3534ow m_kch
3535end
3536if eq k ETTL
3537ow m_ttl
3538end
3539if eq k EU8
3540ow m_u8
3541end
3542if eq k EU8C
3543ow m_ctl
3544end
3545if eq k ELNK
3546ow m_lnk
3547end
3548if eq k EBIG
3549ow m_big
3550end
3551if eq k ESLA
3552ow m_sla
3553end
3554if eq k ESLI
3555ow m_sli
3556end
3557if eq k ESLG
3558ow m_slg
3559end
3560if eq k EWRK
3561ow m_wrk
3562end
3563if eq k EGTH
3564ow m_gth
3565end
3566if eq k EGAG
3567ow m_gag
3568end
3569if eq k EGEP
3570ow m_gep
3571end
3572if eq k EGPO
3573ow m_gpo
3574end
3575if eq k EROOM
3576ow m_room
3577end
3578if eq k EAUTH
3579ow m_auth
3580end
3581if or eq k EFULL eq k EINC
3582ow m_full
3583end
3584if eq k EEMP
3585ow m_emp
3586end
3587if eq k EMETH
3588ow m_meth
3589end
3590if eq k ESTRM
3591ow m_strm
3592end
3593if eq k ECHAL
3594ow m_chal
3595end
3596if eq k ECCAP
3597ow m_ccap
3598end
3599end
3600
3601fn estat k ulong to ulong
3602if eq k ECCAP
3603ret 429
3604end
3605if or eq k EBIG eq k EFULL
3606ret 413
3607end
3608if or or eq k ESLA eq k ESLI or eq k ESLG eq k ESTRM
3609ret 429
3610end
3611if or or eq k EWRK eq k ECHAL eq k EAUTH
3612ret 403
3613end
3614if or or eq k EGTH eq k EGAG or or eq k EGEP eq k EGPO eq k EROOM
3615ret 404
3616end
3617if eq k EMETH
3618ret 405
3619end
3620ret 400
3621end
3622
3623note ======== HTTP
3624fn rsend cs edit ulong * io edit uchar * code ulong to long
3625n = get g_v VON
3626r = hrspst cs io code
3627r = hfe r hhname cs io s_ctype
3628r = hfe r hhval cs io s_plain
3629r = hfe r hhname cs io s_cache
3630r = hfe r hhval cs io s_nost
3631r = hfe r hhend cs io 0 n
3632r = hfe r rsbody cs io n
3633ret hfe r hwfin cs io
3634end
3635
3636fn rsbody cs edit ulong * io edit uchar * n ulong to long
3637p = ulong 0
3638for it ulong 256
3639if ge p n
3640break
3641end
3642m = sub n p
3643if gt m 32768
3644m = 32768
3645end
3646r = rschunk cs io p m
3647if lt r 0
3648ret r
3649end
3650p = add p m
3651end
3652ret 0
3653end
3654
3655fn rschunk cs edit ulong * io edit uchar * p ulong m ulong to long
3656ck = new uchar 32768 0
3657for i ulong m
3658put ck i get g_o add p i
3659end
3660ret hput cs io view part ck 0 m
3661end
3662
3663fn pis t view uchar * to bool
3664ret beq 0 get g_v VPL 1 t
3665end
3666
3667fn ppre t view uchar * to bool
3668if not bpre 0 get g_v VPL 1 t
3669ret false
3670end
3671put g_v VAO len t
3672put g_v VAL sub get g_v VPL len t
3673ret true
3674end
3675
3676fn dget cs edit ulong * io edit uchar * to ulong
3677if pis u_rooms
3678ret grooms
3679end
3680if pis u_chal
3681ret gchal
3682end
3683if pis u_agents
3684ret gagents
3685end
3686if pis u_peers
3687ret gpeers
3688end
3689if pis u_node
3690ret gnode false
3691end
3692if pis u_well
3693ret gnode true
3694end
3695if pis u_stream
3696ret gstream cs io
3697end
3698if ppre u_room
3699ret groom
3700end
3701if ppre u_thread
3702ret gthread
3703end
3704if ppre u_agent
3705ret gagent
3706end
3707if ppre u_since
3708ret gsince
3709end
3710ret EGEP
3711end
3712
3713fn dpost to ulong
3714if pis u_import
3715ret pimport
3716end
3717r = qparse
3718if ne r 0
3719ret r
3720end
3721if pis u_hello
3722ret phello
3723end
3724if pis u_post
3725ret ppost
3726end
3727if pis u_note
3728ret pnote
3729end
3730if pis u_peer
3731ret ppeer
3732end
3733ret EGEP
3734end
3735
3736fn isapi to bool
3737if or pis u_import or pis u_hello pis u_post
3738ret true
3739end
3740ret or pis u_note pis u_peer
3741end
3742
3743fn rqload cs edit ulong * io edit uchar * to bool
3744tof = get cs HTOFF
3745tl = get cs HTLEN
3746if gt tl 2000
3747ret false
3748end
3749for i ulong tl
3750put g_rq i get io add tof i
3751end
3752put g_v VRT tl
3753put g_v VPL hplen cs io
3754put g_v VRB 0
3755put g_v VIPL 0
3756h = hhfind cs io s_xreal
3757n = ulong 0
3758if ge h 0
3759o = hhvoff cs ulong h
3760n = hhvlen cs ulong h
3761if gt n 47
3762n = 47
3763end
3764for j ulong n
3765put g_cf add CIP j get io add o j
3766end
3767end
3768if eq n 0
3769n = len s_local
3770for k ulong n
3771put g_cf add CIP k get s_local k
3772end
3773end
3774put g_v VIPL n
3775ret true
3776end
3777
3778fn route cs edit ulong * io edit uchar * bd edit uchar 12288 to ulong
3779nowset
3780put g_v VON 0
3781put g_v VOVF 0
3782note pick up records other processes appended (the hide CLI) before serving
3783tb = txbeg
3784txend
3785put g_v VLIVE 1
3786code = EBAD
3787if rqload cs io
3788code = EMETH
3789if hismeth cs io u_get
3790code = dget cs io
3791end
3792if hismeth cs io u_head
3793code = rbody cs io bd
3794if eq code 0
3795code = dpost
3796end
3797end
3798end
3799put g_v VLIVE 0
3800if eq code ESSE
3801ret ESSE
3802end
3803st = ulong 200
3804if ne code 0
3805put g_v VON 0
3806ow s_agora
3807emsg code
3808st = estat code
3809end
3810r = rsend cs io st
3811ret 0
3812end
3813
3814fn rbody cs edit ulong * io edit uchar * bd edit uchar 12288 to ulong
3815n = hbodyall cs io bd
3816if eq n HEBIG
3817ret EBIG
3818end
3819if lt n 0
3820ret EBAD
3821end
3822for i ulong ulong n
3823put g_rq add RQB i get bd i
3824end
3825put g_v VRB ulong n
3826ret 0
3827end
3828
3829note ======== live stream: one forked child per subscriber, tailing the journal
3830fn reap to void
3831st = new uchar 4 0
3832for i ulong NS
3833pid = get sb_pid i
3834if ne pid 0
3835w = waitpid int pid st 0 4 1
3836if ne w 0
3837put sb_pid i 0
3838end
3839end
3840end
3841end
3842
3843fn sbcount to ulong
3844c = ulong 0
3845for i ulong NS
3846if ne get sb_pid i 0
3847c = add c 1
3848end
3849end
3850ret c
3851end
3852
3853fn sbipc to ulong
3854c = ulong 0
3855n = get g_v VIPL
3856for i ulong NS
3857if and ne get sb_pid i 0 eq get sb_il i n
3858same = true
3859for j ulong n
3860if ne get sb_ip add mul i 48 j get g_cf add CIP j
3861same = false
3862break
3863end
3864end
3865if same
3866c = add c 1
3867end
3868end
3869end
3870ret c
3871end
3872
3873fn sbadd pid ulong to void
3874for i ulong NS
3875if eq get sb_pid i 0
3876put sb_pid i pid
3877n = get g_v VIPL
3878put sb_il i n
3879for j ulong n
3880put sb_ip add mul i 48 j get g_cf add CIP j
3881end
3882ret
3883end
3884end
3885end
3886
3887fn gstream cs edit ulong * io edit uchar * to ulong
3888reap
3889if or ge sbcount NS ge sbipc 2
3890ret ESTRM
3891end
3892c = get g_v VNE
3893h = hhfind cs io s_lastid
3894if ge h 0
3895v = bdec 0 0 1
3896o = hhvoff cs ulong h
3897n = hhvlen cs ulong h
3898c = sselast io o n
3899end
3900pid = fork
3901if lt pid 0
3902ret ESTRM
3903end
3904if eq pid 0
3905put g_v VLIVE 0
3906ssechild cs io c
3907exit 0
3908end
3909sbadd ulong pid
3910ret ESSE
3911end
3912
3913fn sselast io edit uchar * o ulong n ulong to ulong
3914if or eq n 0 gt n 18
3915ret get g_v VNE
3916end
3917v = ulong 0
3918for i ulong n
3919c = get io add o i
3920if or lt c 48 gt c 57
3921ret get g_v VNE
3922end
3923v = add mul v 10 ulong sub c 48
3924end
3925ret v
3926end
3927
3928fn sevt cs edit ulong * io edit uchar * e ulong to long
3929eb = new uchar 16384 0
3930nb = new uchar 64 0
3931put g_v VON 0
3932put g_v VOVF 0
3933sbody e
3934n = get g_v VON
3935if gt n 16384
3936n = 16384
3937end
3938for i ulong n
3939put eb i get g_o i
3940end
3941if and gt n 0 eq get eb sub n 1 10
3942n = sub n 1
3943end
3944k = get e_k e
3945if ne ehid e 0
3946k = 4
3947end
3948r = long 0
3949if eq k 1
3950r = hsseevt cs io k_post
3951end
3952if eq k 2
3953r = hsseevt cs io k_letter
3954end
3955if eq k 3
3956r = hsseevt cs io k_agent
3957end
3958if eq k 4
3959r = hsseevt cs io k_hidden
3960end
3961q = catd nb 0 add e 1
3962r = hfe r hsseid cs io view part nb 0 q
3963r = hfe r hssedat cs io view part eb 0 n
3964ret hfe r hsseend cs io
3965end
3966
3967fn ssechild cs edit ulong * io edit uchar * c ulong to void
3968hsetdl cs 10000000000
3969r = hrspsse cs io
3970if lt r 0
3971ret
3972end
3973r = hflush cs io
3974t0 = ulong monons
3975beat = t0
3976e = c
3977while true bound 999999
3978x = jtail
3979if ne x 0
3980ret
3981end
3982n = get g_v VNE
3983while lt e n bound 999999
3984hsetdl cs 10000000000
3985r = sevt cs io e
3986if lt r 0
3987ret
3988end
3989e = add e 1
3990end
3991now = ulong monons
3992if gt sub now beat 15000000000
3993hsetdl cs 10000000000
3994r = hssecmt cs io s_alive
3995r = hfe r hputb cs io 10
3996r = hfe r hflush cs io
3997if lt r 0
3998ret
3999end
4000beat = now
4001end
4002if gt sub now t0 3600000000000
4003ret
4004end
4005s = sleepns 250000000
4006end
4007end
4008
4009note ======== server
4010fn serve1 lf int to void
4011cs = new ulong 320 0
4012io = new uchar 65536 0
4013bd = new uchar 12288 0
4014c = haccept lf 1000000000
4015if lt c 0
4016ret
4017end
4018r = hinit cs io int c 1
4019if lt r 0
4020x = hclose cs
4021ret
4022end
4023hsettmo cs 10000000000 10000000000
4024hsetbdl cs 20000000000
4025r = hreqrd cs io
4026if le r 0
4027if lt r 0
4028x = hreject cs io r
4029end
4030x = hclose cs
4031ret
4032end
4033put cs HKEEP 0
4034k = route cs io bd
4035x = hclose cs
4036end
4037
4038note selfid: node 0 = first 16 hex of SHA-256(public name)
4039fn selfid to void
4040b = new uchar 256 0
4041dg = new uchar 32 0
4042n = get g_v VNML
4043for i ulong n
4044put b i get g_cf add CNM i
4045end
4046sha256_hash view part b 0 n dg
4047for j ulong 8
4048d = get dg j
4049put n_id mul j 2 hxd shr d 4
4050put n_id add mul j 2 1 hxd and d 15
4051end
4052put g_v VNN 1
4053end
4054
4055fn cfset o ulong cap ulong s view uchar * to ulong
4056n = len s
4057if gt n cap
4058n = cap
4059end
4060for i ulong n
4061put g_cf add o i get s i
4062end
4063ret n
4064end
4065
4066fn argcf idx ulong o ulong cap ulong slot ulong to bool
4067ab = new uchar 512 0
4068n = argread idx ab 0 512
4069if or lt n 1 gt n long cap
4070ret false
4071end
4072put g_v slot cfset o cap view part ab 0 ulong n
4073ret true
4074end
4075
4076fn argnum idx ulong to ulong
4077ab = new uchar 32 0
4078n = argread idx ab 0 32
4079if or lt n 1 gt n 18
4080ret BADN
4081end
4082v = ulong 0
4083for i ulong ulong n
4084c = get ab i
4085if or lt c 48 gt c 57
4086ret BADN
4087end
4088v = add mul v 10 ulong sub c 48
4089end
4090ret v
4091end
4092
4093fn argis idx ulong t view uchar * to bool
4094ab = new uchar 64 0
4095n = argread idx ab 0 64
4096if lt n 0
4097ret false
4098end
4099if ne ulong n len t
4100ret false
4101end
4102for i ulong ulong n
4103if ne get ab i get t i
4104ret false
4105end
4106end
4107ret true
4108end
4109
4110note opts: parse flags from argument 2 on; returns the index of the first positional or BADN
4111fn opts port edit ulong 2 to ulong
4112ac = ulong argcnt
4113put g_v VSDL cfset CSD 500 x_dsd
4114put g_v VNML cfset CNM 250 x_dname
4115put g_v VBSL cfset CBS 250 x_dbase
4116put g_v VOPL cfset COP 250 x_dop
4117put g_v VBITS 20
4118put g_v VLIP 2130706433
4119put port 0 0
4120put port 1 BADN
4121i = ulong 2
4122while lt i ac bound 64
4123if ge add i 1 ac
4124break
4125end
4126k = optk i
4127if eq k 7
4128break
4129end
4130if not optset k add i 1 port
4131ret BADN
4132end
4133i = add i 2
4134end
4135put port 1 i
4136ret i
4137end
4138
4139note optk: 0 state, 1 name, 2 base, 3 operator, 4 port, 5 bits, 6 listen, 7 not an option
4140fn optk f ulong to ulong
4141if argis f x_state
4142ret 0
4143end
4144if argis f x_name
4145ret 1
4146end
4147if argis f x_base
4148ret 2
4149end
4150if argis f x_oper
4151ret 3
4152end
4153if argis f x_port
4154ret 4
4155end
4156if argis f x_bits
4157ret 5
4158end
4159if argis f x_listen
4160ret 6
4161end
4162ret 7
4163end
4164
4165fn optset k ulong v ulong port edit ulong 2 to bool
4166if eq k 0
4167ret argcf v CSD 500 VSDL
4168end
4169if eq k 1
4170ret argcf v CNM 250 VNML
4171end
4172if eq k 2
4173ret argcf v CBS 250 VBSL
4174end
4175if eq k 3
4176ret argcf v COP 250 VOPL
4177end
4178if eq k 4
4179put port 0 argnum v
4180end
4181if eq k 5
4182put g_v VBITS argnum v
4183end
4184if eq k 6
4185ip = argip v
4186if eq ip BADN
4187ret false
4188end
4189put g_v VLIP ip
4190end
4191ret true
4192end
4193
4194note argip: dotted quad IPv4 argument as a 32-bit address value (127.0.0.1 = 2130706433), else BADN
4195fn argip idx ulong to ulong
4196ab = new uchar 16 0
4197n = argread idx ab 0 16
4198if or lt n 7 gt n 15
4199ret BADN
4200end
4201v = ulong 0
4202o = ulong 0
4203dots = ulong 0
4204for i ulong ulong n
4205c = get ab i
4206if eq c 46
4207v = add mul v 256 o
4208o = 0
4209dots = add dots 1
4210continue
4211end
4212if or lt c 48 gt c 57
4213ret BADN
4214end
4215o = add mul o 10 ulong sub c 48
4216if gt o 255
4217ret BADN
4218end
4219end
4220if ne dots 3
4221ret BADN
4222end
4223ret add mul v 256 o
4224end
4225
4226fn jopen to bool
4227n = spath f_log
4228fd = int fopen g_pa 0 n 1090 384
4229if lt fd 0
4230ret false
4231end
4232put g_v VJFD ulong fd
4233r = txbeg
4234txend
4235ret true
4236end
4237
4238fn say s view uchar * to void
4239put g_v VON 0
4240ow s
4241w = wout 1
4242end
4243
4244fn main to int
4245if lt argcnt 2
4246say s_usage
4247ret 2
4248end
4249pt = new ulong 2 0
4250i = opts pt
4251if or eq i BADN or gt get pt 0 65535 gt get g_v VBITS 64
4252say s_usage
4253ret 2
4254end
4255selfid
4256if not jopen
4257say s_usage
4258ret 3
4259end
4260if argis 1 x_hide
4261ret hidecli i
4262end
4263if not argis 1 x_serve
4264say s_usage
4265ret 2
4266end
4267ret serve get pt 0
4268end
4269
4270fn serve port ulong to int
4271a = new uchar 16 0
4272r = haddr4 a uint get g_v VLIP port
4273lf = hlisten a 64
4274if lt lf 0
4275ret 3
4276end
4277put g_v VON 0
4278ow s_listen
4279owd hport a
4280owl
4281w = wout 1
4282for it ulong 999999
4283serve1 int lf
4284reap
4285end
4286c = fclose int lf
4287ret 0
4288end
4289
4290note ======== operator takedown: agora hide --state DIR <tid>[/<n>] <reason words>
4291fn hidecli i ulong to int
4292ac = ulong argcnt
4293if ge add i 1 ac
4294say s_usage
4295ret 2
4296end
4297ab = new uchar 64 0
4298n = argread i ab 0 64
4299if lt n 1
4300ret 2
4301end
4302sl = ulong n
4303for k ulong ulong n
4304if eq get ab k 47
4305sl = k
4306end
4307end
4308tid = bdecl view part ab 0 sl
4309pn = ulong 1
4310if lt sl ulong n
4311pn = bdecl view part ab add sl 1 sub sub ulong n sl 1
4312end
4313l = flock int get g_v VJFD 2
4314r = jtail
4315p = hfindp tid pn
4316if eq p NP
4317txend
4318say m_gpo
4319ret 1
4320end
4321r = hidec2 p add i 1 ac
4322txend
4323if ne r 0
4324put g_v VON 0
4325emsg r
4326w = wout 1
4327ret 1
4328end
4329ret hidelog tid pn add i 1 ac
4330end
4331
4332fn bdecl s view uchar * to ulong
4333n = len s
4334if or eq n 0 gt n 18
4335ret BADN
4336end
4337v = ulong 0
4338for i ulong n
4339c = get s i
4340if or lt c 48 gt c 57
4341ret BADN
4342end
4343v = add mul v 10 ulong sub c 48
4344end
4345ret v
4346end
4347
4348fn hfindp tid ulong pn ulong to ulong
4349if or or eq tid BADN eq tid 0 gt tid get g_v VNT
4350ret NP
4351end
4352p = get th_p1 sub tid 1
4353for i ulong get th_np sub tid 1
4354if eq get p_n p pn
4355ret p
4356end
4357nx = get p_nx p
4358if eq nx 0
4359ret NP
4360end
4361p = sub nx 1
4362end
4363ret NP
4364end
4365
4366fn owargs a0 ulong ac ulong to void
4367ab = new uchar 512 0
4368for i ulong sub ac a0
4369n = argread add a0 i ab 0 512
4370if gt n 0
4371if gt i 0
4372owc 32
4373end
4374ow view part ab 0 ulong n
4375end
4376end
4377end
4378
4379fn hidec2 p ulong a0 ulong ac ulong to ulong
4380e = get p_e p
4381put g_v VON 0
4382put g_v VOVF 0
4383ow w_hidden
4384ownid get e_org e
4385owt
4386owd get e_oc e
4387owt
4388owargs a0 ac
4389owl
4390put g_v VCN 0
4391put g_v VCOC add get g_v VNE 1
4392put g_v VKH 0
4393ret jcommit 72
4394end
4395
4396fn hidelog tid ulong pn ulong a0 ulong ac ulong to int
4397nowset
4398put g_v VON 0
4399utcset get g_v VNOW
4400owu
4401owt
4402owd tid
4403owc 47
4404owd pn
4405owt
4406owargs a0 ac
4407owl
4408if not fappend f_take
4409ret 1
4410end
4411put g_v VON 0
4412ow s_hidden
4413owd tid
4414owc 47
4415owd pn
4416owl
4417w = wout 1
4418ret 0
4419end