puppet.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2020 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 typing import (Optional, Dict, AsyncIterable, Awaitable, AsyncGenerator, Union,
  17. TYPE_CHECKING, cast)
  18. from uuid import UUID
  19. import asyncio
  20. from yarl import URL
  21. from mausignald.types import Address, Contact, Profile
  22. from mautrix.bridge import BasePuppet
  23. from mautrix.appservice import IntentAPI
  24. from mautrix.types import UserID, SyncToken, RoomID
  25. from mautrix.util.simple_template import SimpleTemplate
  26. from .db import Puppet as DBPuppet
  27. from .config import Config
  28. from . import portal as p
  29. if TYPE_CHECKING:
  30. from .__main__ import SignalBridge
  31. try:
  32. import phonenumbers
  33. except ImportError:
  34. phonenumbers = None
  35. class Puppet(DBPuppet, BasePuppet):
  36. by_uuid: Dict[UUID, 'Puppet'] = {}
  37. by_number: Dict[str, 'Puppet'] = {}
  38. by_custom_mxid: Dict[UserID, 'Puppet'] = {}
  39. hs_domain: str
  40. mxid_template: SimpleTemplate[str]
  41. config: Config
  42. default_mxid_intent: IntentAPI
  43. default_mxid: UserID
  44. _uuid_lock: asyncio.Lock
  45. _update_info_lock: asyncio.Lock
  46. def __init__(self, uuid: Optional[UUID], number: Optional[str], name: Optional[str] = None,
  47. uuid_registered: bool = False, number_registered: bool = False,
  48. custom_mxid: Optional[UserID] = None, access_token: Optional[str] = None,
  49. next_batch: Optional[SyncToken] = None, base_url: Optional[URL] = None) -> None:
  50. super().__init__(uuid=uuid, number=number, name=name, uuid_registered=uuid_registered,
  51. number_registered=number_registered, custom_mxid=custom_mxid,
  52. access_token=access_token, next_batch=next_batch, base_url=base_url)
  53. self.log = self.log.getChild(str(uuid) or number)
  54. self.default_mxid = self.get_mxid_from_id(self.address)
  55. self.default_mxid_intent = self.az.intent.user(self.default_mxid)
  56. self.intent = self._fresh_intent()
  57. self._uuid_lock = asyncio.Lock()
  58. self._update_info_lock = asyncio.Lock()
  59. @classmethod
  60. def init_cls(cls, bridge: 'SignalBridge') -> AsyncIterable[Awaitable[None]]:
  61. cls.config = bridge.config
  62. cls.loop = bridge.loop
  63. cls.mx = bridge.matrix
  64. cls.az = bridge.az
  65. cls.hs_domain = cls.config["homeserver.domain"]
  66. cls.mxid_template = SimpleTemplate(cls.config["bridge.username_template"], "userid",
  67. prefix="@", suffix=f":{cls.hs_domain}", type=str)
  68. cls.sync_with_custom_puppets = cls.config["bridge.sync_with_custom_puppets"]
  69. cls.homeserver_url_map = {server: URL(url) for server, url
  70. in cls.config["bridge.double_puppet_server_map"].items()}
  71. cls.allow_discover_url = cls.config["bridge.double_puppet_allow_discovery"]
  72. cls.login_shared_secret_map = {server: secret.encode("utf-8") for server, secret
  73. in cls.config["bridge.login_shared_secret_map"].items()}
  74. cls.login_device_name = "Signal Bridge"
  75. return (puppet.try_start() async for puppet in cls.all_with_custom_mxid())
  76. def intent_for(self, portal: 'p.Portal') -> IntentAPI:
  77. if portal.chat_id == self.uuid:
  78. return self.default_mxid_intent
  79. return self.intent
  80. @property
  81. def is_registered(self) -> bool:
  82. return self.uuid_registered if self.uuid is not None else self.number_registered
  83. @is_registered.setter
  84. def is_registered(self, value: bool) -> None:
  85. if self.uuid is not None:
  86. self.uuid_registered = value
  87. else:
  88. self.number_registered = value
  89. @property
  90. def address(self) -> Address:
  91. return Address(uuid=self.uuid, number=self.number)
  92. async def handle_uuid_receive(self, uuid: UUID) -> None:
  93. async with self._uuid_lock:
  94. if self.uuid:
  95. # Received UUID was handled while this call was waiting
  96. return
  97. await self._handle_uuid_receive(uuid)
  98. async def _handle_uuid_receive(self, uuid: UUID) -> None:
  99. self.log.debug(f"Found UUID for user: {uuid}")
  100. await self._set_uuid(uuid)
  101. self.by_uuid[self.uuid] = self
  102. prev_intent = self.default_mxid_intent
  103. self.default_mxid = self.get_mxid_from_id(self.address)
  104. self.default_mxid_intent = self.az.intent.user(self.default_mxid)
  105. self.intent = self._fresh_intent()
  106. await self.intent.ensure_registered()
  107. await self.intent.set_displayname(self.name)
  108. self.log = self.log.getChild(str(uuid))
  109. self.log.debug(f"Migrating memberships {prev_intent.mxid} -> {self.default_mxid_intent}")
  110. for room_id in await prev_intent.get_joined_rooms():
  111. await prev_intent.invite_user(room_id, self.default_mxid)
  112. await self.default_mxid_intent.join_room_by_id(room_id)
  113. await prev_intent.leave_room(room_id)
  114. async def update_info(self, info: Union[Profile, Contact]) -> None:
  115. if isinstance(info, (Contact, Address)):
  116. address = info.address if isinstance(info, Contact) else info
  117. if address.uuid and not self.uuid:
  118. await self.handle_uuid_receive(address.uuid)
  119. if not self.config["bridge.allow_contact_list_name_updates"] and self.name is not None:
  120. return
  121. name = info.name if isinstance(info, (Contact, Profile)) else None
  122. async with self._update_info_lock:
  123. update = False
  124. update = await self._update_name(name) or update
  125. if update:
  126. await self.update()
  127. @staticmethod
  128. def fmt_phone(number: str) -> str:
  129. if phonenumbers is None:
  130. return number
  131. parsed = phonenumbers.parse(number)
  132. fmt = phonenumbers.PhoneNumberFormat.INTERNATIONAL
  133. return phonenumbers.format_number(parsed, fmt)
  134. @classmethod
  135. def _get_displayname(cls, address: Address, name: Optional[str]) -> str:
  136. names = name.split("\x00") if name else []
  137. data = {
  138. "first_name": names[0] if len(names) > 0 else "",
  139. "last_name": names[-1] if len(names) > 1 else "",
  140. "full_name": " ".join(names),
  141. "phone": cls.fmt_phone(address.number) if address.number else None,
  142. "uuid": str(address.uuid) if address.uuid else None,
  143. "displayname": "Unknown user",
  144. }
  145. for pref in cls.config["bridge.displayname_preference"]:
  146. value = data.get(pref.replace(" ", "_"))
  147. if value:
  148. data["displayname"] = value
  149. break
  150. return cls.config["bridge.displayname_template"].format(**data)
  151. async def _update_name(self, name: Optional[str]) -> bool:
  152. name = self._get_displayname(self.address, name)
  153. if name != self.name:
  154. self.name = name
  155. await self.default_mxid_intent.set_displayname(self.name)
  156. self.loop.create_task(self._update_portal_names())
  157. return True
  158. return False
  159. async def _update_portal_names(self) -> None:
  160. async for portal in p.Portal.find_private_chats_with(self.uuid):
  161. if portal.receiver == self.number:
  162. # This is a note to self chat, don't change the name
  163. continue
  164. await portal.update_puppet_name(self.name)
  165. async def default_puppet_should_leave_room(self, room_id: RoomID) -> bool:
  166. portal = await p.Portal.get_by_mxid(room_id)
  167. return portal and portal.chat_id != self.uuid
  168. # region Database getters
  169. def _add_to_cache(self) -> None:
  170. if self.uuid:
  171. self.by_uuid[self.uuid] = self
  172. if self.number:
  173. self.by_number[self.number] = self
  174. if self.custom_mxid:
  175. self.by_custom_mxid[self.custom_mxid] = self
  176. async def save(self) -> None:
  177. await self.update()
  178. @classmethod
  179. async def get_by_mxid(cls, mxid: UserID, create: bool = True) -> Optional['Puppet']:
  180. address = cls.get_id_from_mxid(mxid)
  181. if not address:
  182. return None
  183. return await cls.get_by_address(address, create)
  184. @classmethod
  185. async def get_by_custom_mxid(cls, mxid: UserID) -> Optional['Puppet']:
  186. try:
  187. return cls.by_custom_mxid[mxid]
  188. except KeyError:
  189. pass
  190. puppet = cast(cls, await super().get_by_custom_mxid(mxid))
  191. if puppet:
  192. puppet._add_to_cache()
  193. return puppet
  194. return None
  195. @classmethod
  196. def get_id_from_mxid(cls, mxid: UserID) -> Optional[Address]:
  197. identifier = cls.mxid_template.parse(mxid)
  198. if not identifier:
  199. return None
  200. if identifier.startswith("phone_"):
  201. return Address(number="+" + identifier[len("phone_"):])
  202. else:
  203. try:
  204. return Address(uuid=UUID(identifier.upper()))
  205. except ValueError:
  206. return None
  207. @classmethod
  208. def get_mxid_from_id(cls, address: Address) -> UserID:
  209. if address.uuid:
  210. identifier = str(address.uuid).lower()
  211. elif address.number:
  212. identifier = f"phone_{address.number.lstrip('+')}"
  213. else:
  214. raise ValueError("Empty address")
  215. return UserID(cls.mxid_template.format_full(identifier))
  216. @classmethod
  217. async def get_by_address(cls, address: Address, create: bool = True) -> Optional['Puppet']:
  218. puppet = await cls._get_by_address(address, create)
  219. if puppet and address.uuid and not puppet.uuid:
  220. # We found a UUID for this user, store it ASAP
  221. await puppet.handle_uuid_receive(address.uuid)
  222. return puppet
  223. @classmethod
  224. async def _get_by_address(cls, address: Address, create: bool = True) -> Optional['Puppet']:
  225. if not address.is_valid:
  226. raise ValueError("Empty address")
  227. if address.uuid:
  228. try:
  229. return cls.by_uuid[address.uuid]
  230. except KeyError:
  231. pass
  232. if address.number:
  233. try:
  234. return cls.by_number[address.number]
  235. except KeyError:
  236. pass
  237. puppet = cast(cls, await super().get_by_address(address))
  238. if puppet is not None:
  239. puppet._add_to_cache()
  240. return puppet
  241. if create:
  242. puppet = cls(address.uuid, address.number)
  243. await puppet.insert()
  244. puppet._add_to_cache()
  245. return puppet
  246. return None
  247. @classmethod
  248. async def all_with_custom_mxid(cls) -> AsyncGenerator['Puppet', None]:
  249. puppets = await super().all_with_custom_mxid()
  250. puppet: cls
  251. for index, puppet in enumerate(puppets):
  252. try:
  253. yield cls.by_uuid[puppet.uuid]
  254. except KeyError:
  255. try:
  256. yield cls.by_number[puppet.number]
  257. except KeyError:
  258. puppet._add_to_cache()
  259. yield puppet
  260. # endregion