ANSI/MC-Farbcodes strippen, robustes Schreiben der Playerliste

This commit is contained in:
scriptos 2025-09-18 21:59:22 +02:00
parent 122b2b3ef7
commit 9c297ab7b1

View File

@ -4,9 +4,9 @@
# Autor: Patrick Asmus # Autor: Patrick Asmus
# Web: https://www.cleveradmin.de # Web: https://www.cleveradmin.de
# Repository: https://git.techniverse.net/scriptos/minecraft-ntfy-notify # Repository: https://git.techniverse.net/scriptos/minecraft-ntfy-notify
# Version: 1.0 # Version: 1.1
# Datum: 18.09.2025 # Datum: 18.09.2025
# Modifikation: Initial # Modifikation: ANSI/MC-Farbcodes strippen, robustes Schreiben der Playerliste
##################################################### #####################################################
set -euo pipefail set -euo pipefail
@ -42,9 +42,13 @@ STATE_DIR="${STATE_DIR:-${SCRIPT_DIR}/state}"
STATE_PLAYERS="${STATE_DIR}/players.prev" STATE_PLAYERS="${STATE_DIR}/players.prev"
STATE_UP="${STATE_DIR}/server_up.prev" STATE_UP="${STATE_DIR}/server_up.prev"
# Optional Debug
DEBUG="${DEBUG:-false}"
# ===== Helpers ===== # ===== Helpers =====
die() { echo "ERROR: $*" >&2; exit 1; } die() { echo "ERROR: $*" >&2; exit 1; }
need_bin() { command -v "$1" >/dev/null 2>&1 || die "Benötigtes Tool fehlt: $1"; } need_bin() { command -v "$1" >/dev/null 2>&1 || die "Benötigtes Tool fehlt: $1"; }
dbg() { [[ "$DEBUG" == "true" ]] && echo "DBG: $*" >&2 || true; }
ntfy_notify() { ntfy_notify() {
local title="$1" body="$2" tags="$3" priority="$4" local title="$1" body="$2" tags="$3" priority="$4"
@ -63,19 +67,46 @@ ensure_state() {
[[ -f "$STATE_UP" ]] || echo "unknown" >"$STATE_UP" [[ -f "$STATE_UP" ]] || echo "unknown" >"$STATE_UP"
} }
# Strippt ANSI-Escape-Sequenzen, MC-Farbcodes (§x), CR und Steuerzeichen
sanitize() {
# stdin -> stdout
sed -E $'s/\x1B\\[[0-9;]*[A-Za-z]//g' \
| sed -E 's/§[0-9A-FK-ORa-fk-or]//g' \
| tr -d '\r' \
| sed -E 's/[\x00-\x1F\x7F]//g'
}
get_players() { get_players() {
local out local out
if ! out=$(mcrcon -H "$MC_HOST" -P "$RCON_PORT" -p "$RCON_PASSWORD" "list" 2>/dev/null); then if ! out=$(mcrcon -H "$MC_HOST" -P "$RCON_PORT" -p "$RCON_PASSWORD" "list" 2>/dev/null); then
return 1 return 1
fi fi
if grep -q "players online:" <<<"$out"; then dbg "RAW: $out"
local names="${out#*:}"
names="$(echo "$names" | sed 's/^ *//;s/ *$//')" # Normalisieren & Farbcodes raus
if [[ -n "$names" ]]; then out="$(printf '%s' "$out" | sanitize)"
tr ',' '\n' <<<"$names" | sed 's/^ *//;s/ *$//' | sed '/^$/d' | sort -u
return 0 # Erwartete Muster:
fi # "There are 0 of a max of 20 players online"
# "There are 2 of a max of 20 players online: Alice, Bob"
if ! grep -q "players online:" <<<"$out"; then
# Kein Spieler gelistet, aber kein Fehler
return 0
fi fi
local names="${out#*:}"
names="$(echo "$names" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
if [[ -z "$names" ]]; then
return 0
fi
# Namen splitten, trimmen, doppelte raus, sortiert liefern
printf '%s\n' "$names" \
| tr ',' '\n' \
| sed 's/^[[:space:]]*//;s/[[:space:]]*$//' \
| sed '/^$/d' \
| LC_ALL=C sort -u
return 0 return 0
} }
@ -90,9 +121,8 @@ prev_up="$(cat "$STATE_UP")"
while :; do while :; do
tmp_players="$(mktemp)" tmp_players="$(mktemp)"
if players=$(get_players); then if get_players >"$tmp_players"; then
server_up="true" server_up="true"
printf "%s\n" $players 2>/dev/null | sed '/^$/d' | sort -u >"$tmp_players"
else else
server_up="false" server_up="false"
: >"$tmp_players" : >"$tmp_players"
@ -111,7 +141,8 @@ while :; do
fi fi
if [[ "$server_up" == "true" ]]; then if [[ "$server_up" == "true" ]]; then
sort -u "$STATE_PLAYERS" -o "$STATE_PLAYERS" LC_ALL=C sort -u "$STATE_PLAYERS" -o "$STATE_PLAYERS"
joined="$(comm -13 "$STATE_PLAYERS" "$tmp_players" || true)" joined="$(comm -13 "$STATE_PLAYERS" "$tmp_players" || true)"
left="$(comm -23 "$STATE_PLAYERS" "$tmp_players" || true)" left="$(comm -23 "$STATE_PLAYERS" "$tmp_players" || true)"
@ -140,4 +171,4 @@ while :; do
echo -n "$server_up" >"$STATE_UP" echo -n "$server_up" >"$STATE_UP"
prev_up="$server_up" prev_up="$server_up"
sleep "$POLL_SECONDS" sleep "$POLL_SECONDS"
done done