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.

93 lines
2.3 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 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] [-t TITLE ] MESSAGE
  26. ----------------------------------------------
  27. --- Send MESSAGE with TITLE 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. If no TITLE is provided, the message will be sent with the hostname as title."
  32. while [[ $# -gt 0 ]]; do
  33. arg="$1"
  34. shift
  35. case "$arg" in
  36. -h|--help)
  37. echo "$usage"
  38. exit 0
  39. ;;
  40. -c|--channel)
  41. channel="$1"
  42. [ -z "$channel" ] || {
  43. echo "Error: no argument for channel option." >&2
  44. echo "$usage" >&2
  45. exit 1
  46. }
  47. shift
  48. ;;
  49. -t|--title)
  50. title="$1"
  51. [ -z "$title" ] || {
  52. echo "Error: no argument for title option." >&2
  53. echo "$usage" >&2
  54. exit 1
  55. }
  56. shift
  57. ;;
  58. *)
  59. [ -z "$message" ] && { message="$arg"; continue; }
  60. echo "Error : Unexpected positional argument '$arg'." >&2
  61. echo "$usage" >&2
  62. exit 1
  63. ;;
  64. esac
  65. done
  66. [ -n "$message" ] || {
  67. echo "Error: missing message." >&2
  68. echo "$usage" >&2
  69. exit 1
  70. }
  71. curl_opts=(
  72. -s
  73. -u "$LOGIN:$PASSWORD"
  74. -d "$message"
  75. )
  76. if [ -n "$title" ]; then
  77. curl_opts+=(-H "Title: [$(hostname)] $title")
  78. fi
  79. curl "${curl_opts[@]}" "$SERVER/${LOGIN}_$channel" > /dev/null