diff --git a/nextcloud_talk_manager.sh b/nextcloud_talk_manager.sh index a74c885..d7fc78e 100755 --- a/nextcloud_talk_manager.sh +++ b/nextcloud_talk_manager.sh @@ -1,9 +1,9 @@ #!/bin/bash -# Nextcloud Talk Chat-Verwaltungsscript (Nextcloud 34 Final Edition) +# Nextcloud Talk Chat-Verwaltungsscript (Nextcloud 34 - FINAL FIXED) # -# Kompatibel mit Nextcloud 34.x (Talk-App als "spreed") -# Alle Befehle angepasst an die neue Talk-App CLI +# Kompatibel mit Nextcloud 34.x +# Alle Talk-Kommandos angepasst an die neue CLI # # FÜHRUNGSHINWEISE: # 1. Führe dieses Script AUF DEM NEXTcloud-SERVER aus @@ -14,42 +14,29 @@ # cd /pfad/zu/nextcloud # sudo ./nextcloud_talk_manager.sh -# Farben für bessere Lesbarkeit +# Farben RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' -NC='\033[0m' # No Color +NC='\033[0m' -# Prüfe ob wir uns bereits im Nextcloud-Verzeichnis befinden +# ============================================================================ +# INITIALISIERUNG +# ============================================================================ + +# Nextcloud-Verzeichnis finden if [ -f "./occ" ] && [ -f "./config/config.php" ]; then NEXTCLOUD_DIR="." echo -e "${GREEN}Nextcloud-Verzeichnis: aktuelles Verzeichnis (.)${NC}" else find_nextcloud_dir() { - local possible_paths=( - "/var/www/nextcloud" - "/var/www/html/nextcloud" - "/usr/share/nextcloud" - "/srv/nextcloud" - "/opt/nextcloud" - ) - for path in "${possible_paths[@]}"; do - if [ -f "$path/occ" ] && [ -f "$path/config/config.php" ]; then - echo "$path" - return 0 - fi + for path in /var/www/nextcloud /var/www/html/nextcloud /usr/share/nextcloud /srv/nextcloud /opt/nextcloud; do + [ -f "$path/occ" ] && [ -f "$path/config/config.php" ] && echo "$path" && return 0 done - 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 + find / -name "occ" -type f 2>/dev/null | while read occ; do + [ -f "$(dirname "$occ")/config/config.php" ] && echo "$(dirname "$occ")" && return 0 + done | head -1 return 1 } if find_nextcloud_dir > /dev/null 2>&1; then @@ -57,21 +44,16 @@ else echo -e "${GREEN}Nextcloud-Verzeichnis: $NEXTCLOUD_DIR${NC}" else read -p "Nextcloud-Verzeichnis: " NEXTCLOUD_DIR - if [ ! -f "$NEXTCLOUD_DIR/occ" ]; then - echo -e "${RED}Fehler: 'occ' nicht in $NEXTCLOUD_DIR gefunden!${NC}" - exit 1 - fi + [ ! -f "$NEXTCLOUD_DIR/occ" ] && echo -e "${RED}Fehler: occ nicht gefunden!${NC}" && exit 1 fi fi -# Pfad zu PHP finden +# PHP finden PHP_PATH="$(which php 2>/dev/null)" -if [ -z "$PHP_PATH" ]; then - for php_bin in /usr/bin/php /usr/bin/php8.* /usr/bin/php7.* /usr/local/bin/php; do - [ -x "$php_bin" ] && PHP_PATH="$php_bin" && break - done - [ -z "$PHP_PATH" ] && echo -e "${RED}Fehler: PHP nicht gefunden!${NC}" && exit 1 -fi +for php_bin in /usr/bin/php /usr/bin/php8.* /usr/bin/php7.* /usr/local/bin/php; do + [ -x "$php_bin" ] && PHP_PATH="$php_bin" && break +done +[ -z "$PHP_PATH" ] && echo -e "${RED}Fehler: PHP nicht gefunden!${NC}" && exit 1 # Webserver-Benutzer WEB_USER="www-data" @@ -80,32 +62,18 @@ id nginx &>/dev/null && WEB_USER="nginx" # OCC-Befehl NC_OCC="sudo -u $WEB_USER $PHP_PATH $NEXTCLOUD_DIR/occ" +!$NC_OCC --version > /dev/null 2>&1 && NC_OCC="$PHP_PATH $NEXTCLOUD_DIR/occ" +!$NC_OCC --version > /dev/null 2>&1 && echo -e "${RED}Fehler: OCC funktioniert nicht!${NC}" && exit 1 -# Test OCC -if ! $NC_OCC --version > /dev/null 2>&1; then - NC_OCC="$PHP_PATH $NEXTCLOUD_DIR/occ" - if ! $NC_OCC --version > /dev/null 2>&1; then - echo -e "${RED}Fehler: OCC funktioniert nicht!${NC}" && exit 1 - fi -fi - -# Prüfe Talk-App +# Talk-App prüfen TALK_APP=$($NC_OCC app:list 2>/dev/null | grep -iE "spreed|talk" | head -1) -if [ -z "$TALK_APP" ]; then - echo -e "${RED}Talk-App nicht installiert!${NC}" - read -p "Installieren? (j/n) [n]: " install - [ "$install" = "j" ] && $NC_OCC app:install spreed && $NC_OCC app:enable spreed -fi +[ -z "$TALK_APP" ] && echo -e "${RED}Talk-App nicht installiert!${NC}" && read -p "Installieren? (j/n): " && [ "$REPLY" = "j" ] && $NC_OCC app:install spreed && $NC_OCC app:enable spreed -# Prüfe Upgrade -if $NC_OCC status 2>/dev/null | grep -qi "require upgrade"; then - echo -e "${RED}Upgrade erforderlich!${NC}" - read -p "Upgrade jetzt? (j/n) [j]: " do_upgrade - [ "$do_upgrade" = "j" ] || [ -z "$do_upgrade" ] && $NC_OCC upgrade -fi +# Upgrade prüfen +$NC_OCC status 2>/dev/null | grep -qi "require upgrade" && read -p "Upgrade durchführen? (j/n) [j]: " && [ "$REPLY" = "j" ] || [ -z "$REPLY" ] && $NC_OCC upgrade # ============================================================================ -# KERN FUNKTIONEN +# KERN FUNKTIONEN (NUR NEXTCLOUD 34 KOMPATIBEL) # ============================================================================ # Raum validieren - NUTZT talk:monitor:room @@ -117,18 +85,13 @@ validate_room() { result=$($NC_OCC talk:monitor:room "$room_token" 2>&1) exit_code=$? - # Prüfe auf Fehlerwörter in der Ausgabe - if echo "$result" | grep -qi "does not exist\|not found\|invalid\|unknown\|error"; then - return 1 - fi - - # Prüfe auf erfolgreiche Ausgabe (enthält typische talk:monitor:room Ausgabe) + # Wenn Ausgabe typische talk:monitor:room Inhalte hat, existiert der Raum if echo "$result" | grep -qi "conversation\|attendees\|sessions\|has \|with "; then return 0 fi - # Wenn exit_code 0 ist und keine Fehlerwörter, dann existiert der Raum - if [ $exit_code -eq 0 ]; then + # Wenn keine Fehlerwörter und exit_code 0, dann existiert + if ! echo "$result" | grep -qi "does not exist\|not found\|invalid\|unknown\|error" && [ $exit_code -eq 0 ]; then return 0 fi @@ -145,60 +108,8 @@ get_user_id() { $NC_OCC user:list --output=json 2>/dev/null | grep -A2 "\"$1\"" | grep '"id"' | head -1 | cut -d'"' -f4 } -# Raum auflisten (mehrere Methoden) -list_rooms() { - # Methode 1: Versuche API v4 - local api_result - api_result=$(curl -s -H "OCS-APIRequest: true" -u admin:admin "http://localhost/ocs/v2.php/apps/spreed/api/v4/room" 2>/dev/null) - - if echo "$api_result" | grep -q ''; then - echo "$api_result" | grep -oP '\K[^<]+' - return 0 - fi - - # Methode 2: Datenbank (wenn config lesbar) - local db_type=$(sudo -u www-data $PHP_PATH $NEXTCLOUD_DIR/occ config:system:get dbtype 2>/dev/null) - local db_name=$(sudo -u www-data $PHP_PATH $NEXTCLOUD_DIR/occ config:system:get dbname 2>/dev/null) - local db_user=$(sudo -u www-data $PHP_PATH $NEXTCLOUD_DIR/occ config:system:get dbuser 2>/dev/null) - local db_host=$(sudo -u www-data $PHP_PATH $NEXTCLOUD_DIR/occ config:system:get dbhost 2>/dev/null) - - if [ -n "$db_name" ]; then - # Versuche MySQL - if [ "$db_type" = "mysql" ] || [ "$db_type" = "mariadb" ]; then - local db_pass=$(sudo -u www-data $PHP_PATH $NEXTCLOUD_DIR/occ config:system:get dbpassword 2>/dev/null) - local result=$(mysql -h "$db_host" -u "$db_user" -p"$db_pass" -N -e "SELECT token FROM oc_talk_rooms" "$db_name" 2>/dev/null) - [ -n "$result" ] && echo "$result" && return 0 - fi - - # Versuche PostgreSQL - if [ "$db_type" = "pgsql" ]; then - local db_pass=$(sudo -u www-data $PHP_PATH $NEXTCLOUD_DIR/occ config:system:get dbpassword 2>/dev/null) - local result=$(PGPASSWORD="$db_pass" psql -h "$db_host" -U "$db_user" -d "$db_name" -t -c "SELECT token FROM oc_talk_rooms" 2>/dev/null) - [ -n "$result" ] && echo "$result" | sed 's/^ *//' | sed 's/ *$//' && return 0 - fi - - # Versuche SQLite - if [ "$db_type" = "sqlite" ]; then - local db_file="${NEXTCLOUD_DIR}/data/nextcloud.db" - [ -f "$db_file" ] && sqlite3 "$db_file" "SELECT token FROM oc_talk_rooms" 2>/dev/null && return 0 - fi - fi - - # Methode 3: Gib Anleitung - echo "" - echo -e "${YELLOW}Manuelle Methoden zum Auflisten von Chats:${NC}" - echo "" - echo "1. API (empfohlen):" - echo " curl -H 'OCS-APIRequest: true' -u admin:pass 'http://localhost/ocs/v2.php/apps/spreed/api/v4/room'" - echo "" - echo "2. Datenbank:" - echo " mysql -u dbuser -p -e 'SELECT token, name FROM oc_talk_rooms' dbname" - echo "" - return 1 -} - # ============================================================================ -# MENÜ & FUNKTIONEN +# MENÜ # ============================================================================ show_menu() { @@ -214,8 +125,7 @@ show_menu() { echo -e "${YELLOW}5.${NC} Moderatorenrechte entziehen" echo -e "${YELLOW}6.${NC} Chat sperren (read-only)" 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}8.${NC} Chat-Info anzeigen" echo -e "${YELLOW}d.${NC} Debug-Informationen" echo -e "${YELLOW}0.${NC} Beenden" echo "" @@ -226,10 +136,9 @@ show_debug() { echo -e "${BLUE}=== Debug-Informationen ===${NC}\n" echo "Nextcloud: $NEXTCLOUD_DIR" echo "PHP: $PHP_PATH" - echo "User: $WEB_USER" echo "OCC: $NC_OCC" echo "" - echo -e "${YELLOW}Talk-App Status:${NC}" + echo -e "${YELLOW}Talk-App:${NC}" $NC_OCC app:list | grep -iE "spreed|talk" echo "" echo -e "${YELLOW}Teste Raum-Validierung (iba4cezy):${NC}" @@ -237,16 +146,17 @@ show_debug() { echo "✓ Chat existiert" else echo "✗ Chat existiert NICHT" - echo "Teste manuell: $NC_OCC talk:monitor:room iba4cezy" + echo "Manueller Test:" $NC_OCC talk:monitor:room iba4cezy fi echo "" - echo -e "${YELLOW}Verfügbare Talk-Kommandos:${NC}" - $NC_OCC talk: 2>&1 | grep -E ":" | head -20 - echo "" - read -p "Drücken Sie Enter zum Fortfahren..." + read -p "Drücken Sie Enter..." } +# ============================================================================ +# CHAT FUNKTIONEN (ALLE ANGEPASST FÜR NEXTCLOUD 34) +# ============================================================================ + create_chat() { show_menu echo -e "\n${YELLOW}Chat anlegen${NC}\n" @@ -261,28 +171,21 @@ create_chat() { echo -e "\n${BLUE}Befehl: $NC_OCC talk:room:create \"$chat_name\" --type=$chat_type${NC}" local result=$($NC_OCC talk:room:create "$chat_name" --type="$chat_type" 2>&1) - local exit_code=$? - echo "Exit-Code: $exit_code" + echo "Exit-Code: $?" echo "Output: $result" - if [ $exit_code -ne 0 ]; then - echo -e "${RED}Fehler beim Anlegen!${NC}" - read -p "Enter..." && return - fi - - # Token extrahieren - Nextcloud 34 gibt den Token in der Ausgabe zurück + # Token extrahieren local room_token=$(echo "$result" | grep -oE '[a-zA-Z0-9]{8,}') if [ -z "$room_token" ]; then - echo -e "${RED}Token nicht gefunden. Bitte manuell aus Ausgabe entnehmen.${NC}" - echo "Rohausgabe: $result" + echo -e "${RED}Token nicht extrahiert. Bitte manuell aus Ausgabe entnehmen.${NC}" read -p "Enter..." && return fi echo -e "${GREEN}Chat Token: $room_token${NC}" # Nutzer hinzufügen - if [ -n "$room_token" ] && [ -n "$users_to_add" ]; then + if [ -n "$users_to_add" ]; then IFS=',' read -ra users <<< "$users_to_add" for user in "${users[@]}"; do user=$(echo "$user" | tr -d ' ') @@ -300,7 +203,6 @@ create_chat() { done fi - echo -e "${GREEN}Chat angelegt! Token: $room_token${NC}" read -p "Enter..." } @@ -319,33 +221,18 @@ add_user_to_chat() { read -p "Enter..." && return fi - # Versuche Raum zu validieren if ! validate_room "$room_token"; then - # Falls validate_room scheitert, teste direkt mit talk:monitor:room - echo -e "${YELLOW}Teste Raum manuell...${NC}" - local test_result=$($NC_OCC talk:monitor:room "$room_token" 2>&1) - echo "Test-Ergebnis: $test_result" - if echo "$test_result" | grep -qi "does not exist\|not found\|invalid"; then - echo -e "${RED}Chat mit Token '$room_token' existiert nicht!${NC}" - read -p "Enter..." && return - fi - fi - - local user_id=$(get_user_id "$username") - if [ -z "$user_id" ]; then - echo -e "${RED}User-ID für '$username' nicht gefunden!${NC}" + echo -e "${RED}Chat mit Token '$room_token' existiert nicht!${NC}" + echo "Manueller Test: $NC_OCC talk:monitor:room $room_token" + $NC_OCC talk:monitor:room "$room_token" 2>&1 read -p "Enter..." && return fi - echo -e "\n${BLUE}Befehl: $NC_OCC talk:room:add $room_token $user_id${NC}" - local result=$($NC_OCC talk:room:add "$room_token" "$user_id" 2>&1) - echo "Output: $result" + local user_id=$(get_user_id "$username") + [ -z "$user_id" ] && echo -e "${RED}User-ID nicht gefunden!${NC}" && read -p "Enter..." && return - if [ $? -eq 0 ]; then - echo -e "${GREEN}Nutzer $username hinzugefügt!${NC}" - else - echo -e "${RED}Fehler: $result${NC}" - fi + echo -e "\n${BLUE}Befehl: $NC_OCC talk:room:add $room_token $user_id${NC}" + $NC_OCC talk:room:add "$room_token" "$user_id" 2>&1 read -p "Enter..." } @@ -367,26 +254,14 @@ remove_user_from_chat() { if ! validate_room "$room_token"; then echo -e "${RED}Chat mit Token '$room_token' existiert nicht!${NC}" - echo "Teste manuell: $NC_OCC talk:monitor:room $room_token" - $NC_OCC talk:monitor:room "$room_token" 2>&1 read -p "Enter..." && return fi local user_id=$(get_user_id "$username") - if [ -z "$user_id" ]; then - echo -e "${RED}User-ID für '$username' nicht gefunden!${NC}" - read -p "Enter..." && return - fi + [ -z "$user_id" ] && echo -e "${RED}User-ID nicht gefunden!${NC}" && read -p "Enter..." && return echo -e "\n${BLUE}Befehl: $NC_OCC talk:room:remove $room_token $user_id${NC}" - local result=$($NC_OCC talk:room:remove "$room_token" "$user_id" 2>&1) - echo "Output: $result" - - if [ $? -eq 0 ]; then - echo -e "${GREEN}Nutzer $username entfernt!${NC}" - else - echo -e "${RED}Fehler: $result${NC}" - fi + $NC_OCC talk:room:remove "$room_token" "$user_id" 2>&1 read -p "Enter..." } @@ -408,26 +283,14 @@ make_moderator() { if ! validate_room "$room_token"; then echo -e "${RED}Chat mit Token '$room_token' existiert nicht!${NC}" - echo "Teste manuell: $NC_OCC talk:monitor:room $room_token" - $NC_OCC talk:monitor:room "$room_token" 2>&1 read -p "Enter..." && return fi local user_id=$(get_user_id "$username") - if [ -z "$user_id" ]; then - echo -e "${RED}User-ID für '$username' nicht gefunden!${NC}" - read -p "Enter..." && return - fi + [ -z "$user_id" ] && echo -e "${RED}User-ID nicht gefunden!${NC}" && read -p "Enter..." && return echo -e "\n${BLUE}Befehl: $NC_OCC talk:room:promote $room_token $user_id${NC}" - local result=$($NC_OCC talk:room:promote "$room_token" "$user_id" 2>&1) - echo "Output: $result" - - if [ $? -eq 0 ]; then - echo -e "${GREEN}$username ist jetzt Moderator!${NC}" - else - echo -e "${RED}Fehler: $result${NC}" - fi + $NC_OCC talk:room:promote "$room_token" "$user_id" 2>&1 read -p "Enter..." } @@ -449,26 +312,14 @@ remove_moderator() { if ! validate_room "$room_token"; then echo -e "${RED}Chat mit Token '$room_token' existiert nicht!${NC}" - echo "Teste manuell: $NC_OCC talk:monitor:room $room_token" - $NC_OCC talk:monitor:room "$room_token" 2>&1 read -p "Enter..." && return fi local user_id=$(get_user_id "$username") - if [ -z "$user_id" ]; then - echo -e "${RED}User-ID für '$username' nicht gefunden!${NC}" - read -p "Enter..." && return - fi + [ -z "$user_id" ] && echo -e "${RED}User-ID nicht gefunden!${NC}" && read -p "Enter..." && return echo -e "\n${BLUE}Befehl: $NC_OCC talk:room:demote $room_token $user_id${NC}" - local result=$($NC_OCC talk:room:demote "$room_token" "$user_id" 2>&1) - echo "Output: $result" - - if [ $? -eq 0 ]; then - echo -e "${GREEN}Moderatorenrechte von $username entfernt!${NC}" - else - echo -e "${RED}Fehler: $result${NC}" - fi + $NC_OCC talk:room:demote "$room_token" "$user_id" 2>&1 read -p "Enter..." } @@ -486,14 +337,7 @@ lock_chat() { fi echo -e "\n${BLUE}Befehl: $NC_OCC talk:room:update $room_token --read-only=1${NC}" - local result=$($NC_OCC talk:room:update "$room_token" --read-only=1 2>&1) - echo "Output: $result" - - if [ $? -eq 0 ]; then - echo -e "${GREEN}Chat gesperrt!${NC}" - else - echo -e "${RED}Fehler: $result${NC}" - fi + $NC_OCC talk:room:update "$room_token" --read-only=1 2>&1 read -p "Enter..." } @@ -511,35 +355,7 @@ unlock_chat() { fi echo -e "\n${BLUE}Befehl: $NC_OCC talk:room:update $room_token --read-only=0${NC}" - local result=$($NC_OCC talk:room:update "$room_token" --read-only=0 2>&1) - echo "Output: $result" - - if [ $? -eq 0 ]; then - echo -e "${GREEN}Chat entsperrt!${NC}" - else - echo -e "${RED}Fehler: $result${NC}" - fi - - read -p "Enter..." -} - -list_chats() { - show_menu - echo -e "\n${YELLOW}Alle Chats auflisten${NC}\n" - - echo -e "${BLUE}Versuche Chats aufzulisten...${NC}" - local rooms - rooms=$(list_rooms 2>&1) - - if [ -n "$rooms" ]; then - echo -e "\n${GREEN}Gefundene Chats:${NC}" - echo "$rooms" - else - echo -e "${YELLOW}Keine Chats gefunden oder Methode fehlgeschlagen.${NC}" - echo "Verfügbare Methoden:" - echo " 1. API: curl -H 'OCS-APIRequest: true' -u admin:pass 'http://localhost/ocs/v2.php/apps/spreed/api/v4/room'" - echo " 2. Datenbank: SELECT token, name FROM oc_talk_rooms" - fi + $NC_OCC talk:room:update "$room_token" --read-only=0 2>&1 read -p "Enter..." } @@ -552,12 +368,7 @@ show_chat_info() { [ -z "$room_token" ] && echo -e "${RED}Fehler: Token leer!${NC}" && read -p "Enter..." && return echo -e "\n${BLUE}Befehl: $NC_OCC talk:monitor:room $room_token${NC}" - local result=$($NC_OCC talk:monitor:room "$room_token" 2>&1) - local exit_code=$? - - echo "Exit-Code: $exit_code" - echo "" - echo "$result" + $NC_OCC talk:monitor:room "$room_token" 2>&1 read -p "Enter..." } @@ -577,8 +388,7 @@ while true; do 5) remove_moderator ;; 6) lock_chat ;; 7) unlock_chat ;; - 8) list_chats ;; - 9) show_chat_info ;; + 8) show_chat_info ;; d) show_debug ;; 0) echo -e "\n${GREEN}Auf Wiedersehen!${NC}" && exit 0 ;; *) echo -e "${RED}Ungültige Auswahl!${NC}" ;;