diff --git a/.gitattributesdb b/.gitattributesdb index 240e6e8..3a30532 100644 --- a/.gitattributesdb +++ b/.gitattributesdb @@ -256,7 +256,7 @@ ZG9rdXdpa2k= - - ZXRjL2Rva3V3aWtpLy5odGFjY2Vzcw== 1722839243.000000000 1773517662.000000000 root:root 0644 - - ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - ZG9rdXdpa2k= - - -ZXRjL2Rva3V3aWtpL2FjbC5hdXRoLnBocA== 1746154701.000000000 1775311379.000000000 root:www-data 0777 - - +ZXRjL2Rva3V3aWtpL2FjbC5hdXRoLnBocA== 1775230628.961253065 1775230628.961253065 root:root 0777 - - ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - ZG9rdXdpa2k= - - ZXRjL2Rva3V3aWtpL2FjbC5hdXRoLnBocC5kaXN0 1722839243.000000000 1773517662.000000000 root:root 0644 - - @@ -280,7 +280,7 @@ ZG9rdXdpa2k= - - ZXRjL2Rva3V3aWtpL2xpY2Vuc2UucGhw 1722839243.000000000 1773517662.000000000 root:www-data 0664 - - ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - ZG9rdXdpa2k= - - -ZXRjL2Rva3V3aWtpL2xvY2FsLnBocA== 1773597191.209498063 1773597191.209498063 root:www-data 0644 - - +ZXRjL2Rva3V3aWtpL2xvY2FsLnBocA== 1773597191.209498063 1773597191.209498063 root:www-data 0664 - - ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - ZG9rdXdpa2k= - - ZXRjL2Rva3V3aWtpL2xvY2FsLnBocC5iYWsucGhw 1773597164.469917468 1773597164.469917468 root:www-data 0664 - - @@ -319,7 +319,7 @@ ZG9rdXdpa2k= - - ZXRjL2Rva3V3aWtpL3VzZXJwcmludC5jc3M= 1745221948.000000000 1773517662.000000000 root:www-data 0664 - - ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - ZG9rdXdpa2k= - - -ZXRjL2Rva3V3aWtpL3VzZXJzLmF1dGgucGhw 1746154701.000000000 1775311379.000000000 root:www-data 0777 - - +ZXRjL2Rva3V3aWtpL3VzZXJzLmF1dGgucGhw 1775230628.965253000 1775230628.965253000 root:root 0777 - - ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - ZG9rdXdpa2k= - - ZXRjL2Rva3V3aWtpL3VzZXJzLmF1dGgucGhwLmRpc3Q= 1722839243.000000000 1773517662.000000000 root:root 0644 - - @@ -356,10 +356,7 @@ ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - ZXRjL2hvc3Rz 1774987123.981565342 1757594362.000000000 root:root 0644 - - ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - aW5pdC5k - - -ZXRjL2luaXQuZC8uZ2l0aWdub3Jl 1775484728.393595405 1771459200.000000000 root:root 0644 - - -ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - -aW5pdC5k - - -ZXRjL2luaXQuZC9maXJld2FsbA== 1775484698.270090448 1775484698.270090448 root:root 0777 - - +ZXRjL2luaXQuZC8uZ2l0aWdub3Jl 1771459200.000000000 1771459200.000000000 root:root 0644 - - ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - aW5pdC5k - - ZXRjL2luaXQuZC90ZXJyYWZvcm0taHR0cC1iYWNrZW5k 1773654405.833829368 1771459200.000000000 root:root 0755 - - @@ -505,7 +502,7 @@ ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - cGhwbGRhcGFkbWlu - - ZXRjL3BocGxkYXBhZG1pbi9jb25maWcucGhwLmdwZw== 1761052640.000000000 1758539944.000000000 root:root 0644 - - ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - -ZXRjL3BrZ2xpc3Q= 1775433601.983420185 1762560002.068536774 root:root 0644 - - +ZXRjL3BrZ2xpc3Q= 1775260801.281926056 1762560002.068536774 root:root 0644 - - ZXRj 1775237375.888665668 1771501908.000000000 root:root 0755 - - cHVzaG92ZXItY2xpZW50 - - ZXRjL3B1c2hvdmVyLWNsaWVudC8uZ2l0aWdub3Jl 1762628624.365862525 1762448145.464092595 root:root 0644 - - @@ -602,55 +599,55 @@ LnNzaA== - - aG9tZS9zeXNhZG1pbi8uc3NoL2F1dGhvcml6ZWRfa2V5cw== 1757763178.000000000 1757587611.000000000 sysadmin:users 0644 - - b3B0 1771515169.961748163 1771501851.000000000 root:root 0755 - - c2Jpbg== 1773131431.000000000 1773131431.000000000 root:root 0777 - - -b3B0L3NiaW4vY3JvbmpvYi1jbGVhbi1waHA= 1775310349.864494385 1775310349.864494385 root:root 0777 - - +b3B0L3NiaW4vY3JvbmpvYi1jbGVhbi1waHA= 1762538240.962584934 1758289390.000000000 root:root 0755 - - b3B0 1771515169.961748163 1771501851.000000000 root:root 0755 - - c2Jpbg== 1773131431.000000000 1773131431.000000000 root:root 0777 - - -b3B0L3NiaW4vY3JvbmpvYi1kZWh5ZHJhdGVk 1775310365.008247840 1775310365.008247840 root:root 0777 - - +b3B0L3NiaW4vY3JvbmpvYi1kZWh5ZHJhdGVk 1758033093.000000000 1757531685.000000000 root:root 0755 - - b3B0 1771515169.961748163 1771501851.000000000 root:root 0755 - - c2Jpbg== 1773131431.000000000 1773131431.000000000 root:root 0777 - - -b3B0L3NiaW4vY3JvbmpvYi1yb3RhdGUtbG9ncy10b2RheS1zeW1saW5r 1775310379.696008718 1775310379.696008718 root:root 0777 - - +b3B0L3NiaW4vY3JvbmpvYi1yb3RhdGUtbG9ncy10b2RheS1zeW1saW5r 1774882209.447784765 1774882209.447784765 root:root 0755 - - b3B0 1771515169.961748163 1771501851.000000000 root:root 0755 - - c2Jpbg== 1773131431.000000000 1773131431.000000000 root:root 0777 - - -b3B0L3NiaW4vY3JvbmpvYi11cGRhdGUtcGFja2FnZXMtbGlzdA== 1775310406.471572796 1775310406.471572796 root:root 0777 - - +b3B0L3NiaW4vY3JvbmpvYi11cGRhdGUtcGFja2FnZXMtbGlzdA== 1773518953.705866964 1757531121.000000000 root:root 0755 - - b3B0 1771515169.961748163 1771501851.000000000 root:root 0755 - - c2Jpbg== 1773131431.000000000 1773131431.000000000 root:root 0777 - - -b3B0L3NiaW4vY3JvbmpvYi13YXJuLWdpdC1zdGF0dXM= 1775310430.567180514 1775310430.567180514 root:root 0777 - - +b3B0L3NiaW4vY3JvbmpvYi13YXJuLWdpdC1zdGF0dXM= 1758221607.000000000 1757591137.000000000 root:root 0755 - - b3B0 1771515169.961748163 1771501851.000000000 root:root 0755 - - c2Jpbg== 1773131431.000000000 1773131431.000000000 root:root 0777 - - -b3B0L3NiaW4vcHVzaG92ZXItY2xpZW50 1775310306.413201805 1775310306.413201805 root:root 0777 - - +b3B0L3NiaW4vcHVzaG92ZXItY2xpZW50 1758224526.000000000 1758224526.000000000 root:root 0755 - - b3B0 1771515169.961748163 1771501851.000000000 root:root 0755 - - c2Jpbg== 1773131431.000000000 1773131431.000000000 root:root 0777 - - -b3B0L3NiaW4vdGVycmFmb3JtLWh0dHAtYmFja2VuZA== 1775310323.052930897 1775310323.052930897 root:root 0777 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +b3B0L3NiaW4vdGVycmFmb3JtLWh0dHAtYmFja2VuZA== 1757590543.000000000 1757590543.000000000 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - cm9vdC8uYmFzaF9sb2dvdXQ= 1757582867.000000000 1757582867.000000000 root:root 0644 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - cm9vdC8uYmFzaF9wcm9maWxl 1757584711.000000000 1757584711.000000000 root:root 0644 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - cm9vdC8uYmFzaHJj 1774978675.760676759 1757586493.000000000 root:root 0644 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - cm9vdC8uZ2l0Y29uZmln 1757582738.000000000 1757582738.000000000 root:root 0644 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - cm9vdC8uZ2l0aWdub3Jl 1774104492.728356672 1757600312.000000000 root:root 0644 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - LmxvY2Fs - - c2hhcmU= - - bmFubw== - - cm9vdC8ubG9jYWwvc2hhcmUvbmFuby8uZ2l0aWdub3Jl 1757586210.000000000 1757586210.000000000 root:root 0644 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - cm9vdC8ubmFub3Jj 1757585756.000000000 1757585756.000000000 root:root 0644 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - LnNzaA== - - cm9vdC8uc3NoLy5naXRpZ25vcmU= 1757593349.000000000 1757593349.000000000 root:root 0644 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - LnNzaA== - - cm9vdC8uc3NoL2F1dGhvcml6ZWRfa2V5cw== 1757587611.000000000 1757587611.000000000 root:root 0644 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - c3R1ZmYtdG8ta2VlcA== - - cm9vdC9zdHVmZi10by1rZWVwL2NsZWFuLWZk 1758994151.000000000 1758992264.000000000 root:root 0755 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - c3R1ZmYtdG8ta2VlcA== - - cm9vdC9zdHVmZi10by1rZWVwL2R1bW15LWRlZmF1bHQtbXRh 1762020478.278412865 1762020215.034844513 root:root 0644 - - -cm9vdA== 1775445846.385331241 1771512801.616005200 root:root 0755 - - +cm9vdA== 1775301973.257465550 1771512801.616005200 root:root 0755 - - c3R1ZmYtdG8ta2VlcA== - - cm9vdC9zdHVmZi10by1rZWVwL2R1bW15LWRlZmF1bHQtbXRhXzAuMC4xX2FsbC5kZWI= 1762020499.466056182 1762020499.458056317 root:root 0644 - - dXNy 1774997839.417383939 1771501851.000000000 root:root 0755 - - @@ -713,7 +710,7 @@ bG9n - - QXJjaGl2ZWQ= - - dmFyL2xvZy9BcmNoaXZlZC8uZ2l0aWdub3Jl 1774126984.505500258 1774126984.505500258 root:root 0644 - - dmFy 1773949028.799601180 1771512801.620005134 root:root 0755 - - -dG1w 1775481645.792243432 1775217056.000000000 root:root 1777 - - +dG1w 1775260802.217910666 1775217056.000000000 root:root 1777 - - dmFyL3RtcC8uZ2l0aWdub3Jl 1771459200.000000000 1771459200.000000000 root:root 0644 - - dmFy 1773949028.799601180 1771512801.620005134 root:root 0755 - - d3d3 - - diff --git a/etc/init.d/.gitignore b/etc/init.d/.gitignore index f0d9f50..e7df9d0 100644 --- a/etc/init.d/.gitignore +++ b/etc/init.d/.gitignore @@ -1,4 +1,3 @@ /* !/.gitignore -!/firewall !/terraform-http-backend diff --git a/etc/init.d/firewall b/etc/init.d/firewall deleted file mode 120000 index a3e4507..0000000 --- a/etc/init.d/firewall +++ /dev/null @@ -1 +0,0 @@ -/usr/src/random-scripts.git/firewall-initscript \ No newline at end of file diff --git a/opt/sbin/cronjob-clean-php b/opt/sbin/cronjob-clean-php deleted file mode 120000 index 26722b4..0000000 --- a/opt/sbin/cronjob-clean-php +++ /dev/null @@ -1 +0,0 @@ -../../usr/src/random-scripts.git/cronjob-clean-php \ No newline at end of file diff --git a/opt/sbin/cronjob-clean-php b/opt/sbin/cronjob-clean-php new file mode 100755 index 0000000..b47cc39 --- /dev/null +++ b/opt/sbin/cronjob-clean-php @@ -0,0 +1,4 @@ +#!/bin/bash + +[[ -d /var/lib/php/sessions ]] && find /var/lib/php/sessions -mmin +1440 -type f -print0 | xargs -0 rm -f +[[ -d /var/tmp/php-uploads ]] && find /var/tmp/php-uploads -mmin +1440 -type f -print0 | xargs -0 rm -f diff --git a/opt/sbin/cronjob-dehydrated b/opt/sbin/cronjob-dehydrated deleted file mode 120000 index 2cbcfa0..0000000 --- a/opt/sbin/cronjob-dehydrated +++ /dev/null @@ -1 +0,0 @@ -../../usr/src/random-scripts.git/cronjob-dehydrated \ No newline at end of file diff --git a/opt/sbin/cronjob-dehydrated b/opt/sbin/cronjob-dehydrated new file mode 100755 index 0000000..4035bcf --- /dev/null +++ b/opt/sbin/cronjob-dehydrated @@ -0,0 +1,6 @@ +#!/bin/bash +# Run this job in the background. +( # Delay the run for 15 hours (from midnight) and then run at a random time within 3 hours after that. + sleep $(( 54000 + (RANDOM % 10800) )) + + /opt/sbin/dehydrated -c >/dev/null ) & diff --git a/opt/sbin/cronjob-rotate-logs-today-symlink b/opt/sbin/cronjob-rotate-logs-today-symlink deleted file mode 120000 index f834ded..0000000 --- a/opt/sbin/cronjob-rotate-logs-today-symlink +++ /dev/null @@ -1 +0,0 @@ -../../usr/src/random-scripts.git/cronjob-rotate-logs-today-symlink \ No newline at end of file diff --git a/opt/sbin/cronjob-rotate-logs-today-symlink b/opt/sbin/cronjob-rotate-logs-today-symlink new file mode 100755 index 0000000..cc0cfa2 --- /dev/null +++ b/opt/sbin/cronjob-rotate-logs-today-symlink @@ -0,0 +1,24 @@ +#!/bin/bash + +# Default configuration. +LOGS_DIR="/data/logs" + +# Process the directories in the $LOGS_DIR directory. +[[ -d "$LOGS_DIR" ]] && { + TODAY="$(printf "%(%Y/%m/%d)T")" + + for DIR in "$LOGS_DIR"/*/; do + cd "$DIR" 2>/dev/null || { + printf "%s: %s\\n" "${0##*/}" "failed to change directory to '$DIR'" >&2 + continue + } + + # Create the 'today' symlink to the new location.. + ln -sfn "$TODAY" "today" 2>/dev/null || { + printf "%s: %s\\n" "${0##*/}" "updating 'today' symlink failed in '$DIR'" >&2 + continue + } + done +} + +exit 0 diff --git a/opt/sbin/cronjob-update-packages-list b/opt/sbin/cronjob-update-packages-list deleted file mode 120000 index 48fbf2c..0000000 --- a/opt/sbin/cronjob-update-packages-list +++ /dev/null @@ -1 +0,0 @@ -../../usr/src/random-scripts.git/cronjob-update-packages-list \ No newline at end of file diff --git a/opt/sbin/cronjob-update-packages-list b/opt/sbin/cronjob-update-packages-list new file mode 100755 index 0000000..7c74c26 --- /dev/null +++ b/opt/sbin/cronjob-update-packages-list @@ -0,0 +1,34 @@ +#!/bin/bash + +# Check for an /etc/os-release. +[[ ! -e /etc/os-release ]] && { + printf "%s: %s\\n" "${BASH_SOURCE[0]}" "No /etc/os-release to determine system." >&2 + exit 1 +} + +# Source system info. +. /etc/os-release + +# Create package list depending on system type. +case "$ID" in + 'alpine') + apk list -I | cut -d' ' -f1 | rev | cut -d- -f3- | rev >/etc/pkglist + ;; + 'debian'|'devuan'|'ubuntu') + dpkg-query --show --showformat='${Package}\n' >/etc/pkglist + ;; + 'slackware') + slackpkg -batch=on -default_answer=y generate-template "$(hostname --short)" >/dev/null + [[ -L /etc/pkglist ]] && rm -f /etc/pkglist + ls -1 /var/log/packages/ | rev | cut -d- -f4- | rev >/etc/pkglist + ;; + 'void') + xbps-query -l | awk '{ print $2 }' | rev | cut -d- -f2- | rev >/etc/pkglist + ;; + *) + printf "%s: %s\\n" "${BASH_SOURCE[0]}" "Unsupported system." >&2 + exit 1 + ;; +esac + +exit 0 diff --git a/opt/sbin/cronjob-warn-git-status b/opt/sbin/cronjob-warn-git-status deleted file mode 120000 index 356a60e..0000000 --- a/opt/sbin/cronjob-warn-git-status +++ /dev/null @@ -1 +0,0 @@ -../../usr/src/random-scripts.git/cronjob-warn-git-status \ No newline at end of file diff --git a/opt/sbin/cronjob-warn-git-status b/opt/sbin/cronjob-warn-git-status new file mode 100755 index 0000000..c09f931 --- /dev/null +++ b/opt/sbin/cronjob-warn-git-status @@ -0,0 +1,44 @@ +#!/bin/bash + +# Default configuration. +CHECK_DIRS=('/') +EMAIL_TO=('sysadmin@slackware.uk') +EMAIL_FROM="\"Server: ${HOSTNAME%%.*}\" " + +# Allow /etc/default/warn-git-status to override default configuration. +[[ -e /etc/default/warn-git-status ]] && { + source /etc/default/warn-git-status || { + printf "%s: %s\\n" "${0##*/}" "failed reading /etc/default/warn-git-status" >&2 + exit 1 + } +} + +OUTPUT_FILE="/tmp/${0##*/}-$$-$RANDOM" + +# Remove the OUTPUT_FILE when done. +trap 'rm -f "$OUTPUT_FILE"' EXIT + +# Loop through the list and process. +for DIR in "${CHECK_DIRS[@]}"; do + [[ ! -e "$DIR" ]] || [[ ! -d "$DIR" ]] && continue + TMP_OUTPUT="$(cd "$DIR" && [[ "$(git rev-parse --show-toplevel)" == "$PWD" ]] && git status | grep -E -ve "^(On branch|Your branch|No commits|nothing|$)" -e "\(use")" + [[ -n "$TMP_OUTPUT" ]] && printf "%s:\\n%s\\n\\n" "$DIR" "$TMP_OUTPUT" >>"$OUTPUT_FILE" + unset TMP_OUTPUT +done + +[[ ! -s "$OUTPUT_FILE" ]] && { + exit 0 +} + +# Send the message. +if [[ -n "${EMAIL_TO[*]}" ]]; then + mail -r "$EMAIL_FROM" -s "Git statuses" "${EMAIL_TO[@]}" <"$OUTPUT_FILE" >/dev/null 2>&1 || { + printf "%s: %s\\n" "${0##*/}" "mail command failed" >&2 + exit 1 + } +else + printf "%s: %s\\n" "${0##*/}" "no recipient configured for mail delivery" >&2 + exit 1 +fi + +exit 0 diff --git a/opt/sbin/pushover-client b/opt/sbin/pushover-client deleted file mode 120000 index 1bd5007..0000000 --- a/opt/sbin/pushover-client +++ /dev/null @@ -1 +0,0 @@ -../../usr/src/pushover-client.git/pushover-client \ No newline at end of file diff --git a/opt/sbin/pushover-client b/opt/sbin/pushover-client new file mode 100755 index 0000000..1e86583 --- /dev/null +++ b/opt/sbin/pushover-client @@ -0,0 +1,701 @@ +#!/bin/bash +# Version: 0.1.0 +# Copyright (c) 2023: +# Darren 'Tadgy' Austin +# Licensed under the terms of the GNU General Public License version 3. +# +# A PushOver client to send alert messages. + +# Configuration. +CONFIG_API_URL="https://api.pushover.net/1/messages.json" +CONFIG_SOUNDS_API_URL="https://api.pushover.net/1/sounds.json" + +# Defaults. +DEFAULT_SYSTEM_DIR="/etc/pushover-client" +DEFAULT_USER_DIR="${HOME}/.pushover-client" +DEFAULT_CONFIG_FILE="default" +DEFAULT_EXPIRY="3600" +DEFAULT_RETRY="60" + +# Functions. +show_help() { + local SCRIPT="${0##*/}" + + #........1.........2.........3.........4.........5.........6.........7.........8 + cat <<-EOF + Usage: $SCRIPT [config file] [options] + Push notifications to your https://pushover.net registered devices. + + If [config_file] is specified it is used to read the default configuration. + If [config_file] is not specified, a custom user or system 'default' file will be + read to obtain the defaults. If no 'default' custom user or system config file + can be read, command line options are required for operation. + Command line [options] override any config file. + + Options: + -a, --attachment The picture to send with the alert. No default. + -A, --api-url The API URL to use for this submission. Default + is: $CONFIG_API_URL. + Quote if it contains shell special chars. + -c, --callback A URL which is accessed by API server when the + user acknowledges the alert. This option is + only used in priority 2 alerts. + Quote if it contains shell special chars. + -d, --devices A comma seperated list of the devices to receive + the alert. Default is to send to all devices. + -e, --expiry Set the expiration time for alerts sent with + priority 2. Default is 3600 (1 hour). The + maximum expiry time is 10800 (3 hours). + --examples Show extended example usage of this program. + -h, --help Show this help page. + -m, --message The plain text message to send. + Quote if it contains spaces. + This option or -M is required if a message is + not available from a config file. + -M, --html-message The HTML message to send. + Quote if it contains spaces. + This option or -m is required if a message is + not available from a config file. + --monospace Use a monospace font to display the message + given with -m. Default is to use regular font. + This option cannot be used with -M. + -p, --priority Set the priority of the message: + -2 Lowest priority - no alert/notification + will be generated. However, the app + badge or number will update on devices. + -1 Low priority - no alert sound is + emitted but a notification will appear. + During a user's configured quiet hours, + priority -1 is used for messages. + 0 Normal priority (the default) - an alert + sound and notification are generated. + 1 High priority - bypass the user's + configured quiet hours and generate an + alert and notification. + 2 Emergency - as priority 1, but the alert + and notification is repeated (subject to + the -r and -e options) until it is + acknowledged by the recipient. + -q, --quiet Do not print the API execution reply to stdout. + -r, --retry Set the retry interval for alerts sent with + priority 2. Default is 60 (1 minute). The + minimum retry time is 30 seconds. + -s, --subject The subject/title of the message. If unset, + the configured app name is used. + Quote if it contains spaces. + -S, --sound Set the alert sound to play with message: + none None/silent. + vibrate Vibrate only. + pushover Pushover (short, default). + bike Bike (short). + bugle Bugle (short). + cashregister Cash Register (short). + classical Classical (short). + cosmic Cosmic (short). + falling Falling (short). + gamelan Gamelan (short). + incoming Incoming (short). + intermission Intermission (short). + magic Magic (short). + mechanical Mechanical (short). + pianobar Piano Bar (short). + siren Siren (short). + spacealarm Space Alarm (short). + tugboat Tug Boat (short). + alien Alien Alarm (long). + climb Climb (long). + persistent Persistent (long). + echo Pushover Echo (long). + updown Up Down (long). + Or a sound uploaded to the user's account. + -t, --token The pushover.net API token/key for the specific + application. This option is required if not + available from a config file. + -T, --ttl The number of seconds the alert will live (or + be displayed) on a users device before being + automatically removed. The default is no ttl. + This option is ignored when alerts are sent with + priority (-p) 2. + --timestamp The number of seconds since the unix epoch to + use as the timestamp for the alert. + The default timestamp is the time the API + received the message. + -u, --user The pushover.net user key(s). If a single user + key is specified, that account's configuration + will be used for the alerts. If a comma + separated list (maximum 50) of user keys is + given, the alert is sent only to those users. + This option is required if not available from a + config file. + -U, --url Set the URL to send with the alert. + Quote if it contains shell special chars. + --url-title The title of the URL given with -U. Ignored + if -U is not used also. + Quote if it contains spaces. + Option processing ceases with "--". + + For example usage, use: $SCRIPT --examples + EOF +} + +show_examples() { + local SCRIPT="${0##*/}" + + #........1.........2.........3.........4.........5.........6.........7.........8 + cat <<-EOF + Basic usage is: $SCRIPT [config file] [options] + + [config file] configuration options are overridden by command line [options]. + All or part settings may be specified in the [config file], with the addition of + command line [options] to augment the [config file] settings. + [options] can be seen by running: $SCRIPT --help + + Command line examples: + Send a plain text message to all devices, showing all required [options]. + $SCRIPT -u -t -m "Test message" + + Same as the above, but do not show any response from the API server (an + appropriate error/return code is set). + $SCRIPT -q -u -t -m "Test message" + + Send a HTML message (which will be underlined) to all devices, showing all + required [options]. + $SCRIPT -u -t -M "Test message" + + Send a plain text message to all devices, with the addition of a subject/title + to the message. + $SCRIPT -u -t -s "Test subject" -m "..." + + Send a message to all devices, including a picture attachment. + $SCRIPT -u -t -a -m "..." + + Send a message to all devices, including a supplimentary URL with a title. + $SCRIPT -u -t -U "https://afterdark.org.uk" \\ + --url-title "Afterdark" -m "Check out the included URL!" + + Send a message to all devices, selecting an alternative sound alert. + $SCRIPT -u -t -s "magic" -m "..." + + Send a message to a specific list of devices. + $SCRIPT -u -t -d "iphone,ipad" -m "..." + + Send an emergency alert, repeated every minute for an 2 hours until it is + acknowledged by the user. + $SCRIPT -u -t -p 2 -r 60 -e 7200 -m "..." + + Read all configuration from path and use that for sending the + alert. If this path does not exist, search for a file matching the the + name in the user's private pushover directory (~/.pushover-client) + or the system wide pushover directory (/etc/pushover-client), in that order. + $SCRIPT + EOF +} + +# Pick out possible config file and '--' arguments of the command line. +ARGS=( "$@" ) +(( ${#ARGS[@]} >= 1 )) && FIRST_ARG="${ARGS[0]}" +(( ${#ARGS[@]} >= 2 )) && SECOND_ARG="${ARGS[1]}" + +# Get the config filename if it was given on the command line, or use the default. +[[ -z "$FIRST_ARG" ]] || [[ "$FIRST_ARG" =~ -[^-] ]] && FILENAME="$DEFAULT_CONFIG_FILE" && SKIP_CUSTOM=1 +[[ ! -v FILENAME ]] && [[ "$FIRST_ARG" != -* ]] && FILENAME="$FIRST_ARG" && unset "ARGS[0]" +[[ ! -v FILENAME ]] && [[ "$FIRST_ARG" == "--" ]] && [[ -n "$SECOND_ARG" ]] && FILENAME="$SECOND_ARG" && unset "ARGS[1]" + +# Find and validate the correct config filename. +if (( ${SKIP_CUSTOM:-0} == 1 )) && [[ -e "$DEFAULT_USER_DIR/$FILENAME" ]] && [[ ! -d "$DEFAULT_USER_DIR/$FILENAME" ]] && \ + [[ -r "$DEFAULT_USER_DIR/$FILENAME" ]]; then + FILENAME="$DEFAULT_USER_DIR/$FILENAME" +elif (( ${SKIP_CUSTOM:-0} == 1 )) && [[ -e "$DEFAULT_SYSTEM_DIR/$FILENAME" ]] && [[ ! -d "$DEFAULT_SYSTEM_DIR/$FILENAME" ]] && \ + [[ -r "$DEFAULT_SYSTEM_DIR/$FILENAME" ]]; then + FILENAME="$DEFAULT_SYSTEM_DIR/$FILENAME" +elif [[ "${FILENAME:0:1}" != "/" ]] && [[ -e "$FILENAME" ]] && [[ ! -d "$FILENAME" ]] && [[ -r "$FILENAME" ]]; then + : +elif [[ "${FILENAME:0:1}" != "/" ]] && [[ -e "$DEFAULT_USER_DIR/$FILENAME" ]] && [[ ! -d "$DEFAULT_USER_DIR/$FILENAME" ]] && \ + [[ -r "$DEFAULT_USER_DIR/$FILENAME" ]]; then + FILENAME="$DEFAULT_USER_DIR/$FILENAME" +elif [[ "${FILENAME:0:1}" != "/" ]] && [[ -e "$DEFAULT_SYSTEM_DIR/$FILENAME" ]] && [[ ! -d "$DEFAULT_SYSTEM_DIR/$FILENAME" ]] && \ + [[ -r "$DEFAULT_SYSTEM_DIR/$FILENAME" ]]; then + FILENAME="$DEFAULT_SYSTEM_DIR/$FILENAME" +elif [[ "${FILENAME:0:1}" == "/" ]] && [[ -e "$FILENAME" ]] && [[ ! -d "$FILENAME" ]] && [[ -r "$FILENAME" ]]; then + : +elif [[ -n "$FILENAME" ]]; then + printf "%s: '%s' %s\\n" "${0##*/}" "$FILENAME" "invalid config file name" >&2 + exit 1 +fi + +# Read the config file. +[[ -n "$FILENAME" ]] && { + eval "$(awk '!/^(#|$|[[:blank:]]*$)/ { print "CONFIG_"$0 }' "$FILENAME")" 2>/dev/null || { + printf "%s: %s '%s'\\n" "${0##*/}" "error in config file" "$FILENAME" >&2 + exit 1 + } +} + +# Parse command line options. +set -- "${ARGS[@]}" +while [[ -n "$1" ]]; do + case "$1" in + -a|-attachment|--attachment) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "attachment filename cannot be an empty value" >&2 + exit 1 + else + CONFIG_ATTACHMENT="$2" + fi + shift + ;; + -A|-api-url|--api-url) + # Setting is validated below. + CONFIG_API_URL="$2" + shift + ;; + -c|-callback|--callback) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "callback URL address cannot be empty" >&2 + exit 1 + else + CONFIG_CALLBACK_URL="$2" + fi + shift + ;; + -d|-devices|--devices) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "device list cannot be an empty value" >&2 + exit 1 + else + CONFIG_DEVICES="$2" + fi + shift + ;; + -e|-expiry|--expiry) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "expiry time cannot be an empty value" >&2 + exit 1 + else + CONFIG_EXPIRY="$2" + fi + shift + ;; + -examples|--examples) + show_examples + exit 0 + ;; + -h|-help|--help) + show_help + exit 0 + ;; + -m|-message|--message) + # Setting is validated below. + CONFIG_MESSAGE="$2" + shift + ;; + -M|-html-message|--html-message) + # Setting is validated below. + CONFIG_HTML_MESSAGE="$2" + shift + ;; + -monospace|--monospace) + # Setting is validated below. + CONFIG_MONOSPACE="1" + shift + ;; + -p|-priority|--priority) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "priority setting cannot be an empty value" >&2 + exit 1 + else + CONFIG_PRIORITY="$2" + fi + shift + ;; + -q|-quiet|--quiet) + CONFIG_QUIET=1 + shift + ;; + -r|-retry|--retry) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "retry time cannot be an empty value" >&2 + exit 1 + else + CONFIG_RETRY="$2" + fi + shift + ;; + -s|-subject|--subject) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "subject/title cannot be an empty value" >&2 + exit 1 + else + CONFIG_SUBJECT="$2" + fi + shift + ;; + -S|-sound|--sound) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "sound selection cannot be empty" >&2 + exit 1 + else + CONFIG_SOUND="$2" + fi + shift + ;; + -t|-token|--token) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "token key cannot be an empty value" >&2 + exit 1 + else + CONFIG_TOKEN="$2" + fi + shift + ;; + -T|-ttl|--ttl) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "ttl cannot be an empty value" >&2 + exit 1 + else + CONFIG_TTL="$2" + fi + shift + ;; + -timestamp|--timestamp) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "timestamp cannot be an empty value" >&2 + exit 1 + else + CONFIG_TIMESTAMP="$2" + fi + shift + ;; + -u|-user|--user) + # Setting is validated below. + CONFIG_USER_KEYS="$2" + shift + ;; + -U|-url|--url) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "URL address cannot be empty" >&2 + exit 1 + else + CONFIG_URL="$2" + fi + shift + ;; + -url-title|--url-title) + if [[ "$2" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "URL title cannot be empty" >&2 + exit 1 + else + CONFIG_URL_TITLE="$2" + fi + shift + ;; + --) + shift + break + ;; + *) + printf "%s: %s: %s\\n" "${0##*/}" "invalid option" "$1" >&2 + printf "%s: %s %s\\n" "Try" "${0##*/}" "--help" >&2 + exit 1 + ;; + esac + shift +done + +# The options list should be empty if the correct syntax was used. +[[ -n "$1" ]] && { + printf "%s: %s\\n" "${0##*/}" "options cannot come after --" >&2 + exit 1 +} + +# Validate all the CONFIG_* settings. +[[ "$CONFIG_API_URL" =~ ^[[:blank:]]*$ ]] && { + printf "%s: %s\\n" "${0##*/}" "API URL (-A) cannot be an empty value" >&2 + exit 1 +} + +if [[ ! "$CONFIG_ATTACHMENT" =~ ^[[:blank:]]*$ ]]; then + if [[ ! -e "$CONFIG_ATTACHMENT" ]]; then + printf "%s: %s '%s' %s\\n" "${0##*/}" "attachment (-a)" "$CONFIG_ATTACHMENT" "does not exist" >&2 + exit 1 + elif [[ -d "$CONFIG_ATTACHMENT" ]]; then + printf "%s: %s '%s' %s\\n" "${0##*/}" "attachment (-a)" "$CONFIG_ATTACHMENT" "is a directory, not a file" >&2 + exit 1 + elif [[ ! -r "$CONFIG_ATTACHMENT" ]]; then + printf "%s: %s '%s' %s\\n" "${0##*/}" "attachment (-a)" "$CONFIG_ATTACHMENT" "is not readable by you" >&2 + exit 1 + elif (( $(stat --printf=%s "$CONFIG_ATTACHMENT") > 2621440 )); then + printf "%s: %s\\n" "${0##*/}" "attachment (-a) is too large (2.5MB maximum)" >&2 + exit 1 + fi +else + unset "CONFIG_ATTACHMENT" +fi + +if [[ ! "$CONFIG_CALLBACK_URL" =~ ^[[:blank:]]*$ ]]; then + (( ${#CONFIG_CALLBACK_URL} > 512 )) && { + printf "%s: %s\\n" "${0##*/}" "callback URL (-c) is too long (maximum 512 characters)" >&2 + exit 1 + } +else + unset "CONFIG_CALLBACK_URL" +fi + +if [[ ! "$CONFIG_DEVICES" =~ ^[[:blank:]]*$ ]]; then + while read -r -d , DEV; do + [[ -z "$DEV" ]] && continue + if (( "${#DEV}" > 25 )); then + printf "%s: %s\\n" "${0##*/}" "device IDs (-d) are 25 characters or less in length" >&2 + exit 1 + elif [[ ! "$DEV" =~ ^[[:alnum:]_-]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "device IDs (-d) can only be letters, numbers, _ and -" >&2 + exit 1 + else + if [[ -z "$LIST" ]]; then + LIST="$DEV" + else + LIST+=",$DEV" + fi + fi + done <<<"$CONFIG_DEVICES," # The , on the end is required. + CONFIG_DEVICES="$LIST" +else + unset "CONFIG_DEVICES" +fi + +if [[ ! "$CONFIG_EXPIRY" =~ ^[[:blank:]]*$ ]]; then + if [[ ! "$CONFIG_EXPIRY" =~ [[:digit:]] ]]; then + printf "%s: %s\\n" "${0##*/}" "expiry time (-e) must be a number" >&2 + exit 1 + elif (( CONFIG_EXPIRY < 30 )); then + printf "%s: %s\\n" "${0##*/}" "expiry time (-e) cannot be that short (30s minimum)" >&2 + exit 1 + elif (( CONFIG_EXPIRY > 10800 )); then + printf "%s: %s\\n" "${0##*/}" "expiry time (-e) cannot be that long (10800s/3h maximum)" >&2 + exit 1 + elif [[ -n "$CONFIG_RETRY" ]] && (( CONFIG_EXPIRY < CONFIG_RETRY )); then + printf "%s: %s\\n" "${0##*/}" "expiry time (-e) is less than retry time" >&2 + exit 1 + fi +else + unset "CONFIG_EXPIRY" +fi + +if [[ "$CONFIG_MONOSPACE" =~ ^[[:blank:]]*$ ]] || [[ "$CONFIG_MONOSPACE" =~ ^(false|0)$ ]]; then + CONFIG_MONOSPACE=0 +elif [[ "$CONFIG_MONOSPACE" =~ ^(true|1)$ ]]; then + CONFIG_MONOSPACE=1 +else + printf "%s: %s\\n" "${0##*/}" "monospace (--monospace) setting must be 'true', '1', 'false' or '0'" >&2 + exit 1 +fi + +if [[ ! "$CONFIG_MESSAGE" =~ ^[[:blank:]]*$ ]] && [[ ! "$CONFIG_HTML_MESSAGE" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "cannot mix a plain text (-m) and HTML (-M) message setting" >&2 + exit 1 +elif [[ "$CONFIG_MESSAGE" =~ ^[[:blank:]]*$ ]] && [[ "$CONFIG_HTML_MESSAGE" =~ ^[[:blank:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "must set either a plain text (-m) or HTML (-M) message" >&2 + exit 1 +elif [[ ! "$CONFIG_HTML_MESSAGE" =~ ^[[:blank:]]*$ ]] && (( CONFIG_MONOSPACE == 1 )); then + printf "%s: %s\\n" "${0##*/}" "HTML messages (-M) cannot be used with a monospace font (--monospace)" >&2 + exit 1 +elif (( ${#CONFIG_MESSAGE} > 1024 )) || (( ${#CONFIG_HTML_MESSAGE} > 1024 )); then + printf "%s: %s\\n" "${0##*/}" "message (-m/-M) length is too long (maximum 1024 characters)" >&2 + exit 1 +fi + +if [[ ! "$CONFIG_PRIORITY" =~ ^[[:blank:]]*$ ]]; then + if [[ ! "$CONFIG_PRIORITY" =~ [[:digit:]] ]]; then + printf "%s: %s\\n" "${0##*/}" "priority (-p) setting must be a number" >&2 + exit 1 + elif (( CONFIG_PRIORITY < -2 )) || (( CONFIG_PRIORITY > 2 )); then + printf "%s: %s\\n" "${0##*/}" "priority (-p) setting must be in the range -2 - 2" >&2 + exit 1 + fi +else + unset "CONFIG_PRIORITY" +fi + +if [[ "$CONFIG_QUIET" =~ ^[[:blank:]]*$ ]] || [[ "$CONFIG_QUIET" =~ ^(false|0)$ ]]; then + CONFIG_QUIET=0 +elif [[ "$CONFIG_QUIET" =~ ^(true|1)$ ]]; then + CONFIG_QUIET=1 +else + printf "%s: %s\\n" "${0##*/}" "quiet (-q) setting must be 'true', '1', 'false' or '0'" >&2 + exit 1 +fi + +if [[ ! "$CONFIG_RETRY" =~ ^[[:blank:]]*$ ]]; then + if [[ ! "$CONFIG_RETRY" =~ [[:digit:]] ]]; then + printf "%s: %s\\n" "${0##*/}" "retry time (-r) must be a number" >&2 + exit 1 + elif (( CONFIG_RETRY < 30 )); then + printf "%s: %s\\n" "${0##*/}" "retry time (-r) cannot be that short (30s minimum)" >&2 + exit 1 + elif [[ -n "$CONFIG_EXPIRY" ]] && (( CONFIG_RETRY > CONFIG_EXPIRY )); then + printf "%s: %s\\n" "${0##*/}" "retry time (-r) exceeds expiry time" >&2 + exit 1 + fi +else + unset "CONFIG_RETRY" +fi + +if [[ ! "$CONFIG_SUBJECT" =~ ^[[:blank:]]*$ ]]; then + (( ${#CONFIG_SUBJECT} > 250 )) && { + printf "%s: %s\\n" "${0##*/}" "subject/title (-s) is too long (maximum 250 characters)" >&2 + exit 1 + } +else + unset "CONFIG_SUBJECT" +fi + +if [[ ! "$CONFIG_TOKEN" =~ ^[[:blank:]]*$ ]]; then + if (( "${#CONFIG_TOKEN}" != 30 )); then + printf "%s: %s\\n" "${0##*/}" "token key (-t) must be 30 characters in length" >&2 + exit 1 + elif [[ ! "$CONFIG_TOKEN" =~ ^[[:alnum:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "token key (-t) can only be letters and numbers" >&2 + exit 1 + fi +else + printf "%s: %s\\n" "${0##*/}" "token key (-t) cannot be an empty value" >&2 + exit 1 +fi + +if [[ ! "$CONFIG_SOUND" =~ ^[[:blank:]]*$ ]]; then + [[ ! "$CONFIG_SOUND" =~ ^(none|vibrate|pushover|bike|bugle|cashregister|classical|cosmic|falling|gamelan|incoming|intermission|magic|mechanical|pianobar|siren|spacealarm|tugboat|alien|climb|persistent|echo|updown)$ ]] && { + JSON="$(curl -s "$CONFIG_SOUNDS_API_URL?token=$CONFIG_TOKEN" 2>/dev/null)" + if [[ "$JSON" =~ \"status\":1, ]]; then + [[ ! "$JSON" =~ \"$CONFIG_SOUND\": ]] && { + printf "%s: %s\\n" "${0##*/}" "sound (-S) must be a built-in or user uploaded custom sound" >&2 + exit 1 + } + else + printf "%s: %s\\n" "${0##*/}" "failed to get list of custom sounds from API (invalid API token?)" >&2 + exit 1 + fi + } +else + unset "CONFIG_SOUND" +fi + +if [[ ! "$CONFIG_TTL" =~ ^[[:blank:]]*$ ]]; then + if [[ ! "$CONFIG_TTL" =~ [[:digit:]] ]]; then + printf "%s: %s\\n" "${0##*/}" "TTL (-T) must be a number" >&2 + exit 1 + elif (( CONFIG_TTL < 0 )); then + printf "%s: %s\\n" "${0##*/}" "TTL (-T) must be a positive number of seconds" >&2 + exit 1 + fi +else + unset "CONFIG_TTL" +fi + +if [[ ! "$CONFIG_TIMESTAMP" =~ ^[[:blank:]]*$ ]]; then + if [[ ! "$CONFIG_TIMESTAMP" =~ [[:digit:]] ]]; then + printf "%s: %s\\n" "${0##*/}" "timestamp (--timestamp) must be a number" >&2 + exit 1 + elif (( CONFIG_TIMESTAMP < 0 )); then + printf "%s: %s\\n" "${0##*/}" "timestamp (--timestamp) must be a positive number of seconds" >&2 + exit 1 + fi +else + unset "CONFIG_TIMESTAMP" +fi + +if [[ ! "$CONFIG_USER_KEYS" =~ ^[[:blank:]]*$ ]]; then + unset COUNT LIST + while read -r -d , KEY; do + [[ -z "$KEY" ]] && continue + if (( "${#KEY}" != 30 )); then + printf "%s: %s\\n" "${0##*/}" "user keys (-u) must be 30 characters in length" >&2 + exit 1 + elif [[ ! "$KEY" =~ ^[[:alnum:]]*$ ]]; then + printf "%s: %s\\n" "${0##*/}" "user keys (-u) can only be letters and numbers" >&2 + exit 1 + else + if [[ -z "$LIST" ]]; then + LIST="$KEY" + COUNT=1 + else + LIST+=",$KEY" + (( COUNT++ )) + fi + fi + done <<<"$CONFIG_USER_KEYS," # The , on the end is required. + (( COUNT > 50 )) && { + printf "%s: %s\\n" "${0##*/}" "too many user keys (-u) (maximum 50)" >&2 + exit 1 + } + CONFIG_USER_KEYS="$LIST" +else + printf "%s: %s\\n" "${0##*/}" "user keys (-u) cannot be an empty value" >&2 + exit 1 +fi + +if [[ ! "$CONFIG_URL" =~ ^[[:blank:]]*$ ]]; then + (( ${#CONFIG_URL} > 512 )) && { + printf "%s: %s\\n" "${0##*/}" "URL (-U) is too long (maximum 512 characters)" >&2 + exit 1 + } +else + unset "CONFIG_URL" +fi + +if [[ ! "$CONFIG_URL_TITLE" =~ ^[[:blank:]]*$ ]]; then + (( ${#CONFIG_URL_TITLE} > 100 )) && { + printf "%s: %s\\n" "${0##*/}" "URL title (--url-title) is too long (maximum 100 characters)" >&2 + exit 1 + } +else + unset "CONFIG_URL_TITLE" +fi + +# Build the curl command line. +COMMAND_LINE=('-s') +# Required API elements. +COMMAND_LINE+=('--form-string' user="$CONFIG_USER_KEYS") +COMMAND_LINE+=('--form-string' token="$CONFIG_TOKEN") +if [[ -v CONFIG_MESSAGE ]]; then + COMMAND_LINE+=('--form-string' message="$CONFIG_MESSAGE") + (( CONFIG_MONOSPACE == 1 )) && COMMAND_LINE+=('--form-string' 'monospace=1') +else + COMMAND_LINE+=('--form-string' message="$CONFIG_HTML_MESSAGE") + COMMAND_LINE+=('--form-string' 'html=1') +fi +# Optional API elements. +[[ -v CONFIG_ATTACHMENT ]] && COMMAND_LINE+=('--form' attachment="@$CONFIG_ATTACHMENT") +[[ -v CONFIG_DEVICES ]] && COMMAND_LINE+=('--form-string' device="$CONFIG_DEVICES") +[[ -v CONFIG_PRIORITY ]] && { + COMMAND_LINE+=('--form-string' priority="$CONFIG_PRIORITY") + (( CONFIG_PRIORITY == 2 )) && { + [[ -v CONFIG_CALLBACK_URL ]] && COMMAND_LINE+=('--form-string' callback="$CONFIG_CALLBACK_URL") + COMMAND_LINE+=('--form-string' expire="${CONFIG_EXPIRY:-$DEFAULT_EXPIRY}") + COMMAND_LINE+=('--form-string' retry="${CONFIG_RETRY:-$DEFAULT_RETRY}") + } +} +[[ -v CONFIG_SUBJECT ]] && COMMAND_LINE+=('--form-string' title="$CONFIG_SUBJECT") +[[ -v CONFIG_SOUND ]] && COMMAND_LINE+=('--form-string' sound="$CONFIG_SOUND") +[[ -v CONFIG_TTL ]] && COMMAND_LINE+=('--form-string' ttl="$CONFIG_TTL") +[[ -v CONFIG_TIMESTAMP ]] && COMMAND_LINE+=('--form-string' timestamp="$CONFIG_TIMESTAMP") +[[ -v CONFIG_URL ]] && COMMAND_LINE+=('--form-string' url="$CONFIG_URL") +[[ -v CONFIG_URL_TITLE ]] && COMMAND_LINE+=('--form-string' url_title="$CONFIG_URL_TITLE") + +# Make the call to the messaging API. +JSON="$(curl "${COMMAND_LINE[@]}" "$CONFIG_API_URL" 2>/dev/null)" || { + printf "%s: %s\\n" "${0##*/}" "API call failed" >&2 + exit 3 +} +if [[ "$JSON" =~ \"status\":1, ]]; then + (( CONFIG_QUIET == 0 )) && printf "%s\\n" "$JSON" +else + printf "%s: %s\\n" "${0##*/}" "API returned a non-success status code" >&2 + (( CONFIG_QUIET == 0 )) && printf "%s\\n" "$JSON" >&2 + exit 2 +fi + +# 0 = Request submitted OK. +# 1 = Usage error. +# 2 = Request status error from API +# 3 = Error accessing API +exit 0 diff --git a/opt/sbin/terraform-http-backend b/opt/sbin/terraform-http-backend deleted file mode 120000 index df421be..0000000 --- a/opt/sbin/terraform-http-backend +++ /dev/null @@ -1 +0,0 @@ -../../usr/src/random-scripts.git/terraform-http-backend \ No newline at end of file diff --git a/opt/sbin/terraform-http-backend b/opt/sbin/terraform-http-backend new file mode 100755 index 0000000..0f3578a Binary files /dev/null and b/opt/sbin/terraform-http-backend differ