#!/usr/bin/env bash # usage usage() { USAGE=$(cat < [] Sends out notifications. Please see man notifier. EOF ) echo "$USAGE"; } set -e; NOTIFIER_SUBJECT=$1 if [ -z "$NOTIFIER_SUBJECT" ]; then echo "No subject given"; echo ""; usage; exit 1; fi NOTIFIER_CONTENT=$2 if [ -z "$NOTIFIER_CONTENT" ]; then echo "No content given"; echo ""; usage; exit 1; fi argsMailProvided="false" if [[ -n "$3" ]]; then argsMailProvided="true" fi # check for config file apply_config() { local config=$1; if [[ ! -f "$config" ]]; then echo "No config file specified"; echo ""; usage; exit 1; fi set -a; # shellcheck disable=SC1090 source "$config"; set +a; } check_requirements() { local mailEnabled=$1; local gotifyEnabled=$2; local mailAddress=$3; if [[ "${mailEnabled}" == "false" && "${gotifyEnabled}" == "false" ]]; then echo "Mail and gotify cannot be disabled simultanously"; echo ""; usage; exit 1; fi if [[ "${mailEnabled}" == "true" ]]; then type mailx &> /dev/null || { echo "Requiring 'mailx' but it's not installed"; exit 1; } if [ -z "${mailAddress}" ]; then echo "No mail address given"; echo ""; usage; exit 1; fi fi if [[ "${gotifyEnabled}" == "true" ]]; then type gotify &> /dev/null || { echo "Requiring 'gotify' but it's not installed"; exit 1; } fi } source_config() { local configFallback=$1; local configGlobalFallback=$2; checkMailEnabled=${NOTIFIER_MAIL_ENABLED:-1} checkGotifyEnabled=${NOTIFIER_GOTIFY_ENABLED:-1} checkMailAddress=${NOTIFIER_MAIL_ADDRESS:-1} if [[ ! -v "$checkMailEnabled" && ! -v "$checkGotifyEnabled" && ! -v "$checkMailAddress" ]]; then echo "Using environment variables for configuration instead of configuration files"; return; else echo "Using configuration files"; fi if [[ -f "$configFallback" ]]; then apply_config "$configFallback"; return; fi if [[ -f "$configGlobalFallback" ]]; then apply_config "$configGlobalFallback"; return; fi } source_config "$HOME/.notifier.conf" "/etc/notifier.conf" check_requirements "${NOTIFIER_MAIL_ENABLED}" "${NOTIFIER_GOTIFY_ENABLED}" "${NOTIFIER_MAIL_ADDRESS}" if [[ "${argsMailProvided}" == "true" ]]; then NOTIFIER_MAIL_ADDRESS=$3 echo "Overwriting mail with argument value" fi echo "Using NOTIFIER_MAIL_ENABLED=${NOTIFIER_MAIL_ENABLED}" echo "Using NOTIFIER_GOTIFY_ENABLED=${NOTIFIER_GOTIFY_ENABLED}" echo "Using NOTIFIER_MAIL_ADDRESS=${NOTIFIER_MAIL_ADDRESS}" SUBJECT="${NOTIFIER_SUBJECT}" MESSAGE="${NOTIFIER_CONTENT}" if [[ "${NOTIFIER_MAIL_ENABLED}" == "true" ]]; then echo "$MESSAGE"|mailx -Ssendwait -s "$SUBJECT" "$NOTIFIER_MAIL_ADDRESS"; echo "Sent notifiction via mail to $NOTIFIER_MAIL_ADDRESS" else echo "Sending notifictions via mail is disabled" fi if [[ "${NOTIFIER_GOTIFY_ENABLED}" == "true" ]]; then if [[ ! -f "${HOME}/.config/gotify/cli.json" && ! -f "${HOME}/gotify/cli.json" && ! -f "./cli.json" && ! -f "/etc/gotify/cli.json" ]]; then echo "Cannot find a valid cli.json, please run 'gotify init' before using this" exit 1; fi echo "$MESSAGE"|gotify push --quiet --title "${SUBJECT}"; echo "Sent message via gotify" else echo "Sending notifictions via gotify is disabled" fi