user.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2022 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from __future__ import annotations
  17. from typing import TYPE_CHECKING, AsyncGenerator, AsyncIterable, Awaitable, cast
  18. import asyncio
  19. import logging
  20. import time
  21. from mauigpapi import AndroidAPI, AndroidMQTT, AndroidState
  22. from mauigpapi.errors import (
  23. IGNotLoggedInError,
  24. IGUserIDNotFoundError,
  25. IrisSubscribeError,
  26. MQTTNotConnected,
  27. MQTTNotLoggedIn,
  28. )
  29. from mauigpapi.mqtt import Connect, Disconnect, GraphQLSubscription, SkywalkerSubscription
  30. from mauigpapi.types import (
  31. ActivityIndicatorData,
  32. CurrentUser,
  33. MessageSyncEvent,
  34. Operation,
  35. RealtimeDirectEvent,
  36. Thread,
  37. ThreadSyncEvent,
  38. TypingStatus,
  39. )
  40. from mautrix.appservice import AppService
  41. from mautrix.bridge import BaseUser, async_getter_lock
  42. from mautrix.types import EventID, MessageType, RoomID, TextMessageEventContent, UserID
  43. from mautrix.util.bridge_state import BridgeState, BridgeStateEvent
  44. from mautrix.util.logging import TraceLogger
  45. from mautrix.util.opt_prometheus import Gauge, Summary, async_time
  46. from . import portal as po, puppet as pu
  47. from .config import Config
  48. from .db import Portal as DBPortal, User as DBUser
  49. if TYPE_CHECKING:
  50. from .__main__ import InstagramBridge
  51. METRIC_MESSAGE = Summary("bridge_on_message", "calls to handle_message")
  52. METRIC_THREAD_SYNC = Summary("bridge_on_thread_sync", "calls to handle_thread_sync")
  53. METRIC_RTD = Summary("bridge_on_rtd", "calls to handle_rtd")
  54. METRIC_LOGGED_IN = Gauge("bridge_logged_in", "Users logged into the bridge")
  55. METRIC_CONNECTED = Gauge("bridge_connected", "Bridged users connected to Instagram")
  56. BridgeState.human_readable_errors.update(
  57. {
  58. "ig-connection-error": "Instagram disconnected unexpectedly",
  59. "ig-auth-error": "Authentication error from Instagram: {message}",
  60. "ig-disconnected": None,
  61. "ig-no-mqtt": "You're not connected to Instagram",
  62. "logged-out": "You're not logged into Instagram",
  63. }
  64. )
  65. class User(DBUser, BaseUser):
  66. ig_base_log: TraceLogger = logging.getLogger("mau.instagram")
  67. _activity_indicator_ids: dict[str, int] = {}
  68. by_mxid: dict[UserID, User] = {}
  69. by_igpk: dict[int, User] = {}
  70. config: Config
  71. az: AppService
  72. loop: asyncio.AbstractEventLoop
  73. client: AndroidAPI | None
  74. mqtt: AndroidMQTT | None
  75. _listen_task: asyncio.Task | None = None
  76. permission_level: str
  77. username: str | None
  78. _notice_room_lock: asyncio.Lock
  79. _notice_send_lock: asyncio.Lock
  80. _is_logged_in: bool
  81. _is_connected: bool
  82. shutdown: bool
  83. remote_typing_status: TypingStatus | None
  84. def __init__(
  85. self,
  86. mxid: UserID,
  87. igpk: int | None = None,
  88. state: AndroidState | None = None,
  89. notice_room: RoomID | None = None,
  90. ) -> None:
  91. super().__init__(mxid=mxid, igpk=igpk, state=state, notice_room=notice_room)
  92. BaseUser.__init__(self)
  93. self._notice_room_lock = asyncio.Lock()
  94. self._notice_send_lock = asyncio.Lock()
  95. perms = self.config.get_permissions(mxid)
  96. self.relay_whitelisted, self.is_whitelisted, self.is_admin, self.permission_level = perms
  97. self.client = None
  98. self.mqtt = None
  99. self.username = None
  100. self._is_logged_in = False
  101. self._is_connected = False
  102. self._is_refreshing = False
  103. self.shutdown = False
  104. self._listen_task = None
  105. self.remote_typing_status = None
  106. @classmethod
  107. def init_cls(cls, bridge: "InstagramBridge") -> AsyncIterable[Awaitable[None]]:
  108. cls.bridge = bridge
  109. cls.config = bridge.config
  110. cls.az = bridge.az
  111. cls.loop = bridge.loop
  112. return (user.try_connect() async for user in cls.all_logged_in())
  113. # region Connection management
  114. async def is_logged_in(self) -> bool:
  115. return bool(self.client) and self._is_logged_in
  116. async def get_puppet(self) -> pu.Puppet | None:
  117. if not self.igpk:
  118. return None
  119. return await pu.Puppet.get_by_pk(self.igpk)
  120. async def try_connect(self) -> None:
  121. try:
  122. await self.connect()
  123. except Exception:
  124. self.log.exception("Error while connecting to Instagram")
  125. @property
  126. def api_log(self) -> TraceLogger:
  127. return self.ig_base_log.getChild("http").getChild(self.mxid)
  128. @property
  129. def is_connected(self) -> bool:
  130. return bool(self.client) and bool(self.mqtt) and self._is_connected
  131. async def connect(self) -> None:
  132. client = AndroidAPI(self.state, log=self.api_log)
  133. try:
  134. resp = await client.current_user()
  135. except IGNotLoggedInError as e:
  136. self.log.warning(f"Failed to connect to Instagram: {e}, logging out")
  137. await self.send_bridge_notice(
  138. f"You have been logged out of Instagram: {e!s}",
  139. important=True,
  140. error_code="ig-auth-error",
  141. error_message=str(e),
  142. )
  143. await self.logout(from_error=True)
  144. return
  145. self.client = client
  146. self._is_logged_in = True
  147. self.igpk = resp.user.pk
  148. self.username = resp.user.username
  149. await self.push_bridge_state(BridgeStateEvent.CONNECTING)
  150. self._track_metric(METRIC_LOGGED_IN, True)
  151. self.by_igpk[self.igpk] = self
  152. self.mqtt = AndroidMQTT(
  153. self.state, loop=self.loop, log=self.ig_base_log.getChild("mqtt").getChild(self.mxid)
  154. )
  155. self.mqtt.add_event_handler(Connect, self.on_connect)
  156. self.mqtt.add_event_handler(Disconnect, self.on_disconnect)
  157. self.mqtt.add_event_handler(MessageSyncEvent, self.handle_message)
  158. self.mqtt.add_event_handler(ThreadSyncEvent, self.handle_thread_sync)
  159. self.mqtt.add_event_handler(RealtimeDirectEvent, self.handle_rtd)
  160. await self.update()
  161. self.loop.create_task(self._try_sync_puppet(resp.user))
  162. self.loop.create_task(self._try_sync())
  163. async def on_connect(self, evt: Connect) -> None:
  164. self.log.debug("Connected to Instagram")
  165. self._track_metric(METRIC_CONNECTED, True)
  166. self._is_connected = True
  167. await self.send_bridge_notice("Connected to Instagram")
  168. await self.push_bridge_state(BridgeStateEvent.CONNECTED)
  169. async def on_disconnect(self, evt: Disconnect) -> None:
  170. self.log.debug("Disconnected from Instagram")
  171. self._track_metric(METRIC_CONNECTED, False)
  172. self._is_connected = False
  173. # TODO this stuff could probably be moved to mautrix-python
  174. async def get_notice_room(self) -> RoomID:
  175. if not self.notice_room:
  176. async with self._notice_room_lock:
  177. # If someone already created the room while this call was waiting,
  178. # don't make a new room
  179. if self.notice_room:
  180. return self.notice_room
  181. creation_content = {}
  182. if not self.config["bridge.federate_rooms"]:
  183. creation_content["m.federate"] = False
  184. self.notice_room = await self.az.intent.create_room(
  185. is_direct=True,
  186. invitees=[self.mxid],
  187. topic="Instagram bridge notices",
  188. creation_content=creation_content,
  189. )
  190. await self.update()
  191. return self.notice_room
  192. async def fill_bridge_state(self, state: BridgeState) -> None:
  193. await super().fill_bridge_state(state)
  194. if not state.remote_id:
  195. if self.igpk:
  196. state.remote_id = str(self.igpk)
  197. else:
  198. try:
  199. state.remote_id = self.state.user_id
  200. except IGUserIDNotFoundError:
  201. state.remote_id = None
  202. if self.username:
  203. state.remote_name = f"@{self.username}"
  204. async def get_bridge_states(self) -> list[BridgeState]:
  205. if not self.state:
  206. return []
  207. state = BridgeState(state_event=BridgeStateEvent.UNKNOWN_ERROR)
  208. if self.is_connected:
  209. state.state_event = BridgeStateEvent.CONNECTED
  210. elif self._is_refreshing or self.mqtt:
  211. state.state_event = BridgeStateEvent.TRANSIENT_DISCONNECT
  212. return [state]
  213. async def send_bridge_notice(
  214. self,
  215. text: str,
  216. edit: EventID | None = None,
  217. state_event: BridgeStateEvent | None = None,
  218. important: bool = False,
  219. error_code: str | None = None,
  220. error_message: str | None = None,
  221. ) -> EventID | None:
  222. if state_event:
  223. await self.push_bridge_state(
  224. state_event, error=error_code, message=error_message if error_code else text
  225. )
  226. if self.config["bridge.disable_bridge_notices"]:
  227. return None
  228. if not important and not self.config["bridge.unimportant_bridge_notices"]:
  229. self.log.debug("Not sending unimportant bridge notice: %s", text)
  230. return None
  231. event_id = None
  232. try:
  233. self.log.debug("Sending bridge notice: %s", text)
  234. content = TextMessageEventContent(
  235. body=text, msgtype=(MessageType.TEXT if important else MessageType.NOTICE)
  236. )
  237. if edit:
  238. content.set_edit(edit)
  239. # This is locked to prevent notices going out in the wrong order
  240. async with self._notice_send_lock:
  241. event_id = await self.az.intent.send_message(await self.get_notice_room(), content)
  242. except Exception:
  243. self.log.warning("Failed to send bridge notice", exc_info=True)
  244. return edit or event_id
  245. async def _try_sync_puppet(self, user_info: CurrentUser) -> None:
  246. puppet = await pu.Puppet.get_by_pk(self.igpk)
  247. try:
  248. await puppet.update_info(user_info, self)
  249. except Exception:
  250. self.log.exception("Failed to update own puppet info")
  251. try:
  252. if puppet.custom_mxid != self.mxid and puppet.can_auto_login(self.mxid):
  253. self.log.info(f"Automatically enabling custom puppet")
  254. await puppet.switch_mxid(access_token="auto", mxid=self.mxid)
  255. except Exception:
  256. self.log.exception("Failed to automatically enable custom puppet")
  257. async def _try_sync(self) -> None:
  258. try:
  259. await self.sync()
  260. except Exception:
  261. self.log.exception("Exception while syncing")
  262. await self.push_bridge_state(BridgeStateEvent.UNKNOWN_ERROR)
  263. async def get_direct_chats(self) -> dict[UserID, list[RoomID]]:
  264. return {
  265. pu.Puppet.get_mxid_from_id(portal.other_user_pk): [portal.mxid]
  266. for portal in await DBPortal.find_private_chats_of(self.igpk)
  267. if portal.mxid
  268. }
  269. async def refresh(self, resync: bool = True) -> None:
  270. self._is_refreshing = True
  271. try:
  272. await self.stop_listen()
  273. if resync:
  274. retry_count = 0
  275. while True:
  276. try:
  277. await self.sync()
  278. return
  279. except Exception:
  280. if retry_count >= 4:
  281. raise
  282. retry_count += 1
  283. self.log.exception("Error while syncing for refresh, retrying in 1 minute")
  284. await self.push_bridge_state(BridgeStateEvent.UNKNOWN_ERROR)
  285. await asyncio.sleep(60)
  286. else:
  287. await self.start_listen()
  288. finally:
  289. self._is_refreshing = False
  290. async def _sync_thread(self, thread: Thread, min_active_at: int) -> None:
  291. portal = await po.Portal.get_by_thread(thread, self.igpk)
  292. if portal.mxid:
  293. self.log.debug(f"{thread.thread_id} has a portal, syncing and backfilling...")
  294. await portal.update_matrix_room(self, thread, backfill=True)
  295. elif thread.last_activity_at > min_active_at:
  296. self.log.debug(f"{thread.thread_id} has been active recently, creating portal...")
  297. await portal.create_matrix_room(self, thread)
  298. else:
  299. self.log.debug(f"{thread.thread_id} is not active and doesn't have a portal")
  300. async def sync(self) -> None:
  301. resp = await self.client.get_inbox()
  302. if not self._listen_task:
  303. await self.start_listen(resp.seq_id, resp.snapshot_at_ms)
  304. max_age = self.config["bridge.portal_create_max_age"] * 1_000_000
  305. limit = self.config["bridge.chat_sync_limit"]
  306. min_active_at = (time.time() * 1_000_000) - max_age
  307. i = 0
  308. await self.push_bridge_state(BridgeStateEvent.BACKFILLING)
  309. async for thread in self.client.iter_inbox(start_at=resp):
  310. try:
  311. await self._sync_thread(thread, min_active_at)
  312. except Exception:
  313. self.log.exception(f"Error syncing thread {thread.thread_id}")
  314. i += 1
  315. if i >= limit:
  316. break
  317. try:
  318. await self.update_direct_chats()
  319. except Exception:
  320. self.log.exception("Error updating direct chat list")
  321. async def start_listen(
  322. self, seq_id: int | None = None, snapshot_at_ms: int | None = None
  323. ) -> None:
  324. self.shutdown = False
  325. if not seq_id:
  326. resp = await self.client.get_inbox(limit=1)
  327. seq_id, snapshot_at_ms = resp.seq_id, resp.snapshot_at_ms
  328. task = self.listen(seq_id=seq_id, snapshot_at_ms=snapshot_at_ms)
  329. self._listen_task = self.loop.create_task(task)
  330. async def listen(self, seq_id: int, snapshot_at_ms: int) -> None:
  331. try:
  332. await self.mqtt.listen(
  333. graphql_subs={
  334. GraphQLSubscription.app_presence(),
  335. GraphQLSubscription.direct_typing(self.state.user_id),
  336. GraphQLSubscription.direct_status(),
  337. },
  338. skywalker_subs={
  339. SkywalkerSubscription.direct_sub(self.state.user_id),
  340. SkywalkerSubscription.live_sub(self.state.user_id),
  341. },
  342. seq_id=seq_id,
  343. snapshot_at_ms=snapshot_at_ms,
  344. )
  345. except IrisSubscribeError as e:
  346. self.log.warning(f"Got IrisSubscribeError {e}, refreshing...")
  347. await self.refresh()
  348. except (MQTTNotConnected, MQTTNotLoggedIn) as e:
  349. await self.send_bridge_notice(
  350. f"Error in listener: {e}",
  351. important=True,
  352. state_event=BridgeStateEvent.UNKNOWN_ERROR,
  353. error_code="ig-connection-error",
  354. )
  355. self.mqtt.disconnect()
  356. except Exception:
  357. self.log.exception("Fatal error in listener")
  358. await self.send_bridge_notice(
  359. "Fatal error in listener (see logs for more info)",
  360. state_event=BridgeStateEvent.UNKNOWN_ERROR,
  361. important=True,
  362. error_code="ig-connection-error",
  363. )
  364. self.mqtt.disconnect()
  365. else:
  366. if not self.shutdown:
  367. await self.send_bridge_notice(
  368. "Instagram connection closed without error",
  369. state_event=BridgeStateEvent.UNKNOWN_ERROR,
  370. error_code="ig-disconnected",
  371. )
  372. finally:
  373. self._listen_task = None
  374. self._is_connected = False
  375. self._track_metric(METRIC_CONNECTED, False)
  376. async def stop_listen(self) -> None:
  377. if self.mqtt:
  378. self.shutdown = True
  379. self.mqtt.disconnect()
  380. if self._listen_task:
  381. await self._listen_task
  382. self.shutdown = False
  383. self._track_metric(METRIC_CONNECTED, False)
  384. self._is_connected = False
  385. await self.update()
  386. async def logout(self, from_error: bool = False) -> None:
  387. if self.client:
  388. try:
  389. await self.client.logout(one_tap_app_login=False)
  390. except Exception:
  391. self.log.debug("Exception logging out", exc_info=True)
  392. if self.mqtt:
  393. self.mqtt.disconnect()
  394. self._track_metric(METRIC_CONNECTED, False)
  395. self._track_metric(METRIC_LOGGED_IN, False)
  396. if not from_error:
  397. await self.push_bridge_state(BridgeStateEvent.LOGGED_OUT)
  398. puppet = await pu.Puppet.get_by_pk(self.igpk, create=False)
  399. if puppet and puppet.is_real_user:
  400. await puppet.switch_mxid(None, None)
  401. try:
  402. del self.by_igpk[self.igpk]
  403. except KeyError:
  404. pass
  405. self.igpk = None
  406. else:
  407. await self.push_bridge_state(BridgeStateEvent.BAD_CREDENTIALS)
  408. self.client = None
  409. self.mqtt = None
  410. self.state = None
  411. self._is_logged_in = False
  412. await self.update()
  413. # endregion
  414. # region Event handlers
  415. @async_time(METRIC_MESSAGE)
  416. async def handle_message(self, evt: MessageSyncEvent) -> None:
  417. portal = await po.Portal.get_by_thread_id(evt.message.thread_id, receiver=self.igpk)
  418. if not portal or not portal.mxid:
  419. self.log.debug("Got message in thread with no portal, getting info...")
  420. resp = await self.client.get_thread(evt.message.thread_id)
  421. portal = await po.Portal.get_by_thread(resp.thread, self.igpk)
  422. self.log.debug("Got info for unknown portal, creating room")
  423. await portal.create_matrix_room(self, resp.thread)
  424. if not portal.mxid:
  425. self.log.warning(
  426. "Room creation appears to have failed, "
  427. f"dropping message in {evt.message.thread_id}"
  428. )
  429. return
  430. self.log.trace(f"Received message sync event {evt.message}")
  431. sender = await pu.Puppet.get_by_pk(evt.message.user_id) if evt.message.user_id else None
  432. if evt.message.op == Operation.ADD:
  433. if not sender:
  434. # I don't think we care about adds with no sender
  435. return
  436. await portal.handle_instagram_item(self, sender, evt.message)
  437. elif evt.message.op == Operation.REMOVE:
  438. # Removes don't have a sender, only the message sender can unsend messages anyway
  439. await portal.handle_instagram_remove(evt.message.item_id)
  440. elif evt.message.op == Operation.REPLACE:
  441. await portal.handle_instagram_update(evt.message)
  442. @async_time(METRIC_THREAD_SYNC)
  443. async def handle_thread_sync(self, evt: ThreadSyncEvent) -> None:
  444. self.log.trace("Received thread sync event %s", evt)
  445. portal = await po.Portal.get_by_thread(evt, receiver=self.igpk)
  446. await portal.create_matrix_room(self, evt)
  447. @async_time(METRIC_RTD)
  448. async def handle_rtd(self, evt: RealtimeDirectEvent) -> None:
  449. if not isinstance(evt.value, ActivityIndicatorData):
  450. return
  451. now = int(time.time() * 1000)
  452. date = int(evt.value.timestamp) // 1000
  453. expiry = date + evt.value.ttl
  454. if expiry < now:
  455. return
  456. if evt.activity_indicator_id in self._activity_indicator_ids:
  457. return
  458. # TODO clear expired items from this dict
  459. self._activity_indicator_ids[evt.activity_indicator_id] = expiry
  460. puppet = await pu.Puppet.get_by_pk(int(evt.value.sender_id))
  461. portal = await po.Portal.get_by_thread_id(evt.thread_id, receiver=self.igpk)
  462. if not puppet or not portal or not portal.mxid:
  463. return
  464. is_typing = evt.value.activity_status != TypingStatus.OFF
  465. if puppet.pk == self.igpk:
  466. self.remote_typing_status = TypingStatus.TEXT if is_typing else TypingStatus.OFF
  467. await puppet.intent_for(portal).set_typing(
  468. portal.mxid, is_typing=is_typing, timeout=evt.value.ttl
  469. )
  470. # endregion
  471. # region Database getters
  472. def _add_to_cache(self) -> None:
  473. self.by_mxid[self.mxid] = self
  474. if self.igpk:
  475. self.by_igpk[self.igpk] = self
  476. @classmethod
  477. @async_getter_lock
  478. async def get_by_mxid(cls, mxid: UserID, *, create: bool = True) -> User | None:
  479. # Never allow ghosts to be users
  480. if pu.Puppet.get_id_from_mxid(mxid):
  481. return None
  482. try:
  483. return cls.by_mxid[mxid]
  484. except KeyError:
  485. pass
  486. user = cast(cls, await super().get_by_mxid(mxid))
  487. if user is not None:
  488. user._add_to_cache()
  489. return user
  490. if create:
  491. user = cls(mxid)
  492. await user.insert()
  493. user._add_to_cache()
  494. return user
  495. return None
  496. @classmethod
  497. @async_getter_lock
  498. async def get_by_igpk(cls, igpk: int) -> User | None:
  499. try:
  500. return cls.by_igpk[igpk]
  501. except KeyError:
  502. pass
  503. user = cast(cls, await super().get_by_igpk(igpk))
  504. if user is not None:
  505. user._add_to_cache()
  506. return user
  507. return None
  508. @classmethod
  509. async def all_logged_in(cls) -> AsyncGenerator[User, None]:
  510. users = await super().all_logged_in()
  511. user: cls
  512. for index, user in enumerate(users):
  513. try:
  514. yield cls.by_mxid[user.mxid]
  515. except KeyError:
  516. user._add_to_cache()
  517. yield user
  518. # endregion