|
|
|
@ -4,6 +4,7 @@ Connect to the matrix server and handle interactions with the server.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import logging
|
|
|
|
|
import nio
|
|
|
|
|
from aiopath import AsyncPath
|
|
|
|
|
from typing import (
|
|
|
|
@ -18,6 +19,8 @@ from .utils import (
|
|
|
|
|
RoomId
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
class Client(Aobject):
|
|
|
|
|
"""
|
|
|
|
|
Connect to the matrix server and handle interactions with the
|
|
|
|
@ -76,6 +79,7 @@ class Client(Aobject):
|
|
|
|
|
resp = await self.__client.login(password)
|
|
|
|
|
if isinstance(resp, nio.responses.LoginError):
|
|
|
|
|
raise RuntimeError(f"Fail to connect: {resp.message}")
|
|
|
|
|
log.info("logged in")
|
|
|
|
|
|
|
|
|
|
self.whitelist_rooms = None
|
|
|
|
|
if whitelist_rooms_names:
|
|
|
|
@ -146,26 +150,26 @@ class Client(Aobject):
|
|
|
|
|
|
|
|
|
|
if self.whitelist_rooms is not None:
|
|
|
|
|
if room_id not in self.whitelist_rooms:
|
|
|
|
|
print(f"Received invite for {room_id}, but room_id is not in the white list.") # TODO: better logging
|
|
|
|
|
log.warning(f"Received invite for {room_id}, but room_id is not in the white list.")
|
|
|
|
|
else:
|
|
|
|
|
accept_invite = True
|
|
|
|
|
print(f"Received invite for {room_id}: invite accepted.") # TODO: better logging
|
|
|
|
|
log.info(f"Received invite for {room_id}: invite accepted.")
|
|
|
|
|
else:
|
|
|
|
|
accept_invite = True
|
|
|
|
|
print(f"Received invite for {room_id}: invite accepted.") # TODO: better logging
|
|
|
|
|
log.info(f"Received invite for {room_id}: invite accepted.")
|
|
|
|
|
|
|
|
|
|
if accept_invite:
|
|
|
|
|
result = await self.__client.join(room_id)
|
|
|
|
|
if isinstance(result, nio.JoinError):
|
|
|
|
|
print(f"Error while joinning room {room_id}: {result.message}") # TODO: better logging
|
|
|
|
|
log.warning(f"Error while joinning room {room_id}: {result.message}")
|
|
|
|
|
else:
|
|
|
|
|
print(f"{room_id} joined") # TODO: better logging
|
|
|
|
|
log.info(f"{room_id} joined")
|
|
|
|
|
else:
|
|
|
|
|
result = await self.__client.room_leave(room_id)
|
|
|
|
|
if isinstance(result, nio.RoomLeaveError):
|
|
|
|
|
print(f"Error while leaving room {room_id}: {result.message}") # TODO: better logging
|
|
|
|
|
log.warning(f"Error while leaving room {room_id}: {result.message}")
|
|
|
|
|
else:
|
|
|
|
|
print(f"{room_id} left") # TODO: better logging
|
|
|
|
|
log.info(f"{room_id} left")
|
|
|
|
|
|
|
|
|
|
self.__invites_queue.task_done()
|
|
|
|
|
|
|
|
|
@ -183,7 +187,7 @@ class Client(Aobject):
|
|
|
|
|
since=self.__sync_token
|
|
|
|
|
)
|
|
|
|
|
if isinstance(sync_resp, nio.responses.SyncError):
|
|
|
|
|
print(f"Error while syncronizing: {sync_resp.message}") # TODO: use proper logging
|
|
|
|
|
log.warning(f"Error while syncronizing: {sync_resp.message}")
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
self.__sync_token = sync_resp.next_batch
|
|
|
|
|