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
|
2021-02-26 01:28:54 -05:00
|
|
|
# Copyright (C) 2020-2021 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/>.
|
2021-02-10 02:34:19 -05:00
|
|
|
from typing import AsyncGenerator, TypedDict, 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
|
2021-04-20 20:01:50 -04:00
|
|
|
from .types import ChatListInfo, ChatInfo, Message, Receipt, ImageData, StartStatus
|
2020-08-28 09:38:06 -04:00
|
|
|
|
|
|
|
|
2021-02-10 02:34:19 -05:00
|
|
|
class LoginCommand(TypedDict):
|
|
|
|
content: str
|
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()
|
|
|
|
|
2021-04-21 02:38:10 -04:00
|
|
|
async def pause(self) -> None:
|
|
|
|
await self.request("pause")
|
|
|
|
|
|
|
|
async def resume(self) -> None:
|
|
|
|
await self.request("resume")
|
|
|
|
|
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]
|
|
|
|
|
2021-06-11 02:53:30 -04:00
|
|
|
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
|
|
|
|
2021-04-22 02:39:52 -04:00
|
|
|
async def get_messages(self, chat_id: str) -> List[Message]:
|
2020-08-28 09:38:06 -04:00
|
|
|
resp = await self.request("get_messages", chat_id=chat_id)
|
|
|
|
return [Message.deserialize(data) for data in resp]
|
|
|
|
|
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)
|
|
|
|
|
2020-11-03 10:57:51 -05:00
|
|
|
async def is_connected(self) -> bool:
|
|
|
|
resp = await self.request("is_connected")
|
|
|
|
return resp["is_connected"]
|
|
|
|
|
2021-04-22 02:39:52 -04:00
|
|
|
async def send(self, chat_id: str, text: str) -> int:
|
2020-08-28 12:34:13 -04:00
|
|
|
resp = await self.request("send", chat_id=chat_id, text=text)
|
|
|
|
return resp["id"]
|
2020-08-28 09:38:06 -04:00
|
|
|
|
2021-04-22 02:39:52 -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"]
|
|
|
|
|
2021-04-22 02:39:52 -04:00
|
|
|
async def set_last_message_ids(self, msg_ids: Dict[str, int]) -> None:
|
2020-08-28 09:38:06 -04:00
|
|
|
await self.request("set_last_message_ids", msg_ids=msg_ids)
|
|
|
|
|
|
|
|
async def on_message(self, func: Callable[[Message], Awaitable[None]]) -> None:
|
|
|
|
async def wrapper(data: Dict[str, Any]) -> None:
|
2020-08-28 12:34:13 -04:00
|
|
|
await func(Message.deserialize(data["message"]))
|
2020-08-28 09:38:06 -04:00
|
|
|
|
|
|
|
self.add_event_handler("message", wrapper)
|
|
|
|
|
2021-04-20 20:01:50 -04:00
|
|
|
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)
|
|
|
|
|
2021-05-30 17:41:28 -04:00
|
|
|
async def on_logged_out(self, func: Callable[[], Awaitable[None]]) -> None:
|
|
|
|
async def wrapper(data: Dict[str, Any]) -> None:
|
|
|
|
await func()
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2021-02-10 02:34:19 -05:00
|
|
|
async def qr_handler(req: LoginCommand) -> None:
|
|
|
|
data.append(("qr", req["url"]))
|
|
|
|
event.set()
|
|
|
|
|
|
|
|
async def pin_handler(req: LoginCommand) -> None:
|
|
|
|
data.append(("pin", req["pin"]))
|
|
|
|
event.set()
|
|
|
|
|
2021-06-08 00:24:00 -04:00
|
|
|
async def success_handler(req: LoginCommand) -> None:
|
|
|
|
data.append(("login_success", None))
|
|
|
|
event.set()
|
|
|
|
|
2021-02-10 02:34:19 -05:00
|
|
|
async def failure_handler(req: LoginCommand) -> None:
|
2021-06-08 00:24:00 -04:00
|
|
|
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)
|
2021-06-08 00:24:00 -04:00
|
|
|
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)
|
2021-06-08 00:24:00 -04:00
|
|
|
self.remove_event_handler("login_success", success_handler)
|
|
|
|
self.remove_event_handler("login_failure", failure_handler)
|