From 3a56b99e3df2e0172dc4ac462afa728bca732545 Mon Sep 17 00:00:00 2001 From: Andrew Stowell Date: Wed, 15 Nov 2023 14:25:42 +0000 Subject: [PATCH] windows discord note when message is empty, linux ddns and discord new quiet options. --- Discord.ps1 | 2 ++ SendDiscordChunk.ps1 | 31 ++++++++++++++++++------------- discord.sh | 18 ++++++++++++++---- dynv6.sh | 21 ++++++++++++++++++--- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/Discord.ps1 b/Discord.ps1 index 9894863..1bf4a6d 100644 --- a/Discord.ps1 +++ b/Discord.ps1 @@ -105,6 +105,8 @@ end { if ($distinct) { Out-File -FilePath $distinctFile -InputObject $message } + } elseif ($distinct) { + echo "duplicate message" } } diff --git a/SendDiscordChunk.ps1 b/SendDiscordChunk.ps1 index d0ec412..7345921 100644 --- a/SendDiscordChunk.ps1 +++ b/SendDiscordChunk.ps1 @@ -16,7 +16,7 @@ function SendDiscordMessage # convert string into json, escaping the given message $title = ConvertTo-Json -InputObject $title - $message = ConvertTo-Json -InputObject $message + $message = ConvertTo-Json -InputObject $message.trim() $botName = ConvertTo-Json -InputObject $botName # rewrite json string, because "\u001b[m" needs to "\u001b[0m" instead @@ -24,29 +24,34 @@ function SendDiscordMessage $message = $message -Replace "\\u001b\[m", "\u001b[0m" # only enable colors if needed - $prefix = ''; + $prefix = '' if ($message -contains "\u001b") { - $prefix = 'ansi\n'; + $prefix = 'ansi\n' } - # remove wrapping quotes around json string, replace with color-enabled block quotes - $message = '"```' + $prefix + $message.SubString(1, $message.length - 2) + '\n```"' + if ($message -eq '""') { + # message empty, so denote as such + $message = '"\ud83d\udea9 MESSAGE EMPTY \ud83d\udea9"' + } else { + # remove wrapping quotes around json string, replace with color-enabled block quotes + $message = '"```' + $prefix + $message.SubString(1, $message.length - 2) + '\n```"' + } # build post data: {"username": "$botName", "content": "$message"} - $content = "{"; - if ($botName) { - $content += '"username": ' + $botName + ', '; + $content = "{" + if ($botName -ne '""') { + $content += '"username": ' + $botName + ', ' } - $content += '"embeds": [{"description": ' + $message; + $content += '"embeds": [{"description": ' + $message if ($color -ge 0) { - $content += ', "color": ' + $color; + $content += ', "color": ' + $color } - if ($title) { - $content += ', "title": ' + $title; + if ($title -ne '""') { + $content += ', "title": ' + $title } - $content += '}]}'; + $content += '}]}' # post data is complex, so we have to pipe it into curl # display the resulting id/message returned from Discord diff --git a/discord.sh b/discord.sh index 9c73f81..ee88c90 100755 --- a/discord.sh +++ b/discord.sh @@ -9,6 +9,7 @@ # cyan(#00ffff), blue(#0343df), purple(#7e1e9c), magenta(#c20078), grey(#929591), # LIGHT: pink(#ff81c0), apricot(#ffb16d), beige(#e6daa6), mint(#9ffeb0), lavender(#c79fef), white(#ffffff). +quiet=false skipMsg=false declare -A colors colors=( @@ -23,7 +24,7 @@ print_usage() { } # handle all arguments provided -while getopts 'h:c:d:t:z' flag; do +while getopts 'h:c:d:t:zq' flag; do case "$flag" in h) if [[ "$OPTARG" == "http"*"?wait=true" ]]; then @@ -67,6 +68,9 @@ while getopts 'h:c:d:t:z' flag; do z) skipMsg=true ;; + q) + quiet=true + ;; *) echo "unknown option provided '$flag'" print_usage @@ -103,7 +107,8 @@ if [[ -z "$message" ]] && [[ "$skipMsg" = false ]]; then fi # sed removes blank lines from the end of the message -fullMsg=$(echo "$message" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}') +message=$(echo "$message" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}') +fullMsg="$message" # if message is too long, split it, and after curl, recursively call with remainder # 4k max, but we must account for escaping text in json, so limit to 90% capacity @@ -127,8 +132,13 @@ fi message=$(echo "$message" | jq -aRs . | sed 's/\\u001b\[m/\\u001b[0m/g') if [[ "$message" = '"\n"' ]]; then - # message empty, so denote as such - message='"\ud83d\udea9 MESSAGE EMPTY \ud83d\udea9"' + if [[ "$quiet" = true ]]; then + # quiet was specified, and there was no message, so stop now + exit 0 + else + # message empty, so denote as such + message='"\ud83d\udea9 MESSAGE EMPTY \ud83d\udea9"' + fi else if [[ "$message" = *"\\u001b"* ]]; then prefix="ansi\n" diff --git a/dynv6.sh b/dynv6.sh index 9ffdd32..c85b5c5 100755 --- a/dynv6.sh +++ b/dynv6.sh @@ -1,5 +1,15 @@ #!/bin/sh +quiet=false +while getopts 'q' flag; do + case "$flag" in + q) + quiet=true + ;; + esac +done +shift "$((OPTIND-1))" + # we need zone and token, we also accept, netmask, and overrides zone=$1 device=$2 @@ -7,7 +17,8 @@ scope=$3 # stop if we do not have the required information if [ -z "$zone" -o -z "$token" ]; then - echo "Usage: token= [netmask=64] [ipv4=] [ipv6=] $0 your-name.dynv6.net [device] [scope]" + echo '"-q": quiet flag, to suppress "address unchanged" messages' + echo "Usage: token= [netmask=64] [ipv4=] [ipv6=] $0 your-name.dynv6.net [-q] [device] [scope]" exit 1 fi @@ -73,7 +84,9 @@ ipv6="$ipv6/$netmask" # update IPv4, if changed if [ "$old4" = "$ipv4" ]; then - echo "IPv4 address unchanged" + if [ "$quiet" = false ]; then + echo "IPv4 address unchanged" + fi else # send IPv4 address to dynv6 echo -n "Updating IPv4: " @@ -86,7 +99,9 @@ fi # update IPv6, if changed if [ "$old6" = "$ipv6" ]; then - echo "IPv6 address unchanged" + if [ "$quiet" = false ]; then + echo "IPv6 address unchanged" + fi else # send IPv6 address to dynv6 echo -n "Updating IPv6: "