interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / tools / bring / src / 40net.om

1366 lines · 27113 bytes Raw Blame History
1note Network flows. The only network path is /usr/bin/curl with an argument list.
2text tcurl "/usr/bin/curl"
3text tsshk "/usr/bin/ssh-keygen"
4text tdevnul "/dev/null"
5text tdefb "https://interplane.io"
6text tbenv "INTERPLANE_BASE="
7text thomee "HOME="
8text thttps "https://"
9text thttp "http://"
10text tloop1 "http://127.0.0.1:"
11text tloop2 "http://localhost:"
12text tprots "=https"
13text tproth "=http"
14text tbadb "INTERPLANE_BASE must be https://..., or http:// on 127.0.0.1 or localhost"
15text tq "-q"
16text tss "-sS"
17text tproto "--proto"
18text tpredir "--proto-redir"
19text tmaxr "--max-redirs"
20text tzero "0"
21text tdh "-D"
22text tdo "-o"
23text tdw "-w"
24text thcode "%{http_code}"
25text tdhh "-H"
26text tatsign "@"
27text tdata "--data-binary"
28text treqh "reqh"
29text thdr "hdr"
30text tbody "body"
31text tcode "code"
32text tcerr "cerr"
33text tform "form"
34text tstmt "stmt"
35text tstsig "stmt.sig"
36text tchal "chal"
37text tchsig "chal.sig"
38text tgettar "get.tar"
39text torig "Origin: "
40text tcook "Cookie: "
41text tctype "\nContent-Type: application/x-www-form-urlencoded"
42text tsemi "; "
43text tpre "__Host-forge_pre="
44text tsess "__Host-forge_session="
45text tsetc "set-cookie: "
46text tlogin "/login"
47text topenp "/open/"
48text tup "/open/up"
49text tupj "/open/up/"
50text tjson ".json"
51text tcsrf "name=\"csrf\" value=\""
52text tnonce "name=\"nonce\" value=\""
53text tts "name=\"ts\" value=\""
54text tquote "\""
55text tfcsrf "csrf="
56text tfnonce "&nonce="
57text tfts "&ts="
58text tfdid "&did="
59text tfsig "&sig="
60text tachunk "&action=chunk&sha="
61text tafin "&action=finish&sha="
62text tfseq "&seq="
63text tfdata "&data="
64text tffiles "&files="
65text tfbytes "&bytes="
66text tfname "&name="
67text tfcons "&consent=yes"
68text tnext "next "
69text tqueued "queued "
70text tchal1 "interplane-forge-login:"
71text tcolon1 ":"
72text tsig1 "-Y"
73text tsign "sign"
74text tf "-f"
75text tn "-n"
76text tnsf "interplane-forge"
77text tnsb "interplane-bring"
78text ttype "-t"
79text ted "ed25519"
80text tnpw "-N"
81text tcmt "-C"
82text tinter "interplane"
83text tdir "/.interplane"
84text tkeynm "/.interplane/key"
85text tpubnm "/.interplane/key.pub"
86text tssh "ssh-ed25519 "
87text tst1 "interplane-bring 1\nsha256 "
88text tst2 "\nfiles "
89text tst3 "\nbytes "
90text tst4 "\nname "
91text tstate "\"state\":\""
92text trid "\"rid\":\"rad:"
93text tpage "\"page\":\""
94text tdl "\"download\":\""
95text tdlsha "\"download_sha256\":\""
96text tjname "\"name\":\""
97text tstored "stored"
98text trefused "refused"
99text tsent "Sent "
100text tof " of "
101text tbsuf " bytes"
102text twait "Interplane has the files and is publishing them. This can take a minute.\n"
103text tpubd "Your project page: "
104text tdev "Developer details: Radicle ID rad:"
105text tclone ", rad clone rad:"
106text tsaved "Saved in ./"
107text trad "rad:"
108
109text tnetbad "could not reach Interplane (curl failed)"
110text thttpbad "Interplane answered with HTTP "
111text tbadrsp "Interplane sent an answer this program does not understand"
112text t413 "Interplane answered 413: a request was too large for a proxy in front of it (the hub has no project size limit)"
113text t503 "Interplane cannot take uploads right now (queue or storage full); try again later"
114text tbusy "The download server is busy; trying again in 10 seconds.\n"
115text t403 "Interplane refused the session (sign-in, CSRF or origin)"
116text tref "Interplane refused the project: "
117text tkeyerr "could not make or read the key in ~/.interplane"
118text tsigerr "ssh-keygen could not sign"
119text tnohome "HOME is not set"
120text tshabad "the download does not match its published SHA-256; nothing was unpacked"
121text tridbad "not a project page address or Radicle ID"
122text tnodl "this project has no download"
123text tnokey "only the computer that brought a project can withdraw it, and this computer has no Interplane key (~/.interplane/key); nothing sent"
124text tnotmine "this project was brought by another key; only the computer that brought it can withdraw it; nothing sent"
125text tnoshelf "this project is not on the Interplane shelf"
126text tjby "\"brought_by\":\""
127text twd1 "Project: "
128text twd2 "Brought by: "
129text twdq "Withdrawing takes your project off the Interplane shelf, stops this site serving it and stops the Interplane hub sharing it on Radicle. It cannot remove copies that other computers already fetched: publishing was permanent.\nType yes to withdraw it, anything else to stop:\n"
130text twdact "&action=withdraw&rid=rad%3A"
131text twdwait "Interplane is taking the project off the shelf. This can take a minute.\n"
132text twdone "Withdrawn.\n"
133text tsgnout "Interplane signed you out before the withdraw was sent; nothing was withdrawn. Run the line again."
134text tlocn "location:"
135text tloginp "/login"
136text tcss "://"
137global gbase uchar 4096
138global gbasel ulong 1
139global ghome uchar 4096
140global ghomel ulong 1
141global gpre uchar 4096
142global gprel ulong 1
143global gsess uchar 4096
144global gsessl ulong 1
145global gcsrf uchar 4096
146global gcsrfl ulong 1
147global gval uchar 4096
148global ghttp uchar 1
149global gsha uchar 64
150global gname uchar 64
151global gnamel ulong 1
152global gtotal ulong 1
153
154fn envget key view uchar * o edit uchar * to long
155b = new uchar 65536 0
156nn = pl_envc
157for i ulong ulong nn bound 9223372036854775807
158r = pl_env i b
159if gt r 0
160if bat part b 0 ulong r 0 key
161k = sub ulong r len key
162for j ulong k bound 9223372036854775807
163put o j get b add len key j
164end
165ret long k
166end
167end
168end
169ret -1
170end
171fn setbase to void
172o = new uchar 4096 0
173n = envget tbenv o
174if lt n 0
175n = long cpy o 0 tdefb
176end
177k = ulong n
178while and gt k 0 eq get o sub k 1 47 bound 4096
179k = sub k 1
180end
181h = bat part o 0 k 0 thttps
182if not h
183if not or bat part o 0 k 0 tloop1 bat part o 0 k 0 tloop2
184die tbadb
185end
186put ghttp 0 1
187end
188if or le k 8 gt k 4000
189die tbadb
190end
191for i ulong k bound 4096
192c = get o i
193if or le c 32 ge c 127
194die tbadb
195end
196if or eq c 34 eq c 92
197die tbadb
198end
199put gbase i c
200end
201put gbasel 0 k
202hm = new uchar 4096 0
203m = envget thomee hm
204if le m 0
205die tnohome
206end
207for i ulong ulong m bound 4096
208put ghome i get hm i
209end
210put ghomel 0 ulong m
211end
212fn tmpfd nm view uchar * to int
213ret topen nm 2
214end
215fn nullfd to int
216fd = pl_open tdevnul 4 0
217if lt fd 0
218die terrio
219end
220ret int fd
221end
222note runs an argv. stdout goes to tmp file outnm, stderr to cerr.
223fn runprog a view uchar * outnm view uchar * to long
224i = nullfd
225o = tmpfd outnm
226e = tmpfd tcerr
227r = pl_run part a 0 get gavn 0 i o e
228pl_close i
229pl_close o
230pl_close e
231ret r
232end
233fn avtmp a edit uchar * nm view uchar * to void
234p = new uchar 4096 0
235k = tpath nm p
236avadd a part p 0 k
237end
238fn wrcookie fd int to void
239mustwrite fd tcook
240first = true
241if gt get gprel 0 0
242mustwrite fd tpre
243wglob fd 2
244first = false
245end
246if gt get gsessl 0 0
247if not first
248mustwrite fd tsemi
249end
250mustwrite fd tsess
251wglob fd 3
252end
253end
254fn showcerr to void
255fd = topen tcerr 0
256b = new uchar 4096 0
257while true bound 9223372036854775807
258r = read int fd b 0 len b
259if le r 0
260break
261end
262warn part b 0 ulong r
263end
264pl_close int fd
265end
266note GET when post is false; POST sends the form temp file. Returns HTTP status.
267fn http path view uchar * post bool outnm view uchar * to ulong
268reqhfile post
269a = new uchar 4096 0
270curlargs a post outnm
271u = new uchar 8192 0
272un = gcopy 0 u
273un = cpy u un path
274avadd a part u 0 un
275r = runprog a tcode
276if ne r 0
277showcerr
278die tnetbad
279end
280ret readcode
281end
282note busy: 503, 502, 504 and 429 mean the server is busy (dl's cold-build cache answers 503 with
283note Retry-After for up to a minute; the write gate answers 429 and 503).
284fn busy c ulong to bool
285ret or or eq c 503 eq c 502 or eq c 504 eq c 429
286end
287note waitbusy: the plain busy line, then 10 seconds
288fn waitbusy to void
289warn tbusy
290for i ulong 10
291pl_sleep
292end
293end
294note hget: a GET that waits out a busy server for as long as it stays busy (no count, the owner's
295note no-hard-caps rule; the bound is the language's loop bound, not a give-up count). Any other code
296note is returned to the caller unchanged.
297fn hget path view uchar * outnm view uchar * to ulong
298while true bound 9223372036854775807
299c = http path false outnm
300if not busy c
301ret c
302end
303waitbusy
304end
305ret 0
306end
307fn reqhfile post bool to void
308fd = tmpfd treqh
309mustwrite fd torig
310wglob fd 0
311mustwrite fd tnl
312if or gt get gprel 0 0 gt get gsessl 0 0
313wrcookie fd
314end
315if post
316mustwrite fd tctype
317end
318mustwrite fd tnl
319pl_close fd
320end
321fn curlargs a edit uchar * post bool outnm view uchar * to void
322avinit a
323avadd a tcurl
324avadd a tq
325avadd a tss
326avadd a tproto
327if eq get ghttp 0 1
328avadd a tproth
329avadd a tpredir
330avadd a tproth
331else
332avadd a tprots
333avadd a tpredir
334avadd a tprots
335end
336avadd a tmaxr
337avadd a tzero
338avadd a tdh
339avtmp a thdr
340avadd a tdo
341avtmp a outnm
342avadd a tdw
343avadd a thcode
344avadd a tdhh
345avat a treqh
346if post
347avadd a tdata
348avat a tform
349end
350end
351note avat: one argument "@<temp path of nm>" (curl reads that file)
352fn avat a edit uchar * nm view uchar * to void
353p = new uchar 4096 0
354k = tpath nm p
355q = new uchar 4097 0
356put q 0 64
357for i ulong k bound 4096
358put q add i 1 get p i
359end
360avadd a part q 0 add k 1
361end
362fn readcode to ulong
363c = new uchar 8 0
364fd = topen tcode 0
365n = read int fd c 0 8
366pl_close fd
367if ne n 3
368die tbadrsp
369end
370v = new ulong 1 0
371if not pdec part c 0 3 v
372die tbadrsp
373end
374ret get v 0
375end
376note fval: the bytes after marker up to term in temp file nm (into gval); -1 absent, -2 too long or
377note malformed. The file is read through a 64 KiB window that keeps its last 8 KiB, so a response
378note of any size is scanned; a value is a wire field (cookie, csrf, sha, rid, path) of at most 4096 bytes.
379fn fval nm view uchar * mk view uchar * term uchar to long
380fd = topen nm 0
381b = new uchar 73728 0
382n = ulong 0
383while true bound 9223372036854775807
384r = read int fd b n 65536
385if lt r 0
386die terrio
387end
388tot = add n ulong r
389v = fscan part b 0 tot mk term eq r 0
390if ne v -3
391pl_close int fd
392ret v
393end
394n = fkeep b tot
395end
396ret -1
397end
398note fkeep: move the last 8192 bytes of b[0, tot) to the front; returns the kept count
399fn fkeep b edit uchar * tot ulong to ulong
400if le tot 8192
401ret tot
402end
403s = sub tot 8192
404for x ulong 8192
405put b x get b add s x
406end
407ret 8192
408end
409note fscan: -3 when more input is needed (no marker yet, or a value runs to the window's end)
410fn fscan b view uchar * mk view uchar * term uchar eof bool to long
411i = ulong 0
412while le add i len mk len b bound 9223372036854775807
413if bat b i mk
414ret fvalat b add i len mk term eof
415end
416i = add i 1
417end
418if eof
419ret -1
420end
421ret -3
422end
423fn fvalat b view uchar * j ulong term uchar eof bool to long
424k = ulong 0
425p = j
426while lt p len b bound 9223372036854775807
427c = get b p
428if eq c term
429ret long k
430end
431if or ge k 4096 or lt c 32 eq c 92
432ret -2
433end
434put gval k c
435k = add k 1
436p = add p 1
437end
438if not eof
439ret -3
440end
441if eq term 10
442ret long k
443end
444ret -2
445end
446fn mustval mk view uchar * term uchar o edit uchar * to ulong
447r = fval tbody mk term
448if lt r 0
449die tbadrsp
450end
451if gt ulong r len o
452die tbadrsp
453end
454for i ulong ulong r bound 4096
455put o i get gval i
456end
457ret ulong r
458end
459note cookie value from response headers (case-insensitive header name).
460fn getcookie nm view uchar * which ulong to void
461fd = topen thdr 0
462o = new uchar 4096 0
463b = new uchar 1 0
464line = new uchar 8192 0
465n = ulong 0
466while true bound 9223372036854775807
467r = read int fd b 0 1
468if lt r 0
469die terrio
470end
471eol = or eq r 0 eq get b 0 10
472if not eol
473if lt n len line
474put line n get b 0
475end
476n = add n 1
477end
478if eol
479m = n
480if gt m len line
481m = len line
482end
483cookieln part line 0 m nm which
484n = ulong 0
485if eq r 0
486break
487end
488end
489end
490pl_close int fd
491end
492fn cookieln line view uchar * nm view uchar * which ulong to void
493if not bati line 0 tsetc
494ret
495end
496if not bat line len tsetc nm
497ret
498end
499o = new uchar 4096 0
500s = add len tsetc len nm
501k = ulong 0
502while lt add s k len line bound 4096
503c = get line add s k
504if or eq c 59 le c 32
505break
506end
507if ge c 127
508break
509end
510put o k c
511k = add k 1
512end
513gset which part o 0 k
514end
515fn checkhttp c ulong to void
516if eq c 413
517die t413
518end
519if eq c 503
520die t503
521end
522if eq c 403
523die t403
524end
525e = new uchar 24 0
526k = decw c e 0
527die2 thttpbad part e 0 k
528end
529fn joinhome nm view uchar * o edit uchar 4096 to ulong
530n = gcopy 1 o
531ret cpy o n nm
532end
533fn makekey to void
534p = new uchar 4096 0
535k = joinhome tdir p
536r = pl_mkdir part p 0 k 448
537st = new uchar 256 0
538if lt pl_stat part p 0 k st 0
539die tkeyerr
540end
541if ne and pl_mode st 61440 16384
542die tkeyerr
543end
544k = joinhome tkeynm p
545a = new uchar 4096 0
546if lt pl_stat part p 0 k st 0
547avinit a
548avadd a tsshk
549avadd a tq
550avadd a ttype
551avadd a ted
552avadd a tnpw
553avadd a part tnpw 0 0
554avadd a tcmt
555avadd a tinter
556avadd a tf
557avadd a part p 0 k
558if ne runprog a tbody 0
559showcerr
560die tkeyerr
561end
562end
563end
564fn readpk pk edit uchar 32 to void
565p = new uchar 4096 0
566k = joinhome tpubnm p
567fd = pl_open part p 0 k 0 0
568if lt fd 0
569die tkeyerr
570end
571b = new uchar 4096 0
572n = read int fd b 0 len b
573pl_close int fd
574if or le n long len tssh not bat part b 0 ulong n 0 tssh
575die tkeyerr
576end
577s = len tssh
578e = s
579while and lt e ulong n ne get b e 32 bound 4096
580if eq get b e 10
581break
582end
583e = add e 1
584end
585blob = new uchar 64 0
586m = b64d part b s sub e s blob
587if ne m 51
588die tkeyerr
589end
590if or ne rd32 blob 0 184549376 ne get blob 18 32
591die tkeyerr
592end
593for i ulong 32
594put pk i get blob add 19 i
595end
596end
597fn sign nm view uchar * ns view uchar * to void
598p = new uchar 4096 0
599kp = new uchar 4096 0
600kn = joinhome tkeynm kp
601sn = new uchar 4096 0
602k = tpath nm sn
603a = new uchar 4096 0
604avinit a
605avadd a tsshk
606avadd a tsig1
607avadd a tsign
608avadd a tf
609avadd a part kp 0 kn
610avadd a tn
611avadd a ns
612avadd a part sn 0 k
613if ne runprog a tbody 0
614showcerr
615die tsigerr
616end
617end
618fn formput fd int t view uchar * to void
619mustwrite fd t
620end
621fn formsig fd int nm view uchar * to void
622src = topen nm 0
623b = new uchar 4096 0
624o = new uchar 12288 0
625while true bound 9223372036854775807
626r = read int src b 0 len b
627if lt r 0
628die terrio
629end
630if eq r 0
631break
632end
633k = formw part b 0 ulong r o 0
634mustwrite fd part o 0 k
635end
636pl_close int src
637end
638note signin0 pk: sign in (GET /login, the signed challenge, POST /login); the session cookie is kept
639fn signin0 pk view uchar 32 to void
640put gprel 0 0
641put gsessl 0 0
642c = http tlogin false tbody
643if ne c 200
644checkhttp c
645end
646getcookie tpre 2
647if eq get gprel 0 0
648die tbadrsp
649end
650cs = new uchar 4096 0
651csl = mustval tcsrf 34 cs
652no = new uchar 4096 0
653nol = mustval tnonce 34 no
654tv = new uchar 4096 0
655tvl = mustval tts 34 tv
656fd = tmpfd tchal
657mustwrite fd tchal1
658mustwrite fd part no 0 nol
659mustwrite fd tcolon1
660mustwrite fd part tv 0 tvl
661pl_close fd
662sign tchal tnsf
663d = new uchar 128 0
664dl = didkey pk d
665fd = tmpfd tform
666mustwrite fd tfcsrf
667formenc fd part cs 0 csl
668mustwrite fd tfnonce
669formenc fd part no 0 nol
670mustwrite fd tfts
671formenc fd part tv 0 tvl
672mustwrite fd tfdid
673formenc fd part d 0 dl
674mustwrite fd tfsig
675formsig fd tchsig
676pl_close fd
677c = http tlogin true tbody
678if and ne c 303 ne c 200
679checkhttp c
680end
681getcookie tsess 3
682if eq get gsessl 0 0
683die t403
684end
685end
686note csrfof p: the session's write csrf, read from the page p
687fn csrfof p view uchar * to void
688c = http p false tbody
689if ne c 200
690checkhttp c
691end
692cs2 = new uchar 4096 0
693k2 = mustval tcsrf 34 cs2
694gset 4 part cs2 0 k2
695end
696note signin pk: sign in, then take the write csrf from GET /open/ (bring, withdraw). interplane patch
697note takes it from /settings instead (50patch.om): a store-reading forge answers /open/ with its 503
698note "moving" page, and the session's settings page carries the same token on every build
699fn signin pk view uchar 32 to void
700signin0 pk
701csrfof topenp
702end
703fn formenc fd int s view uchar * to void
704o = new uchar 12288 0
705k = formw s o 0
706mustwrite fd part o 0 k
707end
708fn csrfform fd int to void
709mustwrite fd tfcsrf
710cs = new uchar 4096 0
711k = gcopy 4 cs
712formenc fd part cs 0 k
713end
714fn nextval c ulong to ulong
715b = new uchar 64 0
716fd = topen tbody 0
717n = read int fd b 0 len b
718pl_close int fd
719if or lt n long len tnext not bat part b 0 ulong n 0 tnext
720die tbadrsp
721end
722e = ulong n
723while and gt e len tnext or eq get b sub e 1 10 eq get b sub e 1 13 bound 64
724e = sub e 1
725end
726v = new ulong 1 0
727if not pdec part b len tnext sub e len tnext v
728die tbadrsp
729end
730ret get v 0
731end
732fn wdec fd int v ulong to void
733d = new uchar 24 0
734k = decw v d 0
735mustwrite fd part d 0 k
736end
737fn progress done ulong total ulong to void
738x = done
739if gt x total
740x = total
741end
742cr = new uchar 1 0
743put cr 0 13
744warn cr
745warn tsent
746warnd x
747warn tof
748warnd total
749warn tbsuf
750end
751note sendchunks: 45000-byte chunks in seq order. 200 and 409 both name the next seq the server
752note wants; 429 waits a second and retries the same chunk for as long as the server says 429;
753note 503 (queue or storage full) says so in a line, waits 10 seconds and resends the same chunk.
754fn sendchunks tar int sh view uchar * total ulong to void
755nch = div add total 44999 45000
756seq = ulong 0
757while lt seq nch bound 9223372036854775807
758progress mul seq 45000 total
759chunkform tar sh seq total
760c = http tup true tbody
761if eq c 429
762pl_sleep
763continue
764end
765if eq c 503
766warn tnl
767waitbusy
768continue
769end
770if and ne c 200 ne c 409
771checkhttp c
772end
773nx = nextval c
774if gt nx nch
775die tbadrsp
776end
777seq = nx
778end
779progress total total
780warn tnl
781end
782fn chunkform tar int sh view uchar * seq ulong total ulong to void
783b = new uchar 45000 0
784e = new uchar 60000 0
785pos = mul seq 45000
786k = sub total pos
787if gt k 45000
788k = ulong 45000
789end
790r = pl_readat tar part b 0 k pos
791if ne r long k
792die terrio
793end
794el = b64u part b 0 k e
795fd = tmpfd tform
796csrfform fd
797mustwrite fd tachunk
798mustwrite fd sh
799mustwrite fd tfseq
800wdec fd seq
801mustwrite fd tfdata
802mustwrite fd part e 0 el
803pl_close fd
804end
805note bringflow: runs only after the exact "yes". Key, archive, statement and its signature, sign-in,
806note chunks, finish, status poll, final lines.
807fn bringflow folder view uchar * to void
808setbase
809makekey
810pk = new uchar 32 0
811readpk pk
812mkarch
813mkname
814mkstmt
815sign tstmt tnsb
816signin pk
817sh = new uchar 64 0
818k = shacopy sh
819tr = topen ttar 0
820sendchunks tr sh get gtotal 0
821pl_close tr
822finish sh
823pollup sh
824finalln
825end
826fn shacopy o edit uchar 64 to ulong
827for i ulong 64
828put o i get gsha i
829end
830ret 64
831end
832fn mkarch to void
833arf = tmpfd ttar
834archive arf
835pl_close arf
836tr = topen ttar 0
837d = new uchar 32 0
838shafd tr d
839st = new uchar 256 0
840if lt pl_fstat tr st 0
841die terrio
842end
843put gtotal 0 pl_size st
844pl_close tr
845h = new uchar 64 0
846k = hexw d h 0
847for i ulong 64
848put gsha i get h i
849end
850end
851note mkname: the statement name from the last component of the folder's absolute path
852fn mkname to void
853cw = new uchar 4096 0
854cl = pl_cwd cw
855base = ulong 0
856for i ulong cl bound 4096
857if eq get cw i 47
858base = add i 1
859end
860end
861nm = new uchar 64 0
862nl = projname part cw base sub cl base nm
863for i ulong nl bound 64
864put gname i get nm i
865end
866put gnamel 0 nl
867end
868fn wname fd int to void
869nm = new uchar 64 0
870n = get gnamel 0
871for i ulong n bound 64
872put nm i get gname i
873end
874mustwrite fd part nm 0 n
875end
876fn mkstmt to void
877sh = new uchar 64 0
878k = shacopy sh
879fd = tmpfd tstmt
880mustwrite fd tst1
881mustwrite fd sh
882mustwrite fd tst2
883wdec fd get gcount 0
884mustwrite fd tst3
885wdec fd get gtotal 0
886mustwrite fd tst4
887wname fd
888mustwrite fd tnl
889pl_close fd
890end
891fn finish sh view uchar * to void
892fd = tmpfd tform
893csrfform fd
894mustwrite fd tafin
895mustwrite fd sh
896mustwrite fd tffiles
897wdec fd get gcount 0
898mustwrite fd tfbytes
899wdec fd get gtotal 0
900mustwrite fd tfname
901wname fd
902mustwrite fd tfcons
903mustwrite fd tfsig
904formsig fd tstsig
905pl_close fd
906c = http tup true tbody
907while or eq c 503 eq c 429 bound 9223372036854775807
908if eq c 429
909pl_sleep
910else
911waitbusy
912end
913c = http tup true tbody
914end
915if ne c 200
916checkhttp c
917end
918q = new uchar 4096 0
919ql = mustval tqueued 10 q
920if lt ql 64
921die tbadrsp
922end
923if not bat part q 0 ql 0 sh
924die tbadrsp
925end
926end
927fn pollup sh view uchar * to void
928p = new uchar 4096 0
929pn = cpy p 0 tupj
930pn = cpy p pn sh
931pn = cpy p pn tjson
932said = false
933s = new uchar 4096 0
934while true bound 9223372036854775807
935c = hget part p 0 pn tbody
936if ne c 200
937checkhttp c
938end
939sl = mustval tstate 34 s
940if beq part s 0 sl tstored
941break
942end
943if bat part s 0 sl 0 trefused
944die2 tref part s 0 sl
945end
946if not said
947say twait
948said = true
949end
950pl_sleep
951end
952end
953fn finalln to void
954rid = new uchar 4096 0
955rl = mustval trid 34 rid
956pg = new uchar 4096 0
957gl = mustval tpage 34 pg
958if eq gl 0
959die tbadrsp
960end
961if ne get pg 0 47
962die tbadrsp
963end
964bb = new uchar 4096 0
965bl = gcopy 0 bb
966say tpubd
967say part bb 0 bl
968sayp part pg 0 gl
969say tnl
970say tdev
971sayp part rid 0 rl
972say tclone
973sayp part rid 0 rl
974say tnl
975end
976fn b58ok c uchar to bool
977for i ulong 58
978if eq get tb58 i c
979ret true
980end
981end
982ret false
983end
984note ridarg: the RID in a page address (<base>/open/<rid>), rad:<rid> or a bare <rid>, checked and
985note copied into o; anything else stops with tridbad
986fn ridarg arg view uchar * o edit uchar * to ulong
987s = ulong 0
988pfx = new uchar 8192 0
989pn = gcopy 0 pfx
990pn = cpy pfx pn topenp
991if bat arg 0 part pfx 0 pn
992s = pn
993else
994if bat arg 0 trad
995s = len trad
996end
997end
998e = len arg
999while and gt e s eq get arg sub e 1 47 bound 9223372036854775807
1000e = sub e 1
1001end
1002ret ridcopy part arg s sub e s o
1003end
1004fn ridcopy rid view uchar * o edit uchar * to ulong
1005if or lt len rid 20 gt len rid 60
1006die tridbad
1007end
1008if ne get rid 0 122
1009die tridbad
1010end
1011for i ulong len rid bound 60
1012if not b58ok get rid i
1013die tridbad
1014end
1015put o i get rid i
1016end
1017ret len rid
1018end
1019fn ridjson rid view uchar * p edit uchar * to ulong
1020n = cpy p 0 topenp
1021n = cpy p n rid
1022ret cpy p n tjson
1023end
1024note getflow: r is made before setbase: ng front-ir (macOS build) reports "not lowered" for a new
1025note right after a leading setbase here; omc accepts both orders (PLATFORMS.md, ng defects)
1026fn getflow arg view uchar * to void
1027r = new uchar 64 0
1028setbase
1029n = ridarg arg r
1030getflow2 part r 0 n
1031end
1032fn getflow2 rid view uchar * to void
1033p = new uchar 4096 0
1034n = ridjson rid p
1035c = hget part p 0 n tbody
1036if ne c 200
1037checkhttp c
1038end
1039dl = new uchar 4096 0
1040if lt fval tbody tdl 34 0
1041die tnodl
1042end
1043dn = mustval tdl 34 dl
1044getmeta
1045getfile part dl 0 dn
1046end
1047note wdflow: interplane withdraw. The key must already exist (it is never made here); the shelf JSON
1048note names the project and its bringer; a bringer that is not this key's DID stops before anything
1049note is sent; then the plain text and one line from /dev/tty. Only after the exact yes: sign-in (with
1050note the csrf from GET /open/), POST /open/ action=withdraw, and the JSON polled until 404 (off the
1051note shelf). False when the answer was not yes: nothing was sent.
1052fn wdflow arg view uchar * to bool
1053setbase
1054haskey
1055r = new uchar 64 0
1056n = ridarg arg r
1057pk = new uchar 32 0
1058readpk pk
1059p = new uchar 4096 0
1060pn = ridjson part r 0 n p
1061wdcheck pk part p 0 pn
1062if not consent twdq
1063ret false
1064end
1065signin pk
1066wdpost part r 0 n
1067wdpoll part p 0 pn
1068say twdone
1069ret true
1070end
1071fn haskey to void
1072p = new uchar 4096 0
1073st = new uchar 256 0
1074k = joinhome tkeynm p
1075if lt pl_stat part p 0 k st 0
1076die tnokey
1077end
1078k = joinhome tpubnm p
1079if lt pl_stat part p 0 k st 0
1080die tnokey
1081end
1082end
1083note wdcheck: the shelf row's name and brought_by, shown; brought_by must be this key's DID
1084fn wdcheck pk view uchar 32 p view uchar * to void
1085c = hget p tbody
1086if eq c 404
1087die tnoshelf
1088end
1089if ne c 200
1090checkhttp c
1091end
1092nm = new uchar 4096 0
1093nl = mustval tjname 34 nm
1094by = new uchar 4096 0
1095bl = mustval tjby 34 by
1096say twd1
1097sayp part nm 0 nl
1098say tnl
1099say twd2
1100sayp part by 0 bl
1101say tnl
1102d = new uchar 128 0
1103dl = didkey pk d
1104if not beq part by 0 bl part d 0 dl
1105die tnotmine
1106end
1107end
1108fn wdpost rid view uchar * to void
1109fd = tmpfd tform
1110csrfform fd
1111mustwrite fd twdact
1112mustwrite fd rid
1113pl_close fd
1114c = http topenp true tbody
1115while busy c bound 9223372036854775807
1116if eq c 429
1117pl_sleep
1118else
1119waitbusy
1120end
1121c = http topenp true tbody
1122end
1123if ne c 303
1124checkhttp c
1125end
1126lo = new uchar 4096 0
1127ln = hdrloc lo
1128if wdopen part lo 0 ln
1129ret
1130end
1131if gt bfind part lo 0 ln tloginp 0 -1
1132warn tsgnout
1133warn tnl
1134cleanup
1135exit 1
1136end
1137checkhttp c
1138end
1139note wdopen: the withdraw was taken only when the 303 goes to the shelf: Location is <base>/open/,
1140note absolute or as the base's path (the hub writes its base path, e.g. /open/)
1141fn wdopen loc view uchar * to bool
1142bb = new uchar 4096 0
1143bl = gcopy 0 bb
1144bl = cpy bb bl topenp
1145if beq loc part bb 0 bl
1146ret true
1147end
1148p = bfind part bb 0 bl tcss 0
1149if lt p 0
1150ret false
1151end
1152q = add ulong p len tcss
1153while and lt q bl ne get bb q 47 bound 4096
1154q = add q 1
1155end
1156ret beq loc part bb q sub bl q
1157end
1158note hdrloc: the Location header of the last answer (temp file hdr) into o; its length, 0 when none
1159fn hdrloc o edit uchar * to ulong
1160fd = topen thdr 0
1161b = new uchar 1 0
1162line = new uchar 8192 0
1163n = ulong 0
1164k = ulong 0
1165while true bound 9223372036854775807
1166r = read int fd b 0 1
1167if lt r 0
1168die terrio
1169end
1170eol = or eq r 0 eq get b 0 10
1171if not eol
1172if lt n len line
1173put line n get b 0
1174end
1175n = add n 1
1176end
1177if eol
1178if gt n len line
1179n = len line
1180end
1181v = locln part line 0 n o
1182if gt v 0
1183k = v
1184end
1185n = ulong 0
1186if eq r 0
1187break
1188end
1189end
1190end
1191pl_close int fd
1192ret k
1193end
1194fn locln line view uchar * o edit uchar * to ulong
1195if not bati line 0 tlocn
1196ret 0
1197end
1198s = len tlocn
1199while and lt s len line eq get line s 32 bound 8192
1200s = add s 1
1201end
1202k = ulong 0
1203while and lt add s k len line lt k len o bound 8192
1204c = get line add s k
1205if le c 32
1206break
1207end
1208put o k c
1209k = add k 1
1210end
1211ret k
1212end
1213fn wdpoll p view uchar * to void
1214said = false
1215while true bound 9223372036854775807
1216c = hget p tbody
1217if eq c 404
1218ret
1219end
1220if ne c 200
1221checkhttp c
1222end
1223if not said
1224say twdwait
1225said = true
1226end
1227pl_sleep
1228end
1229end
1230note getmeta: download_sha256 into gsha, the folder name (the statement rule) into gname
1231fn getmeta to void
1232want = new uchar 4096 0
1233wn = mustval tdlsha 34 want
1234if ne wn 64
1235die tbadrsp
1236end
1237for i ulong 64
1238c = get want i
1239if not or isdig c and ge c 97 le c 102
1240die tbadrsp
1241end
1242put gsha i c
1243end
1244nm0 = new uchar 4096 0
1245nn0 = mustval tjname 34 nm0
1246nm = new uchar 64 0
1247nl = projname part nm0 0 nn0 nm
1248for i ulong nl bound 64
1249put gname i get nm i
1250end
1251put gnamel 0 nl
1252end
1253fn getfile dl view uchar * to void
1254if or lt len dl 7 not bat dl 0 topenp
1255die tbadrsp
1256end
1257for i ulong len dl bound 4096
1258c8 = get dl i
1259if not or unres c8 eq c8 47
1260die tbadrsp
1261end
1262end
1263if gt bfind dl tdotdot 0 -1
1264die tbadrsp
1265end
1266c = hget dl tgettar
1267if ne c 200
1268checkhttp c
1269end
1270tr = topen tgettar 0
1271d = new uchar 32 0
1272shafd tr d
1273h = new uchar 64 0
1274k = hexw d h 0
1275want = new uchar 64 0
1276k = shacopy want
1277if not beq h want
1278die tshabad
1279end
1280r = pl_seek tr 0
1281tarwalk tr false
1282r = pl_seek tr 0
1283unpackin tr
1284pl_close tr
1285end
1286fn unpackin tr int to void
1287nm = new uchar 64 0
1288n = get gnamel 0
1289for i ulong n bound 64
1290put nm i get gname i
1291end
1292if ne pl_mkdir part nm 0 n 493 0
1293die texists
1294end
1295if lt pl_cd part nm 0 n 0
1296die texists
1297end
1298tarwalk tr true
1299say tsaved
1300say part nm 0 n
1301say tnl
1302end
1303fn gcopy which ulong o edit uchar * to ulong
1304n = ulong 0
1305if eq which 0
1306n = get gbasel 0
1307for i ulong n bound 4096
1308put o i get gbase i
1309end
1310end
1311if eq which 1
1312n = get ghomel 0
1313for i ulong n bound 4096
1314put o i get ghome i
1315end
1316end
1317if eq which 2
1318n = get gprel 0
1319for i ulong n bound 4096
1320put o i get gpre i
1321end
1322end
1323if eq which 3
1324n = get gsessl 0
1325for i ulong n bound 4096
1326put o i get gsess i
1327end
1328end
1329if eq which 4
1330n = get gcsrfl 0
1331for i ulong n bound 4096
1332put o i get gcsrf i
1333end
1334end
1335ret n
1336end
1337fn gset which ulong s view uchar * to void
1338if gt len s 4096
1339die tbadrsp
1340end
1341for i ulong len s bound 4096
1342if eq which 2
1343put gpre i get s i
1344end
1345if eq which 3
1346put gsess i get s i
1347end
1348if eq which 4
1349put gcsrf i get s i
1350end
1351end
1352if eq which 2
1353put gprel 0 len s
1354end
1355if eq which 3
1356put gsessl 0 len s
1357end
1358if eq which 4
1359put gcsrfl 0 len s
1360end
1361end
1362fn wglob fd int which ulong to void
1363o = new uchar 4096 0
1364n = gcopy which o
1365mustwrite fd part o 0 n
1366end