summaryrefslogtreecommitdiff
path: root/burrow
blob: 4ac35fd5c91d887d4682160a7cb9eea397ff2c83 (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
#!/bin/sh

### read from $1 and print on stdout a list of MENU selectors IDs
### in the format:
###
### 1|SELECTOR|HOST|PORT|SHA256
###
### where SHA256 is the SHA256SUM of "1|SELECTOR|HOST|PORT"
###
## function
get_dirs(){
	src_id="$1"
	OLDIFS=$IFS
	#IFS=$'\t\n'
	IFS="|"
	src_dir=$(echo "${src_id}" | cut -c -2)
	while read name sel host port; do
##		echo " --- name: \"$name\""
##		echo " --- sel: \"$sel\""
##		echo " --- host: \"$host\""
##		echo " --- port: \"$port\""
		TYPE=$(echo "$name" | cut -c 1)
##		echo " --- TYPE \"$TYPE\""
##		echo "$TYPE, $name, $sel, $host, $port"
		port=$(echo $port | sed -r -e 's/\r//g')
		dir_id=$(echo "${TYPE}|${sel}|${host}|${port}" | sed 's/^1||/1|\/|/g')
		dest_id=$(echo "${dir_id}" | sha256sum | cut -d " " -f 1 )
		echo "${src_id}" "${dest_id}" >&2
		[ "${TYPE}" = "1" ] && {
			echo "${dir_id}|${dest_id}" | tee -a ${src_dir}/${src_id}
		}
	done 
	IFS="$OLDIFS"
}


### get a MENU selector ID (as generated by get_dirs) and retrieves it
### from the gopherspace
### 
## function
retrieve_selector(){
	sel="$1"
	SEL=$(echo "$1" | cut -d "|" -f 2)
	HOST=$(echo "$1" | cut -d "|" -f 3)
	PORT=$(echo "$1" | cut -d "|" -f 4)
	printf "${SEL}\r\n" | netcat -w 15 "${HOST}" "${PORT}"

}

###
### Check if a given selector has been visited. In that case, just
### output the corresponding file (which contains all the outgoing links 
### of the selector) and exits
###
## function
check_selector_present(){
	sel_id="$1"
	sel_dir="$(echo ${sel_id} | cut -c -2)"
	[ -d "${sel_dir}" -a -f "${sel_dir}/${sel_id}" ] && cat ${sel_dir}/${sel_id} && echo ${sel_id}>>present && exit 1
	[ ! -d "${sel_dir}" ] && mkdir -p "${sel_dir}"
}


SRC="$1"

src_id=$(echo "$SRC" | cut -d "|" -f 5 )

check_selector_present "${src_id}"

echo "selector ${src_id} not found" >> logfile.txt

retrieve_selector "$SRC" | sed -r -e 's/\t/|/g' | get_dirs "${src_id}"