119 lines
No EOL
2.6 KiB
Bash
Executable file
119 lines
No EOL
2.6 KiB
Bash
Executable file
#!/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;
|
|
|
|
# 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}"
|
|
|
|
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 |