|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
##
|
|
|
|
## Get a gopherlink in the format:
|
|
|
|
##
|
|
|
|
## gopher://domain.org:port/*/my/cool/selector
|
|
|
|
##
|
|
|
|
## or a selector in gph format:
|
|
|
|
##
|
|
|
|
## [TYPE|SEL|HOST|PORT]
|
|
|
|
##
|
|
|
|
## and print on output the corresponding "unique" selectorid:
|
|
|
|
##
|
|
|
|
## TYPE|SEL|HOST|PORT|SHA256
|
|
|
|
##
|
|
|
|
## which is understood by `burrow`
|
|
|
|
##
|
|
|
|
## -----------------------
|
|
|
|
##
|
|
|
|
## (C) 2018 Vincenzo 'KatolaZ' Nicosia <katolaz@freaknet.org>
|
|
|
|
##
|
|
|
|
## Use, modify, redistribute under the terms of the GNU General
|
|
|
|
## Public License version 3 or, at your option, any other version.
|
|
|
|
##
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
|
###
|
|
|
|
### get a selector in gph format and transform it in a selectorid
|
|
|
|
###
|
|
|
|
## function
|
|
|
|
gph_to_id(){
|
|
|
|
gph="$( echo $1| sed 's/\[//g;s/\]//g')"
|
|
|
|
OLDIFS=$IFS
|
|
|
|
#IFS=$'\t\n'
|
|
|
|
IFS="|"
|
|
|
|
sid=$(echo "${gph}" | sha256sum | cut -d " " -f 1)
|
|
|
|
echo "${gph}|${sid}"
|
|
|
|
IFS="$OLDIFS"
|
|
|
|
}
|
|
|
|
|
|
|
|
###
|
|
|
|
### Get a gopherurl and transform it in a selectorid
|
|
|
|
###
|
|
|
|
## function
|
|
|
|
gopherurl_to_id(){
|
|
|
|
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="/"
|
|
|
|
gph_to_id "[${type}|${sel}|${host}|$port]"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
[ -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-)
|
|
|
|
gph_to_id "[${type}|${sel}|${host}|$port]"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ $# -lt 1 ] && echo "Usage: $0 <gopherurl>" && echo " $0 <gphselector>" && exit 1
|
|
|
|
|
|
|
|
|
|
|
|
[ -n "$(echo $1 | sed -n '/^gopher:\/\//p')" ] && {
|
|
|
|
gopherurl_to_id "$1"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
[ -n "$(echo $1 | sed -n '/^\[.*\]$/p')" ] && {
|
|
|
|
gph_to_id "$1"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
echo "No valid URL or gph selector provided" >&2
|
|
|
|
exit 1
|