From c6028d9b77d40ca4014c147a3dc692596b6a0612 Mon Sep 17 00:00:00 2001 From: wralto-user-1 Date: Fri, 30 Oct 2026 12:57:07 +0100 Subject: [PATCH] Fix: Improved version with automatic Nextcloud detection and debug output - Auto-detect Nextcloud directory path - Auto-detect PHP path - Show exact OCC commands being executed - Display exit codes and output for debugging - Added debug menu option (d) - Better error handling and user feedback Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- nextcloud_talk_manager.sh | 324 ++++++++++++++++++++++++++++++++++---- 1 file changed, 296 insertions(+), 28 deletions(-) diff --git a/nextcloud_talk_manager.sh b/nextcloud_talk_manager.sh index befd508..fa09cd6 100755 --- a/nextcloud_talk_manager.sh +++ b/nextcloud_talk_manager.sh @@ -1,10 +1,18 @@ #!/bin/bash -# Nextcloud Talk Chat-Verwaltungsscript -# Führe als Nextcloud-Webserver-Benutzer aus: sudo -u www-data ./nextcloud_talk_manager.sh -# Oder direkt auf dem Nextcloud-Server - -NC_OCC="sudo -u www-data php occ" +# Nextcloud Talk Chat-Verwaltungsscript (Verbesserte Version) +# +# FÜHRUNGSHINWEISE: +# 1. Führe dieses Script AUF DEM NEXTcloud-SERVER aus +# 2. Als Benutzer mit sudo-Rechten ausführen +# 3. Das Script sucht automatisch nach dem Nextcloud-Verzeichnis +# +# Beispiel: +# sudo ./nextcloud_talk_manager.sh +# ODER +# ./nextcloud_talk_manager.sh +# +# Falls Nextcloud nicht automatisch gefunden wird, gib den Pfad manuell ein # Farben für bessere Lesbarkeit RED='\033[0;31m' @@ -13,12 +21,159 @@ YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color +# Funktion zum Finden des Nextcloud-Verzeichnisses +find_nextcloud_dir() { + # Häufige Pfade + local possible_paths=( + "/var/www/nextcloud" + "/var/www/html/nextcloud" + "/var/www/nextcloud/html" + "/usr/share/nextcloud" + "/usr/share/webapps/nextcloud" + "/srv/nextcloud" + "/opt/nextcloud" + "/home/nextcloud" + "/home/www/nextcloud" + "/home/www-data/nextcloud" + "/var/lib/nextcloud" + ) + + for path in "${possible_paths[@]}"; do + if [ -f "$path/occ" ] && [ -f "$path/config/config.php" ]; then + echo "$path" + return 0 + fi + done + + # Suche nach occ-Datei + local occ_path=$(find / -name "occ" -type f 2>/dev/null | while read occ; do + if [ -f "$(dirname "$occ")/config/config.php" ]; then + echo "$(dirname "$occ")" + return 0 + fi + done | head -1) + + if [ -n "$occ_path" ]; then + echo "$occ_path" + return 0 + fi + + return 1 +} + +# Nextcloud-Verzeichnis finden +NEXTCLOUD_DIR="" + +# Erstversuch: automatische Suche +if find_nextcloud_dir > /dev/null 2>&1; then + NEXTCLOUD_DIR=$(find_nextcloud_dir) + echo -e "${GREEN}Nextcloud-Verzeichnis gefunden: $NEXTCLOUD_DIR${NC}" +else + # Manuelle Eingabe + echo -e "${YELLOW}Nextcloud-Verzeichnis nicht automatisch gefunden.${NC}" + echo "Bitte geben Sie den vollständigen Pfad zu Ihrem Nextcloud-Verzeichnis ein" + echo "(z. B. /var/www/nextcloud oder /var/www/html/nextcloud)" + read -p "Nextcloud-Verzeichnis: " NEXTCLOUD_DIR + + # Prüfen ob occ existiert + if [ ! -f "$NEXTCLOUD_DIR/occ" ]; then + echo -e "${RED}Fehler: 'occ' nicht in $NEXTCLOUD_DIR gefunden!${NC}" + echo "Bitte überprüfen Sie den Pfad." + exit 1 + fi +fi + +# Pfad zu PHP finden +PHP_PATH="$(which php 2>/dev/null)" +if [ -z "$PHP_PATH" ]; then + # Versuche häufige PHP-Pfade + for php_bin in /usr/bin/php /usr/bin/php8.* /usr/bin/php7.* /usr/local/bin/php /usr/sbin/php; do + if [ -x "$php_bin" ]; then + PHP_PATH="$php_bin" + break + fi + done + + if [ -z "$PHP_PATH" ]; then + echo -e "${RED}Fehler: PHP nicht gefunden!${NC}" + echo "Bitte installieren Sie PHP oder geben Sie den Pfad manuell ein." + exit 1 + fi +fi + +echo -e "${GREEN}PHP gefunden: $PHP_PATH${NC}" + +# Webserver-Benutzer ermitteln +WEB_USER="www-data" +if id apache &>/dev/null; then + WEB_USER="apache" +elif id nginx &>/dev/null; then + WEB_USER="nginx" +elif id www-data &>/dev/null; then + WEB_USER="www-data" +fi + +echo -e "${GREEN}Webserver-Benutzer: $WEB_USER${NC}" + +# OCC-Befehl definieren +NC_OCC="sudo -u $WEB_USER $PHP_PATH $NEXTCLOUD_DIR/occ" + +echo -e "${BLUE}============================================${NC}" +echo "OCC-Befehl: $NC_OCC" +echo -e "${BLUE}============================================${NC}" + +# Test ob OCC funktioniert +echo -e "\n${YELLOW}Teste Nextcloud OCC...${NC}" +if $NC_OCC --version > /dev/null 2>&1; then + echo -e "${GREEN}Nextcloud OCC funktioniert!${NC}" +else + echo -e "${RED}Nextcloud OCC funktioniert nicht mit sudo!${NC}" + echo "Versuche ohne sudo..." + NC_OCC="$PHP_PATH $NEXTCLOUD_DIR/occ" + if $NC_OCC --version > /dev/null 2>&1; then + echo -e "${GREEN}Nextcloud OCC funktioniert ohne sudo!${NC}" + echo -e "${YELLOW}Hinweis: Führe das Script in Zukunft als Webserver-Benutzer aus: sudo -u $WEB_USER $0${NC}" + USE_SUDO=false + else + echo -e "${RED}Fehler: Nextcloud OCC funktioniert nicht!${NC}" + echo "Mögliche Ursachen:" + echo " - Falsches Nextcloud-Verzeichnis" + echo " - PHP nicht korrekt installiert" + echo " - Nextcloud-Konfiguration fehlerhaft" + echo " - Berechtigungsprobleme" + echo "" + echo "Debug-Info:" + echo " Nextcloud-Dir: $NEXTCLOUD_DIR" + echo " occ existiert: $(test -f "$NEXTCLOUD_DIR/occ" && echo "JA" || echo "NEIN")" + echo " config.php existiert: $(test -f "$NEXTCLOUD_DIR/config/config.php" && echo "JA" || echo "NEIN")" + exit 1 + fi +fi + +# Prüfe ob Talk-App installiert ist +if ! $NC_OCC app:list 2>/dev/null | grep -qi "\btalk\b"; then + echo -e "\n${RED}Warnung: Talk-App ist nicht installiert oder aktiviert!${NC}" + read -p "Talk-App installieren und aktivieren? (j/n) [n]: " install_talk + if [[ "$install_talk" =~ ^[Jj]$ ]]; then + echo -e "${BLUE}Installiere Talk-App...${NC}" + $NC_OCC app:install talk + $NC_OCC app:enable talk + echo -e "${GREEN}Talk-App installiert und aktiviert!${NC}" + else + echo -e "${RED}Ohne Talk-App können keine Chat-Operationen durchgeführt werden.${NC}" + exit 1 + fi +fi + # Funktion zum Anzeigen des Menüs show_menu() { clear echo -e "${BLUE}============================================${NC}" echo -e "${BLUE} Nextcloud Talk Chat-Verwaltung${NC}" echo -e "${BLUE}============================================${NC}" + echo -e "${BLUE}Nextcloud: $NEXTCLOUD_DIR${NC}" + echo -e "${BLUE}PHP: $PHP_PATH${NC}" + echo -e "${BLUE}Benutzer: $WEB_USER${NC}" echo "" echo -e "${YELLOW}1.${NC} Chat anlegen" echo -e "${YELLOW}2.${NC} Nutzer zum Chat hinzufügen" @@ -29,14 +184,38 @@ show_menu() { echo -e "${YELLOW}7.${NC} Chat entsperren" echo -e "${YELLOW}8.${NC} Alle Chats auflisten" echo -e "${YELLOW}9.${NC} Chat-Info anzeigen" + echo -e "${YELLOW}d.${NC} Debug-Informationen anzeigen" echo -e "${YELLOW}0.${NC} Beenden" echo "" } +# Debug-Informationen anzeigen +show_debug() { + echo -e "\n${BLUE}=== Debug-Informationen ===${NC}\n" + echo "Nextcloud-Verzeichnis: $NEXTCLOUD_DIR" + echo "PHP-Pfad: $PHP_PATH" + echo "Webserver-Benutzer: $WEB_USER" + echo "OCC-Befehl: $NC_OCC" + echo "" + echo -e "${YELLOW}Teste Nextcloud-Status:${NC}" + $NC_OCC status + echo "" + echo -e "${YELLOW}Teste Talk-App:${NC}" + $NC_OCC app:list | grep -i talk + echo "" + echo -e "${YELLOW}PHP-Version:${NC}" + $PHP_PATH -v | head -1 + echo "" + echo -e "${YELLOW}Nextcloud-Version:${NC}" + $NC_OCC --version + echo "" + read -p "Drücken Sie Enter zum Fortfahren..." +} + # Funktion zum Validieren des Benutzers validate_user() { local username="$1" - if $NC_OCC user:list | grep -qw "$username"; then + if $NC_OCC user:list 2>/dev/null | grep -qw "$username"; then return 0 else return 1 @@ -56,7 +235,8 @@ validate_room() { # Funktion zum Holen der User-ID get_user_id() { local username="$1" - $NC_OCC user:list --output=json 2>/dev/null | grep -A1 "\"$username\"" | grep "id" | head -1 | cut -d'"' -f4 + local result=$($NC_OCC user:list --output=json 2>/dev/null) + echo "$result" | grep -A2 "\"$username\"" | grep '"id"' | head -1 | cut -d'"' -f4 } # 1. Chat anlegen @@ -72,13 +252,17 @@ create_chat() { read -p "Typ (public/private/group/federated): [public] " chat_type chat_type=${chat_type:-public} - read -p "Nutzer hinzufügen ( Komma-getrennt, optional): " users_to_add + read -p "Nutzer hinzufügen (Komma-getrennt, optional): " users_to_add echo -e "\n${BLUE}Lege Chat an...${NC}" + echo "Befehl: $NC_OCC talk:chat:create \"$chat_name\" --type=$chat_type" local result=$($NC_OCC talk:chat:create "$chat_name" --type="$chat_type" 2>&1) local exit_code=$? + echo "Exit-Code: $exit_code" + echo "Output: $result" + if [ $exit_code -ne 0 ]; then echo -e "${RED}Fehler beim Anlegen des Chats: $result${NC}" return @@ -87,6 +271,14 @@ create_chat() { # Token extrahieren local room_token=$(echo "$result" | grep -oP 'Token: \K[^\s]+') + if [ -z "$room_token" ]; then + echo -e "${RED}Fehler: Konnte Token nicht aus der Ausgabe extrahieren.${NC}" + echo "Rohausgabe: $result" + return + fi + + echo -e "${GREEN}Chat Token: $room_token${NC}" + if [ -n "$room_token" ] && [ -n "$users_to_add" ]; then IFS=',' read -ra users <<< "$users_to_add" for user in "${users[@]}"; do @@ -94,11 +286,20 @@ create_chat() { if validate_user "$user"; then local user_id=$(get_user_id "$user") if [ -n "$user_id" ]; then - $NC_OCC talk:participant:add "$room_token" "$user_id" 2>/dev/null - echo -e "${GREEN}✓ Nutzer $user hinzugefügt${NC}" + echo "Füge $user hinzu (ID: $user_id)..." + local add_result=$($NC_OCC talk:participant:add "$room_token" "$user_id" 2>&1) + local add_exit=$? + echo " Exit: $add_exit, Output: $add_result" + if [ $add_exit -eq 0 ]; then + echo -e " ${GREEN}Nutzer $user hinzugefügt${NC}" + else + echo -e " ${RED}Fehler: $add_result${NC}" + fi + else + echo -e " ${RED}Konnte User-ID nicht ermitteln${NC}" fi else - echo -e "${RED}✗ Nutzer $user existiert nicht${NC}" + echo -e " ${RED}Nutzer $user existiert nicht${NC}" fi done fi @@ -136,12 +337,22 @@ add_user_to_chat() { local user_id=$(get_user_id "$username") - echo -e "\n${BLUE}Füge Nutzer $username zum Chat hinzu...${NC}" + if [ -z "$user_id" ]; then + echo -e "${RED}Fehler: Konnte User-ID für '$username' nicht ermitteln!${NC}" + return + fi + + echo -e "\n${BLUE}Füge Nutzer $username (ID: $user_id) zum Chat $room_token hinzu...${NC}" + echo "Befehl: $NC_OCC talk:participant:add $room_token $user_id" + local result=$($NC_OCC talk:participant:add "$room_token" "$user_id" 2>&1) local exit_code=$? + echo "Exit-Code: $exit_code" + echo "Output: $result" + if [ $exit_code -eq 0 ]; then - echo -e "${GREEN}✓ Nutzer $username erfolgreich hinzugefügt!${NC}" + echo -e "${GREEN}Nutzer $username erfolgreich hinzugefügt!${NC}" else echo -e "${RED}Fehler: $result${NC}" fi @@ -177,12 +388,22 @@ remove_user_from_chat() { local user_id=$(get_user_id "$username") - echo -e "\n${BLUE}Entferne Nutzer $username aus dem Chat...${NC}" + if [ -z "$user_id" ]; then + echo -e "${RED}Fehler: Konnte User-ID für '$username' nicht ermitteln!${NC}" + return + fi + + echo -e "\n${BLUE}Entferne Nutzer $username (ID: $user_id) aus dem Chat $room_token...${NC}" + echo "Befehl: $NC_OCC talk:participant:remove $room_token $user_id" + local result=$($NC_OCC talk:participant:remove "$room_token" "$user_id" 2>&1) local exit_code=$? + echo "Exit-Code: $exit_code" + echo "Output: $result" + if [ $exit_code -eq 0 ]; then - echo -e "${GREEN}✓ Nutzer $username erfolgreich entfernt!${NC}" + echo -e "${GREEN}Nutzer $username erfolgreich entfernt!${NC}" else echo -e "${RED}Fehler: $result${NC}" fi @@ -218,12 +439,22 @@ make_moderator() { local user_id=$(get_user_id "$username") - echo -e "\n${BLUE}Ernenne $username zum Moderator...${NC}" + if [ -z "$user_id" ]; then + echo -e "${RED}Fehler: Konnte User-ID für '$username' nicht ermitteln!${NC}" + return + fi + + echo -e "\n${BLUE}Ernenne $username (ID: $user_id) zum Moderator im Chat $room_token...${NC}" + echo "Befehl: $NC_OCC talk:moderator:add $room_token $user_id" + local result=$($NC_OCC talk:moderator:add "$room_token" "$user_id" 2>&1) local exit_code=$? + echo "Exit-Code: $exit_code" + echo "Output: $result" + if [ $exit_code -eq 0 ]; then - echo -e "${GREEN}✓ $username ist jetzt Moderator!${NC}" + echo -e "${GREEN}$username ist jetzt Moderator!${NC}" else echo -e "${RED}Fehler: $result${NC}" fi @@ -259,12 +490,22 @@ remove_moderator() { local user_id=$(get_user_id "$username") - echo -e "\n${BLUE}Entziehe $username die Moderatorenrechte...${NC}" + if [ -z "$user_id" ]; then + echo -e "${RED}Fehler: Konnte User-ID für '$username' nicht ermitteln!${NC}" + return + fi + + echo -e "\n${BLUE}Entziehe $username (ID: $user_id) die Moderatorenrechte im Chat $room_token...${NC}" + echo "Befehl: $NC_OCC talk:moderator:remove $room_token $user_id" + local result=$($NC_OCC talk:moderator:remove "$room_token" "$user_id" 2>&1) local exit_code=$? + echo "Exit-Code: $exit_code" + echo "Output: $result" + if [ $exit_code -eq 0 ]; then - echo -e "${GREEN}✓ Moderatorenrechte von $username entfernt!${NC}" + echo -e "${GREEN}Moderatorenrechte von $username entfernt!${NC}" else echo -e "${RED}Fehler: $result${NC}" fi @@ -287,12 +528,17 @@ lock_chat() { return fi - echo -e "\n${BLUE}Sperre Chat...${NC}" + echo -e "\n${BLUE}Sperre Chat $room_token...${NC}" + echo "Befehl: $NC_OCC talk:room:read-only $room_token 1" + local result=$($NC_OCC talk:room:read-only "$room_token" 1 2>&1) local exit_code=$? + echo "Exit-Code: $exit_code" + echo "Output: $result" + if [ $exit_code -eq 0 ]; then - echo -e "${GREEN}✓ Chat erfolgreich gesperrt (read-only)!${NC}" + echo -e "${GREEN}Chat erfolgreich gesperrt (read-only)!${NC}" else echo -e "${RED}Fehler: $result${NC}" fi @@ -315,12 +561,17 @@ unlock_chat() { return fi - echo -e "\n${BLUE}Entsperre Chat...${NC}" + echo -e "\n${BLUE}Entsperre Chat $room_token...${NC}" + echo "Befehl: $NC_OCC talk:room:read-only $room_token 0" + local result=$($NC_OCC talk:room:read-only "$room_token" 0 2>&1) local exit_code=$? + echo "Exit-Code: $exit_code" + echo "Output: $result" + if [ $exit_code -eq 0 ]; then - echo -e "${GREEN}✓ Chat erfolgreich entsperrt!${NC}" + echo -e "${GREEN}Chat erfolgreich entsperrt!${NC}" else echo -e "${RED}Fehler: $result${NC}" fi @@ -332,8 +583,18 @@ unlock_chat() { list_chats() { echo -e "\n${YELLOW}Alle Chats auflisten${NC}\n" - echo -e "${BLUE}Chats:${NC}" - $NC_OCC talk:room:list 2>/dev/null + echo "Befehl: $NC_OCC talk:room:list" + local result=$($NC_OCC talk:room:list 2>&1) + local exit_code=$? + + echo "Exit-Code: $exit_code" + echo "" + + if [ $exit_code -eq 0 ]; then + echo "$result" + else + echo -e "${RED}Fehler: $result${NC}" + fi read -p "\nDrücken Sie Enter zum Fortfahren..." } @@ -354,7 +615,13 @@ show_chat_info() { fi echo -e "\n${BLUE}Chat-Informationen:${NC}" - $NC_OCC talk:room:info "$room_token" 2>/dev/null + echo "Befehl: $NC_OCC talk:room:info $room_token" + local result=$($NC_OCC talk:room:info "$room_token" 2>&1) + local exit_code=$? + + echo "Exit-Code: $exit_code" + echo "" + echo "$result" read -p "\nDrücken Sie Enter zum Fortfahren..." } @@ -362,7 +629,7 @@ show_chat_info() { # Hauptschleife while true; do show_menu - read -p "Wählen Sie eine Option [0-9]: " choice + read -p "Wählen Sie eine Option [0-9,d]: " choice case $choice in 1) create_chat ;; @@ -374,12 +641,13 @@ while true; do 7) unlock_chat ;; 8) list_chats ;; 9) show_chat_info ;; + d) show_debug ;; 0) echo -e "\n${GREEN}Auf Wiedersehen!${NC}" exit 0 ;; *) - echo -e "${RED}Ungültige Auswahl! Bitte wählen Sie 0-9.${NC}" + echo -e "${RED}Ungültige Auswahl! Bitte wählen Sie 0-9 oder d.${NC}" sleep 2 ;; esac