diff options
author | KatolaZ <katolaz@freaknet.org> | 2019-01-23 22:46:02 +0000 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2019-01-23 22:46:02 +0000 |
commit | bdfe168ec85a774c585d6153ef98f6bd789a1b3c (patch) | |
tree | c110e3b7f1fdabe0a69a39a26c434dd06fea6438 |
initial commit
-rwxr-xr-x | gophed | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -0,0 +1,68 @@ +#!/bin/sh + + +cleanup () { + rm -f $TMPFILE; +} + +go () { + printf "${1}\r\n" | nc $2 $3 +} + +usage() { + printf "usage: \n" + printf " visit a resource:\n" + printf " $0 <path> <server> <port>\n" + printf " init links:\n" + printf " $0 -i \n" + printf " show this help:\n" + printf " $0 -h\n" + printf "\n" + printf "In a gopher page, just type:\n" + printf " !./visit %% LINE\n" + printf "to visit the resource at LINE, or:\n" + printf " !./dl %% LINE\n" + printf "to download the resource at LINE\n\n" + exit 0 +} + +trap cleanup INT QUIT HUP KILL ABRT TERM + +if [ "$1" = "-h" ]; then usage; fi + +if [ "$1" = "-i" ]; then + ln -s $0 visit + ln -s $0 dl + exit 0 +fi + +script_name=$(basename $0) + +if [ "${script_name}" = "gophed" ]; then + sel=$(echo $1 | sed -r 's:/:+:g') + TMPFILE=$(mktemp /tmp/gophed_$2_${sel}_$3.XXXXXXXXXXXXXXXXXXX) + go "$1" "$2" "$3" > $TMPFILE + cat show - show | ed $TMPFILE +fi +if [ "${script_name}" = "visit" ]; then + LINE=$(awk "{if (NR == $2) print \$0;}" $1) + RESOURCE=$(echo "$LINE" | cut -d ' ' -f 2) + HOST=$(echo "$LINE" | cut -d ' ' -f 3) + PORT=$(echo "$LINE" | awk -F ' ' '{ match($4,/[[:digit:]]+/); print substr($4,RSTART,RLENGTH) }') + SEL=$(echo $RESOURCE | sed -r 's:/:+:g') + TMPFILE=$(mktemp /tmp/gophed_${HOST}_${SEL}_${PORT}.XXXXXXXXXXXXXXXXXXX) + go "$RESOURCE" "$HOST" "$PORT" > $TMPFILE + cat show - show | ed $TMPFILE +fi +if [ "${script_name}" = "dl" ]; then + LINE=$(awk "{if (NR == $2) print \$0;}" $1) + RESOURCE=$(echo "$LINE" | cut -d ' ' -f 2) + HOST=$(echo "$LINE" | cut -d ' ' -f 3) + PORT=$(echo "$LINE" | awk -F ' ' '{ match($4,/[[:digit:]]+/); print substr($4,RSTART,RLENGTH) }') + DIR_RESOURCE=$(dirname $RESOURCE) + echo "Download $RESOURCE in ${HOST}${RESOURCE}" + mkdir -p "$HOST/$DIR_RESOURCE" + go "$RESOURCE" "$HOST" "$PORT" > "${HOST}${RESOURCE}" +fi + +cleanup |