2
0
Fork 0
rootfs/usr/local/bin/sh.zio.notify

135 lines
3.2 KiB
Plaintext
Raw Normal View History

2024-03-30 17:57:47 +01:00
#!/usr/bin/env bash
_PLUG_TITLE="ntfy Helper"
_PLUG_DESCRIPTION=""
_PLUG_ARGS=(
2024-04-02 00:15:12 +02:00
"message;m;;string"
2024-03-30 17:57:47 +01:00
"title;t;;string;(No Title)"
2024-04-01 23:22:27 +02:00
"file;f;;path"
2024-03-30 17:57:47 +01:00
"level;l;;string;info"
2024-04-01 22:26:52 +02:00
"invoked-by;i;;string"
2024-03-30 17:57:47 +01:00
)
2024-04-02 01:48:49 +02:00
function get_host_emoji() {
hostname="$1"
hostname_length="${#hostname}"
if [[ "$hostname" =~ ([a-z]{2}[0-9]{2,3}) ]]; then
case "${hostname:0:2}" in
2024-04-02 01:48:49 +02:00
"de") echo "🇩🇪" ;;
"fi") echo "🇫🇮" ;;
"fr") echo "🇫🇷" ;;
"gb") echo "🇬🇧" ;;
"nl") echo "🇳🇱" ;;
*) echo "❔" ;;
esac
elif [[ "$hostname" =~ ([a-z]{3}[0-9]{3}) ]]; then
echo "☁️"
else
echo "🖥️"
fi
}
2024-03-30 17:57:47 +01:00
function get_level_emoji() {
case "$1" in
"critical"|"error") echo "⛔" ;;
"warning") echo "⚠️" ;;
"success") echo "✅" ;;
"info") echo "" ;;
"debug"|*) echo "🔧" ;;
esac
}
function get_level_priority() {
case "$1" in
"critical") echo "5" ;;
"error") echo "4" ;;
"warning") echo "4" ;;
"success") echo "3" ;;
"info") echo "2" ;;
"debug"|*) echo "1" ;;
esac
}
2024-04-01 22:26:52 +02:00
function get_topic() {
invoked_by="$1"
topic_prefix="zio"
topic="test"
if [[ -n "$invoked_by" ]]; then
topic="$(echo "$invoked_by" | sed -r 's/sh.zio.//g' | sed -r 's/[.]+/-/g')"
fi
echo "${topic_prefix}_$topic"
}
2024-03-30 17:57:47 +01:00
function main() {
token_path="$(get_config_dir "sh.zio.notify")/token"
[[ ! -f "$token_path" ]] && die "'$token_path' does not exist"
token="$(cat "$token_path")"
hostname="$(hostname -s)"
2024-04-02 01:48:49 +02:00
host_emoji="$(get_host_emoji $hostname)"
2024-04-01 22:26:52 +02:00
invoked_by="$_invoked_by"
2024-03-30 17:57:47 +01:00
level_emoji="$(get_level_emoji "$_level")"
2024-04-02 01:48:49 +02:00
priority="$(get_level_priority "$_level")"
2024-04-02 00:15:12 +02:00
topic="$(get_topic "$_invoked_by")"
2024-03-30 17:57:47 +01:00
2024-04-02 00:15:12 +02:00
curl_command="curl"
2024-03-30 17:57:47 +01:00
2024-04-01 23:22:27 +02:00
curl_command+=" -H \"Authorization: Bearer $token\""
curl_command+=" -H \"Markdown: yes\""
2024-04-02 01:48:49 +02:00
curl_command+=" -H \"Priority: $priority\""
2024-04-02 00:15:12 +02:00
curl_command+=" -H \"Tags: $hostname,$invoked_by\""
2024-04-02 01:48:49 +02:00
curl_command+=" -H \"Title: $host_emoji $hostname | $level_emoji $_title\""
2024-04-01 23:22:27 +02:00
2024-04-02 00:15:12 +02:00
if [[ "$_message" != "true" ]]; then
curl_command+=" -H \"Message: $_message\""
fi
2024-04-01 23:22:27 +02:00
if [[ "$_file" != "" ]]; then
if [[ ! -f "$_file" ]]; then
die "Cannot attach '$_file': file does not exist"
fi
2024-04-02 00:15:12 +02:00
if [[ "$n_message" != "" ]]; then
say warning "Cannot send --message/-m with --file/-f. Only sending file"
fi
2024-04-01 23:22:27 +02:00
full_file="$(realpath -s "$_file")"
curl_command+=" -T \"$full_file\""
curl_command+=" -H \"Filename: $(basename "$full_file")\""
2024-04-02 00:15:12 +02:00
else
2024-04-02 00:51:33 +02:00
curl_command+=" -X POST"
2024-04-02 00:15:12 +02:00
# HACK: Attachments don't work if you use "Cache: no"
if [[ "$topic" == "zio_test" ]]; then
curl_command+=" -H \"Cache: no\""
fi
2024-04-01 23:22:27 +02:00
fi
2024-04-02 00:15:12 +02:00
curl_command+=" https://ntfy.zio.sh/$topic"
2024-04-02 00:51:33 +02:00
eval $curl_command
2024-03-30 17:57:47 +01:00
}
if [[ $_PLUG_INVOKED != "true" ]]; then
base_dir="$(dirname "$(realpath -s "$0")")"
git_dir="$base_dir/../../.."
if [[ -d "$git_dir/.git" ]]; then
. "$git_dir/usr/local/libexec/zio/helpers/bash.sh"
else
. "/usr/local/libexec/zio/helpers/bash.sh"
fi
export -f get_config_dir
if [[ -d "$git_dir/.git" ]]; then
"$git_dir/usr/local/libexec/sodalite/invoker/src/invoke.sh" "$0" $@
else
"/usr/local/libexec/sodalite/invoker/src/invoke.sh" "$0" $@
fi
fi