signal.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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
  17. import base64
  18. import io
  19. from mautrix.types import MediaMessageEventContent, MessageType, ImageInfo
  20. from mautrix.bridge.commands import HelpSection, command_handler
  21. from mausignald.types import Address
  22. from .. import puppet as pu, portal as po
  23. from .auth import make_qr
  24. from .typehint import CommandEvent
  25. try:
  26. import qrcode
  27. import PIL as _
  28. except ImportError:
  29. qrcode = None
  30. SECTION_SIGNAL = HelpSection("Signal actions", 20, "")
  31. remove_extra_chars = str.maketrans("", "", " .,-()")
  32. async def _get_puppet_from_cmd(evt: CommandEvent) -> Optional['pu.Puppet']:
  33. if len(evt.args) == 0 or not evt.args[0].startswith("+"):
  34. await evt.reply("**Usage:** `$cmdprefix+sp pm <phone>` "
  35. "(enter phone number in international format)")
  36. return None
  37. phone = "".join(evt.args).translate(remove_extra_chars)
  38. if not phone[1:].isdecimal():
  39. await evt.reply("**Usage:** `$cmdprefix+sp pm <phone>` "
  40. "(enter phone number in international format)")
  41. return None
  42. return await pu.Puppet.get_by_address(Address(number=phone))
  43. def _format_safety_number(number: str) -> str:
  44. line_size = 20
  45. chunk_size = 5
  46. return "\n".join(" ".join([number[chunk:chunk + chunk_size]
  47. for chunk in range(line, line + line_size, chunk_size)])
  48. for line in range(0, len(number), line_size))
  49. def _pill(puppet: 'pu.Puppet') -> str:
  50. return f"[{puppet.name}](https://matrix.to/#/{puppet.mxid})"
  51. @command_handler(needs_auth=True, management_only=False, help_section=SECTION_SIGNAL,
  52. help_text="Open a private chat portal with a specific phone number",
  53. help_args="<_phone_>")
  54. async def pm(evt: CommandEvent) -> None:
  55. puppet = await _get_puppet_from_cmd(evt)
  56. if not puppet:
  57. return
  58. portal = await po.Portal.get_by_chat_id(puppet.address, receiver=evt.sender.username,
  59. create=True)
  60. if portal.mxid:
  61. await evt.reply(f"You already have a private chat with {puppet.name}: "
  62. f"[{portal.mxid}](https://matrix.to/#/{portal.mxid})")
  63. await portal.main_intent.invite_user(portal.mxid, evt.sender.mxid)
  64. return
  65. await portal.create_matrix_room(evt.sender, puppet.address)
  66. await evt.reply(f"Created a portal room with {_pill(puppet)} and invited you to it")
  67. @command_handler(needs_auth=True, management_only=False, help_section=SECTION_SIGNAL,
  68. help_text="View the safety number of a specific user",
  69. help_args="[--qr] [_phone_]")
  70. async def safety_number(evt: CommandEvent) -> None:
  71. show_qr = evt.args and evt.args[0].lower() == "--qr"
  72. if show_qr:
  73. if not qrcode:
  74. await evt.reply("Can't generate QR code: qrcode and/or PIL not installed")
  75. return
  76. evt.args = evt.args[1:]
  77. puppet = await _get_puppet_from_cmd(evt)
  78. if not puppet:
  79. return
  80. resp = await evt.bridge.signal.get_identities(evt.sender.username, puppet.address)
  81. if not resp.identities:
  82. await evt.reply(f"No identities found for {_pill(puppet)}")
  83. return
  84. most_recent = resp.identities[0]
  85. for identity in resp.identities:
  86. if identity.added > most_recent.added:
  87. most_recent = identity
  88. uuid = most_recent.address.uuid or "unknown"
  89. await evt.reply(f"### {puppet.name}\n\n"
  90. f"**UUID:** {uuid} \n"
  91. f"**Trust level:** {most_recent.trust_level} \n"
  92. f"**Safety number:**\n"
  93. f"```\n{_format_safety_number(most_recent.safety_number)}\n```")
  94. if show_qr and most_recent.qr_code_data:
  95. data = base64.b64decode(most_recent.qr_code_data)
  96. content = await make_qr(evt.az.intent, data, "verification-qr.png")
  97. await evt.az.intent.send_message(evt.room_id, content)