summaryrefslogtreecommitdiff
path: root/url_to_id
blob: f9317e6d763b91d4ae1d4a6fe6e79c74555b1531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/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