diff --git a/.env b/.env index a0b6cc6..a8a7b9c 100644 --- a/.env +++ b/.env @@ -7,9 +7,16 @@ export RCON_PASSWORD="" export POLL_SECONDS="10" export ANNOUNCE_SERVER_UPDOWN="true" -# Anzeigename/Tag +# Spiel-Adresse: +export GAME_HOST="mc.techniverse.net" +export GAME_PORT="25573" + +# Anzeigename: SERVER_NAME="[GER] Blockventure | ⌁25573" +# Optionale Zusatzzeile im Body +MESSAGE_EXTRA="Whitelist aktiv" + # ntfy export NTFY_SERVER="https://ntfy.pushservice.techniverse.net" export NTFY_TOPIC="mc-events" diff --git a/mc-ntfy-notify.v1.sh b/mc-ntfy-notify.v1.sh index 153ff70..6efb9d1 100644 --- a/mc-ntfy-notify.v1.sh +++ b/mc-ntfy-notify.v1.sh @@ -4,9 +4,13 @@ # Autor: Patrick Asmus # Web: https://www.cleveradmin.de # Repository: https://git.techniverse.net/scriptos/minecraft-ntfy-notify -# Version: 1.5 -# Datum: 18.09.2025 -# Modifikation: Tags entfernt; Titel statisch (kein Join/Leave im Titel); DNS:Port im Body ergänzt +# Version: 1.8 +# Datum: 19.09.2025 +# Änderungen: +# - SERVER_NAME im Titel +# - GAME_HOST/GAME_PORT im Body +# - MESSAGE_EXTRA als zusätzliche Body-Zeile (optional) +# - Body-Layout mehrzeilig: Event, Server, Port, [Extra] ##################################################### set -euo pipefail export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin @@ -21,10 +25,15 @@ elif [[ -f "${SCRIPT_DIR}/.env" ]]; then fi # ===== Konfiguration via ENV ===== +# RCON (für Polling) MC_HOST="${MC_HOST:-127.0.0.1}" RCON_PORT="${RCON_PORT:-25575}" RCON_PASSWORD="${RCON_PASSWORD:-changeme}" +# Spiel-Adresse (DNS:Port zum Joinen, nur Anzeige) +GAME_HOST="${GAME_HOST:-${MC_HOST}}" +GAME_PORT="${GAME_PORT:-25565}" + POLL_SECONDS="${POLL_SECONDS:-10}" ANNOUNCE_SERVER_UPDOWN="${ANNOUNCE_SERVER_UPDOWN:-true}" @@ -39,9 +48,12 @@ NTFY_PRIORITY_UP="${NTFY_PRIORITY_UP:-4}" NTFY_PRIORITY_DOWN="${NTFY_PRIORITY_DOWN:-5}" NTFY_MARKDOWN="${NTFY_MARKDOWN:-false}" -# Optionaler Anzeigename (nur informativ) +# Anzeigename für den Titel SERVER_NAME="${SERVER_NAME:-}" +# Optionale Zusatzzeile im Body +MESSAGE_EXTRA="${MESSAGE_EXTRA:-}" + # Lock & State je Instanz (Host:Port:Topic) LOCK_KEY="$(printf '%s' "${MC_HOST}_${RCON_PORT}_${NTFY_TOPIC}" | tr -c 'A-Za-z0-9._-' '_')" STATE_DIR="${STATE_DIR:-${SCRIPT_DIR}/state.${LOCK_KEY}}" @@ -73,7 +85,6 @@ acquire_lock() { if [[ -f "$PID_FILE" ]]; then oldpid="$(cat "$PID_FILE" 2>/dev/null || true)" if [[ -n "${oldpid:-}" ]] && kill -0 "$oldpid" 2>/dev/null; then - dbg "Bereits laufend (PID $oldpid), beende." exit 0 fi fi @@ -84,11 +95,33 @@ acquire_lock() { fi } +build_title() { + if [[ -n "$SERVER_NAME" ]]; then + printf '%s - %s' "$NTFY_TITLE_PREFIX" "$SERVER_NAME" + else + printf '%s' "$NTFY_TITLE_PREFIX" + fi +} + +# Baut den Body in gewünschtem Layout: +# \nServer: \nPort: \n[] +build_body() { + local event="$1" + if [[ -n "$MESSAGE_EXTRA" ]]; then + printf '%s\n\nServer: %s\nPort: %s\n%s' \ + "$event" "$GAME_HOST" "$GAME_PORT" "$MESSAGE_EXTRA" + else + printf '%s\nServer: %s\nPort: %s' \ + "$event" "$GAME_HOST" "$GAME_PORT" + fi +} + ntfy_notify() { - # ntfy_notify "Titel" "Nachricht" PRIORITY - local title="$1" body="$2" priority="$3" + # ntfy_notify "Body" PRIORITY + local body="$1" priority="$2" [[ -z "$NTFY_SERVER" || -z "$NTFY_TOPIC" ]] && { echo "ntfy Server/Topic fehlt – skip"; return 1; } local url="${NTFY_SERVER%/}/${NTFY_TOPIC}" + local title; title="$(build_title)" local args=(-sS -X POST "$url" -H "Title: ${title}" -H "Priority: ${priority}") [[ "$NTFY_MARKDOWN" == "true" ]] && args+=(-H "Markdown: yes") [[ -n "$NTFY_TOKEN" ]] && args+=(-H "Authorization: Bearer ${NTFY_TOKEN}") @@ -102,7 +135,7 @@ ensure_state() { [[ -f "$STATE_UP" ]] || echo "unknown" >"$STATE_UP" } -# ANSI- und MC-Farbcodes entfernen +# ANSI- & MC-Farbcodes entfernen sanitize() { sed -E $'s/\x1B\\[[0-9;]*[A-Za-z]//g' \ | sed -E 's/§[0-9A-FK-ORa-fk-or]//g' \ @@ -140,7 +173,7 @@ need_bin mcrcon ensure_state acquire_lock -echo "Starte Polling ${MC_HOST}:${RCON_PORT} -> ntfy ${NTFY_SERVER}/${NTFY_TOPIC} (PID $$)" +echo "Starte Polling RCON ${MC_HOST}:${RCON_PORT} -> ntfy ${NTFY_SERVER}/${NTFY_TOPIC} (PID $$)" prev_up="$(cat "$STATE_UP")" # ===== Main Loop ===== @@ -157,13 +190,9 @@ while :; do if [[ "$ANNOUNCE_SERVER_UPDOWN" == "true" && "$prev_up" != "unknown" && "$server_up" != "$prev_up" ]]; then if [[ "$server_up" == "true" ]]; then - ntfy_notify "${NTFY_TITLE_PREFIX}" \ - "Server ist wieder erreichbar (${MC_HOST}:${RCON_PORT})." \ - "$NTFY_PRIORITY_UP" || true + ntfy_notify "$(build_body "Server ist wieder erreichbar.")" "$NTFY_PRIORITY_UP" || true else - ntfy_notify "${NTFY_TITLE_PREFIX}" \ - "Server ist nicht erreichbar (${MC_HOST}:${RCON_PORT})." \ - "$NTFY_PRIORITY_DOWN" || true + ntfy_notify "$(build_body "Server ist nicht erreichbar.")" "$NTFY_PRIORITY_DOWN" || true fi fi @@ -175,17 +204,13 @@ while :; do if [[ -n "$joined" ]]; then while IFS= read -r name; do [[ -z "$name" ]] && continue - ntfy_notify "${NTFY_TITLE_PREFIX}" \ - "Player \"${name}\" ist beigetreten. Server: ${MC_HOST}:${RCON_PORT}" \ - "$NTFY_PRIORITY_JOIN" || true + ntfy_notify "$(build_body "Player \"${name}\" ist beigetreten.")" "$NTFY_PRIORITY_JOIN" || true done <<<"$joined" fi if [[ -n "$left" ]]; then while IFS= read -r name; do [[ -z "$name" ]] && continue - ntfy_notify "${NTFY_TITLE_PREFIX}" \ - "Player \"${name}\" hat den Server verlassen. Server: ${MC_HOST}:${RCON_PORT}" \ - "$NTFY_PRIORITY_LEAVE" || true + ntfy_notify "$(build_body "Player \"${name}\" hat den Server verlassen.")" "$NTFY_PRIORITY_LEAVE" || true done <<<"$left" fi