summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2019-02-17 08:33:41 +0000
committerKatolaZ <katolaz@freaknet.org>2019-02-17 08:33:41 +0000
commitaa624aff7cc03e780814f7447af1f7acf2873f7f (patch)
tree10d713b6d490f8fa9ce9371691c5598324e096cc
parentacd8a0891086e1f7bfbf0cb4a17aa3f9ab21f118 (diff)
accept URLs
-rw-r--r--README.md4
-rwxr-xr-xgophed37
2 files changed, 33 insertions, 8 deletions
diff --git a/README.md b/README.md
index bd05d1c..7749307 100644
--- a/README.md
+++ b/README.md
@@ -35,13 +35,13 @@ and `d`, which will be used as `gophed` commands. Once `gophed` has
been initialised, you can start visiting some gopherholes:
```
- ./gophed / kalos.mine.nu 70
+ ./gophed gopher://kalos.mine.nu
```
or equivalently:
```
- ./v / kalos.mine.nu 70
+ ./v gopher://kalos.mine.nu
```
This command will download the corresponding resource, and print it on
diff --git a/gophed b/gophed
index e512d25..64d9b16 100755
--- a/gophed
+++ b/gophed
@@ -14,8 +14,8 @@ go () {
usage() {
printf '%s\n' \
'usage:' \
- ' visit a resource:' \
- " $0 <path> <server> <port>" \
+ ' visit a Gopher URL:' \
+ " $0 <URL>" \
' init links:' \
" $0 -i" \
' show this help:' \
@@ -25,10 +25,35 @@ usage() {
" !./g % LINE" \
'to visit the resource at LINE, or:' \
" !./d % LINE" \
- 'to download the resource at LINE\n'
+ 'to download the resource at LINE\n'\
+ " !./v URL" \
+ 'to visit a Gopher URL'
exit 0
}
+goto_url(){
+ URL="$(echo $1 | sed 's,gopher://,,g')"
+ hostport=$(echo "$URL" | cut -d "/" -f 1)
+ host="$(echo $hostport | cut -d ":" -f 1)"
+ port="$(echo $hostport | cut -s -d ":" -f 2)"
+ [ -z "$port" ] && port='70'
+ type=$(echo "$URL" | cut -s -d "/" -f 2)
+ [ -z "$type" ] && {
+ type='1'
+ sel="/"
+ go $sel $host $port
+ return
+ }
+ [ -n "${type#?}" ] && echo "Invalid Gopher URL" >&2 && exit 1
+ ## Check if type is a valid one
+ type="$(echo $type | sed -n '/^[0-9ITghis+]$/p')"
+ [ -z "${type}" ] && echo "Invalid Gopher URL" >&2 && exit 1
+ sel=/$(echo "$URL" | cut -s -d "/" -f 3-)
+ go $sel $host $port
+}
+
+
+
trap cleanup INT QUIT HUP KILL ABRT TERM
[ "$#" -lt 1 ] || [ "$1" = "-h" ] && usage
@@ -46,8 +71,8 @@ fi
if [ "$script_name" = "gophed" ] || [ "$script_name" = "v" ]; then
sel="$(printf '%s' "$1" | sed -r 's:/:+:g')"
TMPFILE="$(mktemp "/tmp/gophed_${2}_${sel}_$3.XXXXXXXXXXXXXXXXXXX")"
- go "$1" "$2" "$3" > "$TMPFILE"
- cat "$SHOW" - | ed -p "$PROMPT" "$TMPFILE"
+ goto_url "$1" > "$TMPFILE"
+ while read line; do echo $line; done | ed -p "$PROMPT" "$TMPFILE"
cleanup
exit 0
fi
@@ -62,7 +87,7 @@ if [ "$script_name" = "g" ] || [ "$script_name" = "d" ]; then
SEL="$(printf '%s' "$RESOURCE" | sed -r 's:/:+:g')"
TMPFILE="$(mktemp "/tmp/gophed_${HOST}_${SEL}_$PORT.XXXXXXXXXXXXXXXXXXX")"
go "$RESOURCE" "$HOST" "$PORT" > "$TMPFILE"
- cat "$SHOW" - | ed -p "$PROMPT" "$TMPFILE"
+ while read line; do echo $line; done | ed -p "$PROMPT" "$TMPFILE"
fi
if [ "$script_name" = "d" ]; then
DIR_RESOURCE="$(dirname "$RESOURCE")"