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.

92 lines
2.2 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  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_FILE="/etc/ntfy/ntfy.conf"
  5. else
  6. NTFY_CONFIG_FILE=~/.config/ntfy/ntfy.conf
  7. fi
  8. SERVER="https://ntfy.0k.io/"
  9. if ! [ -e "$NTFY_CONFIG_FILE" ]; then
  10. mkdir -p "${NTFY_CONFIG_FILE%/*}"
  11. ## default option to change if needed
  12. echo "SERVER=$SERVER" > "$NTFY_CONFIG_FILE"
  13. elif ! grep -q "^SERVER=" "$NTFY_CONFIG_FILE"; then
  14. echo "SERVER=$SERVER" >> "$NTFY_CONFIG_FILE"
  15. fi
  16. source "$NTFY_CONFIG_FILE"
  17. for var in SERVER LOGIN PASSWORD; do
  18. if ! [ -v "$var" ]; then
  19. echo "Error: missing $var in $NTFY_CONFIG_FILE"
  20. exit 1
  21. fi
  22. done
  23. exname=${0##*/}
  24. channel="main"
  25. usage="Usage: $exname [-c CHANNEL] MESSAGE
  26. ----------------------------------------------
  27. --- Send MESSAGE to the specified CHANNEL. ---
  28. ----------------------------------------------
  29. If no CHANNEL is provided, the message will be sent to the default channel
  30. Default CHANNEL is format as follow : ConfiguredLOGIN_${default_channel}"
  31. while [[ $# -gt 0 ]]; do
  32. arg="$1"
  33. shift
  34. case "$arg" in
  35. -h|--help)
  36. echo "$usage"
  37. exit 0
  38. ;;
  39. -c|--channel)
  40. channel="$1"
  41. [ -z "$channel" ] || {
  42. echo "Error: no argument for channel option." >&2
  43. echo "$usage" >&2
  44. exit 1
  45. }
  46. shift
  47. ;;
  48. -t|--title)
  49. title="$1"
  50. [ -z "$title" ] || {
  51. echo "Error: no argument for title option." >&2
  52. echo "$usage" >&2
  53. exit 1
  54. }
  55. shift
  56. ;;
  57. *)
  58. [ -z "$message" ] && { message="$arg"; continue; }
  59. echo "Error : Unexpected positional argument '$arg'." >&2
  60. echo "$usage" >&2
  61. exit 1
  62. ;;
  63. esac
  64. done
  65. [ -n "$message" ] || {
  66. echo "Error: missing message." >&2
  67. echo "$usage" >&2
  68. exit 1
  69. }
  70. curl_opts=(
  71. -s
  72. -u "$LOGIN:$PASSWORD"
  73. -d "$message"
  74. )
  75. if [ -n "$title" ]; then
  76. curl_opts+=(-H "Title: [$(hostname)] $title")
  77. fi
  78. curl "${curl_opts[@]}" "$SERVER/${LOGIN}_$channel" > /dev/null