blob: 2b0d7f28b88f8b0cf6c6de31a4e10040f4436c36 (
plain) (
blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#!/bin/bash
### arguments:
# $1: the URI we're starting
# $2: the "referer" of the URI we're starting.
### requirements:
# getsrv, urimatch, urijoin, uricut, some-way-to-ask-the-user-for-a-line (dmenu by default)
# getsrv tries dig and host, you only need one or the other.
##might have to configure me.
ASKUSER="choose"
check=dont
eval "$(printf "%s\n" "$1" | uricut | sed 's/^/export URI/;s/'\''/'\''\\'\'''\''/;s/: /='\''/g;s/$/'\''/')"
uristart_config=~/.config/uristart.conf
usage() {
printf "usage: uristart [-v|--verbose|-h|--help|-c|--check] [-f|--config file] URI\n"
}
while case "$1" in
--help|-h)
usage
exit 0
;;
--verbose|-v)
verbose=verbose
;;
--config|-f)
shift
export uristart_config="$1"
;;
--check|-c)
check=check
;;
*)
break
;;
esac;do shift;done
if [ "$verbose" ];then
echo "uristart: verbose:" "$verbose"
echo "uristart: config:" "$uristart_config" >&2
echo "uristart: check:" "$check" >&2
echo "uristart: other:" "$@" >&2
fi
if [ ! "$*" ];then
usage >&2 #to stderr because it is an error to not have any extra args to process
exit 1
fi
if [ ! -e "$uristart_config" ];then
printf "uristart: missing config file: %s\n" "$uristart_config" >&2
exit 1
fi
uri="${1}"
scheme="$(printf "%s\n" "$uri" | uricut -s)"
line="$(urigetline "$uri")"
### if we do not have a port, we are going to try to get it from their srv records.
if printf "%s\n" "$uri" | urimatch nP >/dev/null;then
proto="$(grep ^"$scheme"'\s' /etc/services | tr ' ' '\t' | tr -s '\t ' | cut -f2 | cut -d/ -f2 | head -n1)"
if [ ! "$proto" ]; then
proto=tcp
fi
uri="$(cat <(printf "%s\n" "$uri" | uricut) \
<(getsrv $(printf "%s\n" "$uri" | uriprintf '%s '"$proto"' %d' | cut -d+ -f2-) 2>&- | tr ':' ' ' | sed 's/ /_port: /' | sed 's/^/domain: /' | tr _ '\n') \
| urijoin 2>/dev/null)"
[ "$verbose" ] && printf "uristart: srv (%s) modified uri: %s\n" "$proto" "$uri" >&2
fi
### if they do not have srv records, fall-back to /etc/services
if printf "%s\n" "$uri" | urimatch nP >/dev/null;then #if we *still* don't have a port
uri="$(cat <(printf "%s\n" "$uri" | uricut) \
<(grep ^"$scheme"'\s' /etc/services | tr ' ' '\t' | tr -s '\t' | cut -f2 | cut -d/ -f1 | head -n1 | sed 's/^/port: /') \
| urijoin 2>/dev/null)"
[ "$verbose" ] && printf "uristart: getent modified uri: %s\n" "$uri" >&2
fi
### ask the user for a pipeline for starting these URIs if there isn't a pipeline already in the config.
[ "$verbose" ] && echo "uristart: the uri we're using: $uri" >&2
if [ "$line" = "" ];then
line="$(printf "" | $ASKUSER "protocol scheme (${scheme}) not configured yet. enter new pipeline to use:")"
if [ "$line" = "" ];then
echo "uristart: user noped on entering new pipeline" >&2
exit 1
fi
printf '%s:\t%s\n' "${scheme}" "${line}" >> ~/.config/uristart.conf
fi
### log the uri if it isn't logged already.
if [ -w ~/.cache/uristart.log ];then #only log if it is writable
if ! cut '-d ' -f1 ~/.cache/uristart.log | grep -Fx "$uri" 2>&1 >/dev/null;then #only log URIs that aren't already listed
if [ "$2" ];then
printf "%s %s\n" "$uri" "$2" >> ~/.cache/uristart.log
else
printf "%s\n" "$uri" >> ~/.cache/uristart.log
fi
fi
else
[ "$verbose" ] && printf "uristart: ~/.cacche/uristart.log is not writable. not logging.\n" >&2
fi
### do the magic
if [ "$check" = "check" ];then
printf "%s\n" "$(printf "%s\n" "$uri" | sed 's/'\''/'\''\\'\'''\''/g' | uriprintf "$line")"
else
eval "$(printf "%s\n" "$uri" | sed 's/'\''/'\''\\'\'''\''/g' | uriprintf "$line")"
fi
|