user.py 19 KB

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