add TID and replace old MD5 by SHA256

This commit is contained in:
Jan-Piet Mens
2024-11-25 20:23:54 +01:00
parent fde9692a31
commit 8e87664e5e

View File

@@ -1,7 +1,7 @@
#!/bin/sh
# gravatar2card.sh
# Copyright (C) 2015-2016 Jan-Piet Mens <jpmens@gmail.com>
# Copyright (C) 2015-2024 Jan-Piet Mens <jpmens@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@@ -17,7 +17,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Usage: gravatar2card email "full name"
# Usage: gravatar2card email "full name" tid
#
# Calculate email hash and obtain a 40x40 PNG from Gravatar
# and convert image to BASE64. Create a JSON payload to be published
@@ -25,7 +25,7 @@
#
# You probably want to do this:
#
# gravatar2card.sh email-address "Jane Jolie" > card.json
# gravatar2card.sh email-address "Jane Jolie" JJ > card.json
# mosquitto_pub -t owntracks/jane/phone/info -r -f card.json
#
# Note: the two commands cannot be piplelined (mosquitto_pub -l)
@@ -33,19 +33,20 @@
# If you have a newer version it should work fine.
[ $# -ne 2 ] && { echo "Usage: $0 email full-name" >&2; exit 2; }
[ $# -ne 3 ] && { echo "Usage: $0 email full-name tid" >&2; exit 2; }
md5=md5
hasher="shasum -a 256"
if [ -x /usr/bin/md5sum ]; then
md5=/usr/bin/md5sum
fi
email="$1"
fullname="$2"
tid="$3"
tmp=$(mktemp /tmp/gravatar.XXXXXX)
hash=$(echo -n "${email}" | tr '[:upper:]' '[:lower:]' | $md5)
url="http://www.gravatar.com/avatar/${hash}?s=40"
hash=$(printf "${email}" | tr '[:upper:]' '[:lower:]' | $hasher | cut -d' ' -f1)
url="https://www.gravatar.com/avatar/${hash}?s=40"
curl -s -o $tmp "${url}"
@@ -54,6 +55,6 @@ imgdata=$(base64 -i $tmp)
rm -f $tmp
cat <<EndOfFile
{"_type":"card","name":"${fullname}","face":"${imgdata}"}
{"_type":"card","tid":"${tid}","name":"${fullname}","face":"${imgdata}"}
EndOfFile