💾 Archived View for runjimmyrunrunyoufuckerrun.com › rc › webshit captured on 2022-03-01 at 15:21:31.

View Raw

More Information

⬅️ Previous capture (2021-12-17)

-=-=-=-=-=-=-

#!/bin/rc
# webshittery for use with the following plumbing rules:

# wdir is /mnt/webshit
# data matches \[submit\]|§|□|○|[⁰¹²³⁴⁵⁶⁷⁸⁹]+|hpost.*|←|→|{[0-9]*}
# plumb to webshit

# data matches (gemini|gopher|https?)://[a-zA-Z0-9\-._~:/?#[\]@!{body}amp;''()*+,;=%]+
# plumb to webshit
# plumb client window webshit

# and rules for image, audio & video ports to handle multimedia:

# dst is video
# plumb to video
# plumb client window tube

# dst is audio
# plumb to audio
# plumb client window playlist

# dst is image
# plumb to image
# plumb client page -iw

rfork en
width = 70
nl = '
'
fn jumpto{
	if(test -f $1){
		n = $1
		cat $n >/dev/text
		echo -n  >/dev/kbdin
	}
	if not
		read `{ls -rt}
}
fn format{
	if(test -s incoming){
		if(~ $mime text/*){
			n = `{ls | wc -l}
			if(~ $data *' '*)
				echo ← '{'^$n^'} → {'^$data^'}'^$nl >$n
			if not
				echo ← '{'^$n^'} →' $data^$nl >$n
			switch($mime){
			case text/gemini*
				tr -d 
 <incoming | awk '
					BEGIN{
						sup = "⁰¹²³⁴⁵⁶⁷⁸⁹"
						n = 1
						match(ENVIRON["data"], "^gemini://[^/]+")
						root = substr(ENVIRON["data"], 1, RLENGTH);
						if(match(ENVIRON["data"], "^gemini://.+/"))
							base = substr(ENVIRON["data"], 1, RLENGTH);
						else
							base = root "/"
					}
					function fn(i){
						s = ""
						while(i){
							j = int(i % 10) + 1
							s = substr(sup, j, 1) s
							i = int(i / 10)
						}
						return s
					}
					/^=>[	 ]/{
						sub(/^=>[	 ]+/, "")
						if(match($1, "^//"))
							url[n] = "gemini:"$1
						else if(match($1, "^/"))
							url[n] = root $1
						else if(match($1, "^[a-z]+://"))
							url[n] = $1
						else
							url[n] = base $1
						sub(/^[^ 	]+[	 ]+/, "")
						sub(/$/, fn(n++))
					}
					{print}
					END{
						if(n == 1) exit
						printf "\nReferences:\n"
						for(i = 1; i < n; i++)
							printf "%s%s\n", fn(i), url[i]
					}
				' | fmt -j -l $width >>$n
			case text/gopher*
				tr -d 
 <incoming | awk '
					BEGIN{
						FS = "	"
						sup = "⁰¹²³⁴⁵⁶⁷⁸⁹"
						n = 1
						t["0"] = "Text file: "
						t["1"] = "Directory: "
						t["2"] = "CSO name server: "
						t["4"] = "Mac HQX filer: "
						t["5"] = "PC binary: "
						t["6"] = "UNIX uuencoded file: "
						t["7"] = "Search server: "
						t["8"] = "Telnet Session: "
						t["9"] = "Binary File: "
						t["c"] = "Calendar: "
						t["e"] = "Event: "
						t["g"] = "GIF image: "
						t["s"] = "Sound: "
						t["I"] = "Image: "
						t["M"] = "MIME multipart/mixed message: "
						t["T"] = "TN3270 Session: "
						match(ENVIRON["data"], "^gopher://[^/:]+")
						host = substr(ENVIRON["data"], 10, RLENGTH - 9);
						match(ENVIRON["what"], ".*/")
						base = substr(ENVIRON["what"], 1, RLENGTH);
					}
					function fn(i){
						s = ""
						while(i){
							j = int(i % 10) + 1
							s = substr(sup, j, 1) s
							i = int(i / 10)
						}
						return s
					}
					NF == 1{print}
					NF > 1{
						type = substr($1, 1, 1)
						display = substr($1, 2, length($1) - 1)
						if(type == "i" || type == "3")
							print display
						else if(type == "h"){
							sub("^URL:", "", $2)
							url[n] = $2
							print "HTTP: " display fn(n++)
						}else{
							if($3 == "") s = host
							else s = $3
							if($4 != "" && $4 != "70")
								s = s ":" $4
							if($4 == "" && s == host && ENVIRON["port"] != "70")
								s = s ":" ENVIRON["port"]
							if(substr($2, 1, 1) == "/")
								url[n] = "gopher://" s "/" type $2
							else
								url[n] = "gopher://" s "/" type base $2
							print t[type] display fn(n++)
						}
					}
					END{
						if(n == 1) exit
						printf "References:\n"
						for(i = 1; i < n; i++)
							printf "%s%s\n", fn(i), url[i]
					}
				' >>$n
			case text/html*
				if(~ $data hpost*)
					data = `{echo $data | awk '{print $3}'}
				htmlfmt -u $data -l $width incoming >>$n
			case *
				tr -d 
 <incoming >>$n
			}
			cat $n >/dev/text
			echo -n  >/dev/kbdin
		}
		if not{
			if(~ $mime application/^(troff postscript pdf ghostscript))
				port = image
			if not
				port = `{echo $mime | sed 's,/.*,,'}
			if(! test -r /mnt/plumb/$port || ! plumb -id $port <incoming){
				echo `{du -h incoming | awk '{print $1}'} $mime
				echo -n  >/dev/kbdin
				k = ''
				while(~ $k ''){
					echo -n 'write: '
					k = `{read /dev/cons}
					if(! ~ $k /*){
						echo 'must be an absolute path! (/dev/null is always there…)'
						k = ''
					}
					if not if(test -d $k){
						echo $k is a directory!
						k = ''
					}
					if not if(test -f $k){
						echo $k exists! 𝐨verwrite?
						>[3]/dev/consctl{
							echo rawon >[1=3]
							if(! ~ `''{read -c 1 /dev/cons} o || ! echo -n >$k){
								k = ''
								echo try again!
							}
						}
					}
					if not{
						echo -n >$k
						if(! test -f $k)
							k = ''
					}
				}
				cp incoming $k
				echo written
			}
		}
	}
	rm -f incoming
}
fn gemget{
	echo $data^
	reply = `{read | tr -d 
}
	switch($reply(1)){
	case 1*
		echo $reply(2-)
		echo -n  >/dev/kbdin
		echo -n input: $data^'?' >[1=3]
		>/dev/consctl{
			echo holdon
			sed 's/ /%20/g' /dev/cons | urlencode | sed 's/%0A$//' >[1=3]
		}
	case 2*
		cat >incoming >[2]/dev/null
		echo $reply(2) | tr -d 
 >[1=3]
	case 3*	# REDIRECT
		echo redirect: $reply(2) | tr -d 
 >[1=3]
	case *	# ERRORS
		echo error: $reply(2-) >[1=3]
	}
}
ramfs -um /mnt/webshit
cd /mnt/webshit
echo -n /mnt/webshit >/dev/wdir
echo -n webshit >/dev/label
echo -n scroll >/dev/wctl
n = 0
</mnt/plumb/webshit while(>/dev/null{
	read; read; read; read; read
	ndata = `{read}
	data = `''{read -c $ndata}
})
	switch($data){
	case gemini://?* gopher://?* https://?* http://?*
		echo unhide >/dev/wctl >[2]/dev/null
		echo current >/dev/wctl
		echo fetching $data …
		proto = `{echo $data | sed 's,([^:]+).*,\1,'}
		host = `{echo $data | sed 's,[^:]+://([^:/]+).*,\1,'}
		port = `{echo $data | sed 's,[^:]+://[^:/]+:([0-9]+).*,\1,'}
		if(! ~ $port [0-9]*) port = $proto
		switch($proto){
		case gemini
			if(~ $port gemini) port = 1965
			mime = `{tlsclient tcp!$host!$port /bin/rc -c gemget >[3=1] >[2]/dev/null}
			switch($mime(1)){
			case input: redirect:
				plumb $mime(2)
			case error:
				echo $mime
			case *
				format
			}
		case gopher
			if(~ $port gopher) port = 70
			host = `{ndb/dnsgetip $host}
			item = `{echo $data | sed 's!^gopher://[^/]+/(.)/.*!\1!'}
			if(! ~ $item ?) item = 1
			what = `{echo $data | sed 's!^gopher://[^/]+(/./|/)?(.*)!/\2!'}
			if(~ $item 7){
				echo -n  >/dev/kbdin
				echo -n 'Enter search string: '
				what = $what^'	'^`$nl{read /dev/cons}
			}
			</net/tcp/clone{
				con = /net/tcp/`{read}
				echo -n connect $host!$port > $con/ctl
				echo $what^
 > $con/data
				cat $con/data
			}>incoming
			if(~ $item 1 7)
				mime = text/gopher
			if not
				mime = `{file -m incoming}
			format
		case http*
			<>/mnt/web/clone {
				d=/mnt/web/^`{sed 1q}
				echo -n url $data >[1=0]
				cp $d/body incoming
				data = `{cat $d/parsed/url}
				mime = `{cat $d/contenttype >[2]/dev/null}
			}
			if(~ $mime '' application/octet-stream)
				mime = `{file -m incoming >[2]/dev/null}
			format
		}
	case '[submit]' § □ ○
		if(test -f $n){
			data = `{read $n}
			if(~ $data(4) http*) hpost $data(4)
			if not if(~ $data(6) http*) hpost $data(6)
		}
	case hpost*
		eval $data >incoming
		mime = `{file -m incoming}
		echo unhide >/dev/wctl >[2]/dev/null
		echo current >/dev/wctl
		format
	case [⁰¹²³⁴⁵⁶⁷⁸⁹][⁰¹²³⁴⁵⁶⁷⁸⁹][⁰¹²³⁴⁵⁶⁷⁸⁹][⁰¹²³⁴⁵⁶⁷⁸⁹] [⁰¹²³⁴⁵⁶⁷⁸⁹][⁰¹²³⁴⁵⁶⁷⁸⁹][⁰¹²³⁴⁵⁶⁷⁸⁹] [⁰¹²³⁴⁵⁶⁷⁸⁹][⁰¹²³⁴⁵⁶⁷⁸⁹] [⁰¹²³⁴⁵⁶⁷⁸⁹]
		if(test -f $n)
			<$n awk '
				BEGIN{while($0 != "References:") getline}
				/^'^$data^' /{
					found = 1
					if(match($2, "https?://"))
						system("plumb ''"$2"''")
					else
						print "bad footnote reference: "$0
					exit
				}
				END{if(!found) print "no reference for footnote '^$data^'"}
			'
	case ←
		jumpto `{echo $n 1-p | dc}
	case →
		jumpto `{echo $n 1+p | dc}
	case '{'*'}'
		jumpto `{echo $data | tr -dc 0-9}
	case *
		echo received bad plumb: $data
	}