#!/usr/bin/env sh # # Send Pushover messages from cli # Based on https://github.com/mrusme/dotfiles/blob/master/usr/local/bin/pushover # # Modified by Ismo Vuorinen 2023 __pushover_usage() { printf "pushover \n" printf " -c \n" printf " -d \n" printf " -D \n" printf " -e \n" printf " -p \n" printf " -r \n" printf " -t \n" printf " -T <TOKEN> (required if not in 'PUSHOVER_TOKEN' env)\n" printf " -s <sound>\n" printf " -u <url>\n" printf " -U <USER> (required if not in 'PUSHOVER_USER' env)\n" printf " -a <url_title>\n" return 1 } __pushover_opt_field() { field=$1 shift value="${*}" if [ -n "${value}" ]; then printf "%s \"%s=%s\"\n" "-F" "$field" "$value" fi } __pushover_send_message() { device="${1:-}" curl_cmd="\"${CURL}\" -s -S \ ${CURL_OPTS} \ -F \"token=${TOKEN}\" \ -F \"user=${USER}\" \ -F \"message=${message}\" \ $(__pushover_opt_field device "${device}") \ $(__pushover_opt_field callback "${callback}") \ $(__pushover_opt_field timestamp "${timestamp}") \ $(__pushover_opt_field priority "${priority}") \ $(__pushover_opt_field retry "${retry}") \ $(__pushover_opt_field expire "${expire}") \ $(__pushover_opt_field title "${title}") \ $(__pushover_opt_field sound "${sound}") \ $(__pushover_opt_field url "${url}") \ $(__pushover_opt_field url_title "${url_title}") \ \"${PUSHOVER_URL}\"" response="$(eval "${curl_cmd}")" printf "%s\n" "$response" # Parse response status. Expect JSON like: {"status":1,"request":"..."} if echo "$response" | grep -q '"status"[[:space:]]*:[[:space:]]*1'; then r=0 else r=1 fi if [ "$r" -ne 0 ]; then # Extract possible error message from JSON err=$(echo "$response" | grep -o '"errors".*' | sed 's/"errors"[:,\[]//g' | tr -d '[]"') [ -n "$err" ] && printf "%s: %s\n" "$0" "$err" >&2 printf "%s: Failed to send message\n" "$0" >&2 fi return "$r" } CURL="$(command -v curl)" PUSHOVER_URL="https://api.pushover.net/1/messages.json" TOKEN=$PUSHOVER_TOKEN USER=$PUSHOVER_USER CURL_OPTS="" devices="" optstring="c:d:D:e:p:r:t:T:s:u:U:a:h" OPTIND=1 while getopts ${optstring} c; do case ${c} in c) callback="${OPTARG}" ;; d) devices="${devices} ${OPTARG}" ;; D) timestamp="${OPTARG}" ;; e) expire="${OPTARG}" ;; p) priority="${OPTARG}" ;; r) retry="${OPTARG}" ;; t) title="${OPTARG}" ;; T) TOKEN="${OPTARG}" ;; s) sound="${OPTARG}" ;; u) url="${OPTARG}" ;; U) USER="${OPTARG}" ;; a) url_title="${OPTARG}" ;; [h\?]) __pushover_usage exit 1 ;; esac done shift $((OPTIND - 1)) if [ "$#" -lt 1 ]; then __pushover_usage exit 1 fi message="$*" if [ ! -x "${CURL}" ]; then printf "CURL is unset, empty, or does not point to curl executable.\n \ This script requires curl!\n" >&2 exit 1 fi devices="$(printf "%s" "${devices}" | xargs -n1 | sort -u)" if [ -z "${devices}" ]; then __pushover_send_message r=${?} else for device in ${devices}; do __pushover_send_message "${device}" r=${?} if [ "${r}" -ne 0 ]; then break fi done fi exit "${r}"