-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravatarhash.sh
161 lines (138 loc) · 3.4 KB
/
gravatarhash.sh
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh
#
# Copyright (c) 2014-2016,2018 Tristan Le Guern <tleguern@bouledef.eu>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
set -e
readonly PROGNAME="$(basename $0)"
readonly VERSION='v1.1'
usage() {
echo "usage: $PROGNAME [-fs] [-d default] [-h hash] [-p provider] email"
}
_sha256() {
if which sha256 > /dev/null; then
sha256 -qs $1
else
echo -n $1 | sha256sum | cut -d' ' -f1
fi
}
_md5() {
if which md5 > /dev/null; then
md5 -qs $1
else
echo -n $1 | md5sum | cut -d' ' -f1
fi
}
dflag=
fflag=false
hflag='md5'
pflag=''
sflag=0
while getopts ":d:fh:p:s" opt;do
case $opt in
d) dflag=$OPTARG;;
f) fflag=true;;
h) hflag=$OPTARG;;
p) pflag=$OPTARG;;
s) sflag=1;;
:) echo "$PROGNAME: option requires an argument -- $OPTARG";
usage; exit 1;; # NOTREACHED
\?) echo "$PROGNAME: unkown option -- $OPTARG";
usage; exit 1;; # NOTREACHED
esac
done
shift $(( $OPTIND -1 ))
if [ -z "$1" ]; then
echo "$PROGNAME: email address expected"
usage
exit 1
else
email="$1"
shift
fi
if [ $# -ge 1 ]; then
echo "$PROGNAME: invalid trailing chars -- $@"
usage
exit 1
fi
[ -n "$pflag" ] && case "$pflag" in
dns|gravatar|libravatar) :;;
*) usage; exit 1;; # NOTREACHED
esac
case "$hflag" in
md5) hflag=_md5;;
sha256) if [ "$pflag" = "gravatar" ]; then
echo "$PROGNAME: sha256 mode can't be used" \
"with gravatar" >&2
exit 1
fi
hflag=_sha256;;
*) usage; exit 1;; # NOTREACHED
esac
set -u
getgravatarbaseuri() {
if [ $sflag -eq 1 ]; then
echo "https://secure.gravatar.com/avatar"
else
echo "http://www.gravatar.com/avatar"
fi
}
getlibravatarbaseuri() {
if [ $sflag -eq 1 ]; then
echo "https://seccdn.libravatar.org/avatar"
else
echo "http://cdn.libravatar.org/avatar"
fi
}
getdnsbaseuri() {
dns_domain="$( echo $1 | cut -d '@' -f 2 )"
if [ $sflag -eq 1 ]; then
dns_service="_avatars-sec._tcp"
scheme="https"
else
dns_service="_avatars._tcp"
scheme="http"
fi
if ! dns_ret="$(dig +short $dns_service.$dns_domain -t SRV)"; then
return 1
fi
if [ -z "$dns_ret" ]; then
return 1
fi
# TODO: Check priority and weight
# Clean up the trailing dot
dns_ret="$(echo $dns_ret|rev|cut -d'.' -f2-|rev|cut -d ' ' -f 4)"
echo "$scheme://$dns_ret/avatar"
}
# If asked, use the specific provider. If not do a DNS request and
# fallback to gravatar.
case "$pflag" in
dns) baseuri="$(getdnsbaseuri $email)";;
libravatar) baseuri="$(getlibravatarbaseuri)";;
gravatar) baseuri="$(getgravatarbaseuri)";;
*) baseuri="$(getdnsbaseuri $email || getgravatarbaseuri)";;
esac
email="$(echo $email|tr '[A-Z' '[a-z]')"
hash="$($hflag $email)"
tail=
if $fflag || [ -n "$dflag" ]; then
tail='?'
if $fflag; then
tail="${tail}f=y"
fi
if [ -n "$dflag" ]; then
tail="${tail}d=$dflag"
fi
fi
echo "$baseuri/$hash$tail"