|
|
|
@ -14,6 +14,11 @@ from typing import (
|
|
|
|
|
Union
|
|
|
|
|
)
|
|
|
|
|
from .async_utils import Aobject
|
|
|
|
|
from .invite_policy import (
|
|
|
|
|
DeclineAll,
|
|
|
|
|
InvitePolicy,
|
|
|
|
|
WhiteList
|
|
|
|
|
)
|
|
|
|
|
from .utils import (
|
|
|
|
|
Room,
|
|
|
|
|
RoomAlias,
|
|
|
|
@ -27,10 +32,6 @@ class Client(Aobject):
|
|
|
|
|
Connect to the matrix server and handle interactions with the
|
|
|
|
|
server.
|
|
|
|
|
|
|
|
|
|
whitelist_rooms: dict of the rooms where the bot is allowed to connect, indexed
|
|
|
|
|
by id (the name starting with '!'). If set to None, the bot connect to
|
|
|
|
|
all room where it is invited.
|
|
|
|
|
|
|
|
|
|
/!\ The client is initialized asyncronously: `client = await Client(...)`
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
@ -41,14 +42,14 @@ class Client(Aobject):
|
|
|
|
|
__sync_token:Optional[str]
|
|
|
|
|
__sync_token_queue: asyncio.Queue[str]
|
|
|
|
|
__invite_queue: asyncio.Queue[tuple[RoomId, nio.responses.InviteInfo]]
|
|
|
|
|
whitelist_rooms: Optional[dict[RoomId, Room]]
|
|
|
|
|
__invite_policy: InvitePolicy
|
|
|
|
|
|
|
|
|
|
async def __init__(
|
|
|
|
|
self,
|
|
|
|
|
username: str,
|
|
|
|
|
homeserver: str,
|
|
|
|
|
password: str,
|
|
|
|
|
whitelist_rooms_names: Optional[list[Union[RoomAlias, RoomId]]]=None,
|
|
|
|
|
invite_policy: Optional[InvitePolicy]=None,
|
|
|
|
|
sync_token_file:str="./.sync_token",
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
@ -56,9 +57,7 @@ class Client(Aobject):
|
|
|
|
|
username: the username used by the bot
|
|
|
|
|
homeserver: the matrix home server of the bot (expl: "https://matrix.org")
|
|
|
|
|
password: the password of the user
|
|
|
|
|
whitelist_rooms: the list of the rooms where the bot is allowed to connect
|
|
|
|
|
(given by room id (expl: '!xxx:matrix.org') of room alias (expl:
|
|
|
|
|
'#xxx:matrix.org'))
|
|
|
|
|
invite_policy: the policy to apply to invitation, default is to decline all.
|
|
|
|
|
sync_token_file: the file where is stored the last sync token received from
|
|
|
|
|
the sync responses. This token avoid reloadind all the history of the
|
|
|
|
|
bot each time we start it.
|
|
|
|
@ -77,18 +76,12 @@ class Client(Aobject):
|
|
|
|
|
self.__sync_token = None
|
|
|
|
|
self.__sync_token_queue = asyncio.Queue()
|
|
|
|
|
self.__invite_queue = asyncio.Queue()
|
|
|
|
|
self.__invite_policy = invite_policy or DeclineAll()
|
|
|
|
|
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:
|
|
|
|
|
self.whitelist_rooms = {}
|
|
|
|
|
rooms = await asyncio.gather(*(self.resolve_room(room_name) for room_name in whitelist_rooms_names))
|
|
|
|
|
for room in rooms:
|
|
|
|
|
self.whitelist_rooms[room.id] = room # room uniqueness is handled by self.resolve_room
|
|
|
|
|
|
|
|
|
|
async def resolve_room(
|
|
|
|
|
self,
|
|
|
|
|
room_name: Union[RoomAlias, RoomId]
|
|
|
|
@ -131,6 +124,15 @@ class Client(Aobject):
|
|
|
|
|
self.__rooms_by_aliases[room_name] = room
|
|
|
|
|
return room
|
|
|
|
|
|
|
|
|
|
def set_invite_policy(
|
|
|
|
|
self,
|
|
|
|
|
invite_policy: InvitePolicy
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Set the invite policy.
|
|
|
|
|
"""
|
|
|
|
|
self.__invite_policy = invite_policy
|
|
|
|
|
|
|
|
|
|
async def send_message(
|
|
|
|
|
self,
|
|
|
|
|
room: Union[RoomAlias, RoomId],
|
|
|
|
@ -160,17 +162,7 @@ class Client(Aobject):
|
|
|
|
|
room_id, invite_info = await self.__invite_queue.get()
|
|
|
|
|
accept_invite = False
|
|
|
|
|
|
|
|
|
|
if self.whitelist_rooms is not None:
|
|
|
|
|
if room_id not in self.whitelist_rooms:
|
|
|
|
|
log.warning(f"Received invite for {room_id}, but room_id is not in the white list.")
|
|
|
|
|
else:
|
|
|
|
|
accept_invite = True
|
|
|
|
|
log.info(f"Received invite for {room_id}: invite accepted.")
|
|
|
|
|
else:
|
|
|
|
|
accept_invite = True
|
|
|
|
|
log.info(f"Received invite for {room_id}: invite accepted.")
|
|
|
|
|
|
|
|
|
|
if accept_invite:
|
|
|
|
|
if (await self.__invite_policy.accept_invite(room_id, invite_info)):
|
|
|
|
|
result = await self.__client.join(room_id)
|
|
|
|
|
if isinstance(result, nio.JoinError):
|
|
|
|
|
log.warning(f"Error while joinning room {room_id}: {result.message}")
|
|
|
|
|