diff --git a/matrixpack.sh b/matrixpack.sh index b81c1c903fc4b98cf5a4b2b1a7b80362f576d6de..72c221645dd3371a266d22519c4f60fe401951c1 100755 --- a/matrixpack.sh +++ b/matrixpack.sh @@ -32,6 +32,63 @@ if test $# -lt 3 ; then exit 1 fi +# Display usage funtion +display_usage (){ + echo -e "\nUsage : ./matrixpack.sh token homeserver directory\n" +} + +# Erase http:// or https:// if added in the homeserver +slug_homeserver() { + echo "$1" | iconv -c -t ascii//TRANSLIT | sed -E 's/^http:\/\/|^https:\/\///g' | sed -E 's/^-+|-+$//g' +} + +# Checking params +if test $# -lt 3 ; then + echo 1>&2 "$0:FATAL: invalid argument number (expected 3)" + display_usage + exit 1 +fi + +# Checking if ImageMagick is installed +if ! command -v convert &> /dev/null && ! command -v montage &> /dev/null; then + echo "$0:ERROR: ImageMagick could not be found" + exit +fi + +# Setting variables +dir=$(pwd) +token=$1 +packname=${dir##*/} +slug=$(slugify "$packname") +homeserver=$(slug_homeserver $2) + +# Checking if the homeserver exist +curl "https://$homeserver" -I -o headers -s +if test -f "headers"; then + rm headers +else + echo 1>&2 "$0:FATAL: The homeserver \"$2\" is not correct." + exit 1 +fi + +# Checking if the token exist +curl -s -X POST "https://$homeserver/_matrix/media/r0/upload?access_token=$token" | python3 -c "import sys, json; print(json.load(sys.stdin)['errcode'])" >> headers +while read line; do + if [[ ($line == "M_UNKNOWN") ]] + then + rm headers + else if [[ ($line == "M_UNKNOWN_TOKEN") ]] + then + echo "$0:ERROR: The token provided does not exist" + rm headers + exit 1 + else + echo "$0:ERROR: Not yet implemented error" + exit 1 + fi + fi +done < headers + for i in "$@" do if [ -d "$i" ]; then @@ -39,15 +96,8 @@ do # Changing directory to work directly in it cd "$i" - # Setting variables - dir=$(pwd) - token=$1 - packname=${dir##*/} - slug=$(slugify "$packname") - homeserver=$2 - # Printing informations - echo -e "Creating sticker pack \033[94;1m$packname\033[0m\nhomeserver=\033[94m$homeserver\033[0m\noutput=\033[94m$slug\033[0m" + echo -e "Creating sticker pack : \033[94;1m$packname\033[0m\nhomeserver=\033[94m$homeserver\033[0m\noutput=\033[94m$slug\033[0m" # Making result folder if ! [ -d $slug ]; then mkdir $slug; fi