111 lines
3.5 KiB
Python
Executable File
111 lines
3.5 KiB
Python
Executable File
import asyncio
|
|
import getpass
|
|
import json
|
|
import os
|
|
import sys
|
|
import traceback
|
|
|
|
import aiofiles
|
|
|
|
from nio import (
|
|
AsyncClient,
|
|
AsyncClientConfig,
|
|
KeyVerificationCancel,
|
|
KeyVerificationEvent,
|
|
KeyVerificationKey,
|
|
KeyVerificationMac,
|
|
KeyVerificationStart,
|
|
LocalProtocolError,
|
|
LoginResponse,
|
|
ToDeviceError,
|
|
ToDeviceMessage,
|
|
UnknownToDeviceEvent,
|
|
)
|
|
|
|
CONFIG_FILE = "credentials.json"
|
|
STORE_PATH = "./store/" # local directory
|
|
|
|
async def login() -> AsyncClient:
|
|
"""Handle login with or without stored credentials."""
|
|
# Configuration options for the AsyncClient
|
|
client_config = AsyncClientConfig(
|
|
max_limit_exceeded=0,
|
|
max_timeouts=0,
|
|
store_sync_tokens=True,
|
|
encryption_enabled=True,
|
|
)
|
|
|
|
# If there are no previously-saved credentials, we'll use the password
|
|
if not os.path.exists(CONFIG_FILE):
|
|
# print(
|
|
# "First time use. Did not find credential file. Asking for "
|
|
# "homeserver, user, and password to create credential file."
|
|
# )
|
|
# homeserver = "https://matrix.example.org"
|
|
# homeserver = input(f"Enter your homeserver URL: [{homeserver}] ")
|
|
|
|
# if not (homeserver.startswith("https://") or homeserver.startswith("http://")):
|
|
# homeserver = "https://" + homeserver
|
|
|
|
# user_id = "@user:example.org"
|
|
# user_id = input(f"Enter your full user ID: [{user_id}] ")
|
|
|
|
# device_name = "matrix-nio"
|
|
# device_name = input(f"Choose a name for this device: [{device_name}] ")
|
|
homeserver = "https://matrix.domnomnom.com"
|
|
user_id = "@getbot:matrix.domnomnom.com"
|
|
room_id = "!sZpfYzLsRbnIOKJlPH:matrix.domnomnom.com"
|
|
device_name = "whitebox-nio"
|
|
|
|
|
|
if not os.path.exists(STORE_PATH):
|
|
os.makedirs(STORE_PATH)
|
|
|
|
# Initialize the matrix client
|
|
client = AsyncClient(
|
|
homeserver,
|
|
user_id,
|
|
store_path=STORE_PATH,
|
|
config=client_config,
|
|
)
|
|
pw = getpass.getpass()
|
|
|
|
resp = await client.login(password=pw, device_name=device_name)
|
|
|
|
# check that we logged in successfully
|
|
if isinstance(resp, LoginResponse):
|
|
write_details_to_disk(resp, homeserver)
|
|
else:
|
|
print(f'homeserver = "{homeserver}"; user = "{user_id}"')
|
|
print(f"Failed to log in: {resp}")
|
|
sys.exit(1)
|
|
|
|
print(
|
|
"Logged in using a password. Credentials were stored. "
|
|
"On next execution the stored login credentials will be used."
|
|
)
|
|
|
|
# Otherwise the config file exists, so we'll use the stored credentials
|
|
else:
|
|
# open the file in read-only mode
|
|
async with aiofiles.open(CONFIG_FILE, "r") as f:
|
|
contents = await f.read()
|
|
config = json.loads(contents)
|
|
# Initialize the matrix client based on credentials from file
|
|
client = AsyncClient(
|
|
config["homeserver"],
|
|
config["user_id"],
|
|
device_id=config["device_id"],
|
|
store_path=STORE_PATH,
|
|
config=client_config,
|
|
)
|
|
|
|
client.restore_login(
|
|
user_id=config["user_id"],
|
|
device_id=config["device_id"],
|
|
access_token=config["access_token"],
|
|
)
|
|
print(f"Logged in using stored credentials as {client.user_id}. device_id={client.device_id}")
|
|
|
|
return client
|