You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.8 KiB

  1. #!/bin/bash
  2. ## Send a notification with NTFY and check if the config file is complete
  3. if [[ "$UID" == "0" ]]; then
  4. NTFY_CONFIG_DIR="${NTFY_CONFIG_DIR:-/etc/ntfy}"
  5. else
  6. NTFY_CONFIG_DIR="${NTFY_CONFIG_DIR:-~/.config/ntfy}"
  7. fi
  8. NTFY_CONFIG_FILE="$NTFY_CONFIG_DIR/ntfy.conf"
  9. SERVER="https://ntfy.0k.io/"
  10. [ -f "$NTFY_CONFIG_DIR/topics.yml" ] || {
  11. echo "Error: no 'topics.yml' file found in $NTFY_CONFIG_DIR" >&2
  12. echo " Please setup the topics for the notification channels in this file." >&2
  13. exit 1
  14. }
  15. if ! [ -e "$NTFY_CONFIG_FILE" ]; then
  16. mkdir -p "${NTFY_CONFIG_FILE%/*}"
  17. ## default option to change if needed
  18. echo "SERVER=$SERVER" > "$NTFY_CONFIG_FILE"
  19. elif ! grep -q "^SERVER=" "$NTFY_CONFIG_FILE"; then
  20. echo "SERVER=$SERVER" >> "$NTFY_CONFIG_FILE"
  21. fi
  22. source "$NTFY_CONFIG_FILE"
  23. for var in SERVER LOGIN PASSWORD; do
  24. if ! [ -v "$var" ]; then
  25. echo "Error: missing $var in $NTFY_CONFIG_FILE"
  26. exit 1
  27. fi
  28. done
  29. exname=${0##*/}
  30. channels=("main")
  31. usage="Usage: $exname [-c CHANNEL...] [-t TITLE ] MESSAGE
  32. ----------------------------------------------
  33. --- Send MESSAGE with TITLE to the differents topics defined by a CHANNEL. ---
  34. --- If no CHANNEL is provided, the message will be sent to the default channel. ---
  35. ----------------------------------------------
  36. -c CHANNEL: One or multiple channels. If no CHANNEL is provided,
  37. the message will be sent to the main channel.
  38. You can provide multiple channels with -c channel1 -c channel2 ...
  39. topics are configured in $NTFY_CONFIG_DIR/topics.yml
  40. -t TITLE: If no TITLE is provided, the message will be sent with the hostname as title.
  41. - MESSAGE: The message to send.
  42. "
  43. while [[ $# -gt 0 ]]; do
  44. arg="$1"
  45. shift
  46. case "$arg" in
  47. -h|--help)
  48. echo "$usage"
  49. exit 0
  50. ;;
  51. -c|--channel)
  52. [ -n "$1" ] || {
  53. echo "Error: no argument for channel option." >&2
  54. echo "$usage" >&2
  55. exit 1
  56. }
  57. IFS=", " channels+=($1)
  58. shift
  59. ;;
  60. -t|--title)
  61. title="$1"
  62. [ -z "$title" ] || {
  63. echo "Error: no argument for title option." >&2
  64. echo "$usage" >&2
  65. exit 1
  66. }
  67. shift
  68. ;;
  69. *)
  70. [ -z "$message" ] && { message="$arg"; continue; }
  71. echo "Error : Unexpected positional argument '$arg'." >&2
  72. echo "$usage" >&2
  73. exit 1
  74. ;;
  75. esac
  76. done
  77. [ -n "$message" ] || {
  78. echo "Error: missing message." >&2
  79. echo "$usage" >&2
  80. exit 1
  81. }
  82. read-0() {
  83. local eof= IFS=''
  84. while [ "$1" ]; do
  85. read -r -d '' -- "$1" || eof=1
  86. shift
  87. done
  88. [ -z "$eof" ]
  89. }
  90. curl_opts=(
  91. -s
  92. -u "$LOGIN:$PASSWORD"
  93. -d "$message"
  94. )
  95. if [ -n "$title" ]; then
  96. curl_opts+=(-H "Title: [$(hostname)] $title")
  97. fi
  98. declare -A sent_topic=()
  99. for channel in "${channels[@]}"; do
  100. channel_quoted=$(printf "%q" "$channel")
  101. while read-0 topic; do
  102. ttopic=$(printf "%s" "$topic" | yq "type")
  103. if [ "$ttopic" != '!!str' ]; then
  104. echo "Error: Unexpected '$ttopic' type for value of channel $channel." >&2
  105. exit 1
  106. fi
  107. topic=$(printf "%s" "$topic" | yq -r " \"\" + .")
  108. if ! [[ "$topic" =~ ^[a-zA-Z0-9\$\{\}*\ \,_.-]+$ ]]; then
  109. echo "Error: Invalid topic value '$topic' expression in $channel channel." >&2
  110. exit 1
  111. fi
  112. new_topics=($(eval echo "${topic//\*/\\*}"))
  113. for new_topic in "${new_topics[@]}"; do
  114. [ -n "${sent_topic["$new_topic"]}" ] && continue
  115. sent_topic["$new_topic"]=1
  116. echo curl "${curl_opts[@]}" "$SERVER/${new_topic}" # > /dev/null
  117. done
  118. done < <(yq -0 -r=false -e ".[\"$channel_quoted\"] | .[]" "$NTFY_CONFIG_DIR/topics.yml")
  119. done