#!/usr/bin/env bash # Hopefully one day this will be a full featured Pleroma client. # Let's see how far we can get. :) # Display usage information. help() { echo "${0##*/}" echo "Released under the terms of the WTFPL License" echo -e "Usage:\n" echo "With no arguments, open the interactive client." for i in "${!command[@]}" ; do echo "-${i/:/ }: ${command[${i}]}" done | sort echo echo "Configuration files can be found in ${configPath}" echo exit 0 } # Get an oauth token get_oauth_token() { echo "Welcome to ${softwareName}!" echo echo "Let's get you connected to your instance." while [[ -z "${instanceURL}" ]]; do echo read -er -p "Enter the URL of a Pleroma instance: " instanceURL if [[ ! "${instanceURL}" =~ ^https:// ]]; then instanceURL="https://${instanceURL}" fi done redirectURI="urn:ietf:wg:oauth:2.0:oob" website="https://git.stormux.org/storm/ratatoskr" # get client id and secret curl -s -X POST -d client_name="${softwareName}" -d "redirect_uris=${redirectURI}" -d "scopes=read write follow push" -d "website=${website}" "${instanceURL}/api/v1/apps" | jq --raw-output '"client_id=\"\(.client_id)\"\nclient_secret=\"\(.client_secret)\""' > "${configPath}/${configFile}" # Load the new variables from the configuration file source "${configPath}/${configFile}" # Create the url to get the oauth token local url="${instanceURL}/oauth/authorize?client_id=${client_id}&redirect_uri=${redirectURI}&response_type=code&scope=read%20write%20follow%20push" echo "Please open the following url in your browser." echo "Copy the generated token, and paste it here, then press enter to continue." echo if command -v xclip &> /dev/null ; then echo "${url}" | tee >(xclip -selection clipboard -d "${DISPLAY:-:0}" 2> /dev/null && echo "For convenience the url has been copied to your clipboard.") else echo "${url}" fi echo read -er oauth_token # This is actually a authorization token, so get the actual token. oauth_token="$(curl -sS -d "client_id=${client_id}" -d "client_secret=${client_secret}" -d "code=${oauth_token}" -d "grant_type=authorization_code" "${instanceURL}/oauth/token")" echo "oauth_token=\"${oauth_token}\"" >> "${configPath}/${configFile}" echo "instanceURL=\"${instanceURL}\"" >> "${configPath}/${configFile}" } # Functions that deal with posting. # Post music with -M flag post_music() { curl -vS --oauth2-bearer "${oauth_token}" \ -d "$(playerctl metadata -f 'album={{album}}')" \ -d "$(playerctl metadata -f 'artist={{artist}}')" \ -d "$(playerctl metadata -f 'title={{title}}')" \ "${instanceURL}/api/v1/pleroma/scrobble" exit 0 } # Variable initialization configPath="${XDG_CONFIG_HOME:-$HOME/.config}/ratatoskr" # Path for settings, usually ~/.config/ratatoskr configFile="default.conf" # The default config file, eventually will support multiple accounts. softwareName="Ratatoskr" # The name of the client. # Main code starts here # make sure the configuration path exists: mkdir -p "${configPath}" # Associative array of command line parameters and short description of what they do. declare -A command=( [C]="Recreate default configuration file. Acquire new oauth token." [h]="Help, show usage information for ${0##*/}." [M]="Post the currently playing music track, requires playerctl." ) # if the default file doesn't exist, create it if [[ ! -e "${configPath}/${configFile}" ]]; then get_oauth_token else # Read configuration file source "${configPath}/${configFile}" fi # Handle command line parameters # Convert the keys of the associative array to a format usable by getopts args="${!command[*]}" args="${args//[[:space:]]/}" while getopts "${args}" i ; do case "$i" in C) get_oauth_token;; h) help;; M) post_music;; esac done exit 0