Skip to content

Handling encrypted rooms #2

@schrmh

Description

@schrmh

I wonder if anybody got an idea regarding a lightweight way of handling encrypted rooms so that messages from other users can be parsed.
I know about pantalaimon but it doesn't seem that trivial to set that up + I would like some solution using shell utilities or some platform independent C code (if really necessary)...

Current state of my room history viewing shell script:

#!/bin/sh

MATRIX_TOKEN=""
ROOM_ID=""
MATRIX_SERVER=""
MATRIX_API_BASE_URL="https://$MATRIX_SERVER/_matrix/client/r0"

last_event_id=""
last_date=""

# Function to convert Unix timestamp to human-readable format
convert_timestamp() {
    timestamp="$1"
    formatted_time=$(date -d "@$timestamp" "+%T")
    echo "$formatted_time"
}

# Function to display date line
display_date_line() {
    date_to_display="$1"
    echo "=== $date_to_display ==="
}

# Function to fetch and display messages from the Matrix room
get_and_display_messages() {
    while true; do
        # Fetch messages using Matrix API with a since token
        response=$(curl -s -H "Authorization: Bearer $MATRIX_TOKEN" "$MATRIX_API_BASE_URL/rooms/$ROOM_ID/messages?from=$last_event_id")
        
        # Check if the response is empty or null
        if [ -z "$response" ] || [ "$response" = "null" ]; then
            sleep 10
            continue
        fi
        
        # Parse the JSON response and extract messages with sender info and timestamp
        messages=$(echo "$response" | jq -r '.chunk[] | select(.type == "m.room.message") | "\(.origin_server_ts) [\(.sender)]: \(.content.body)"')
        
        # Check if the messages variable is empty or null
        if [ -z "$messages" ] || [ "$messages" = "null" ]; then
            sleep 10
            continue
        fi
        
        # Display each new message
        while read -r message; do
            if [ -n "$message" ]; then
                timestamp=$(echo "$message" | awk '{print $1}')
                sender_and_content=$(echo "$message" | cut -d' ' -f2-)
                formatted_time=$(convert_timestamp "$((timestamp / 1000))")
                date_of_message=$(date -d "@$((timestamp / 1000))" "+%Y-%m-%d")
                
                if [ "$date_of_message" != "$last_date" ]; then
                    display_date_line "$date_of_message"
                    last_date="$date_of_message"
                fi
                
                echo "[$formatted_time] $sender_and_content"
            fi
        done <<< "$messages"
        
        # Update the last event ID
        last_event_id=$(echo "$response" | jq -r '.end')
        
        # Wait for a few seconds before fetching messages again
        sleep 10
    done
}

# Start fetching and displaying messages
get_and_display_messages

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions