system-helpers/usr/local/bin/notifier

130 lines
2.8 KiB
Text
Raw Normal View History

#!/usr/bin/env bash
# usage
usage() {
USAGE=$(cat <<EOF
Usage: notifier <subject> <content> [<mail address>]
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
NOTIFIER_MAIL_ENABLED="true";
NOTIFIER_GOTIFY_ENABLED="false";
NOTIFIER_MAIL_ADDRESS=$3;
argsMailProvided="false"
if [[ -n "${NOTIFIER_MAIL_ADDRESS}" ]]; 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;
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
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"
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