user.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2021 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, cast
  18. from asyncio.tasks import sleep
  19. from datetime import datetime
  20. from uuid import UUID
  21. import asyncio
  22. from mausignald.errors import AuthorizationFailedError, ProfileUnavailableError
  23. from mausignald.types import (
  24. Account,
  25. Address,
  26. GroupV2,
  27. MessageResendSuccessEvent,
  28. Profile,
  29. WebsocketConnectionState,
  30. WebsocketConnectionStateChangeEvent,
  31. )
  32. from mautrix.appservice import AppService
  33. from mautrix.bridge import AutologinError, BaseUser, async_getter_lock
  34. from mautrix.types import EventType, RoomID, UserID
  35. from mautrix.util import background_task
  36. from mautrix.util.bridge_state import BridgeState, BridgeStateEvent
  37. from mautrix.util.message_send_checkpoint import MessageSendCheckpointStatus
  38. from mautrix.util.opt_prometheus import Gauge
  39. from . import portal as po, puppet as pu
  40. from .config import Config
  41. from .db import Message as DBMessage, User as DBUser
  42. if TYPE_CHECKING:
  43. from .__main__ import SignalBridge
  44. METRIC_CONNECTED = Gauge("bridge_connected", "Bridge users connected to Signal")
  45. METRIC_LOGGED_IN = Gauge("bridge_logged_in", "Bridge users logged into Signal")
  46. BridgeState.human_readable_errors.update(
  47. {
  48. "logged-out": "You're not logged into Signal",
  49. "signal-not-connected": None,
  50. }
  51. )
  52. class User(DBUser, BaseUser):
  53. by_mxid: dict[UserID, User] = {}
  54. by_username: dict[str, User] = {}
  55. by_uuid: dict[UUID, User] = {}
  56. config: Config
  57. az: AppService
  58. loop: asyncio.AbstractEventLoop
  59. bridge: "SignalBridge"
  60. relay_whitelisted: bool
  61. is_admin: bool
  62. permission_level: str
  63. _sync_lock: asyncio.Lock
  64. _notice_room_lock: asyncio.Lock
  65. _connected: bool
  66. _state_id: str | None
  67. _websocket_connection_state: BridgeStateEvent | None
  68. _latest_non_transient_bridge_state: datetime | None
  69. def __init__(
  70. self,
  71. mxid: UserID,
  72. username: str | None = None,
  73. uuid: UUID | None = None,
  74. notice_room: RoomID | None = None,
  75. ) -> None:
  76. super().__init__(mxid=mxid, username=username, uuid=uuid, notice_room=notice_room)
  77. BaseUser.__init__(self)
  78. self._notice_room_lock = asyncio.Lock()
  79. self._sync_lock = asyncio.Lock()
  80. self._connected = False
  81. self._state_id = self.username
  82. self._websocket_connection_state = None
  83. perms = self.config.get_permissions(mxid)
  84. self.relay_whitelisted, self.is_whitelisted, self.is_admin, self.permission_level = perms
  85. @classmethod
  86. def init_cls(cls, bridge: "SignalBridge") -> None:
  87. cls.bridge = bridge
  88. cls.config = bridge.config
  89. cls.az = bridge.az
  90. cls.loop = bridge.loop
  91. @property
  92. def address(self) -> Address | None:
  93. if not self.username:
  94. return None
  95. return Address(uuid=self.uuid, number=self.username)
  96. async def is_logged_in(self) -> bool:
  97. return bool(self.username)
  98. async def needs_relay(self, portal: po.Portal) -> bool:
  99. return not await self.is_logged_in() or (
  100. portal.is_direct and portal.receiver != self.username
  101. )
  102. async def logout(self) -> None:
  103. if not self.username:
  104. return
  105. username = self.username
  106. if self.uuid and self.by_uuid.get(self.uuid) == self:
  107. del self.by_uuid[self.uuid]
  108. if self.username and self.by_username.get(self.username) == self:
  109. del self.by_username[self.username]
  110. self.username = None
  111. self.uuid = None
  112. await self.update()
  113. await self.bridge.signal.unsubscribe(username)
  114. # Wait a while for signald to finish disconnecting
  115. await asyncio.sleep(1)
  116. await self.bridge.signal.delete_account(username)
  117. self._track_metric(METRIC_LOGGED_IN, False)
  118. await self.push_bridge_state(BridgeStateEvent.LOGGED_OUT, remote_id=username)
  119. async def fill_bridge_state(self, state: BridgeState) -> None:
  120. await super().fill_bridge_state(state)
  121. if not state.remote_id:
  122. state.remote_id = self._state_id
  123. if self.address:
  124. puppet = await self.get_puppet()
  125. state.remote_name = puppet.name or self.username
  126. async def get_bridge_states(self) -> list[BridgeState]:
  127. if not self.username:
  128. return []
  129. state = BridgeState(state_event=BridgeStateEvent.UNKNOWN_ERROR)
  130. if self.bridge.signal.is_connected and self._connected:
  131. state.state_event = BridgeStateEvent.CONNECTED
  132. else:
  133. state.state_event = BridgeStateEvent.TRANSIENT_DISCONNECT
  134. return [state]
  135. async def handle_auth_failure(self, e: Exception) -> None:
  136. if isinstance(e, AuthorizationFailedError):
  137. self.username = None
  138. await self.push_bridge_state(BridgeStateEvent.BAD_CREDENTIALS, error=str(e))
  139. async def get_puppet(self) -> pu.Puppet | None:
  140. if not self.address:
  141. return None
  142. return await pu.Puppet.get_by_address(self.address)
  143. async def get_portal_with(self, puppet: pu.Puppet, create: bool = True) -> po.Portal | None:
  144. if not self.username:
  145. return None
  146. return await po.Portal.get_by_chat_id(puppet.uuid, receiver=self.username, create=create)
  147. async def on_signin(self, account: Account) -> None:
  148. self.username = account.account_id
  149. self._state_id = account.account_id
  150. self.uuid = account.address.uuid
  151. self._add_to_cache()
  152. await self.update()
  153. self.log.debug(f"Subscribing to {self.username} / {self.uuid}")
  154. if await self.bridge.signal.subscribe(self.username):
  155. background_task.create(self.sync())
  156. self._track_metric(METRIC_LOGGED_IN, True)
  157. self.log.debug("Successfully subscribed")
  158. else:
  159. self.log.warning("Failed to subscribe")
  160. self.username = None
  161. def on_websocket_connection_state_change(
  162. self, evt: WebsocketConnectionStateChangeEvent
  163. ) -> None:
  164. if evt.state == WebsocketConnectionState.CONNECTED:
  165. self.log.info(f"Connected to Signal (ws: {evt.socket})")
  166. self._track_metric(METRIC_CONNECTED, True)
  167. self._track_metric(METRIC_LOGGED_IN, True)
  168. self._connected = True
  169. else:
  170. if evt.exception:
  171. self.log.error(
  172. f"New {evt.socket} websocket state from signald {evt.state} "
  173. f"with error {evt.exception}"
  174. )
  175. else:
  176. self.log.warning(f"New {evt.socket} websocket state from signald {evt.state}")
  177. self._track_metric(METRIC_CONNECTED, False)
  178. self._connected = False
  179. bridge_state = {
  180. # Signald disconnected
  181. WebsocketConnectionState.SOCKET_DISCONNECTED: BridgeStateEvent.TRANSIENT_DISCONNECT,
  182. # Websocket state reported by signald
  183. WebsocketConnectionState.DISCONNECTED: (
  184. None
  185. if self._websocket_connection_state == BridgeStateEvent.BAD_CREDENTIALS
  186. else BridgeStateEvent.TRANSIENT_DISCONNECT
  187. ),
  188. WebsocketConnectionState.CONNECTING: BridgeStateEvent.CONNECTING,
  189. WebsocketConnectionState.CONNECTED: BridgeStateEvent.CONNECTED,
  190. WebsocketConnectionState.RECONNECTING: BridgeStateEvent.TRANSIENT_DISCONNECT,
  191. WebsocketConnectionState.DISCONNECTING: BridgeStateEvent.TRANSIENT_DISCONNECT,
  192. WebsocketConnectionState.AUTHENTICATION_FAILED: BridgeStateEvent.BAD_CREDENTIALS,
  193. WebsocketConnectionState.FAILED: BridgeStateEvent.TRANSIENT_DISCONNECT,
  194. }.get(evt.state)
  195. if bridge_state is None:
  196. self.log.info(f"Websocket state {evt.state} seen, not reporting new bridge state")
  197. return
  198. now = datetime.now()
  199. if bridge_state in (BridgeStateEvent.TRANSIENT_DISCONNECT, BridgeStateEvent.CONNECTING):
  200. self.log.debug(
  201. f"New bridge state {bridge_state} is likely transient. Waiting 15 seconds to send."
  202. )
  203. async def wait_report_bridge_state():
  204. # Wait for 15 seconds (that should be enough for the bridge to get connected)
  205. # before sending a TRANSIENT_DISCONNECT/CONNECTING.
  206. await sleep(15)
  207. if (
  208. self._latest_non_transient_bridge_state
  209. and now > self._latest_non_transient_bridge_state
  210. ):
  211. background_task.create(self.push_bridge_state(bridge_state))
  212. self._websocket_connection_state = bridge_state
  213. # Wait for another minute. If the bridge stays in TRANSIENT_DISCONNECT/CONNECTING
  214. # for that long, something terrible has happened (signald failed to restart, the
  215. # internet broke, etc.)
  216. await sleep(60)
  217. if (
  218. self._latest_non_transient_bridge_state
  219. and now > self._latest_non_transient_bridge_state
  220. ):
  221. background_task.create(
  222. self.push_bridge_state(
  223. BridgeStateEvent.UNKNOWN_ERROR,
  224. message="Failed to restore connection to Signal",
  225. )
  226. )
  227. self._websocket_connection_state = BridgeStateEvent.UNKNOWN_ERROR
  228. else:
  229. self.log.info(
  230. f"New state since last {bridge_state} push, "
  231. "not transitioning to UNKNOWN_ERROR."
  232. )
  233. background_task.create(wait_report_bridge_state())
  234. elif self._websocket_connection_state == bridge_state:
  235. self.log.info("Websocket state unchanged, not reporting new bridge state")
  236. self._latest_non_transient_bridge_state = now
  237. else:
  238. if bridge_state == BridgeStateEvent.BAD_CREDENTIALS:
  239. self.username = None
  240. background_task.create(self.push_bridge_state(bridge_state))
  241. self._latest_non_transient_bridge_state = now
  242. self._websocket_connection_state = bridge_state
  243. async def on_message_resend_success(self, evt: MessageResendSuccessEvent):
  244. # These messages mean we need to resend the message to that user.
  245. my_uuid = self.address.uuid
  246. self.log.debug(f"Successfully resent message {my_uuid}/{evt.timestamp}")
  247. message = await DBMessage.find_by_sender_timestamp(my_uuid, evt.timestamp)
  248. if not message:
  249. self.log.warning("Couldn't find message that was resent")
  250. return
  251. self.log.debug(f"Successfully resent {message.mxid} in {message.mx_room}")
  252. self.send_remote_checkpoint(
  253. status=MessageSendCheckpointStatus.SUCCESS,
  254. event_id=message.mxid,
  255. room_id=message.mx_room,
  256. event_type=EventType.ROOM_MESSAGE,
  257. )
  258. async def _sync_puppet(self) -> None:
  259. puppet = await self.get_puppet()
  260. if not puppet:
  261. self.log.warning(f"Didn't find puppet for own address {self.address}")
  262. return
  263. if puppet.uuid and not self.uuid:
  264. self.uuid = puppet.uuid
  265. self.by_uuid[self.uuid] = self
  266. if puppet.custom_mxid != self.mxid and puppet.can_auto_login(self.mxid):
  267. self.log.info("Automatically enabling custom puppet")
  268. try:
  269. await puppet.switch_mxid(access_token="auto", mxid=self.mxid)
  270. except AutologinError as e:
  271. self.log.warning(f"Failed to enable custom puppet: {e}")
  272. async def sync(self) -> None:
  273. await self.sync_puppet()
  274. await self.sync_contacts()
  275. await self.sync_groups()
  276. self.log.debug("Sync complete")
  277. async def sync_puppet(self) -> None:
  278. try:
  279. async with self._sync_lock:
  280. await self._sync_puppet()
  281. except Exception:
  282. self.log.exception("Error while syncing own puppet")
  283. async def sync_contacts(self) -> None:
  284. try:
  285. async with self._sync_lock:
  286. await self._sync_contacts()
  287. except Exception as e:
  288. self.log.exception("Error while syncing contacts")
  289. await self.handle_auth_failure(e)
  290. async def sync_groups(self) -> None:
  291. try:
  292. async with self._sync_lock:
  293. await self._sync_groups()
  294. except Exception as e:
  295. self.log.exception("Error while syncing groups")
  296. await self.handle_auth_failure(e)
  297. async def sync_contact(
  298. self, contact: Profile | Address, create_portals: bool = False, use_cache: bool = True
  299. ) -> None:
  300. self.log.trace("Syncing contact %s", contact)
  301. try:
  302. if isinstance(contact, Address):
  303. address = contact
  304. try:
  305. profile = await self.bridge.signal.get_profile(
  306. self.username, address, use_cache=use_cache
  307. )
  308. except ProfileUnavailableError:
  309. self.log.debug(f"Profile of {address} was not available when syncing")
  310. profile = None
  311. if profile and profile.name:
  312. self.log.trace("Got profile for %s: %s", address, profile)
  313. else:
  314. address = contact.address
  315. profile = contact
  316. puppet = await pu.Puppet.get_by_address(address, resolve_via=self.username)
  317. if not puppet:
  318. self.log.debug(f"Didn't find puppet for {address} while syncing contact")
  319. return
  320. await puppet.update_info(profile or address, self)
  321. if create_portals:
  322. portal = await po.Portal.get_by_chat_id(
  323. puppet.uuid, receiver=self.username, create=True
  324. )
  325. await portal.create_matrix_room(self, profile or address)
  326. except Exception as e:
  327. await self.handle_auth_failure(e)
  328. raise
  329. async def _sync_group_v2(self, group: GroupV2, create_portals: bool) -> None:
  330. self.log.trace("Syncing group %s", group.id)
  331. portal = await po.Portal.get_by_chat_id(group.id, create=True)
  332. if create_portals:
  333. await portal.create_matrix_room(self, group)
  334. elif portal.mxid:
  335. await portal.update_matrix_room(self, group)
  336. async def _sync_contacts(self) -> None:
  337. create_contact_portal = self.config["bridge.autocreate_contact_portal"]
  338. for contact in await self.bridge.signal.list_contacts(self.username):
  339. try:
  340. await self.sync_contact(contact, create_contact_portal)
  341. except Exception:
  342. self.log.exception(f"Failed to sync contact {contact.address}")
  343. async def _sync_groups(self) -> None:
  344. create_group_portal = self.config["bridge.autocreate_group_portal"]
  345. for group in await self.bridge.signal.list_groups(self.username):
  346. try:
  347. await self._sync_group_v2(group, create_group_portal)
  348. except Exception:
  349. self.log.exception(f"Failed to sync group {group.id}")
  350. # region Database getters
  351. def _add_to_cache(self) -> None:
  352. self.by_mxid[self.mxid] = self
  353. if self.username:
  354. self.by_username[self.username] = self
  355. if self.uuid:
  356. self.by_uuid[self.uuid] = self
  357. @classmethod
  358. @async_getter_lock
  359. async def get_by_mxid(cls, mxid: UserID, /, *, create: bool = True) -> User | None:
  360. # Never allow ghosts to be users
  361. if pu.Puppet.get_id_from_mxid(mxid):
  362. return None
  363. try:
  364. return cls.by_mxid[mxid]
  365. except KeyError:
  366. pass
  367. user = cast(cls, await super().get_by_mxid(mxid))
  368. if user is not None:
  369. user._add_to_cache()
  370. return user
  371. if create:
  372. user = cls(mxid)
  373. await user.insert()
  374. user._add_to_cache()
  375. return user
  376. return None
  377. @classmethod
  378. @async_getter_lock
  379. async def get_by_username(cls, username: str, /) -> User | None:
  380. try:
  381. return cls.by_username[username]
  382. except KeyError:
  383. pass
  384. user = cast(cls, await super().get_by_username(username))
  385. if user is not None:
  386. user._add_to_cache()
  387. return user
  388. return None
  389. @classmethod
  390. @async_getter_lock
  391. async def get_by_uuid(cls, uuid: UUID, /) -> User | None:
  392. try:
  393. return cls.by_uuid[uuid]
  394. except KeyError:
  395. pass
  396. user = cast(cls, await super().get_by_uuid(uuid))
  397. if user is not None:
  398. user._add_to_cache()
  399. return user
  400. return None
  401. @classmethod
  402. async def get_by_address(cls, address: Address) -> User | None:
  403. if address.uuid:
  404. return await cls.get_by_uuid(address.uuid)
  405. elif address.number:
  406. return await cls.get_by_username(address.number)
  407. else:
  408. raise ValueError("Given address is blank")
  409. @classmethod
  410. async def all_logged_in(cls) -> AsyncGenerator[User, None]:
  411. users = await super().all_logged_in()
  412. user: cls
  413. for user in users:
  414. try:
  415. yield cls.by_mxid[user.mxid]
  416. except KeyError:
  417. user._add_to_cache()
  418. yield user
  419. # endregion