#!/usr/bin/env python3 # ungleich glarus ag, 2025-01-23 import base58 import base64 import json import sys import argparse import requests import pprint class UngleichMatrixClient: def __init__(self, args): self.server = args.server_url self.room_id = args.room_id self.username = args.login_username self.password = args.login_password self.access_token = False self.matrix_url = {} self.matrix_url['login'] = f"{args.server_url}/_matrix/client/v3/login" def ensure_logged_in(self): if not self.access_token: self.login_response = self.login_to_server() self.access_token = self.login_response.json()['access_token'] print(self.access_token) def login_to_server(self): login_data = { 'identifier': { "type": "m.id.user", "user": f"{self.username}" }, 'type': "m.login.password", 'device_id': "ungleich-matrix-client", 'initial_device_display_name' : "ungleich-matrix-client", 'password': f"{self.password}" } r = requests.post(self.matrix_url['login'], json=login_data) if not r.status_code == 200: raise Exception("Login Failed") return r def get_messages(self): self.ensure_logged_in() # Login, get token # matrix_server=https://ungleich.matrix.ungleich.cloud # matrix_key_url=https://${matrix_server}/_matrix/client/v3/room_keys/keys # matrix_user="\@nico:ungleich.ch" # matrix_password=$(pass ldap/nico) # matrix_room_id='!pkPjzozYKUNEbcZKKA:unchat.cat' # matrix_access_token=$(echo $matrix_login | jq .access_token | sed 's/"//g') # Get room_keys # Get messages from room # Decrypt each message: # Retrieve the session key if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("--server-url", required=True, help="Matrix Server URL, i.e. https://your-server ") parser.add_argument("--room-id", required=True, help="ID of the room to get messages from, i.e. !...:your-matrix-domain ") parser.add_argument("--login-username", required=True, help="Username for logging into the server, i.e. @you:your-matrix-domain ") parser.add_argument("--login-password", required=True, help="Password for logging into the server, i.e. your-very-safe-password!! ") args = parser.parse_args() client = UngleichMatrixClient(args) client.get_messages()