#!/bin/sh # wcaddmembers - add existing users to an existing room # Usage: ./wcaddmembers [-d db_path] set -eu # Default database path DB="fanr.sqlite" # Parse options while getopts "d:h" opt; do case "$opt" in d) DB="$OPTARG" ;; h) cat < Add existing users to an existing room. Options: -d db_path SQLite database path (default: fanr.sqlite) -h Show this help Arguments: room_id ID of the room to add users to usernames... One or more usernames to add Examples: $0 general alice bob charlie $0 -d /path/to/chat.db 06ETZ4E1RMTJ80AVAAZPHSHWXG alice bob EOF exit 0 ;; *) exit 1 ;; esac done shift $((OPTIND - 1)) # Check arguments if [ $# -lt 2 ]; then echo "Error: room_id and at least one username required" >&2 echo "Usage: $0 [-d db_path] " >&2 exit 1 fi ROOM_ID="$1" shift # Check if database exists if [ ! -f "$DB" ]; then echo "Error: Database '$DB' not found" >&2 exit 1 fi # Check if sqlite3 is available command -v sqlite3 >/dev/null 2>&1 || { echo "Error: sqlite3 not found" >&2 exit 1 } # Verify room exists ROOM_EXISTS=$(sqlite3 "$DB" "SELECT COUNT(*) FROM rooms WHERE id = '$ROOM_ID';") if [ "$ROOM_EXISTS" -eq 0 ]; then echo "Error: Room '$ROOM_ID' not found" >&2 exit 1 fi # Add users ADDED_COUNT=0 for USERNAME in "$@"; do USER_ID=$(sqlite3 "$DB" "SELECT id FROM users WHERE username = '$USERNAME';") if [ -z "$USER_ID" ]; then echo "Warning: User '$USERNAME' not found, skipping." >&2 continue fi # Check if already a member ALREADY_MEMBER=$(sqlite3 "$DB" "SELECT COUNT(*) FROM room_members WHERE room_id = '$ROOM_ID' AND user_id = '$USER_ID';") if [ "$ALREADY_MEMBER" -gt 0 ]; then echo "Warning: User '$USERNAME' is already a member of room '$ROOM_ID', skipping." >&2 continue fi sqlite3 "$DB" <&2 ADDED_COUNT=$((ADDED_COUNT + 1)) done if [ $ADDED_COUNT -eq 0 ]; then echo "Error: No users were added." >&2 exit 1 fi echo "Added $ADDED_COUNT user(s) to room '$ROOM_ID'." >&2