diff --git a/notify_templates/notify_HA.sh b/notify_templates/notify_HA.sh index 9ea1a0c..e1f52bd 100755 --- a/notify_templates/notify_HA.sh +++ b/notify_templates/notify_HA.sh @@ -24,7 +24,7 @@ trigger_HA_notification() { printf "The ${HA_channel} notification channel is enabled, but required configuration variables are missing. Home assistant notifications will not be sent.\n" remove_channel HA - return 1 + return 0 fi AccessToken="${!HATokenVar}" diff --git a/notify_templates/notify_apprise.sh b/notify_templates/notify_apprise.sh index eef74bd..d2ab0a5 100644 --- a/notify_templates/notify_apprise.sh +++ b/notify_templates/notify_apprise.sh @@ -23,7 +23,7 @@ trigger_apprise_notification() { printf "The ${apprise_channel} notification channel is enabled, but required configuration variables are missing. Apprise notifications will not be sent.\n" remove_channel apprise - return 1 + return 0 fi if [[ -n "${!ApprisePayloadVar:-}" ]]; then diff --git a/notify_templates/notify_discord.sh b/notify_templates/notify_discord.sh index 9336435..4ac050a 100644 --- a/notify_templates/notify_discord.sh +++ b/notify_templates/notify_discord.sh @@ -21,7 +21,7 @@ trigger_discord_notification() { printf "The ${discord_channel} notification channel is enabled, but required configuration variables are missing. Discord notifications will not be sent.\n" remove_channel discord - return 1 + return 0 fi DiscordWebhookUrl="${!DiscordWebhookUrlVar}" # e.g. DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/ diff --git a/notify_templates/notify_gotify.sh b/notify_templates/notify_gotify.sh index 751961f..66e04ef 100644 --- a/notify_templates/notify_gotify.sh +++ b/notify_templates/notify_gotify.sh @@ -22,7 +22,7 @@ trigger_gotify_notification() { printf "The ${gotify_channel} notification channel is enabled, but required configuration variables are missing. Gotify notifications will not be sent.\n" remove_channel gotify - return 1 + return 0 fi GotifyToken="${!GotifyTokenVar}" # e.g. GOTIFY_TOKEN=token-value diff --git a/notify_templates/notify_matrix.sh b/notify_templates/notify_matrix.sh index 7681aa7..efdf37d 100644 --- a/notify_templates/notify_matrix.sh +++ b/notify_templates/notify_matrix.sh @@ -23,7 +23,7 @@ trigger_matrix_notification() { printf "The ${matrix_channel} notification channel is enabled, but required configuration variables are missing. Matrix notifications will not be sent.\n" remove_channel matrix - return 1 + return 0 fi AccessToken="${!AccessTokenVar}" # e.g. MATRIX_ACCESS_TOKEN=token-value diff --git a/notify_templates/notify_ntfy.sh b/notify_templates/notify_ntfy.sh index 8d6108b..dff7234 100644 --- a/notify_templates/notify_ntfy.sh +++ b/notify_templates/notify_ntfy.sh @@ -23,7 +23,7 @@ trigger_ntfy_notification() { printf "The ${ntfy_channel} notification channel is enabled, but required configuration variables are missing. Ntfy notifications will not be sent.\n" remove_channel ntfy - return 1 + return 0 fi NtfyUrl="${!NtfyDomainVar}/${!NtfyTopicNameVar}" diff --git a/notify_templates/notify_pushbullet.sh b/notify_templates/notify_pushbullet.sh index 321d79c..b061e1c 100644 --- a/notify_templates/notify_pushbullet.sh +++ b/notify_templates/notify_pushbullet.sh @@ -23,7 +23,7 @@ trigger_pushbullet_notification() { printf "The ${pushbullet_channel} notification channel is enabled, but required configuration variables are missing. Pushbullet notifications will not be sent.\n" remove_channel pushbullet - return 1 + return 0 fi PushUrl="${!PushUrlVar}" # e.g. PUSHBULLET_URL=https://api.pushbullet.com/v2/pushes diff --git a/notify_templates/notify_pushover.sh b/notify_templates/notify_pushover.sh index 846ac5d..92eea46 100644 --- a/notify_templates/notify_pushover.sh +++ b/notify_templates/notify_pushover.sh @@ -24,7 +24,7 @@ trigger_pushover_notification() { printf "The ${pushover_channel} notification channel is enabled, but required configuration variables are missing. Pushover notifications will not be sent.\n" remove_channel pushover - return 1 + return 0 fi PushoverUrl="${!PushoverUrlVar}" # e.g. PUSHOVER_URL=https://api.pushover.net/1/messages.json diff --git a/notify_templates/notify_slack.sh b/notify_templates/notify_slack.sh index 6807dcf..e2616e2 100644 --- a/notify_templates/notify_slack.sh +++ b/notify_templates/notify_slack.sh @@ -22,7 +22,7 @@ trigger_slack_notification() { printf "The ${slack_channel} notification channel is enabled, but required configuration variables are missing. Slack notifications will not be sent.\n" remove_channel slack - return 1 + return 0 fi AccessToken="${!AccessTokenVar}" # e.g. SLACK_ACCESS_TOKEN=some-token diff --git a/notify_templates/notify_smtp.sh b/notify_templates/notify_smtp.sh index 931c336..89bd9bc 100644 --- a/notify_templates/notify_smtp.sh +++ b/notify_templates/notify_smtp.sh @@ -38,7 +38,7 @@ trigger_smtp_notification() { printf "The ${smtp_channel} notification channel is enabled, but required configuration variables are missing. SMTP notifications will not be sent.\n" remove_channel smtp - return 1 + return 0 fi SendMailFrom="${!SendMailFromVar}" # e.g. MAIL_FROM=me@mydomain.tld diff --git a/notify_templates/notify_telegram.sh b/notify_templates/notify_telegram.sh index 2d549a2..785a037 100644 --- a/notify_templates/notify_telegram.sh +++ b/notify_templates/notify_telegram.sh @@ -23,7 +23,7 @@ trigger_telegram_notification() { printf "The ${telegram_channel} notification channel is enabled, but required configuration variables are missing. Telegram notifications will not be sent.\n" remove_channel telegram - return 1 + return 0 fi if [[ "$PrintMarkdownURL" == true ]]; then diff --git a/notify_templates/notify_v2.sh b/notify_templates/notify_v2.sh index 1a38f09..5ff088f 100644 --- a/notify_templates/notify_v2.sh +++ b/notify_templates/notify_v2.sh @@ -229,9 +229,39 @@ format_output() { fi } +skip_notification() { + # Skip notification logic. Default to false. Handle all cases, and only those cases, where notifications should be skipped. + local SkipNotification="false" + local Channel="$1" + local UnsnoozedCount="$2" + local NotificationType="$3" + + if [[ $(containers_only "${Channel}") == "true" ]] && [[ "${NotificationType}" != "container" ]]; then + # Do not send notifications through channels only configured for container update notifications + SkipNotification="true" + else + # Handle empty update cases separately + if [[ -z "${UpdToString}" ]]; then + if [[ $(allow_empty "${Channel}") == "false" ]]; then + # Do not send notifications if there are none and allow_empty is false + SkipNotification="true" + fi + else + if [[ $(skip_snooze "${Channel}") == "false" ]] && [[ ${UnsnoozedCount} -eq 0 ]]; then + # Do not send notifications if there are any, they are all snoozed, and skip_snooze is false + SkipNotification="true" + fi + fi + fi + + printf "${SkipNotification}" +} + send_notification() { [[ -s "$ScriptWorkDir"/urls.list ]] && releasenotes || Updates=("$@") + [[ -n "${snooze}" ]] && cleanup_snooze "${Updates[@]}" + UnsnoozedContainers=$(unsnoozed_count "${Updates[@]}") NotifyError=false Notified="false" @@ -246,15 +276,16 @@ send_notification() { UpdToString=${UpdToString%\\n} for channel in "${enabled_notify_channels[@]}"; do - local template=$(get_channel_template "${channel}") + local SkipNotification=$(skip_notification "${channel}" "${UnsnoozedContainers}" "container") + if [[ "${SkipNotification}" == "false" ]]; then + local template=$(get_channel_template "${channel}") - # Formats UpdToString variable per channel settings - format_output "container_update" "$(output_format "${channel}")" "🐋 Containers on $FromHost with updates available:\n\n" + # Formats UpdToString variable per channel settings + format_output "container_update" "$(output_format "${channel}")" "🐋 Containers on $FromHost with updates available:\n\n" - # Setting the MessageBody variable here. - printf -v MessageBody "${FormattedOutput}" + # Setting the MessageBody variable here. + printf -v MessageBody "${FormattedOutput}" - if { { [[ "${MessageBody}" == "None" ]] || [[ "${MessageBody}" == '{"updates": []}' ]]; } && [[ $(allow_empty "${channel}") == "true" ]]; } || { [[ $(skip_snooze "${channel}") == "true" ]] || [[ ${UnsnoozedContainers} -gt 0 ]]; }; then printf "\nSending ${channel} notification" exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \ printf "\nAttempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory." @@ -263,10 +294,9 @@ send_notification() { done if [[ "${Notified}" == "true" ]]; then - [[ -n "${snooze}" ]] && [[ "${NotifyError}" == "false" ]] && [[ "${FormattedOutput}" != "None" ]] && [[ "${Notified}" == "true" ]] && update_snooze "${Updates[@]}" + [[ -n "${snooze}" ]] && [[ -n "${UpdToString}" ]] && [[ "${NotifyError}" == "false" ]] && update_snooze "${Updates[@]}" printf "\n" fi - [[ -n "${snooze}" ]] && [[ "${FormattedOutput}" != "None" ]] && cleanup_snooze "${Updates[@]}" return 0 } @@ -275,6 +305,7 @@ send_notification() { ### to not send notifications when dockcheck itself has updates. dockcheck_notification() { if [[ ! "${DISABLE_DOCKCHECK_NOTIFICATION:-}" == "true" ]]; then + UnsnoozedDockcheck=$(unsnoozed_count "dockcheck\.sh") NotifyError=false Notified=false @@ -282,15 +313,16 @@ dockcheck_notification() { UpdToString="dockcheck.sh,$1,$2,\"$3\"" for channel in "${enabled_notify_channels[@]}"; do - local template=$(get_channel_template "${channel}") + local SkipNotification=$(skip_notification "${channel}" "${UnsnoozedDockcheck}" "dockcheck") + if [[ "${SkipNotification}" == "false" ]]; then + local template=$(get_channel_template "${channel}") - # Formats UpdToString variable per channel settings - format_output "dockcheck_update" "$(output_format "${channel}")" "Installed version: \nLatest version: \n\nChangenotes: \n" "$1" "$2" "$3" + # Formats UpdToString variable per channel settings + format_output "dockcheck_update" "$(output_format "${channel}")" "Installed version: \nLatest version: \n\nChangenotes: \n" "$1" "$2" "$3" - # Setting the MessageBody variable here. - printf -v MessageBody "${FormattedOutput}" + # Setting the MessageBody variable here. + printf -v MessageBody "${FormattedOutput}" - if { { [[ "${MessageBody}" == "None" ]] || [[ "${MessageBody}" == '{"updates": []}' ]]; } && [[ $(allow_empty "${channel}") == "true" ]]; } && { [[ $(skip_snooze "${channel}") == "true" ]] || [[ $(is_snoozed "dockcheck\.sh") == "false" ]]; } && [[ $(containers_only "${channel}") == "false" ]]; then printf "\nSending dockcheck update notification - ${channel}" exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \ printf "\nAttempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory." @@ -333,6 +365,10 @@ notify_update_notification() { fi done + UpdatesPlusDockcheck=("${NotifyUpdates[@]}") + UpdatesPlusDockcheck+=("dockcheck.sh") + [[ -n "${snooze}" ]] && cleanup_snooze "${UpdatesPlusDockcheck[@]}" + UnsnoozedTemplates=$(unsnoozed_count "${NotifyUpdates[@]}") MessageTitle="$FromHost - New version of notify templates available." @@ -342,15 +378,17 @@ notify_update_notification() { UpdToString=${UpdToString%\\n} for channel in "${enabled_notify_channels[@]}"; do - local template=$(get_channel_template "${channel}") + local SkipNotification=$(skip_notification "${channel}" "${UnsnoozedTemplates}" "notify") - # Formats UpdToString variable per channel settings - format_output "notify_update" "$(output_format "${channel}")" "Notify templates on $FromHost with updates available:\n\n" + if [[ "${SkipNotification}" == "false" ]]; then + local template=$(get_channel_template "${channel}") - # Setting the MessageBody variable here. - printf -v MessageBody "${FormattedOutput}" + # Formats UpdToString variable per channel settings + format_output "notify_update" "$(output_format "${channel}")" "Notify templates on $FromHost with updates available:\n\n" + + # Setting the MessageBody variable here. + printf -v MessageBody "${FormattedOutput}" - if { { [[ "${MessageBody}" == "None" ]] || [[ "${MessageBody}" == '{"updates": []}' ]]; } && [[ $(allow_empty "${channel}") == "true" ]]; } && { [[ $(skip_snooze "${channel}") == "true" ]] || [[ ${UnsnoozedTemplates} -gt 0 ]]; } && [[ $(containers_only "${channel}") == "false" ]]; then printf "\nSending notify template update notification - ${channel}" exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \ printf "\nAttempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory." @@ -359,13 +397,9 @@ notify_update_notification() { done if [[ "${Notified}" == "true" ]]; then - [[ -n "${snooze}" ]] && [[ "${NotifyError}" == "false" ]] && [[ "${FormattedOutput}" != "None" ]] && [[ "${Notified}" == "true" ]] && update_snooze "${NotifyUpdates[@]}" + [[ -n "${snooze}" ]] && [[ -n "${UpdToString}" ]] && [[ "${NotifyError}" == "false" ]] && update_snooze "${NotifyUpdates[@]}" printf "\n" fi - - UpdatesPlusDockcheck=("${NotifyUpdates[@]}") - UpdatesPlusDockcheck+=("dockcheck.sh") - [[ -n "${snooze}" ]] && [[ "${FormattedOutput}" != "None" ]] && cleanup_snooze "${UpdatesPlusDockcheck[@]}" fi return 0