matrix-puppeteer-line/matrix_puppeteer_line/rpc/client.py

173 lines
6.8 KiB
Python
Raw Normal View History

2021-03-15 01:40:56 -04:00
# matrix-puppeteer-line - A very hacky Matrix-LINE bridge based on running LINE's Chrome extension in Puppeteer
2022-03-28 15:00:36 -04:00
# Copyright (C) 2020-2022 Tulir Asokan, Andrew Ferrazzutti
2020-08-28 09:38:06 -04:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import AsyncGenerator, List, Tuple, Dict, Callable, Awaitable, Any
2020-08-28 09:38:06 -04:00
from collections import deque
2021-03-26 02:27:21 -04:00
from base64 import b64decode
2020-08-28 09:38:06 -04:00
import asyncio
from .rpc import RPCClient
from .types import ChatEvents, ChatListInfo, ChatInfo, ImageData, Message, Participant, Receipt, StartStatus
2020-08-28 09:38:06 -04:00
class Client(RPCClient):
async def start(self) -> StartStatus:
await self.connect()
return StartStatus.deserialize(await self.request("start"))
async def stop(self) -> None:
await self.request("stop")
await self.disconnect()
async def pause(self) -> None:
await self.request("pause")
async def resume(self) -> None:
await self.request("resume")
async def get_own_profile(self) -> Participant:
return Participant.deserialize(await self.request("get_own_profile"))
async def get_contacts(self) -> List[Participant]:
resp = await self.request("get_contacts")
return [Participant.deserialize(data) for data in resp]
2020-08-28 09:38:06 -04:00
async def get_chats(self) -> List[ChatListInfo]:
resp = await self.request("get_chats")
return [ChatListInfo.deserialize(data) for data in resp]
async def get_chat(self, chat_id: str, force_view: bool = False) -> ChatInfo:
return ChatInfo.deserialize(await self.request("get_chat", chat_id=chat_id, force_view=force_view))
2020-08-28 09:38:06 -04:00
async def get_messages(self, chat_id: str) -> ChatEvents:
return ChatEvents.deserialize(await self.request("get_messages", chat_id=chat_id))
2020-08-28 09:38:06 -04:00
2021-03-26 02:27:21 -04:00
async def read_image(self, image_url: str) -> ImageData:
resp = await self.request("read_image", image_url=image_url)
if not resp.startswith("data:"):
raise TypeError("Image data is not in the form of a Data URL")
typestart = 5
typeend = resp.find(",", typestart)
data = bytes(resp[typeend+1:], "utf-8")
paramstart = resp.rfind(";", typestart, typeend)
if paramstart == -1:
mime = resp[typestart:typeend]
else:
mime = resp[typestart:paramstart]
if resp[paramstart+1:typeend] == "base64":
data = b64decode(data)
return ImageData(mime=mime, data=data)
async def is_connected(self) -> bool:
resp = await self.request("is_connected")
return resp["is_connected"]
async def send(self, chat_id: str, text: str) -> int:
resp = await self.request("send", chat_id=chat_id, text=text)
return resp["id"]
2020-08-28 09:38:06 -04:00
async def send_file(self, chat_id: str, file_path: str) -> int:
2021-03-27 03:37:41 -04:00
resp = await self.request("send_file", chat_id=chat_id, file_path=file_path)
return resp["id"]
async def set_last_message_ids(self, msg_ids: Dict[str, int], own_msg_ids: Dict[str, int], rct_ids: Dict[str, Dict[int, int]]) -> None:
await self.request("set_last_message_ids", msg_ids=msg_ids, own_msg_ids=own_msg_ids, rct_ids=rct_ids)
2020-08-28 09:38:06 -04:00
2021-07-22 03:20:58 -04:00
async def forget_chat(self, chat_id: str) -> None:
await self.request("forget_chat", chat_id=chat_id)
2020-08-28 09:38:06 -04:00
async def on_message(self, func: Callable[[Message], Awaitable[None]]) -> None:
async def wrapper(data: Dict[str, Any]) -> None:
await func(Message.deserialize(data["message"]))
2020-08-28 09:38:06 -04:00
self.add_event_handler("message", wrapper)
async def on_receipt(self, func: Callable[[Receipt], Awaitable[None]]) -> None:
async def wrapper(data: Dict[str, Any]) -> None:
await func(Receipt.deserialize(data["receipt"]))
self.add_event_handler("receipt", wrapper)
async def on_logged_out(self, func: Callable[[str], Awaitable[None]]) -> None:
async def wrapper(data: Dict[str, str]) -> None:
await func(data.get("message"))
self.add_event_handler("logged_out", wrapper)
2021-02-10 02:34:19 -05:00
# TODO Type hint for sender
async def login(self, sender, **login_data) -> AsyncGenerator[Tuple[str, str], None]:
login_data["login_type"] = sender.command_status["login_type"]
2020-08-28 09:38:06 -04:00
data = deque()
event = asyncio.Event()
async def qr_handler(req: Dict[str, str]) -> None:
2021-02-10 02:34:19 -05:00
data.append(("qr", req["url"]))
event.set()
async def pin_handler(req: Dict[str, str]) -> None:
2021-02-10 02:34:19 -05:00
data.append(("pin", req["pin"]))
event.set()
async def success_handler(req: Dict[str, str]) -> None:
data.append(("login_success", None))
event.set()
async def failure_handler(req: Dict[str, str]) -> None:
data.append(("login_failure", req.get("reason")))
2020-08-28 09:38:06 -04:00
event.set()
2021-02-10 02:34:19 -05:00
async def cancel_watcher() -> None:
try:
while sender.command_status is not None:
await asyncio.sleep(1)
await self._raw_request("cancel_login")
except asyncio.CancelledError:
pass
cancel_watcher_task = asyncio.create_task(cancel_watcher())
2020-08-28 09:38:06 -04:00
def login_handler(_fut: asyncio.Future) -> None:
2021-02-10 02:34:19 -05:00
cancel_watcher_task.cancel()
e = _fut.exception()
if e is not None:
data.append(("error", str(e)))
2020-08-28 09:38:06 -04:00
data.append(None)
event.set()
2021-02-10 02:34:19 -05:00
login_future = await self._raw_request("login", **login_data)
2020-08-28 09:38:06 -04:00
login_future.add_done_callback(login_handler)
self.add_event_handler("qr", qr_handler)
2021-02-10 02:34:19 -05:00
self.add_event_handler("pin", pin_handler)
self.add_event_handler("login_success", success_handler)
self.add_event_handler("login_failure", failure_handler)
2020-08-28 09:38:06 -04:00
try:
while True:
await event.wait()
2020-08-28 12:38:49 -04:00
while len(data) > 0:
item = data.popleft()
2020-08-28 09:38:06 -04:00
if item is None:
return
yield item
event.clear()
finally:
self.remove_event_handler("qr", qr_handler)
2021-02-10 02:34:19 -05:00
self.remove_event_handler("pin", pin_handler)
self.remove_event_handler("login_success", success_handler)
self.remove_event_handler("login_failure", failure_handler)