signal.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 mautrix.bridge.commands import HelpSection, command_handler
  17. from mausignald.types import Address
  18. from .. import puppet as pu, portal as po
  19. from .typehint import CommandEvent
  20. SECTION_CREATING_PORTALS = HelpSection("Creating portals", 20, "")
  21. remove_extra_chars = str.maketrans("", "", " .,-()")
  22. @command_handler(needs_auth=True, management_only=False, help_section=SECTION_CREATING_PORTALS,
  23. help_text="Open a private chat portal with a specific phone number",
  24. help_args="<_phone_>")
  25. async def pm(evt: CommandEvent) -> None:
  26. if len(evt.args) == 0 or not evt.args[0].startswith("+"):
  27. await evt.reply("**Usage:** `$cmdprefix+sp pm <phone>` "
  28. "(enter phone number in international format)")
  29. return
  30. phone = "".join(evt.args).translate(remove_extra_chars)
  31. if not phone[1:].isdecimal():
  32. await evt.reply("**Usage:** `$cmdprefix+sp pm <phone>` "
  33. "(enter phone number in international format)")
  34. return
  35. puppet = await pu.Puppet.get_by_address(Address(number=phone))
  36. portal = await po.Portal.get_by_chat_id(puppet.address, receiver=evt.sender.username,
  37. create=True)
  38. if portal.mxid:
  39. await evt.reply(f"You already have a private chat with {puppet.name}: "
  40. f"[{portal.mxid}](https://matrix.to/#/{portal.mxid})")
  41. await portal.main_intent.invite_user(portal.mxid, evt.sender.mxid)
  42. return
  43. await portal.create_matrix_room(evt.sender, puppet.address)
  44. await evt.reply(f"Created a portal room with [{puppet.name}](https://matrix.to/#/{puppet.mxid}) and invited you to it")